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

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