pngwtran.c 14 KB

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