pngwtran.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /* pngwtran.c - transforms the data in a row for PNG writers
  2. *
  3. * Last changed in libpng 1.5.6 [November 3, 2011]
  4. * Copyright (c) 1998-2011 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. 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. png_byte 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. png_uint_16 v;
  249. int j;
  250. v = *bp;
  251. *bp = 0;
  252. for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0])
  253. {
  254. if (j > 0)
  255. *bp |= (png_byte)((v << j) & 0xff);
  256. else
  257. *bp |= (png_byte)((v >> (-j)) & mask);
  258. }
  259. }
  260. }
  261. else if (row_info->bit_depth == 8)
  262. {
  263. png_bytep bp = row;
  264. png_uint_32 i;
  265. png_uint_32 istop = channels * row_info->width;
  266. for (i = 0; i < istop; i++, bp++)
  267. {
  268. png_uint_16 v;
  269. int j;
  270. int c = (int)(i%channels);
  271. v = *bp;
  272. *bp = 0;
  273. for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
  274. {
  275. if (j > 0)
  276. *bp |= (png_byte)((v << j) & 0xff);
  277. else
  278. *bp |= (png_byte)((v >> (-j)) & 0xff);
  279. }
  280. }
  281. }
  282. else
  283. {
  284. png_bytep bp;
  285. png_uint_32 i;
  286. png_uint_32 istop = channels * row_info->width;
  287. for (bp = row, i = 0; i < istop; i++)
  288. {
  289. int c = (int)(i%channels);
  290. png_uint_16 value, v;
  291. int j;
  292. v = (png_uint_16)(((png_uint_16)(*bp) << 8) + *(bp + 1));
  293. value = 0;
  294. for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
  295. {
  296. if (j > 0)
  297. value |= (png_uint_16)((v << j) & (png_uint_16)0xffff);
  298. else
  299. value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
  300. }
  301. *bp++ = (png_byte)(value >> 8);
  302. *bp++ = (png_byte)(value & 0xff);
  303. }
  304. }
  305. }
  306. }
  307. #endif
  308. #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
  309. void /* PRIVATE */
  310. png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
  311. {
  312. png_debug(1, "in png_do_write_swap_alpha");
  313. {
  314. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  315. {
  316. if (row_info->bit_depth == 8)
  317. {
  318. /* This converts from ARGB to RGBA */
  319. png_bytep sp, dp;
  320. png_uint_32 i;
  321. png_uint_32 row_width = row_info->width;
  322. for (i = 0, sp = dp = row; i < row_width; i++)
  323. {
  324. png_byte save = *(sp++);
  325. *(dp++) = *(sp++);
  326. *(dp++) = *(sp++);
  327. *(dp++) = *(sp++);
  328. *(dp++) = save;
  329. }
  330. }
  331. #ifdef PNG_WRITE_16BIT_SUPPORTED
  332. else
  333. {
  334. /* This converts from AARRGGBB to RRGGBBAA */
  335. png_bytep sp, dp;
  336. png_uint_32 i;
  337. png_uint_32 row_width = row_info->width;
  338. for (i = 0, sp = dp = row; i < row_width; i++)
  339. {
  340. png_byte save[2];
  341. save[0] = *(sp++);
  342. save[1] = *(sp++);
  343. *(dp++) = *(sp++);
  344. *(dp++) = *(sp++);
  345. *(dp++) = *(sp++);
  346. *(dp++) = *(sp++);
  347. *(dp++) = *(sp++);
  348. *(dp++) = *(sp++);
  349. *(dp++) = save[0];
  350. *(dp++) = save[1];
  351. }
  352. }
  353. #endif /* PNG_WRITE_16BIT_SUPPORTED */
  354. }
  355. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  356. {
  357. if (row_info->bit_depth == 8)
  358. {
  359. /* This converts from AG to GA */
  360. png_bytep sp, dp;
  361. png_uint_32 i;
  362. png_uint_32 row_width = row_info->width;
  363. for (i = 0, sp = dp = row; i < row_width; i++)
  364. {
  365. png_byte save = *(sp++);
  366. *(dp++) = *(sp++);
  367. *(dp++) = save;
  368. }
  369. }
  370. #ifdef PNG_WRITE_16BIT_SUPPORTED
  371. else
  372. {
  373. /* This converts from AAGG to GGAA */
  374. png_bytep sp, dp;
  375. png_uint_32 i;
  376. png_uint_32 row_width = row_info->width;
  377. for (i = 0, sp = dp = row; i < row_width; i++)
  378. {
  379. png_byte save[2];
  380. save[0] = *(sp++);
  381. save[1] = *(sp++);
  382. *(dp++) = *(sp++);
  383. *(dp++) = *(sp++);
  384. *(dp++) = save[0];
  385. *(dp++) = save[1];
  386. }
  387. }
  388. #endif /* PNG_WRITE_16BIT_SUPPORTED */
  389. }
  390. }
  391. }
  392. #endif
  393. #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  394. void /* PRIVATE */
  395. png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
  396. {
  397. png_debug(1, "in png_do_write_invert_alpha");
  398. {
  399. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  400. {
  401. if (row_info->bit_depth == 8)
  402. {
  403. /* This inverts the alpha channel in RGBA */
  404. png_bytep sp, dp;
  405. png_uint_32 i;
  406. png_uint_32 row_width = row_info->width;
  407. for (i = 0, sp = dp = row; i < row_width; i++)
  408. {
  409. /* Does nothing
  410. *(dp++) = *(sp++);
  411. *(dp++) = *(sp++);
  412. *(dp++) = *(sp++);
  413. */
  414. sp+=3; dp = sp;
  415. *(dp++) = (png_byte)(255 - *(sp++));
  416. }
  417. }
  418. #ifdef PNG_WRITE_16BIT_SUPPORTED
  419. else
  420. {
  421. /* This inverts the alpha channel in RRGGBBAA */
  422. png_bytep sp, dp;
  423. png_uint_32 i;
  424. png_uint_32 row_width = row_info->width;
  425. for (i = 0, sp = dp = row; i < row_width; i++)
  426. {
  427. /* Does nothing
  428. *(dp++) = *(sp++);
  429. *(dp++) = *(sp++);
  430. *(dp++) = *(sp++);
  431. *(dp++) = *(sp++);
  432. *(dp++) = *(sp++);
  433. *(dp++) = *(sp++);
  434. */
  435. sp+=6; dp = sp;
  436. *(dp++) = (png_byte)(255 - *(sp++));
  437. *(dp++) = (png_byte)(255 - *(sp++));
  438. }
  439. }
  440. #endif /* PNG_WRITE_16BIT_SUPPORTED */
  441. }
  442. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  443. {
  444. if (row_info->bit_depth == 8)
  445. {
  446. /* This inverts the alpha channel in GA */
  447. png_bytep sp, dp;
  448. png_uint_32 i;
  449. png_uint_32 row_width = row_info->width;
  450. for (i = 0, sp = dp = row; i < row_width; i++)
  451. {
  452. *(dp++) = *(sp++);
  453. *(dp++) = (png_byte)(255 - *(sp++));
  454. }
  455. }
  456. #ifdef PNG_WRITE_16BIT_SUPPORTED
  457. else
  458. {
  459. /* This inverts the alpha channel in GGAA */
  460. png_bytep sp, dp;
  461. png_uint_32 i;
  462. png_uint_32 row_width = row_info->width;
  463. for (i = 0, sp = dp = row; i < row_width; i++)
  464. {
  465. /* Does nothing
  466. *(dp++) = *(sp++);
  467. *(dp++) = *(sp++);
  468. */
  469. sp+=2; dp = sp;
  470. *(dp++) = (png_byte)(255 - *(sp++));
  471. *(dp++) = (png_byte)(255 - *(sp++));
  472. }
  473. }
  474. #endif /* PNG_WRITE_16BIT_SUPPORTED */
  475. }
  476. }
  477. }
  478. #endif
  479. #endif /* PNG_WRITE_TRANSFORMS_SUPPORTED */
  480. #ifdef PNG_MNG_FEATURES_SUPPORTED
  481. /* Undoes intrapixel differencing */
  482. void /* PRIVATE */
  483. png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
  484. {
  485. png_debug(1, "in png_do_write_intrapixel");
  486. if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
  487. {
  488. int bytes_per_pixel;
  489. png_uint_32 row_width = row_info->width;
  490. if (row_info->bit_depth == 8)
  491. {
  492. png_bytep rp;
  493. png_uint_32 i;
  494. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  495. bytes_per_pixel = 3;
  496. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  497. bytes_per_pixel = 4;
  498. else
  499. return;
  500. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  501. {
  502. *(rp) = (png_byte)((*rp - *(rp + 1)) & 0xff);
  503. *(rp + 2) = (png_byte)((*(rp + 2) - *(rp + 1)) & 0xff);
  504. }
  505. }
  506. #ifdef PNG_WRITE_16BIT_SUPPORTED
  507. else if (row_info->bit_depth == 16)
  508. {
  509. png_bytep rp;
  510. png_uint_32 i;
  511. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  512. bytes_per_pixel = 6;
  513. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  514. bytes_per_pixel = 8;
  515. else
  516. return;
  517. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  518. {
  519. png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
  520. png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
  521. png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
  522. png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
  523. png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
  524. *(rp ) = (png_byte)((red >> 8) & 0xff);
  525. *(rp + 1) = (png_byte)(red & 0xff);
  526. *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
  527. *(rp + 5) = (png_byte)(blue & 0xff);
  528. }
  529. }
  530. #endif /* PNG_WRITE_16BIT_SUPPORTED */
  531. }
  532. }
  533. #endif /* PNG_MNG_FEATURES_SUPPORTED */
  534. #endif /* PNG_WRITE_SUPPORTED */