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.

4303 lines
135 KiB

  1. /* pngrtran.c - transforms the data in a row for PNG readers
  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. * This file contains functions optionally called by an application
  13. * in order to tell libpng how to handle data when reading a PNG.
  14. * Transformations that are used in both reading and writing are
  15. * in pngtrans.c.
  16. */
  17. #include "pngpriv.h"
  18. #ifdef PNG_READ_SUPPORTED
  19. /* Set the action on getting a CRC error for an ancillary or critical chunk. */
  20. void PNGAPI
  21. png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
  22. {
  23. png_debug(1, "in png_set_crc_action");
  24. if (png_ptr == NULL)
  25. return;
  26. /* Tell libpng how we react to CRC errors in critical chunks */
  27. switch (crit_action)
  28. {
  29. case PNG_CRC_NO_CHANGE: /* Leave setting as is */
  30. break;
  31. case PNG_CRC_WARN_USE: /* Warn/use data */
  32. png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  33. png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
  34. break;
  35. case PNG_CRC_QUIET_USE: /* Quiet/use data */
  36. png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  37. png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
  38. PNG_FLAG_CRC_CRITICAL_IGNORE;
  39. break;
  40. case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
  41. png_warning(png_ptr,
  42. "Can't discard critical data on CRC error");
  43. case PNG_CRC_ERROR_QUIT: /* Error/quit */
  44. case PNG_CRC_DEFAULT:
  45. default:
  46. png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  47. break;
  48. }
  49. /* Tell libpng how we react to CRC errors in ancillary chunks */
  50. switch (ancil_action)
  51. {
  52. case PNG_CRC_NO_CHANGE: /* Leave setting as is */
  53. break;
  54. case PNG_CRC_WARN_USE: /* Warn/use data */
  55. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  56. png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
  57. break;
  58. case PNG_CRC_QUIET_USE: /* Quiet/use data */
  59. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  60. png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
  61. PNG_FLAG_CRC_ANCILLARY_NOWARN;
  62. break;
  63. case PNG_CRC_ERROR_QUIT: /* Error/quit */
  64. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  65. png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
  66. break;
  67. case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
  68. case PNG_CRC_DEFAULT:
  69. default:
  70. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  71. break;
  72. }
  73. }
  74. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  75. /* Handle alpha and tRNS via a background color */
  76. void PNGFAPI
  77. png_set_background_fixed(png_structp png_ptr,
  78. png_const_color_16p background_color, int background_gamma_code,
  79. int need_expand, png_fixed_point background_gamma)
  80. {
  81. png_debug(1, "in png_set_background_fixed");
  82. if (png_ptr == NULL)
  83. return;
  84. if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
  85. {
  86. png_warning(png_ptr, "Application must supply a known background gamma");
  87. return;
  88. }
  89. png_ptr->transformations |= PNG_BACKGROUND;
  90. png_memcpy(&(png_ptr->background), background_color,
  91. png_sizeof(png_color_16));
  92. png_ptr->background_gamma = background_gamma;
  93. png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
  94. png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
  95. }
  96. # ifdef PNG_FLOATING_POINT_SUPPORTED
  97. void PNGAPI
  98. png_set_background(png_structp png_ptr,
  99. png_const_color_16p background_color, int background_gamma_code,
  100. int need_expand, double background_gamma)
  101. {
  102. png_set_background_fixed(png_ptr, background_color, background_gamma_code,
  103. need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
  104. }
  105. # endif /* FLOATING_POINT */
  106. #endif /* READ_BACKGROUND */
  107. #ifdef PNG_READ_16_TO_8_SUPPORTED
  108. /* Strip 16 bit depth files to 8 bit depth */
  109. void PNGAPI
  110. png_set_strip_16(png_structp png_ptr)
  111. {
  112. png_debug(1, "in png_set_strip_16");
  113. if (png_ptr == NULL)
  114. return;
  115. png_ptr->transformations |= PNG_16_TO_8;
  116. png_ptr->transformations &= ~PNG_EXPAND_16;
  117. }
  118. #endif
  119. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  120. void PNGAPI
  121. png_set_strip_alpha(png_structp png_ptr)
  122. {
  123. png_debug(1, "in png_set_strip_alpha");
  124. if (png_ptr == NULL)
  125. return;
  126. png_ptr->transformations |= PNG_STRIP_ALPHA;
  127. }
  128. #endif
  129. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  130. /* Dither file to 8 bit. Supply a palette, the current number
  131. * of elements in the palette, the maximum number of elements
  132. * allowed, and a histogram if possible. If the current number
  133. * of colors is greater then the maximum number, the palette will be
  134. * modified to fit in the maximum number. "full_quantize" indicates
  135. * whether we need a quantizing cube set up for RGB images, or if we
  136. * simply are reducing the number of colors in a paletted image.
  137. */
  138. typedef struct png_dsort_struct
  139. {
  140. struct png_dsort_struct FAR * next;
  141. png_byte left;
  142. png_byte right;
  143. } png_dsort;
  144. typedef png_dsort FAR * png_dsortp;
  145. typedef png_dsort FAR * FAR * png_dsortpp;
  146. void PNGAPI
  147. png_set_quantize(png_structp png_ptr, png_colorp palette,
  148. int num_palette, int maximum_colors, png_const_uint_16p histogram,
  149. int full_quantize)
  150. {
  151. png_debug(1, "in png_set_quantize");
  152. if (png_ptr == NULL)
  153. return;
  154. png_ptr->transformations |= PNG_QUANTIZE;
  155. if (!full_quantize)
  156. {
  157. int i;
  158. png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
  159. (png_uint_32)(num_palette * png_sizeof(png_byte)));
  160. for (i = 0; i < num_palette; i++)
  161. png_ptr->quantize_index[i] = (png_byte)i;
  162. }
  163. if (num_palette > maximum_colors)
  164. {
  165. if (histogram != NULL)
  166. {
  167. /* This is easy enough, just throw out the least used colors.
  168. * Perhaps not the best solution, but good enough.
  169. */
  170. int i;
  171. /* Initialize an array to sort colors */
  172. png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
  173. (png_uint_32)(num_palette * png_sizeof(png_byte)));
  174. /* Initialize the quantize_sort array */
  175. for (i = 0; i < num_palette; i++)
  176. png_ptr->quantize_sort[i] = (png_byte)i;
  177. /* Find the least used palette entries by starting a
  178. * bubble sort, and running it until we have sorted
  179. * out enough colors. Note that we don't care about
  180. * sorting all the colors, just finding which are
  181. * least used.
  182. */
  183. for (i = num_palette - 1; i >= maximum_colors; i--)
  184. {
  185. int done; /* To stop early if the list is pre-sorted */
  186. int j;
  187. done = 1;
  188. for (j = 0; j < i; j++)
  189. {
  190. if (histogram[png_ptr->quantize_sort[j]]
  191. < histogram[png_ptr->quantize_sort[j + 1]])
  192. {
  193. png_byte t;
  194. t = png_ptr->quantize_sort[j];
  195. png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
  196. png_ptr->quantize_sort[j + 1] = t;
  197. done = 0;
  198. }
  199. }
  200. if (done)
  201. break;
  202. }
  203. /* Swap the palette around, and set up a table, if necessary */
  204. if (full_quantize)
  205. {
  206. int j = num_palette;
  207. /* Put all the useful colors within the max, but don't
  208. * move the others.
  209. */
  210. for (i = 0; i < maximum_colors; i++)
  211. {
  212. if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
  213. {
  214. do
  215. j--;
  216. while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
  217. palette[i] = palette[j];
  218. }
  219. }
  220. }
  221. else
  222. {
  223. int j = num_palette;
  224. /* Move all the used colors inside the max limit, and
  225. * develop a translation table.
  226. */
  227. for (i = 0; i < maximum_colors; i++)
  228. {
  229. /* Only move the colors we need to */
  230. if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
  231. {
  232. png_color tmp_color;
  233. do
  234. j--;
  235. while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
  236. tmp_color = palette[j];
  237. palette[j] = palette[i];
  238. palette[i] = tmp_color;
  239. /* Indicate where the color went */
  240. png_ptr->quantize_index[j] = (png_byte)i;
  241. png_ptr->quantize_index[i] = (png_byte)j;
  242. }
  243. }
  244. /* Find closest color for those colors we are not using */
  245. for (i = 0; i < num_palette; i++)
  246. {
  247. if ((int)png_ptr->quantize_index[i] >= maximum_colors)
  248. {
  249. int min_d, k, min_k, d_index;
  250. /* Find the closest color to one we threw out */
  251. d_index = png_ptr->quantize_index[i];
  252. min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
  253. for (k = 1, min_k = 0; k < maximum_colors; k++)
  254. {
  255. int d;
  256. d = PNG_COLOR_DIST(palette[d_index], palette[k]);
  257. if (d < min_d)
  258. {
  259. min_d = d;
  260. min_k = k;
  261. }
  262. }
  263. /* Point to closest color */
  264. png_ptr->quantize_index[i] = (png_byte)min_k;
  265. }
  266. }
  267. }
  268. png_free(png_ptr, png_ptr->quantize_sort);
  269. png_ptr->quantize_sort = NULL;
  270. }
  271. else
  272. {
  273. /* This is much harder to do simply (and quickly). Perhaps
  274. * we need to go through a median cut routine, but those
  275. * don't always behave themselves with only a few colors
  276. * as input. So we will just find the closest two colors,
  277. * and throw out one of them (chosen somewhat randomly).
  278. * [We don't understand this at all, so if someone wants to
  279. * work on improving it, be our guest - AED, GRP]
  280. */
  281. int i;
  282. int max_d;
  283. int num_new_palette;
  284. png_dsortp t;
  285. png_dsortpp hash;
  286. t = NULL;
  287. /* Initialize palette index arrays */
  288. png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
  289. (png_uint_32)(num_palette * png_sizeof(png_byte)));
  290. png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
  291. (png_uint_32)(num_palette * png_sizeof(png_byte)));
  292. /* Initialize the sort array */
  293. for (i = 0; i < num_palette; i++)
  294. {
  295. png_ptr->index_to_palette[i] = (png_byte)i;
  296. png_ptr->palette_to_index[i] = (png_byte)i;
  297. }
  298. hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
  299. png_sizeof(png_dsortp)));
  300. num_new_palette = num_palette;
  301. /* Initial wild guess at how far apart the farthest pixel
  302. * pair we will be eliminating will be. Larger
  303. * numbers mean more areas will be allocated, Smaller
  304. * numbers run the risk of not saving enough data, and
  305. * having to do this all over again.
  306. *
  307. * I have not done extensive checking on this number.
  308. */
  309. max_d = 96;
  310. while (num_new_palette > maximum_colors)
  311. {
  312. for (i = 0; i < num_new_palette - 1; i++)
  313. {
  314. int j;
  315. for (j = i + 1; j < num_new_palette; j++)
  316. {
  317. int d;
  318. d = PNG_COLOR_DIST(palette[i], palette[j]);
  319. if (d <= max_d)
  320. {
  321. t = (png_dsortp)png_malloc_warn(png_ptr,
  322. (png_uint_32)(png_sizeof(png_dsort)));
  323. if (t == NULL)
  324. break;
  325. t->next = hash[d];
  326. t->left = (png_byte)i;
  327. t->right = (png_byte)j;
  328. hash[d] = t;
  329. }
  330. }
  331. if (t == NULL)
  332. break;
  333. }
  334. if (t != NULL)
  335. for (i = 0; i <= max_d; i++)
  336. {
  337. if (hash[i] != NULL)
  338. {
  339. png_dsortp p;
  340. for (p = hash[i]; p; p = p->next)
  341. {
  342. if ((int)png_ptr->index_to_palette[p->left]
  343. < num_new_palette &&
  344. (int)png_ptr->index_to_palette[p->right]
  345. < num_new_palette)
  346. {
  347. int j, next_j;
  348. if (num_new_palette & 0x01)
  349. {
  350. j = p->left;
  351. next_j = p->right;
  352. }
  353. else
  354. {
  355. j = p->right;
  356. next_j = p->left;
  357. }
  358. num_new_palette--;
  359. palette[png_ptr->index_to_palette[j]]
  360. = palette[num_new_palette];
  361. if (!full_quantize)
  362. {
  363. int k;
  364. for (k = 0; k < num_palette; k++)
  365. {
  366. if (png_ptr->quantize_index[k] ==
  367. png_ptr->index_to_palette[j])
  368. png_ptr->quantize_index[k] =
  369. png_ptr->index_to_palette[next_j];
  370. if ((int)png_ptr->quantize_index[k] ==
  371. num_new_palette)
  372. png_ptr->quantize_index[k] =
  373. png_ptr->index_to_palette[j];
  374. }
  375. }
  376. png_ptr->index_to_palette[png_ptr->palette_to_index
  377. [num_new_palette]] = png_ptr->index_to_palette[j];
  378. png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
  379. = png_ptr->palette_to_index[num_new_palette];
  380. png_ptr->index_to_palette[j] =
  381. (png_byte)num_new_palette;
  382. png_ptr->palette_to_index[num_new_palette] =
  383. (png_byte)j;
  384. }
  385. if (num_new_palette <= maximum_colors)
  386. break;
  387. }
  388. if (num_new_palette <= maximum_colors)
  389. break;
  390. }
  391. }
  392. for (i = 0; i < 769; i++)
  393. {
  394. if (hash[i] != NULL)
  395. {
  396. png_dsortp p = hash[i];
  397. while (p)
  398. {
  399. t = p->next;
  400. png_free(png_ptr, p);
  401. p = t;
  402. }
  403. }
  404. hash[i] = 0;
  405. }
  406. max_d += 96;
  407. }
  408. png_free(png_ptr, hash);
  409. png_free(png_ptr, png_ptr->palette_to_index);
  410. png_free(png_ptr, png_ptr->index_to_palette);
  411. png_ptr->palette_to_index = NULL;
  412. png_ptr->index_to_palette = NULL;
  413. }
  414. num_palette = maximum_colors;
  415. }
  416. if (png_ptr->palette == NULL)
  417. {
  418. png_ptr->palette = palette;
  419. }
  420. png_ptr->num_palette = (png_uint_16)num_palette;
  421. if (full_quantize)
  422. {
  423. int i;
  424. png_bytep distance;
  425. int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
  426. PNG_QUANTIZE_BLUE_BITS;
  427. int num_red = (1 << PNG_QUANTIZE_RED_BITS);
  428. int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
  429. int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
  430. png_size_t num_entries = ((png_size_t)1 << total_bits);
  431. png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
  432. (png_uint_32)(num_entries * png_sizeof(png_byte)));
  433. distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
  434. png_sizeof(png_byte)));
  435. png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
  436. for (i = 0; i < num_palette; i++)
  437. {
  438. int ir, ig, ib;
  439. int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
  440. int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
  441. int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));
  442. for (ir = 0; ir < num_red; ir++)
  443. {
  444. /* int dr = abs(ir - r); */
  445. int dr = ((ir > r) ? ir - r : r - ir);
  446. int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
  447. PNG_QUANTIZE_GREEN_BITS));
  448. for (ig = 0; ig < num_green; ig++)
  449. {
  450. /* int dg = abs(ig - g); */
  451. int dg = ((ig > g) ? ig - g : g - ig);
  452. int dt = dr + dg;
  453. int dm = ((dr > dg) ? dr : dg);
  454. int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);
  455. for (ib = 0; ib < num_blue; ib++)
  456. {
  457. int d_index = index_g | ib;
  458. /* int db = abs(ib - b); */
  459. int db = ((ib > b) ? ib - b : b - ib);
  460. int dmax = ((dm > db) ? dm : db);
  461. int d = dmax + dt + db;
  462. if (d < (int)distance[d_index])
  463. {
  464. distance[d_index] = (png_byte)d;
  465. png_ptr->palette_lookup[d_index] = (png_byte)i;
  466. }
  467. }
  468. }
  469. }
  470. }
  471. png_free(png_ptr, distance);
  472. }
  473. }
  474. #endif /* PNG_READ_QUANTIZE_SUPPORTED */
  475. #ifdef PNG_READ_GAMMA_SUPPORTED
  476. /* Transform the image from the file_gamma to the screen_gamma. We
  477. * only do transformations on images where the file_gamma and screen_gamma
  478. * are not close reciprocals, otherwise it slows things down slightly, and
  479. * also needlessly introduces small errors.
  480. *
  481. * We will turn off gamma transformation later if no semitransparent entries
  482. * are present in the tRNS array for palette images. We can't do it here
  483. * because we don't necessarily have the tRNS chunk yet.
  484. */
  485. static int /* PRIVATE */
  486. png_gamma_threshold(png_fixed_point scrn_gamma, png_fixed_point file_gamma)
  487. {
  488. /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
  489. * correction as a difference of the overall transform from 1.0
  490. *
  491. * We want to compare the threshold with s*f - 1, if we get
  492. * overflow here it is because of wacky gamma values so we
  493. * turn on processing anyway.
  494. */
  495. png_fixed_point gtest;
  496. return !png_muldiv(&gtest, scrn_gamma, file_gamma, PNG_FP_1) ||
  497. png_gamma_significant(gtest);
  498. }
  499. void PNGFAPI
  500. png_set_gamma_fixed(png_structp png_ptr, png_fixed_point scrn_gamma,
  501. png_fixed_point file_gamma)
  502. {
  503. png_debug(1, "in png_set_gamma_fixed");
  504. if (png_ptr == NULL)
  505. return;
  506. if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
  507. (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
  508. png_gamma_threshold(scrn_gamma, file_gamma))
  509. png_ptr->transformations |= PNG_GAMMA;
  510. png_ptr->gamma = file_gamma;
  511. png_ptr->screen_gamma = scrn_gamma;
  512. }
  513. # ifdef PNG_FLOATING_POINT_SUPPORTED
  514. void PNGAPI
  515. png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
  516. {
  517. png_set_gamma_fixed(png_ptr,
  518. png_fixed(png_ptr, scrn_gamma, "png_set_gamma screen gamma"),
  519. png_fixed(png_ptr, file_gamma, "png_set_gamma file gamma"));
  520. }
  521. # endif /* FLOATING_POINT_SUPPORTED */
  522. #endif /* READ_GAMMA */
  523. #ifdef PNG_READ_EXPAND_SUPPORTED
  524. /* Expand paletted images to RGB, expand grayscale images of
  525. * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
  526. * to alpha channels.
  527. */
  528. void PNGAPI
  529. png_set_expand(png_structp png_ptr)
  530. {
  531. png_debug(1, "in png_set_expand");
  532. if (png_ptr == NULL)
  533. return;
  534. png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  535. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  536. }
  537. /* GRR 19990627: the following three functions currently are identical
  538. * to png_set_expand(). However, it is entirely reasonable that someone
  539. * might wish to expand an indexed image to RGB but *not* expand a single,
  540. * fully transparent palette entry to a full alpha channel--perhaps instead
  541. * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
  542. * the transparent color with a particular RGB value, or drop tRNS entirely.
  543. * IOW, a future version of the library may make the transformations flag
  544. * a bit more fine-grained, with separate bits for each of these three
  545. * functions.
  546. *
  547. * More to the point, these functions make it obvious what libpng will be
  548. * doing, whereas "expand" can (and does) mean any number of things.
  549. *
  550. * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
  551. * to expand only the sample depth but not to expand the tRNS to alpha
  552. * and its name was changed to png_set_expand_gray_1_2_4_to_8().
  553. */
  554. /* Expand paletted images to RGB. */
  555. void PNGAPI
  556. png_set_palette_to_rgb(png_structp png_ptr)
  557. {
  558. png_debug(1, "in png_set_palette_to_rgb");
  559. if (png_ptr == NULL)
  560. return;
  561. png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  562. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  563. }
  564. /* Expand grayscale images of less than 8-bit depth to 8 bits. */
  565. void PNGAPI
  566. png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
  567. {
  568. png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
  569. if (png_ptr == NULL)
  570. return;
  571. png_ptr->transformations |= PNG_EXPAND;
  572. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  573. }
  574. /* Expand tRNS chunks to alpha channels. */
  575. void PNGAPI
  576. png_set_tRNS_to_alpha(png_structp png_ptr)
  577. {
  578. png_debug(1, "in png_set_tRNS_to_alpha");
  579. png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  580. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  581. }
  582. #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
  583. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  584. /* Expand to 16 bit channels, expand the tRNS chunk too (because otherwise
  585. * it may not work correctly.)
  586. */
  587. void PNGAPI
  588. png_set_expand_16(png_structp png_ptr)
  589. {
  590. png_debug(1, "in png_set_expand_16");
  591. if (png_ptr == NULL)
  592. return;
  593. png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
  594. png_ptr->transformations &= ~PNG_16_TO_8;
  595. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  596. }
  597. #endif
  598. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  599. void PNGAPI
  600. png_set_gray_to_rgb(png_structp png_ptr)
  601. {
  602. png_debug(1, "in png_set_gray_to_rgb");
  603. if (png_ptr != NULL)
  604. {
  605. /* Because rgb must be 8 bits or more: */
  606. png_set_expand_gray_1_2_4_to_8(png_ptr);
  607. png_ptr->transformations |= PNG_GRAY_TO_RGB;
  608. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  609. }
  610. }
  611. #endif
  612. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  613. void PNGFAPI
  614. png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
  615. png_fixed_point red, png_fixed_point green)
  616. {
  617. png_debug(1, "in png_set_rgb_to_gray");
  618. if (png_ptr == NULL)
  619. return;
  620. switch(error_action)
  621. {
  622. case 1:
  623. png_ptr->transformations |= PNG_RGB_TO_GRAY;
  624. break;
  625. case 2:
  626. png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
  627. break;
  628. case 3:
  629. png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
  630. break;
  631. default:
  632. png_error(png_ptr, "invalid error action to rgb_to_gray");
  633. break;
  634. }
  635. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  636. #ifdef PNG_READ_EXPAND_SUPPORTED
  637. png_ptr->transformations |= PNG_EXPAND;
  638. #else
  639. {
  640. png_warning(png_ptr,
  641. "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
  642. png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
  643. }
  644. #endif
  645. {
  646. png_uint_16 red_int, green_int;
  647. if (red < 0 || green < 0)
  648. {
  649. red_int = 6968; /* .212671 * 32768 + .5 */
  650. green_int = 23434; /* .715160 * 32768 + .5 */
  651. }
  652. else if (red + green < 100000L)
  653. {
  654. red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
  655. green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
  656. }
  657. else
  658. {
  659. png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
  660. red_int = 6968;
  661. green_int = 23434;
  662. }
  663. png_ptr->rgb_to_gray_red_coeff = red_int;
  664. png_ptr->rgb_to_gray_green_coeff = green_int;
  665. png_ptr->rgb_to_gray_blue_coeff =
  666. (png_uint_16)(32768 - red_int - green_int);
  667. }
  668. }
  669. #ifdef PNG_FLOATING_POINT_SUPPORTED
  670. /* Convert a RGB image to a grayscale of the same width. This allows us,
  671. * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
  672. */
  673. void PNGAPI
  674. png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
  675. double green)
  676. {
  677. if (png_ptr == NULL)
  678. return;
  679. png_set_rgb_to_gray_fixed(png_ptr, error_action,
  680. png_fixed(png_ptr, red, "rgb to gray red coefficient"),
  681. png_fixed(png_ptr, green, "rgb to gray green coefficient"));
  682. }
  683. #endif /* FLOATING POINT */
  684. #endif
  685. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  686. defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  687. void PNGAPI
  688. png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
  689. read_user_transform_fn)
  690. {
  691. png_debug(1, "in png_set_read_user_transform_fn");
  692. if (png_ptr == NULL)
  693. return;
  694. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  695. png_ptr->transformations |= PNG_USER_TRANSFORM;
  696. png_ptr->read_user_transform_fn = read_user_transform_fn;
  697. #endif
  698. }
  699. #endif
  700. /* Initialize everything needed for the read. This includes modifying
  701. * the palette.
  702. */
  703. void /* PRIVATE */
  704. png_init_read_transformations(png_structp png_ptr)
  705. {
  706. png_debug(1, "in png_init_read_transformations");
  707. {
  708. #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
  709. defined(PNG_READ_SHIFT_SUPPORTED) || \
  710. defined(PNG_READ_GAMMA_SUPPORTED)
  711. int color_type = png_ptr->color_type;
  712. #endif
  713. #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
  714. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  715. /* Detect gray background and attempt to enable optimization
  716. * for gray --> RGB case
  717. *
  718. * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
  719. * RGB_ALPHA (in which case need_expand is superfluous anyway), the
  720. * background color might actually be gray yet not be flagged as such.
  721. * This is not a problem for the current code, which uses
  722. * PNG_BACKGROUND_IS_GRAY only to decide when to do the
  723. * png_do_gray_to_rgb() transformation.
  724. */
  725. if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  726. !(color_type & PNG_COLOR_MASK_COLOR))
  727. {
  728. png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
  729. }
  730. else if ((png_ptr->transformations & PNG_BACKGROUND) &&
  731. !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  732. (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
  733. png_ptr->background.red == png_ptr->background.green &&
  734. png_ptr->background.red == png_ptr->background.blue)
  735. {
  736. png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
  737. png_ptr->background.gray = png_ptr->background.red;
  738. }
  739. #endif
  740. if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  741. (png_ptr->transformations & PNG_EXPAND))
  742. {
  743. if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */
  744. {
  745. /* Expand background and tRNS chunks */
  746. switch (png_ptr->bit_depth)
  747. {
  748. case 1:
  749. png_ptr->background.gray *= (png_uint_16)0xff;
  750. png_ptr->background.red = png_ptr->background.green
  751. = png_ptr->background.blue = png_ptr->background.gray;
  752. if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  753. {
  754. png_ptr->trans_color.gray *= (png_uint_16)0xff;
  755. png_ptr->trans_color.red = png_ptr->trans_color.green
  756. = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
  757. }
  758. break;
  759. case 2:
  760. png_ptr->background.gray *= (png_uint_16)0x55;
  761. png_ptr->background.red = png_ptr->background.green
  762. = png_ptr->background.blue = png_ptr->background.gray;
  763. if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  764. {
  765. png_ptr->trans_color.gray *= (png_uint_16)0x55;
  766. png_ptr->trans_color.red = png_ptr->trans_color.green
  767. = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
  768. }
  769. break;
  770. case 4:
  771. png_ptr->background.gray *= (png_uint_16)0x11;
  772. png_ptr->background.red = png_ptr->background.green
  773. = png_ptr->background.blue = png_ptr->background.gray;
  774. if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  775. {
  776. png_ptr->trans_color.gray *= (png_uint_16)0x11;
  777. png_ptr->trans_color.red = png_ptr->trans_color.green
  778. = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
  779. }
  780. break;
  781. default:
  782. case 8:
  783. case 16:
  784. png_ptr->background.red = png_ptr->background.green
  785. = png_ptr->background.blue = png_ptr->background.gray;
  786. break;
  787. }
  788. }
  789. else if (color_type == PNG_COLOR_TYPE_PALETTE)
  790. {
  791. png_ptr->background.red =
  792. png_ptr->palette[png_ptr->background.index].red;
  793. png_ptr->background.green =
  794. png_ptr->palette[png_ptr->background.index].green;
  795. png_ptr->background.blue =
  796. png_ptr->palette[png_ptr->background.index].blue;
  797. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  798. if (png_ptr->transformations & PNG_INVERT_ALPHA)
  799. {
  800. #ifdef PNG_READ_EXPAND_SUPPORTED
  801. if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  802. #endif
  803. {
  804. /* Invert the alpha channel (in tRNS) unless the pixels are
  805. * going to be expanded, in which case leave it for later
  806. */
  807. int i, istop;
  808. istop=(int)png_ptr->num_trans;
  809. for (i=0; i<istop; i++)
  810. png_ptr->trans_alpha[i] = (png_byte)(255 -
  811. png_ptr->trans_alpha[i]);
  812. }
  813. }
  814. #endif
  815. }
  816. }
  817. #endif
  818. #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  819. png_ptr->background_1 = png_ptr->background;
  820. #endif
  821. #ifdef PNG_READ_GAMMA_SUPPORTED
  822. if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
  823. && png_gamma_threshold(png_ptr->screen_gamma, png_ptr->gamma))
  824. {
  825. int i, k;
  826. k=0;
  827. for (i=0; i<png_ptr->num_trans; i++)
  828. {
  829. if (png_ptr->trans_alpha[i] != 0 && png_ptr->trans_alpha[i] != 0xff)
  830. k=1; /* Partial transparency is present */
  831. }
  832. if (k == 0)
  833. png_ptr->transformations &= ~PNG_GAMMA;
  834. }
  835. if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
  836. png_ptr->gamma != 0)
  837. {
  838. png_build_gamma_table(png_ptr, png_ptr->bit_depth);
  839. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  840. if (png_ptr->transformations & PNG_BACKGROUND)
  841. {
  842. if (color_type == PNG_COLOR_TYPE_PALETTE)
  843. {
  844. /* Could skip if no transparency */
  845. png_color back, back_1;
  846. png_colorp palette = png_ptr->palette;
  847. int num_palette = png_ptr->num_palette;
  848. int i;
  849. if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
  850. {
  851. back.red = png_ptr->gamma_table[png_ptr->background.red];
  852. back.green = png_ptr->gamma_table[png_ptr->background.green];
  853. back.blue = png_ptr->gamma_table[png_ptr->background.blue];
  854. back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
  855. back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
  856. back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
  857. }
  858. else
  859. {
  860. png_fixed_point g, gs;
  861. switch (png_ptr->background_gamma_type)
  862. {
  863. case PNG_BACKGROUND_GAMMA_SCREEN:
  864. g = (png_ptr->screen_gamma);
  865. gs = PNG_FP_1;
  866. break;
  867. case PNG_BACKGROUND_GAMMA_FILE:
  868. g = png_reciprocal(png_ptr->gamma);
  869. gs = png_reciprocal2(png_ptr->gamma,
  870. png_ptr->screen_gamma);
  871. break;
  872. case PNG_BACKGROUND_GAMMA_UNIQUE:
  873. g = png_reciprocal(png_ptr->background_gamma);
  874. gs = png_reciprocal2(png_ptr->background_gamma,
  875. png_ptr->screen_gamma);
  876. break;
  877. default:
  878. g = PNG_FP_1; /* back_1 */
  879. gs = PNG_FP_1; /* back */
  880. break;
  881. }
  882. if (png_gamma_significant(gs))
  883. {
  884. back.red = (png_byte)png_ptr->background.red;
  885. back.green = (png_byte)png_ptr->background.green;
  886. back.blue = (png_byte)png_ptr->background.blue;
  887. }
  888. else
  889. {
  890. back.red = png_gamma_8bit_correct(png_ptr->background.red,
  891. gs);
  892. back.green = png_gamma_8bit_correct(png_ptr->background.green,
  893. gs);
  894. back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
  895. gs);
  896. }
  897. back_1.red = png_gamma_8bit_correct(png_ptr->background.red, g);
  898. back_1.green = png_gamma_8bit_correct(png_ptr->background.green,
  899. g);
  900. back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
  901. g);
  902. }
  903. for (i = 0; i < num_palette; i++)
  904. {
  905. if (i < (int)png_ptr->num_trans &&
  906. png_ptr->trans_alpha[i] != 0xff)
  907. {
  908. if (png_ptr->trans_alpha[i] == 0)
  909. {
  910. palette[i] = back;
  911. }
  912. else /* if (png_ptr->trans_alpha[i] != 0xff) */
  913. {
  914. png_byte v, w;
  915. v = png_ptr->gamma_to_1[palette[i].red];
  916. png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
  917. palette[i].red = png_ptr->gamma_from_1[w];
  918. v = png_ptr->gamma_to_1[palette[i].green];
  919. png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
  920. palette[i].green = png_ptr->gamma_from_1[w];
  921. v = png_ptr->gamma_to_1[palette[i].blue];
  922. png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
  923. palette[i].blue = png_ptr->gamma_from_1[w];
  924. }
  925. }
  926. else
  927. {
  928. palette[i].red = png_ptr->gamma_table[palette[i].red];
  929. palette[i].green = png_ptr->gamma_table[palette[i].green];
  930. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  931. }
  932. }
  933. /* Prevent the transformations being done again, and make sure
  934. * that the now spurious alpha channel is stripped - the code
  935. * has just reduced background composition and gamma correction
  936. * to a simple alpha channel strip.
  937. */
  938. png_ptr->transformations &= ~PNG_BACKGROUND;
  939. png_ptr->transformations &= ~PNG_GAMMA;
  940. png_ptr->transformations |= PNG_STRIP_ALPHA;
  941. }
  942. /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
  943. else
  944. /* color_type != PNG_COLOR_TYPE_PALETTE */
  945. {
  946. png_fixed_point g = PNG_FP_1;
  947. png_fixed_point gs = PNG_FP_1;
  948. switch (png_ptr->background_gamma_type)
  949. {
  950. case PNG_BACKGROUND_GAMMA_SCREEN:
  951. g = png_ptr->screen_gamma;
  952. /* gs = PNG_FP_1; */
  953. break;
  954. case PNG_BACKGROUND_GAMMA_FILE:
  955. g = png_reciprocal(png_ptr->gamma);
  956. gs = png_reciprocal2(png_ptr->gamma, png_ptr->screen_gamma);
  957. break;
  958. case PNG_BACKGROUND_GAMMA_UNIQUE:
  959. g = png_reciprocal(png_ptr->background_gamma);
  960. gs = png_reciprocal2(png_ptr->background_gamma,
  961. png_ptr->screen_gamma);
  962. break;
  963. default:
  964. png_error(png_ptr, "invalid background gamma type");
  965. }
  966. png_ptr->background_1.gray = png_gamma_correct(png_ptr,
  967. png_ptr->background.gray, g);
  968. png_ptr->background.gray = png_gamma_correct(png_ptr,
  969. png_ptr->background.gray, gs);
  970. if ((png_ptr->background.red != png_ptr->background.green) ||
  971. (png_ptr->background.red != png_ptr->background.blue) ||
  972. (png_ptr->background.red != png_ptr->background.gray))
  973. {
  974. /* RGB or RGBA with color background */
  975. png_ptr->background_1.red = png_gamma_correct(png_ptr,
  976. png_ptr->background.red, g);
  977. png_ptr->background_1.green = png_gamma_correct(png_ptr,
  978. png_ptr->background.green, g);
  979. png_ptr->background_1.blue = png_gamma_correct(png_ptr,
  980. png_ptr->background.blue, g);
  981. png_ptr->background.red = png_gamma_correct(png_ptr,
  982. png_ptr->background.red, gs);
  983. png_ptr->background.green = png_gamma_correct(png_ptr,
  984. png_ptr->background.green, gs);
  985. png_ptr->background.blue = png_gamma_correct(png_ptr,
  986. png_ptr->background.blue, gs);
  987. }
  988. else
  989. {
  990. /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
  991. png_ptr->background_1.red = png_ptr->background_1.green
  992. = png_ptr->background_1.blue = png_ptr->background_1.gray;
  993. png_ptr->background.red = png_ptr->background.green
  994. = png_ptr->background.blue = png_ptr->background.gray;
  995. }
  996. }
  997. }
  998. else
  999. /* Transformation does not include PNG_BACKGROUND */
  1000. #endif /* PNG_READ_BACKGROUND_SUPPORTED */
  1001. if (color_type == PNG_COLOR_TYPE_PALETTE)
  1002. {
  1003. png_colorp palette = png_ptr->palette;
  1004. int num_palette = png_ptr->num_palette;
  1005. int i;
  1006. for (i = 0; i < num_palette; i++)
  1007. {
  1008. palette[i].red = png_ptr->gamma_table[palette[i].red];
  1009. palette[i].green = png_ptr->gamma_table[palette[i].green];
  1010. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  1011. }
  1012. /* Done the gamma correction. */
  1013. png_ptr->transformations &= ~PNG_GAMMA;
  1014. }
  1015. }
  1016. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1017. else
  1018. #endif
  1019. #endif /* PNG_READ_GAMMA_SUPPORTED */
  1020. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1021. /* No GAMMA transformation */
  1022. if ((png_ptr->transformations & PNG_BACKGROUND) &&
  1023. (color_type == PNG_COLOR_TYPE_PALETTE))
  1024. {
  1025. int i;
  1026. int istop = (int)png_ptr->num_trans;
  1027. png_color back;
  1028. png_colorp palette = png_ptr->palette;
  1029. back.red = (png_byte)png_ptr->background.red;
  1030. back.green = (png_byte)png_ptr->background.green;
  1031. back.blue = (png_byte)png_ptr->background.blue;
  1032. for (i = 0; i < istop; i++)
  1033. {
  1034. if (png_ptr->trans_alpha[i] == 0)
  1035. {
  1036. palette[i] = back;
  1037. }
  1038. else if (png_ptr->trans_alpha[i] != 0xff)
  1039. {
  1040. /* The png_composite() macro is defined in png.h */
  1041. png_composite(palette[i].red, palette[i].red,
  1042. png_ptr->trans_alpha[i], back.red);
  1043. png_composite(palette[i].green, palette[i].green,
  1044. png_ptr->trans_alpha[i], back.green);
  1045. png_composite(palette[i].blue, palette[i].blue,
  1046. png_ptr->trans_alpha[i], back.blue);
  1047. }
  1048. }
  1049. /* Handled alpha, still need to strip the channel. */
  1050. png_ptr->transformations &= ~PNG_BACKGROUND;
  1051. png_ptr->transformations |= PNG_STRIP_ALPHA;
  1052. }
  1053. #endif /* PNG_READ_BACKGROUND_SUPPORTED */
  1054. #ifdef PNG_READ_SHIFT_SUPPORTED
  1055. if ((png_ptr->transformations & PNG_SHIFT) &&
  1056. (color_type == PNG_COLOR_TYPE_PALETTE))
  1057. {
  1058. png_uint_16 i;
  1059. png_uint_16 istop = png_ptr->num_palette;
  1060. int sr = 8 - png_ptr->sig_bit.red;
  1061. int sg = 8 - png_ptr->sig_bit.green;
  1062. int sb = 8 - png_ptr->sig_bit.blue;
  1063. if (sr < 0 || sr > 8)
  1064. sr = 0;
  1065. if (sg < 0 || sg > 8)
  1066. sg = 0;
  1067. if (sb < 0 || sb > 8)
  1068. sb = 0;
  1069. for (i = 0; i < istop; i++)
  1070. {
  1071. png_ptr->palette[i].red >>= sr;
  1072. png_ptr->palette[i].green >>= sg;
  1073. png_ptr->palette[i].blue >>= sb;
  1074. }
  1075. }
  1076. #endif /* PNG_READ_SHIFT_SUPPORTED */
  1077. }
  1078. #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
  1079. && !defined(PNG_READ_BACKGROUND_SUPPORTED)
  1080. if (png_ptr)
  1081. return;
  1082. #endif
  1083. }
  1084. /* Modify the info structure to reflect the transformations. The
  1085. * info should be updated so a PNG file could be written with it,
  1086. * assuming the transformations result in valid PNG data.
  1087. */
  1088. void /* PRIVATE */
  1089. png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
  1090. {
  1091. png_debug(1, "in png_read_transform_info");
  1092. #ifdef PNG_READ_EXPAND_SUPPORTED
  1093. if (png_ptr->transformations & PNG_EXPAND)
  1094. {
  1095. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1096. {
  1097. if (png_ptr->num_trans &&
  1098. (png_ptr->transformations & PNG_EXPAND_tRNS))
  1099. info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  1100. else
  1101. info_ptr->color_type = PNG_COLOR_TYPE_RGB;
  1102. info_ptr->bit_depth = 8;
  1103. info_ptr->num_trans = 0;
  1104. }
  1105. else
  1106. {
  1107. if (png_ptr->num_trans)
  1108. {
  1109. if (png_ptr->transformations & PNG_EXPAND_tRNS)
  1110. info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
  1111. }
  1112. if (info_ptr->bit_depth < 8)
  1113. info_ptr->bit_depth = 8;
  1114. info_ptr->num_trans = 0;
  1115. }
  1116. }
  1117. #endif
  1118. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  1119. if (png_ptr->transformations & PNG_EXPAND_16 && info_ptr->bit_depth == 8 &&
  1120. info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  1121. {
  1122. info_ptr->bit_depth = 16;
  1123. }
  1124. #endif
  1125. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1126. if (png_ptr->transformations & PNG_BACKGROUND)
  1127. {
  1128. info_ptr->color_type = (png_byte)(info_ptr->color_type &
  1129. ~PNG_COLOR_MASK_ALPHA);
  1130. info_ptr->num_trans = 0;
  1131. info_ptr->background = png_ptr->background;
  1132. }
  1133. #endif
  1134. #ifdef PNG_READ_GAMMA_SUPPORTED
  1135. if (png_ptr->transformations & PNG_GAMMA)
  1136. {
  1137. info_ptr->gamma = png_ptr->gamma;
  1138. }
  1139. #endif
  1140. #ifdef PNG_READ_16_TO_8_SUPPORTED
  1141. #ifdef PNG_READ_16BIT_SUPPORTED
  1142. if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
  1143. info_ptr->bit_depth = 8;
  1144. #else
  1145. /* Force chopping 16-bit input down to 8 */
  1146. if (info_ptr->bit_depth == 16)
  1147. {
  1148. png_ptr->transformations |=PNG_16_TO_8;
  1149. info_ptr->bit_depth = 8;
  1150. }
  1151. #endif
  1152. #endif
  1153. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1154. if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  1155. info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
  1156. #endif
  1157. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  1158. if (png_ptr->transformations & PNG_RGB_TO_GRAY)
  1159. info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
  1160. #endif
  1161. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  1162. if (png_ptr->transformations & PNG_QUANTIZE)
  1163. {
  1164. if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
  1165. (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
  1166. png_ptr->palette_lookup && info_ptr->bit_depth == 8)
  1167. {
  1168. info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
  1169. }
  1170. }
  1171. #endif
  1172. #ifdef PNG_READ_PACK_SUPPORTED
  1173. if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
  1174. info_ptr->bit_depth = 8;
  1175. #endif
  1176. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1177. info_ptr->channels = 1;
  1178. else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  1179. info_ptr->channels = 3;
  1180. else
  1181. info_ptr->channels = 1;
  1182. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  1183. if (png_ptr->transformations & PNG_STRIP_ALPHA)
  1184. info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
  1185. #endif
  1186. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  1187. info_ptr->channels++;
  1188. #ifdef PNG_READ_FILLER_SUPPORTED
  1189. /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
  1190. if ((png_ptr->transformations & PNG_FILLER) &&
  1191. ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
  1192. (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
  1193. {
  1194. info_ptr->channels++;
  1195. /* If adding a true alpha channel not just filler */
  1196. if (png_ptr->transformations & PNG_ADD_ALPHA)
  1197. info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
  1198. }
  1199. #endif
  1200. #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
  1201. defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1202. if (png_ptr->transformations & PNG_USER_TRANSFORM)
  1203. {
  1204. if (info_ptr->bit_depth < png_ptr->user_transform_depth)
  1205. info_ptr->bit_depth = png_ptr->user_transform_depth;
  1206. if (info_ptr->channels < png_ptr->user_transform_channels)
  1207. info_ptr->channels = png_ptr->user_transform_channels;
  1208. }
  1209. #endif
  1210. info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
  1211. info_ptr->bit_depth);
  1212. info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
  1213. #ifndef PNG_READ_EXPAND_SUPPORTED
  1214. if (png_ptr)
  1215. return;
  1216. #endif
  1217. }
  1218. /* Transform the row. The order of transformations is significant,
  1219. * and is very touchy. If you add a transformation, take care to
  1220. * decide how it fits in with the other transformations here.
  1221. */
  1222. void /* PRIVATE */
  1223. png_do_read_transformations(png_structp png_ptr)
  1224. {
  1225. png_debug(1, "in png_do_read_transformations");
  1226. if (png_ptr->row_buf == NULL)
  1227. {
  1228. #ifdef PNG_CONSOLE_IO_SUPPORTED
  1229. char msg[50];
  1230. png_snprintf2(msg, 50,
  1231. "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
  1232. png_ptr->pass);
  1233. png_error(png_ptr, msg);
  1234. #else
  1235. png_error(png_ptr, "NULL row buffer");
  1236. #endif
  1237. }
  1238. #ifdef PNG_WARN_UNINITIALIZED_ROW
  1239. if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
  1240. /* Application has failed to call either png_read_start_image()
  1241. * or png_read_update_info() after setting transforms that expand
  1242. * pixels. This check added to libpng-1.2.19
  1243. */
  1244. #if (PNG_WARN_UNINITIALIZED_ROW==1)
  1245. png_error(png_ptr, "Uninitialized row");
  1246. #else
  1247. png_warning(png_ptr, "Uninitialized row");
  1248. #endif
  1249. #endif
  1250. #ifdef PNG_READ_EXPAND_SUPPORTED
  1251. if (png_ptr->transformations & PNG_EXPAND)
  1252. {
  1253. if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
  1254. {
  1255. png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
  1256. png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
  1257. }
  1258. else
  1259. {
  1260. if (png_ptr->num_trans &&
  1261. (png_ptr->transformations & PNG_EXPAND_tRNS))
  1262. png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
  1263. &(png_ptr->trans_color));
  1264. else
  1265. png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
  1266. NULL);
  1267. }
  1268. }
  1269. #endif
  1270. /* Delay the 'expand 16' step until later for efficiency, so that the
  1271. * intermediate steps work with 8 bit data.
  1272. */
  1273. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  1274. if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
  1275. (png_ptr->row_info.color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  1276. png_ptr->row_info.color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
  1277. png_do_strip_channel(&(png_ptr->row_info), png_ptr->row_buf + 1,
  1278. 0/*!at_start, because SWAP_ALPHA happens later*/);
  1279. #endif
  1280. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  1281. if (png_ptr->transformations & PNG_RGB_TO_GRAY)
  1282. {
  1283. int rgb_error =
  1284. png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info),
  1285. png_ptr->row_buf + 1);
  1286. if (rgb_error)
  1287. {
  1288. png_ptr->rgb_to_gray_status=1;
  1289. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
  1290. PNG_RGB_TO_GRAY_WARN)
  1291. png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
  1292. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
  1293. PNG_RGB_TO_GRAY_ERR)
  1294. png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
  1295. }
  1296. }
  1297. #endif
  1298. /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
  1299. *
  1300. * In most cases, the "simple transparency" should be done prior to doing
  1301. * gray-to-RGB, or you will have to test 3x as many bytes to check if a
  1302. * pixel is transparent. You would also need to make sure that the
  1303. * transparency information is upgraded to RGB.
  1304. *
  1305. * To summarize, the current flow is:
  1306. * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
  1307. * with background "in place" if transparent,
  1308. * convert to RGB if necessary
  1309. * - Gray + alpha -> composite with gray background and remove alpha bytes,
  1310. * convert to RGB if necessary
  1311. *
  1312. * To support RGB backgrounds for gray images we need:
  1313. * - Gray + simple transparency -> convert to RGB + simple transparency,
  1314. * compare 3 or 6 bytes and composite with
  1315. * background "in place" if transparent
  1316. * (3x compare/pixel compared to doing
  1317. * composite with gray bkgrnd)
  1318. * - Gray + alpha -> convert to RGB + alpha, composite with background and
  1319. * remove alpha bytes (3x float
  1320. * operations/pixel compared with composite
  1321. * on gray background)
  1322. *
  1323. * Greg's change will do this. The reason it wasn't done before is for
  1324. * performance, as this increases the per-pixel operations. If we would check
  1325. * in advance if the background was gray or RGB, and position the gray-to-RGB
  1326. * transform appropriately, then it would save a lot of work/time.
  1327. */
  1328. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1329. /* If gray -> RGB, do so now only if background is non-gray; else do later
  1330. * for performance reasons
  1331. */
  1332. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
  1333. !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
  1334. png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1335. #endif
  1336. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1337. if ((png_ptr->transformations & PNG_BACKGROUND) &&
  1338. ((png_ptr->num_trans != 0) ||
  1339. (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
  1340. png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
  1341. &(png_ptr->trans_color), &(png_ptr->background)
  1342. #ifdef PNG_READ_GAMMA_SUPPORTED
  1343. , &(png_ptr->background_1),
  1344. png_ptr->gamma_table, png_ptr->gamma_from_1,
  1345. png_ptr->gamma_to_1, png_ptr->gamma_16_table,
  1346. png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
  1347. png_ptr->gamma_shift
  1348. #endif
  1349. );
  1350. #endif
  1351. #ifdef PNG_READ_GAMMA_SUPPORTED
  1352. if ((png_ptr->transformations & PNG_GAMMA) &&
  1353. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1354. !((png_ptr->transformations & PNG_BACKGROUND) &&
  1355. ((png_ptr->num_trans != 0) ||
  1356. (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
  1357. #endif
  1358. (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
  1359. png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
  1360. png_ptr->gamma_table, png_ptr->gamma_16_table,
  1361. png_ptr->gamma_shift);
  1362. #endif
  1363. #ifdef PNG_READ_16_TO_8_SUPPORTED
  1364. if (png_ptr->transformations & PNG_16_TO_8)
  1365. png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1366. #endif
  1367. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  1368. if (png_ptr->transformations & PNG_QUANTIZE)
  1369. {
  1370. png_do_quantize(&(png_ptr->row_info), png_ptr->row_buf + 1,
  1371. png_ptr->palette_lookup, png_ptr->quantize_index);
  1372. if (png_ptr->row_info.rowbytes == 0)
  1373. png_error(png_ptr, "png_do_quantize returned rowbytes=0");
  1374. }
  1375. #endif /* PNG_READ_QUANTIZE_SUPPORTED */
  1376. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  1377. /* Do the expansion now, after all the arithmetic has been done. Notice
  1378. * that previous transformations can handle the PNG_EXPAND_16 flag if this
  1379. * is efficient (particularly true in the case of gamma correction, where
  1380. * better accuracy results faster!)
  1381. */
  1382. if (png_ptr->transformations & PNG_EXPAND_16)
  1383. png_do_expand_16(&png_ptr->row_info, png_ptr->row_buf + 1);
  1384. #endif
  1385. #ifdef PNG_READ_INVERT_SUPPORTED
  1386. if (png_ptr->transformations & PNG_INVERT_MONO)
  1387. png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1388. #endif
  1389. #ifdef PNG_READ_SHIFT_SUPPORTED
  1390. if (png_ptr->transformations & PNG_SHIFT)
  1391. png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
  1392. &(png_ptr->shift));
  1393. #endif
  1394. #ifdef PNG_READ_PACK_SUPPORTED
  1395. if (png_ptr->transformations & PNG_PACK)
  1396. png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1397. #endif
  1398. #ifdef PNG_READ_BGR_SUPPORTED
  1399. if (png_ptr->transformations & PNG_BGR)
  1400. png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1401. #endif
  1402. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  1403. if (png_ptr->transformations & PNG_PACKSWAP)
  1404. png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1405. #endif
  1406. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1407. /*NOTE: this must be in the wrong place - what happens if BGR is set too?
  1408. * Need pngvalid to test this combo.
  1409. */
  1410. /* If gray -> RGB, do so now only if we did not do so above */
  1411. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
  1412. (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
  1413. png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1414. #endif
  1415. #ifdef PNG_READ_FILLER_SUPPORTED
  1416. if (png_ptr->transformations & PNG_FILLER)
  1417. png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
  1418. (png_uint_32)png_ptr->filler, png_ptr->flags);
  1419. #endif
  1420. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  1421. if (png_ptr->transformations & PNG_INVERT_ALPHA)
  1422. png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1423. #endif
  1424. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  1425. if (png_ptr->transformations & PNG_SWAP_ALPHA)
  1426. png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1427. #endif
  1428. #ifdef PNG_READ_16BIT_SUPPORTED
  1429. #ifdef PNG_READ_SWAP_SUPPORTED
  1430. if (png_ptr->transformations & PNG_SWAP_BYTES)
  1431. png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1432. #endif
  1433. #endif
  1434. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  1435. if (png_ptr->transformations & PNG_USER_TRANSFORM)
  1436. {
  1437. if (png_ptr->read_user_transform_fn != NULL)
  1438. (*(png_ptr->read_user_transform_fn)) /* User read transform function */
  1439. (png_ptr, /* png_ptr */
  1440. &(png_ptr->row_info), /* row_info: */
  1441. /* png_uint_32 width; width of row */
  1442. /* png_size_t rowbytes; number of bytes in row */
  1443. /* png_byte color_type; color type of pixels */
  1444. /* png_byte bit_depth; bit depth of samples */
  1445. /* png_byte channels; number of channels (1-4) */
  1446. /* png_byte pixel_depth; bits per pixel (depth*channels) */
  1447. png_ptr->row_buf + 1); /* start of pixel data for row */
  1448. #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
  1449. if (png_ptr->user_transform_depth)
  1450. png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
  1451. if (png_ptr->user_transform_channels)
  1452. png_ptr->row_info.channels = png_ptr->user_transform_channels;
  1453. #endif
  1454. png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
  1455. png_ptr->row_info.channels);
  1456. png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  1457. png_ptr->row_info.width);
  1458. }
  1459. #endif
  1460. }
  1461. #ifdef PNG_READ_PACK_SUPPORTED
  1462. /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
  1463. * without changing the actual values. Thus, if you had a row with
  1464. * a bit depth of 1, you would end up with bytes that only contained
  1465. * the numbers 0 or 1. If you would rather they contain 0 and 255, use
  1466. * png_do_shift() after this.
  1467. */
  1468. void /* PRIVATE */
  1469. png_do_unpack(png_row_infop row_info, png_bytep row)
  1470. {
  1471. png_debug(1, "in png_do_unpack");
  1472. if (row_info->bit_depth < 8)
  1473. {
  1474. png_uint_32 i;
  1475. png_uint_32 row_width=row_info->width;
  1476. switch (row_info->bit_depth)
  1477. {
  1478. case 1:
  1479. {
  1480. png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
  1481. png_bytep dp = row + (png_size_t)row_width - 1;
  1482. png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
  1483. for (i = 0; i < row_width; i++)
  1484. {
  1485. *dp = (png_byte)((*sp >> shift) & 0x01);
  1486. if (shift == 7)
  1487. {
  1488. shift = 0;
  1489. sp--;
  1490. }
  1491. else
  1492. shift++;
  1493. dp--;
  1494. }
  1495. break;
  1496. }
  1497. case 2:
  1498. {
  1499. png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
  1500. png_bytep dp = row + (png_size_t)row_width - 1;
  1501. png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  1502. for (i = 0; i < row_width; i++)
  1503. {
  1504. *dp = (png_byte)((*sp >> shift) & 0x03);
  1505. if (shift == 6)
  1506. {
  1507. shift = 0;
  1508. sp--;
  1509. }
  1510. else
  1511. shift += 2;
  1512. dp--;
  1513. }
  1514. break;
  1515. }
  1516. case 4:
  1517. {
  1518. png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
  1519. png_bytep dp = row + (png_size_t)row_width - 1;
  1520. png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
  1521. for (i = 0; i < row_width; i++)
  1522. {
  1523. *dp = (png_byte)((*sp >> shift) & 0x0f);
  1524. if (shift == 4)
  1525. {
  1526. shift = 0;
  1527. sp--;
  1528. }
  1529. else
  1530. shift = 4;
  1531. dp--;
  1532. }
  1533. break;
  1534. }
  1535. default:
  1536. break;
  1537. }
  1538. row_info->bit_depth = 8;
  1539. row_info->pixel_depth = (png_byte)(8 * row_info->channels);
  1540. row_info->rowbytes = row_width * row_info->channels;
  1541. }
  1542. }
  1543. #endif
  1544. #ifdef PNG_READ_SHIFT_SUPPORTED
  1545. /* Reverse the effects of png_do_shift. This routine merely shifts the
  1546. * pixels back to their significant bits values. Thus, if you have
  1547. * a row of bit depth 8, but only 5 are significant, this will shift
  1548. * the values back to 0 through 31.
  1549. */
  1550. void /* PRIVATE */
  1551. png_do_unshift(png_row_infop row_info, png_bytep row,
  1552. png_const_color_8p sig_bits)
  1553. {
  1554. png_debug(1, "in png_do_unshift");
  1555. if (
  1556. row_info->color_type != PNG_COLOR_TYPE_PALETTE)
  1557. {
  1558. int shift[4];
  1559. int channels = 0;
  1560. int c;
  1561. png_uint_16 value = 0;
  1562. png_uint_32 row_width = row_info->width;
  1563. if (row_info->color_type & PNG_COLOR_MASK_COLOR)
  1564. {
  1565. shift[channels++] = row_info->bit_depth - sig_bits->red;
  1566. shift[channels++] = row_info->bit_depth - sig_bits->green;
  1567. shift[channels++] = row_info->bit_depth - sig_bits->blue;
  1568. }
  1569. else
  1570. {
  1571. shift[channels++] = row_info->bit_depth - sig_bits->gray;
  1572. }
  1573. if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
  1574. {
  1575. shift[channels++] = row_info->bit_depth - sig_bits->alpha;
  1576. }
  1577. for (c = 0; c < channels; c++)
  1578. {
  1579. if (shift[c] <= 0)
  1580. shift[c] = 0;
  1581. else
  1582. value = 1;
  1583. }
  1584. if (!value)
  1585. return;
  1586. switch (row_info->bit_depth)
  1587. {
  1588. default:
  1589. break;
  1590. case 2:
  1591. {
  1592. png_bytep bp;
  1593. png_size_t i;
  1594. png_size_t istop = row_info->rowbytes;
  1595. for (bp = row, i = 0; i < istop; i++)
  1596. {
  1597. *bp >>= 1;
  1598. *bp++ &= 0x55;
  1599. }
  1600. break;
  1601. }
  1602. case 4:
  1603. {
  1604. png_bytep bp = row;
  1605. png_size_t i;
  1606. png_size_t istop = row_info->rowbytes;
  1607. png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
  1608. (png_byte)((int)0xf >> shift[0]));
  1609. for (i = 0; i < istop; i++)
  1610. {
  1611. *bp >>= shift[0];
  1612. *bp++ &= mask;
  1613. }
  1614. break;
  1615. }
  1616. case 8:
  1617. {
  1618. png_bytep bp = row;
  1619. png_uint_32 i;
  1620. png_uint_32 istop = row_width * channels;
  1621. for (i = 0; i < istop; i++)
  1622. {
  1623. *bp++ >>= shift[i%channels];
  1624. }
  1625. break;
  1626. }
  1627. #ifdef PNG_READ_16BIT_SUPPORTED
  1628. case 16:
  1629. {
  1630. png_bytep bp = row;
  1631. png_uint_32 i;
  1632. png_uint_32 istop = channels * row_width;
  1633. for (i = 0; i < istop; i++)
  1634. {
  1635. value = (png_uint_16)((*bp << 8) + *(bp + 1));
  1636. value >>= shift[i%channels];
  1637. *bp++ = (png_byte)(value >> 8);
  1638. *bp++ = (png_byte)(value & 0xff);
  1639. }
  1640. break;
  1641. }
  1642. #endif
  1643. }
  1644. }
  1645. }
  1646. #endif
  1647. #ifdef PNG_READ_16_TO_8_SUPPORTED
  1648. /* Chop rows of bit depth 16 down to 8 */
  1649. void /* PRIVATE */
  1650. png_do_chop(png_row_infop row_info, png_bytep row)
  1651. {
  1652. png_debug(1, "in png_do_chop");
  1653. if (row_info->bit_depth == 16)
  1654. {
  1655. png_bytep sp = row;
  1656. png_bytep dp = row;
  1657. png_uint_32 i;
  1658. png_uint_32 istop = row_info->width * row_info->channels;
  1659. for (i = 0; i<istop; i++, sp += 2, dp++)
  1660. {
  1661. #ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
  1662. /* This does a more accurate scaling of the 16-bit color
  1663. * value, rather than a simple low-byte truncation.
  1664. *
  1665. * What the ideal calculation should be:
  1666. * *dp = (((((png_uint_32)(*sp) << 8) |
  1667. * (png_uint_32)(*(sp + 1))) * 255 + 127)
  1668. * / (png_uint_32)65535L;
  1669. *
  1670. * GRR: no, I think this is what it really should be:
  1671. * *dp = (((((png_uint_32)(*sp) << 8) |
  1672. * (png_uint_32)(*(sp + 1))) + 128L)
  1673. * / (png_uint_32)257L;
  1674. *
  1675. * GRR: here's the exact calculation with shifts:
  1676. * temp = (((png_uint_32)(*sp) << 8) |
  1677. * (png_uint_32)(*(sp + 1))) + 128L;
  1678. * *dp = (temp - (temp >> 8)) >> 8;
  1679. *
  1680. * Approximate calculation with shift/add instead of multiply/divide:
  1681. * *dp = ((((png_uint_32)(*sp) << 8) |
  1682. * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
  1683. *
  1684. * What we actually do to avoid extra shifting and conversion:
  1685. */
  1686. *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
  1687. #else
  1688. /* Simply discard the low order byte */
  1689. *dp = *sp;
  1690. #endif
  1691. }
  1692. row_info->bit_depth = 8;
  1693. row_info->pixel_depth = (png_byte)(8 * row_info->channels);
  1694. row_info->rowbytes = row_info->width * row_info->channels;
  1695. }
  1696. }
  1697. #endif
  1698. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  1699. void /* PRIVATE */
  1700. png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
  1701. {
  1702. png_debug(1, "in png_do_read_swap_alpha");
  1703. {
  1704. png_uint_32 row_width = row_info->width;
  1705. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  1706. {
  1707. /* This converts from RGBA to ARGB */
  1708. if (row_info->bit_depth == 8)
  1709. {
  1710. png_bytep sp = row + row_info->rowbytes;
  1711. png_bytep dp = sp;
  1712. png_byte save;
  1713. png_uint_32 i;
  1714. for (i = 0; i < row_width; i++)
  1715. {
  1716. save = *(--sp);
  1717. *(--dp) = *(--sp);
  1718. *(--dp) = *(--sp);
  1719. *(--dp) = *(--sp);
  1720. *(--dp) = save;
  1721. }
  1722. }
  1723. #ifdef PNG_READ_16BIT_SUPPORTED
  1724. /* This converts from RRGGBBAA to AARRGGBB */
  1725. else
  1726. {
  1727. png_bytep sp = row + row_info->rowbytes;
  1728. png_bytep dp = sp;
  1729. png_byte save[2];
  1730. png_uint_32 i;
  1731. for (i = 0; i < row_width; i++)
  1732. {
  1733. save[0] = *(--sp);
  1734. save[1] = *(--sp);
  1735. *(--dp) = *(--sp);
  1736. *(--dp) = *(--sp);
  1737. *(--dp) = *(--sp);
  1738. *(--dp) = *(--sp);
  1739. *(--dp) = *(--sp);
  1740. *(--dp) = *(--sp);
  1741. *(--dp) = save[0];
  1742. *(--dp) = save[1];
  1743. }
  1744. }
  1745. #endif
  1746. }
  1747. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  1748. {
  1749. /* This converts from GA to AG */
  1750. if (row_info->bit_depth == 8)
  1751. {
  1752. png_bytep sp = row + row_info->rowbytes;
  1753. png_bytep dp = sp;
  1754. png_byte save;
  1755. png_uint_32 i;
  1756. for (i = 0; i < row_width; i++)
  1757. {
  1758. save = *(--sp);
  1759. *(--dp) = *(--sp);
  1760. *(--dp) = save;
  1761. }
  1762. }
  1763. #ifdef PNG_READ_16BIT_SUPPORTED
  1764. /* This converts from GGAA to AAGG */
  1765. else
  1766. {
  1767. png_bytep sp = row + row_info->rowbytes;
  1768. png_bytep dp = sp;
  1769. png_byte save[2];
  1770. png_uint_32 i;
  1771. for (i = 0; i < row_width; i++)
  1772. {
  1773. save[0] = *(--sp);
  1774. save[1] = *(--sp);
  1775. *(--dp) = *(--sp);
  1776. *(--dp) = *(--sp);
  1777. *(--dp) = save[0];
  1778. *(--dp) = save[1];
  1779. }
  1780. }
  1781. #endif
  1782. }
  1783. }
  1784. }
  1785. #endif
  1786. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  1787. void /* PRIVATE */
  1788. png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
  1789. {
  1790. png_uint_32 row_width;
  1791. png_debug(1, "in png_do_read_invert_alpha");
  1792. row_width = row_info->width;
  1793. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  1794. {
  1795. if (row_info->bit_depth == 8)
  1796. {
  1797. /* This inverts the alpha channel in RGBA */
  1798. png_bytep sp = row + row_info->rowbytes;
  1799. png_bytep dp = sp;
  1800. png_uint_32 i;
  1801. for (i = 0; i < row_width; i++)
  1802. {
  1803. *(--dp) = (png_byte)(255 - *(--sp));
  1804. /* This does nothing:
  1805. *(--dp) = *(--sp);
  1806. *(--dp) = *(--sp);
  1807. *(--dp) = *(--sp);
  1808. We can replace it with:
  1809. */
  1810. sp-=3;
  1811. dp=sp;
  1812. }
  1813. }
  1814. #ifdef PNG_READ_16BIT_SUPPORTED
  1815. /* This inverts the alpha channel in RRGGBBAA */
  1816. else
  1817. {
  1818. png_bytep sp = row + row_info->rowbytes;
  1819. png_bytep dp = sp;
  1820. png_uint_32 i;
  1821. for (i = 0; i < row_width; i++)
  1822. {
  1823. *(--dp) = (png_byte)(255 - *(--sp));
  1824. *(--dp) = (png_byte)(255 - *(--sp));
  1825. /* This does nothing:
  1826. *(--dp) = *(--sp);
  1827. *(--dp) = *(--sp);
  1828. *(--dp) = *(--sp);
  1829. *(--dp) = *(--sp);
  1830. *(--dp) = *(--sp);
  1831. *(--dp) = *(--sp);
  1832. We can replace it with:
  1833. */
  1834. sp-=6;
  1835. dp=sp;
  1836. }
  1837. }
  1838. #endif
  1839. }
  1840. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  1841. {
  1842. if (row_info->bit_depth == 8)
  1843. {
  1844. /* This inverts the alpha channel in GA */
  1845. png_bytep sp = row + row_info->rowbytes;
  1846. png_bytep dp = sp;
  1847. png_uint_32 i;
  1848. for (i = 0; i < row_width; i++)
  1849. {
  1850. *(--dp) = (png_byte)(255 - *(--sp));
  1851. *(--dp) = *(--sp);
  1852. }
  1853. }
  1854. #ifdef PNG_READ_16BIT_SUPPORTED
  1855. else
  1856. {
  1857. /* This inverts the alpha channel in GGAA */
  1858. png_bytep sp = row + row_info->rowbytes;
  1859. png_bytep dp = sp;
  1860. png_uint_32 i;
  1861. for (i = 0; i < row_width; i++)
  1862. {
  1863. *(--dp) = (png_byte)(255 - *(--sp));
  1864. *(--dp) = (png_byte)(255 - *(--sp));
  1865. /*
  1866. *(--dp) = *(--sp);
  1867. *(--dp) = *(--sp);
  1868. */
  1869. sp-=2;
  1870. dp=sp;
  1871. }
  1872. }
  1873. #endif
  1874. }
  1875. }
  1876. #endif
  1877. #ifdef PNG_READ_FILLER_SUPPORTED
  1878. /* Add filler channel if we have RGB color */
  1879. void /* PRIVATE */
  1880. png_do_read_filler(png_row_infop row_info, png_bytep row,
  1881. png_uint_32 filler, png_uint_32 flags)
  1882. {
  1883. png_uint_32 i;
  1884. png_uint_32 row_width = row_info->width;
  1885. #ifdef PNG_READ_16BIT_SUPPORTED
  1886. png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
  1887. #endif
  1888. png_byte lo_filler = (png_byte)(filler & 0xff);
  1889. png_debug(1, "in png_do_read_filler");
  1890. if (
  1891. row_info->color_type == PNG_COLOR_TYPE_GRAY)
  1892. {
  1893. if (row_info->bit_depth == 8)
  1894. {
  1895. if (flags & PNG_FLAG_FILLER_AFTER)
  1896. {
  1897. /* This changes the data from G to GX */
  1898. png_bytep sp = row + (png_size_t)row_width;
  1899. png_bytep dp = sp + (png_size_t)row_width;
  1900. for (i = 1; i < row_width; i++)
  1901. {
  1902. *(--dp) = lo_filler;
  1903. *(--dp) = *(--sp);
  1904. }
  1905. *(--dp) = lo_filler;
  1906. row_info->channels = 2;
  1907. row_info->pixel_depth = 16;
  1908. row_info->rowbytes = row_width * 2;
  1909. }
  1910. else
  1911. {
  1912. /* This changes the data from G to XG */
  1913. png_bytep sp = row + (png_size_t)row_width;
  1914. png_bytep dp = sp + (png_size_t)row_width;
  1915. for (i = 0; i < row_width; i++)
  1916. {
  1917. *(--dp) = *(--sp);
  1918. *(--dp) = lo_filler;
  1919. }
  1920. row_info->channels = 2;
  1921. row_info->pixel_depth = 16;
  1922. row_info->rowbytes = row_width * 2;
  1923. }
  1924. }
  1925. #ifdef PNG_READ_16BIT_SUPPORTED
  1926. else if (row_info->bit_depth == 16)
  1927. {
  1928. if (flags & PNG_FLAG_FILLER_AFTER)
  1929. {
  1930. /* This changes the data from GG to GGXX */
  1931. png_bytep sp = row + (png_size_t)row_width * 2;
  1932. png_bytep dp = sp + (png_size_t)row_width * 2;
  1933. for (i = 1; i < row_width; i++)
  1934. {
  1935. *(--dp) = hi_filler;
  1936. *(--dp) = lo_filler;
  1937. *(--dp) = *(--sp);
  1938. *(--dp) = *(--sp);
  1939. }
  1940. *(--dp) = hi_filler;
  1941. *(--dp) = lo_filler;
  1942. row_info->channels = 2;
  1943. row_info->pixel_depth = 32;
  1944. row_info->rowbytes = row_width * 4;
  1945. }
  1946. else
  1947. {
  1948. /* This changes the data from GG to XXGG */
  1949. png_bytep sp = row + (png_size_t)row_width * 2;
  1950. png_bytep dp = sp + (png_size_t)row_width * 2;
  1951. for (i = 0; i < row_width; i++)
  1952. {
  1953. *(--dp) = *(--sp);
  1954. *(--dp) = *(--sp);
  1955. *(--dp) = hi_filler;
  1956. *(--dp) = lo_filler;
  1957. }
  1958. row_info->channels = 2;
  1959. row_info->pixel_depth = 32;
  1960. row_info->rowbytes = row_width * 4;
  1961. }
  1962. }
  1963. #endif
  1964. } /* COLOR_TYPE == GRAY */
  1965. else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  1966. {
  1967. if (row_info->bit_depth == 8)
  1968. {
  1969. if (flags & PNG_FLAG_FILLER_AFTER)
  1970. {
  1971. /* This changes the data from RGB to RGBX */
  1972. png_bytep sp = row + (png_size_t)row_width * 3;
  1973. png_bytep dp = sp + (png_size_t)row_width;
  1974. for (i = 1; i < row_width; i++)
  1975. {
  1976. *(--dp) = lo_filler;
  1977. *(--dp) = *(--sp);
  1978. *(--dp) = *(--sp);
  1979. *(--dp) = *(--sp);
  1980. }
  1981. *(--dp) = lo_filler;
  1982. row_info->channels = 4;
  1983. row_info->pixel_depth = 32;
  1984. row_info->rowbytes = row_width * 4;
  1985. }
  1986. else
  1987. {
  1988. /* This changes the data from RGB to XRGB */
  1989. png_bytep sp = row + (png_size_t)row_width * 3;
  1990. png_bytep dp = sp + (png_size_t)row_width;
  1991. for (i = 0; i < row_width; i++)
  1992. {
  1993. *(--dp) = *(--sp);
  1994. *(--dp) = *(--sp);
  1995. *(--dp) = *(--sp);
  1996. *(--dp) = lo_filler;
  1997. }
  1998. row_info->channels = 4;
  1999. row_info->pixel_depth = 32;
  2000. row_info->rowbytes = row_width * 4;
  2001. }
  2002. }
  2003. #ifdef PNG_READ_16BIT_SUPPORTED
  2004. else if (row_info->bit_depth == 16)
  2005. {
  2006. if (flags & PNG_FLAG_FILLER_AFTER)
  2007. {
  2008. /* This changes the data from RRGGBB to RRGGBBXX */
  2009. png_bytep sp = row + (png_size_t)row_width * 6;
  2010. png_bytep dp = sp + (png_size_t)row_width * 2;
  2011. for (i = 1; i < row_width; i++)
  2012. {
  2013. *(--dp) = hi_filler;
  2014. *(--dp) = lo_filler;
  2015. *(--dp) = *(--sp);
  2016. *(--dp) = *(--sp);
  2017. *(--dp) = *(--sp);
  2018. *(--dp) = *(--sp);
  2019. *(--dp) = *(--sp);
  2020. *(--dp) = *(--sp);
  2021. }
  2022. *(--dp) = hi_filler;
  2023. *(--dp) = lo_filler;
  2024. row_info->channels = 4;
  2025. row_info->pixel_depth = 64;
  2026. row_info->rowbytes = row_width * 8;
  2027. }
  2028. else
  2029. {
  2030. /* This changes the data from RRGGBB to XXRRGGBB */
  2031. png_bytep sp = row + (png_size_t)row_width * 6;
  2032. png_bytep dp = sp + (png_size_t)row_width * 2;
  2033. for (i = 0; i < row_width; i++)
  2034. {
  2035. *(--dp) = *(--sp);
  2036. *(--dp) = *(--sp);
  2037. *(--dp) = *(--sp);
  2038. *(--dp) = *(--sp);
  2039. *(--dp) = *(--sp);
  2040. *(--dp) = *(--sp);
  2041. *(--dp) = hi_filler;
  2042. *(--dp) = lo_filler;
  2043. }
  2044. row_info->channels = 4;
  2045. row_info->pixel_depth = 64;
  2046. row_info->rowbytes = row_width * 8;
  2047. }
  2048. }
  2049. #endif
  2050. } /* COLOR_TYPE == RGB */
  2051. }
  2052. #endif
  2053. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  2054. /* Expand grayscale files to RGB, with or without alpha */
  2055. void /* PRIVATE */
  2056. png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
  2057. {
  2058. png_uint_32 i;
  2059. png_uint_32 row_width = row_info->width;
  2060. png_debug(1, "in png_do_gray_to_rgb");
  2061. if (row_info->bit_depth >= 8 &&
  2062. !(row_info->color_type & PNG_COLOR_MASK_COLOR))
  2063. {
  2064. if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  2065. {
  2066. if (row_info->bit_depth == 8)
  2067. {
  2068. /* This changes G to RGB */
  2069. png_bytep sp = row + (png_size_t)row_width - 1;
  2070. png_bytep dp = sp + (png_size_t)row_width * 2;
  2071. for (i = 0; i < row_width; i++)
  2072. {
  2073. *(dp--) = *sp;
  2074. *(dp--) = *sp;
  2075. *(dp--) = *(sp--);
  2076. }
  2077. }
  2078. else
  2079. {
  2080. /* This changes GG to RRGGBB */
  2081. png_bytep sp = row + (png_size_t)row_width * 2 - 1;
  2082. png_bytep dp = sp + (png_size_t)row_width * 4;
  2083. for (i = 0; i < row_width; i++)
  2084. {
  2085. *(dp--) = *sp;
  2086. *(dp--) = *(sp - 1);
  2087. *(dp--) = *sp;
  2088. *(dp--) = *(sp - 1);
  2089. *(dp--) = *(sp--);
  2090. *(dp--) = *(sp--);
  2091. }
  2092. }
  2093. }
  2094. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2095. {
  2096. if (row_info->bit_depth == 8)
  2097. {
  2098. /* This changes GA to RGBA */
  2099. png_bytep sp = row + (png_size_t)row_width * 2 - 1;
  2100. png_bytep dp = sp + (png_size_t)row_width * 2;
  2101. for (i = 0; i < row_width; i++)
  2102. {
  2103. *(dp--) = *(sp--);
  2104. *(dp--) = *sp;
  2105. *(dp--) = *sp;
  2106. *(dp--) = *(sp--);
  2107. }
  2108. }
  2109. else
  2110. {
  2111. /* This changes GGAA to RRGGBBAA */
  2112. png_bytep sp = row + (png_size_t)row_width * 4 - 1;
  2113. png_bytep dp = sp + (png_size_t)row_width * 4;
  2114. for (i = 0; i < row_width; i++)
  2115. {
  2116. *(dp--) = *(sp--);
  2117. *(dp--) = *(sp--);
  2118. *(dp--) = *sp;
  2119. *(dp--) = *(sp - 1);
  2120. *(dp--) = *sp;
  2121. *(dp--) = *(sp - 1);
  2122. *(dp--) = *(sp--);
  2123. *(dp--) = *(sp--);
  2124. }
  2125. }
  2126. }
  2127. row_info->channels += (png_byte)2;
  2128. row_info->color_type |= PNG_COLOR_MASK_COLOR;
  2129. row_info->pixel_depth = (png_byte)(row_info->channels *
  2130. row_info->bit_depth);
  2131. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  2132. }
  2133. }
  2134. #endif
  2135. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  2136. /* Reduce RGB files to grayscale, with or without alpha
  2137. * using the equation given in Poynton's ColorFAQ at
  2138. * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008)
  2139. * New link:
  2140. * <http://www.poynton.com/notes/colour_and_gamma/>
  2141. * Charles Poynton poynton at poynton.com
  2142. *
  2143. * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
  2144. *
  2145. * We approximate this with
  2146. *
  2147. * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B
  2148. *
  2149. * which can be expressed with integers as
  2150. *
  2151. * Y = (6969 * R + 23434 * G + 2365 * B)/32768
  2152. *
  2153. * The calculation is to be done in a linear colorspace.
  2154. *
  2155. * Other integer coefficents can be used via png_set_rgb_to_gray().
  2156. */
  2157. int /* PRIVATE */
  2158. png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
  2159. {
  2160. png_uint_32 i;
  2161. png_uint_32 row_width = row_info->width;
  2162. int rgb_error = 0;
  2163. png_debug(1, "in png_do_rgb_to_gray");
  2164. if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) &&
  2165. (row_info->color_type & PNG_COLOR_MASK_COLOR))
  2166. {
  2167. png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
  2168. png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
  2169. png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
  2170. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  2171. {
  2172. if (row_info->bit_depth == 8)
  2173. {
  2174. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  2175. if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
  2176. {
  2177. png_bytep sp = row;
  2178. png_bytep dp = row;
  2179. for (i = 0; i < row_width; i++)
  2180. {
  2181. png_byte red = png_ptr->gamma_to_1[*(sp++)];
  2182. png_byte green = png_ptr->gamma_to_1[*(sp++)];
  2183. png_byte blue = png_ptr->gamma_to_1[*(sp++)];
  2184. if (red != green || red != blue)
  2185. {
  2186. rgb_error |= 1;
  2187. *(dp++) = png_ptr->gamma_from_1[
  2188. (rc*red + gc*green + bc*blue)>>15];
  2189. }
  2190. else
  2191. *(dp++) = *(sp - 1);
  2192. }
  2193. }
  2194. else
  2195. #endif
  2196. {
  2197. png_bytep sp = row;
  2198. png_bytep dp = row;
  2199. for (i = 0; i < row_width; i++)
  2200. {
  2201. png_byte red = *(sp++);
  2202. png_byte green = *(sp++);
  2203. png_byte blue = *(sp++);
  2204. if (red != green || red != blue)
  2205. {
  2206. rgb_error |= 1;
  2207. *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
  2208. }
  2209. else
  2210. *(dp++) = *(sp - 1);
  2211. }
  2212. }
  2213. }
  2214. else /* RGB bit_depth == 16 */
  2215. {
  2216. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  2217. if (png_ptr->gamma_16_to_1 != NULL &&
  2218. png_ptr->gamma_16_from_1 != NULL)
  2219. {
  2220. png_bytep sp = row;
  2221. png_bytep dp = row;
  2222. for (i = 0; i < row_width; i++)
  2223. {
  2224. png_uint_16 red, green, blue, w;
  2225. red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2226. green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2227. blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2228. if (red == green && red == blue)
  2229. w = red;
  2230. else
  2231. {
  2232. png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff)
  2233. >> png_ptr->gamma_shift][red>>8];
  2234. png_uint_16 green_1 =
  2235. png_ptr->gamma_16_to_1[(green&0xff) >>
  2236. png_ptr->gamma_shift][green>>8];
  2237. png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff)
  2238. >> png_ptr->gamma_shift][blue>>8];
  2239. png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
  2240. + bc*blue_1)>>15);
  2241. w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
  2242. png_ptr->gamma_shift][gray16 >> 8];
  2243. rgb_error |= 1;
  2244. }
  2245. *(dp++) = (png_byte)((w>>8) & 0xff);
  2246. *(dp++) = (png_byte)(w & 0xff);
  2247. }
  2248. }
  2249. else
  2250. #endif
  2251. {
  2252. png_bytep sp = row;
  2253. png_bytep dp = row;
  2254. for (i = 0; i < row_width; i++)
  2255. {
  2256. png_uint_16 red, green, blue, gray16;
  2257. red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2258. green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2259. blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2260. if (red != green || red != blue)
  2261. rgb_error |= 1;
  2262. gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
  2263. *(dp++) = (png_byte)((gray16>>8) & 0xff);
  2264. *(dp++) = (png_byte)(gray16 & 0xff);
  2265. }
  2266. }
  2267. }
  2268. }
  2269. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2270. {
  2271. if (row_info->bit_depth == 8)
  2272. {
  2273. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  2274. if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
  2275. {
  2276. png_bytep sp = row;
  2277. png_bytep dp = row;
  2278. for (i = 0; i < row_width; i++)
  2279. {
  2280. png_byte red = png_ptr->gamma_to_1[*(sp++)];
  2281. png_byte green = png_ptr->gamma_to_1[*(sp++)];
  2282. png_byte blue = png_ptr->gamma_to_1[*(sp++)];
  2283. if (red != green || red != blue)
  2284. rgb_error |= 1;
  2285. *(dp++) = png_ptr->gamma_from_1
  2286. [(rc*red + gc*green + bc*blue)>>15];
  2287. *(dp++) = *(sp++); /* alpha */
  2288. }
  2289. }
  2290. else
  2291. #endif
  2292. {
  2293. png_bytep sp = row;
  2294. png_bytep dp = row;
  2295. for (i = 0; i < row_width; i++)
  2296. {
  2297. png_byte red = *(sp++);
  2298. png_byte green = *(sp++);
  2299. png_byte blue = *(sp++);
  2300. if (red != green || red != blue)
  2301. rgb_error |= 1;
  2302. *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
  2303. *(dp++) = *(sp++); /* alpha */
  2304. }
  2305. }
  2306. }
  2307. else /* RGBA bit_depth == 16 */
  2308. {
  2309. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  2310. if (png_ptr->gamma_16_to_1 != NULL &&
  2311. png_ptr->gamma_16_from_1 != NULL)
  2312. {
  2313. png_bytep sp = row;
  2314. png_bytep dp = row;
  2315. for (i = 0; i < row_width; i++)
  2316. {
  2317. png_uint_16 red, green, blue, w;
  2318. red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2319. green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2320. blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2321. if (red == green && red == blue)
  2322. w = red;
  2323. else
  2324. {
  2325. png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >>
  2326. png_ptr->gamma_shift][red>>8];
  2327. png_uint_16 green_1 =
  2328. png_ptr->gamma_16_to_1[(green&0xff) >>
  2329. png_ptr->gamma_shift][green>>8];
  2330. png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >>
  2331. png_ptr->gamma_shift][blue>>8];
  2332. png_uint_16 gray16 = (png_uint_16)((rc * red_1
  2333. + gc * green_1 + bc * blue_1)>>15);
  2334. w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
  2335. png_ptr->gamma_shift][gray16 >> 8];
  2336. rgb_error |= 1;
  2337. }
  2338. *(dp++) = (png_byte)((w>>8) & 0xff);
  2339. *(dp++) = (png_byte)(w & 0xff);
  2340. *(dp++) = *(sp++); /* alpha */
  2341. *(dp++) = *(sp++);
  2342. }
  2343. }
  2344. else
  2345. #endif
  2346. {
  2347. png_bytep sp = row;
  2348. png_bytep dp = row;
  2349. for (i = 0; i < row_width; i++)
  2350. {
  2351. png_uint_16 red, green, blue, gray16;
  2352. red = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
  2353. green = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
  2354. blue = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
  2355. if (red != green || red != blue)
  2356. rgb_error |= 1;
  2357. gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
  2358. *(dp++) = (png_byte)((gray16>>8) & 0xff);
  2359. *(dp++) = (png_byte)(gray16 & 0xff);
  2360. *(dp++) = *(sp++); /* alpha */
  2361. *(dp++) = *(sp++);
  2362. }
  2363. }
  2364. }
  2365. }
  2366. row_info->channels -= 2;
  2367. row_info->color_type = (png_byte)(row_info->color_type &
  2368. ~PNG_COLOR_MASK_COLOR);
  2369. row_info->pixel_depth = (png_byte)(row_info->channels *
  2370. row_info->bit_depth);
  2371. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  2372. }
  2373. return rgb_error;
  2374. }
  2375. #endif
  2376. /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
  2377. * large of png_color. This lets grayscale images be treated as
  2378. * paletted. Most useful for gamma correction and simplification
  2379. * of code.
  2380. */
  2381. void PNGAPI
  2382. png_build_grayscale_palette(int bit_depth, png_colorp palette)
  2383. {
  2384. int num_palette;
  2385. int color_inc;
  2386. int i;
  2387. int v;
  2388. png_debug(1, "in png_do_build_grayscale_palette");
  2389. if (palette == NULL)
  2390. return;
  2391. switch (bit_depth)
  2392. {
  2393. case 1:
  2394. num_palette = 2;
  2395. color_inc = 0xff;
  2396. break;
  2397. case 2:
  2398. num_palette = 4;
  2399. color_inc = 0x55;
  2400. break;
  2401. case 4:
  2402. num_palette = 16;
  2403. color_inc = 0x11;
  2404. break;
  2405. case 8:
  2406. num_palette = 256;
  2407. color_inc = 1;
  2408. break;
  2409. default:
  2410. num_palette = 0;
  2411. color_inc = 0;
  2412. break;
  2413. }
  2414. for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
  2415. {
  2416. palette[i].red = (png_byte)v;
  2417. palette[i].green = (png_byte)v;
  2418. palette[i].blue = (png_byte)v;
  2419. }
  2420. }
  2421. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  2422. /* Replace any alpha or transparency with the supplied background color.
  2423. * "background" is already in the screen gamma, while "background_1" is
  2424. * at a gamma of 1.0. Paletted files have already been taken care of.
  2425. */
  2426. void /* PRIVATE */
  2427. png_do_background(png_row_infop row_info, png_bytep row,
  2428. png_const_color_16p trans_color, png_const_color_16p background
  2429. #ifdef PNG_READ_GAMMA_SUPPORTED
  2430. , png_const_color_16p background_1, png_const_bytep gamma_table,
  2431. png_const_bytep gamma_from_1, png_const_bytep gamma_to_1,
  2432. png_const_uint_16pp gamma_16, png_const_uint_16pp gamma_16_from_1,
  2433. png_const_uint_16pp gamma_16_to_1, int gamma_shift
  2434. #endif
  2435. )
  2436. {
  2437. png_bytep sp, dp;
  2438. png_uint_32 i;
  2439. png_uint_32 row_width = row_info->width;
  2440. int shift;
  2441. png_debug(1, "in png_do_background");
  2442. if (background != NULL &&
  2443. (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
  2444. (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_color)))
  2445. {
  2446. switch (row_info->color_type)
  2447. {
  2448. case PNG_COLOR_TYPE_GRAY:
  2449. {
  2450. switch (row_info->bit_depth)
  2451. {
  2452. case 1:
  2453. {
  2454. sp = row;
  2455. shift = 7;
  2456. for (i = 0; i < row_width; i++)
  2457. {
  2458. if ((png_uint_16)((*sp >> shift) & 0x01)
  2459. == trans_color->gray)
  2460. {
  2461. *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  2462. *sp |= (png_byte)(background->gray << shift);
  2463. }
  2464. if (!shift)
  2465. {
  2466. shift = 7;
  2467. sp++;
  2468. }
  2469. else
  2470. shift--;
  2471. }
  2472. break;
  2473. }
  2474. case 2:
  2475. {
  2476. #ifdef PNG_READ_GAMMA_SUPPORTED
  2477. if (gamma_table != NULL)
  2478. {
  2479. sp = row;
  2480. shift = 6;
  2481. for (i = 0; i < row_width; i++)
  2482. {
  2483. if ((png_uint_16)((*sp >> shift) & 0x03)
  2484. == trans_color->gray)
  2485. {
  2486. *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  2487. *sp |= (png_byte)(background->gray << shift);
  2488. }
  2489. else
  2490. {
  2491. png_byte p = (png_byte)((*sp >> shift) & 0x03);
  2492. png_byte g = (png_byte)((gamma_table [p | (p << 2) |
  2493. (p << 4) | (p << 6)] >> 6) & 0x03);
  2494. *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  2495. *sp |= (png_byte)(g << shift);
  2496. }
  2497. if (!shift)
  2498. {
  2499. shift = 6;
  2500. sp++;
  2501. }
  2502. else
  2503. shift -= 2;
  2504. }
  2505. }
  2506. else
  2507. #endif
  2508. {
  2509. sp = row;
  2510. shift = 6;
  2511. for (i = 0; i < row_width; i++)
  2512. {
  2513. if ((png_uint_16)((*sp >> shift) & 0x03)
  2514. == trans_color->gray)
  2515. {
  2516. *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  2517. *sp |= (png_byte)(background->gray << shift);
  2518. }
  2519. if (!shift)
  2520. {
  2521. shift = 6;
  2522. sp++;
  2523. }
  2524. else
  2525. shift -= 2;
  2526. }
  2527. }
  2528. break;
  2529. }
  2530. case 4:
  2531. {
  2532. #ifdef PNG_READ_GAMMA_SUPPORTED
  2533. if (gamma_table != NULL)
  2534. {
  2535. sp = row;
  2536. shift = 4;
  2537. for (i = 0; i < row_width; i++)
  2538. {
  2539. if ((png_uint_16)((*sp >> shift) & 0x0f)
  2540. == trans_color->gray)
  2541. {
  2542. *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  2543. *sp |= (png_byte)(background->gray << shift);
  2544. }
  2545. else
  2546. {
  2547. png_byte p = (png_byte)((*sp >> shift) & 0x0f);
  2548. png_byte g = (png_byte)((gamma_table[p |
  2549. (p << 4)] >> 4) & 0x0f);
  2550. *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  2551. *sp |= (png_byte)(g << shift);
  2552. }
  2553. if (!shift)
  2554. {
  2555. shift = 4;
  2556. sp++;
  2557. }
  2558. else
  2559. shift -= 4;
  2560. }
  2561. }
  2562. else
  2563. #endif
  2564. {
  2565. sp = row;
  2566. shift = 4;
  2567. for (i = 0; i < row_width; i++)
  2568. {
  2569. if ((png_uint_16)((*sp >> shift) & 0x0f)
  2570. == trans_color->gray)
  2571. {
  2572. *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  2573. *sp |= (png_byte)(background->gray << shift);
  2574. }
  2575. if (!shift)
  2576. {
  2577. shift = 4;
  2578. sp++;
  2579. }
  2580. else
  2581. shift -= 4;
  2582. }
  2583. }
  2584. break;
  2585. }
  2586. case 8:
  2587. {
  2588. #ifdef PNG_READ_GAMMA_SUPPORTED
  2589. if (gamma_table != NULL)
  2590. {
  2591. sp = row;
  2592. for (i = 0; i < row_width; i++, sp++)
  2593. {
  2594. if (*sp == trans_color->gray)
  2595. *sp = (png_byte)background->gray;
  2596. else
  2597. *sp = gamma_table[*sp];
  2598. }
  2599. }
  2600. else
  2601. #endif
  2602. {
  2603. sp = row;
  2604. for (i = 0; i < row_width; i++, sp++)
  2605. {
  2606. if (*sp == trans_color->gray)
  2607. *sp = (png_byte)background->gray;
  2608. }
  2609. }
  2610. break;
  2611. }
  2612. case 16:
  2613. {
  2614. #ifdef PNG_READ_GAMMA_SUPPORTED
  2615. if (gamma_16 != NULL)
  2616. {
  2617. sp = row;
  2618. for (i = 0; i < row_width; i++, sp += 2)
  2619. {
  2620. png_uint_16 v;
  2621. v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  2622. if (v == trans_color->gray)
  2623. {
  2624. /* Background is already in screen gamma */
  2625. *sp = (png_byte)((background->gray >> 8) & 0xff);
  2626. *(sp + 1) = (png_byte)(background->gray & 0xff);
  2627. }
  2628. else
  2629. {
  2630. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  2631. *sp = (png_byte)((v >> 8) & 0xff);
  2632. *(sp + 1) = (png_byte)(v & 0xff);
  2633. }
  2634. }
  2635. }
  2636. else
  2637. #endif
  2638. {
  2639. sp = row;
  2640. for (i = 0; i < row_width; i++, sp += 2)
  2641. {
  2642. png_uint_16 v;
  2643. v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  2644. if (v == trans_color->gray)
  2645. {
  2646. *sp = (png_byte)((background->gray >> 8) & 0xff);
  2647. *(sp + 1) = (png_byte)(background->gray & 0xff);
  2648. }
  2649. }
  2650. }
  2651. break;
  2652. }
  2653. default:
  2654. break;
  2655. }
  2656. break;
  2657. }
  2658. case PNG_COLOR_TYPE_RGB:
  2659. {
  2660. if (row_info->bit_depth == 8)
  2661. {
  2662. #ifdef PNG_READ_GAMMA_SUPPORTED
  2663. if (gamma_table != NULL)
  2664. {
  2665. sp = row;
  2666. for (i = 0; i < row_width; i++, sp += 3)
  2667. {
  2668. if (*sp == trans_color->red &&
  2669. *(sp + 1) == trans_color->green &&
  2670. *(sp + 2) == trans_color->blue)
  2671. {
  2672. *sp = (png_byte)background->red;
  2673. *(sp + 1) = (png_byte)background->green;
  2674. *(sp + 2) = (png_byte)background->blue;
  2675. }
  2676. else
  2677. {
  2678. *sp = gamma_table[*sp];
  2679. *(sp + 1) = gamma_table[*(sp + 1)];
  2680. *(sp + 2) = gamma_table[*(sp + 2)];
  2681. }
  2682. }
  2683. }
  2684. else
  2685. #endif
  2686. {
  2687. sp = row;
  2688. for (i = 0; i < row_width; i++, sp += 3)
  2689. {
  2690. if (*sp == trans_color->red &&
  2691. *(sp + 1) == trans_color->green &&
  2692. *(sp + 2) == trans_color->blue)
  2693. {
  2694. *sp = (png_byte)background->red;
  2695. *(sp + 1) = (png_byte)background->green;
  2696. *(sp + 2) = (png_byte)background->blue;
  2697. }
  2698. }
  2699. }
  2700. }
  2701. else /* if (row_info->bit_depth == 16) */
  2702. {
  2703. #ifdef PNG_READ_GAMMA_SUPPORTED
  2704. if (gamma_16 != NULL)
  2705. {
  2706. sp = row;
  2707. for (i = 0; i < row_width; i++, sp += 6)
  2708. {
  2709. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  2710. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  2711. + *(sp + 3));
  2712. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  2713. + *(sp + 5));
  2714. if (r == trans_color->red && g == trans_color->green &&
  2715. b == trans_color->blue)
  2716. {
  2717. /* Background is already in screen gamma */
  2718. *sp = (png_byte)((background->red >> 8) & 0xff);
  2719. *(sp + 1) = (png_byte)(background->red & 0xff);
  2720. *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
  2721. *(sp + 3) = (png_byte)(background->green & 0xff);
  2722. *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  2723. *(sp + 5) = (png_byte)(background->blue & 0xff);
  2724. }
  2725. else
  2726. {
  2727. png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  2728. *sp = (png_byte)((v >> 8) & 0xff);
  2729. *(sp + 1) = (png_byte)(v & 0xff);
  2730. v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  2731. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  2732. *(sp + 3) = (png_byte)(v & 0xff);
  2733. v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  2734. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  2735. *(sp + 5) = (png_byte)(v & 0xff);
  2736. }
  2737. }
  2738. }
  2739. else
  2740. #endif
  2741. {
  2742. sp = row;
  2743. for (i = 0; i < row_width; i++, sp += 6)
  2744. {
  2745. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  2746. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  2747. + *(sp + 3));
  2748. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  2749. + *(sp + 5));
  2750. if (r == trans_color->red && g == trans_color->green &&
  2751. b == trans_color->blue)
  2752. {
  2753. *sp = (png_byte)((background->red >> 8) & 0xff);
  2754. *(sp + 1) = (png_byte)(background->red & 0xff);
  2755. *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
  2756. *(sp + 3) = (png_byte)(background->green & 0xff);
  2757. *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  2758. *(sp + 5) = (png_byte)(background->blue & 0xff);
  2759. }
  2760. }
  2761. }
  2762. }
  2763. break;
  2764. }
  2765. case PNG_COLOR_TYPE_GRAY_ALPHA:
  2766. {
  2767. if (row_info->bit_depth == 8)
  2768. {
  2769. #ifdef PNG_READ_GAMMA_SUPPORTED
  2770. if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  2771. gamma_table != NULL)
  2772. {
  2773. sp = row;
  2774. dp = row;
  2775. for (i = 0; i < row_width; i++, sp += 2, dp++)
  2776. {
  2777. png_uint_16 a = *(sp + 1);
  2778. if (a == 0xff)
  2779. *dp = gamma_table[*sp];
  2780. else if (a == 0)
  2781. {
  2782. /* Background is already in screen gamma */
  2783. *dp = (png_byte)background->gray;
  2784. }
  2785. else
  2786. {
  2787. png_byte v, w;
  2788. v = gamma_to_1[*sp];
  2789. png_composite(w, v, a, background_1->gray);
  2790. *dp = gamma_from_1[w];
  2791. }
  2792. }
  2793. }
  2794. else
  2795. #endif
  2796. {
  2797. sp = row;
  2798. dp = row;
  2799. for (i = 0; i < row_width; i++, sp += 2, dp++)
  2800. {
  2801. png_byte a = *(sp + 1);
  2802. if (a == 0xff)
  2803. *dp = *sp;
  2804. #ifdef PNG_READ_GAMMA_SUPPORTED
  2805. else if (a == 0)
  2806. *dp = (png_byte)background->gray;
  2807. else
  2808. png_composite(*dp, *sp, a, background_1->gray);
  2809. #else
  2810. *dp = (png_byte)background->gray;
  2811. #endif
  2812. }
  2813. }
  2814. }
  2815. else /* if (png_ptr->bit_depth == 16) */
  2816. {
  2817. #ifdef PNG_READ_GAMMA_SUPPORTED
  2818. if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  2819. gamma_16_to_1 != NULL)
  2820. {
  2821. sp = row;
  2822. dp = row;
  2823. for (i = 0; i < row_width; i++, sp += 4, dp += 2)
  2824. {
  2825. png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
  2826. + *(sp + 3));
  2827. if (a == (png_uint_16)0xffff)
  2828. {
  2829. png_uint_16 v;
  2830. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  2831. *dp = (png_byte)((v >> 8) & 0xff);
  2832. *(dp + 1) = (png_byte)(v & 0xff);
  2833. }
  2834. #ifdef PNG_READ_GAMMA_SUPPORTED
  2835. else if (a == 0)
  2836. #else
  2837. else
  2838. #endif
  2839. {
  2840. /* Background is already in screen gamma */
  2841. *dp = (png_byte)((background->gray >> 8) & 0xff);
  2842. *(dp + 1) = (png_byte)(background->gray & 0xff);
  2843. }
  2844. #ifdef PNG_READ_GAMMA_SUPPORTED
  2845. else
  2846. {
  2847. png_uint_16 g, v, w;
  2848. g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  2849. png_composite_16(v, g, a, background_1->gray);
  2850. w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
  2851. *dp = (png_byte)((w >> 8) & 0xff);
  2852. *(dp + 1) = (png_byte)(w & 0xff);
  2853. }
  2854. #endif
  2855. }
  2856. }
  2857. else
  2858. #endif
  2859. {
  2860. sp = row;
  2861. dp = row;
  2862. for (i = 0; i < row_width; i++, sp += 4, dp += 2)
  2863. {
  2864. png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
  2865. + *(sp + 3));
  2866. if (a == (png_uint_16)0xffff)
  2867. png_memcpy(dp, sp, 2);
  2868. #ifdef PNG_READ_GAMMA_SUPPORTED
  2869. else if (a == 0)
  2870. #else
  2871. else
  2872. #endif
  2873. {
  2874. *dp = (png_byte)((background->gray >> 8) & 0xff);
  2875. *(dp + 1) = (png_byte)(background->gray & 0xff);
  2876. }
  2877. #ifdef PNG_READ_GAMMA_SUPPORTED
  2878. else
  2879. {
  2880. png_uint_16 g, v;
  2881. g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  2882. png_composite_16(v, g, a, background_1->gray);
  2883. *dp = (png_byte)((v >> 8) & 0xff);
  2884. *(dp + 1) = (png_byte)(v & 0xff);
  2885. }
  2886. #endif
  2887. }
  2888. }
  2889. }
  2890. break;
  2891. }
  2892. case PNG_COLOR_TYPE_RGB_ALPHA:
  2893. {
  2894. if (row_info->bit_depth == 8)
  2895. {
  2896. #ifdef PNG_READ_GAMMA_SUPPORTED
  2897. if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  2898. gamma_table != NULL)
  2899. {
  2900. sp = row;
  2901. dp = row;
  2902. for (i = 0; i < row_width; i++, sp += 4, dp += 3)
  2903. {
  2904. png_byte a = *(sp + 3);
  2905. if (a == 0xff)
  2906. {
  2907. *dp = gamma_table[*sp];
  2908. *(dp + 1) = gamma_table[*(sp + 1)];
  2909. *(dp + 2) = gamma_table[*(sp + 2)];
  2910. }
  2911. else if (a == 0)
  2912. {
  2913. /* Background is already in screen gamma */
  2914. *dp = (png_byte)background->red;
  2915. *(dp + 1) = (png_byte)background->green;
  2916. *(dp + 2) = (png_byte)background->blue;
  2917. }
  2918. else
  2919. {
  2920. png_byte v, w;
  2921. v = gamma_to_1[*sp];
  2922. png_composite(w, v, a, background_1->red);
  2923. *dp = gamma_from_1[w];
  2924. v = gamma_to_1[*(sp + 1)];
  2925. png_composite(w, v, a, background_1->green);
  2926. *(dp + 1) = gamma_from_1[w];
  2927. v = gamma_to_1[*(sp + 2)];
  2928. png_composite(w, v, a, background_1->blue);
  2929. *(dp + 2) = gamma_from_1[w];
  2930. }
  2931. }
  2932. }
  2933. else
  2934. #endif
  2935. {
  2936. sp = row;
  2937. dp = row;
  2938. for (i = 0; i < row_width; i++, sp += 4, dp += 3)
  2939. {
  2940. png_byte a = *(sp + 3);
  2941. if (a == 0xff)
  2942. {
  2943. *dp = *sp;
  2944. *(dp + 1) = *(sp + 1);
  2945. *(dp + 2) = *(sp + 2);
  2946. }
  2947. else if (a == 0)
  2948. {
  2949. *dp = (png_byte)background->red;
  2950. *(dp + 1) = (png_byte)background->green;
  2951. *(dp + 2) = (png_byte)background->blue;
  2952. }
  2953. else
  2954. {
  2955. png_composite(*dp, *sp, a, background->red);
  2956. png_composite(*(dp + 1), *(sp + 1), a,
  2957. background->green);
  2958. png_composite(*(dp + 2), *(sp + 2), a,
  2959. background->blue);
  2960. }
  2961. }
  2962. }
  2963. }
  2964. else /* if (row_info->bit_depth == 16) */
  2965. {
  2966. #ifdef PNG_READ_GAMMA_SUPPORTED
  2967. if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  2968. gamma_16_to_1 != NULL)
  2969. {
  2970. sp = row;
  2971. dp = row;
  2972. for (i = 0; i < row_width; i++, sp += 8, dp += 6)
  2973. {
  2974. png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
  2975. << 8) + (png_uint_16)(*(sp + 7)));
  2976. if (a == (png_uint_16)0xffff)
  2977. {
  2978. png_uint_16 v;
  2979. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  2980. *dp = (png_byte)((v >> 8) & 0xff);
  2981. *(dp + 1) = (png_byte)(v & 0xff);
  2982. v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  2983. *(dp + 2) = (png_byte)((v >> 8) & 0xff);
  2984. *(dp + 3) = (png_byte)(v & 0xff);
  2985. v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  2986. *(dp + 4) = (png_byte)((v >> 8) & 0xff);
  2987. *(dp + 5) = (png_byte)(v & 0xff);
  2988. }
  2989. else if (a == 0)
  2990. {
  2991. /* Background is already in screen gamma */
  2992. *dp = (png_byte)((background->red >> 8) & 0xff);
  2993. *(dp + 1) = (png_byte)(background->red & 0xff);
  2994. *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
  2995. *(dp + 3) = (png_byte)(background->green & 0xff);
  2996. *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  2997. *(dp + 5) = (png_byte)(background->blue & 0xff);
  2998. }
  2999. else
  3000. {
  3001. png_uint_16 v, w, x;
  3002. v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  3003. png_composite_16(w, v, a, background_1->red);
  3004. x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
  3005. *dp = (png_byte)((x >> 8) & 0xff);
  3006. *(dp + 1) = (png_byte)(x & 0xff);
  3007. v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
  3008. png_composite_16(w, v, a, background_1->green);
  3009. x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
  3010. *(dp + 2) = (png_byte)((x >> 8) & 0xff);
  3011. *(dp + 3) = (png_byte)(x & 0xff);
  3012. v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
  3013. png_composite_16(w, v, a, background_1->blue);
  3014. x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
  3015. *(dp + 4) = (png_byte)((x >> 8) & 0xff);
  3016. *(dp + 5) = (png_byte)(x & 0xff);
  3017. }
  3018. }
  3019. }
  3020. else
  3021. #endif
  3022. {
  3023. sp = row;
  3024. dp = row;
  3025. for (i = 0; i < row_width; i++, sp += 8, dp += 6)
  3026. {
  3027. png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
  3028. << 8) + (png_uint_16)(*(sp + 7)));
  3029. if (a == (png_uint_16)0xffff)
  3030. {
  3031. png_memcpy(dp, sp, 6);
  3032. }
  3033. else if (a == 0)
  3034. {
  3035. *dp = (png_byte)((background->red >> 8) & 0xff);
  3036. *(dp + 1) = (png_byte)(background->red & 0xff);
  3037. *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
  3038. *(dp + 3) = (png_byte)(background->green & 0xff);
  3039. *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  3040. *(dp + 5) = (png_byte)(background->blue & 0xff);
  3041. }
  3042. else
  3043. {
  3044. png_uint_16 v;
  3045. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3046. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  3047. + *(sp + 3));
  3048. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  3049. + *(sp + 5));
  3050. png_composite_16(v, r, a, background->red);
  3051. *dp = (png_byte)((v >> 8) & 0xff);
  3052. *(dp + 1) = (png_byte)(v & 0xff);
  3053. png_composite_16(v, g, a, background->green);
  3054. *(dp + 2) = (png_byte)((v >> 8) & 0xff);
  3055. *(dp + 3) = (png_byte)(v & 0xff);
  3056. png_composite_16(v, b, a, background->blue);
  3057. *(dp + 4) = (png_byte)((v >> 8) & 0xff);
  3058. *(dp + 5) = (png_byte)(v & 0xff);
  3059. }
  3060. }
  3061. }
  3062. }
  3063. break;
  3064. }
  3065. default:
  3066. break;
  3067. }
  3068. if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
  3069. {
  3070. row_info->color_type = (png_byte)(row_info->color_type &
  3071. ~PNG_COLOR_MASK_ALPHA);
  3072. row_info->channels--;
  3073. row_info->pixel_depth = (png_byte)(row_info->channels *
  3074. row_info->bit_depth);
  3075. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  3076. }
  3077. }
  3078. }
  3079. #endif
  3080. #ifdef PNG_READ_GAMMA_SUPPORTED
  3081. /* Gamma correct the image, avoiding the alpha channel. Make sure
  3082. * you do this after you deal with the transparency issue on grayscale
  3083. * or RGB images. If your bit depth is 8, use gamma_table, if it
  3084. * is 16, use gamma_16_table and gamma_shift. Build these with
  3085. * build_gamma_table().
  3086. */
  3087. void /* PRIVATE */
  3088. png_do_gamma(png_row_infop row_info, png_bytep row,
  3089. png_const_bytep gamma_table, png_const_uint_16pp gamma_16_table,
  3090. int gamma_shift)
  3091. {
  3092. png_bytep sp;
  3093. png_uint_32 i;
  3094. png_uint_32 row_width=row_info->width;
  3095. png_debug(1, "in png_do_gamma");
  3096. if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
  3097. (row_info->bit_depth == 16 && gamma_16_table != NULL)))
  3098. {
  3099. switch (row_info->color_type)
  3100. {
  3101. case PNG_COLOR_TYPE_RGB:
  3102. {
  3103. if (row_info->bit_depth == 8)
  3104. {
  3105. sp = row;
  3106. for (i = 0; i < row_width; i++)
  3107. {
  3108. *sp = gamma_table[*sp];
  3109. sp++;
  3110. *sp = gamma_table[*sp];
  3111. sp++;
  3112. *sp = gamma_table[*sp];
  3113. sp++;
  3114. }
  3115. }
  3116. else /* if (row_info->bit_depth == 16) */
  3117. {
  3118. sp = row;
  3119. for (i = 0; i < row_width; i++)
  3120. {
  3121. png_uint_16 v;
  3122. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3123. *sp = (png_byte)((v >> 8) & 0xff);
  3124. *(sp + 1) = (png_byte)(v & 0xff);
  3125. sp += 2;
  3126. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3127. *sp = (png_byte)((v >> 8) & 0xff);
  3128. *(sp + 1) = (png_byte)(v & 0xff);
  3129. sp += 2;
  3130. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3131. *sp = (png_byte)((v >> 8) & 0xff);
  3132. *(sp + 1) = (png_byte)(v & 0xff);
  3133. sp += 2;
  3134. }
  3135. }
  3136. break;
  3137. }
  3138. case PNG_COLOR_TYPE_RGB_ALPHA:
  3139. {
  3140. if (row_info->bit_depth == 8)
  3141. {
  3142. sp = row;
  3143. for (i = 0; i < row_width; i++)
  3144. {
  3145. *sp = gamma_table[*sp];
  3146. sp++;
  3147. *sp = gamma_table[*sp];
  3148. sp++;
  3149. *sp = gamma_table[*sp];
  3150. sp++;
  3151. sp++;
  3152. }
  3153. }
  3154. else /* if (row_info->bit_depth == 16) */
  3155. {
  3156. sp = row;
  3157. for (i = 0; i < row_width; i++)
  3158. {
  3159. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3160. *sp = (png_byte)((v >> 8) & 0xff);
  3161. *(sp + 1) = (png_byte)(v & 0xff);
  3162. sp += 2;
  3163. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3164. *sp = (png_byte)((v >> 8) & 0xff);
  3165. *(sp + 1) = (png_byte)(v & 0xff);
  3166. sp += 2;
  3167. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3168. *sp = (png_byte)((v >> 8) & 0xff);
  3169. *(sp + 1) = (png_byte)(v & 0xff);
  3170. sp += 4;
  3171. }
  3172. }
  3173. break;
  3174. }
  3175. case PNG_COLOR_TYPE_GRAY_ALPHA:
  3176. {
  3177. if (row_info->bit_depth == 8)
  3178. {
  3179. sp = row;
  3180. for (i = 0; i < row_width; i++)
  3181. {
  3182. *sp = gamma_table[*sp];
  3183. sp += 2;
  3184. }
  3185. }
  3186. else /* if (row_info->bit_depth == 16) */
  3187. {
  3188. sp = row;
  3189. for (i = 0; i < row_width; i++)
  3190. {
  3191. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3192. *sp = (png_byte)((v >> 8) & 0xff);
  3193. *(sp + 1) = (png_byte)(v & 0xff);
  3194. sp += 4;
  3195. }
  3196. }
  3197. break;
  3198. }
  3199. case PNG_COLOR_TYPE_GRAY:
  3200. {
  3201. if (row_info->bit_depth == 2)
  3202. {
  3203. sp = row;
  3204. for (i = 0; i < row_width; i += 4)
  3205. {
  3206. int a = *sp & 0xc0;
  3207. int b = *sp & 0x30;
  3208. int c = *sp & 0x0c;
  3209. int d = *sp & 0x03;
  3210. *sp = (png_byte)(
  3211. ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
  3212. ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
  3213. ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
  3214. ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
  3215. sp++;
  3216. }
  3217. }
  3218. if (row_info->bit_depth == 4)
  3219. {
  3220. sp = row;
  3221. for (i = 0; i < row_width; i += 2)
  3222. {
  3223. int msb = *sp & 0xf0;
  3224. int lsb = *sp & 0x0f;
  3225. *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
  3226. | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
  3227. sp++;
  3228. }
  3229. }
  3230. else if (row_info->bit_depth == 8)
  3231. {
  3232. sp = row;
  3233. for (i = 0; i < row_width; i++)
  3234. {
  3235. *sp = gamma_table[*sp];
  3236. sp++;
  3237. }
  3238. }
  3239. else if (row_info->bit_depth == 16)
  3240. {
  3241. sp = row;
  3242. for (i = 0; i < row_width; i++)
  3243. {
  3244. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3245. *sp = (png_byte)((v >> 8) & 0xff);
  3246. *(sp + 1) = (png_byte)(v & 0xff);
  3247. sp += 2;
  3248. }
  3249. }
  3250. break;
  3251. }
  3252. default:
  3253. break;
  3254. }
  3255. }
  3256. }
  3257. #endif
  3258. #ifdef PNG_READ_EXPAND_SUPPORTED
  3259. /* Expands a palette row to an RGB or RGBA row depending
  3260. * upon whether you supply trans and num_trans.
  3261. */
  3262. void /* PRIVATE */
  3263. png_do_expand_palette(png_row_infop row_info, png_bytep row,
  3264. png_const_colorp palette, png_const_bytep trans_alpha, int num_trans)
  3265. {
  3266. int shift, value;
  3267. png_bytep sp, dp;
  3268. png_uint_32 i;
  3269. png_uint_32 row_width=row_info->width;
  3270. png_debug(1, "in png_do_expand_palette");
  3271. if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
  3272. {
  3273. if (row_info->bit_depth < 8)
  3274. {
  3275. switch (row_info->bit_depth)
  3276. {
  3277. case 1:
  3278. {
  3279. sp = row + (png_size_t)((row_width - 1) >> 3);
  3280. dp = row + (png_size_t)row_width - 1;
  3281. shift = 7 - (int)((row_width + 7) & 0x07);
  3282. for (i = 0; i < row_width; i++)
  3283. {
  3284. if ((*sp >> shift) & 0x01)
  3285. *dp = 1;
  3286. else
  3287. *dp = 0;
  3288. if (shift == 7)
  3289. {
  3290. shift = 0;
  3291. sp--;
  3292. }
  3293. else
  3294. shift++;
  3295. dp--;
  3296. }
  3297. break;
  3298. }
  3299. case 2:
  3300. {
  3301. sp = row + (png_size_t)((row_width - 1) >> 2);
  3302. dp = row + (png_size_t)row_width - 1;
  3303. shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  3304. for (i = 0; i < row_width; i++)
  3305. {
  3306. value = (*sp >> shift) & 0x03;
  3307. *dp = (png_byte)value;
  3308. if (shift == 6)
  3309. {
  3310. shift = 0;
  3311. sp--;
  3312. }
  3313. else
  3314. shift += 2;
  3315. dp--;
  3316. }
  3317. break;
  3318. }
  3319. case 4:
  3320. {
  3321. sp = row + (png_size_t)((row_width - 1) >> 1);
  3322. dp = row + (png_size_t)row_width - 1;
  3323. shift = (int)((row_width & 0x01) << 2);
  3324. for (i = 0; i < row_width; i++)
  3325. {
  3326. value = (*sp >> shift) & 0x0f;
  3327. *dp = (png_byte)value;
  3328. if (shift == 4)
  3329. {
  3330. shift = 0;
  3331. sp--;
  3332. }
  3333. else
  3334. shift += 4;
  3335. dp--;
  3336. }
  3337. break;
  3338. }
  3339. default:
  3340. break;
  3341. }
  3342. row_info->bit_depth = 8;
  3343. row_info->pixel_depth = 8;
  3344. row_info->rowbytes = row_width;
  3345. }
  3346. if (row_info->bit_depth == 8)
  3347. {
  3348. {
  3349. if (trans_alpha != NULL)
  3350. {
  3351. sp = row + (png_size_t)row_width - 1;
  3352. dp = row + (png_size_t)(row_width << 2) - 1;
  3353. for (i = 0; i < row_width; i++)
  3354. {
  3355. if ((int)(*sp) >= num_trans)
  3356. *dp-- = 0xff;
  3357. else
  3358. *dp-- = trans_alpha[*sp];
  3359. *dp-- = palette[*sp].blue;
  3360. *dp-- = palette[*sp].green;
  3361. *dp-- = palette[*sp].red;
  3362. sp--;
  3363. }
  3364. row_info->bit_depth = 8;
  3365. row_info->pixel_depth = 32;
  3366. row_info->rowbytes = row_width * 4;
  3367. row_info->color_type = 6;
  3368. row_info->channels = 4;
  3369. }
  3370. else
  3371. {
  3372. sp = row + (png_size_t)row_width - 1;
  3373. dp = row + (png_size_t)(row_width * 3) - 1;
  3374. for (i = 0; i < row_width; i++)
  3375. {
  3376. *dp-- = palette[*sp].blue;
  3377. *dp-- = palette[*sp].green;
  3378. *dp-- = palette[*sp].red;
  3379. sp--;
  3380. }
  3381. row_info->bit_depth = 8;
  3382. row_info->pixel_depth = 24;
  3383. row_info->rowbytes = row_width * 3;
  3384. row_info->color_type = 2;
  3385. row_info->channels = 3;
  3386. }
  3387. }
  3388. }
  3389. }
  3390. }
  3391. /* If the bit depth < 8, it is expanded to 8. Also, if the already
  3392. * expanded transparency value is supplied, an alpha channel is built.
  3393. */
  3394. void /* PRIVATE */
  3395. png_do_expand(png_row_infop row_info, png_bytep row,
  3396. png_const_color_16p trans_value)
  3397. {
  3398. int shift, value;
  3399. png_bytep sp, dp;
  3400. png_uint_32 i;
  3401. png_uint_32 row_width=row_info->width;
  3402. png_debug(1, "in png_do_expand");
  3403. {
  3404. if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  3405. {
  3406. png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
  3407. if (row_info->bit_depth < 8)
  3408. {
  3409. switch (row_info->bit_depth)
  3410. {
  3411. case 1:
  3412. {
  3413. gray = (png_uint_16)((gray & 0x01) * 0xff);
  3414. sp = row + (png_size_t)((row_width - 1) >> 3);
  3415. dp = row + (png_size_t)row_width - 1;
  3416. shift = 7 - (int)((row_width + 7) & 0x07);
  3417. for (i = 0; i < row_width; i++)
  3418. {
  3419. if ((*sp >> shift) & 0x01)
  3420. *dp = 0xff;
  3421. else
  3422. *dp = 0;
  3423. if (shift == 7)
  3424. {
  3425. shift = 0;
  3426. sp--;
  3427. }
  3428. else
  3429. shift++;
  3430. dp--;
  3431. }
  3432. break;
  3433. }
  3434. case 2:
  3435. {
  3436. gray = (png_uint_16)((gray & 0x03) * 0x55);
  3437. sp = row + (png_size_t)((row_width - 1) >> 2);
  3438. dp = row + (png_size_t)row_width - 1;
  3439. shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  3440. for (i = 0; i < row_width; i++)
  3441. {
  3442. value = (*sp >> shift) & 0x03;
  3443. *dp = (png_byte)(value | (value << 2) | (value << 4) |
  3444. (value << 6));
  3445. if (shift == 6)
  3446. {
  3447. shift = 0;
  3448. sp--;
  3449. }
  3450. else
  3451. shift += 2;
  3452. dp--;
  3453. }
  3454. break;
  3455. }
  3456. case 4:
  3457. {
  3458. gray = (png_uint_16)((gray & 0x0f) * 0x11);
  3459. sp = row + (png_size_t)((row_width - 1) >> 1);
  3460. dp = row + (png_size_t)row_width - 1;
  3461. shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
  3462. for (i = 0; i < row_width; i++)
  3463. {
  3464. value = (*sp >> shift) & 0x0f;
  3465. *dp = (png_byte)(value | (value << 4));
  3466. if (shift == 4)
  3467. {
  3468. shift = 0;
  3469. sp--;
  3470. }
  3471. else
  3472. shift = 4;
  3473. dp--;
  3474. }
  3475. break;
  3476. }
  3477. default:
  3478. break;
  3479. }
  3480. row_info->bit_depth = 8;
  3481. row_info->pixel_depth = 8;
  3482. row_info->rowbytes = row_width;
  3483. }
  3484. if (trans_value != NULL)
  3485. {
  3486. if (row_info->bit_depth == 8)
  3487. {
  3488. gray = gray & 0xff;
  3489. sp = row + (png_size_t)row_width - 1;
  3490. dp = row + (png_size_t)(row_width << 1) - 1;
  3491. for (i = 0; i < row_width; i++)
  3492. {
  3493. if (*sp == gray)
  3494. *dp-- = 0;
  3495. else
  3496. *dp-- = 0xff;
  3497. *dp-- = *sp--;
  3498. }
  3499. }
  3500. else if (row_info->bit_depth == 16)
  3501. {
  3502. png_byte gray_high = (png_byte)((gray >> 8) & 0xff);
  3503. png_byte gray_low = (png_byte)(gray & 0xff);
  3504. sp = row + row_info->rowbytes - 1;
  3505. dp = row + (row_info->rowbytes << 1) - 1;
  3506. for (i = 0; i < row_width; i++)
  3507. {
  3508. if (*(sp - 1) == gray_high && *(sp) == gray_low)
  3509. {
  3510. *dp-- = 0;
  3511. *dp-- = 0;
  3512. }
  3513. else
  3514. {
  3515. *dp-- = 0xff;
  3516. *dp-- = 0xff;
  3517. }
  3518. *dp-- = *sp--;
  3519. *dp-- = *sp--;
  3520. }
  3521. }
  3522. row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
  3523. row_info->channels = 2;
  3524. row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
  3525. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
  3526. row_width);
  3527. }
  3528. }
  3529. else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
  3530. {
  3531. if (row_info->bit_depth == 8)
  3532. {
  3533. png_byte red = (png_byte)(trans_value->red & 0xff);
  3534. png_byte green = (png_byte)(trans_value->green & 0xff);
  3535. png_byte blue = (png_byte)(trans_value->blue & 0xff);
  3536. sp = row + (png_size_t)row_info->rowbytes - 1;
  3537. dp = row + (png_size_t)(row_width << 2) - 1;
  3538. for (i = 0; i < row_width; i++)
  3539. {
  3540. if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
  3541. *dp-- = 0;
  3542. else
  3543. *dp-- = 0xff;
  3544. *dp-- = *sp--;
  3545. *dp-- = *sp--;
  3546. *dp-- = *sp--;
  3547. }
  3548. }
  3549. else if (row_info->bit_depth == 16)
  3550. {
  3551. png_byte red_high = (png_byte)((trans_value->red >> 8) & 0xff);
  3552. png_byte green_high = (png_byte)((trans_value->green >> 8) & 0xff);
  3553. png_byte blue_high = (png_byte)((trans_value->blue >> 8) & 0xff);
  3554. png_byte red_low = (png_byte)(trans_value->red & 0xff);
  3555. png_byte green_low = (png_byte)(trans_value->green & 0xff);
  3556. png_byte blue_low = (png_byte)(trans_value->blue & 0xff);
  3557. sp = row + row_info->rowbytes - 1;
  3558. dp = row + (png_size_t)(row_width << 3) - 1;
  3559. for (i = 0; i < row_width; i++)
  3560. {
  3561. if (*(sp - 5) == red_high &&
  3562. *(sp - 4) == red_low &&
  3563. *(sp - 3) == green_high &&
  3564. *(sp - 2) == green_low &&
  3565. *(sp - 1) == blue_high &&
  3566. *(sp ) == blue_low)
  3567. {
  3568. *dp-- = 0;
  3569. *dp-- = 0;
  3570. }
  3571. else
  3572. {
  3573. *dp-- = 0xff;
  3574. *dp-- = 0xff;
  3575. }
  3576. *dp-- = *sp--;
  3577. *dp-- = *sp--;
  3578. *dp-- = *sp--;
  3579. *dp-- = *sp--;
  3580. *dp-- = *sp--;
  3581. *dp-- = *sp--;
  3582. }
  3583. }
  3584. row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  3585. row_info->channels = 4;
  3586. row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
  3587. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  3588. }
  3589. }
  3590. }
  3591. #endif
  3592. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  3593. /* If the bit depth is 8 and the colour type is not a palette type expand the
  3594. * whole row to 16 bits. Has no effect otherwise.
  3595. */
  3596. void /* PRIVATE */
  3597. png_do_expand_16(png_row_infop row_info, png_bytep row)
  3598. {
  3599. if (row_info->bit_depth == 8 &&
  3600. row_info->color_type != PNG_COLOR_TYPE_PALETTE)
  3601. {
  3602. /* The row have a sequence of bytes containing [0..255] and we need
  3603. * to turn it into another row containing [0..65535], to do this we
  3604. * calculate:
  3605. *
  3606. * (input / 255) * 65535
  3607. *
  3608. * Which happens to be exactly input * 257 and this can be achieved
  3609. * simply by byte replication in place (copying backwards).
  3610. */
  3611. png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
  3612. png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */
  3613. while (dp > sp)
  3614. dp[-2] = dp[-1] = *--sp, dp -= 2;
  3615. row_info->rowbytes *= 2;
  3616. row_info->bit_depth = 16;
  3617. row_info->pixel_depth = (png_byte)(row_info->channels * 16);
  3618. }
  3619. }
  3620. #endif
  3621. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  3622. void /* PRIVATE */
  3623. png_do_quantize(png_row_infop row_info, png_bytep row,
  3624. png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
  3625. {
  3626. png_bytep sp, dp;
  3627. png_uint_32 i;
  3628. png_uint_32 row_width=row_info->width;
  3629. png_debug(1, "in png_do_quantize");
  3630. if (row_info->bit_depth == 8)
  3631. {
  3632. if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
  3633. {
  3634. int r, g, b, p;
  3635. sp = row;
  3636. dp = row;
  3637. for (i = 0; i < row_width; i++)
  3638. {
  3639. r = *sp++;
  3640. g = *sp++;
  3641. b = *sp++;
  3642. /* This looks real messy, but the compiler will reduce
  3643. * it down to a reasonable formula. For example, with
  3644. * 5 bits per color, we get:
  3645. * p = (((r >> 3) & 0x1f) << 10) |
  3646. * (((g >> 3) & 0x1f) << 5) |
  3647. * ((b >> 3) & 0x1f);
  3648. */
  3649. p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
  3650. ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
  3651. (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
  3652. (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
  3653. ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
  3654. (PNG_QUANTIZE_BLUE_BITS)) |
  3655. ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
  3656. ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
  3657. *dp++ = palette_lookup[p];
  3658. }
  3659. row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  3660. row_info->channels = 1;
  3661. row_info->pixel_depth = row_info->bit_depth;
  3662. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  3663. }
  3664. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  3665. palette_lookup != NULL)
  3666. {
  3667. int r, g, b, p;
  3668. sp = row;
  3669. dp = row;
  3670. for (i = 0; i < row_width; i++)
  3671. {
  3672. r = *sp++;
  3673. g = *sp++;
  3674. b = *sp++;
  3675. sp++;
  3676. p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
  3677. ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
  3678. (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
  3679. (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
  3680. ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
  3681. (PNG_QUANTIZE_BLUE_BITS)) |
  3682. ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
  3683. ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
  3684. *dp++ = palette_lookup[p];
  3685. }
  3686. row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  3687. row_info->channels = 1;
  3688. row_info->pixel_depth = row_info->bit_depth;
  3689. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  3690. }
  3691. else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
  3692. quantize_lookup)
  3693. {
  3694. sp = row;
  3695. for (i = 0; i < row_width; i++, sp++)
  3696. {
  3697. *sp = quantize_lookup[*sp];
  3698. }
  3699. }
  3700. }
  3701. }
  3702. #endif /* PNG_READ_QUANTIZE_SUPPORTED */
  3703. #ifdef PNG_MNG_FEATURES_SUPPORTED
  3704. /* Undoes intrapixel differencing */
  3705. void /* PRIVATE */
  3706. png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
  3707. {
  3708. png_debug(1, "in png_do_read_intrapixel");
  3709. if (
  3710. (row_info->color_type & PNG_COLOR_MASK_COLOR))
  3711. {
  3712. int bytes_per_pixel;
  3713. png_uint_32 row_width = row_info->width;
  3714. if (row_info->bit_depth == 8)
  3715. {
  3716. png_bytep rp;
  3717. png_uint_32 i;
  3718. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  3719. bytes_per_pixel = 3;
  3720. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  3721. bytes_per_pixel = 4;
  3722. else
  3723. return;
  3724. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  3725. {
  3726. *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
  3727. *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
  3728. }
  3729. }
  3730. else if (row_info->bit_depth == 16)
  3731. {
  3732. png_bytep rp;
  3733. png_uint_32 i;
  3734. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  3735. bytes_per_pixel = 6;
  3736. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  3737. bytes_per_pixel = 8;
  3738. else
  3739. return;
  3740. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  3741. {
  3742. png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
  3743. png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
  3744. png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
  3745. png_uint_32 red = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
  3746. png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
  3747. *(rp ) = (png_byte)((red >> 8) & 0xff);
  3748. *(rp + 1) = (png_byte)(red & 0xff);
  3749. *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
  3750. *(rp + 5) = (png_byte)(blue & 0xff);
  3751. }
  3752. }
  3753. }
  3754. }
  3755. #endif /* PNG_MNG_FEATURES_SUPPORTED */
  3756. #endif /* PNG_READ_SUPPORTED */