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.

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