dbf-halloween2015

annotate libs/libpng/pngrtran.c @ 1:c3f5c32cb210

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