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.

638 lines
18KB

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