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.

612 lines
19KB

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