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. #if JUCE_CLANG
  46. #pragma clang diagnostic push
  47. #pragma clang diagnostic ignored "-Wsign-conversion"
  48. #endif
  49. using std::abs;
  50. #define PNG_INTERNAL
  51. #define NO_DUMMY_DECL
  52. #define PNG_SETJMP_NOT_SUPPORTED
  53. #include "pnglib/png.h"
  54. #include "pnglib/pngconf.h"
  55. #define PNG_NO_EXTERN
  56. #include "pnglib/png.c"
  57. #include "pnglib/pngerror.c"
  58. #include "pnglib/pngget.c"
  59. #include "pnglib/pngmem.c"
  60. #include "pnglib/pngread.c"
  61. #include "pnglib/pngpread.c"
  62. #include "pnglib/pngrio.c"
  63. #include "pnglib/pngrtran.c"
  64. #include "pnglib/pngrutil.c"
  65. #include "pnglib/pngset.c"
  66. #include "pnglib/pngtrans.c"
  67. #include "pnglib/pngwio.c"
  68. #include "pnglib/pngwrite.c"
  69. #include "pnglib/pngwtran.c"
  70. #include "pnglib/pngwutil.c"
  71. #if JUCE_CLANG
  72. #pragma clang diagnostic pop
  73. #endif
  74. #else
  75. extern "C"
  76. {
  77. #include <png.h>
  78. #include <pngconf.h>
  79. }
  80. #endif
  81. }
  82. #undef max
  83. #undef min
  84. #undef fdopen
  85. #if JUCE_MSVC
  86. #pragma warning (pop)
  87. #endif
  88. //==============================================================================
  89. namespace PNGHelpers
  90. {
  91. using namespace pnglibNamespace;
  92. static void JUCE_CDECL writeDataCallback (png_structp png, png_bytep data, png_size_t length)
  93. {
  94. static_cast<OutputStream*> (png_get_io_ptr (png))->write (data, length);
  95. }
  96. #if ! JUCE_USING_COREIMAGE_LOADER
  97. static void JUCE_CDECL readCallback (png_structp png, png_bytep data, png_size_t length)
  98. {
  99. static_cast<InputStream*> (png_get_io_ptr (png))->read (data, (int) length);
  100. }
  101. struct PNGErrorStruct {};
  102. static void JUCE_CDECL errorCallback (png_structp, png_const_charp)
  103. {
  104. throw PNGErrorStruct();
  105. }
  106. #endif
  107. }
  108. //==============================================================================
  109. PNGImageFormat::PNGImageFormat() {}
  110. PNGImageFormat::~PNGImageFormat() {}
  111. String PNGImageFormat::getFormatName() { return "PNG"; }
  112. bool PNGImageFormat::usesFileExtension (const File& f) { return f.hasFileExtension ("png"); }
  113. bool PNGImageFormat::canUnderstand (InputStream& in)
  114. {
  115. const int bytesNeeded = 4;
  116. char header [bytesNeeded];
  117. return in.read (header, bytesNeeded) == bytesNeeded
  118. && header[1] == 'P'
  119. && header[2] == 'N'
  120. && header[3] == 'G';
  121. }
  122. #if JUCE_USING_COREIMAGE_LOADER
  123. Image juce_loadWithCoreImage (InputStream& input);
  124. #endif
  125. Image PNGImageFormat::decodeImage (InputStream& in)
  126. {
  127. #if JUCE_USING_COREIMAGE_LOADER
  128. return juce_loadWithCoreImage (in);
  129. #else
  130. using namespace pnglibNamespace;
  131. Image image;
  132. if (png_structp pngReadStruct = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0))
  133. {
  134. try
  135. {
  136. png_infop pngInfoStruct = png_create_info_struct (pngReadStruct);
  137. if (pngInfoStruct == nullptr)
  138. {
  139. png_destroy_read_struct (&pngReadStruct, 0, 0);
  140. return Image::null;
  141. }
  142. png_set_error_fn (pngReadStruct, 0, PNGHelpers::errorCallback, PNGHelpers::errorCallback);
  143. // read the header..
  144. png_set_read_fn (pngReadStruct, &in, PNGHelpers::readCallback);
  145. png_uint_32 width, height;
  146. int bitDepth, colorType, interlaceType;
  147. png_read_info (pngReadStruct, pngInfoStruct);
  148. png_get_IHDR (pngReadStruct, pngInfoStruct,
  149. &width, &height,
  150. &bitDepth, &colorType,
  151. &interlaceType, 0, 0);
  152. if (bitDepth == 16)
  153. png_set_strip_16 (pngReadStruct);
  154. if (colorType == PNG_COLOR_TYPE_PALETTE)
  155. png_set_expand (pngReadStruct);
  156. if (bitDepth < 8)
  157. png_set_expand (pngReadStruct);
  158. if (png_get_valid (pngReadStruct, pngInfoStruct, PNG_INFO_tRNS))
  159. png_set_expand (pngReadStruct);
  160. if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
  161. png_set_gray_to_rgb (pngReadStruct);
  162. png_set_add_alpha (pngReadStruct, 0xff, PNG_FILLER_AFTER);
  163. bool hasAlphaChan = (colorType & PNG_COLOR_MASK_ALPHA) != 0
  164. || pngInfoStruct->num_trans > 0;
  165. // Load the image into a temp buffer in the pnglib format..
  166. const size_t lineStride = width * 4;
  167. HeapBlock <uint8> tempBuffer (height * lineStride);
  168. HeapBlock <png_bytep> rows (height);
  169. for (int y = (int) height; --y >= 0;)
  170. rows[y] = (png_bytep) (tempBuffer + lineStride * y);
  171. try
  172. {
  173. png_read_image (pngReadStruct, rows);
  174. png_read_end (pngReadStruct, pngInfoStruct);
  175. }
  176. catch (PNGHelpers::PNGErrorStruct&)
  177. {}
  178. png_destroy_read_struct (&pngReadStruct, &pngInfoStruct, 0);
  179. // now convert the data to a juce image format..
  180. image = Image (hasAlphaChan ? Image::ARGB : Image::RGB,
  181. (int) width, (int) height, hasAlphaChan);
  182. image.getProperties()->set ("originalImageHadAlpha", image.hasAlphaChannel());
  183. hasAlphaChan = image.hasAlphaChannel(); // (the native image creator may not give back what we expect)
  184. const Image::BitmapData destData (image, Image::BitmapData::writeOnly);
  185. for (int y = 0; y < (int) height; ++y)
  186. {
  187. const uint8* src = rows[y];
  188. uint8* dest = destData.getLinePointer (y);
  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 == nullptr)
  223. return false;
  224. png_infop pngInfoStruct = png_create_info_struct (pngWriteStruct);
  225. if (pngInfoStruct == nullptr)
  226. {
  227. png_destroy_write_struct (&pngWriteStruct, (png_infopp) nullptr);
  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. }