/src/compiler/android/jni/ftk/pngwtran.c

http://ftk.googlecode.com/ · C · 582 lines · 480 code · 43 blank · 59 comment · 122 complexity · b856783411fb805fb1517905428b2d54 MD5 · raw file

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