The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

457 lines
14KB

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