Audio plugin host https://kx.studio/carla
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.

juce_PNGLoader.cpp 19KB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #if JUCE_MSVC
  18. #pragma warning (push)
  19. #pragma warning (disable: 4390 4611 4365 4267)
  20. #ifdef __INTEL_COMPILER
  21. #pragma warning (disable: 2544 2545)
  22. #endif
  23. #endif
  24. namespace zlibNamespace
  25. {
  26. #if JUCE_INCLUDE_ZLIB_CODE
  27. #undef OS_CODE
  28. #undef fdopen
  29. #include "../../juce_core/zip/zlib/zlib.h"
  30. #undef OS_CODE
  31. #else
  32. #include JUCE_ZLIB_INCLUDE_PATH
  33. #endif
  34. }
  35. #if ! defined (jmp_buf) || ! defined (longjmp)
  36. #include <setjmp.h>
  37. #endif
  38. namespace pnglibNamespace
  39. {
  40. using namespace zlibNamespace;
  41. #if JUCE_INCLUDE_PNGLIB_CODE || ! defined (JUCE_INCLUDE_PNGLIB_CODE)
  42. #if _MSC_VER != 1310
  43. using std::calloc; // (causes conflict in VS.NET 2003)
  44. using std::malloc;
  45. using std::free;
  46. #endif
  47. #if JUCE_CLANG
  48. #pragma clang diagnostic push
  49. #pragma clang diagnostic ignored "-Wsign-conversion"
  50. #endif
  51. using std::abs;
  52. #define NO_DUMMY_DECL
  53. #define PNGLCONF_H 1
  54. #if JUCE_ANDROID
  55. #define PNG_ARM_NEON_SUPPORTED
  56. #endif
  57. #define PNG_16BIT_SUPPORTED
  58. #define PNG_ALIGNED_MEMORY_SUPPORTED
  59. #define PNG_BENIGN_ERRORS_SUPPORTED
  60. #define PNG_BENIGN_READ_ERRORS_SUPPORTED
  61. #define PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
  62. #define PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
  63. #define PNG_COLORSPACE_SUPPORTED
  64. #define PNG_CONSOLE_IO_SUPPORTED
  65. #define PNG_EASY_ACCESS_SUPPORTED
  66. #define PNG_FIXED_POINT_SUPPORTED
  67. #define PNG_FLOATING_ARITHMETIC_SUPPORTED
  68. #define PNG_FLOATING_POINT_SUPPORTED
  69. #define PNG_FORMAT_AFIRST_SUPPORTED
  70. #define PNG_FORMAT_BGR_SUPPORTED
  71. #define PNG_GAMMA_SUPPORTED
  72. #define PNG_GET_PALETTE_MAX_SUPPORTED
  73. #define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  74. #define PNG_INCH_CONVERSIONS_SUPPORTED
  75. #define PNG_INFO_IMAGE_SUPPORTED
  76. #define PNG_IO_STATE_SUPPORTED
  77. #define PNG_MNG_FEATURES_SUPPORTED
  78. #define PNG_POINTER_INDEXING_SUPPORTED
  79. #define PNG_PROGRESSIVE_READ_SUPPORTED
  80. #define PNG_READ_16BIT_SUPPORTED
  81. #define PNG_READ_ALPHA_MODE_SUPPORTED
  82. #define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
  83. #define PNG_READ_BACKGROUND_SUPPORTED
  84. #define PNG_READ_BGR_SUPPORTED
  85. #define PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
  86. #define PNG_READ_COMPOSITE_NODIV_SUPPORTED
  87. #define PNG_READ_COMPRESSED_TEXT_SUPPORTED
  88. #define PNG_READ_EXPAND_16_SUPPORTED
  89. #define PNG_READ_EXPAND_SUPPORTED
  90. #define PNG_READ_FILLER_SUPPORTED
  91. #define PNG_READ_GAMMA_SUPPORTED
  92. #define PNG_READ_GET_PALETTE_MAX_SUPPORTED
  93. #define PNG_READ_GRAY_TO_RGB_SUPPORTED
  94. #define PNG_READ_INTERLACING_SUPPORTED
  95. #define PNG_READ_INT_FUNCTIONS_SUPPORTED
  96. #define PNG_READ_INVERT_ALPHA_SUPPORTED
  97. #define PNG_READ_INVERT_SUPPORTED
  98. #define PNG_READ_OPT_PLTE_SUPPORTED
  99. #define PNG_READ_PACKSWAP_SUPPORTED
  100. #define PNG_READ_PACK_SUPPORTED
  101. #define PNG_READ_QUANTIZE_SUPPORTED
  102. #define PNG_READ_RGB_TO_GRAY_SUPPORTED
  103. #define PNG_READ_SCALE_16_TO_8_SUPPORTED
  104. #define PNG_READ_SHIFT_SUPPORTED
  105. #define PNG_READ_STRIP_16_TO_8_SUPPORTED
  106. #define PNG_READ_STRIP_ALPHA_SUPPORTED
  107. #define PNG_READ_SUPPORTED
  108. #define PNG_READ_SWAP_ALPHA_SUPPORTED
  109. #define PNG_READ_SWAP_SUPPORTED
  110. #define PNG_READ_TEXT_SUPPORTED
  111. #define PNG_READ_TRANSFORMS_SUPPORTED
  112. #define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  113. #define PNG_READ_USER_CHUNKS_SUPPORTED
  114. #define PNG_READ_USER_TRANSFORM_SUPPORTED
  115. #define PNG_READ_bKGD_SUPPORTED
  116. #define PNG_READ_cHRM_SUPPORTED
  117. #define PNG_READ_gAMA_SUPPORTED
  118. #define PNG_READ_hIST_SUPPORTED
  119. #define PNG_READ_iCCP_SUPPORTED
  120. #define PNG_READ_iTXt_SUPPORTED
  121. #define PNG_READ_oFFs_SUPPORTED
  122. #define PNG_READ_pCAL_SUPPORTED
  123. #define PNG_READ_pHYs_SUPPORTED
  124. #define PNG_READ_sBIT_SUPPORTED
  125. #define PNG_READ_sCAL_SUPPORTED
  126. #define PNG_READ_sPLT_SUPPORTED
  127. #define PNG_READ_sRGB_SUPPORTED
  128. #define PNG_READ_tEXt_SUPPORTED
  129. #define PNG_READ_tIME_SUPPORTED
  130. #define PNG_READ_tRNS_SUPPORTED
  131. #define PNG_READ_zTXt_SUPPORTED
  132. #define PNG_SAVE_INT_32_SUPPORTED
  133. #define PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
  134. #define PNG_SEQUENTIAL_READ_SUPPORTED
  135. #define PNG_SET_CHUNK_CACHE_LIMIT_SUPPORTED
  136. #define PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
  137. #define PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  138. #define PNG_SET_USER_LIMITS_SUPPORTED
  139. #define PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
  140. #define PNG_SIMPLIFIED_READ_BGR_SUPPORTED
  141. #define PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
  142. #define PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
  143. #define PNG_STDIO_SUPPORTED
  144. #define PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
  145. #define PNG_TEXT_SUPPORTED
  146. #define PNG_TIME_RFC1123_SUPPORTED
  147. #define PNG_UNKNOWN_CHUNKS_SUPPORTED
  148. #define PNG_USER_CHUNKS_SUPPORTED
  149. #define PNG_USER_LIMITS_SUPPORTED
  150. #define PNG_USER_TRANSFORM_INFO_SUPPORTED
  151. #define PNG_USER_TRANSFORM_PTR_SUPPORTED
  152. #define PNG_WARNINGS_SUPPORTED
  153. #define PNG_WRITE_16BIT_SUPPORTED
  154. #define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
  155. #define PNG_WRITE_BGR_SUPPORTED
  156. #define PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
  157. #define PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
  158. #define PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
  159. #define PNG_WRITE_FILLER_SUPPORTED
  160. #define PNG_WRITE_FILTER_SUPPORTED
  161. #define PNG_WRITE_FLUSH_SUPPORTED
  162. #define PNG_WRITE_GET_PALETTE_MAX_SUPPORTED
  163. #define PNG_WRITE_INTERLACING_SUPPORTED
  164. #define PNG_WRITE_INT_FUNCTIONS_SUPPORTED
  165. #define PNG_WRITE_INVERT_ALPHA_SUPPORTED
  166. #define PNG_WRITE_INVERT_SUPPORTED
  167. #define PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
  168. #define PNG_WRITE_PACKSWAP_SUPPORTED
  169. #define PNG_WRITE_PACK_SUPPORTED
  170. #define PNG_WRITE_SHIFT_SUPPORTED
  171. #define PNG_WRITE_SUPPORTED
  172. #define PNG_WRITE_SWAP_ALPHA_SUPPORTED
  173. #define PNG_WRITE_SWAP_SUPPORTED
  174. #define PNG_WRITE_TEXT_SUPPORTED
  175. #define PNG_WRITE_TRANSFORMS_SUPPORTED
  176. #define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  177. #define PNG_WRITE_USER_TRANSFORM_SUPPORTED
  178. #define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  179. #define PNG_WRITE_bKGD_SUPPORTED
  180. #define PNG_WRITE_cHRM_SUPPORTED
  181. #define PNG_WRITE_gAMA_SUPPORTED
  182. #define PNG_WRITE_hIST_SUPPORTED
  183. #define PNG_WRITE_iCCP_SUPPORTED
  184. #define PNG_WRITE_iTXt_SUPPORTED
  185. #define PNG_WRITE_oFFs_SUPPORTED
  186. #define PNG_WRITE_pCAL_SUPPORTED
  187. #define PNG_WRITE_pHYs_SUPPORTED
  188. #define PNG_WRITE_sBIT_SUPPORTED
  189. #define PNG_WRITE_sCAL_SUPPORTED
  190. #define PNG_WRITE_sPLT_SUPPORTED
  191. #define PNG_WRITE_sRGB_SUPPORTED
  192. #define PNG_WRITE_tEXt_SUPPORTED
  193. #define PNG_WRITE_tIME_SUPPORTED
  194. #define PNG_WRITE_tRNS_SUPPORTED
  195. #define PNG_WRITE_zTXt_SUPPORTED
  196. #define PNG_bKGD_SUPPORTED
  197. #define PNG_cHRM_SUPPORTED
  198. #define PNG_gAMA_SUPPORTED
  199. #define PNG_hIST_SUPPORTED
  200. #define PNG_iCCP_SUPPORTED
  201. #define PNG_iTXt_SUPPORTED
  202. #define PNG_oFFs_SUPPORTED
  203. #define PNG_pCAL_SUPPORTED
  204. #define PNG_pHYs_SUPPORTED
  205. #define PNG_sBIT_SUPPORTED
  206. #define PNG_sCAL_SUPPORTED
  207. #define PNG_sPLT_SUPPORTED
  208. #define PNG_sRGB_SUPPORTED
  209. #define PNG_tEXt_SUPPORTED
  210. #define PNG_tIME_SUPPORTED
  211. #define PNG_tRNS_SUPPORTED
  212. #define PNG_zTXt_SUPPORTED
  213. #define PNG_STRING_COPYRIGHT "";
  214. #define PNG_STRING_NEWLINE "\n"
  215. #define PNG_LITERAL_SHARP 0x23
  216. #define PNG_LITERAL_LEFT_SQUARE_BRACKET 0x5b
  217. #define PNG_LITERAL_RIGHT_SQUARE_BRACKET 0x5d
  218. #define PNG_API_RULE 0
  219. #define PNG_CALLOC_SUPPORTED
  220. #define PNG_COST_SHIFT 3
  221. #define PNG_DEFAULT_READ_MACROS 1
  222. #define PNG_GAMMA_THRESHOLD_FIXED 5000
  223. #define PNG_IDAT_READ_SIZE PNG_ZBUF_SIZE
  224. #define PNG_INFLATE_BUF_SIZE 1024
  225. #define PNG_MAX_GAMMA_8 11
  226. #define PNG_QUANTIZE_BLUE_BITS 5
  227. #define PNG_QUANTIZE_GREEN_BITS 5
  228. #define PNG_QUANTIZE_RED_BITS 5
  229. #define PNG_TEXT_Z_DEFAULT_COMPRESSION (-1)
  230. #define PNG_TEXT_Z_DEFAULT_STRATEGY 0
  231. #define PNG_WEIGHT_SHIFT 8
  232. #define PNG_ZBUF_SIZE 8192
  233. #define PNG_Z_DEFAULT_COMPRESSION (-1)
  234. #define PNG_Z_DEFAULT_NOFILTER_STRATEGY 0
  235. #define PNG_Z_DEFAULT_STRATEGY 1
  236. #define PNG_sCAL_PRECISION 5
  237. #define PNG_sRGB_PROFILE_CHECKS 2
  238. #define png_debug(a, b)
  239. #define png_debug1(a, b, c)
  240. #define png_debug2(a, b, c, d)
  241. #include "pnglib/png.h"
  242. #include "pnglib/pngconf.h"
  243. #define PNG_NO_EXTERN
  244. #include "pnglib/png.c"
  245. #include "pnglib/pngerror.c"
  246. #include "pnglib/pngget.c"
  247. #include "pnglib/pngmem.c"
  248. #include "pnglib/pngread.c"
  249. #include "pnglib/pngpread.c"
  250. #include "pnglib/pngrio.c"
  251. #include "pnglib/pngrtran.c"
  252. #include "pnglib/pngrutil.c"
  253. #include "pnglib/pngset.c"
  254. #include "pnglib/pngtrans.c"
  255. #include "pnglib/pngwio.c"
  256. #include "pnglib/pngwrite.c"
  257. #include "pnglib/pngwtran.c"
  258. #include "pnglib/pngwutil.c"
  259. #if JUCE_CLANG
  260. #pragma clang diagnostic pop
  261. #endif
  262. #else
  263. extern "C"
  264. {
  265. #include <png.h>
  266. #include <pngconf.h>
  267. }
  268. #endif
  269. }
  270. #undef max
  271. #undef min
  272. #undef fdopen
  273. #if JUCE_MSVC
  274. #pragma warning (pop)
  275. #endif
  276. //==============================================================================
  277. namespace PNGHelpers
  278. {
  279. using namespace pnglibNamespace;
  280. static void JUCE_CDECL writeDataCallback (png_structp png, png_bytep data, png_size_t length)
  281. {
  282. static_cast<OutputStream*> (png_get_io_ptr (png))->write (data, length);
  283. }
  284. #if ! JUCE_USING_COREIMAGE_LOADER
  285. static void JUCE_CDECL readCallback (png_structp png, png_bytep data, png_size_t length)
  286. {
  287. static_cast<InputStream*> (png_get_io_ptr (png))->read (data, (int) length);
  288. }
  289. struct PNGErrorStruct {};
  290. static void JUCE_CDECL errorCallback (png_structp p, png_const_charp)
  291. {
  292. longjmp (*(jmp_buf*) p->error_ptr, 1);
  293. }
  294. static void JUCE_CDECL warningCallback (png_structp, png_const_charp) {}
  295. #if JUCE_MSVC
  296. #pragma warning (push)
  297. #pragma warning (disable: 4611) // (warning about setjmp)
  298. #endif
  299. static bool readHeader (InputStream& in, png_structp pngReadStruct, png_infop pngInfoStruct, jmp_buf& errorJumpBuf,
  300. png_uint_32& width, png_uint_32& height, int& bitDepth, int& colorType, int& interlaceType) noexcept
  301. {
  302. if (setjmp (errorJumpBuf) == 0)
  303. {
  304. // read the header..
  305. png_set_read_fn (pngReadStruct, &in, readCallback);
  306. png_read_info (pngReadStruct, pngInfoStruct);
  307. png_get_IHDR (pngReadStruct, pngInfoStruct,
  308. &width, &height,
  309. &bitDepth, &colorType,
  310. &interlaceType, 0, 0);
  311. if (bitDepth == 16)
  312. png_set_strip_16 (pngReadStruct);
  313. if (colorType == PNG_COLOR_TYPE_PALETTE)
  314. png_set_expand (pngReadStruct);
  315. if (bitDepth < 8)
  316. png_set_expand (pngReadStruct);
  317. if (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
  318. png_set_gray_to_rgb (pngReadStruct);
  319. return true;
  320. }
  321. return false;
  322. }
  323. static bool readImageData (png_structp pngReadStruct, png_infop pngInfoStruct, jmp_buf& errorJumpBuf, png_bytepp rows) noexcept
  324. {
  325. if (setjmp (errorJumpBuf) == 0)
  326. {
  327. if (png_get_valid (pngReadStruct, pngInfoStruct, PNG_INFO_tRNS))
  328. png_set_expand (pngReadStruct);
  329. png_set_add_alpha (pngReadStruct, 0xff, PNG_FILLER_AFTER);
  330. png_read_image (pngReadStruct, rows);
  331. png_read_end (pngReadStruct, pngInfoStruct);
  332. return true;
  333. }
  334. return false;
  335. }
  336. #if JUCE_MSVC
  337. #pragma warning (pop)
  338. #endif
  339. static Image createImageFromData (bool hasAlphaChan, int width, int height, png_bytepp rows)
  340. {
  341. // now convert the data to a juce image format..
  342. Image image (hasAlphaChan ? Image::ARGB : Image::RGB, width, height, hasAlphaChan);
  343. image.getProperties()->set ("originalImageHadAlpha", image.hasAlphaChannel());
  344. hasAlphaChan = image.hasAlphaChannel(); // (the native image creator may not give back what we expect)
  345. const Image::BitmapData destData (image, Image::BitmapData::writeOnly);
  346. for (int y = 0; y < (int) height; ++y)
  347. {
  348. const uint8* src = rows[y];
  349. uint8* dest = destData.getLinePointer (y);
  350. if (hasAlphaChan)
  351. {
  352. for (int i = (int) width; --i >= 0;)
  353. {
  354. ((PixelARGB*) dest)->setARGB (src[3], src[0], src[1], src[2]);
  355. ((PixelARGB*) dest)->premultiply();
  356. dest += destData.pixelStride;
  357. src += 4;
  358. }
  359. }
  360. else
  361. {
  362. for (int i = (int) width; --i >= 0;)
  363. {
  364. ((PixelRGB*) dest)->setARGB (0, src[0], src[1], src[2]);
  365. dest += destData.pixelStride;
  366. src += 4;
  367. }
  368. }
  369. }
  370. return image;
  371. }
  372. static Image readImage (InputStream& in, png_structp pngReadStruct, png_infop pngInfoStruct)
  373. {
  374. jmp_buf errorJumpBuf;
  375. png_set_error_fn (pngReadStruct, &errorJumpBuf, errorCallback, warningCallback);
  376. png_uint_32 width = 0, height = 0;
  377. int bitDepth = 0, colorType = 0, interlaceType = 0;
  378. if (readHeader (in, pngReadStruct, pngInfoStruct, errorJumpBuf,
  379. width, height, bitDepth, colorType, interlaceType))
  380. {
  381. // Load the image into a temp buffer..
  382. const size_t lineStride = width * 4;
  383. HeapBlock<uint8> tempBuffer (height * lineStride);
  384. HeapBlock<png_bytep> rows (height);
  385. for (size_t y = 0; y < height; ++y)
  386. rows[y] = (png_bytep) (tempBuffer + lineStride * y);
  387. if (readImageData (pngReadStruct, pngInfoStruct, errorJumpBuf, rows))
  388. return createImageFromData ((colorType & PNG_COLOR_MASK_ALPHA) != 0 || pngInfoStruct->num_trans > 0,
  389. (int) width, (int) height, rows);
  390. }
  391. return Image();
  392. }
  393. static Image readImage (InputStream& in)
  394. {
  395. if (png_structp pngReadStruct = png_create_read_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0))
  396. {
  397. if (png_infop pngInfoStruct = png_create_info_struct (pngReadStruct))
  398. {
  399. Image image (readImage (in, pngReadStruct, pngInfoStruct));
  400. png_destroy_read_struct (&pngReadStruct, &pngInfoStruct, 0);
  401. return image;
  402. }
  403. png_destroy_read_struct (&pngReadStruct, 0, 0);
  404. }
  405. return Image();
  406. }
  407. #endif
  408. }
  409. //==============================================================================
  410. PNGImageFormat::PNGImageFormat() {}
  411. PNGImageFormat::~PNGImageFormat() {}
  412. String PNGImageFormat::getFormatName() { return "PNG"; }
  413. bool PNGImageFormat::usesFileExtension (const File& f) { return f.hasFileExtension ("png"); }
  414. bool PNGImageFormat::canUnderstand (InputStream& in)
  415. {
  416. const int bytesNeeded = 4;
  417. char header [bytesNeeded];
  418. return in.read (header, bytesNeeded) == bytesNeeded
  419. && header[1] == 'P'
  420. && header[2] == 'N'
  421. && header[3] == 'G';
  422. }
  423. #if JUCE_USING_COREIMAGE_LOADER
  424. Image juce_loadWithCoreImage (InputStream&);
  425. #endif
  426. Image PNGImageFormat::decodeImage (InputStream& in)
  427. {
  428. #if JUCE_USING_COREIMAGE_LOADER
  429. return juce_loadWithCoreImage (in);
  430. #else
  431. return PNGHelpers::readImage (in);
  432. #endif
  433. }
  434. bool PNGImageFormat::writeImageToStream (const Image& image, OutputStream& out)
  435. {
  436. using namespace pnglibNamespace;
  437. const int width = image.getWidth();
  438. const int height = image.getHeight();
  439. png_structp pngWriteStruct = png_create_write_struct (PNG_LIBPNG_VER_STRING, 0, 0, 0);
  440. if (pngWriteStruct == nullptr)
  441. return false;
  442. png_infop pngInfoStruct = png_create_info_struct (pngWriteStruct);
  443. if (pngInfoStruct == nullptr)
  444. {
  445. png_destroy_write_struct (&pngWriteStruct, (png_infopp) nullptr);
  446. return false;
  447. }
  448. png_set_write_fn (pngWriteStruct, &out, PNGHelpers::writeDataCallback, 0);
  449. png_set_IHDR (pngWriteStruct, pngInfoStruct, (png_uint_32) width, (png_uint_32) height, 8,
  450. image.hasAlphaChannel() ? PNG_COLOR_TYPE_RGB_ALPHA
  451. : PNG_COLOR_TYPE_RGB,
  452. PNG_INTERLACE_NONE,
  453. PNG_COMPRESSION_TYPE_BASE,
  454. PNG_FILTER_TYPE_BASE);
  455. HeapBlock<uint8> rowData ((size_t) width * 4);
  456. png_color_8 sig_bit;
  457. sig_bit.red = 8;
  458. sig_bit.green = 8;
  459. sig_bit.blue = 8;
  460. sig_bit.gray = 0;
  461. sig_bit.alpha = 8;
  462. png_set_sBIT (pngWriteStruct, pngInfoStruct, &sig_bit);
  463. png_write_info (pngWriteStruct, pngInfoStruct);
  464. png_set_shift (pngWriteStruct, &sig_bit);
  465. png_set_packing (pngWriteStruct);
  466. const Image::BitmapData srcData (image, Image::BitmapData::readOnly);
  467. for (int y = 0; y < height; ++y)
  468. {
  469. uint8* dst = rowData;
  470. const uint8* src = srcData.getLinePointer (y);
  471. if (image.hasAlphaChannel())
  472. {
  473. for (int i = width; --i >= 0;)
  474. {
  475. PixelARGB p (*(const PixelARGB*) src);
  476. p.unpremultiply();
  477. *dst++ = p.getRed();
  478. *dst++ = p.getGreen();
  479. *dst++ = p.getBlue();
  480. *dst++ = p.getAlpha();
  481. src += srcData.pixelStride;
  482. }
  483. }
  484. else
  485. {
  486. for (int i = width; --i >= 0;)
  487. {
  488. *dst++ = ((const PixelRGB*) src)->getRed();
  489. *dst++ = ((const PixelRGB*) src)->getGreen();
  490. *dst++ = ((const PixelRGB*) src)->getBlue();
  491. src += srcData.pixelStride;
  492. }
  493. }
  494. png_bytep rowPtr = rowData;
  495. png_write_rows (pngWriteStruct, &rowPtr, 1);
  496. }
  497. png_write_end (pngWriteStruct, pngInfoStruct);
  498. png_destroy_write_struct (&pngWriteStruct, &pngInfoStruct);
  499. return true;
  500. }