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.

588 lines
15KB

  1. /* pngwtran.c - transforms the data in a row for PNG writers
  2. *
  3. * Copyright (c) 2018 Cosmin Truta
  4. * Copyright (c) 1998-2002,2004,2006-2016,2018 Glenn Randers-Pehrson
  5. * Copyright (c) 1996-1997 Andreas Dilger
  6. * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. */
  12. #include "pngpriv.h"
  13. #ifdef PNG_WRITE_SUPPORTED
  14. #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
  15. #ifdef PNG_WRITE_PACK_SUPPORTED
  16. /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The
  17. * row_info bit depth should be 8 (one pixel per byte). The channels
  18. * should be 1 (this only happens on grayscale and paletted images).
  19. */
  20. static void
  21. png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
  22. {
  23. png_debug(1, "in png_do_pack");
  24. if (row_info->bit_depth == 8 &&
  25. row_info->channels == 1)
  26. {
  27. switch ((int)bit_depth)
  28. {
  29. case 1:
  30. {
  31. png_bytep sp, dp;
  32. int mask, v;
  33. png_uint_32 i;
  34. png_uint_32 row_width = row_info->width;
  35. sp = row;
  36. dp = row;
  37. mask = 0x80;
  38. v = 0;
  39. for (i = 0; i < row_width; i++)
  40. {
  41. if (*sp != 0)
  42. v |= mask;
  43. sp++;
  44. if (mask > 1)
  45. mask >>= 1;
  46. else
  47. {
  48. mask = 0x80;
  49. *dp = (png_byte)v;
  50. dp++;
  51. v = 0;
  52. }
  53. }
  54. if (mask != 0x80)
  55. *dp = (png_byte)v;
  56. break;
  57. }
  58. case 2:
  59. {
  60. png_bytep sp, dp;
  61. unsigned int shift;
  62. int v;
  63. png_uint_32 i;
  64. png_uint_32 row_width = row_info->width;
  65. sp = row;
  66. dp = row;
  67. shift = 6;
  68. v = 0;
  69. for (i = 0; i < row_width; i++)
  70. {
  71. png_byte value;
  72. value = (png_byte)(*sp & 0x03);
  73. v |= (value << shift);
  74. if (shift == 0)
  75. {
  76. shift = 6;
  77. *dp = (png_byte)v;
  78. dp++;
  79. v = 0;
  80. }
  81. else
  82. shift -= 2;
  83. sp++;
  84. }
  85. if (shift != 6)
  86. *dp = (png_byte)v;
  87. break;
  88. }
  89. case 4:
  90. {
  91. png_bytep sp, dp;
  92. unsigned int shift;
  93. int v;
  94. png_uint_32 i;
  95. png_uint_32 row_width = row_info->width;
  96. sp = row;
  97. dp = row;
  98. shift = 4;
  99. v = 0;
  100. for (i = 0; i < row_width; i++)
  101. {
  102. png_byte value;
  103. value = (png_byte)(*sp & 0x0f);
  104. v |= (value << shift);
  105. if (shift == 0)
  106. {
  107. shift = 4;
  108. *dp = (png_byte)v;
  109. dp++;
  110. v = 0;
  111. }
  112. else
  113. shift -= 4;
  114. sp++;
  115. }
  116. if (shift != 4)
  117. *dp = (png_byte)v;
  118. break;
  119. }
  120. default:
  121. break;
  122. }
  123. row_info->bit_depth = (png_byte)bit_depth;
  124. row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels);
  125. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
  126. row_info->width);
  127. }
  128. }
  129. #endif
  130. #ifdef PNG_WRITE_SHIFT_SUPPORTED
  131. /* Shift pixel values to take advantage of whole range. Pass the
  132. * true number of bits in bit_depth. The row should be packed
  133. * according to row_info->bit_depth. Thus, if you had a row of
  134. * bit depth 4, but the pixels only had values from 0 to 7, you
  135. * would pass 3 as bit_depth, and this routine would translate the
  136. * data to 0 to 15.
  137. */
  138. static void
  139. png_do_shift(png_row_infop row_info, png_bytep row,
  140. png_const_color_8p bit_depth)
  141. {
  142. png_debug(1, "in png_do_shift");
  143. if (row_info->color_type != PNG_COLOR_TYPE_PALETTE)
  144. {
  145. int shift_start[4], shift_dec[4];
  146. unsigned int channels = 0;
  147. if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
  148. {
  149. shift_start[channels] = row_info->bit_depth - bit_depth->red;
  150. shift_dec[channels] = bit_depth->red;
  151. channels++;
  152. shift_start[channels] = row_info->bit_depth - bit_depth->green;
  153. shift_dec[channels] = bit_depth->green;
  154. channels++;
  155. shift_start[channels] = row_info->bit_depth - bit_depth->blue;
  156. shift_dec[channels] = bit_depth->blue;
  157. channels++;
  158. }
  159. else
  160. {
  161. shift_start[channels] = row_info->bit_depth - bit_depth->gray;
  162. shift_dec[channels] = bit_depth->gray;
  163. channels++;
  164. }
  165. if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  166. {
  167. shift_start[channels] = row_info->bit_depth - bit_depth->alpha;
  168. shift_dec[channels] = bit_depth->alpha;
  169. channels++;
  170. }
  171. /* With low row depths, could only be grayscale, so one channel */
  172. if (row_info->bit_depth < 8)
  173. {
  174. png_bytep bp = row;
  175. size_t i;
  176. unsigned int mask;
  177. size_t row_bytes = row_info->rowbytes;
  178. if (bit_depth->gray == 1 && row_info->bit_depth == 2)
  179. mask = 0x55;
  180. else if (row_info->bit_depth == 4 && bit_depth->gray == 3)
  181. mask = 0x11;
  182. else
  183. mask = 0xff;
  184. for (i = 0; i < row_bytes; i++, bp++)
  185. {
  186. int j;
  187. unsigned int v, out;
  188. v = *bp;
  189. out = 0;
  190. for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
  191. {
  192. if (j > 0)
  193. out |= v << j;
  194. else
  195. out |= (v >> (-j)) & mask;
  196. }
  197. *bp = (png_byte)(out & 0xff);
  198. }
  199. }
  200. else if (row_info->bit_depth == 8)
  201. {
  202. png_bytep bp = row;
  203. png_uint_32 i;
  204. png_uint_32 istop = channels * row_info->width;
  205. for (i = 0; i < istop; i++, bp++)
  206. {
  207. unsigned int c = i%channels;
  208. int j;
  209. unsigned int v, out;
  210. v = *bp;
  211. out = 0;
  212. for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
  213. {
  214. if (j > 0)
  215. out |= v << j;
  216. else
  217. out |= v >> (-j);
  218. }
  219. *bp = (png_byte)(out & 0xff);
  220. }
  221. }
  222. else
  223. {
  224. png_bytep bp;
  225. png_uint_32 i;
  226. png_uint_32 istop = channels * row_info->width;
  227. for (bp = row, i = 0; i < istop; i++)
  228. {
  229. unsigned int c = i%channels;
  230. int j;
  231. unsigned int value, v;
  232. v = png_get_uint_16(bp);
  233. value = 0;
  234. for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
  235. {
  236. if (j > 0)
  237. value |= v << j;
  238. else
  239. value |= v >> (-j);
  240. }
  241. *bp++ = (png_byte)((value >> 8) & 0xff);
  242. *bp++ = (png_byte)(value & 0xff);
  243. }
  244. }
  245. }
  246. }
  247. #endif
  248. #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
  249. static void
  250. png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
  251. {
  252. png_debug(1, "in png_do_write_swap_alpha");
  253. {
  254. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  255. {
  256. if (row_info->bit_depth == 8)
  257. {
  258. /* This converts from ARGB to RGBA */
  259. png_bytep sp, dp;
  260. png_uint_32 i;
  261. png_uint_32 row_width = row_info->width;
  262. for (i = 0, sp = dp = row; i < row_width; i++)
  263. {
  264. png_byte save = *(sp++);
  265. *(dp++) = *(sp++);
  266. *(dp++) = *(sp++);
  267. *(dp++) = *(sp++);
  268. *(dp++) = save;
  269. }
  270. }
  271. #ifdef PNG_WRITE_16BIT_SUPPORTED
  272. else
  273. {
  274. /* This converts from AARRGGBB to RRGGBBAA */
  275. png_bytep sp, dp;
  276. png_uint_32 i;
  277. png_uint_32 row_width = row_info->width;
  278. for (i = 0, sp = dp = row; i < row_width; i++)
  279. {
  280. png_byte save[2];
  281. save[0] = *(sp++);
  282. save[1] = *(sp++);
  283. *(dp++) = *(sp++);
  284. *(dp++) = *(sp++);
  285. *(dp++) = *(sp++);
  286. *(dp++) = *(sp++);
  287. *(dp++) = *(sp++);
  288. *(dp++) = *(sp++);
  289. *(dp++) = save[0];
  290. *(dp++) = save[1];
  291. }
  292. }
  293. #endif /* WRITE_16BIT */
  294. }
  295. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  296. {
  297. if (row_info->bit_depth == 8)
  298. {
  299. /* This converts from AG to GA */
  300. png_bytep sp, dp;
  301. png_uint_32 i;
  302. png_uint_32 row_width = row_info->width;
  303. for (i = 0, sp = dp = row; i < row_width; i++)
  304. {
  305. png_byte save = *(sp++);
  306. *(dp++) = *(sp++);
  307. *(dp++) = save;
  308. }
  309. }
  310. #ifdef PNG_WRITE_16BIT_SUPPORTED
  311. else
  312. {
  313. /* This converts from AAGG to GGAA */
  314. png_bytep sp, dp;
  315. png_uint_32 i;
  316. png_uint_32 row_width = row_info->width;
  317. for (i = 0, sp = dp = row; i < row_width; i++)
  318. {
  319. png_byte save[2];
  320. save[0] = *(sp++);
  321. save[1] = *(sp++);
  322. *(dp++) = *(sp++);
  323. *(dp++) = *(sp++);
  324. *(dp++) = save[0];
  325. *(dp++) = save[1];
  326. }
  327. }
  328. #endif /* WRITE_16BIT */
  329. }
  330. }
  331. }
  332. #endif
  333. #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  334. static void
  335. png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
  336. {
  337. png_debug(1, "in png_do_write_invert_alpha");
  338. {
  339. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  340. {
  341. if (row_info->bit_depth == 8)
  342. {
  343. /* This inverts the alpha channel in RGBA */
  344. png_bytep sp, dp;
  345. png_uint_32 i;
  346. png_uint_32 row_width = row_info->width;
  347. for (i = 0, sp = dp = row; i < row_width; i++)
  348. {
  349. /* Does nothing
  350. *(dp++) = *(sp++);
  351. *(dp++) = *(sp++);
  352. *(dp++) = *(sp++);
  353. */
  354. sp+=3; dp = sp;
  355. *dp = (png_byte)(255 - *(sp++));
  356. }
  357. // JUCE CHANGE STARTS HERE
  358. (void) dp;
  359. // JUCE CHANGE ENDS HERE
  360. }
  361. #ifdef PNG_WRITE_16BIT_SUPPORTED
  362. else
  363. {
  364. /* This inverts the alpha channel in RRGGBBAA */
  365. png_bytep sp, dp;
  366. png_uint_32 i;
  367. png_uint_32 row_width = row_info->width;
  368. for (i = 0, sp = dp = row; i < row_width; i++)
  369. {
  370. /* Does nothing
  371. *(dp++) = *(sp++);
  372. *(dp++) = *(sp++);
  373. *(dp++) = *(sp++);
  374. *(dp++) = *(sp++);
  375. *(dp++) = *(sp++);
  376. *(dp++) = *(sp++);
  377. */
  378. sp+=6; dp = sp;
  379. *(dp++) = (png_byte)(255 - *(sp++));
  380. *dp = (png_byte)(255 - *(sp++));
  381. }
  382. // JUCE CHANGE STARTS HERE
  383. (void) dp;
  384. // JUCE CHANGE ENDS HERE
  385. }
  386. #endif /* WRITE_16BIT */
  387. }
  388. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  389. {
  390. if (row_info->bit_depth == 8)
  391. {
  392. /* This inverts the alpha channel in GA */
  393. png_bytep sp, dp;
  394. png_uint_32 i;
  395. png_uint_32 row_width = row_info->width;
  396. for (i = 0, sp = dp = row; i < row_width; i++)
  397. {
  398. *(dp++) = *(sp++);
  399. *(dp++) = (png_byte)(255 - *(sp++));
  400. }
  401. }
  402. #ifdef PNG_WRITE_16BIT_SUPPORTED
  403. else
  404. {
  405. /* This inverts the alpha channel in GGAA */
  406. png_bytep sp, dp;
  407. png_uint_32 i;
  408. png_uint_32 row_width = row_info->width;
  409. for (i = 0, sp = dp = row; i < row_width; i++)
  410. {
  411. /* Does nothing
  412. *(dp++) = *(sp++);
  413. *(dp++) = *(sp++);
  414. */
  415. sp+=2; dp = sp;
  416. *(dp++) = (png_byte)(255 - *(sp++));
  417. *dp = (png_byte)(255 - *(sp++));
  418. }
  419. // JUCE CHANGE STARTS HERE
  420. (void) dp;
  421. // JUCE CHANGE ENDS HERE
  422. }
  423. #endif /* WRITE_16BIT */
  424. }
  425. }
  426. }
  427. #endif
  428. /* Transform the data according to the user's wishes. The order of
  429. * transformations is significant.
  430. */
  431. void /* PRIVATE */
  432. png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info)
  433. {
  434. png_debug(1, "in png_do_write_transformations");
  435. if (png_ptr == NULL)
  436. return;
  437. #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  438. if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
  439. if (png_ptr->write_user_transform_fn != NULL)
  440. (*(png_ptr->write_user_transform_fn)) /* User write transform
  441. function */
  442. (png_ptr, /* png_ptr */
  443. row_info, /* row_info: */
  444. /* png_uint_32 width; width of row */
  445. /* size_t rowbytes; number of bytes in row */
  446. /* png_byte color_type; color type of pixels */
  447. /* png_byte bit_depth; bit depth of samples */
  448. /* png_byte channels; number of channels (1-4) */
  449. /* png_byte pixel_depth; bits per pixel (depth*channels) */
  450. png_ptr->row_buf + 1); /* start of pixel data for row */
  451. #endif
  452. #ifdef PNG_WRITE_FILLER_SUPPORTED
  453. if ((png_ptr->transformations & PNG_FILLER) != 0)
  454. png_do_strip_channel(row_info, png_ptr->row_buf + 1,
  455. !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
  456. #endif
  457. #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
  458. if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
  459. png_do_packswap(row_info, png_ptr->row_buf + 1);
  460. #endif
  461. #ifdef PNG_WRITE_PACK_SUPPORTED
  462. if ((png_ptr->transformations & PNG_PACK) != 0)
  463. png_do_pack(row_info, png_ptr->row_buf + 1,
  464. (png_uint_32)png_ptr->bit_depth);
  465. #endif
  466. #ifdef PNG_WRITE_SWAP_SUPPORTED
  467. # ifdef PNG_16BIT_SUPPORTED
  468. if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
  469. png_do_swap(row_info, png_ptr->row_buf + 1);
  470. # endif
  471. #endif
  472. #ifdef PNG_WRITE_SHIFT_SUPPORTED
  473. if ((png_ptr->transformations & PNG_SHIFT) != 0)
  474. png_do_shift(row_info, png_ptr->row_buf + 1,
  475. &(png_ptr->shift));
  476. #endif
  477. #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
  478. if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
  479. png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
  480. #endif
  481. #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  482. if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
  483. png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
  484. #endif
  485. #ifdef PNG_WRITE_BGR_SUPPORTED
  486. if ((png_ptr->transformations & PNG_BGR) != 0)
  487. png_do_bgr(row_info, png_ptr->row_buf + 1);
  488. #endif
  489. #ifdef PNG_WRITE_INVERT_SUPPORTED
  490. if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
  491. png_do_invert(row_info, png_ptr->row_buf + 1);
  492. #endif
  493. }
  494. #endif /* WRITE_TRANSFORMS */
  495. #endif /* WRITE */