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.

353 lines
11KB

  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: 4390 4611 4365 4267)
  21. #ifdef __INTEL_COMPILER
  22. #pragma warning (disable: 2544 2545)
  23. #endif
  24. #endif
  25. namespace zlibNamespace
  26. {
  27. #if JUCE_INCLUDE_ZLIB_CODE
  28. #undef OS_CODE
  29. #undef fdopen
  30. #include "../../juce_core/zip/zlib/zlib.h"
  31. #undef OS_CODE
  32. #else
  33. #include JUCE_ZLIB_INCLUDE_PATH
  34. #endif
  35. }
  36. namespace pnglibNamespace
  37. {
  38. using namespace zlibNamespace;
  39. #if JUCE_INCLUDE_PNGLIB_CODE || ! defined (JUCE_INCLUDE_PNGLIB_CODE)
  40. #if _MSC_VER != 1310
  41. using std::calloc; // (causes conflict in VS.NET 2003)
  42. using std::malloc;
  43. using std::free;
  44. #endif
  45. using std::abs;
  46. #define PNG_INTERNAL
  47. #define NO_DUMMY_DECL
  48. #define PNG_SETJMP_NOT_SUPPORTED
  49. #include "pnglib/png.h"
  50. #include "pnglib/pngconf.h"
  51. #define PNG_NO_EXTERN
  52. #include "pnglib/png.c"
  53. #include "pnglib/pngerror.c"
  54. #include "pnglib/pngget.c"
  55. #include "pnglib/pngmem.c"
  56. #include "pnglib/pngread.c"
  57. #include "pnglib/pngpread.c"
  58. #include "pnglib/pngrio.c"
  59. #include "pnglib/pngrtran.c"
  60. #include "pnglib/pngrutil.c"
  61. #include "pnglib/pngset.c"
  62. #include "pnglib/pngtrans.c"
  63. #include "pnglib/pngwio.c"
  64. #include "pnglib/pngwrite.c"
  65. #include "pnglib/pngwtran.c"
  66. #include "pnglib/pngwutil.c"
  67. #else
  68. extern "C"
  69. {
  70. #include <png.h>
  71. #include <pngconf.h>
  72. }
  73. #endif
  74. }
  75. #undef max
  76. #undef min
  77. #if JUCE_MSVC
  78. #pragma warning (pop)
  79. #endif
  80. //==============================================================================
  81. namespace PNGHelpers
  82. {
  83. using namespace pnglibNamespace;
  84. static void JUCE_CDECL writeDataCallback (png_structp png, png_bytep data, png_size_t length)
  85. {
  86. static_cast<OutputStream*> (png_get_io_ptr (png))->write (data, (int) length);
  87. }
  88. #if ! JUCE_USING_COREIMAGE_LOADER
  89. static void JUCE_CDECL readCallback (png_structp png, png_bytep data, png_size_t length)
  90. {
  91. static_cast<InputStream*> (png_get_io_ptr (png))->read (data, (int) length);
  92. }
  93. struct PNGErrorStruct {};
  94. static void JUCE_CDECL errorCallback (png_structp, png_const_charp)
  95. {
  96. throw PNGErrorStruct();
  97. }
  98. #endif
  99. }
  100. //==============================================================================
  101. PNGImageFormat::PNGImageFormat() {}
  102. PNGImageFormat::~PNGImageFormat() {}
  103. String PNGImageFormat::getFormatName() { return "PNG"; }
  104. bool PNGImageFormat::canUnderstand (InputStream& in)
  105. {
  106. const int bytesNeeded = 4;
  107. char header [bytesNeeded];
  108. return in.read (header, bytesNeeded) == bytesNeeded
  109. && header[1] == 'P'
  110. && header[2] == 'N'
  111. && header[3] == 'G';
  112. }
  113. #if JUCE_USING_COREIMAGE_LOADER
  114. Image juce_loadWithCoreImage (InputStream& input);
  115. #endif
  116. Image PNGImageFormat::decodeImage (InputStream& in)
  117. {
  118. #if JUCE_USING_COREIMAGE_LOADER
  119. return juce_loadWithCoreImage (in);
  120. #else
  121. using namespace pnglibNamespace;
  122. Image image;
  123. png_structp pngReadStruct;
  124. png_infop pngInfoStruct;
  125. pngReadStruct = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
  126. if (pngReadStruct != 0)
  127. {
  128. try
  129. {
  130. pngInfoStruct = png_create_info_struct (pngReadStruct);
  131. if (pngInfoStruct == 0)
  132. {
  133. png_destroy_read_struct (&pngReadStruct, 0, 0);
  134. return Image::null;
  135. }
  136. png_set_error_fn (pngReadStruct, 0, PNGHelpers::errorCallback, PNGHelpers::errorCallback );
  137. // read the header..
  138. png_set_read_fn (pngReadStruct, &in, PNGHelpers::readCallback);
  139. png_uint_32 width, height;
  140. int bitDepth, colorType, interlaceType;
  141. png_read_info (pngReadStruct, pngInfoStruct);
  142. png_get_IHDR (pngReadStruct, pngInfoStruct,
  143. &width, &height,
  144. &bitDepth, &colorType,
  145. &interlaceType, 0, 0);
  146. if (bitDepth == 16)
  147. png_set_strip_16 (pngReadStruct);
  148. if (colorType == PNG_COLOR_TYPE_PALETTE)
  149. png_set_expand (pngReadStruct);
  150. if (bitDepth < 8)
  151. png_set_expand (pngReadStruct);
  152. if (png_get_valid (pngReadStruct, pngInfoStruct, PNG_INFO_tRNS))
  153. png_set_expand (pngReadStruct);
  154. if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
  155. png_set_gray_to_rgb (pngReadStruct);
  156. png_set_add_alpha (pngReadStruct, 0xff, PNG_FILLER_AFTER);
  157. bool hasAlphaChan = (colorType & PNG_COLOR_MASK_ALPHA) != 0
  158. || pngInfoStruct->num_trans > 0;
  159. // Load the image into a temp buffer in the pnglib format..
  160. HeapBlock <uint8> tempBuffer (height * (width << 2));
  161. {
  162. HeapBlock <png_bytep> rows (height);
  163. for (int y = (int) height; --y >= 0;)
  164. rows[y] = (png_bytep) (tempBuffer + (width << 2) * y);
  165. try
  166. {
  167. png_read_image (pngReadStruct, rows);
  168. png_read_end (pngReadStruct, pngInfoStruct);
  169. }
  170. catch (PNGHelpers::PNGErrorStruct&)
  171. {}
  172. }
  173. png_destroy_read_struct (&pngReadStruct, &pngInfoStruct, 0);
  174. // now convert the data to a juce image format..
  175. image = Image (hasAlphaChan ? Image::ARGB : Image::RGB,
  176. (int) width, (int) height, hasAlphaChan);
  177. image.getProperties()->set ("originalImageHadAlpha", image.hasAlphaChannel());
  178. hasAlphaChan = image.hasAlphaChannel(); // (the native image creator may not give back what we expect)
  179. const Image::BitmapData destData (image, Image::BitmapData::writeOnly);
  180. uint8* srcRow = tempBuffer;
  181. uint8* destRow = destData.data;
  182. for (int y = 0; y < (int) height; ++y)
  183. {
  184. const uint8* src = srcRow;
  185. srcRow += (width << 2);
  186. uint8* dest = destRow;
  187. destRow += destData.lineStride;
  188. if (hasAlphaChan)
  189. {
  190. for (int i = (int) width; --i >= 0;)
  191. {
  192. ((PixelARGB*) dest)->setARGB (src[3], src[0], src[1], src[2]);
  193. ((PixelARGB*) dest)->premultiply();
  194. dest += destData.pixelStride;
  195. src += 4;
  196. }
  197. }
  198. else
  199. {
  200. for (int i = (int) width; --i >= 0;)
  201. {
  202. ((PixelRGB*) dest)->setARGB (0, src[0], src[1], src[2]);
  203. dest += destData.pixelStride;
  204. src += 4;
  205. }
  206. }
  207. }
  208. }
  209. catch (PNGHelpers::PNGErrorStruct&)
  210. {}
  211. }
  212. return image;
  213. #endif
  214. }
  215. bool PNGImageFormat::writeImageToStream (const Image& image, OutputStream& out)
  216. {
  217. using namespace pnglibNamespace;
  218. const int width = image.getWidth();
  219. const int height = image.getHeight();
  220. png_structp pngWriteStruct = png_create_write_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
  221. if (pngWriteStruct == 0)
  222. return false;
  223. png_infop pngInfoStruct = png_create_info_struct (pngWriteStruct);
  224. if (pngInfoStruct == 0)
  225. {
  226. png_destroy_write_struct (&pngWriteStruct, (png_infopp) 0);
  227. return false;
  228. }
  229. png_set_write_fn (pngWriteStruct, &out, PNGHelpers::writeDataCallback, 0);
  230. png_set_IHDR (pngWriteStruct, pngInfoStruct, (png_uint_32) width, (png_uint_32) height, 8,
  231. image.hasAlphaChannel() ? PNG_COLOR_TYPE_RGB_ALPHA
  232. : PNG_COLOR_TYPE_RGB,
  233. PNG_INTERLACE_NONE,
  234. PNG_COMPRESSION_TYPE_BASE,
  235. PNG_FILTER_TYPE_BASE);
  236. HeapBlock <uint8> rowData ((size_t) width * 4);
  237. png_color_8 sig_bit;
  238. sig_bit.red = 8;
  239. sig_bit.green = 8;
  240. sig_bit.blue = 8;
  241. sig_bit.alpha = 8;
  242. png_set_sBIT (pngWriteStruct, pngInfoStruct, &sig_bit);
  243. png_write_info (pngWriteStruct, pngInfoStruct);
  244. png_set_shift (pngWriteStruct, &sig_bit);
  245. png_set_packing (pngWriteStruct);
  246. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  247. for (int y = 0; y < height; ++y)
  248. {
  249. uint8* dst = rowData;
  250. const uint8* src = srcData.getLinePointer (y);
  251. if (image.hasAlphaChannel())
  252. {
  253. for (int i = width; --i >= 0;)
  254. {
  255. PixelARGB p (*(const PixelARGB*) src);
  256. p.unpremultiply();
  257. *dst++ = p.getRed();
  258. *dst++ = p.getGreen();
  259. *dst++ = p.getBlue();
  260. *dst++ = p.getAlpha();
  261. src += srcData.pixelStride;
  262. }
  263. }
  264. else
  265. {
  266. for (int i = width; --i >= 0;)
  267. {
  268. *dst++ = ((const PixelRGB*) src)->getRed();
  269. *dst++ = ((const PixelRGB*) src)->getGreen();
  270. *dst++ = ((const PixelRGB*) src)->getBlue();
  271. src += srcData.pixelStride;
  272. }
  273. }
  274. png_bytep rowPtr = rowData;
  275. png_write_rows (pngWriteStruct, &rowPtr, 1);
  276. }
  277. png_write_end (pngWriteStruct, pngInfoStruct);
  278. png_destroy_write_struct (&pngWriteStruct, &pngInfoStruct);
  279. return true;
  280. }