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.

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