PageRenderTime 78ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/cximage-6.0/png/pngrtran.c

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