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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. struct JPEGDecodingFailure {};
  125. static void fatalErrorHandler (j_common_ptr) { throw JPEGDecodingFailure(); }
  126. static void silentErrorCallback1 (j_common_ptr) {}
  127. static void silentErrorCallback2 (j_common_ptr, int) {}
  128. static void silentErrorCallback3 (j_common_ptr, char*) {}
  129. static void setupSilentErrorHandler (struct jpeg_error_mgr& err)
  130. {
  131. zerostruct (err);
  132. err.error_exit = fatalErrorHandler;
  133. err.emit_message = silentErrorCallback2;
  134. err.output_message = silentErrorCallback1;
  135. err.format_message = silentErrorCallback3;
  136. err.reset_error_mgr = silentErrorCallback1;
  137. }
  138. //==============================================================================
  139. #if ! JUCE_USING_COREIMAGE_LOADER
  140. static void dummyCallback1 (j_decompress_ptr) {}
  141. static void jpegSkip (j_decompress_ptr decompStruct, long num)
  142. {
  143. decompStruct->src->next_input_byte += num;
  144. num = jmin (num, (long) decompStruct->src->bytes_in_buffer);
  145. decompStruct->src->bytes_in_buffer -= (size_t) num;
  146. }
  147. static boolean jpegFill (j_decompress_ptr)
  148. {
  149. return 0;
  150. }
  151. #endif
  152. //==============================================================================
  153. const int jpegBufferSize = 512;
  154. struct JuceJpegDest : public jpeg_destination_mgr
  155. {
  156. OutputStream* output;
  157. char* buffer;
  158. };
  159. static void jpegWriteInit (j_compress_ptr) {}
  160. static void jpegWriteTerminate (j_compress_ptr cinfo)
  161. {
  162. JuceJpegDest* const dest = static_cast <JuceJpegDest*> (cinfo->dest);
  163. const size_t numToWrite = jpegBufferSize - dest->free_in_buffer;
  164. dest->output->write (dest->buffer, numToWrite);
  165. }
  166. static boolean jpegWriteFlush (j_compress_ptr cinfo)
  167. {
  168. JuceJpegDest* const dest = static_cast <JuceJpegDest*> (cinfo->dest);
  169. const int numToWrite = jpegBufferSize;
  170. dest->next_output_byte = reinterpret_cast <JOCTET*> (dest->buffer);
  171. dest->free_in_buffer = jpegBufferSize;
  172. return (boolean) dest->output->write (dest->buffer, (size_t) numToWrite);
  173. }
  174. }
  175. //==============================================================================
  176. JPEGImageFormat::JPEGImageFormat()
  177. : quality (-1.0f)
  178. {
  179. }
  180. JPEGImageFormat::~JPEGImageFormat() {}
  181. void JPEGImageFormat::setQuality (const float newQuality)
  182. {
  183. quality = newQuality;
  184. }
  185. String JPEGImageFormat::getFormatName() { return "JPEG"; }
  186. bool JPEGImageFormat::usesFileExtension (const File& f) { return f.hasFileExtension ("jpeg;jpg"); }
  187. bool JPEGImageFormat::canUnderstand (InputStream& in)
  188. {
  189. const int bytesNeeded = 10;
  190. uint8 header [bytesNeeded];
  191. return in.read (header, bytesNeeded) == bytesNeeded
  192. && header[0] == 0xff
  193. && header[1] == 0xd8
  194. && header[2] == 0xff;
  195. }
  196. #if JUCE_USING_COREIMAGE_LOADER
  197. Image juce_loadWithCoreImage (InputStream& input);
  198. #endif
  199. Image JPEGImageFormat::decodeImage (InputStream& in)
  200. {
  201. #if JUCE_USING_COREIMAGE_LOADER
  202. return juce_loadWithCoreImage (in);
  203. #else
  204. using namespace jpeglibNamespace;
  205. using namespace JPEGHelpers;
  206. MemoryOutputStream mb;
  207. mb << in;
  208. Image image;
  209. if (mb.getDataSize() > 16)
  210. {
  211. struct jpeg_decompress_struct jpegDecompStruct;
  212. struct jpeg_error_mgr jerr;
  213. setupSilentErrorHandler (jerr);
  214. jpegDecompStruct.err = &jerr;
  215. jpeg_create_decompress (&jpegDecompStruct);
  216. jpegDecompStruct.src = (jpeg_source_mgr*)(jpegDecompStruct.mem->alloc_small)
  217. ((j_common_ptr)(&jpegDecompStruct), JPOOL_PERMANENT, sizeof (jpeg_source_mgr));
  218. jpegDecompStruct.src->init_source = dummyCallback1;
  219. jpegDecompStruct.src->fill_input_buffer = jpegFill;
  220. jpegDecompStruct.src->skip_input_data = jpegSkip;
  221. jpegDecompStruct.src->resync_to_restart = jpeg_resync_to_restart;
  222. jpegDecompStruct.src->term_source = dummyCallback1;
  223. jpegDecompStruct.src->next_input_byte = static_cast <const unsigned char*> (mb.getData());
  224. jpegDecompStruct.src->bytes_in_buffer = mb.getDataSize();
  225. try
  226. {
  227. jpeg_read_header (&jpegDecompStruct, TRUE);
  228. jpeg_calc_output_dimensions (&jpegDecompStruct);
  229. const int width = (int) jpegDecompStruct.output_width;
  230. const int height = (int) jpegDecompStruct.output_height;
  231. jpegDecompStruct.out_color_space = JCS_RGB;
  232. JSAMPARRAY buffer
  233. = (*jpegDecompStruct.mem->alloc_sarray) ((j_common_ptr) &jpegDecompStruct,
  234. JPOOL_IMAGE,
  235. (JDIMENSION) width * 3, 1);
  236. if (jpeg_start_decompress (&jpegDecompStruct))
  237. {
  238. image = Image (Image::RGB, width, height, false);
  239. image.getProperties()->set ("originalImageHadAlpha", false);
  240. const bool hasAlphaChan = image.hasAlphaChannel(); // (the native image creator may not give back what we expect)
  241. const Image::BitmapData destData (image, Image::BitmapData::writeOnly);
  242. for (int y = 0; y < height; ++y)
  243. {
  244. jpeg_read_scanlines (&jpegDecompStruct, buffer, 1);
  245. const uint8* src = *buffer;
  246. uint8* dest = destData.getLinePointer (y);
  247. if (hasAlphaChan)
  248. {
  249. for (int i = width; --i >= 0;)
  250. {
  251. ((PixelARGB*) dest)->setARGB (0xff, src[0], src[1], src[2]);
  252. ((PixelARGB*) dest)->premultiply();
  253. dest += destData.pixelStride;
  254. src += 3;
  255. }
  256. }
  257. else
  258. {
  259. for (int i = width; --i >= 0;)
  260. {
  261. ((PixelRGB*) dest)->setARGB (0xff, src[0], src[1], src[2]);
  262. dest += destData.pixelStride;
  263. src += 3;
  264. }
  265. }
  266. }
  267. jpeg_finish_decompress (&jpegDecompStruct);
  268. in.setPosition (((char*) jpegDecompStruct.src->next_input_byte) - (char*) mb.getData());
  269. }
  270. jpeg_destroy_decompress (&jpegDecompStruct);
  271. }
  272. catch (...)
  273. {}
  274. }
  275. return image;
  276. #endif
  277. }
  278. bool JPEGImageFormat::writeImageToStream (const Image& image, OutputStream& out)
  279. {
  280. using namespace jpeglibNamespace;
  281. using namespace JPEGHelpers;
  282. jpeg_compress_struct jpegCompStruct;
  283. zerostruct (jpegCompStruct);
  284. jpeg_create_compress (&jpegCompStruct);
  285. struct jpeg_error_mgr jerr;
  286. setupSilentErrorHandler (jerr);
  287. jpegCompStruct.err = &jerr;
  288. JuceJpegDest dest;
  289. jpegCompStruct.dest = &dest;
  290. dest.output = &out;
  291. HeapBlock <char> tempBuffer (jpegBufferSize);
  292. dest.buffer = tempBuffer;
  293. dest.next_output_byte = (JOCTET*) dest.buffer;
  294. dest.free_in_buffer = jpegBufferSize;
  295. dest.init_destination = jpegWriteInit;
  296. dest.empty_output_buffer = jpegWriteFlush;
  297. dest.term_destination = jpegWriteTerminate;
  298. jpegCompStruct.image_width = (JDIMENSION) image.getWidth();
  299. jpegCompStruct.image_height = (JDIMENSION) image.getHeight();
  300. jpegCompStruct.input_components = 3;
  301. jpegCompStruct.in_color_space = JCS_RGB;
  302. jpegCompStruct.write_JFIF_header = 1;
  303. jpegCompStruct.X_density = 72;
  304. jpegCompStruct.Y_density = 72;
  305. jpeg_set_defaults (&jpegCompStruct);
  306. jpegCompStruct.dct_method = JDCT_FLOAT;
  307. jpegCompStruct.optimize_coding = 1;
  308. if (quality < 0.0f)
  309. quality = 0.85f;
  310. jpeg_set_quality (&jpegCompStruct, jlimit (0, 100, roundToInt (quality * 100.0f)), TRUE);
  311. jpeg_start_compress (&jpegCompStruct, TRUE);
  312. const int strideBytes = (int) (jpegCompStruct.image_width * (unsigned int) jpegCompStruct.input_components);
  313. JSAMPARRAY buffer = (*jpegCompStruct.mem->alloc_sarray) ((j_common_ptr) &jpegCompStruct,
  314. JPOOL_IMAGE, (JDIMENSION) strideBytes, 1);
  315. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  316. while (jpegCompStruct.next_scanline < jpegCompStruct.image_height)
  317. {
  318. uint8* dst = *buffer;
  319. if (srcData.pixelFormat == Image::RGB)
  320. {
  321. const uint8* src = srcData.getLinePointer ((int) jpegCompStruct.next_scanline);
  322. for (int i = srcData.width; --i >= 0;)
  323. {
  324. *dst++ = ((const PixelRGB*) src)->getRed();
  325. *dst++ = ((const PixelRGB*) src)->getGreen();
  326. *dst++ = ((const PixelRGB*) src)->getBlue();
  327. src += srcData.pixelStride;
  328. }
  329. }
  330. else
  331. {
  332. for (int x = 0; x < srcData.width; ++x)
  333. {
  334. const Colour pixel (srcData.getPixelColour (x, (int) jpegCompStruct.next_scanline));
  335. *dst++ = pixel.getRed();
  336. *dst++ = pixel.getGreen();
  337. *dst++ = pixel.getBlue();
  338. }
  339. }
  340. jpeg_write_scanlines (&jpegCompStruct, buffer, 1);
  341. }
  342. jpeg_finish_compress (&jpegCompStruct);
  343. jpeg_destroy_compress (&jpegCompStruct);
  344. return true;
  345. }