PageRenderTime 88ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/src/FreeImage/Source/LibPNG/pngrtran.c

https://bitbucket.org/cabalistic/ogredeps/
C | 5023 lines | 3620 code | 639 blank | 764 comment | 777 complexity | ce6789b8366bc3c5861c3acda0174527 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  1. /* pngrtran.c - transforms the data in a row for PNG readers
  2. *
  3. * Last changed in libpng 1.5.7 [December 15, 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_COMPOSE | PNG_STRIP_ALPHA;
  90. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  91. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  92. png_memcpy(&(png_ptr->background), background_color,
  93. png_sizeof(png_color_16));
  94. png_ptr->background_gamma = background_gamma;
  95. png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
  96. if (need_expand)
  97. png_ptr->transformations |= PNG_BACKGROUND_EXPAND;
  98. else
  99. png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
  100. }
  101. # ifdef PNG_FLOATING_POINT_SUPPORTED
  102. void PNGAPI
  103. png_set_background(png_structp png_ptr,
  104. png_const_color_16p background_color, int background_gamma_code,
  105. int need_expand, double background_gamma)
  106. {
  107. png_set_background_fixed(png_ptr, background_color, background_gamma_code,
  108. need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
  109. }
  110. # endif /* FLOATING_POINT */
  111. #endif /* READ_BACKGROUND */
  112. /* Scale 16-bit depth files to 8-bit depth. If both of these are set then the
  113. * one that pngrtran does first (scale) happens. This is necessary to allow the
  114. * TRANSFORM and API behavior to be somewhat consistent, and it's simpler.
  115. */
  116. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  117. void PNGAPI
  118. png_set_scale_16(png_structp png_ptr)
  119. {
  120. png_debug(1, "in png_set_scale_16");
  121. if (png_ptr == NULL)
  122. return;
  123. png_ptr->transformations |= PNG_SCALE_16_TO_8;
  124. }
  125. #endif
  126. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  127. /* Chop 16-bit depth files to 8-bit depth */
  128. void PNGAPI
  129. png_set_strip_16(png_structp png_ptr)
  130. {
  131. png_debug(1, "in png_set_strip_16");
  132. if (png_ptr == NULL)
  133. return;
  134. png_ptr->transformations |= PNG_16_TO_8;
  135. }
  136. #endif
  137. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  138. void PNGAPI
  139. png_set_strip_alpha(png_structp png_ptr)
  140. {
  141. png_debug(1, "in png_set_strip_alpha");
  142. if (png_ptr == NULL)
  143. return;
  144. png_ptr->transformations |= PNG_STRIP_ALPHA;
  145. }
  146. #endif
  147. #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
  148. static png_fixed_point
  149. translate_gamma_flags(png_structp png_ptr, png_fixed_point output_gamma,
  150. int is_screen)
  151. {
  152. /* Check for flag values. The main reason for having the old Mac value as a
  153. * flag is that it is pretty near impossible to work out what the correct
  154. * value is from Apple documentation - a working Mac system is needed to
  155. * discover the value!
  156. */
  157. if (output_gamma == PNG_DEFAULT_sRGB ||
  158. output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB)
  159. {
  160. /* If there is no sRGB support this just sets the gamma to the standard
  161. * sRGB value. (This is a side effect of using this function!)
  162. */
  163. # ifdef PNG_READ_sRGB_SUPPORTED
  164. png_ptr->flags |= PNG_FLAG_ASSUME_sRGB;
  165. # endif
  166. if (is_screen)
  167. output_gamma = PNG_GAMMA_sRGB;
  168. else
  169. output_gamma = PNG_GAMMA_sRGB_INVERSE;
  170. }
  171. else if (output_gamma == PNG_GAMMA_MAC_18 ||
  172. output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18)
  173. {
  174. if (is_screen)
  175. output_gamma = PNG_GAMMA_MAC_OLD;
  176. else
  177. output_gamma = PNG_GAMMA_MAC_INVERSE;
  178. }
  179. return output_gamma;
  180. }
  181. # ifdef PNG_FLOATING_POINT_SUPPORTED
  182. static png_fixed_point
  183. convert_gamma_value(png_structp png_ptr, double output_gamma)
  184. {
  185. /* The following silently ignores cases where fixed point (times 100,000)
  186. * gamma values are passed to the floating point API. This is safe and it
  187. * means the fixed point constants work just fine with the floating point
  188. * API. The alternative would just lead to undetected errors and spurious
  189. * bug reports. Negative values fail inside the _fixed API unless they
  190. * correspond to the flag values.
  191. */
  192. if (output_gamma > 0 && output_gamma < 128)
  193. output_gamma *= PNG_FP_1;
  194. /* This preserves -1 and -2 exactly: */
  195. output_gamma = floor(output_gamma + .5);
  196. if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN)
  197. png_fixed_error(png_ptr, "gamma value");
  198. return (png_fixed_point)output_gamma;
  199. }
  200. # endif
  201. #endif /* READ_ALPHA_MODE || READ_GAMMA */
  202. #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  203. void PNGFAPI
  204. png_set_alpha_mode_fixed(png_structp png_ptr, int mode,
  205. png_fixed_point output_gamma)
  206. {
  207. int compose = 0;
  208. png_fixed_point file_gamma;
  209. png_debug(1, "in png_set_alpha_mode");
  210. if (png_ptr == NULL)
  211. return;
  212. output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/);
  213. /* Validate the value to ensure it is in a reasonable range. The value
  214. * is expected to be 1 or greater, but this range test allows for some
  215. * viewing correction values. The intent is to weed out users of this API
  216. * who use the inverse of the gamma value accidentally! Since some of these
  217. * values are reasonable this may have to be changed.
  218. */
  219. if (output_gamma < 70000 || output_gamma > 300000)
  220. png_error(png_ptr, "output gamma out of expected range");
  221. /* The default file gamma is the inverse of the output gamma; the output
  222. * gamma may be changed below so get the file value first:
  223. */
  224. file_gamma = png_reciprocal(output_gamma);
  225. /* There are really 8 possibilities here, composed of any combination
  226. * of:
  227. *
  228. * premultiply the color channels
  229. * do not encode non-opaque pixels
  230. * encode the alpha as well as the color channels
  231. *
  232. * The differences disappear if the input/output ('screen') gamma is 1.0,
  233. * because then the encoding is a no-op and there is only the choice of
  234. * premultiplying the color channels or not.
  235. *
  236. * png_set_alpha_mode and png_set_background interact because both use
  237. * png_compose to do the work. Calling both is only useful when
  238. * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along
  239. * with a default gamma value. Otherwise PNG_COMPOSE must not be set.
  240. */
  241. switch (mode)
  242. {
  243. case PNG_ALPHA_PNG: /* default: png standard */
  244. /* No compose, but it may be set by png_set_background! */
  245. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  246. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  247. break;
  248. case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */
  249. compose = 1;
  250. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  251. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  252. /* The output is linear: */
  253. output_gamma = PNG_FP_1;
  254. break;
  255. case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */
  256. compose = 1;
  257. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  258. png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA;
  259. /* output_gamma records the encoding of opaque pixels! */
  260. break;
  261. case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */
  262. compose = 1;
  263. png_ptr->transformations |= PNG_ENCODE_ALPHA;
  264. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  265. break;
  266. default:
  267. png_error(png_ptr, "invalid alpha mode");
  268. }
  269. /* Only set the default gamma if the file gamma has not been set (this has
  270. * the side effect that the gamma in a second call to png_set_alpha_mode will
  271. * be ignored.)
  272. */
  273. if (png_ptr->gamma == 0)
  274. png_ptr->gamma = file_gamma;
  275. /* But always set the output gamma: */
  276. png_ptr->screen_gamma = output_gamma;
  277. /* Finally, if pre-multiplying, set the background fields to achieve the
  278. * desired result.
  279. */
  280. if (compose)
  281. {
  282. /* And obtain alpha pre-multiplication by composing on black: */
  283. png_memset(&png_ptr->background, 0, sizeof png_ptr->background);
  284. png_ptr->background_gamma = png_ptr->gamma; /* just in case */
  285. png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE;
  286. png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
  287. if (png_ptr->transformations & PNG_COMPOSE)
  288. png_error(png_ptr,
  289. "conflicting calls to set alpha mode and background");
  290. png_ptr->transformations |= PNG_COMPOSE;
  291. }
  292. /* New API, make sure apps call the correct initializers: */
  293. png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
  294. }
  295. # ifdef PNG_FLOATING_POINT_SUPPORTED
  296. void PNGAPI
  297. png_set_alpha_mode(png_structp png_ptr, int mode, double output_gamma)
  298. {
  299. png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr,
  300. output_gamma));
  301. }
  302. # endif
  303. #endif
  304. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  305. /* Dither file to 8-bit. Supply a palette, the current number
  306. * of elements in the palette, the maximum number of elements
  307. * allowed, and a histogram if possible. If the current number
  308. * of colors is greater then the maximum number, the palette will be
  309. * modified to fit in the maximum number. "full_quantize" indicates
  310. * whether we need a quantizing cube set up for RGB images, or if we
  311. * simply are reducing the number of colors in a paletted image.
  312. */
  313. typedef struct png_dsort_struct
  314. {
  315. struct png_dsort_struct FAR * next;
  316. png_byte left;
  317. png_byte right;
  318. } png_dsort;
  319. typedef png_dsort FAR * png_dsortp;
  320. typedef png_dsort FAR * FAR * png_dsortpp;
  321. void PNGAPI
  322. png_set_quantize(png_structp png_ptr, png_colorp palette,
  323. int num_palette, int maximum_colors, png_const_uint_16p histogram,
  324. int full_quantize)
  325. {
  326. png_debug(1, "in png_set_quantize");
  327. if (png_ptr == NULL)
  328. return;
  329. png_ptr->transformations |= PNG_QUANTIZE;
  330. if (!full_quantize)
  331. {
  332. int i;
  333. png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
  334. (png_uint_32)(num_palette * png_sizeof(png_byte)));
  335. for (i = 0; i < num_palette; i++)
  336. png_ptr->quantize_index[i] = (png_byte)i;
  337. }
  338. if (num_palette > maximum_colors)
  339. {
  340. if (histogram != NULL)
  341. {
  342. /* This is easy enough, just throw out the least used colors.
  343. * Perhaps not the best solution, but good enough.
  344. */
  345. int i;
  346. /* Initialize an array to sort colors */
  347. png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
  348. (png_uint_32)(num_palette * png_sizeof(png_byte)));
  349. /* Initialize the quantize_sort array */
  350. for (i = 0; i < num_palette; i++)
  351. png_ptr->quantize_sort[i] = (png_byte)i;
  352. /* Find the least used palette entries by starting a
  353. * bubble sort, and running it until we have sorted
  354. * out enough colors. Note that we don't care about
  355. * sorting all the colors, just finding which are
  356. * least used.
  357. */
  358. for (i = num_palette - 1; i >= maximum_colors; i--)
  359. {
  360. int done; /* To stop early if the list is pre-sorted */
  361. int j;
  362. done = 1;
  363. for (j = 0; j < i; j++)
  364. {
  365. if (histogram[png_ptr->quantize_sort[j]]
  366. < histogram[png_ptr->quantize_sort[j + 1]])
  367. {
  368. png_byte t;
  369. t = png_ptr->quantize_sort[j];
  370. png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
  371. png_ptr->quantize_sort[j + 1] = t;
  372. done = 0;
  373. }
  374. }
  375. if (done)
  376. break;
  377. }
  378. /* Swap the palette around, and set up a table, if necessary */
  379. if (full_quantize)
  380. {
  381. int j = num_palette;
  382. /* Put all the useful colors within the max, but don't
  383. * move the others.
  384. */
  385. for (i = 0; i < maximum_colors; i++)
  386. {
  387. if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
  388. {
  389. do
  390. j--;
  391. while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
  392. palette[i] = palette[j];
  393. }
  394. }
  395. }
  396. else
  397. {
  398. int j = num_palette;
  399. /* Move all the used colors inside the max limit, and
  400. * develop a translation table.
  401. */
  402. for (i = 0; i < maximum_colors; i++)
  403. {
  404. /* Only move the colors we need to */
  405. if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
  406. {
  407. png_color tmp_color;
  408. do
  409. j--;
  410. while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
  411. tmp_color = palette[j];
  412. palette[j] = palette[i];
  413. palette[i] = tmp_color;
  414. /* Indicate where the color went */
  415. png_ptr->quantize_index[j] = (png_byte)i;
  416. png_ptr->quantize_index[i] = (png_byte)j;
  417. }
  418. }
  419. /* Find closest color for those colors we are not using */
  420. for (i = 0; i < num_palette; i++)
  421. {
  422. if ((int)png_ptr->quantize_index[i] >= maximum_colors)
  423. {
  424. int min_d, k, min_k, d_index;
  425. /* Find the closest color to one we threw out */
  426. d_index = png_ptr->quantize_index[i];
  427. min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
  428. for (k = 1, min_k = 0; k < maximum_colors; k++)
  429. {
  430. int d;
  431. d = PNG_COLOR_DIST(palette[d_index], palette[k]);
  432. if (d < min_d)
  433. {
  434. min_d = d;
  435. min_k = k;
  436. }
  437. }
  438. /* Point to closest color */
  439. png_ptr->quantize_index[i] = (png_byte)min_k;
  440. }
  441. }
  442. }
  443. png_free(png_ptr, png_ptr->quantize_sort);
  444. png_ptr->quantize_sort = NULL;
  445. }
  446. else
  447. {
  448. /* This is much harder to do simply (and quickly). Perhaps
  449. * we need to go through a median cut routine, but those
  450. * don't always behave themselves with only a few colors
  451. * as input. So we will just find the closest two colors,
  452. * and throw out one of them (chosen somewhat randomly).
  453. * [We don't understand this at all, so if someone wants to
  454. * work on improving it, be our guest - AED, GRP]
  455. */
  456. int i;
  457. int max_d;
  458. int num_new_palette;
  459. png_dsortp t;
  460. png_dsortpp hash;
  461. t = NULL;
  462. /* Initialize palette index arrays */
  463. png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
  464. (png_uint_32)(num_palette * png_sizeof(png_byte)));
  465. png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
  466. (png_uint_32)(num_palette * png_sizeof(png_byte)));
  467. /* Initialize the sort array */
  468. for (i = 0; i < num_palette; i++)
  469. {
  470. png_ptr->index_to_palette[i] = (png_byte)i;
  471. png_ptr->palette_to_index[i] = (png_byte)i;
  472. }
  473. hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
  474. png_sizeof(png_dsortp)));
  475. num_new_palette = num_palette;
  476. /* Initial wild guess at how far apart the farthest pixel
  477. * pair we will be eliminating will be. Larger
  478. * numbers mean more areas will be allocated, Smaller
  479. * numbers run the risk of not saving enough data, and
  480. * having to do this all over again.
  481. *
  482. * I have not done extensive checking on this number.
  483. */
  484. max_d = 96;
  485. while (num_new_palette > maximum_colors)
  486. {
  487. for (i = 0; i < num_new_palette - 1; i++)
  488. {
  489. int j;
  490. for (j = i + 1; j < num_new_palette; j++)
  491. {
  492. int d;
  493. d = PNG_COLOR_DIST(palette[i], palette[j]);
  494. if (d <= max_d)
  495. {
  496. t = (png_dsortp)png_malloc_warn(png_ptr,
  497. (png_uint_32)(png_sizeof(png_dsort)));
  498. if (t == NULL)
  499. break;
  500. t->next = hash[d];
  501. t->left = (png_byte)i;
  502. t->right = (png_byte)j;
  503. hash[d] = t;
  504. }
  505. }
  506. if (t == NULL)
  507. break;
  508. }
  509. if (t != NULL)
  510. for (i = 0; i <= max_d; i++)
  511. {
  512. if (hash[i] != NULL)
  513. {
  514. png_dsortp p;
  515. for (p = hash[i]; p; p = p->next)
  516. {
  517. if ((int)png_ptr->index_to_palette[p->left]
  518. < num_new_palette &&
  519. (int)png_ptr->index_to_palette[p->right]
  520. < num_new_palette)
  521. {
  522. int j, next_j;
  523. if (num_new_palette & 0x01)
  524. {
  525. j = p->left;
  526. next_j = p->right;
  527. }
  528. else
  529. {
  530. j = p->right;
  531. next_j = p->left;
  532. }
  533. num_new_palette--;
  534. palette[png_ptr->index_to_palette[j]]
  535. = palette[num_new_palette];
  536. if (!full_quantize)
  537. {
  538. int k;
  539. for (k = 0; k < num_palette; k++)
  540. {
  541. if (png_ptr->quantize_index[k] ==
  542. png_ptr->index_to_palette[j])
  543. png_ptr->quantize_index[k] =
  544. png_ptr->index_to_palette[next_j];
  545. if ((int)png_ptr->quantize_index[k] ==
  546. num_new_palette)
  547. png_ptr->quantize_index[k] =
  548. png_ptr->index_to_palette[j];
  549. }
  550. }
  551. png_ptr->index_to_palette[png_ptr->palette_to_index
  552. [num_new_palette]] = png_ptr->index_to_palette[j];
  553. png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
  554. = png_ptr->palette_to_index[num_new_palette];
  555. png_ptr->index_to_palette[j] =
  556. (png_byte)num_new_palette;
  557. png_ptr->palette_to_index[num_new_palette] =
  558. (png_byte)j;
  559. }
  560. if (num_new_palette <= maximum_colors)
  561. break;
  562. }
  563. if (num_new_palette <= maximum_colors)
  564. break;
  565. }
  566. }
  567. for (i = 0; i < 769; i++)
  568. {
  569. if (hash[i] != NULL)
  570. {
  571. png_dsortp p = hash[i];
  572. while (p)
  573. {
  574. t = p->next;
  575. png_free(png_ptr, p);
  576. p = t;
  577. }
  578. }
  579. hash[i] = 0;
  580. }
  581. max_d += 96;
  582. }
  583. png_free(png_ptr, hash);
  584. png_free(png_ptr, png_ptr->palette_to_index);
  585. png_free(png_ptr, png_ptr->index_to_palette);
  586. png_ptr->palette_to_index = NULL;
  587. png_ptr->index_to_palette = NULL;
  588. }
  589. num_palette = maximum_colors;
  590. }
  591. if (png_ptr->palette == NULL)
  592. {
  593. png_ptr->palette = palette;
  594. }
  595. png_ptr->num_palette = (png_uint_16)num_palette;
  596. if (full_quantize)
  597. {
  598. int i;
  599. png_bytep distance;
  600. int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
  601. PNG_QUANTIZE_BLUE_BITS;
  602. int num_red = (1 << PNG_QUANTIZE_RED_BITS);
  603. int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
  604. int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
  605. png_size_t num_entries = ((png_size_t)1 << total_bits);
  606. png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
  607. (png_uint_32)(num_entries * png_sizeof(png_byte)));
  608. distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
  609. png_sizeof(png_byte)));
  610. png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
  611. for (i = 0; i < num_palette; i++)
  612. {
  613. int ir, ig, ib;
  614. int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
  615. int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
  616. int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));
  617. for (ir = 0; ir < num_red; ir++)
  618. {
  619. /* int dr = abs(ir - r); */
  620. int dr = ((ir > r) ? ir - r : r - ir);
  621. int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
  622. PNG_QUANTIZE_GREEN_BITS));
  623. for (ig = 0; ig < num_green; ig++)
  624. {
  625. /* int dg = abs(ig - g); */
  626. int dg = ((ig > g) ? ig - g : g - ig);
  627. int dt = dr + dg;
  628. int dm = ((dr > dg) ? dr : dg);
  629. int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);
  630. for (ib = 0; ib < num_blue; ib++)
  631. {
  632. int d_index = index_g | ib;
  633. /* int db = abs(ib - b); */
  634. int db = ((ib > b) ? ib - b : b - ib);
  635. int dmax = ((dm > db) ? dm : db);
  636. int d = dmax + dt + db;
  637. if (d < (int)distance[d_index])
  638. {
  639. distance[d_index] = (png_byte)d;
  640. png_ptr->palette_lookup[d_index] = (png_byte)i;
  641. }
  642. }
  643. }
  644. }
  645. }
  646. png_free(png_ptr, distance);
  647. }
  648. }
  649. #endif /* PNG_READ_QUANTIZE_SUPPORTED */
  650. #ifdef PNG_READ_GAMMA_SUPPORTED
  651. void PNGFAPI
  652. png_set_gamma_fixed(png_structp png_ptr, png_fixed_point scrn_gamma,
  653. png_fixed_point file_gamma)
  654. {
  655. png_debug(1, "in png_set_gamma_fixed");
  656. if (png_ptr == NULL)
  657. return;
  658. /* New in libpng-1.5.4 - reserve particular negative values as flags. */
  659. scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/);
  660. file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/);
  661. #if PNG_LIBPNG_VER >= 10600
  662. /* Checking the gamma values for being >0 was added in 1.5.4 along with the
  663. * premultiplied alpha support; this actually hides an undocumented feature
  664. * of the previous implementation which allowed gamma processing to be
  665. * disabled in background handling. There is no evidence (so far) that this
  666. * was being used; however, png_set_background itself accepted and must still
  667. * accept '0' for the gamma value it takes, because it isn't always used.
  668. *
  669. * Since this is an API change (albeit a very minor one that removes an
  670. * undocumented API feature) it will not be made until libpng-1.6.0.
  671. */
  672. if (file_gamma <= 0)
  673. png_error(png_ptr, "invalid file gamma in png_set_gamma");
  674. if (scrn_gamma <= 0)
  675. png_error(png_ptr, "invalid screen gamma in png_set_gamma");
  676. #endif
  677. /* Set the gamma values unconditionally - this overrides the value in the PNG
  678. * file if a gAMA chunk was present. png_set_alpha_mode provides a
  679. * different, easier, way to default the file gamma.
  680. */
  681. png_ptr->gamma = file_gamma;
  682. png_ptr->screen_gamma = scrn_gamma;
  683. }
  684. # ifdef PNG_FLOATING_POINT_SUPPORTED
  685. void PNGAPI
  686. png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
  687. {
  688. png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma),
  689. convert_gamma_value(png_ptr, file_gamma));
  690. }
  691. # endif /* FLOATING_POINT_SUPPORTED */
  692. #endif /* READ_GAMMA */
  693. #ifdef PNG_READ_EXPAND_SUPPORTED
  694. /* Expand paletted images to RGB, expand grayscale images of
  695. * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
  696. * to alpha channels.
  697. */
  698. void PNGAPI
  699. png_set_expand(png_structp png_ptr)
  700. {
  701. png_debug(1, "in png_set_expand");
  702. if (png_ptr == NULL)
  703. return;
  704. png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  705. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  706. }
  707. /* GRR 19990627: the following three functions currently are identical
  708. * to png_set_expand(). However, it is entirely reasonable that someone
  709. * might wish to expand an indexed image to RGB but *not* expand a single,
  710. * fully transparent palette entry to a full alpha channel--perhaps instead
  711. * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
  712. * the transparent color with a particular RGB value, or drop tRNS entirely.
  713. * IOW, a future version of the library may make the transformations flag
  714. * a bit more fine-grained, with separate bits for each of these three
  715. * functions.
  716. *
  717. * More to the point, these functions make it obvious what libpng will be
  718. * doing, whereas "expand" can (and does) mean any number of things.
  719. *
  720. * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
  721. * to expand only the sample depth but not to expand the tRNS to alpha
  722. * and its name was changed to png_set_expand_gray_1_2_4_to_8().
  723. */
  724. /* Expand paletted images to RGB. */
  725. void PNGAPI
  726. png_set_palette_to_rgb(png_structp png_ptr)
  727. {
  728. png_debug(1, "in png_set_palette_to_rgb");
  729. if (png_ptr == NULL)
  730. return;
  731. png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  732. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  733. }
  734. /* Expand grayscale images of less than 8-bit depth to 8 bits. */
  735. void PNGAPI
  736. png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
  737. {
  738. png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
  739. if (png_ptr == NULL)
  740. return;
  741. png_ptr->transformations |= PNG_EXPAND;
  742. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  743. }
  744. /* Expand tRNS chunks to alpha channels. */
  745. void PNGAPI
  746. png_set_tRNS_to_alpha(png_structp png_ptr)
  747. {
  748. png_debug(1, "in png_set_tRNS_to_alpha");
  749. png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  750. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  751. }
  752. #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
  753. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  754. /* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise
  755. * it may not work correctly.)
  756. */
  757. void PNGAPI
  758. png_set_expand_16(png_structp png_ptr)
  759. {
  760. png_debug(1, "in png_set_expand_16");
  761. if (png_ptr == NULL)
  762. return;
  763. png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
  764. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  765. /* New API, make sure apps call the correct initializers: */
  766. png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
  767. }
  768. #endif
  769. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  770. void PNGAPI
  771. png_set_gray_to_rgb(png_structp png_ptr)
  772. {
  773. png_debug(1, "in png_set_gray_to_rgb");
  774. if (png_ptr != NULL)
  775. {
  776. /* Because rgb must be 8 bits or more: */
  777. png_set_expand_gray_1_2_4_to_8(png_ptr);
  778. png_ptr->transformations |= PNG_GRAY_TO_RGB;
  779. png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
  780. }
  781. }
  782. #endif
  783. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  784. void PNGFAPI
  785. png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
  786. png_fixed_point red, png_fixed_point green)
  787. {
  788. png_debug(1, "in png_set_rgb_to_gray");
  789. if (png_ptr == NULL)
  790. return;
  791. switch(error_action)
  792. {
  793. case PNG_ERROR_ACTION_NONE:
  794. png_ptr->transformations |= PNG_RGB_TO_GRAY;
  795. break;
  796. case PNG_ERROR_ACTION_WARN:
  797. png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
  798. break;
  799. case PNG_ERROR_ACTION_ERROR:
  800. png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
  801. break;
  802. default:
  803. png_error(png_ptr, "invalid error action to rgb_to_gray");
  804. break;
  805. }
  806. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  807. #ifdef PNG_READ_EXPAND_SUPPORTED
  808. png_ptr->transformations |= PNG_EXPAND;
  809. #else
  810. {
  811. png_warning(png_ptr,
  812. "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
  813. png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
  814. }
  815. #endif
  816. {
  817. if (red >= 0 && green >= 0 && red + green <= PNG_FP_1)
  818. {
  819. png_uint_16 red_int, green_int;
  820. /* NOTE: this calculation does not round, but this behavior is retained
  821. * for consistency, the inaccuracy is very small. The code here always
  822. * overwrites the coefficients, regardless of whether they have been
  823. * defaulted or set already.
  824. */
  825. red_int = (png_uint_16)(((png_uint_32)red*32768)/100000);
  826. green_int = (png_uint_16)(((png_uint_32)green*32768)/100000);
  827. png_ptr->rgb_to_gray_red_coeff = red_int;
  828. png_ptr->rgb_to_gray_green_coeff = green_int;
  829. png_ptr->rgb_to_gray_coefficients_set = 1;
  830. }
  831. else
  832. {
  833. if (red >= 0 && green >= 0)
  834. png_warning(png_ptr,
  835. "ignoring out of range rgb_to_gray coefficients");
  836. /* Use the defaults, from the cHRM chunk if set, else the historical
  837. * values which are close to the sRGB/HDTV/ITU-Rec 709 values. See
  838. * png_do_rgb_to_gray for more discussion of the values. In this case
  839. * the coefficients are not marked as 'set' and are not overwritten if
  840. * something has already provided a default.
  841. */
  842. if (png_ptr->rgb_to_gray_red_coeff == 0 &&
  843. png_ptr->rgb_to_gray_green_coeff == 0)
  844. {
  845. png_ptr->rgb_to_gray_red_coeff = 6968;
  846. png_ptr->rgb_to_gray_green_coeff = 23434;
  847. /* png_ptr->rgb_to_gray_blue_coeff = 2366; */
  848. }
  849. }
  850. }
  851. }
  852. #ifdef PNG_FLOATING_POINT_SUPPORTED
  853. /* Convert a RGB image to a grayscale of the same width. This allows us,
  854. * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
  855. */
  856. void PNGAPI
  857. png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
  858. double green)
  859. {
  860. if (png_ptr == NULL)
  861. return;
  862. png_set_rgb_to_gray_fixed(png_ptr, error_action,
  863. png_fixed(png_ptr, red, "rgb to gray red coefficient"),
  864. png_fixed(png_ptr, green, "rgb to gray green coefficient"));
  865. }
  866. #endif /* FLOATING POINT */
  867. #endif
  868. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  869. defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  870. void PNGAPI
  871. png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
  872. read_user_transform_fn)
  873. {
  874. png_debug(1, "in png_set_read_user_transform_fn");
  875. if (png_ptr == NULL)
  876. return;
  877. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  878. png_ptr->transformations |= PNG_USER_TRANSFORM;
  879. png_ptr->read_user_transform_fn = read_user_transform_fn;
  880. #endif
  881. }
  882. #endif
  883. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  884. #ifdef PNG_READ_GAMMA_SUPPORTED
  885. /* In the case of gamma transformations only do transformations on images where
  886. * the [file] gamma and screen_gamma are not close reciprocals, otherwise it
  887. * slows things down slightly, and also needlessly introduces small errors.
  888. */
  889. static int /* PRIVATE */
  890. png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma)
  891. {
  892. /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
  893. * correction as a difference of the overall transform from 1.0
  894. *
  895. * We want to compare the threshold with s*f - 1, if we get
  896. * overflow here it is because of wacky gamma values so we
  897. * turn on processing anyway.
  898. */
  899. png_fixed_point gtest;
  900. return !png_muldiv(&gtest, screen_gamma, file_gamma, PNG_FP_1) ||
  901. png_gamma_significant(gtest);
  902. }
  903. #endif
  904. /* Initialize everything needed for the read. This includes modifying
  905. * the palette.
  906. */
  907. /*For the moment 'png_init_palette_transformations' and
  908. * 'png_init_rgb_transformations' only do some flag canceling optimizations.
  909. * The intent is that these two routines should have palette or rgb operations
  910. * extracted from 'png_init_read_transformations'.
  911. */
  912. static void /* PRIVATE */
  913. png_init_palette_transformations(png_structp png_ptr)
  914. {
  915. /* Called to handle the (input) palette case. In png_do_read_transformations
  916. * the first step is to expand the palette if requested, so this code must
  917. * take care to only make changes that are invariant with respect to the
  918. * palette expansion, or only do them if there is no expansion.
  919. *
  920. * STRIP_ALPHA has already been handled in the caller (by setting num_trans
  921. * to 0.)
  922. */
  923. int input_has_alpha = 0;
  924. int input_has_transparency = 0;
  925. if (png_ptr->num_trans > 0)
  926. {
  927. int i;
  928. /* Ignore if all the entries are opaque (unlikely!) */
  929. for (i=0; i<png_ptr->num_trans; ++i)
  930. if (png_ptr->trans_alpha[i] == 255)
  931. continue;
  932. else if (png_ptr->trans_alpha[i] == 0)
  933. input_has_transparency = 1;
  934. else
  935. input_has_alpha = 1;
  936. }
  937. /* If no alpha we can optimize. */
  938. if (!input_has_alpha)
  939. {
  940. /* Any alpha means background and associative alpha processing is
  941. * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
  942. * and ENCODE_ALPHA are irrelevant.
  943. */
  944. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  945. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  946. if (!input_has_transparency)
  947. png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
  948. }
  949. #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
  950. /* png_set_background handling - deals with the complexity of whether the
  951. * background color is in the file format or the screen format in the case
  952. * where an 'expand' will happen.
  953. */
  954. /* The following code cannot be entered in the alpha pre-multiplication case
  955. * because PNG_BACKGROUND_EXPAND is cancelled below.
  956. */
  957. if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  958. (png_ptr->transformations & PNG_EXPAND))
  959. {
  960. {
  961. png_ptr->background.red =
  962. png_ptr->palette[png_ptr->background.index].red;
  963. png_ptr->background.green =
  964. png_ptr->palette[png_ptr->background.index].green;
  965. png_ptr->background.blue =
  966. png_ptr->palette[png_ptr->background.index].blue;
  967. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  968. if (png_ptr->transformations & PNG_INVERT_ALPHA)
  969. {
  970. if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  971. {
  972. /* Invert the alpha channel (in tRNS) unless the pixels are
  973. * going to be expanded, in which case leave it for later
  974. */
  975. int i, istop = png_ptr->num_trans;
  976. for (i=0; i<istop; i++)
  977. png_ptr->trans_alpha[i] = (png_byte)(255 -
  978. png_ptr->trans_alpha[i]);
  979. }
  980. }
  981. #endif /* PNG_READ_INVERT_ALPHA_SUPPORTED */
  982. }
  983. } /* background expand and (therefore) no alpha association. */
  984. #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
  985. }
  986. static void /* PRIVATE */
  987. png_init_rgb_transformations(png_structp png_ptr)
  988. {
  989. /* Added to libpng-1.5.4: check the color type to determine whether there
  990. * is any alpha or transparency in the image and simply cancel the
  991. * background and alpha mode stuff if there isn't.
  992. */
  993. int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0;
  994. int input_has_transparency = png_ptr->num_trans > 0;
  995. /* If no alpha we can optimize. */
  996. if (!input_has_alpha)
  997. {
  998. /* Any alpha means background and associative alpha processing is
  999. * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
  1000. * and ENCODE_ALPHA are irrelevant.
  1001. */
  1002. # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  1003. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  1004. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  1005. # endif
  1006. if (!input_has_transparency)
  1007. png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
  1008. }
  1009. #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
  1010. /* png_set_background handling - deals with the complexity of whether the
  1011. * background color is in the file format or the screen format in the case
  1012. * where an 'expand' will happen.
  1013. */
  1014. /* The following code cannot be entered in the alpha pre-multiplication case
  1015. * because PNG_BACKGROUND_EXPAND is cancelled below.
  1016. */
  1017. if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  1018. (png_ptr->transformations & PNG_EXPAND) &&
  1019. !(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
  1020. /* i.e., GRAY or GRAY_ALPHA */
  1021. {
  1022. {
  1023. /* Expand background and tRNS chunks */
  1024. int gray = png_ptr->background.gray;
  1025. int trans_gray = png_ptr->trans_color.gray;
  1026. switch (png_ptr->bit_depth)
  1027. {
  1028. case 1:
  1029. gray *= 0xff;
  1030. trans_gray *= 0xff;
  1031. break;
  1032. case 2:
  1033. gray *= 0x55;
  1034. trans_gray *= 0x55;
  1035. break;
  1036. case 4:
  1037. gray *= 0x11;
  1038. trans_gray *= 0x11;
  1039. break;
  1040. default:
  1041. case 8:
  1042. /* Already 8 bits, fall through */
  1043. case 16:
  1044. /* Already a full 16 bits */
  1045. break;
  1046. }
  1047. png_ptr->background.red = png_ptr->background.green =
  1048. png_ptr->background.blue = (png_uint_16)gray;
  1049. if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
  1050. {
  1051. png_ptr->trans_color.red = png_ptr->trans_color.green =
  1052. png_ptr->trans_color.blue = (png_uint_16)trans_gray;
  1053. }
  1054. }
  1055. } /* background expand and (therefore) no alpha association. */
  1056. #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
  1057. }
  1058. void /* PRIVATE */
  1059. png_init_read_transformations(png_structp png_ptr)
  1060. {
  1061. png_debug(1, "in png_init_read_transformations");
  1062. /* This internal function is called from png_read_start_row in pngrutil.c
  1063. * and it is called before the 'rowbytes' calculation is done, so the code
  1064. * in here can change or update the transformations flags.
  1065. *
  1066. * First do updates that do not depend on the details of the PNG image data
  1067. * being processed.
  1068. */
  1069. #ifdef PNG_READ_GAMMA_SUPPORTED
  1070. /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds
  1071. * png_set_alpha_mode and this is another source for a default file gamma so
  1072. * the test needs to be performed later - here. In addition prior to 1.5.4
  1073. * the tests were repeated for the PALETTE color type here - this is no
  1074. * longer necessary (and doesn't seem to have been necessary before.)
  1075. */
  1076. {
  1077. /* The following temporary indicates if overall gamma correction is
  1078. * required.
  1079. */
  1080. int gamma_correction = 0;
  1081. if (png_ptr->gamma != 0) /* has been set */
  1082. {
  1083. if (png_ptr->screen_gamma != 0) /* screen set too */
  1084. gamma_correction = png_gamma_threshold(png_ptr->gamma,
  1085. png_ptr->screen_gamma);
  1086. else
  1087. /* Assume the output matches the input; a long time default behavior
  1088. * of libpng, although the standard has nothing to say about this.
  1089. */
  1090. png_ptr->screen_gamma = png_reciprocal(png_ptr->gamma);
  1091. }
  1092. else if (png_ptr->screen_gamma != 0)
  1093. /* The converse - assume the file matches the screen, note that this
  1094. * perhaps undesireable default can (from 1.5.4) be changed by calling
  1095. * png_set_alpha_mode (even if the alpha handling mode isn't required
  1096. * or isn't changed from the default.)
  1097. */
  1098. png_ptr->gamma = png_reciprocal(png_ptr->screen_gamma);
  1099. else /* neither are set */
  1100. /* Just in case the following prevents any processing - file and screen
  1101. * are both assumed to be linear and there is no way to introduce a
  1102. * third gamma value other than png_set_background with 'UNIQUE', and,
  1103. * prior to 1.5.4
  1104. */
  1105. png_ptr->screen_gamma = png_ptr->gamma = PNG_FP_1;
  1106. /* Now turn the gamma transformation on or off as appropriate. Notice
  1107. * that PNG_GAMMA just refers to the file->screen correction. Alpha
  1108. * composition may independently cause gamma correction because it needs
  1109. * linear data (e.g. if the file has a gAMA chunk but the screen gamma
  1110. * hasn't been specified.) In any case this flag may get turned off in
  1111. * the code immediately below if the transform can be handled outside the
  1112. * row loop.
  1113. */
  1114. if (gamma_correction)
  1115. png_ptr->transformations |= PNG_GAMMA;
  1116. else
  1117. png_ptr->transformations &= ~PNG_GAMMA;
  1118. }
  1119. #endif
  1120. /* Certain transformations have the effect of preventing other
  1121. * transformations that happen afterward in png_do_read_transformations,
  1122. * resolve the interdependencies here. From the code of
  1123. * png_do_read_transformations the order is:
  1124. *
  1125. * 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
  1126. * 2) PNG_STRIP_ALPHA (if no compose)
  1127. * 3) PNG_RGB_TO_GRAY
  1128. * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
  1129. * 5) PNG_COMPOSE
  1130. * 6) PNG_GAMMA
  1131. * 7) PNG_STRIP_ALPHA (if compose)
  1132. * 8) PNG_ENCODE_ALPHA
  1133. * 9) PNG_SCALE_16_TO_8
  1134. * 10) PNG_16_TO_8
  1135. * 11) PNG_QUANTIZE (converts to palette)
  1136. * 12) PNG_EXPAND_16
  1137. * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
  1138. * 14) PNG_INVERT_MONO
  1139. * 15) PNG_SHIFT
  1140. * 16) PNG_PACK
  1141. * 17) PNG_BGR
  1142. * 18) PNG_PACKSWAP
  1143. * 19) PNG_FILLER (includes PNG_ADD_ALPHA)
  1144. * 20) PNG_INVERT_ALPHA
  1145. * 21) PNG_SWAP_ALPHA
  1146. * 22) PNG_SWAP_BYTES
  1147. * 23) PNG_USER_TRANSFORM [must be last]
  1148. */
  1149. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  1150. if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
  1151. !(png_ptr->transformations & PNG_COMPOSE))
  1152. {
  1153. /* Stripping the alpha channel happens immediately after the 'expand'
  1154. * transformations, before all other transformation, so it cancels out
  1155. * the alpha handling. It has the side effect negating the effect of
  1156. * PNG_EXPAND_tRNS too:
  1157. */
  1158. png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA |
  1159. PNG_EXPAND_tRNS);
  1160. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  1161. /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen
  1162. * so transparency information would remain just so long as it wasn't
  1163. * expanded. This produces unexpected API changes if the set of things
  1164. * that do PNG_EXPAND_tRNS changes (perfectly possible given the
  1165. * documentation - which says ask for what you want, accept what you
  1166. * get.) This makes the behavior consistent from 1.5.4:
  1167. */
  1168. png_ptr->num_trans = 0;
  1169. }
  1170. #endif /* STRIP_ALPHA supported, no COMPOSE */
  1171. #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  1172. /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
  1173. * settings will have no effect.
  1174. */
  1175. if (!png_gamma_significant(png_ptr->screen_gamma))
  1176. {
  1177. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  1178. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  1179. }
  1180. #endif
  1181. #if defined(PNG_READ_EXPAND_SUPPORTED) && \
  1182. defined(PNG_READ_BACKGROUND_SUPPORTED) && \
  1183. defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  1184. /* Detect gray background and attempt to enable optimization for
  1185. * gray --> RGB case.
  1186. *
  1187. * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
  1188. * RGB_ALPHA (in which case need_expand is superfluous anyway), the
  1189. * background color might actually be gray yet not be flagged as such.
  1190. * This is not a problem for the current code, which uses
  1191. * PNG_BACKGROUND_IS_GRAY only to decide when to do the
  1192. * png_do_gray_to_rgb() transformation.
  1193. *
  1194. * TODO: this code needs to be revised to avoid the complexity and
  1195. * interdependencies. The color type of the background should be recorded in
  1196. * png_set_background, along with the bit depth, then the code has a record
  1197. * of exactly what color space the background is currently in.
  1198. */
  1199. if (png_ptr->transformations & PNG_BACKGROUND_EXPAND)
  1200. {
  1201. /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
  1202. * the file was grayscale the background value is gray.
  1203. */
  1204. if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
  1205. png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
  1206. }
  1207. else if (png_ptr->transformations & PNG_COMPOSE)
  1208. {
  1209. /* PNG_COMPOSE: png_set_background was called with need_expand false,
  1210. * so the color is in the color space of the output or png_set_alpha_mode
  1211. * was called and the color is black. Ignore RGB_TO_GRAY because that
  1212. * happens before GRAY_TO_RGB.
  1213. */
  1214. if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  1215. {
  1216. if (png_ptr->background.red == png_ptr->background.green &&
  1217. png_ptr->background.red == png_ptr->background.blue)
  1218. {
  1219. png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
  1220. png_ptr->background.gray = png_ptr->background.red;
  1221. }
  1222. }
  1223. }
  1224. #endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED (etc) */
  1225. /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
  1226. * can be performed directly on the palette, and some (such as rgb to gray)
  1227. * can be optimized inside the palette. This is particularly true of the
  1228. * composite (background and alpha) stuff, which can be pretty much all done
  1229. * in the palette even if the result is expanded to RGB or gray afterward.
  1230. *
  1231. * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
  1232. * earlier and the palette stuff is actually handled on the first row. This
  1233. * leads to the reported bug that the palette returned by png_get_PLTE is not
  1234. * updated.
  1235. */
  1236. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1237. png_init_palette_transformations(png_ptr);
  1238. else
  1239. png_init_rgb_transformations(png_ptr);
  1240. #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
  1241. defined(PNG_READ_EXPAND_16_SUPPORTED)
  1242. if ((png_ptr->transformations & PNG_EXPAND_16) &&
  1243. (png_ptr->transformations & PNG_COMPOSE) &&
  1244. !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  1245. png_ptr->bit_depth != 16)
  1246. {
  1247. /* TODO: fix this. Because the expand_16 operation is after the compose
  1248. * handling the background color must be 8, not 16, bits deep, but the
  1249. * application will supply a 16-bit value so reduce it here.
  1250. *
  1251. * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
  1252. * present, so that case is ok (until do_expand_16 is moved.)
  1253. *
  1254. * NOTE: this discards the low 16 bits of the user supplied background
  1255. * color, but until expand_16 works properly there is no choice!
  1256. */
  1257. # define CHOP(x) (x)=((png_uint_16)(((png_uint_32)(x)*255+32895) >> 16))
  1258. CHOP(png_ptr->background.red);
  1259. CHOP(png_ptr->background.green);
  1260. CHOP(png_ptr->background.blue);
  1261. CHOP(png_ptr->background.gray);
  1262. # undef CHOP
  1263. }
  1264. #endif /* PNG_READ_BACKGROUND_SUPPORTED && PNG_READ_EXPAND_16_SUPPORTED */
  1265. #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
  1266. (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \
  1267. defined(PNG_READ_STRIP_16_TO_8_SUPPORTED))
  1268. if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) &&
  1269. (png_ptr->transformations & PNG_COMPOSE) &&
  1270. !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
  1271. png_ptr->bit_depth == 16)
  1272. {
  1273. /* On the other hand, if a 16-bit file is to be reduced to 8-bits per
  1274. * component this will also happen after PNG_COMPOSE and so the background
  1275. * color must be pre-expanded here.
  1276. *
  1277. * TODO: fix this too.
  1278. */
  1279. png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257);
  1280. png_ptr->background.green =
  1281. (png_uint_16)(png_ptr->background.green * 257);
  1282. png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257);
  1283. png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257);
  1284. }
  1285. #endif
  1286. /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the
  1287. * background support (see the comments in scripts/pnglibconf.dfa), this
  1288. * allows pre-multiplication of the alpha channel to be implemented as
  1289. * compositing on black. This is probably sub-optimal and has been done in
  1290. * 1.5.4 betas simply to enable external critique and testing (i.e. to
  1291. * implement the new API quickly, without lots of internal changes.)
  1292. */
  1293. #ifdef PNG_READ_GAMMA_SUPPORTED
  1294. # ifdef PNG_READ_BACKGROUND_SUPPORTED
  1295. /* Includes ALPHA_MODE */
  1296. png_ptr->background_1 = png_ptr->background;
  1297. # endif
  1298. /* This needs to change - in the palette image case a whole set of tables are
  1299. * built when it would be quicker to just calculate the correct value for
  1300. * each palette entry directly. Also, the test is too tricky - why check
  1301. * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that
  1302. * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the
  1303. * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
  1304. * the gamma tables will not be built even if composition is required on a
  1305. * gamma encoded value.
  1306. *
  1307. * In 1.5.4 this is addressed below by an additional check on the individual
  1308. * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
  1309. * tables.
  1310. */
  1311. if ((png_ptr->transformations & PNG_GAMMA)
  1312. || ((png_ptr->transformations & PNG_RGB_TO_GRAY)
  1313. && (png_gamma_significant(png_ptr->gamma) ||
  1314. png_gamma_significant(png_ptr->screen_gamma)))
  1315. || ((png_ptr->transformations & PNG_COMPOSE)
  1316. && (png_gamma_significant(png_ptr->gamma)
  1317. || png_gamma_significant(png_ptr->screen_gamma)
  1318. # ifdef PNG_READ_BACKGROUND_SUPPORTED
  1319. || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE
  1320. && png_gamma_significant(png_ptr->background_gamma))
  1321. # endif
  1322. )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA)
  1323. && png_gamma_significant(png_ptr->screen_gamma))
  1324. )
  1325. {
  1326. png_build_gamma_table(png_ptr, png_ptr->bit_depth);
  1327. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1328. if (png_ptr->transformations & PNG_COMPOSE)
  1329. {
  1330. /* Issue a warning about this combination: because RGB_TO_GRAY is
  1331. * optimized to do the gamma transform if present yet do_background has
  1332. * to do the same thing if both options are set a
  1333. * double-gamma-correction happens. This is true in all versions of
  1334. * libpng to date.
  1335. */
  1336. if (png_ptr->transformations & PNG_RGB_TO_GRAY)
  1337. png_warning(png_ptr,
  1338. "libpng does not support gamma+background+rgb_to_gray");
  1339. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1340. {
  1341. /* We don't get to here unless there is a tRNS chunk with non-opaque
  1342. * entries - see the checking code at the start of this function.
  1343. */
  1344. png_color back, back_1;
  1345. png_colorp palette = png_ptr->palette;
  1346. int num_palette = png_ptr->num_palette;
  1347. int i;
  1348. if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
  1349. {
  1350. back.red = png_ptr->gamma_table[png_ptr->background.red];
  1351. back.green = png_ptr->gamma_table[png_ptr->background.green];
  1352. back.blue = png_ptr->gamma_table[png_ptr->background.blue];
  1353. back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
  1354. back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
  1355. back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
  1356. }
  1357. else
  1358. {
  1359. png_fixed_point g, gs;
  1360. switch (png_ptr->background_gamma_type)
  1361. {
  1362. case PNG_BACKGROUND_GAMMA_SCREEN:
  1363. g = (png_ptr->screen_gamma);
  1364. gs = PNG_FP_1;
  1365. break;
  1366. case PNG_BACKGROUND_GAMMA_FILE:
  1367. g = png_reciprocal(png_ptr->gamma);
  1368. gs = png_reciprocal2(png_ptr->gamma,
  1369. png_ptr->screen_gamma);
  1370. break;
  1371. case PNG_BACKGROUND_GAMMA_UNIQUE:
  1372. g = png_reciprocal(png_ptr->background_gamma);
  1373. gs = png_reciprocal2(png_ptr->background_gamma,
  1374. png_ptr->screen_gamma);
  1375. break;
  1376. default:
  1377. g = PNG_FP_1; /* back_1 */
  1378. gs = PNG_FP_1; /* back */
  1379. break;
  1380. }
  1381. if (png_gamma_significant(gs))
  1382. {
  1383. back.red = png_gamma_8bit_correct(png_ptr->background.red,
  1384. gs);
  1385. back.green = png_gamma_8bit_correct(png_ptr->background.green,
  1386. gs);
  1387. back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
  1388. gs);
  1389. }
  1390. else
  1391. {
  1392. back.red = (png_byte)png_ptr->background.red;
  1393. back.green = (png_byte)png_ptr->background.green;
  1394. back.blue = (png_byte)png_ptr->background.blue;
  1395. }
  1396. if (png_gamma_significant(g))
  1397. {
  1398. back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
  1399. g);
  1400. back_1.green = png_gamma_8bit_correct(
  1401. png_ptr->background.green, g);
  1402. back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
  1403. g);
  1404. }
  1405. else
  1406. {
  1407. back_1.red = (png_byte)png_ptr->background.red;
  1408. back_1.green = (png_byte)png_ptr->background.green;
  1409. back_1.blue = (png_byte)png_ptr->background.blue;
  1410. }
  1411. }
  1412. for (i = 0; i < num_palette; i++)
  1413. {
  1414. if (i < (int)png_ptr->num_trans &&
  1415. png_ptr->trans_alpha[i] != 0xff)
  1416. {
  1417. if (png_ptr->trans_alpha[i] == 0)
  1418. {
  1419. palette[i] = back;
  1420. }
  1421. else /* if (png_ptr->trans_alpha[i] != 0xff) */
  1422. {
  1423. png_byte v, w;
  1424. v = png_ptr->gamma_to_1[palette[i].red];
  1425. png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
  1426. palette[i].red = png_ptr->gamma_from_1[w];
  1427. v = png_ptr->gamma_to_1[palette[i].green];
  1428. png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
  1429. palette[i].green = png_ptr->gamma_from_1[w];
  1430. v = png_ptr->gamma_to_1[palette[i].blue];
  1431. png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
  1432. palette[i].blue = png_ptr->gamma_from_1[w];
  1433. }
  1434. }
  1435. else
  1436. {
  1437. palette[i].red = png_ptr->gamma_table[palette[i].red];
  1438. palette[i].green = png_ptr->gamma_table[palette[i].green];
  1439. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  1440. }
  1441. }
  1442. /* Prevent the transformations being done again.
  1443. *
  1444. * NOTE: this is highly dubious; it removes the transformations in
  1445. * place. This seems inconsistent with the general treatment of the
  1446. * transformations elsewhere.
  1447. */
  1448. png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA);
  1449. } /* color_type == PNG_COLOR_TYPE_PALETTE */
  1450. /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
  1451. else /* color_type != PNG_COLOR_TYPE_PALETTE */
  1452. {
  1453. int gs_sig, g_sig;
  1454. png_fixed_point g = PNG_FP_1; /* Correction to linear */
  1455. png_fixed_point gs = PNG_FP_1; /* Correction to screen */
  1456. switch (png_ptr->background_gamma_type)
  1457. {
  1458. case PNG_BACKGROUND_GAMMA_SCREEN:
  1459. g = png_ptr->screen_gamma;
  1460. /* gs = PNG_FP_1; */
  1461. break;
  1462. case PNG_BACKGROUND_GAMMA_FILE:
  1463. g = png_reciprocal(png_ptr->gamma);
  1464. gs = png_reciprocal2(png_ptr->gamma, png_ptr->screen_gamma);
  1465. break;
  1466. case PNG_BACKGROUND_GAMMA_UNIQUE:
  1467. g = png_reciprocal(png_ptr->background_gamma);
  1468. gs = png_reciprocal2(png_ptr->background_gamma,
  1469. png_ptr->screen_gamma);
  1470. break;
  1471. default:
  1472. png_error(png_ptr, "invalid background gamma type");
  1473. }
  1474. g_sig = png_gamma_significant(g);
  1475. gs_sig = png_gamma_significant(gs);
  1476. if (g_sig)
  1477. png_ptr->background_1.gray = png_gamma_correct(png_ptr,
  1478. png_ptr->background.gray, g);
  1479. if (gs_sig)
  1480. png_ptr->background.gray = png_gamma_correct(png_ptr,
  1481. png_ptr->background.gray, gs);
  1482. if ((png_ptr->background.red != png_ptr->background.green) ||
  1483. (png_ptr->background.red != png_ptr->background.blue) ||
  1484. (png_ptr->background.red != png_ptr->background.gray))
  1485. {
  1486. /* RGB or RGBA with color background */
  1487. if (g_sig)
  1488. {
  1489. png_ptr->background_1.red = png_gamma_correct(png_ptr,
  1490. png_ptr->background.red, g);
  1491. png_ptr->background_1.green = png_gamma_correct(png_ptr,
  1492. png_ptr->background.green, g);
  1493. png_ptr->background_1.blue = png_gamma_correct(png_ptr,
  1494. png_ptr->background.blue, g);
  1495. }
  1496. if (gs_sig)
  1497. {
  1498. png_ptr->background.red = png_gamma_correct(png_ptr,
  1499. png_ptr->background.red, gs);
  1500. png_ptr->background.green = png_gamma_correct(png_ptr,
  1501. png_ptr->background.green, gs);
  1502. png_ptr->background.blue = png_gamma_correct(png_ptr,
  1503. png_ptr->background.blue, gs);
  1504. }
  1505. }
  1506. else
  1507. {
  1508. /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
  1509. png_ptr->background_1.red = png_ptr->background_1.green
  1510. = png_ptr->background_1.blue = png_ptr->background_1.gray;
  1511. png_ptr->background.red = png_ptr->background.green
  1512. = png_ptr->background.blue = png_ptr->background.gray;
  1513. }
  1514. /* The background is now in screen gamma: */
  1515. png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN;
  1516. } /* color_type != PNG_COLOR_TYPE_PALETTE */
  1517. }/* png_ptr->transformations & PNG_BACKGROUND */
  1518. else
  1519. /* Transformation does not include PNG_BACKGROUND */
  1520. #endif /* PNG_READ_BACKGROUND_SUPPORTED */
  1521. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE
  1522. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  1523. /* RGB_TO_GRAY needs to have non-gamma-corrected values! */
  1524. && ((png_ptr->transformations & PNG_EXPAND) == 0 ||
  1525. (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
  1526. #endif
  1527. )
  1528. {
  1529. png_colorp palette = png_ptr->palette;
  1530. int num_palette = png_ptr->num_palette;
  1531. int i;
  1532. /*NOTE: there are other transformations that should probably be in here
  1533. * too.
  1534. */
  1535. for (i = 0; i < num_palette; i++)
  1536. {
  1537. palette[i].red = png_ptr->gamma_table[palette[i].red];
  1538. palette[i].green = png_ptr->gamma_table[palette[i].green];
  1539. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  1540. }
  1541. /* Done the gamma correction. */
  1542. png_ptr->transformations &= ~PNG_GAMMA;
  1543. } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
  1544. }
  1545. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1546. else
  1547. #endif
  1548. #endif /* PNG_READ_GAMMA_SUPPORTED */
  1549. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1550. /* No GAMMA transformation (see the hanging else 4 lines above) */
  1551. if ((png_ptr->transformations & PNG_COMPOSE) &&
  1552. (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
  1553. {
  1554. int i;
  1555. int istop = (int)png_ptr->num_trans;
  1556. png_color back;
  1557. png_colorp palette = png_ptr->palette;
  1558. back.red = (png_byte)png_ptr->background.red;
  1559. back.green = (png_byte)png_ptr->background.green;
  1560. back.blue = (png_byte)png_ptr->background.blue;
  1561. for (i = 0; i < istop; i++)
  1562. {
  1563. if (png_ptr->trans_alpha[i] == 0)
  1564. {
  1565. palette[i] = back;
  1566. }
  1567. else if (png_ptr->trans_alpha[i] != 0xff)
  1568. {
  1569. /* The png_composite() macro is defined in png.h */
  1570. png_composite(palette[i].red, palette[i].red,
  1571. png_ptr->trans_alpha[i], back.red);
  1572. png_composite(palette[i].green, palette[i].green,
  1573. png_ptr->trans_alpha[i], back.green);
  1574. png_composite(palette[i].blue, palette[i].blue,
  1575. png_ptr->trans_alpha[i], back.blue);
  1576. }
  1577. }
  1578. png_ptr->transformations &= ~PNG_COMPOSE;
  1579. }
  1580. #endif /* PNG_READ_BACKGROUND_SUPPORTED */
  1581. #ifdef PNG_READ_SHIFT_SUPPORTED
  1582. if ((png_ptr->transformations & PNG_SHIFT) &&
  1583. (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
  1584. {
  1585. int i;
  1586. int istop = png_ptr->num_palette;
  1587. int shift = 8 - png_ptr->sig_bit.red;
  1588. /* significant bits can be in the range 1 to 7 for a meaninful result, if
  1589. * the number of significant bits is 0 then no shift is done (this is an
  1590. * error condition which is silently ignored.)
  1591. */
  1592. if (shift > 0 && shift < 8) for (i=0; i<istop; ++i)
  1593. {
  1594. int component = png_ptr->palette[i].red;
  1595. component >>= shift;
  1596. png_ptr->palette[i].red = (png_byte)component;
  1597. }
  1598. shift = 8 - png_ptr->sig_bit.green;
  1599. if (shift > 0 && shift < 8) for (i=0; i<istop; ++i)
  1600. {
  1601. int component = png_ptr->palette[i].green;
  1602. component >>= shift;
  1603. png_ptr->palette[i].green = (png_byte)component;
  1604. }
  1605. shift = 8 - png_ptr->sig_bit.blue;
  1606. if (shift > 0 && shift < 8) for (i=0; i<istop; ++i)
  1607. {
  1608. int component = png_ptr->palette[i].blue;
  1609. component >>= shift;
  1610. png_ptr->palette[i].blue = (png_byte)component;
  1611. }
  1612. }
  1613. #endif /* PNG_READ_SHIFT_SUPPORTED */
  1614. }
  1615. /* Modify the info structure to reflect the transformations. The
  1616. * info should be updated so a PNG file could be written with it,
  1617. * assuming the transformations result in valid PNG data.
  1618. */
  1619. void /* PRIVATE */
  1620. png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
  1621. {
  1622. png_debug(1, "in png_read_transform_info");
  1623. #ifdef PNG_READ_EXPAND_SUPPORTED
  1624. if (png_ptr->transformations & PNG_EXPAND)
  1625. {
  1626. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1627. {
  1628. /* This check must match what actually happens in
  1629. * png_do_expand_palette; if it ever checks the tRNS chunk to see if
  1630. * it is all opaque we must do the same (at present it does not.)
  1631. */
  1632. if (png_ptr->num_trans > 0)
  1633. info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  1634. else
  1635. info_ptr->color_type = PNG_COLOR_TYPE_RGB;
  1636. info_ptr->bit_depth = 8;
  1637. info_ptr->num_trans = 0;
  1638. }
  1639. else
  1640. {
  1641. if (png_ptr->num_trans)
  1642. {
  1643. if (png_ptr->transformations & PNG_EXPAND_tRNS)
  1644. info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
  1645. }
  1646. if (info_ptr->bit_depth < 8)
  1647. info_ptr->bit_depth = 8;
  1648. info_ptr->num_trans = 0;
  1649. }
  1650. }
  1651. #endif
  1652. #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
  1653. defined(PNG_READ_ALPHA_MODE_SUPPORTED)
  1654. /* The following is almost certainly wrong unless the background value is in
  1655. * the screen space!
  1656. */
  1657. if (png_ptr->transformations & PNG_COMPOSE)
  1658. info_ptr->background = png_ptr->background;
  1659. #endif
  1660. #ifdef PNG_READ_GAMMA_SUPPORTED
  1661. /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
  1662. * however it seems that the code in png_init_read_transformations, which has
  1663. * been called before this from png_read_update_info->png_read_start_row
  1664. * sometimes does the gamma transform and cancels the flag.
  1665. */
  1666. info_ptr->gamma = png_ptr->gamma;
  1667. #endif
  1668. if (info_ptr->bit_depth == 16)
  1669. {
  1670. # ifdef PNG_READ_16BIT_SUPPORTED
  1671. # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  1672. if (png_ptr->transformations & PNG_SCALE_16_TO_8)
  1673. info_ptr->bit_depth = 8;
  1674. # endif
  1675. # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  1676. if (png_ptr->transformations & PNG_16_TO_8)
  1677. info_ptr->bit_depth = 8;
  1678. # endif
  1679. # else
  1680. /* No 16 bit support: force chopping 16-bit input down to 8, in this case
  1681. * the app program can chose if both APIs are available by setting the
  1682. * correct scaling to use.
  1683. */
  1684. # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  1685. /* For compatibility with previous versions use the strip method by
  1686. * default. This code works because if PNG_SCALE_16_TO_8 is already
  1687. * set the code below will do that in preference to the chop.
  1688. */
  1689. png_ptr->transformations |= PNG_16_TO_8;
  1690. info_ptr->bit_depth = 8;
  1691. # else
  1692. # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  1693. png_ptr->transformations |= PNG_SCALE_16_TO_8;
  1694. info_ptr->bit_depth = 8;
  1695. # else
  1696. CONFIGURATION ERROR: you must enable at least one 16 to 8 method
  1697. # endif
  1698. # endif
  1699. #endif /* !READ_16BIT_SUPPORTED */
  1700. }
  1701. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1702. if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  1703. info_ptr->color_type = (png_byte)(info_ptr->color_type |
  1704. PNG_COLOR_MASK_COLOR);
  1705. #endif
  1706. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  1707. if (png_ptr->transformations & PNG_RGB_TO_GRAY)
  1708. info_ptr->color_type = (png_byte)(info_ptr->color_type &
  1709. ~PNG_COLOR_MASK_COLOR);
  1710. #endif
  1711. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  1712. if (png_ptr->transformations & PNG_QUANTIZE)
  1713. {
  1714. if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
  1715. (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
  1716. png_ptr->palette_lookup && info_ptr->bit_depth == 8)
  1717. {
  1718. info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
  1719. }
  1720. }
  1721. #endif
  1722. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  1723. if (png_ptr->transformations & PNG_EXPAND_16 && info_ptr->bit_depth == 8 &&
  1724. info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  1725. {
  1726. info_ptr->bit_depth = 16;
  1727. }
  1728. #endif
  1729. #ifdef PNG_READ_PACK_SUPPORTED
  1730. if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
  1731. info_ptr->bit_depth = 8;
  1732. #endif
  1733. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1734. info_ptr->channels = 1;
  1735. else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  1736. info_ptr->channels = 3;
  1737. else
  1738. info_ptr->channels = 1;
  1739. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  1740. if (png_ptr->transformations & PNG_STRIP_ALPHA)
  1741. {
  1742. info_ptr->color_type = (png_byte)(info_ptr->color_type &
  1743. ~PNG_COLOR_MASK_ALPHA);
  1744. info_ptr->num_trans = 0;
  1745. }
  1746. #endif
  1747. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  1748. info_ptr->channels++;
  1749. #ifdef PNG_READ_FILLER_SUPPORTED
  1750. /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
  1751. if ((png_ptr->transformations & PNG_FILLER) &&
  1752. ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
  1753. (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
  1754. {
  1755. info_ptr->channels++;
  1756. /* If adding a true alpha channel not just filler */
  1757. if (png_ptr->transformations & PNG_ADD_ALPHA)
  1758. info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
  1759. }
  1760. #endif
  1761. #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
  1762. defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1763. if (png_ptr->transformations & PNG_USER_TRANSFORM)
  1764. {
  1765. if (info_ptr->bit_depth < png_ptr->user_transform_depth)
  1766. info_ptr->bit_depth = png_ptr->user_transform_depth;
  1767. if (info_ptr->channels < png_ptr->user_transform_channels)
  1768. info_ptr->channels = png_ptr->user_transform_channels;
  1769. }
  1770. #endif
  1771. info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
  1772. info_ptr->bit_depth);
  1773. info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
  1774. /* Adding in 1.5.4: cache the above value in png_struct so that we can later
  1775. * check in png_rowbytes that the user buffer won't get overwritten. Note
  1776. * that the field is not always set - if png_read_update_info isn't called
  1777. * the application has to either not do any transforms or get the calculation
  1778. * right itself.
  1779. */
  1780. png_ptr->info_rowbytes = info_ptr->rowbytes;
  1781. #ifndef PNG_READ_EXPAND_SUPPORTED
  1782. if (png_ptr)
  1783. return;
  1784. #endif
  1785. }
  1786. /* Transform the row. The order of transformations is significant,
  1787. * and is very touchy. If you add a transformation, take care to
  1788. * decide how it fits in with the other transformations here.
  1789. */
  1790. void /* PRIVATE */
  1791. png_do_read_transformations(png_structp png_ptr, png_row_infop row_info)
  1792. {
  1793. png_debug(1, "in png_do_read_transformations");
  1794. if (png_ptr->row_buf == NULL)
  1795. {
  1796. /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
  1797. * error is incredibly rare and incredibly easy to debug without this
  1798. * information.
  1799. */
  1800. png_error(png_ptr, "NULL row buffer");
  1801. }
  1802. /* The following is debugging; prior to 1.5.4 the code was never compiled in;
  1803. * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
  1804. * PNG_WARN_UNINITIALIZED_ROW removed. In 1.5 the new flag is set only for
  1805. * selected new APIs to ensure that there is no API change.
  1806. */
  1807. if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
  1808. !(png_ptr->flags & PNG_FLAG_ROW_INIT))
  1809. {
  1810. /* Application has failed to call either png_read_start_image() or
  1811. * png_read_update_info() after setting transforms that expand pixels.
  1812. * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
  1813. */
  1814. png_error(png_ptr, "Uninitialized row");
  1815. }
  1816. #ifdef PNG_READ_EXPAND_SUPPORTED
  1817. if (png_ptr->transformations & PNG_EXPAND)
  1818. {
  1819. if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
  1820. {
  1821. png_do_expand_palette(row_info, png_ptr->row_buf + 1,
  1822. png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
  1823. }
  1824. else
  1825. {
  1826. if (png_ptr->num_trans &&
  1827. (png_ptr->transformations & PNG_EXPAND_tRNS))
  1828. png_do_expand(row_info, png_ptr->row_buf + 1,
  1829. &(png_ptr->trans_color));
  1830. else
  1831. png_do_expand(row_info, png_ptr->row_buf + 1,
  1832. NULL);
  1833. }
  1834. }
  1835. #endif
  1836. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  1837. if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
  1838. !(png_ptr->transformations & PNG_COMPOSE) &&
  1839. (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  1840. row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
  1841. png_do_strip_channel(row_info, png_ptr->row_buf + 1,
  1842. 0 /* at_start == false, because SWAP_ALPHA happens later */);
  1843. #endif
  1844. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  1845. if (png_ptr->transformations & PNG_RGB_TO_GRAY)
  1846. {
  1847. int rgb_error =
  1848. png_do_rgb_to_gray(png_ptr, row_info,
  1849. png_ptr->row_buf + 1);
  1850. if (rgb_error)
  1851. {
  1852. png_ptr->rgb_to_gray_status=1;
  1853. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
  1854. PNG_RGB_TO_GRAY_WARN)
  1855. png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
  1856. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
  1857. PNG_RGB_TO_GRAY_ERR)
  1858. png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
  1859. }
  1860. }
  1861. #endif
  1862. /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
  1863. *
  1864. * In most cases, the "simple transparency" should be done prior to doing
  1865. * gray-to-RGB, or you will have to test 3x as many bytes to check if a
  1866. * pixel is transparent. You would also need to make sure that the
  1867. * transparency information is upgraded to RGB.
  1868. *
  1869. * To summarize, the current flow is:
  1870. * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
  1871. * with background "in place" if transparent,
  1872. * convert to RGB if necessary
  1873. * - Gray + alpha -> composite with gray background and remove alpha bytes,
  1874. * convert to RGB if necessary
  1875. *
  1876. * To support RGB backgrounds for gray images we need:
  1877. * - Gray + simple transparency -> convert to RGB + simple transparency,
  1878. * compare 3 or 6 bytes and composite with
  1879. * background "in place" if transparent
  1880. * (3x compare/pixel compared to doing
  1881. * composite with gray bkgrnd)
  1882. * - Gray + alpha -> convert to RGB + alpha, composite with background and
  1883. * remove alpha bytes (3x float
  1884. * operations/pixel compared with composite
  1885. * on gray background)
  1886. *
  1887. * Greg's change will do this. The reason it wasn't done before is for
  1888. * performance, as this increases the per-pixel operations. If we would check
  1889. * in advance if the background was gray or RGB, and position the gray-to-RGB
  1890. * transform appropriately, then it would save a lot of work/time.
  1891. */
  1892. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1893. /* If gray -> RGB, do so now only if background is non-gray; else do later
  1894. * for performance reasons
  1895. */
  1896. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
  1897. !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
  1898. png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
  1899. #endif
  1900. #if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
  1901. (defined PNG_READ_ALPHA_MODE_SUPPORTED)
  1902. if (png_ptr->transformations & PNG_COMPOSE)
  1903. png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
  1904. #endif
  1905. #ifdef PNG_READ_GAMMA_SUPPORTED
  1906. if ((png_ptr->transformations & PNG_GAMMA) &&
  1907. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  1908. /* Because RGB_TO_GRAY does the gamma transform. */
  1909. !(png_ptr->transformations & PNG_RGB_TO_GRAY) &&
  1910. #endif
  1911. #if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
  1912. (defined PNG_READ_ALPHA_MODE_SUPPORTED)
  1913. /* Because PNG_COMPOSE does the gamma transform if there is something to
  1914. * do (if there is an alpha channel or transparency.)
  1915. */
  1916. !((png_ptr->transformations & PNG_COMPOSE) &&
  1917. ((png_ptr->num_trans != 0) ||
  1918. (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
  1919. #endif
  1920. /* Because png_init_read_transformations transforms the palette, unless
  1921. * RGB_TO_GRAY will do the transform.
  1922. */
  1923. (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
  1924. png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
  1925. #endif
  1926. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  1927. if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
  1928. (png_ptr->transformations & PNG_COMPOSE) &&
  1929. (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  1930. row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
  1931. png_do_strip_channel(row_info, png_ptr->row_buf + 1,
  1932. 0 /* at_start == false, because SWAP_ALPHA happens later */);
  1933. #endif
  1934. #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  1935. if ((png_ptr->transformations & PNG_ENCODE_ALPHA) &&
  1936. (row_info->color_type & PNG_COLOR_MASK_ALPHA))
  1937. png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr);
  1938. #endif
  1939. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  1940. if (png_ptr->transformations & PNG_SCALE_16_TO_8)
  1941. png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
  1942. #endif
  1943. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  1944. /* There is no harm in doing both of these because only one has any effect,
  1945. * by putting the 'scale' option first if the app asks for scale (either by
  1946. * calling the API or in a TRANSFORM flag) this is what happens.
  1947. */
  1948. if (png_ptr->transformations & PNG_16_TO_8)
  1949. png_do_chop(row_info, png_ptr->row_buf + 1);
  1950. #endif
  1951. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  1952. if (png_ptr->transformations & PNG_QUANTIZE)
  1953. {
  1954. png_do_quantize(row_info, png_ptr->row_buf + 1,
  1955. png_ptr->palette_lookup, png_ptr->quantize_index);
  1956. if (row_info->rowbytes == 0)
  1957. png_error(png_ptr, "png_do_quantize returned rowbytes=0");
  1958. }
  1959. #endif /* PNG_READ_QUANTIZE_SUPPORTED */
  1960. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  1961. /* Do the expansion now, after all the arithmetic has been done. Notice
  1962. * that previous transformations can handle the PNG_EXPAND_16 flag if this
  1963. * is efficient (particularly true in the case of gamma correction, where
  1964. * better accuracy results faster!)
  1965. */
  1966. if (png_ptr->transformations & PNG_EXPAND_16)
  1967. png_do_expand_16(row_info, png_ptr->row_buf + 1);
  1968. #endif
  1969. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1970. /*NOTE: moved here in 1.5.4 (from much later in this list.) */
  1971. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
  1972. (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
  1973. png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
  1974. #endif
  1975. #ifdef PNG_READ_INVERT_SUPPORTED
  1976. if (png_ptr->transformations & PNG_INVERT_MONO)
  1977. png_do_invert(row_info, png_ptr->row_buf + 1);
  1978. #endif
  1979. #ifdef PNG_READ_SHIFT_SUPPORTED
  1980. if (png_ptr->transformations & PNG_SHIFT)
  1981. png_do_unshift(row_info, png_ptr->row_buf + 1,
  1982. &(png_ptr->shift));
  1983. #endif
  1984. #ifdef PNG_READ_PACK_SUPPORTED
  1985. if (png_ptr->transformations & PNG_PACK)
  1986. png_do_unpack(row_info, png_ptr->row_buf + 1);
  1987. #endif
  1988. #ifdef PNG_READ_BGR_SUPPORTED
  1989. if (png_ptr->transformations & PNG_BGR)
  1990. png_do_bgr(row_info, png_ptr->row_buf + 1);
  1991. #endif
  1992. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  1993. if (png_ptr->transformations & PNG_PACKSWAP)
  1994. png_do_packswap(row_info, png_ptr->row_buf + 1);
  1995. #endif
  1996. #ifdef PNG_READ_FILLER_SUPPORTED
  1997. if (png_ptr->transformations & PNG_FILLER)
  1998. png_do_read_filler(row_info, png_ptr->row_buf + 1,
  1999. (png_uint_32)png_ptr->filler, png_ptr->flags);
  2000. #endif
  2001. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  2002. if (png_ptr->transformations & PNG_INVERT_ALPHA)
  2003. png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
  2004. #endif
  2005. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  2006. if (png_ptr->transformations & PNG_SWAP_ALPHA)
  2007. png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
  2008. #endif
  2009. #ifdef PNG_READ_16BIT_SUPPORTED
  2010. #ifdef PNG_READ_SWAP_SUPPORTED
  2011. if (png_ptr->transformations & PNG_SWAP_BYTES)
  2012. png_do_swap(row_info, png_ptr->row_buf + 1);
  2013. #endif
  2014. #endif
  2015. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  2016. if (png_ptr->transformations & PNG_USER_TRANSFORM)
  2017. {
  2018. if (png_ptr->read_user_transform_fn != NULL)
  2019. (*(png_ptr->read_user_transform_fn)) /* User read transform function */
  2020. (png_ptr, /* png_ptr */
  2021. row_info, /* row_info: */
  2022. /* png_uint_32 width; width of row */
  2023. /* png_size_t rowbytes; number of bytes in row */
  2024. /* png_byte color_type; color type of pixels */
  2025. /* png_byte bit_depth; bit depth of samples */
  2026. /* png_byte channels; number of channels (1-4) */
  2027. /* png_byte pixel_depth; bits per pixel (depth*channels) */
  2028. png_ptr->row_buf + 1); /* start of pixel data for row */
  2029. #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
  2030. if (png_ptr->user_transform_depth)
  2031. row_info->bit_depth = png_ptr->user_transform_depth;
  2032. if (png_ptr->user_transform_channels)
  2033. row_info->channels = png_ptr->user_transform_channels;
  2034. #endif
  2035. row_info->pixel_depth = (png_byte)(row_info->bit_depth *
  2036. row_info->channels);
  2037. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width);
  2038. }
  2039. #endif
  2040. }
  2041. #ifdef PNG_READ_PACK_SUPPORTED
  2042. /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
  2043. * without changing the actual values. Thus, if you had a row with
  2044. * a bit depth of 1, you would end up with bytes that only contained
  2045. * the numbers 0 or 1. If you would rather they contain 0 and 255, use
  2046. * png_do_shift() after this.
  2047. */
  2048. void /* PRIVATE */
  2049. png_do_unpack(png_row_infop row_info, png_bytep row)
  2050. {
  2051. png_debug(1, "in png_do_unpack");
  2052. if (row_info->bit_depth < 8)
  2053. {
  2054. png_uint_32 i;
  2055. png_uint_32 row_width=row_info->width;
  2056. switch (row_info->bit_depth)
  2057. {
  2058. case 1:
  2059. {
  2060. png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
  2061. png_bytep dp = row + (png_size_t)row_width - 1;
  2062. png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
  2063. for (i = 0; i < row_width; i++)
  2064. {
  2065. *dp = (png_byte)((*sp >> shift) & 0x01);
  2066. if (shift == 7)
  2067. {
  2068. shift = 0;
  2069. sp--;
  2070. }
  2071. else
  2072. shift++;
  2073. dp--;
  2074. }
  2075. break;
  2076. }
  2077. case 2:
  2078. {
  2079. png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
  2080. png_bytep dp = row + (png_size_t)row_width - 1;
  2081. png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  2082. for (i = 0; i < row_width; i++)
  2083. {
  2084. *dp = (png_byte)((*sp >> shift) & 0x03);
  2085. if (shift == 6)
  2086. {
  2087. shift = 0;
  2088. sp--;
  2089. }
  2090. else
  2091. shift += 2;
  2092. dp--;
  2093. }
  2094. break;
  2095. }
  2096. case 4:
  2097. {
  2098. png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
  2099. png_bytep dp = row + (png_size_t)row_width - 1;
  2100. png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
  2101. for (i = 0; i < row_width; i++)
  2102. {
  2103. *dp = (png_byte)((*sp >> shift) & 0x0f);
  2104. if (shift == 4)
  2105. {
  2106. shift = 0;
  2107. sp--;
  2108. }
  2109. else
  2110. shift = 4;
  2111. dp--;
  2112. }
  2113. break;
  2114. }
  2115. default:
  2116. break;
  2117. }
  2118. row_info->bit_depth = 8;
  2119. row_info->pixel_depth = (png_byte)(8 * row_info->channels);
  2120. row_info->rowbytes = row_width * row_info->channels;
  2121. }
  2122. }
  2123. #endif
  2124. #ifdef PNG_READ_SHIFT_SUPPORTED
  2125. /* Reverse the effects of png_do_shift. This routine merely shifts the
  2126. * pixels back to their significant bits values. Thus, if you have
  2127. * a row of bit depth 8, but only 5 are significant, this will shift
  2128. * the values back to 0 through 31.
  2129. */
  2130. void /* PRIVATE */
  2131. png_do_unshift(png_row_infop row_info, png_bytep row,
  2132. png_const_color_8p sig_bits)
  2133. {
  2134. int color_type;
  2135. png_debug(1, "in png_do_unshift");
  2136. /* The palette case has already been handled in the _init routine. */
  2137. color_type = row_info->color_type;
  2138. if (color_type != PNG_COLOR_TYPE_PALETTE)
  2139. {
  2140. int shift[4];
  2141. int channels = 0;
  2142. int bit_depth = row_info->bit_depth;
  2143. if (color_type & PNG_COLOR_MASK_COLOR)
  2144. {
  2145. shift[channels++] = bit_depth - sig_bits->red;
  2146. shift[channels++] = bit_depth - sig_bits->green;
  2147. shift[channels++] = bit_depth - sig_bits->blue;
  2148. }
  2149. else
  2150. {
  2151. shift[channels++] = bit_depth - sig_bits->gray;
  2152. }
  2153. if (color_type & PNG_COLOR_MASK_ALPHA)
  2154. {
  2155. shift[channels++] = bit_depth - sig_bits->alpha;
  2156. }
  2157. {
  2158. int c, have_shift;
  2159. for (c = have_shift = 0; c < channels; ++c)
  2160. {
  2161. /* A shift of more than the bit depth is an error condition but it
  2162. * gets ignored here.
  2163. */
  2164. if (shift[c] <= 0 || shift[c] >= bit_depth)
  2165. shift[c] = 0;
  2166. else
  2167. have_shift = 1;
  2168. }
  2169. if (!have_shift)
  2170. return;
  2171. }
  2172. switch (bit_depth)
  2173. {
  2174. default:
  2175. /* Must be 1bpp gray: should not be here! */
  2176. /* NOTREACHED */
  2177. break;
  2178. case 2:
  2179. /* Must be 2bpp gray */
  2180. /* assert(channels == 1 && shift[0] == 1) */
  2181. {
  2182. png_bytep bp = row;
  2183. png_bytep bp_end = bp + row_info->rowbytes;
  2184. while (bp < bp_end)
  2185. {
  2186. int b = (*bp >> 1) & 0x55;
  2187. *bp++ = (png_byte)b;
  2188. }
  2189. break;
  2190. }
  2191. case 4:
  2192. /* Must be 4bpp gray */
  2193. /* assert(channels == 1) */
  2194. {
  2195. png_bytep bp = row;
  2196. png_bytep bp_end = bp + row_info->rowbytes;
  2197. int gray_shift = shift[0];
  2198. int mask = 0xf >> gray_shift;
  2199. mask |= mask << 4;
  2200. while (bp < bp_end)
  2201. {
  2202. int b = (*bp >> gray_shift) & mask;
  2203. *bp++ = (png_byte)b;
  2204. }
  2205. break;
  2206. }
  2207. case 8:
  2208. /* Single byte components, G, GA, RGB, RGBA */
  2209. {
  2210. png_bytep bp = row;
  2211. png_bytep bp_end = bp + row_info->rowbytes;
  2212. int channel = 0;
  2213. while (bp < bp_end)
  2214. {
  2215. int b = *bp >> shift[channel];
  2216. if (++channel >= channels)
  2217. channel = 0;
  2218. *bp++ = (png_byte)b;
  2219. }
  2220. break;
  2221. }
  2222. #ifdef PNG_READ_16BIT_SUPPORTED
  2223. case 16:
  2224. /* Double byte components, G, GA, RGB, RGBA */
  2225. {
  2226. png_bytep bp = row;
  2227. png_bytep bp_end = bp + row_info->rowbytes;
  2228. int channel = 0;
  2229. while (bp < bp_end)
  2230. {
  2231. int value = (bp[0] << 8) + bp[1];
  2232. value >>= shift[channel];
  2233. if (++channel >= channels)
  2234. channel = 0;
  2235. *bp++ = (png_byte)(value >> 8);
  2236. *bp++ = (png_byte)(value & 0xff);
  2237. }
  2238. break;
  2239. }
  2240. #endif
  2241. }
  2242. }
  2243. }
  2244. #endif
  2245. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  2246. /* Scale rows of bit depth 16 down to 8 accurately */
  2247. void /* PRIVATE */
  2248. png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
  2249. {
  2250. png_debug(1, "in png_do_scale_16_to_8");
  2251. if (row_info->bit_depth == 16)
  2252. {
  2253. png_bytep sp = row; /* source */
  2254. png_bytep dp = row; /* destination */
  2255. png_bytep ep = sp + row_info->rowbytes; /* end+1 */
  2256. while (sp < ep)
  2257. {
  2258. /* The input is an array of 16 bit components, these must be scaled to
  2259. * 8 bits each. For a 16 bit value V the required value (from the PNG
  2260. * specification) is:
  2261. *
  2262. * (V * 255) / 65535
  2263. *
  2264. * This reduces to round(V / 257), or floor((V + 128.5)/257)
  2265. *
  2266. * Represent V as the two byte value vhi.vlo. Make a guess that the
  2267. * result is the top byte of V, vhi, then the correction to this value
  2268. * is:
  2269. *
  2270. * error = floor(((V-vhi.vhi) + 128.5) / 257)
  2271. * = floor(((vlo-vhi) + 128.5) / 257)
  2272. *
  2273. * This can be approximated using integer arithmetic (and a signed
  2274. * shift):
  2275. *
  2276. * error = (vlo-vhi+128) >> 8;
  2277. *
  2278. * The approximate differs from the exact answer only when (vlo-vhi) is
  2279. * 128; it then gives a correction of +1 when the exact correction is
  2280. * 0. This gives 128 errors. The exact answer (correct for all 16 bit
  2281. * input values) is:
  2282. *
  2283. * error = (vlo-vhi+128)*65535 >> 24;
  2284. *
  2285. * An alternative arithmetic calculation which also gives no errors is:
  2286. *
  2287. * (V * 255 + 32895) >> 16
  2288. */
  2289. png_int_32 tmp = *sp++; /* must be signed! */
  2290. tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24;
  2291. *dp++ = (png_byte)tmp;
  2292. }
  2293. row_info->bit_depth = 8;
  2294. row_info->pixel_depth = (png_byte)(8 * row_info->channels);
  2295. row_info->rowbytes = row_info->width * row_info->channels;
  2296. }
  2297. }
  2298. #endif
  2299. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  2300. void /* PRIVATE */
  2301. /* Simply discard the low byte. This was the default behavior prior
  2302. * to libpng-1.5.4.
  2303. */
  2304. png_do_chop(png_row_infop row_info, png_bytep row)
  2305. {
  2306. png_debug(1, "in png_do_chop");
  2307. if (row_info->bit_depth == 16)
  2308. {
  2309. png_bytep sp = row; /* source */
  2310. png_bytep dp = row; /* destination */
  2311. png_bytep ep = sp + row_info->rowbytes; /* end+1 */
  2312. while (sp < ep)
  2313. {
  2314. *dp++ = *sp;
  2315. sp += 2; /* skip low byte */
  2316. }
  2317. row_info->bit_depth = 8;
  2318. row_info->pixel_depth = (png_byte)(8 * row_info->channels);
  2319. row_info->rowbytes = row_info->width * row_info->channels;
  2320. }
  2321. }
  2322. #endif
  2323. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  2324. void /* PRIVATE */
  2325. png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
  2326. {
  2327. png_debug(1, "in png_do_read_swap_alpha");
  2328. {
  2329. png_uint_32 row_width = row_info->width;
  2330. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2331. {
  2332. /* This converts from RGBA to ARGB */
  2333. if (row_info->bit_depth == 8)
  2334. {
  2335. png_bytep sp = row + row_info->rowbytes;
  2336. png_bytep dp = sp;
  2337. png_byte save;
  2338. png_uint_32 i;
  2339. for (i = 0; i < row_width; i++)
  2340. {
  2341. save = *(--sp);
  2342. *(--dp) = *(--sp);
  2343. *(--dp) = *(--sp);
  2344. *(--dp) = *(--sp);
  2345. *(--dp) = save;
  2346. }
  2347. }
  2348. #ifdef PNG_READ_16BIT_SUPPORTED
  2349. /* This converts from RRGGBBAA to AARRGGBB */
  2350. else
  2351. {
  2352. png_bytep sp = row + row_info->rowbytes;
  2353. png_bytep dp = sp;
  2354. png_byte save[2];
  2355. png_uint_32 i;
  2356. for (i = 0; i < row_width; i++)
  2357. {
  2358. save[0] = *(--sp);
  2359. save[1] = *(--sp);
  2360. *(--dp) = *(--sp);
  2361. *(--dp) = *(--sp);
  2362. *(--dp) = *(--sp);
  2363. *(--dp) = *(--sp);
  2364. *(--dp) = *(--sp);
  2365. *(--dp) = *(--sp);
  2366. *(--dp) = save[0];
  2367. *(--dp) = save[1];
  2368. }
  2369. }
  2370. #endif
  2371. }
  2372. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2373. {
  2374. /* This converts from GA to AG */
  2375. if (row_info->bit_depth == 8)
  2376. {
  2377. png_bytep sp = row + row_info->rowbytes;
  2378. png_bytep dp = sp;
  2379. png_byte save;
  2380. png_uint_32 i;
  2381. for (i = 0; i < row_width; i++)
  2382. {
  2383. save = *(--sp);
  2384. *(--dp) = *(--sp);
  2385. *(--dp) = save;
  2386. }
  2387. }
  2388. #ifdef PNG_READ_16BIT_SUPPORTED
  2389. /* This converts from GGAA to AAGG */
  2390. else
  2391. {
  2392. png_bytep sp = row + row_info->rowbytes;
  2393. png_bytep dp = sp;
  2394. png_byte save[2];
  2395. png_uint_32 i;
  2396. for (i = 0; i < row_width; i++)
  2397. {
  2398. save[0] = *(--sp);
  2399. save[1] = *(--sp);
  2400. *(--dp) = *(--sp);
  2401. *(--dp) = *(--sp);
  2402. *(--dp) = save[0];
  2403. *(--dp) = save[1];
  2404. }
  2405. }
  2406. #endif
  2407. }
  2408. }
  2409. }
  2410. #endif
  2411. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  2412. void /* PRIVATE */
  2413. png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
  2414. {
  2415. png_uint_32 row_width;
  2416. png_debug(1, "in png_do_read_invert_alpha");
  2417. row_width = row_info->width;
  2418. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2419. {
  2420. if (row_info->bit_depth == 8)
  2421. {
  2422. /* This inverts the alpha channel in RGBA */
  2423. png_bytep sp = row + row_info->rowbytes;
  2424. png_bytep dp = sp;
  2425. png_uint_32 i;
  2426. for (i = 0; i < row_width; i++)
  2427. {
  2428. *(--dp) = (png_byte)(255 - *(--sp));
  2429. /* This does nothing:
  2430. *(--dp) = *(--sp);
  2431. *(--dp) = *(--sp);
  2432. *(--dp) = *(--sp);
  2433. We can replace it with:
  2434. */
  2435. sp-=3;
  2436. dp=sp;
  2437. }
  2438. }
  2439. #ifdef PNG_READ_16BIT_SUPPORTED
  2440. /* This inverts the alpha channel in RRGGBBAA */
  2441. else
  2442. {
  2443. png_bytep sp = row + row_info->rowbytes;
  2444. png_bytep dp = sp;
  2445. png_uint_32 i;
  2446. for (i = 0; i < row_width; i++)
  2447. {
  2448. *(--dp) = (png_byte)(255 - *(--sp));
  2449. *(--dp) = (png_byte)(255 - *(--sp));
  2450. /* This does nothing:
  2451. *(--dp) = *(--sp);
  2452. *(--dp) = *(--sp);
  2453. *(--dp) = *(--sp);
  2454. *(--dp) = *(--sp);
  2455. *(--dp) = *(--sp);
  2456. *(--dp) = *(--sp);
  2457. We can replace it with:
  2458. */
  2459. sp-=6;
  2460. dp=sp;
  2461. }
  2462. }
  2463. #endif
  2464. }
  2465. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2466. {
  2467. if (row_info->bit_depth == 8)
  2468. {
  2469. /* This inverts the alpha channel in GA */
  2470. png_bytep sp = row + row_info->rowbytes;
  2471. png_bytep dp = sp;
  2472. png_uint_32 i;
  2473. for (i = 0; i < row_width; i++)
  2474. {
  2475. *(--dp) = (png_byte)(255 - *(--sp));
  2476. *(--dp) = *(--sp);
  2477. }
  2478. }
  2479. #ifdef PNG_READ_16BIT_SUPPORTED
  2480. else
  2481. {
  2482. /* This inverts the alpha channel in GGAA */
  2483. png_bytep sp = row + row_info->rowbytes;
  2484. png_bytep dp = sp;
  2485. png_uint_32 i;
  2486. for (i = 0; i < row_width; i++)
  2487. {
  2488. *(--dp) = (png_byte)(255 - *(--sp));
  2489. *(--dp) = (png_byte)(255 - *(--sp));
  2490. /*
  2491. *(--dp) = *(--sp);
  2492. *(--dp) = *(--sp);
  2493. */
  2494. sp-=2;
  2495. dp=sp;
  2496. }
  2497. }
  2498. #endif
  2499. }
  2500. }
  2501. #endif
  2502. #ifdef PNG_READ_FILLER_SUPPORTED
  2503. /* Add filler channel if we have RGB color */
  2504. void /* PRIVATE */
  2505. png_do_read_filler(png_row_infop row_info, png_bytep row,
  2506. png_uint_32 filler, png_uint_32 flags)
  2507. {
  2508. png_uint_32 i;
  2509. png_uint_32 row_width = row_info->width;
  2510. #ifdef PNG_READ_16BIT_SUPPORTED
  2511. png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
  2512. #endif
  2513. png_byte lo_filler = (png_byte)(filler & 0xff);
  2514. png_debug(1, "in png_do_read_filler");
  2515. if (
  2516. row_info->color_type == PNG_COLOR_TYPE_GRAY)
  2517. {
  2518. if (row_info->bit_depth == 8)
  2519. {
  2520. if (flags & PNG_FLAG_FILLER_AFTER)
  2521. {
  2522. /* This changes the data from G to GX */
  2523. png_bytep sp = row + (png_size_t)row_width;
  2524. png_bytep dp = sp + (png_size_t)row_width;
  2525. for (i = 1; i < row_width; i++)
  2526. {
  2527. *(--dp) = lo_filler;
  2528. *(--dp) = *(--sp);
  2529. }
  2530. *(--dp) = lo_filler;
  2531. row_info->channels = 2;
  2532. row_info->pixel_depth = 16;
  2533. row_info->rowbytes = row_width * 2;
  2534. }
  2535. else
  2536. {
  2537. /* This changes the data from G to XG */
  2538. png_bytep sp = row + (png_size_t)row_width;
  2539. png_bytep dp = sp + (png_size_t)row_width;
  2540. for (i = 0; i < row_width; i++)
  2541. {
  2542. *(--dp) = *(--sp);
  2543. *(--dp) = lo_filler;
  2544. }
  2545. row_info->channels = 2;
  2546. row_info->pixel_depth = 16;
  2547. row_info->rowbytes = row_width * 2;
  2548. }
  2549. }
  2550. #ifdef PNG_READ_16BIT_SUPPORTED
  2551. else if (row_info->bit_depth == 16)
  2552. {
  2553. if (flags & PNG_FLAG_FILLER_AFTER)
  2554. {
  2555. /* This changes the data from GG to GGXX */
  2556. png_bytep sp = row + (png_size_t)row_width * 2;
  2557. png_bytep dp = sp + (png_size_t)row_width * 2;
  2558. for (i = 1; i < row_width; i++)
  2559. {
  2560. *(--dp) = hi_filler;
  2561. *(--dp) = lo_filler;
  2562. *(--dp) = *(--sp);
  2563. *(--dp) = *(--sp);
  2564. }
  2565. *(--dp) = hi_filler;
  2566. *(--dp) = lo_filler;
  2567. row_info->channels = 2;
  2568. row_info->pixel_depth = 32;
  2569. row_info->rowbytes = row_width * 4;
  2570. }
  2571. else
  2572. {
  2573. /* This changes the data from GG to XXGG */
  2574. png_bytep sp = row + (png_size_t)row_width * 2;
  2575. png_bytep dp = sp + (png_size_t)row_width * 2;
  2576. for (i = 0; i < row_width; i++)
  2577. {
  2578. *(--dp) = *(--sp);
  2579. *(--dp) = *(--sp);
  2580. *(--dp) = hi_filler;
  2581. *(--dp) = lo_filler;
  2582. }
  2583. row_info->channels = 2;
  2584. row_info->pixel_depth = 32;
  2585. row_info->rowbytes = row_width * 4;
  2586. }
  2587. }
  2588. #endif
  2589. } /* COLOR_TYPE == GRAY */
  2590. else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  2591. {
  2592. if (row_info->bit_depth == 8)
  2593. {
  2594. if (flags & PNG_FLAG_FILLER_AFTER)
  2595. {
  2596. /* This changes the data from RGB to RGBX */
  2597. png_bytep sp = row + (png_size_t)row_width * 3;
  2598. png_bytep dp = sp + (png_size_t)row_width;
  2599. for (i = 1; i < row_width; i++)
  2600. {
  2601. *(--dp) = lo_filler;
  2602. *(--dp) = *(--sp);
  2603. *(--dp) = *(--sp);
  2604. *(--dp) = *(--sp);
  2605. }
  2606. *(--dp) = lo_filler;
  2607. row_info->channels = 4;
  2608. row_info->pixel_depth = 32;
  2609. row_info->rowbytes = row_width * 4;
  2610. }
  2611. else
  2612. {
  2613. /* This changes the data from RGB to XRGB */
  2614. png_bytep sp = row + (png_size_t)row_width * 3;
  2615. png_bytep dp = sp + (png_size_t)row_width;
  2616. for (i = 0; i < row_width; i++)
  2617. {
  2618. *(--dp) = *(--sp);
  2619. *(--dp) = *(--sp);
  2620. *(--dp) = *(--sp);
  2621. *(--dp) = lo_filler;
  2622. }
  2623. row_info->channels = 4;
  2624. row_info->pixel_depth = 32;
  2625. row_info->rowbytes = row_width * 4;
  2626. }
  2627. }
  2628. #ifdef PNG_READ_16BIT_SUPPORTED
  2629. else if (row_info->bit_depth == 16)
  2630. {
  2631. if (flags & PNG_FLAG_FILLER_AFTER)
  2632. {
  2633. /* This changes the data from RRGGBB to RRGGBBXX */
  2634. png_bytep sp = row + (png_size_t)row_width * 6;
  2635. png_bytep dp = sp + (png_size_t)row_width * 2;
  2636. for (i = 1; i < row_width; i++)
  2637. {
  2638. *(--dp) = hi_filler;
  2639. *(--dp) = lo_filler;
  2640. *(--dp) = *(--sp);
  2641. *(--dp) = *(--sp);
  2642. *(--dp) = *(--sp);
  2643. *(--dp) = *(--sp);
  2644. *(--dp) = *(--sp);
  2645. *(--dp) = *(--sp);
  2646. }
  2647. *(--dp) = hi_filler;
  2648. *(--dp) = lo_filler;
  2649. row_info->channels = 4;
  2650. row_info->pixel_depth = 64;
  2651. row_info->rowbytes = row_width * 8;
  2652. }
  2653. else
  2654. {
  2655. /* This changes the data from RRGGBB to XXRRGGBB */
  2656. png_bytep sp = row + (png_size_t)row_width * 6;
  2657. png_bytep dp = sp + (png_size_t)row_width * 2;
  2658. for (i = 0; i < row_width; i++)
  2659. {
  2660. *(--dp) = *(--sp);
  2661. *(--dp) = *(--sp);
  2662. *(--dp) = *(--sp);
  2663. *(--dp) = *(--sp);
  2664. *(--dp) = *(--sp);
  2665. *(--dp) = *(--sp);
  2666. *(--dp) = hi_filler;
  2667. *(--dp) = lo_filler;
  2668. }
  2669. row_info->channels = 4;
  2670. row_info->pixel_depth = 64;
  2671. row_info->rowbytes = row_width * 8;
  2672. }
  2673. }
  2674. #endif
  2675. } /* COLOR_TYPE == RGB */
  2676. }
  2677. #endif
  2678. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  2679. /* Expand grayscale files to RGB, with or without alpha */
  2680. void /* PRIVATE */
  2681. png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
  2682. {
  2683. png_uint_32 i;
  2684. png_uint_32 row_width = row_info->width;
  2685. png_debug(1, "in png_do_gray_to_rgb");
  2686. if (row_info->bit_depth >= 8 &&
  2687. !(row_info->color_type & PNG_COLOR_MASK_COLOR))
  2688. {
  2689. if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  2690. {
  2691. if (row_info->bit_depth == 8)
  2692. {
  2693. /* This changes G to RGB */
  2694. png_bytep sp = row + (png_size_t)row_width - 1;
  2695. png_bytep dp = sp + (png_size_t)row_width * 2;
  2696. for (i = 0; i < row_width; i++)
  2697. {
  2698. *(dp--) = *sp;
  2699. *(dp--) = *sp;
  2700. *(dp--) = *(sp--);
  2701. }
  2702. }
  2703. else
  2704. {
  2705. /* This changes GG to RRGGBB */
  2706. png_bytep sp = row + (png_size_t)row_width * 2 - 1;
  2707. png_bytep dp = sp + (png_size_t)row_width * 4;
  2708. for (i = 0; i < row_width; i++)
  2709. {
  2710. *(dp--) = *sp;
  2711. *(dp--) = *(sp - 1);
  2712. *(dp--) = *sp;
  2713. *(dp--) = *(sp - 1);
  2714. *(dp--) = *(sp--);
  2715. *(dp--) = *(sp--);
  2716. }
  2717. }
  2718. }
  2719. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2720. {
  2721. if (row_info->bit_depth == 8)
  2722. {
  2723. /* This changes GA to RGBA */
  2724. png_bytep sp = row + (png_size_t)row_width * 2 - 1;
  2725. png_bytep dp = sp + (png_size_t)row_width * 2;
  2726. for (i = 0; i < row_width; i++)
  2727. {
  2728. *(dp--) = *(sp--);
  2729. *(dp--) = *sp;
  2730. *(dp--) = *sp;
  2731. *(dp--) = *(sp--);
  2732. }
  2733. }
  2734. else
  2735. {
  2736. /* This changes GGAA to RRGGBBAA */
  2737. png_bytep sp = row + (png_size_t)row_width * 4 - 1;
  2738. png_bytep dp = sp + (png_size_t)row_width * 4;
  2739. for (i = 0; i < row_width; i++)
  2740. {
  2741. *(dp--) = *(sp--);
  2742. *(dp--) = *(sp--);
  2743. *(dp--) = *sp;
  2744. *(dp--) = *(sp - 1);
  2745. *(dp--) = *sp;
  2746. *(dp--) = *(sp - 1);
  2747. *(dp--) = *(sp--);
  2748. *(dp--) = *(sp--);
  2749. }
  2750. }
  2751. }
  2752. row_info->channels = (png_byte)(row_info->channels + 2);
  2753. row_info->color_type |= PNG_COLOR_MASK_COLOR;
  2754. row_info->pixel_depth = (png_byte)(row_info->channels *
  2755. row_info->bit_depth);
  2756. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  2757. }
  2758. }
  2759. #endif
  2760. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  2761. /* Reduce RGB files to grayscale, with or without alpha
  2762. * using the equation given in Poynton's ColorFAQ of 1998-01-04 at
  2763. * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but
  2764. * versions dated 1998 through November 2002 have been archived at
  2765. * http://web.archive.org/web/20000816232553/http://www.inforamp.net/
  2766. * ~poynton/notes/colour_and_gamma/ColorFAQ.txt )
  2767. * Charles Poynton poynton at poynton.com
  2768. *
  2769. * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
  2770. *
  2771. * which can be expressed with integers as
  2772. *
  2773. * Y = (6969 * R + 23434 * G + 2365 * B)/32768
  2774. *
  2775. * Poynton's current link (as of January 2003 through July 2011):
  2776. * <http://www.poynton.com/notes/colour_and_gamma/>
  2777. * has changed the numbers slightly:
  2778. *
  2779. * Y = 0.2126*R + 0.7152*G + 0.0722*B
  2780. *
  2781. * which can be expressed with integers as
  2782. *
  2783. * Y = (6966 * R + 23436 * G + 2366 * B)/32768
  2784. *
  2785. * Historically, however, libpng uses numbers derived from the ITU-R Rec 709
  2786. * end point chromaticities and the D65 white point. Depending on the
  2787. * precision used for the D65 white point this produces a variety of different
  2788. * numbers, however if the four decimal place value used in ITU-R Rec 709 is
  2789. * used (0.3127,0.3290) the Y calculation would be:
  2790. *
  2791. * Y = (6968 * R + 23435 * G + 2366 * B)/32768
  2792. *
  2793. * While this is correct the rounding results in an overflow for white, because
  2794. * the sum of the rounded coefficients is 32769, not 32768. Consequently
  2795. * libpng uses, instead, the closest non-overflowing approximation:
  2796. *
  2797. * Y = (6968 * R + 23434 * G + 2366 * B)/32768
  2798. *
  2799. * Starting with libpng-1.5.5, if the image being converted has a cHRM chunk
  2800. * (including an sRGB chunk) then the chromaticities are used to calculate the
  2801. * coefficients. See the chunk handling in pngrutil.c for more information.
  2802. *
  2803. * In all cases the calculation is to be done in a linear colorspace. If no
  2804. * gamma information is available to correct the encoding of the original RGB
  2805. * values this results in an implicit assumption that the original PNG RGB
  2806. * values were linear.
  2807. *
  2808. * Other integer coefficents can be used via png_set_rgb_to_gray(). Because
  2809. * the API takes just red and green coefficients the blue coefficient is
  2810. * calculated to make the sum 32768. This will result in different rounding
  2811. * to that used above.
  2812. */
  2813. int /* PRIVATE */
  2814. png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
  2815. {
  2816. int rgb_error = 0;
  2817. png_debug(1, "in png_do_rgb_to_gray");
  2818. if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) &&
  2819. (row_info->color_type & PNG_COLOR_MASK_COLOR))
  2820. {
  2821. PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
  2822. PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
  2823. PNG_CONST png_uint_32 bc = 32768 - rc - gc;
  2824. PNG_CONST png_uint_32 row_width = row_info->width;
  2825. PNG_CONST int have_alpha =
  2826. (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0;
  2827. if (row_info->bit_depth == 8)
  2828. {
  2829. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  2830. /* Notice that gamma to/from 1 are not necessarily inverses (if
  2831. * there is an overall gamma correction). Prior to 1.5.5 this code
  2832. * checked the linearized values for equality; this doesn't match
  2833. * the documentation, the original values must be checked.
  2834. */
  2835. if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
  2836. {
  2837. png_bytep sp = row;
  2838. png_bytep dp = row;
  2839. png_uint_32 i;
  2840. for (i = 0; i < row_width; i++)
  2841. {
  2842. png_byte red = *(sp++);
  2843. png_byte green = *(sp++);
  2844. png_byte blue = *(sp++);
  2845. if (red != green || red != blue)
  2846. {
  2847. red = png_ptr->gamma_to_1[red];
  2848. green = png_ptr->gamma_to_1[green];
  2849. blue = png_ptr->gamma_to_1[blue];
  2850. rgb_error |= 1;
  2851. *(dp++) = png_ptr->gamma_from_1[
  2852. (rc*red + gc*green + bc*blue + 16384)>>15];
  2853. }
  2854. else
  2855. {
  2856. /* If there is no overall correction the table will not be
  2857. * set.
  2858. */
  2859. if (png_ptr->gamma_table != NULL)
  2860. red = png_ptr->gamma_table[red];
  2861. *(dp++) = red;
  2862. }
  2863. if (have_alpha)
  2864. *(dp++) = *(sp++);
  2865. }
  2866. }
  2867. else
  2868. #endif
  2869. {
  2870. png_bytep sp = row;
  2871. png_bytep dp = row;
  2872. png_uint_32 i;
  2873. for (i = 0; i < row_width; i++)
  2874. {
  2875. png_byte red = *(sp++);
  2876. png_byte green = *(sp++);
  2877. png_byte blue = *(sp++);
  2878. if (red != green || red != blue)
  2879. {
  2880. rgb_error |= 1;
  2881. /*NOTE: this is the historical approach which simply
  2882. * truncates the results.
  2883. */
  2884. *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
  2885. }
  2886. else
  2887. *(dp++) = red;
  2888. if (have_alpha)
  2889. *(dp++) = *(sp++);
  2890. }
  2891. }
  2892. }
  2893. else /* RGB bit_depth == 16 */
  2894. {
  2895. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  2896. if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL)
  2897. {
  2898. png_bytep sp = row;
  2899. png_bytep dp = row;
  2900. png_uint_32 i;
  2901. for (i = 0; i < row_width; i++)
  2902. {
  2903. png_uint_16 red, green, blue, w;
  2904. red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2905. green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2906. blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2907. if (red == green && red == blue)
  2908. {
  2909. if (png_ptr->gamma_16_table != NULL)
  2910. w = png_ptr->gamma_16_table[(red&0xff)
  2911. >> png_ptr->gamma_shift][red>>8];
  2912. else
  2913. w = red;
  2914. }
  2915. else
  2916. {
  2917. png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff)
  2918. >> png_ptr->gamma_shift][red>>8];
  2919. png_uint_16 green_1 =
  2920. png_ptr->gamma_16_to_1[(green&0xff) >>
  2921. png_ptr->gamma_shift][green>>8];
  2922. png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff)
  2923. >> png_ptr->gamma_shift][blue>>8];
  2924. png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
  2925. + bc*blue_1 + 16384)>>15);
  2926. w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
  2927. png_ptr->gamma_shift][gray16 >> 8];
  2928. rgb_error |= 1;
  2929. }
  2930. *(dp++) = (png_byte)((w>>8) & 0xff);
  2931. *(dp++) = (png_byte)(w & 0xff);
  2932. if (have_alpha)
  2933. {
  2934. *(dp++) = *(sp++);
  2935. *(dp++) = *(sp++);
  2936. }
  2937. }
  2938. }
  2939. else
  2940. #endif
  2941. {
  2942. png_bytep sp = row;
  2943. png_bytep dp = row;
  2944. png_uint_32 i;
  2945. for (i = 0; i < row_width; i++)
  2946. {
  2947. png_uint_16 red, green, blue, gray16;
  2948. red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2949. green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2950. blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
  2951. if (red != green || red != blue)
  2952. rgb_error |= 1;
  2953. /* From 1.5.5 in the 16 bit case do the accurate conversion even
  2954. * in the 'fast' case - this is because this is where the code
  2955. * ends up when handling linear 16 bit data.
  2956. */
  2957. gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >>
  2958. 15);
  2959. *(dp++) = (png_byte)((gray16>>8) & 0xff);
  2960. *(dp++) = (png_byte)(gray16 & 0xff);
  2961. if (have_alpha)
  2962. {
  2963. *(dp++) = *(sp++);
  2964. *(dp++) = *(sp++);
  2965. }
  2966. }
  2967. }
  2968. }
  2969. row_info->channels = (png_byte)(row_info->channels - 2);
  2970. row_info->color_type = (png_byte)(row_info->color_type &
  2971. ~PNG_COLOR_MASK_COLOR);
  2972. row_info->pixel_depth = (png_byte)(row_info->channels *
  2973. row_info->bit_depth);
  2974. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  2975. }
  2976. return rgb_error;
  2977. }
  2978. #endif
  2979. #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
  2980. #ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
  2981. /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
  2982. * large of png_color. This lets grayscale images be treated as
  2983. * paletted. Most useful for gamma correction and simplification
  2984. * of code. This API is not used internally.
  2985. */
  2986. void PNGAPI
  2987. png_build_grayscale_palette(int bit_depth, png_colorp palette)
  2988. {
  2989. int num_palette;
  2990. int color_inc;
  2991. int i;
  2992. int v;
  2993. png_debug(1, "in png_do_build_grayscale_palette");
  2994. if (palette == NULL)
  2995. return;
  2996. switch (bit_depth)
  2997. {
  2998. case 1:
  2999. num_palette = 2;
  3000. color_inc = 0xff;
  3001. break;
  3002. case 2:
  3003. num_palette = 4;
  3004. color_inc = 0x55;
  3005. break;
  3006. case 4:
  3007. num_palette = 16;
  3008. color_inc = 0x11;
  3009. break;
  3010. case 8:
  3011. num_palette = 256;
  3012. color_inc = 1;
  3013. break;
  3014. default:
  3015. num_palette = 0;
  3016. color_inc = 0;
  3017. break;
  3018. }
  3019. for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
  3020. {
  3021. palette[i].red = (png_byte)v;
  3022. palette[i].green = (png_byte)v;
  3023. palette[i].blue = (png_byte)v;
  3024. }
  3025. }
  3026. #endif
  3027. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  3028. #if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
  3029. (defined PNG_READ_ALPHA_MODE_SUPPORTED)
  3030. /* Replace any alpha or transparency with the supplied background color.
  3031. * "background" is already in the screen gamma, while "background_1" is
  3032. * at a gamma of 1.0. Paletted files have already been taken care of.
  3033. */
  3034. void /* PRIVATE */
  3035. png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr)
  3036. {
  3037. #ifdef PNG_READ_GAMMA_SUPPORTED
  3038. png_const_bytep gamma_table = png_ptr->gamma_table;
  3039. png_const_bytep gamma_from_1 = png_ptr->gamma_from_1;
  3040. png_const_bytep gamma_to_1 = png_ptr->gamma_to_1;
  3041. png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table;
  3042. png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1;
  3043. png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1;
  3044. int gamma_shift = png_ptr->gamma_shift;
  3045. #endif
  3046. png_bytep sp;
  3047. png_uint_32 i;
  3048. png_uint_32 row_width = row_info->width;
  3049. int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0;
  3050. int shift;
  3051. png_debug(1, "in png_do_compose");
  3052. {
  3053. switch (row_info->color_type)
  3054. {
  3055. case PNG_COLOR_TYPE_GRAY:
  3056. {
  3057. switch (row_info->bit_depth)
  3058. {
  3059. case 1:
  3060. {
  3061. sp = row;
  3062. shift = 7;
  3063. for (i = 0; i < row_width; i++)
  3064. {
  3065. if ((png_uint_16)((*sp >> shift) & 0x01)
  3066. == png_ptr->trans_color.gray)
  3067. {
  3068. *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  3069. *sp |= (png_byte)(png_ptr->background.gray << shift);
  3070. }
  3071. if (!shift)
  3072. {
  3073. shift = 7;
  3074. sp++;
  3075. }
  3076. else
  3077. shift--;
  3078. }
  3079. break;
  3080. }
  3081. case 2:
  3082. {
  3083. #ifdef PNG_READ_GAMMA_SUPPORTED
  3084. if (gamma_table != NULL)
  3085. {
  3086. sp = row;
  3087. shift = 6;
  3088. for (i = 0; i < row_width; i++)
  3089. {
  3090. if ((png_uint_16)((*sp >> shift) & 0x03)
  3091. == png_ptr->trans_color.gray)
  3092. {
  3093. *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  3094. *sp |= (png_byte)(png_ptr->background.gray << shift);
  3095. }
  3096. else
  3097. {
  3098. png_byte p = (png_byte)((*sp >> shift) & 0x03);
  3099. png_byte g = (png_byte)((gamma_table [p | (p << 2) |
  3100. (p << 4) | (p << 6)] >> 6) & 0x03);
  3101. *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  3102. *sp |= (png_byte)(g << shift);
  3103. }
  3104. if (!shift)
  3105. {
  3106. shift = 6;
  3107. sp++;
  3108. }
  3109. else
  3110. shift -= 2;
  3111. }
  3112. }
  3113. else
  3114. #endif
  3115. {
  3116. sp = row;
  3117. shift = 6;
  3118. for (i = 0; i < row_width; i++)
  3119. {
  3120. if ((png_uint_16)((*sp >> shift) & 0x03)
  3121. == png_ptr->trans_color.gray)
  3122. {
  3123. *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  3124. *sp |= (png_byte)(png_ptr->background.gray << shift);
  3125. }
  3126. if (!shift)
  3127. {
  3128. shift = 6;
  3129. sp++;
  3130. }
  3131. else
  3132. shift -= 2;
  3133. }
  3134. }
  3135. break;
  3136. }
  3137. case 4:
  3138. {
  3139. #ifdef PNG_READ_GAMMA_SUPPORTED
  3140. if (gamma_table != NULL)
  3141. {
  3142. sp = row;
  3143. shift = 4;
  3144. for (i = 0; i < row_width; i++)
  3145. {
  3146. if ((png_uint_16)((*sp >> shift) & 0x0f)
  3147. == png_ptr->trans_color.gray)
  3148. {
  3149. *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  3150. *sp |= (png_byte)(png_ptr->background.gray << shift);
  3151. }
  3152. else
  3153. {
  3154. png_byte p = (png_byte)((*sp >> shift) & 0x0f);
  3155. png_byte g = (png_byte)((gamma_table[p |
  3156. (p << 4)] >> 4) & 0x0f);
  3157. *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  3158. *sp |= (png_byte)(g << shift);
  3159. }
  3160. if (!shift)
  3161. {
  3162. shift = 4;
  3163. sp++;
  3164. }
  3165. else
  3166. shift -= 4;
  3167. }
  3168. }
  3169. else
  3170. #endif
  3171. {
  3172. sp = row;
  3173. shift = 4;
  3174. for (i = 0; i < row_width; i++)
  3175. {
  3176. if ((png_uint_16)((*sp >> shift) & 0x0f)
  3177. == png_ptr->trans_color.gray)
  3178. {
  3179. *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  3180. *sp |= (png_byte)(png_ptr->background.gray << shift);
  3181. }
  3182. if (!shift)
  3183. {
  3184. shift = 4;
  3185. sp++;
  3186. }
  3187. else
  3188. shift -= 4;
  3189. }
  3190. }
  3191. break;
  3192. }
  3193. case 8:
  3194. {
  3195. #ifdef PNG_READ_GAMMA_SUPPORTED
  3196. if (gamma_table != NULL)
  3197. {
  3198. sp = row;
  3199. for (i = 0; i < row_width; i++, sp++)
  3200. {
  3201. if (*sp == png_ptr->trans_color.gray)
  3202. *sp = (png_byte)png_ptr->background.gray;
  3203. else
  3204. *sp = gamma_table[*sp];
  3205. }
  3206. }
  3207. else
  3208. #endif
  3209. {
  3210. sp = row;
  3211. for (i = 0; i < row_width; i++, sp++)
  3212. {
  3213. if (*sp == png_ptr->trans_color.gray)
  3214. *sp = (png_byte)png_ptr->background.gray;
  3215. }
  3216. }
  3217. break;
  3218. }
  3219. case 16:
  3220. {
  3221. #ifdef PNG_READ_GAMMA_SUPPORTED
  3222. if (gamma_16 != NULL)
  3223. {
  3224. sp = row;
  3225. for (i = 0; i < row_width; i++, sp += 2)
  3226. {
  3227. png_uint_16 v;
  3228. v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3229. if (v == png_ptr->trans_color.gray)
  3230. {
  3231. /* Background is already in screen gamma */
  3232. *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff);
  3233. *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
  3234. }
  3235. else
  3236. {
  3237. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3238. *sp = (png_byte)((v >> 8) & 0xff);
  3239. *(sp + 1) = (png_byte)(v & 0xff);
  3240. }
  3241. }
  3242. }
  3243. else
  3244. #endif
  3245. {
  3246. sp = row;
  3247. for (i = 0; i < row_width; i++, sp += 2)
  3248. {
  3249. png_uint_16 v;
  3250. v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3251. if (v == png_ptr->trans_color.gray)
  3252. {
  3253. *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff);
  3254. *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
  3255. }
  3256. }
  3257. }
  3258. break;
  3259. }
  3260. default:
  3261. break;
  3262. }
  3263. break;
  3264. }
  3265. case PNG_COLOR_TYPE_RGB:
  3266. {
  3267. if (row_info->bit_depth == 8)
  3268. {
  3269. #ifdef PNG_READ_GAMMA_SUPPORTED
  3270. if (gamma_table != NULL)
  3271. {
  3272. sp = row;
  3273. for (i = 0; i < row_width; i++, sp += 3)
  3274. {
  3275. if (*sp == png_ptr->trans_color.red &&
  3276. *(sp + 1) == png_ptr->trans_color.green &&
  3277. *(sp + 2) == png_ptr->trans_color.blue)
  3278. {
  3279. *sp = (png_byte)png_ptr->background.red;
  3280. *(sp + 1) = (png_byte)png_ptr->background.green;
  3281. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3282. }
  3283. else
  3284. {
  3285. *sp = gamma_table[*sp];
  3286. *(sp + 1) = gamma_table[*(sp + 1)];
  3287. *(sp + 2) = gamma_table[*(sp + 2)];
  3288. }
  3289. }
  3290. }
  3291. else
  3292. #endif
  3293. {
  3294. sp = row;
  3295. for (i = 0; i < row_width; i++, sp += 3)
  3296. {
  3297. if (*sp == png_ptr->trans_color.red &&
  3298. *(sp + 1) == png_ptr->trans_color.green &&
  3299. *(sp + 2) == png_ptr->trans_color.blue)
  3300. {
  3301. *sp = (png_byte)png_ptr->background.red;
  3302. *(sp + 1) = (png_byte)png_ptr->background.green;
  3303. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3304. }
  3305. }
  3306. }
  3307. }
  3308. else /* if (row_info->bit_depth == 16) */
  3309. {
  3310. #ifdef PNG_READ_GAMMA_SUPPORTED
  3311. if (gamma_16 != NULL)
  3312. {
  3313. sp = row;
  3314. for (i = 0; i < row_width; i++, sp += 6)
  3315. {
  3316. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3317. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  3318. + *(sp + 3));
  3319. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  3320. + *(sp + 5));
  3321. if (r == png_ptr->trans_color.red &&
  3322. g == png_ptr->trans_color.green &&
  3323. b == png_ptr->trans_color.blue)
  3324. {
  3325. /* Background is already in screen gamma */
  3326. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3327. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3328. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff);
  3329. *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff);
  3330. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff);
  3331. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3332. }
  3333. else
  3334. {
  3335. png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3336. *sp = (png_byte)((v >> 8) & 0xff);
  3337. *(sp + 1) = (png_byte)(v & 0xff);
  3338. v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  3339. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  3340. *(sp + 3) = (png_byte)(v & 0xff);
  3341. v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  3342. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  3343. *(sp + 5) = (png_byte)(v & 0xff);
  3344. }
  3345. }
  3346. }
  3347. else
  3348. #endif
  3349. {
  3350. sp = row;
  3351. for (i = 0; i < row_width; i++, sp += 6)
  3352. {
  3353. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3354. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  3355. + *(sp + 3));
  3356. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  3357. + *(sp + 5));
  3358. if (r == png_ptr->trans_color.red &&
  3359. g == png_ptr->trans_color.green &&
  3360. b == png_ptr->trans_color.blue)
  3361. {
  3362. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3363. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3364. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff);
  3365. *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff);
  3366. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff);
  3367. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3368. }
  3369. }
  3370. }
  3371. }
  3372. break;
  3373. }
  3374. case PNG_COLOR_TYPE_GRAY_ALPHA:
  3375. {
  3376. if (row_info->bit_depth == 8)
  3377. {
  3378. #ifdef PNG_READ_GAMMA_SUPPORTED
  3379. if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  3380. gamma_table != NULL)
  3381. {
  3382. sp = row;
  3383. for (i = 0; i < row_width; i++, sp += 2)
  3384. {
  3385. png_uint_16 a = *(sp + 1);
  3386. if (a == 0xff)
  3387. *sp = gamma_table[*sp];
  3388. else if (a == 0)
  3389. {
  3390. /* Background is already in screen gamma */
  3391. *sp = (png_byte)png_ptr->background.gray;
  3392. }
  3393. else
  3394. {
  3395. png_byte v, w;
  3396. v = gamma_to_1[*sp];
  3397. png_composite(w, v, a, png_ptr->background_1.gray);
  3398. if (!optimize)
  3399. w = gamma_from_1[w];
  3400. *sp = w;
  3401. }
  3402. }
  3403. }
  3404. else
  3405. #endif
  3406. {
  3407. sp = row;
  3408. for (i = 0; i < row_width; i++, sp += 2)
  3409. {
  3410. png_byte a = *(sp + 1);
  3411. if (a == 0)
  3412. *sp = (png_byte)png_ptr->background.gray;
  3413. else if (a < 0xff)
  3414. png_composite(*sp, *sp, a, png_ptr->background_1.gray);
  3415. }
  3416. }
  3417. }
  3418. else /* if (png_ptr->bit_depth == 16) */
  3419. {
  3420. #ifdef PNG_READ_GAMMA_SUPPORTED
  3421. if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  3422. gamma_16_to_1 != NULL)
  3423. {
  3424. sp = row;
  3425. for (i = 0; i < row_width; i++, sp += 4)
  3426. {
  3427. png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
  3428. + *(sp + 3));
  3429. if (a == (png_uint_16)0xffff)
  3430. {
  3431. png_uint_16 v;
  3432. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3433. *sp = (png_byte)((v >> 8) & 0xff);
  3434. *(sp + 1) = (png_byte)(v & 0xff);
  3435. }
  3436. else if (a == 0)
  3437. {
  3438. /* Background is already in screen gamma */
  3439. *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff);
  3440. *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
  3441. }
  3442. else
  3443. {
  3444. png_uint_16 g, v, w;
  3445. g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  3446. png_composite_16(v, g, a, png_ptr->background_1.gray);
  3447. if (optimize)
  3448. w = v;
  3449. else
  3450. w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
  3451. *sp = (png_byte)((w >> 8) & 0xff);
  3452. *(sp + 1) = (png_byte)(w & 0xff);
  3453. }
  3454. }
  3455. }
  3456. else
  3457. #endif
  3458. {
  3459. sp = row;
  3460. for (i = 0; i < row_width; i++, sp += 4)
  3461. {
  3462. png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
  3463. + *(sp + 3));
  3464. if (a == 0)
  3465. {
  3466. *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff);
  3467. *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
  3468. }
  3469. else if (a < 0xffff)
  3470. {
  3471. png_uint_16 g, v;
  3472. g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3473. png_composite_16(v, g, a, png_ptr->background_1.gray);
  3474. *sp = (png_byte)((v >> 8) & 0xff);
  3475. *(sp + 1) = (png_byte)(v & 0xff);
  3476. }
  3477. }
  3478. }
  3479. }
  3480. break;
  3481. }
  3482. case PNG_COLOR_TYPE_RGB_ALPHA:
  3483. {
  3484. if (row_info->bit_depth == 8)
  3485. {
  3486. #ifdef PNG_READ_GAMMA_SUPPORTED
  3487. if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  3488. gamma_table != NULL)
  3489. {
  3490. sp = row;
  3491. for (i = 0; i < row_width; i++, sp += 4)
  3492. {
  3493. png_byte a = *(sp + 3);
  3494. if (a == 0xff)
  3495. {
  3496. *sp = gamma_table[*sp];
  3497. *(sp + 1) = gamma_table[*(sp + 1)];
  3498. *(sp + 2) = gamma_table[*(sp + 2)];
  3499. }
  3500. else if (a == 0)
  3501. {
  3502. /* Background is already in screen gamma */
  3503. *sp = (png_byte)png_ptr->background.red;
  3504. *(sp + 1) = (png_byte)png_ptr->background.green;
  3505. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3506. }
  3507. else
  3508. {
  3509. png_byte v, w;
  3510. v = gamma_to_1[*sp];
  3511. png_composite(w, v, a, png_ptr->background_1.red);
  3512. if (!optimize) w = gamma_from_1[w];
  3513. *sp = w;
  3514. v = gamma_to_1[*(sp + 1)];
  3515. png_composite(w, v, a, png_ptr->background_1.green);
  3516. if (!optimize) w = gamma_from_1[w];
  3517. *(sp + 1) = w;
  3518. v = gamma_to_1[*(sp + 2)];
  3519. png_composite(w, v, a, png_ptr->background_1.blue);
  3520. if (!optimize) w = gamma_from_1[w];
  3521. *(sp + 2) = w;
  3522. }
  3523. }
  3524. }
  3525. else
  3526. #endif
  3527. {
  3528. sp = row;
  3529. for (i = 0; i < row_width; i++, sp += 4)
  3530. {
  3531. png_byte a = *(sp + 3);
  3532. if (a == 0)
  3533. {
  3534. *sp = (png_byte)png_ptr->background.red;
  3535. *(sp + 1) = (png_byte)png_ptr->background.green;
  3536. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3537. }
  3538. else if (a < 0xff)
  3539. {
  3540. png_composite(*sp, *sp, a, png_ptr->background.red);
  3541. png_composite(*(sp + 1), *(sp + 1), a,
  3542. png_ptr->background.green);
  3543. png_composite(*(sp + 2), *(sp + 2), a,
  3544. png_ptr->background.blue);
  3545. }
  3546. }
  3547. }
  3548. }
  3549. else /* if (row_info->bit_depth == 16) */
  3550. {
  3551. #ifdef PNG_READ_GAMMA_SUPPORTED
  3552. if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  3553. gamma_16_to_1 != NULL)
  3554. {
  3555. sp = row;
  3556. for (i = 0; i < row_width; i++, sp += 8)
  3557. {
  3558. png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
  3559. << 8) + (png_uint_16)(*(sp + 7)));
  3560. if (a == (png_uint_16)0xffff)
  3561. {
  3562. png_uint_16 v;
  3563. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3564. *sp = (png_byte)((v >> 8) & 0xff);
  3565. *(sp + 1) = (png_byte)(v & 0xff);
  3566. v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  3567. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  3568. *(sp + 3) = (png_byte)(v & 0xff);
  3569. v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  3570. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  3571. *(sp + 5) = (png_byte)(v & 0xff);
  3572. }
  3573. else if (a == 0)
  3574. {
  3575. /* Background is already in screen gamma */
  3576. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3577. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3578. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff);
  3579. *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff);
  3580. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff);
  3581. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3582. }
  3583. else
  3584. {
  3585. png_uint_16 v, w;
  3586. v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  3587. png_composite_16(w, v, a, png_ptr->background_1.red);
  3588. if (!optimize)
  3589. w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
  3590. *sp = (png_byte)((w >> 8) & 0xff);
  3591. *(sp + 1) = (png_byte)(w & 0xff);
  3592. v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
  3593. png_composite_16(w, v, a, png_ptr->background_1.green);
  3594. if (!optimize)
  3595. w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
  3596. *(sp + 2) = (png_byte)((w >> 8) & 0xff);
  3597. *(sp + 3) = (png_byte)(w & 0xff);
  3598. v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
  3599. png_composite_16(w, v, a, png_ptr->background_1.blue);
  3600. if (!optimize)
  3601. w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
  3602. *(sp + 4) = (png_byte)((w >> 8) & 0xff);
  3603. *(sp + 5) = (png_byte)(w & 0xff);
  3604. }
  3605. }
  3606. }
  3607. else
  3608. #endif
  3609. {
  3610. sp = row;
  3611. for (i = 0; i < row_width; i++, sp += 8)
  3612. {
  3613. png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
  3614. << 8) + (png_uint_16)(*(sp + 7)));
  3615. if (a == 0)
  3616. {
  3617. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3618. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3619. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff);
  3620. *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff);
  3621. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff);
  3622. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3623. }
  3624. else if (a < 0xffff)
  3625. {
  3626. png_uint_16 v;
  3627. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3628. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  3629. + *(sp + 3));
  3630. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  3631. + *(sp + 5));
  3632. png_composite_16(v, r, a, png_ptr->background.red);
  3633. *sp = (png_byte)((v >> 8) & 0xff);
  3634. *(sp + 1) = (png_byte)(v & 0xff);
  3635. png_composite_16(v, g, a, png_ptr->background.green);
  3636. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  3637. *(sp + 3) = (png_byte)(v & 0xff);
  3638. png_composite_16(v, b, a, png_ptr->background.blue);
  3639. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  3640. *(sp + 5) = (png_byte)(v & 0xff);
  3641. }
  3642. }
  3643. }
  3644. }
  3645. break;
  3646. }
  3647. default:
  3648. break;
  3649. }
  3650. }
  3651. }
  3652. #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_READ_ALPHA_MODE_SUPPORTED */
  3653. #ifdef PNG_READ_GAMMA_SUPPORTED
  3654. /* Gamma correct the image, avoiding the alpha channel. Make sure
  3655. * you do this after you deal with the transparency issue on grayscale
  3656. * or RGB images. If your bit depth is 8, use gamma_table, if it
  3657. * is 16, use gamma_16_table and gamma_shift. Build these with
  3658. * build_gamma_table().
  3659. */
  3660. void /* PRIVATE */
  3661. png_do_gamma(png_row_infop row_info, png_bytep row, png_structp png_ptr)
  3662. {
  3663. png_const_bytep gamma_table = png_ptr->gamma_table;
  3664. png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table;
  3665. int gamma_shift = png_ptr->gamma_shift;
  3666. png_bytep sp;
  3667. png_uint_32 i;
  3668. png_uint_32 row_width=row_info->width;
  3669. png_debug(1, "in png_do_gamma");
  3670. if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
  3671. (row_info->bit_depth == 16 && gamma_16_table != NULL)))
  3672. {
  3673. switch (row_info->color_type)
  3674. {
  3675. case PNG_COLOR_TYPE_RGB:
  3676. {
  3677. if (row_info->bit_depth == 8)
  3678. {
  3679. sp = row;
  3680. for (i = 0; i < row_width; i++)
  3681. {
  3682. *sp = gamma_table[*sp];
  3683. sp++;
  3684. *sp = gamma_table[*sp];
  3685. sp++;
  3686. *sp = gamma_table[*sp];
  3687. sp++;
  3688. }
  3689. }
  3690. else /* if (row_info->bit_depth == 16) */
  3691. {
  3692. sp = row;
  3693. for (i = 0; i < row_width; i++)
  3694. {
  3695. png_uint_16 v;
  3696. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3697. *sp = (png_byte)((v >> 8) & 0xff);
  3698. *(sp + 1) = (png_byte)(v & 0xff);
  3699. sp += 2;
  3700. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3701. *sp = (png_byte)((v >> 8) & 0xff);
  3702. *(sp + 1) = (png_byte)(v & 0xff);
  3703. sp += 2;
  3704. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3705. *sp = (png_byte)((v >> 8) & 0xff);
  3706. *(sp + 1) = (png_byte)(v & 0xff);
  3707. sp += 2;
  3708. }
  3709. }
  3710. break;
  3711. }
  3712. case PNG_COLOR_TYPE_RGB_ALPHA:
  3713. {
  3714. if (row_info->bit_depth == 8)
  3715. {
  3716. sp = row;
  3717. for (i = 0; i < row_width; i++)
  3718. {
  3719. *sp = gamma_table[*sp];
  3720. sp++;
  3721. *sp = gamma_table[*sp];
  3722. sp++;
  3723. *sp = gamma_table[*sp];
  3724. sp++;
  3725. sp++;
  3726. }
  3727. }
  3728. else /* if (row_info->bit_depth == 16) */
  3729. {
  3730. sp = row;
  3731. for (i = 0; i < row_width; i++)
  3732. {
  3733. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3734. *sp = (png_byte)((v >> 8) & 0xff);
  3735. *(sp + 1) = (png_byte)(v & 0xff);
  3736. sp += 2;
  3737. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3738. *sp = (png_byte)((v >> 8) & 0xff);
  3739. *(sp + 1) = (png_byte)(v & 0xff);
  3740. sp += 2;
  3741. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3742. *sp = (png_byte)((v >> 8) & 0xff);
  3743. *(sp + 1) = (png_byte)(v & 0xff);
  3744. sp += 4;
  3745. }
  3746. }
  3747. break;
  3748. }
  3749. case PNG_COLOR_TYPE_GRAY_ALPHA:
  3750. {
  3751. if (row_info->bit_depth == 8)
  3752. {
  3753. sp = row;
  3754. for (i = 0; i < row_width; i++)
  3755. {
  3756. *sp = gamma_table[*sp];
  3757. sp += 2;
  3758. }
  3759. }
  3760. else /* if (row_info->bit_depth == 16) */
  3761. {
  3762. sp = row;
  3763. for (i = 0; i < row_width; i++)
  3764. {
  3765. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3766. *sp = (png_byte)((v >> 8) & 0xff);
  3767. *(sp + 1) = (png_byte)(v & 0xff);
  3768. sp += 4;
  3769. }
  3770. }
  3771. break;
  3772. }
  3773. case PNG_COLOR_TYPE_GRAY:
  3774. {
  3775. if (row_info->bit_depth == 2)
  3776. {
  3777. sp = row;
  3778. for (i = 0; i < row_width; i += 4)
  3779. {
  3780. int a = *sp & 0xc0;
  3781. int b = *sp & 0x30;
  3782. int c = *sp & 0x0c;
  3783. int d = *sp & 0x03;
  3784. *sp = (png_byte)(
  3785. ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
  3786. ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
  3787. ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
  3788. ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
  3789. sp++;
  3790. }
  3791. }
  3792. if (row_info->bit_depth == 4)
  3793. {
  3794. sp = row;
  3795. for (i = 0; i < row_width; i += 2)
  3796. {
  3797. int msb = *sp & 0xf0;
  3798. int lsb = *sp & 0x0f;
  3799. *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
  3800. | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
  3801. sp++;
  3802. }
  3803. }
  3804. else if (row_info->bit_depth == 8)
  3805. {
  3806. sp = row;
  3807. for (i = 0; i < row_width; i++)
  3808. {
  3809. *sp = gamma_table[*sp];
  3810. sp++;
  3811. }
  3812. }
  3813. else if (row_info->bit_depth == 16)
  3814. {
  3815. sp = row;
  3816. for (i = 0; i < row_width; i++)
  3817. {
  3818. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3819. *sp = (png_byte)((v >> 8) & 0xff);
  3820. *(sp + 1) = (png_byte)(v & 0xff);
  3821. sp += 2;
  3822. }
  3823. }
  3824. break;
  3825. }
  3826. default:
  3827. break;
  3828. }
  3829. }
  3830. }
  3831. #endif
  3832. #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  3833. /* Encode the alpha channel to the output gamma (the input channel is always
  3834. * linear.) Called only with color types that have an alpha channel. Needs the
  3835. * from_1 tables.
  3836. */
  3837. void /* PRIVATE */
  3838. png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structp png_ptr)
  3839. {
  3840. png_uint_32 row_width = row_info->width;
  3841. png_debug(1, "in png_do_encode_alpha");
  3842. if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
  3843. {
  3844. if (row_info->bit_depth == 8)
  3845. {
  3846. PNG_CONST png_bytep table = png_ptr->gamma_from_1;
  3847. if (table != NULL)
  3848. {
  3849. PNG_CONST int step =
  3850. (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2;
  3851. /* The alpha channel is the last component: */
  3852. row += step - 1;
  3853. for (; row_width > 0; --row_width, row += step)
  3854. *row = table[*row];
  3855. return;
  3856. }
  3857. }
  3858. else if (row_info->bit_depth == 16)
  3859. {
  3860. PNG_CONST png_uint_16pp table = png_ptr->gamma_16_from_1;
  3861. PNG_CONST int gamma_shift = png_ptr->gamma_shift;
  3862. if (table != NULL)
  3863. {
  3864. PNG_CONST int step =
  3865. (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4;
  3866. /* The alpha channel is the last component: */
  3867. row += step - 2;
  3868. for (; row_width > 0; --row_width, row += step)
  3869. {
  3870. png_uint_16 v;
  3871. v = table[*(row + 1) >> gamma_shift][*row];
  3872. *row = (png_byte)((v >> 8) & 0xff);
  3873. *(row + 1) = (png_byte)(v & 0xff);
  3874. }
  3875. return;
  3876. }
  3877. }
  3878. }
  3879. /* Only get to here if called with a weird row_info; no harm has been done,
  3880. * so just issue a warning.
  3881. */
  3882. png_warning(png_ptr, "png_do_encode_alpha: unexpected call");
  3883. }
  3884. #endif
  3885. #ifdef PNG_READ_EXPAND_SUPPORTED
  3886. /* Expands a palette row to an RGB or RGBA row depending
  3887. * upon whether you supply trans and num_trans.
  3888. */
  3889. void /* PRIVATE */
  3890. png_do_expand_palette(png_row_infop row_info, png_bytep row,
  3891. png_const_colorp palette, png_const_bytep trans_alpha, int num_trans)
  3892. {
  3893. int shift, value;
  3894. png_bytep sp, dp;
  3895. png_uint_32 i;
  3896. png_uint_32 row_width=row_info->width;
  3897. png_debug(1, "in png_do_expand_palette");
  3898. if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
  3899. {
  3900. if (row_info->bit_depth < 8)
  3901. {
  3902. switch (row_info->bit_depth)
  3903. {
  3904. case 1:
  3905. {
  3906. sp = row + (png_size_t)((row_width - 1) >> 3);
  3907. dp = row + (png_size_t)row_width - 1;
  3908. shift = 7 - (int)((row_width + 7) & 0x07);
  3909. for (i = 0; i < row_width; i++)
  3910. {
  3911. if ((*sp >> shift) & 0x01)
  3912. *dp = 1;
  3913. else
  3914. *dp = 0;
  3915. if (shift == 7)
  3916. {
  3917. shift = 0;
  3918. sp--;
  3919. }
  3920. else
  3921. shift++;
  3922. dp--;
  3923. }
  3924. break;
  3925. }
  3926. case 2:
  3927. {
  3928. sp = row + (png_size_t)((row_width - 1) >> 2);
  3929. dp = row + (png_size_t)row_width - 1;
  3930. shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  3931. for (i = 0; i < row_width; i++)
  3932. {
  3933. value = (*sp >> shift) & 0x03;
  3934. *dp = (png_byte)value;
  3935. if (shift == 6)
  3936. {
  3937. shift = 0;
  3938. sp--;
  3939. }
  3940. else
  3941. shift += 2;
  3942. dp--;
  3943. }
  3944. break;
  3945. }
  3946. case 4:
  3947. {
  3948. sp = row + (png_size_t)((row_width - 1) >> 1);
  3949. dp = row + (png_size_t)row_width - 1;
  3950. shift = (int)((row_width & 0x01) << 2);
  3951. for (i = 0; i < row_width; i++)
  3952. {
  3953. value = (*sp >> shift) & 0x0f;
  3954. *dp = (png_byte)value;
  3955. if (shift == 4)
  3956. {
  3957. shift = 0;
  3958. sp--;
  3959. }
  3960. else
  3961. shift += 4;
  3962. dp--;
  3963. }
  3964. break;
  3965. }
  3966. default:
  3967. break;
  3968. }
  3969. row_info->bit_depth = 8;
  3970. row_info->pixel_depth = 8;
  3971. row_info->rowbytes = row_width;
  3972. }
  3973. if (row_info->bit_depth == 8)
  3974. {
  3975. {
  3976. if (num_trans > 0)
  3977. {
  3978. sp = row + (png_size_t)row_width - 1;
  3979. dp = row + (png_size_t)(row_width << 2) - 1;
  3980. for (i = 0; i < row_width; i++)
  3981. {
  3982. if ((int)(*sp) >= num_trans)
  3983. *dp-- = 0xff;
  3984. else
  3985. *dp-- = trans_alpha[*sp];
  3986. *dp-- = palette[*sp].blue;
  3987. *dp-- = palette[*sp].green;
  3988. *dp-- = palette[*sp].red;
  3989. sp--;
  3990. }
  3991. row_info->bit_depth = 8;
  3992. row_info->pixel_depth = 32;
  3993. row_info->rowbytes = row_width * 4;
  3994. row_info->color_type = 6;
  3995. row_info->channels = 4;
  3996. }
  3997. else
  3998. {
  3999. sp = row + (png_size_t)row_width - 1;
  4000. dp = row + (png_size_t)(row_width * 3) - 1;
  4001. for (i = 0; i < row_width; i++)
  4002. {
  4003. *dp-- = palette[*sp].blue;
  4004. *dp-- = palette[*sp].green;
  4005. *dp-- = palette[*sp].red;
  4006. sp--;
  4007. }
  4008. row_info->bit_depth = 8;
  4009. row_info->pixel_depth = 24;
  4010. row_info->rowbytes = row_width * 3;
  4011. row_info->color_type = 2;
  4012. row_info->channels = 3;
  4013. }
  4014. }
  4015. }
  4016. }
  4017. }
  4018. /* If the bit depth < 8, it is expanded to 8. Also, if the already
  4019. * expanded transparency value is supplied, an alpha channel is built.
  4020. */
  4021. void /* PRIVATE */
  4022. png_do_expand(png_row_infop row_info, png_bytep row,
  4023. png_const_color_16p trans_color)
  4024. {
  4025. int shift, value;
  4026. png_bytep sp, dp;
  4027. png_uint_32 i;
  4028. png_uint_32 row_width=row_info->width;
  4029. png_debug(1, "in png_do_expand");
  4030. {
  4031. if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  4032. {
  4033. png_uint_16 gray = (png_uint_16)(trans_color ? trans_color->gray : 0);
  4034. if (row_info->bit_depth < 8)
  4035. {
  4036. switch (row_info->bit_depth)
  4037. {
  4038. case 1:
  4039. {
  4040. gray = (png_uint_16)((gray & 0x01) * 0xff);
  4041. sp = row + (png_size_t)((row_width - 1) >> 3);
  4042. dp = row + (png_size_t)row_width - 1;
  4043. shift = 7 - (int)((row_width + 7) & 0x07);
  4044. for (i = 0; i < row_width; i++)
  4045. {
  4046. if ((*sp >> shift) & 0x01)
  4047. *dp = 0xff;
  4048. else
  4049. *dp = 0;
  4050. if (shift == 7)
  4051. {
  4052. shift = 0;
  4053. sp--;
  4054. }
  4055. else
  4056. shift++;
  4057. dp--;
  4058. }
  4059. break;
  4060. }
  4061. case 2:
  4062. {
  4063. gray = (png_uint_16)((gray & 0x03) * 0x55);
  4064. sp = row + (png_size_t)((row_width - 1) >> 2);
  4065. dp = row + (png_size_t)row_width - 1;
  4066. shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  4067. for (i = 0; i < row_width; i++)
  4068. {
  4069. value = (*sp >> shift) & 0x03;
  4070. *dp = (png_byte)(value | (value << 2) | (value << 4) |
  4071. (value << 6));
  4072. if (shift == 6)
  4073. {
  4074. shift = 0;
  4075. sp--;
  4076. }
  4077. else
  4078. shift += 2;
  4079. dp--;
  4080. }
  4081. break;
  4082. }
  4083. case 4:
  4084. {
  4085. gray = (png_uint_16)((gray & 0x0f) * 0x11);
  4086. sp = row + (png_size_t)((row_width - 1) >> 1);
  4087. dp = row + (png_size_t)row_width - 1;
  4088. shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
  4089. for (i = 0; i < row_width; i++)
  4090. {
  4091. value = (*sp >> shift) & 0x0f;
  4092. *dp = (png_byte)(value | (value << 4));
  4093. if (shift == 4)
  4094. {
  4095. shift = 0;
  4096. sp--;
  4097. }
  4098. else
  4099. shift = 4;
  4100. dp--;
  4101. }
  4102. break;
  4103. }
  4104. default:
  4105. break;
  4106. }
  4107. row_info->bit_depth = 8;
  4108. row_info->pixel_depth = 8;
  4109. row_info->rowbytes = row_width;
  4110. }
  4111. if (trans_color != NULL)
  4112. {
  4113. if (row_info->bit_depth == 8)
  4114. {
  4115. gray = gray & 0xff;
  4116. sp = row + (png_size_t)row_width - 1;
  4117. dp = row + (png_size_t)(row_width << 1) - 1;
  4118. for (i = 0; i < row_width; i++)
  4119. {
  4120. if (*sp == gray)
  4121. *dp-- = 0;
  4122. else
  4123. *dp-- = 0xff;
  4124. *dp-- = *sp--;
  4125. }
  4126. }
  4127. else if (row_info->bit_depth == 16)
  4128. {
  4129. png_byte gray_high = (png_byte)((gray >> 8) & 0xff);
  4130. png_byte gray_low = (png_byte)(gray & 0xff);
  4131. sp = row + row_info->rowbytes - 1;
  4132. dp = row + (row_info->rowbytes << 1) - 1;
  4133. for (i = 0; i < row_width; i++)
  4134. {
  4135. if (*(sp - 1) == gray_high && *(sp) == gray_low)
  4136. {
  4137. *dp-- = 0;
  4138. *dp-- = 0;
  4139. }
  4140. else
  4141. {
  4142. *dp-- = 0xff;
  4143. *dp-- = 0xff;
  4144. }
  4145. *dp-- = *sp--;
  4146. *dp-- = *sp--;
  4147. }
  4148. }
  4149. row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
  4150. row_info->channels = 2;
  4151. row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
  4152. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
  4153. row_width);
  4154. }
  4155. }
  4156. else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_color)
  4157. {
  4158. if (row_info->bit_depth == 8)
  4159. {
  4160. png_byte red = (png_byte)(trans_color->red & 0xff);
  4161. png_byte green = (png_byte)(trans_color->green & 0xff);
  4162. png_byte blue = (png_byte)(trans_color->blue & 0xff);
  4163. sp = row + (png_size_t)row_info->rowbytes - 1;
  4164. dp = row + (png_size_t)(row_width << 2) - 1;
  4165. for (i = 0; i < row_width; i++)
  4166. {
  4167. if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
  4168. *dp-- = 0;
  4169. else
  4170. *dp-- = 0xff;
  4171. *dp-- = *sp--;
  4172. *dp-- = *sp--;
  4173. *dp-- = *sp--;
  4174. }
  4175. }
  4176. else if (row_info->bit_depth == 16)
  4177. {
  4178. png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff);
  4179. png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff);
  4180. png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff);
  4181. png_byte red_low = (png_byte)(trans_color->red & 0xff);
  4182. png_byte green_low = (png_byte)(trans_color->green & 0xff);
  4183. png_byte blue_low = (png_byte)(trans_color->blue & 0xff);
  4184. sp = row + row_info->rowbytes - 1;
  4185. dp = row + (png_size_t)(row_width << 3) - 1;
  4186. for (i = 0; i < row_width; i++)
  4187. {
  4188. if (*(sp - 5) == red_high &&
  4189. *(sp - 4) == red_low &&
  4190. *(sp - 3) == green_high &&
  4191. *(sp - 2) == green_low &&
  4192. *(sp - 1) == blue_high &&
  4193. *(sp ) == blue_low)
  4194. {
  4195. *dp-- = 0;
  4196. *dp-- = 0;
  4197. }
  4198. else
  4199. {
  4200. *dp-- = 0xff;
  4201. *dp-- = 0xff;
  4202. }
  4203. *dp-- = *sp--;
  4204. *dp-- = *sp--;
  4205. *dp-- = *sp--;
  4206. *dp-- = *sp--;
  4207. *dp-- = *sp--;
  4208. *dp-- = *sp--;
  4209. }
  4210. }
  4211. row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  4212. row_info->channels = 4;
  4213. row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
  4214. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  4215. }
  4216. }
  4217. }
  4218. #endif
  4219. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  4220. /* If the bit depth is 8 and the color type is not a palette type expand the
  4221. * whole row to 16 bits. Has no effect otherwise.
  4222. */
  4223. void /* PRIVATE */
  4224. png_do_expand_16(png_row_infop row_info, png_bytep row)
  4225. {
  4226. if (row_info->bit_depth == 8 &&
  4227. row_info->color_type != PNG_COLOR_TYPE_PALETTE)
  4228. {
  4229. /* The row have a sequence of bytes containing [0..255] and we need
  4230. * to turn it into another row containing [0..65535], to do this we
  4231. * calculate:
  4232. *
  4233. * (input / 255) * 65535
  4234. *
  4235. * Which happens to be exactly input * 257 and this can be achieved
  4236. * simply by byte replication in place (copying backwards).
  4237. */
  4238. png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
  4239. png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */
  4240. while (dp > sp)
  4241. dp[-2] = dp[-1] = *--sp, dp -= 2;
  4242. row_info->rowbytes *= 2;
  4243. row_info->bit_depth = 16;
  4244. row_info->pixel_depth = (png_byte)(row_info->channels * 16);
  4245. }
  4246. }
  4247. #endif
  4248. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  4249. void /* PRIVATE */
  4250. png_do_quantize(png_row_infop row_info, png_bytep row,
  4251. png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
  4252. {
  4253. png_bytep sp, dp;
  4254. png_uint_32 i;
  4255. png_uint_32 row_width=row_info->width;
  4256. png_debug(1, "in png_do_quantize");
  4257. if (row_info->bit_depth == 8)
  4258. {
  4259. if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
  4260. {
  4261. int r, g, b, p;
  4262. sp = row;
  4263. dp = row;
  4264. for (i = 0; i < row_width; i++)
  4265. {
  4266. r = *sp++;
  4267. g = *sp++;
  4268. b = *sp++;
  4269. /* This looks real messy, but the compiler will reduce
  4270. * it down to a reasonable formula. For example, with
  4271. * 5 bits per color, we get:
  4272. * p = (((r >> 3) & 0x1f) << 10) |
  4273. * (((g >> 3) & 0x1f) << 5) |
  4274. * ((b >> 3) & 0x1f);
  4275. */
  4276. p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
  4277. ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
  4278. (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
  4279. (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
  4280. ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
  4281. (PNG_QUANTIZE_BLUE_BITS)) |
  4282. ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
  4283. ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
  4284. *dp++ = palette_lookup[p];
  4285. }
  4286. row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  4287. row_info->channels = 1;
  4288. row_info->pixel_depth = row_info->bit_depth;
  4289. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  4290. }
  4291. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  4292. palette_lookup != NULL)
  4293. {
  4294. int r, g, b, p;
  4295. sp = row;
  4296. dp = row;
  4297. for (i = 0; i < row_width; i++)
  4298. {
  4299. r = *sp++;
  4300. g = *sp++;
  4301. b = *sp++;
  4302. sp++;
  4303. p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
  4304. ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
  4305. (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
  4306. (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
  4307. ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
  4308. (PNG_QUANTIZE_BLUE_BITS)) |
  4309. ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
  4310. ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
  4311. *dp++ = palette_lookup[p];
  4312. }
  4313. row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  4314. row_info->channels = 1;
  4315. row_info->pixel_depth = row_info->bit_depth;
  4316. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  4317. }
  4318. else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
  4319. quantize_lookup)
  4320. {
  4321. sp = row;
  4322. for (i = 0; i < row_width; i++, sp++)
  4323. {
  4324. *sp = quantize_lookup[*sp];
  4325. }
  4326. }
  4327. }
  4328. }
  4329. #endif /* PNG_READ_QUANTIZE_SUPPORTED */
  4330. #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
  4331. #ifdef PNG_MNG_FEATURES_SUPPORTED
  4332. /* Undoes intrapixel differencing */
  4333. void /* PRIVATE */
  4334. png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
  4335. {
  4336. png_debug(1, "in png_do_read_intrapixel");
  4337. if (
  4338. (row_info->color_type & PNG_COLOR_MASK_COLOR))
  4339. {
  4340. int bytes_per_pixel;
  4341. png_uint_32 row_width = row_info->width;
  4342. if (row_info->bit_depth == 8)
  4343. {
  4344. png_bytep rp;
  4345. png_uint_32 i;
  4346. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  4347. bytes_per_pixel = 3;
  4348. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  4349. bytes_per_pixel = 4;
  4350. else
  4351. return;
  4352. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  4353. {
  4354. *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
  4355. *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
  4356. }
  4357. }
  4358. else if (row_info->bit_depth == 16)
  4359. {
  4360. png_bytep rp;
  4361. png_uint_32 i;
  4362. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  4363. bytes_per_pixel = 6;
  4364. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  4365. bytes_per_pixel = 8;
  4366. else
  4367. return;
  4368. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  4369. {
  4370. png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
  4371. png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
  4372. png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
  4373. png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
  4374. png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
  4375. *(rp ) = (png_byte)((red >> 8) & 0xff);
  4376. *(rp + 1) = (png_byte)(red & 0xff);
  4377. *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
  4378. *(rp + 5) = (png_byte)(blue & 0xff);
  4379. }
  4380. }
  4381. }
  4382. }
  4383. #endif /* PNG_MNG_FEATURES_SUPPORTED */
  4384. #endif /* PNG_READ_SUPPORTED */