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.

626 lines
21KB

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