pngwtran.c 17 KB

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