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.

447 lines
14KB

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