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.

422 lines
13KB

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