3dphotoshoot

annotate libs/libpng/pngrtran.c @ 14:06dc8b9b4f89

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