Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_JPEGLoader.cpp 14KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_MSVC
  18. #pragma warning (push)
  19. #pragma warning (disable: 4365)
  20. #endif
  21. namespace jpeglibNamespace
  22. {
  23. #if JUCE_INCLUDE_JPEGLIB_CODE || ! defined (JUCE_INCLUDE_JPEGLIB_CODE)
  24. #if JUCE_MINGW
  25. typedef unsigned char boolean;
  26. #endif
  27. #if JUCE_CLANG
  28. #pragma clang diagnostic push
  29. #pragma clang diagnostic ignored "-Wconversion"
  30. #pragma clang diagnostic ignored "-Wdeprecated-register"
  31. #endif
  32. #define JPEG_INTERNALS
  33. #undef FAR
  34. #include "jpglib/jpeglib.h"
  35. #include "jpglib/jcapimin.c"
  36. #include "jpglib/jcapistd.c"
  37. #include "jpglib/jccoefct.c"
  38. #include "jpglib/jccolor.c"
  39. #undef FIX
  40. #include "jpglib/jcdctmgr.c"
  41. #undef CONST_BITS
  42. #include "jpglib/jchuff.c"
  43. #undef emit_byte
  44. #include "jpglib/jcinit.c"
  45. #include "jpglib/jcmainct.c"
  46. #include "jpglib/jcmarker.c"
  47. #include "jpglib/jcmaster.c"
  48. #include "jpglib/jcomapi.c"
  49. #include "jpglib/jcparam.c"
  50. #include "jpglib/jcphuff.c"
  51. #include "jpglib/jcprepct.c"
  52. #include "jpglib/jcsample.c"
  53. #include "jpglib/jctrans.c"
  54. #include "jpglib/jdapistd.c"
  55. #include "jpglib/jdapimin.c"
  56. #include "jpglib/jdatasrc.c"
  57. #include "jpglib/jdcoefct.c"
  58. #undef FIX
  59. #include "jpglib/jdcolor.c"
  60. #undef FIX
  61. #include "jpglib/jddctmgr.c"
  62. #undef CONST_BITS
  63. #undef ASSIGN_STATE
  64. #include "jpglib/jdhuff.c"
  65. #include "jpglib/jdinput.c"
  66. #include "jpglib/jdmainct.c"
  67. #include "jpglib/jdmarker.c"
  68. #include "jpglib/jdmaster.c"
  69. #undef FIX
  70. #include "jpglib/jdmerge.c"
  71. #undef ASSIGN_STATE
  72. #include "jpglib/jdphuff.c"
  73. #include "jpglib/jdpostct.c"
  74. #undef FIX
  75. #include "jpglib/jdsample.c"
  76. #include "jpglib/jdtrans.c"
  77. #include "jpglib/jfdctflt.c"
  78. #include "jpglib/jfdctint.c"
  79. #undef CONST_BITS
  80. #undef MULTIPLY
  81. #undef FIX_0_541196100
  82. #include "jpglib/jfdctfst.c"
  83. #undef FIX_0_541196100
  84. #include "jpglib/jidctflt.c"
  85. #undef CONST_BITS
  86. #undef FIX_1_847759065
  87. #undef MULTIPLY
  88. #undef DEQUANTIZE
  89. #undef DESCALE
  90. #include "jpglib/jidctfst.c"
  91. #undef CONST_BITS
  92. #undef FIX_1_847759065
  93. #undef MULTIPLY
  94. #undef DEQUANTIZE
  95. #include "jpglib/jidctint.c"
  96. #include "jpglib/jidctred.c"
  97. #include "jpglib/jmemmgr.c"
  98. #include "jpglib/jmemnobs.c"
  99. #include "jpglib/jquant1.c"
  100. #include "jpglib/jquant2.c"
  101. #include "jpglib/jutils.c"
  102. #include "jpglib/transupp.c"
  103. #if JUCE_CLANG
  104. #pragma clang diagnostic pop
  105. #endif
  106. #else
  107. #define JPEG_INTERNALS
  108. #undef FAR
  109. #include <jpeglib.h>
  110. #endif
  111. }
  112. #undef max
  113. #undef min
  114. #if JUCE_MSVC
  115. #pragma warning (pop)
  116. #endif
  117. //==============================================================================
  118. namespace JPEGHelpers
  119. {
  120. using namespace jpeglibNamespace;
  121. #if ! JUCE_MSVC
  122. using jpeglibNamespace::boolean;
  123. #endif
  124. static void fatalErrorHandler (j_common_ptr p) { *((bool*) (p->client_data)) = true; }
  125. static void silentErrorCallback1 (j_common_ptr) {}
  126. static void silentErrorCallback2 (j_common_ptr, int) {}
  127. static void silentErrorCallback3 (j_common_ptr, char*) {}
  128. static void setupSilentErrorHandler (struct jpeg_error_mgr& err)
  129. {
  130. zerostruct (err);
  131. err.error_exit = fatalErrorHandler;
  132. err.emit_message = silentErrorCallback2;
  133. err.output_message = silentErrorCallback1;
  134. err.format_message = silentErrorCallback3;
  135. err.reset_error_mgr = silentErrorCallback1;
  136. }
  137. //==============================================================================
  138. #if ! JUCE_USING_COREIMAGE_LOADER
  139. static void dummyCallback1 (j_decompress_ptr) {}
  140. static void jpegSkip (j_decompress_ptr decompStruct, long num)
  141. {
  142. decompStruct->src->next_input_byte += num;
  143. num = jmin (num, (long) decompStruct->src->bytes_in_buffer);
  144. decompStruct->src->bytes_in_buffer -= (size_t) num;
  145. }
  146. static boolean jpegFill (j_decompress_ptr)
  147. {
  148. return 0;
  149. }
  150. #endif
  151. //==============================================================================
  152. const int jpegBufferSize = 512;
  153. struct JuceJpegDest : public jpeg_destination_mgr
  154. {
  155. OutputStream* output;
  156. char* buffer;
  157. };
  158. static void jpegWriteInit (j_compress_ptr) {}
  159. static void jpegWriteTerminate (j_compress_ptr cinfo)
  160. {
  161. JuceJpegDest* const dest = static_cast <JuceJpegDest*> (cinfo->dest);
  162. const size_t numToWrite = jpegBufferSize - dest->free_in_buffer;
  163. dest->output->write (dest->buffer, numToWrite);
  164. }
  165. static boolean jpegWriteFlush (j_compress_ptr cinfo)
  166. {
  167. JuceJpegDest* const dest = static_cast <JuceJpegDest*> (cinfo->dest);
  168. const int numToWrite = jpegBufferSize;
  169. dest->next_output_byte = reinterpret_cast <JOCTET*> (dest->buffer);
  170. dest->free_in_buffer = jpegBufferSize;
  171. return (boolean) dest->output->write (dest->buffer, (size_t) numToWrite);
  172. }
  173. }
  174. //==============================================================================
  175. JPEGImageFormat::JPEGImageFormat()
  176. : quality (-1.0f)
  177. {
  178. }
  179. JPEGImageFormat::~JPEGImageFormat() {}
  180. void JPEGImageFormat::setQuality (const float newQuality)
  181. {
  182. quality = newQuality;
  183. }
  184. String JPEGImageFormat::getFormatName() { return "JPEG"; }
  185. bool JPEGImageFormat::usesFileExtension (const File& f) { return f.hasFileExtension ("jpeg;jpg"); }
  186. bool JPEGImageFormat::canUnderstand (InputStream& in)
  187. {
  188. const int bytesNeeded = 10;
  189. uint8 header [bytesNeeded];
  190. return in.read (header, bytesNeeded) == bytesNeeded
  191. && header[0] == 0xff
  192. && header[1] == 0xd8
  193. && header[2] == 0xff;
  194. }
  195. #if JUCE_USING_COREIMAGE_LOADER
  196. Image juce_loadWithCoreImage (InputStream& input);
  197. #endif
  198. Image JPEGImageFormat::decodeImage (InputStream& in)
  199. {
  200. #if JUCE_USING_COREIMAGE_LOADER
  201. return juce_loadWithCoreImage (in);
  202. #else
  203. using namespace jpeglibNamespace;
  204. using namespace JPEGHelpers;
  205. MemoryOutputStream mb;
  206. mb << in;
  207. Image image;
  208. if (mb.getDataSize() > 16)
  209. {
  210. struct jpeg_decompress_struct jpegDecompStruct;
  211. struct jpeg_error_mgr jerr;
  212. setupSilentErrorHandler (jerr);
  213. jpegDecompStruct.err = &jerr;
  214. jpeg_create_decompress (&jpegDecompStruct);
  215. jpegDecompStruct.src = (jpeg_source_mgr*)(jpegDecompStruct.mem->alloc_small)
  216. ((j_common_ptr)(&jpegDecompStruct), JPOOL_PERMANENT, sizeof (jpeg_source_mgr));
  217. bool hasFailed = false;
  218. jpegDecompStruct.client_data = &hasFailed;
  219. jpegDecompStruct.src->init_source = dummyCallback1;
  220. jpegDecompStruct.src->fill_input_buffer = jpegFill;
  221. jpegDecompStruct.src->skip_input_data = jpegSkip;
  222. jpegDecompStruct.src->resync_to_restart = jpeg_resync_to_restart;
  223. jpegDecompStruct.src->term_source = dummyCallback1;
  224. jpegDecompStruct.src->next_input_byte = static_cast <const unsigned char*> (mb.getData());
  225. jpegDecompStruct.src->bytes_in_buffer = mb.getDataSize();
  226. jpeg_read_header (&jpegDecompStruct, TRUE);
  227. if (! hasFailed)
  228. {
  229. jpeg_calc_output_dimensions (&jpegDecompStruct);
  230. if (! hasFailed)
  231. {
  232. const int width = (int) jpegDecompStruct.output_width;
  233. const int height = (int) jpegDecompStruct.output_height;
  234. jpegDecompStruct.out_color_space = JCS_RGB;
  235. JSAMPARRAY buffer
  236. = (*jpegDecompStruct.mem->alloc_sarray) ((j_common_ptr) &jpegDecompStruct,
  237. JPOOL_IMAGE,
  238. (JDIMENSION) width * 3, 1);
  239. if (jpeg_start_decompress (&jpegDecompStruct) && ! hasFailed)
  240. {
  241. image = Image (Image::RGB, width, height, false);
  242. image.getProperties()->set ("originalImageHadAlpha", false);
  243. const bool hasAlphaChan = image.hasAlphaChannel(); // (the native image creator may not give back what we expect)
  244. const Image::BitmapData destData (image, Image::BitmapData::writeOnly);
  245. for (int y = 0; y < height; ++y)
  246. {
  247. jpeg_read_scanlines (&jpegDecompStruct, buffer, 1);
  248. if (hasFailed)
  249. break;
  250. const uint8* src = *buffer;
  251. uint8* dest = destData.getLinePointer (y);
  252. if (hasAlphaChan)
  253. {
  254. for (int i = width; --i >= 0;)
  255. {
  256. ((PixelARGB*) dest)->setARGB (0xff, src[0], src[1], src[2]);
  257. ((PixelARGB*) dest)->premultiply();
  258. dest += destData.pixelStride;
  259. src += 3;
  260. }
  261. }
  262. else
  263. {
  264. for (int i = width; --i >= 0;)
  265. {
  266. ((PixelRGB*) dest)->setARGB (0xff, src[0], src[1], src[2]);
  267. dest += destData.pixelStride;
  268. src += 3;
  269. }
  270. }
  271. }
  272. if (! hasFailed)
  273. jpeg_finish_decompress (&jpegDecompStruct);
  274. in.setPosition (((char*) jpegDecompStruct.src->next_input_byte) - (char*) mb.getData());
  275. }
  276. }
  277. }
  278. jpeg_destroy_decompress (&jpegDecompStruct);
  279. }
  280. return image;
  281. #endif
  282. }
  283. bool JPEGImageFormat::writeImageToStream (const Image& image, OutputStream& out)
  284. {
  285. using namespace jpeglibNamespace;
  286. using namespace JPEGHelpers;
  287. jpeg_compress_struct jpegCompStruct;
  288. zerostruct (jpegCompStruct);
  289. jpeg_create_compress (&jpegCompStruct);
  290. struct jpeg_error_mgr jerr;
  291. setupSilentErrorHandler (jerr);
  292. jpegCompStruct.err = &jerr;
  293. JuceJpegDest dest;
  294. jpegCompStruct.dest = &dest;
  295. dest.output = &out;
  296. HeapBlock <char> tempBuffer (jpegBufferSize);
  297. dest.buffer = tempBuffer;
  298. dest.next_output_byte = (JOCTET*) dest.buffer;
  299. dest.free_in_buffer = jpegBufferSize;
  300. dest.init_destination = jpegWriteInit;
  301. dest.empty_output_buffer = jpegWriteFlush;
  302. dest.term_destination = jpegWriteTerminate;
  303. jpegCompStruct.image_width = (JDIMENSION) image.getWidth();
  304. jpegCompStruct.image_height = (JDIMENSION) image.getHeight();
  305. jpegCompStruct.input_components = 3;
  306. jpegCompStruct.in_color_space = JCS_RGB;
  307. jpegCompStruct.write_JFIF_header = 1;
  308. jpegCompStruct.X_density = 72;
  309. jpegCompStruct.Y_density = 72;
  310. jpeg_set_defaults (&jpegCompStruct);
  311. jpegCompStruct.dct_method = JDCT_FLOAT;
  312. jpegCompStruct.optimize_coding = 1;
  313. if (quality < 0.0f)
  314. quality = 0.85f;
  315. jpeg_set_quality (&jpegCompStruct, jlimit (0, 100, roundToInt (quality * 100.0f)), TRUE);
  316. jpeg_start_compress (&jpegCompStruct, TRUE);
  317. const int strideBytes = (int) (jpegCompStruct.image_width * (unsigned int) jpegCompStruct.input_components);
  318. JSAMPARRAY buffer = (*jpegCompStruct.mem->alloc_sarray) ((j_common_ptr) &jpegCompStruct,
  319. JPOOL_IMAGE, (JDIMENSION) strideBytes, 1);
  320. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  321. while (jpegCompStruct.next_scanline < jpegCompStruct.image_height)
  322. {
  323. uint8* dst = *buffer;
  324. if (srcData.pixelFormat == Image::RGB)
  325. {
  326. const uint8* src = srcData.getLinePointer ((int) jpegCompStruct.next_scanline);
  327. for (int i = srcData.width; --i >= 0;)
  328. {
  329. *dst++ = ((const PixelRGB*) src)->getRed();
  330. *dst++ = ((const PixelRGB*) src)->getGreen();
  331. *dst++ = ((const PixelRGB*) src)->getBlue();
  332. src += srcData.pixelStride;
  333. }
  334. }
  335. else
  336. {
  337. for (int x = 0; x < srcData.width; ++x)
  338. {
  339. const Colour pixel (srcData.getPixelColour (x, (int) jpegCompStruct.next_scanline));
  340. *dst++ = pixel.getRed();
  341. *dst++ = pixel.getGreen();
  342. *dst++ = pixel.getBlue();
  343. }
  344. }
  345. jpeg_write_scanlines (&jpegCompStruct, buffer, 1);
  346. }
  347. jpeg_finish_compress (&jpegCompStruct);
  348. jpeg_destroy_compress (&jpegCompStruct);
  349. return true;
  350. }