Counter Strike : Global Offensive Source Code
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.

631 lines
17 KiB

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