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.

424 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. static void fatalErrorHandler (j_common_ptr) { throw JPEGDecodingFailure(); }
  119. static void silentErrorCallback1 (j_common_ptr) {}
  120. static void silentErrorCallback2 (j_common_ptr, int) {}
  121. static void silentErrorCallback3 (j_common_ptr, char*) {}
  122. static 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. #if ! JUCE_USING_COREIMAGE_LOADER
  133. static void dummyCallback1 (j_decompress_ptr) {}
  134. static void jpegSkip (j_decompress_ptr decompStruct, long num)
  135. {
  136. decompStruct->src->next_input_byte += num;
  137. num = jmin (num, (long) decompStruct->src->bytes_in_buffer);
  138. decompStruct->src->bytes_in_buffer -= num;
  139. }
  140. static boolean jpegFill (j_decompress_ptr)
  141. {
  142. return 0;
  143. }
  144. #endif
  145. //==============================================================================
  146. const int jpegBufferSize = 512;
  147. struct JuceJpegDest : public jpeg_destination_mgr
  148. {
  149. OutputStream* output;
  150. char* buffer;
  151. };
  152. static void jpegWriteInit (j_compress_ptr) {}
  153. static void jpegWriteTerminate (j_compress_ptr cinfo)
  154. {
  155. JuceJpegDest* const dest = static_cast <JuceJpegDest*> (cinfo->dest);
  156. const size_t numToWrite = jpegBufferSize - dest->free_in_buffer;
  157. dest->output->write (dest->buffer, (int) numToWrite);
  158. }
  159. static boolean jpegWriteFlush (j_compress_ptr cinfo)
  160. {
  161. JuceJpegDest* const dest = static_cast <JuceJpegDest*> (cinfo->dest);
  162. const int numToWrite = jpegBufferSize;
  163. dest->next_output_byte = reinterpret_cast <JOCTET*> (dest->buffer);
  164. dest->free_in_buffer = jpegBufferSize;
  165. return (boolean) dest->output->write (dest->buffer, numToWrite);
  166. }
  167. }
  168. //==============================================================================
  169. JPEGImageFormat::JPEGImageFormat()
  170. : quality (-1.0f)
  171. {
  172. }
  173. JPEGImageFormat::~JPEGImageFormat() {}
  174. void JPEGImageFormat::setQuality (const float newQuality)
  175. {
  176. quality = newQuality;
  177. }
  178. String JPEGImageFormat::getFormatName() { return "JPEG"; }
  179. bool JPEGImageFormat::canUnderstand (InputStream& in)
  180. {
  181. const int bytesNeeded = 10;
  182. uint8 header [bytesNeeded];
  183. if (in.read (header, bytesNeeded) == bytesNeeded)
  184. {
  185. return header[0] == 0xff
  186. && header[1] == 0xd8
  187. && header[2] == 0xff
  188. && (header[3] == 0xe0 || header[3] == 0xe1);
  189. }
  190. return false;
  191. }
  192. #if JUCE_USING_COREIMAGE_LOADER
  193. Image juce_loadWithCoreImage (InputStream& input);
  194. #endif
  195. Image JPEGImageFormat::decodeImage (InputStream& in)
  196. {
  197. #if JUCE_USING_COREIMAGE_LOADER
  198. return juce_loadWithCoreImage (in);
  199. #else
  200. using namespace jpeglibNamespace;
  201. using namespace JPEGHelpers;
  202. MemoryOutputStream mb;
  203. mb << in;
  204. Image image;
  205. if (mb.getDataSize() > 16)
  206. {
  207. struct jpeg_decompress_struct jpegDecompStruct;
  208. struct jpeg_error_mgr jerr;
  209. setupSilentErrorHandler (jerr);
  210. jpegDecompStruct.err = &jerr;
  211. jpeg_create_decompress (&jpegDecompStruct);
  212. jpegDecompStruct.src = (jpeg_source_mgr*)(jpegDecompStruct.mem->alloc_small)
  213. ((j_common_ptr)(&jpegDecompStruct), JPOOL_PERMANENT, sizeof (jpeg_source_mgr));
  214. jpegDecompStruct.src->init_source = dummyCallback1;
  215. jpegDecompStruct.src->fill_input_buffer = jpegFill;
  216. jpegDecompStruct.src->skip_input_data = jpegSkip;
  217. jpegDecompStruct.src->resync_to_restart = jpeg_resync_to_restart;
  218. jpegDecompStruct.src->term_source = dummyCallback1;
  219. jpegDecompStruct.src->next_input_byte = static_cast <const unsigned char*> (mb.getData());
  220. jpegDecompStruct.src->bytes_in_buffer = mb.getDataSize();
  221. try
  222. {
  223. jpeg_read_header (&jpegDecompStruct, TRUE);
  224. jpeg_calc_output_dimensions (&jpegDecompStruct);
  225. const int width = (int) jpegDecompStruct.output_width;
  226. const int height = (int) jpegDecompStruct.output_height;
  227. jpegDecompStruct.out_color_space = JCS_RGB;
  228. JSAMPARRAY buffer
  229. = (*jpegDecompStruct.mem->alloc_sarray) ((j_common_ptr) &jpegDecompStruct,
  230. JPOOL_IMAGE,
  231. (JDIMENSION) width * 3, 1);
  232. if (jpeg_start_decompress (&jpegDecompStruct))
  233. {
  234. image = Image (Image::RGB, width, height, false);
  235. image.getProperties()->set ("originalImageHadAlpha", false);
  236. const bool hasAlphaChan = image.hasAlphaChannel(); // (the native image creator may not give back what we expect)
  237. const Image::BitmapData destData (image, Image::BitmapData::writeOnly);
  238. for (int y = 0; y < height; ++y)
  239. {
  240. jpeg_read_scanlines (&jpegDecompStruct, buffer, 1);
  241. const uint8* src = *buffer;
  242. uint8* dest = destData.getLinePointer (y);
  243. if (hasAlphaChan)
  244. {
  245. for (int i = width; --i >= 0;)
  246. {
  247. ((PixelARGB*) dest)->setARGB (0xff, src[0], src[1], src[2]);
  248. ((PixelARGB*) dest)->premultiply();
  249. dest += destData.pixelStride;
  250. src += 3;
  251. }
  252. }
  253. else
  254. {
  255. for (int i = width; --i >= 0;)
  256. {
  257. ((PixelRGB*) dest)->setARGB (0xff, src[0], src[1], src[2]);
  258. dest += destData.pixelStride;
  259. src += 3;
  260. }
  261. }
  262. }
  263. jpeg_finish_decompress (&jpegDecompStruct);
  264. in.setPosition (((char*) jpegDecompStruct.src->next_input_byte) - (char*) mb.getData());
  265. }
  266. jpeg_destroy_decompress (&jpegDecompStruct);
  267. }
  268. catch (...)
  269. {}
  270. }
  271. return image;
  272. #endif
  273. }
  274. bool JPEGImageFormat::writeImageToStream (const Image& image, OutputStream& out)
  275. {
  276. using namespace jpeglibNamespace;
  277. using namespace JPEGHelpers;
  278. struct jpeg_compress_struct jpegCompStruct;
  279. jpeg_create_compress (&jpegCompStruct);
  280. struct jpeg_error_mgr jerr;
  281. setupSilentErrorHandler (jerr);
  282. jpegCompStruct.err = &jerr;
  283. JuceJpegDest dest;
  284. jpegCompStruct.dest = &dest;
  285. dest.output = &out;
  286. HeapBlock <char> tempBuffer (jpegBufferSize);
  287. dest.buffer = tempBuffer;
  288. dest.next_output_byte = (JOCTET*) dest.buffer;
  289. dest.free_in_buffer = jpegBufferSize;
  290. dest.init_destination = jpegWriteInit;
  291. dest.empty_output_buffer = jpegWriteFlush;
  292. dest.term_destination = jpegWriteTerminate;
  293. jpegCompStruct.image_width = (JDIMENSION) image.getWidth();
  294. jpegCompStruct.image_height = (JDIMENSION) image.getHeight();
  295. jpegCompStruct.input_components = 3;
  296. jpegCompStruct.in_color_space = JCS_RGB;
  297. jpegCompStruct.write_JFIF_header = 1;
  298. jpegCompStruct.X_density = 72;
  299. jpegCompStruct.Y_density = 72;
  300. jpeg_set_defaults (&jpegCompStruct);
  301. jpegCompStruct.dct_method = JDCT_FLOAT;
  302. jpegCompStruct.optimize_coding = 1;
  303. if (quality < 0.0f)
  304. quality = 0.85f;
  305. jpeg_set_quality (&jpegCompStruct, jlimit (0, 100, roundToInt (quality * 100.0f)), TRUE);
  306. jpeg_start_compress (&jpegCompStruct, TRUE);
  307. const int strideBytes = (int) (jpegCompStruct.image_width * jpegCompStruct.input_components);
  308. JSAMPARRAY buffer = (*jpegCompStruct.mem->alloc_sarray) ((j_common_ptr) &jpegCompStruct,
  309. JPOOL_IMAGE, (JDIMENSION) strideBytes, 1);
  310. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  311. while (jpegCompStruct.next_scanline < jpegCompStruct.image_height)
  312. {
  313. uint8* dst = *buffer;
  314. if (srcData.pixelFormat == Image::RGB)
  315. {
  316. const uint8* src = srcData.getLinePointer ((int) jpegCompStruct.next_scanline);
  317. for (int i = srcData.width; --i >= 0;)
  318. {
  319. *dst++ = ((const PixelRGB*) src)->getRed();
  320. *dst++ = ((const PixelRGB*) src)->getGreen();
  321. *dst++ = ((const PixelRGB*) src)->getBlue();
  322. src += srcData.pixelStride;
  323. }
  324. }
  325. else
  326. {
  327. for (int x = 0; x < srcData.width; ++x)
  328. {
  329. const Colour pixel (srcData.getPixelColour (x, (int) jpegCompStruct.next_scanline));
  330. *dst++ = pixel.getRed();
  331. *dst++ = pixel.getGreen();
  332. *dst++ = pixel.getBlue();
  333. }
  334. }
  335. jpeg_write_scanlines (&jpegCompStruct, buffer, 1);
  336. }
  337. jpeg_finish_compress (&jpegCompStruct);
  338. jpeg_destroy_compress (&jpegCompStruct);
  339. return true;
  340. }