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.

436 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #if JUCE_MSVC
  19. #pragma warning (push)
  20. #pragma warning (disable: 4365)
  21. #endif
  22. namespace jpeglibNamespace
  23. {
  24. #if JUCE_INCLUDE_JPEGLIB_CODE || ! defined (JUCE_INCLUDE_JPEGLIB_CODE)
  25. #if JUCE_MINGW
  26. typedef unsigned char boolean;
  27. #endif
  28. #if JUCE_CLANG
  29. #pragma clang diagnostic push
  30. #pragma clang diagnostic ignored "-Wconversion"
  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_MSVC
  122. using jpeglibNamespace::boolean;
  123. #endif
  124. struct JPEGDecodingFailure {};
  125. static void fatalErrorHandler (j_common_ptr) { throw JPEGDecodingFailure(); }
  126. static void silentErrorCallback1 (j_common_ptr) {}
  127. static void silentErrorCallback2 (j_common_ptr, int) {}
  128. static void silentErrorCallback3 (j_common_ptr, char*) {}
  129. static void setupSilentErrorHandler (struct jpeg_error_mgr& err)
  130. {
  131. zerostruct (err);
  132. err.error_exit = fatalErrorHandler;
  133. err.emit_message = silentErrorCallback2;
  134. err.output_message = silentErrorCallback1;
  135. err.format_message = silentErrorCallback3;
  136. err.reset_error_mgr = silentErrorCallback1;
  137. }
  138. //==============================================================================
  139. #if ! JUCE_USING_COREIMAGE_LOADER
  140. static void dummyCallback1 (j_decompress_ptr) {}
  141. static void jpegSkip (j_decompress_ptr decompStruct, long num)
  142. {
  143. decompStruct->src->next_input_byte += num;
  144. num = jmin (num, (long) decompStruct->src->bytes_in_buffer);
  145. decompStruct->src->bytes_in_buffer -= num;
  146. }
  147. static boolean jpegFill (j_decompress_ptr)
  148. {
  149. return 0;
  150. }
  151. #endif
  152. //==============================================================================
  153. const int jpegBufferSize = 512;
  154. struct JuceJpegDest : public jpeg_destination_mgr
  155. {
  156. OutputStream* output;
  157. char* buffer;
  158. };
  159. static void jpegWriteInit (j_compress_ptr) {}
  160. static void jpegWriteTerminate (j_compress_ptr cinfo)
  161. {
  162. JuceJpegDest* const dest = static_cast <JuceJpegDest*> (cinfo->dest);
  163. const size_t numToWrite = jpegBufferSize - dest->free_in_buffer;
  164. dest->output->write (dest->buffer, numToWrite);
  165. }
  166. static boolean jpegWriteFlush (j_compress_ptr cinfo)
  167. {
  168. JuceJpegDest* const dest = static_cast <JuceJpegDest*> (cinfo->dest);
  169. const int numToWrite = jpegBufferSize;
  170. dest->next_output_byte = reinterpret_cast <JOCTET*> (dest->buffer);
  171. dest->free_in_buffer = jpegBufferSize;
  172. return (boolean) dest->output->write (dest->buffer, (size_t) numToWrite);
  173. }
  174. }
  175. //==============================================================================
  176. JPEGImageFormat::JPEGImageFormat()
  177. : quality (-1.0f)
  178. {
  179. }
  180. JPEGImageFormat::~JPEGImageFormat() {}
  181. void JPEGImageFormat::setQuality (const float newQuality)
  182. {
  183. quality = newQuality;
  184. }
  185. String JPEGImageFormat::getFormatName() { return "JPEG"; }
  186. bool JPEGImageFormat::usesFileExtension (const File& f) { return f.hasFileExtension ("jpeg;jpg"); }
  187. bool JPEGImageFormat::canUnderstand (InputStream& in)
  188. {
  189. const int bytesNeeded = 10;
  190. uint8 header [bytesNeeded];
  191. if (in.read (header, bytesNeeded) == bytesNeeded)
  192. {
  193. return header[0] == 0xff
  194. && header[1] == 0xd8
  195. && header[2] == 0xff
  196. && (header[3] == 0xe0 || header[3] == 0xe1);
  197. }
  198. return false;
  199. }
  200. #if JUCE_USING_COREIMAGE_LOADER
  201. Image juce_loadWithCoreImage (InputStream& input);
  202. #endif
  203. Image JPEGImageFormat::decodeImage (InputStream& in)
  204. {
  205. #if JUCE_USING_COREIMAGE_LOADER
  206. return juce_loadWithCoreImage (in);
  207. #else
  208. using namespace jpeglibNamespace;
  209. using namespace JPEGHelpers;
  210. MemoryOutputStream mb;
  211. mb << in;
  212. Image image;
  213. if (mb.getDataSize() > 16)
  214. {
  215. struct jpeg_decompress_struct jpegDecompStruct;
  216. struct jpeg_error_mgr jerr;
  217. setupSilentErrorHandler (jerr);
  218. jpegDecompStruct.err = &jerr;
  219. jpeg_create_decompress (&jpegDecompStruct);
  220. jpegDecompStruct.src = (jpeg_source_mgr*)(jpegDecompStruct.mem->alloc_small)
  221. ((j_common_ptr)(&jpegDecompStruct), JPOOL_PERMANENT, sizeof (jpeg_source_mgr));
  222. jpegDecompStruct.src->init_source = dummyCallback1;
  223. jpegDecompStruct.src->fill_input_buffer = jpegFill;
  224. jpegDecompStruct.src->skip_input_data = jpegSkip;
  225. jpegDecompStruct.src->resync_to_restart = jpeg_resync_to_restart;
  226. jpegDecompStruct.src->term_source = dummyCallback1;
  227. jpegDecompStruct.src->next_input_byte = static_cast <const unsigned char*> (mb.getData());
  228. jpegDecompStruct.src->bytes_in_buffer = mb.getDataSize();
  229. try
  230. {
  231. jpeg_read_header (&jpegDecompStruct, TRUE);
  232. jpeg_calc_output_dimensions (&jpegDecompStruct);
  233. const int width = (int) jpegDecompStruct.output_width;
  234. const int height = (int) jpegDecompStruct.output_height;
  235. jpegDecompStruct.out_color_space = JCS_RGB;
  236. JSAMPARRAY buffer
  237. = (*jpegDecompStruct.mem->alloc_sarray) ((j_common_ptr) &jpegDecompStruct,
  238. JPOOL_IMAGE,
  239. (JDIMENSION) width * 3, 1);
  240. if (jpeg_start_decompress (&jpegDecompStruct))
  241. {
  242. image = Image (Image::RGB, width, height, false);
  243. image.getProperties()->set ("originalImageHadAlpha", false);
  244. const bool hasAlphaChan = image.hasAlphaChannel(); // (the native image creator may not give back what we expect)
  245. const Image::BitmapData destData (image, Image::BitmapData::writeOnly);
  246. for (int y = 0; y < height; ++y)
  247. {
  248. jpeg_read_scanlines (&jpegDecompStruct, buffer, 1);
  249. const uint8* src = *buffer;
  250. uint8* dest = destData.getLinePointer (y);
  251. if (hasAlphaChan)
  252. {
  253. for (int i = width; --i >= 0;)
  254. {
  255. ((PixelARGB*) dest)->setARGB (0xff, src[0], src[1], src[2]);
  256. ((PixelARGB*) dest)->premultiply();
  257. dest += destData.pixelStride;
  258. src += 3;
  259. }
  260. }
  261. else
  262. {
  263. for (int i = width; --i >= 0;)
  264. {
  265. ((PixelRGB*) dest)->setARGB (0xff, src[0], src[1], src[2]);
  266. dest += destData.pixelStride;
  267. src += 3;
  268. }
  269. }
  270. }
  271. jpeg_finish_decompress (&jpegDecompStruct);
  272. in.setPosition (((char*) jpegDecompStruct.src->next_input_byte) - (char*) mb.getData());
  273. }
  274. jpeg_destroy_decompress (&jpegDecompStruct);
  275. }
  276. catch (...)
  277. {}
  278. }
  279. return image;
  280. #endif
  281. }
  282. bool JPEGImageFormat::writeImageToStream (const Image& image, OutputStream& out)
  283. {
  284. using namespace jpeglibNamespace;
  285. using namespace JPEGHelpers;
  286. struct jpeg_compress_struct jpegCompStruct;
  287. jpeg_create_compress (&jpegCompStruct);
  288. struct jpeg_error_mgr jerr;
  289. setupSilentErrorHandler (jerr);
  290. jpegCompStruct.err = &jerr;
  291. JuceJpegDest dest;
  292. jpegCompStruct.dest = &dest;
  293. dest.output = &out;
  294. HeapBlock <char> tempBuffer (jpegBufferSize);
  295. dest.buffer = tempBuffer;
  296. dest.next_output_byte = (JOCTET*) dest.buffer;
  297. dest.free_in_buffer = jpegBufferSize;
  298. dest.init_destination = jpegWriteInit;
  299. dest.empty_output_buffer = jpegWriteFlush;
  300. dest.term_destination = jpegWriteTerminate;
  301. jpegCompStruct.image_width = (JDIMENSION) image.getWidth();
  302. jpegCompStruct.image_height = (JDIMENSION) image.getHeight();
  303. jpegCompStruct.input_components = 3;
  304. jpegCompStruct.in_color_space = JCS_RGB;
  305. jpegCompStruct.write_JFIF_header = 1;
  306. jpegCompStruct.X_density = 72;
  307. jpegCompStruct.Y_density = 72;
  308. jpeg_set_defaults (&jpegCompStruct);
  309. jpegCompStruct.dct_method = JDCT_FLOAT;
  310. jpegCompStruct.optimize_coding = 1;
  311. if (quality < 0.0f)
  312. quality = 0.85f;
  313. jpeg_set_quality (&jpegCompStruct, jlimit (0, 100, roundToInt (quality * 100.0f)), TRUE);
  314. jpeg_start_compress (&jpegCompStruct, TRUE);
  315. const int strideBytes = (int) (jpegCompStruct.image_width * (unsigned int) jpegCompStruct.input_components);
  316. JSAMPARRAY buffer = (*jpegCompStruct.mem->alloc_sarray) ((j_common_ptr) &jpegCompStruct,
  317. JPOOL_IMAGE, (JDIMENSION) strideBytes, 1);
  318. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  319. while (jpegCompStruct.next_scanline < jpegCompStruct.image_height)
  320. {
  321. uint8* dst = *buffer;
  322. if (srcData.pixelFormat == Image::RGB)
  323. {
  324. const uint8* src = srcData.getLinePointer ((int) jpegCompStruct.next_scanline);
  325. for (int i = srcData.width; --i >= 0;)
  326. {
  327. *dst++ = ((const PixelRGB*) src)->getRed();
  328. *dst++ = ((const PixelRGB*) src)->getGreen();
  329. *dst++ = ((const PixelRGB*) src)->getBlue();
  330. src += srcData.pixelStride;
  331. }
  332. }
  333. else
  334. {
  335. for (int x = 0; x < srcData.width; ++x)
  336. {
  337. const Colour pixel (srcData.getPixelColour (x, (int) jpegCompStruct.next_scanline));
  338. *dst++ = pixel.getRed();
  339. *dst++ = pixel.getGreen();
  340. *dst++ = pixel.getBlue();
  341. }
  342. }
  343. jpeg_write_scanlines (&jpegCompStruct, buffer, 1);
  344. }
  345. jpeg_finish_compress (&jpegCompStruct);
  346. jpeg_destroy_compress (&jpegCompStruct);
  347. return true;
  348. }