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.

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