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.

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