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.

474 lines
15KB

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