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.

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