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.

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