istereo

annotate libs/libpng/pngrtran.c @ 26:862a3329a8f0

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