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.

351 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 readCallback (png_structp png, png_bytep data, png_size_t length)
  85. {
  86. static_cast<InputStream*> (png_get_io_ptr (png))->read (data, (int) length);
  87. }
  88. static void JUCE_CDECL writeDataCallback (png_structp png, png_bytep data, png_size_t length)
  89. {
  90. static_cast<OutputStream*> (png_get_io_ptr (png))->write (data, (int) length);
  91. }
  92. struct PNGErrorStruct {};
  93. static void JUCE_CDECL errorCallback (png_structp, png_const_charp)
  94. {
  95. throw PNGErrorStruct();
  96. }
  97. }
  98. //==============================================================================
  99. PNGImageFormat::PNGImageFormat() {}
  100. PNGImageFormat::~PNGImageFormat() {}
  101. String PNGImageFormat::getFormatName() { return "PNG"; }
  102. bool PNGImageFormat::canUnderstand (InputStream& in)
  103. {
  104. const int bytesNeeded = 4;
  105. char header [bytesNeeded];
  106. return in.read (header, bytesNeeded) == bytesNeeded
  107. && header[1] == 'P'
  108. && header[2] == 'N'
  109. && header[3] == 'G';
  110. }
  111. #if (JUCE_MAC || JUCE_IOS) && USE_COREGRAPHICS_RENDERING && JUCE_USE_COREIMAGE_LOADER
  112. Image juce_loadWithCoreImage (InputStream& input);
  113. #endif
  114. Image PNGImageFormat::decodeImage (InputStream& in)
  115. {
  116. #if (JUCE_MAC || JUCE_IOS) && USE_COREGRAPHICS_RENDERING && JUCE_USE_COREIMAGE_LOADER
  117. return juce_loadWithCoreImage (in);
  118. #else
  119. using namespace pnglibNamespace;
  120. Image image;
  121. png_structp pngReadStruct;
  122. png_infop pngInfoStruct;
  123. pngReadStruct = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
  124. if (pngReadStruct != 0)
  125. {
  126. try
  127. {
  128. pngInfoStruct = png_create_info_struct (pngReadStruct);
  129. if (pngInfoStruct == 0)
  130. {
  131. png_destroy_read_struct (&pngReadStruct, 0, 0);
  132. return Image::null;
  133. }
  134. png_set_error_fn (pngReadStruct, 0, PNGHelpers::errorCallback, PNGHelpers::errorCallback );
  135. // read the header..
  136. png_set_read_fn (pngReadStruct, &in, PNGHelpers::readCallback);
  137. png_uint_32 width, height;
  138. int bitDepth, colorType, interlaceType;
  139. png_read_info (pngReadStruct, pngInfoStruct);
  140. png_get_IHDR (pngReadStruct, pngInfoStruct,
  141. &width, &height,
  142. &bitDepth, &colorType,
  143. &interlaceType, 0, 0);
  144. if (bitDepth == 16)
  145. png_set_strip_16 (pngReadStruct);
  146. if (colorType == PNG_COLOR_TYPE_PALETTE)
  147. png_set_expand (pngReadStruct);
  148. if (bitDepth < 8)
  149. png_set_expand (pngReadStruct);
  150. if (png_get_valid (pngReadStruct, pngInfoStruct, PNG_INFO_tRNS))
  151. png_set_expand (pngReadStruct);
  152. if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
  153. png_set_gray_to_rgb (pngReadStruct);
  154. png_set_add_alpha (pngReadStruct, 0xff, PNG_FILLER_AFTER);
  155. bool hasAlphaChan = (colorType & PNG_COLOR_MASK_ALPHA) != 0
  156. || pngInfoStruct->num_trans > 0;
  157. // Load the image into a temp buffer in the pnglib format..
  158. HeapBlock <uint8> tempBuffer (height * (width << 2));
  159. {
  160. HeapBlock <png_bytep> rows (height);
  161. for (int y = (int) height; --y >= 0;)
  162. rows[y] = (png_bytep) (tempBuffer + (width << 2) * y);
  163. try
  164. {
  165. png_read_image (pngReadStruct, rows);
  166. png_read_end (pngReadStruct, pngInfoStruct);
  167. }
  168. catch (PNGHelpers::PNGErrorStruct&)
  169. {}
  170. }
  171. png_destroy_read_struct (&pngReadStruct, &pngInfoStruct, 0);
  172. // now convert the data to a juce image format..
  173. image = Image (hasAlphaChan ? Image::ARGB : Image::RGB,
  174. (int) width, (int) height, hasAlphaChan);
  175. image.getProperties()->set ("originalImageHadAlpha", image.hasAlphaChannel());
  176. hasAlphaChan = image.hasAlphaChannel(); // (the native image creator may not give back what we expect)
  177. const Image::BitmapData destData (image, Image::BitmapData::writeOnly);
  178. uint8* srcRow = tempBuffer;
  179. uint8* destRow = destData.data;
  180. for (int y = 0; y < (int) height; ++y)
  181. {
  182. const uint8* src = srcRow;
  183. srcRow += (width << 2);
  184. uint8* dest = destRow;
  185. destRow += destData.lineStride;
  186. if (hasAlphaChan)
  187. {
  188. for (int i = (int) width; --i >= 0;)
  189. {
  190. ((PixelARGB*) dest)->setARGB (src[3], src[0], src[1], src[2]);
  191. ((PixelARGB*) dest)->premultiply();
  192. dest += destData.pixelStride;
  193. src += 4;
  194. }
  195. }
  196. else
  197. {
  198. for (int i = (int) width; --i >= 0;)
  199. {
  200. ((PixelRGB*) dest)->setARGB (0, src[0], src[1], src[2]);
  201. dest += destData.pixelStride;
  202. src += 4;
  203. }
  204. }
  205. }
  206. }
  207. catch (PNGHelpers::PNGErrorStruct&)
  208. {}
  209. }
  210. return image;
  211. #endif
  212. }
  213. bool PNGImageFormat::writeImageToStream (const Image& image, OutputStream& out)
  214. {
  215. using namespace pnglibNamespace;
  216. const int width = image.getWidth();
  217. const int height = image.getHeight();
  218. png_structp pngWriteStruct = png_create_write_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
  219. if (pngWriteStruct == 0)
  220. return false;
  221. png_infop pngInfoStruct = png_create_info_struct (pngWriteStruct);
  222. if (pngInfoStruct == 0)
  223. {
  224. png_destroy_write_struct (&pngWriteStruct, (png_infopp) 0);
  225. return false;
  226. }
  227. png_set_write_fn (pngWriteStruct, &out, PNGHelpers::writeDataCallback, 0);
  228. png_set_IHDR (pngWriteStruct, pngInfoStruct, (png_uint_32) width, (png_uint_32) height, 8,
  229. image.hasAlphaChannel() ? PNG_COLOR_TYPE_RGB_ALPHA
  230. : PNG_COLOR_TYPE_RGB,
  231. PNG_INTERLACE_NONE,
  232. PNG_COMPRESSION_TYPE_BASE,
  233. PNG_FILTER_TYPE_BASE);
  234. HeapBlock <uint8> rowData ((size_t) width * 4);
  235. png_color_8 sig_bit;
  236. sig_bit.red = 8;
  237. sig_bit.green = 8;
  238. sig_bit.blue = 8;
  239. sig_bit.alpha = 8;
  240. png_set_sBIT (pngWriteStruct, pngInfoStruct, &sig_bit);
  241. png_write_info (pngWriteStruct, pngInfoStruct);
  242. png_set_shift (pngWriteStruct, &sig_bit);
  243. png_set_packing (pngWriteStruct);
  244. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  245. for (int y = 0; y < height; ++y)
  246. {
  247. uint8* dst = rowData;
  248. const uint8* src = srcData.getLinePointer (y);
  249. if (image.hasAlphaChannel())
  250. {
  251. for (int i = width; --i >= 0;)
  252. {
  253. PixelARGB p (*(const PixelARGB*) src);
  254. p.unpremultiply();
  255. *dst++ = p.getRed();
  256. *dst++ = p.getGreen();
  257. *dst++ = p.getBlue();
  258. *dst++ = p.getAlpha();
  259. src += srcData.pixelStride;
  260. }
  261. }
  262. else
  263. {
  264. for (int i = width; --i >= 0;)
  265. {
  266. *dst++ = ((const PixelRGB*) src)->getRed();
  267. *dst++ = ((const PixelRGB*) src)->getGreen();
  268. *dst++ = ((const PixelRGB*) src)->getBlue();
  269. src += srcData.pixelStride;
  270. }
  271. }
  272. png_bytep rowPtr = rowData;
  273. png_write_rows (pngWriteStruct, &rowPtr, 1);
  274. }
  275. png_write_end (pngWriteStruct, pngInfoStruct);
  276. png_destroy_write_struct (&pngWriteStruct, &pngInfoStruct);
  277. return true;
  278. }