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.

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