istereo2

annotate libs/libpng/pngrtran.c @ 20:2b85d05df3f2

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