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

https://bitbucket.org/cabalistic/ogredeps/ · C · 5023 lines · 3620 code · 639 blank · 764 comment · 778 complexity · ce6789b8366bc3c5861c3acda0174527 MD5 · raw file

Large files are truncated click here to view the full file

  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. *…