nuclear@26: nuclear@26: /* pngrtran.c - transforms the data in a row for PNG readers nuclear@26: * nuclear@26: * Last changed in libpng 1.2.30 [August 15, 2008] nuclear@26: * For conditions of distribution and use, see copyright notice in png.h nuclear@26: * Copyright (c) 1998-2008 Glenn Randers-Pehrson nuclear@26: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) nuclear@26: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) nuclear@26: * nuclear@26: * This file contains functions optionally called by an application nuclear@26: * in order to tell libpng how to handle data when reading a PNG. nuclear@26: * Transformations that are used in both reading and writing are nuclear@26: * in pngtrans.c. nuclear@26: */ nuclear@26: nuclear@26: #define PNG_INTERNAL nuclear@26: #include "png.h" nuclear@26: #if defined(PNG_READ_SUPPORTED) nuclear@26: nuclear@26: /* Set the action on getting a CRC error for an ancillary or critical chunk. */ nuclear@26: void PNGAPI nuclear@26: png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action) nuclear@26: { nuclear@26: png_debug(1, "in png_set_crc_action\n"); nuclear@26: /* Tell libpng how we react to CRC errors in critical chunks */ nuclear@26: if (png_ptr == NULL) return; nuclear@26: switch (crit_action) nuclear@26: { nuclear@26: case PNG_CRC_NO_CHANGE: /* leave setting as is */ nuclear@26: break; nuclear@26: case PNG_CRC_WARN_USE: /* warn/use data */ nuclear@26: png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; nuclear@26: png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; nuclear@26: break; nuclear@26: case PNG_CRC_QUIET_USE: /* quiet/use data */ nuclear@26: png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; nuclear@26: png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | nuclear@26: PNG_FLAG_CRC_CRITICAL_IGNORE; nuclear@26: break; nuclear@26: case PNG_CRC_WARN_DISCARD: /* not a valid action for critical data */ nuclear@26: png_warning(png_ptr, nuclear@26: "Can't discard critical data on CRC error."); nuclear@26: case PNG_CRC_ERROR_QUIT: /* error/quit */ nuclear@26: case PNG_CRC_DEFAULT: nuclear@26: default: nuclear@26: png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; nuclear@26: break; nuclear@26: } nuclear@26: nuclear@26: switch (ancil_action) nuclear@26: { nuclear@26: case PNG_CRC_NO_CHANGE: /* leave setting as is */ nuclear@26: break; nuclear@26: case PNG_CRC_WARN_USE: /* warn/use data */ nuclear@26: png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; nuclear@26: png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; nuclear@26: break; nuclear@26: case PNG_CRC_QUIET_USE: /* quiet/use data */ nuclear@26: png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; nuclear@26: png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | nuclear@26: PNG_FLAG_CRC_ANCILLARY_NOWARN; nuclear@26: break; nuclear@26: case PNG_CRC_ERROR_QUIT: /* error/quit */ nuclear@26: png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; nuclear@26: png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; nuclear@26: break; nuclear@26: case PNG_CRC_WARN_DISCARD: /* warn/discard data */ nuclear@26: case PNG_CRC_DEFAULT: nuclear@26: default: nuclear@26: png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ nuclear@26: defined(PNG_FLOATING_POINT_SUPPORTED) nuclear@26: /* handle alpha and tRNS via a background color */ nuclear@26: void PNGAPI nuclear@26: png_set_background(png_structp png_ptr, nuclear@26: png_color_16p background_color, int background_gamma_code, nuclear@26: int need_expand, double background_gamma) nuclear@26: { nuclear@26: png_debug(1, "in png_set_background\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) nuclear@26: { nuclear@26: png_warning(png_ptr, "Application must supply a known background gamma"); nuclear@26: return; nuclear@26: } nuclear@26: nuclear@26: png_ptr->transformations |= PNG_BACKGROUND; nuclear@26: png_memcpy(&(png_ptr->background), background_color, nuclear@26: png_sizeof(png_color_16)); nuclear@26: png_ptr->background_gamma = (float)background_gamma; nuclear@26: png_ptr->background_gamma_type = (png_byte)(background_gamma_code); nuclear@26: png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0); nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_16_TO_8_SUPPORTED) nuclear@26: /* strip 16 bit depth files to 8 bit depth */ nuclear@26: void PNGAPI nuclear@26: png_set_strip_16(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_set_strip_16\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: png_ptr->transformations |= PNG_16_TO_8; nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) nuclear@26: void PNGAPI nuclear@26: png_set_strip_alpha(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_set_strip_alpha\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: png_ptr->flags |= PNG_FLAG_STRIP_ALPHA; nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_DITHER_SUPPORTED) nuclear@26: /* Dither file to 8 bit. Supply a palette, the current number nuclear@26: * of elements in the palette, the maximum number of elements nuclear@26: * allowed, and a histogram if possible. If the current number nuclear@26: * of colors is greater then the maximum number, the palette will be nuclear@26: * modified to fit in the maximum number. "full_dither" indicates nuclear@26: * whether we need a dithering cube set up for RGB images, or if we nuclear@26: * simply are reducing the number of colors in a paletted image. nuclear@26: */ nuclear@26: nuclear@26: typedef struct png_dsort_struct nuclear@26: { nuclear@26: struct png_dsort_struct FAR * next; nuclear@26: png_byte left; nuclear@26: png_byte right; nuclear@26: } png_dsort; nuclear@26: typedef png_dsort FAR * png_dsortp; nuclear@26: typedef png_dsort FAR * FAR * png_dsortpp; nuclear@26: nuclear@26: void PNGAPI nuclear@26: png_set_dither(png_structp png_ptr, png_colorp palette, nuclear@26: int num_palette, int maximum_colors, png_uint_16p histogram, nuclear@26: int full_dither) nuclear@26: { nuclear@26: png_debug(1, "in png_set_dither\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: png_ptr->transformations |= PNG_DITHER; nuclear@26: nuclear@26: if (!full_dither) nuclear@26: { nuclear@26: int i; nuclear@26: nuclear@26: png_ptr->dither_index = (png_bytep)png_malloc(png_ptr, nuclear@26: (png_uint_32)(num_palette * png_sizeof(png_byte))); nuclear@26: for (i = 0; i < num_palette; i++) nuclear@26: png_ptr->dither_index[i] = (png_byte)i; nuclear@26: } nuclear@26: nuclear@26: if (num_palette > maximum_colors) nuclear@26: { nuclear@26: if (histogram != NULL) nuclear@26: { nuclear@26: /* This is easy enough, just throw out the least used colors. nuclear@26: Perhaps not the best solution, but good enough. */ nuclear@26: nuclear@26: int i; nuclear@26: nuclear@26: /* initialize an array to sort colors */ nuclear@26: png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr, nuclear@26: (png_uint_32)(num_palette * png_sizeof(png_byte))); nuclear@26: nuclear@26: /* initialize the dither_sort array */ nuclear@26: for (i = 0; i < num_palette; i++) nuclear@26: png_ptr->dither_sort[i] = (png_byte)i; nuclear@26: nuclear@26: /* Find the least used palette entries by starting a nuclear@26: bubble sort, and running it until we have sorted nuclear@26: out enough colors. Note that we don't care about nuclear@26: sorting all the colors, just finding which are nuclear@26: least used. */ nuclear@26: nuclear@26: for (i = num_palette - 1; i >= maximum_colors; i--) nuclear@26: { nuclear@26: int done; /* to stop early if the list is pre-sorted */ nuclear@26: int j; nuclear@26: nuclear@26: done = 1; nuclear@26: for (j = 0; j < i; j++) nuclear@26: { nuclear@26: if (histogram[png_ptr->dither_sort[j]] nuclear@26: < histogram[png_ptr->dither_sort[j + 1]]) nuclear@26: { nuclear@26: png_byte t; nuclear@26: nuclear@26: t = png_ptr->dither_sort[j]; nuclear@26: png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1]; nuclear@26: png_ptr->dither_sort[j + 1] = t; nuclear@26: done = 0; nuclear@26: } nuclear@26: } nuclear@26: if (done) nuclear@26: break; nuclear@26: } nuclear@26: nuclear@26: /* swap the palette around, and set up a table, if necessary */ nuclear@26: if (full_dither) nuclear@26: { nuclear@26: int j = num_palette; nuclear@26: nuclear@26: /* put all the useful colors within the max, but don't nuclear@26: move the others */ nuclear@26: for (i = 0; i < maximum_colors; i++) nuclear@26: { nuclear@26: if ((int)png_ptr->dither_sort[i] >= maximum_colors) nuclear@26: { nuclear@26: do nuclear@26: j--; nuclear@26: while ((int)png_ptr->dither_sort[j] >= maximum_colors); nuclear@26: palette[i] = palette[j]; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: int j = num_palette; nuclear@26: nuclear@26: /* move all the used colors inside the max limit, and nuclear@26: develop a translation table */ nuclear@26: for (i = 0; i < maximum_colors; i++) nuclear@26: { nuclear@26: /* only move the colors we need to */ nuclear@26: if ((int)png_ptr->dither_sort[i] >= maximum_colors) nuclear@26: { nuclear@26: png_color tmp_color; nuclear@26: nuclear@26: do nuclear@26: j--; nuclear@26: while ((int)png_ptr->dither_sort[j] >= maximum_colors); nuclear@26: nuclear@26: tmp_color = palette[j]; nuclear@26: palette[j] = palette[i]; nuclear@26: palette[i] = tmp_color; nuclear@26: /* indicate where the color went */ nuclear@26: png_ptr->dither_index[j] = (png_byte)i; nuclear@26: png_ptr->dither_index[i] = (png_byte)j; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: /* find closest color for those colors we are not using */ nuclear@26: for (i = 0; i < num_palette; i++) nuclear@26: { nuclear@26: if ((int)png_ptr->dither_index[i] >= maximum_colors) nuclear@26: { nuclear@26: int min_d, k, min_k, d_index; nuclear@26: nuclear@26: /* find the closest color to one we threw out */ nuclear@26: d_index = png_ptr->dither_index[i]; nuclear@26: min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); nuclear@26: for (k = 1, min_k = 0; k < maximum_colors; k++) nuclear@26: { nuclear@26: int d; nuclear@26: nuclear@26: d = PNG_COLOR_DIST(palette[d_index], palette[k]); nuclear@26: nuclear@26: if (d < min_d) nuclear@26: { nuclear@26: min_d = d; nuclear@26: min_k = k; nuclear@26: } nuclear@26: } nuclear@26: /* point to closest color */ nuclear@26: png_ptr->dither_index[i] = (png_byte)min_k; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: png_free(png_ptr, png_ptr->dither_sort); nuclear@26: png_ptr->dither_sort = NULL; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: /* This is much harder to do simply (and quickly). Perhaps nuclear@26: we need to go through a median cut routine, but those nuclear@26: don't always behave themselves with only a few colors nuclear@26: as input. So we will just find the closest two colors, nuclear@26: and throw out one of them (chosen somewhat randomly). nuclear@26: [We don't understand this at all, so if someone wants to nuclear@26: work on improving it, be our guest - AED, GRP] nuclear@26: */ nuclear@26: int i; nuclear@26: int max_d; nuclear@26: int num_new_palette; nuclear@26: png_dsortp t; nuclear@26: png_dsortpp hash; nuclear@26: nuclear@26: t = NULL; nuclear@26: nuclear@26: /* initialize palette index arrays */ nuclear@26: png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, nuclear@26: (png_uint_32)(num_palette * png_sizeof(png_byte))); nuclear@26: png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, nuclear@26: (png_uint_32)(num_palette * png_sizeof(png_byte))); nuclear@26: nuclear@26: /* initialize the sort array */ nuclear@26: for (i = 0; i < num_palette; i++) nuclear@26: { nuclear@26: png_ptr->index_to_palette[i] = (png_byte)i; nuclear@26: png_ptr->palette_to_index[i] = (png_byte)i; nuclear@26: } nuclear@26: nuclear@26: hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 * nuclear@26: png_sizeof(png_dsortp))); nuclear@26: for (i = 0; i < 769; i++) nuclear@26: hash[i] = NULL; nuclear@26: /* png_memset(hash, 0, 769 * png_sizeof(png_dsortp)); */ nuclear@26: nuclear@26: num_new_palette = num_palette; nuclear@26: nuclear@26: /* initial wild guess at how far apart the farthest pixel nuclear@26: pair we will be eliminating will be. Larger nuclear@26: numbers mean more areas will be allocated, Smaller nuclear@26: numbers run the risk of not saving enough data, and nuclear@26: having to do this all over again. nuclear@26: nuclear@26: I have not done extensive checking on this number. nuclear@26: */ nuclear@26: max_d = 96; nuclear@26: nuclear@26: while (num_new_palette > maximum_colors) nuclear@26: { nuclear@26: for (i = 0; i < num_new_palette - 1; i++) nuclear@26: { nuclear@26: int j; nuclear@26: nuclear@26: for (j = i + 1; j < num_new_palette; j++) nuclear@26: { nuclear@26: int d; nuclear@26: nuclear@26: d = PNG_COLOR_DIST(palette[i], palette[j]); nuclear@26: nuclear@26: if (d <= max_d) nuclear@26: { nuclear@26: nuclear@26: t = (png_dsortp)png_malloc_warn(png_ptr, nuclear@26: (png_uint_32)(png_sizeof(png_dsort))); nuclear@26: if (t == NULL) nuclear@26: break; nuclear@26: t->next = hash[d]; nuclear@26: t->left = (png_byte)i; nuclear@26: t->right = (png_byte)j; nuclear@26: hash[d] = t; nuclear@26: } nuclear@26: } nuclear@26: if (t == NULL) nuclear@26: break; nuclear@26: } nuclear@26: nuclear@26: if (t != NULL) nuclear@26: for (i = 0; i <= max_d; i++) nuclear@26: { nuclear@26: if (hash[i] != NULL) nuclear@26: { nuclear@26: png_dsortp p; nuclear@26: nuclear@26: for (p = hash[i]; p; p = p->next) nuclear@26: { nuclear@26: if ((int)png_ptr->index_to_palette[p->left] nuclear@26: < num_new_palette && nuclear@26: (int)png_ptr->index_to_palette[p->right] nuclear@26: < num_new_palette) nuclear@26: { nuclear@26: int j, next_j; nuclear@26: nuclear@26: if (num_new_palette & 0x01) nuclear@26: { nuclear@26: j = p->left; nuclear@26: next_j = p->right; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: j = p->right; nuclear@26: next_j = p->left; nuclear@26: } nuclear@26: nuclear@26: num_new_palette--; nuclear@26: palette[png_ptr->index_to_palette[j]] nuclear@26: = palette[num_new_palette]; nuclear@26: if (!full_dither) nuclear@26: { nuclear@26: int k; nuclear@26: nuclear@26: for (k = 0; k < num_palette; k++) nuclear@26: { nuclear@26: if (png_ptr->dither_index[k] == nuclear@26: png_ptr->index_to_palette[j]) nuclear@26: png_ptr->dither_index[k] = nuclear@26: png_ptr->index_to_palette[next_j]; nuclear@26: if ((int)png_ptr->dither_index[k] == nuclear@26: num_new_palette) nuclear@26: png_ptr->dither_index[k] = nuclear@26: png_ptr->index_to_palette[j]; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: png_ptr->index_to_palette[png_ptr->palette_to_index nuclear@26: [num_new_palette]] = png_ptr->index_to_palette[j]; nuclear@26: png_ptr->palette_to_index[png_ptr->index_to_palette[j]] nuclear@26: = png_ptr->palette_to_index[num_new_palette]; nuclear@26: nuclear@26: png_ptr->index_to_palette[j] = (png_byte)num_new_palette; nuclear@26: png_ptr->palette_to_index[num_new_palette] = (png_byte)j; nuclear@26: } nuclear@26: if (num_new_palette <= maximum_colors) nuclear@26: break; nuclear@26: } nuclear@26: if (num_new_palette <= maximum_colors) nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: for (i = 0; i < 769; i++) nuclear@26: { nuclear@26: if (hash[i] != NULL) nuclear@26: { nuclear@26: png_dsortp p = hash[i]; nuclear@26: while (p) nuclear@26: { nuclear@26: t = p->next; nuclear@26: png_free(png_ptr, p); nuclear@26: p = t; nuclear@26: } nuclear@26: } nuclear@26: hash[i] = 0; nuclear@26: } nuclear@26: max_d += 96; nuclear@26: } nuclear@26: png_free(png_ptr, hash); nuclear@26: png_free(png_ptr, png_ptr->palette_to_index); nuclear@26: png_free(png_ptr, png_ptr->index_to_palette); nuclear@26: png_ptr->palette_to_index = NULL; nuclear@26: png_ptr->index_to_palette = NULL; nuclear@26: } nuclear@26: num_palette = maximum_colors; nuclear@26: } nuclear@26: if (png_ptr->palette == NULL) nuclear@26: { nuclear@26: png_ptr->palette = palette; nuclear@26: } nuclear@26: png_ptr->num_palette = (png_uint_16)num_palette; nuclear@26: nuclear@26: if (full_dither) nuclear@26: { nuclear@26: int i; nuclear@26: png_bytep distance; nuclear@26: int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS + nuclear@26: PNG_DITHER_BLUE_BITS; nuclear@26: int num_red = (1 << PNG_DITHER_RED_BITS); nuclear@26: int num_green = (1 << PNG_DITHER_GREEN_BITS); nuclear@26: int num_blue = (1 << PNG_DITHER_BLUE_BITS); nuclear@26: png_size_t num_entries = ((png_size_t)1 << total_bits); nuclear@26: nuclear@26: png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr, nuclear@26: (png_uint_32)(num_entries * png_sizeof(png_byte))); nuclear@26: nuclear@26: png_memset(png_ptr->palette_lookup, 0, num_entries * nuclear@26: png_sizeof(png_byte)); nuclear@26: nuclear@26: distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries * nuclear@26: png_sizeof(png_byte))); nuclear@26: nuclear@26: png_memset(distance, 0xff, num_entries * png_sizeof(png_byte)); nuclear@26: nuclear@26: for (i = 0; i < num_palette; i++) nuclear@26: { nuclear@26: int ir, ig, ib; nuclear@26: int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS)); nuclear@26: int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS)); nuclear@26: int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS)); nuclear@26: nuclear@26: for (ir = 0; ir < num_red; ir++) nuclear@26: { nuclear@26: /* int dr = abs(ir - r); */ nuclear@26: int dr = ((ir > r) ? ir - r : r - ir); nuclear@26: int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS)); nuclear@26: nuclear@26: for (ig = 0; ig < num_green; ig++) nuclear@26: { nuclear@26: /* int dg = abs(ig - g); */ nuclear@26: int dg = ((ig > g) ? ig - g : g - ig); nuclear@26: int dt = dr + dg; nuclear@26: int dm = ((dr > dg) ? dr : dg); nuclear@26: int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS); nuclear@26: nuclear@26: for (ib = 0; ib < num_blue; ib++) nuclear@26: { nuclear@26: int d_index = index_g | ib; nuclear@26: /* int db = abs(ib - b); */ nuclear@26: int db = ((ib > b) ? ib - b : b - ib); nuclear@26: int dmax = ((dm > db) ? dm : db); nuclear@26: int d = dmax + dt + db; nuclear@26: nuclear@26: if (d < (int)distance[d_index]) nuclear@26: { nuclear@26: distance[d_index] = (png_byte)d; nuclear@26: png_ptr->palette_lookup[d_index] = (png_byte)i; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: png_free(png_ptr, distance); nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) nuclear@26: /* Transform the image from the file_gamma to the screen_gamma. We nuclear@26: * only do transformations on images where the file_gamma and screen_gamma nuclear@26: * are not close reciprocals, otherwise it slows things down slightly, and nuclear@26: * also needlessly introduces small errors. nuclear@26: * nuclear@26: * We will turn off gamma transformation later if no semitransparent entries nuclear@26: * are present in the tRNS array for palette images. We can't do it here nuclear@26: * because we don't necessarily have the tRNS chunk yet. nuclear@26: */ nuclear@26: void PNGAPI nuclear@26: png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma) nuclear@26: { nuclear@26: png_debug(1, "in png_set_gamma\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) || nuclear@26: (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) || nuclear@26: (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) nuclear@26: png_ptr->transformations |= PNG_GAMMA; nuclear@26: png_ptr->gamma = (float)file_gamma; nuclear@26: png_ptr->screen_gamma = (float)scrn_gamma; nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_EXPAND_SUPPORTED) nuclear@26: /* Expand paletted images to RGB, expand grayscale images of nuclear@26: * less than 8-bit depth to 8-bit depth, and expand tRNS chunks nuclear@26: * to alpha channels. nuclear@26: */ nuclear@26: void PNGAPI nuclear@26: png_set_expand(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_set_expand\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); nuclear@26: png_ptr->flags &= ~PNG_FLAG_ROW_INIT; nuclear@26: } nuclear@26: nuclear@26: /* GRR 19990627: the following three functions currently are identical nuclear@26: * to png_set_expand(). However, it is entirely reasonable that someone nuclear@26: * might wish to expand an indexed image to RGB but *not* expand a single, nuclear@26: * fully transparent palette entry to a full alpha channel--perhaps instead nuclear@26: * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace nuclear@26: * the transparent color with a particular RGB value, or drop tRNS entirely. nuclear@26: * IOW, a future version of the library may make the transformations flag nuclear@26: * a bit more fine-grained, with separate bits for each of these three nuclear@26: * functions. nuclear@26: * nuclear@26: * More to the point, these functions make it obvious what libpng will be nuclear@26: * doing, whereas "expand" can (and does) mean any number of things. nuclear@26: * nuclear@26: * GRP 20060307: In libpng-1.4.0, png_set_gray_1_2_4_to_8() was modified nuclear@26: * to expand only the sample depth but not to expand the tRNS to alpha. nuclear@26: */ nuclear@26: nuclear@26: /* Expand paletted images to RGB. */ nuclear@26: void PNGAPI nuclear@26: png_set_palette_to_rgb(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_set_palette_to_rgb\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); nuclear@26: png_ptr->flags &= ~PNG_FLAG_ROW_INIT; nuclear@26: } nuclear@26: nuclear@26: #if !defined(PNG_1_0_X) nuclear@26: /* Expand grayscale images of less than 8-bit depth to 8 bits. */ nuclear@26: void PNGAPI nuclear@26: png_set_expand_gray_1_2_4_to_8(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_set_expand_gray_1_2_4_to_8\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: png_ptr->transformations |= PNG_EXPAND; nuclear@26: png_ptr->flags &= ~PNG_FLAG_ROW_INIT; nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_1_0_X) || defined(PNG_1_2_X) nuclear@26: /* Expand grayscale images of less than 8-bit depth to 8 bits. */ nuclear@26: /* Deprecated as of libpng-1.2.9 */ nuclear@26: void PNGAPI nuclear@26: png_set_gray_1_2_4_to_8(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_set_gray_1_2_4_to_8\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: nuclear@26: /* Expand tRNS chunks to alpha channels. */ nuclear@26: void PNGAPI nuclear@26: png_set_tRNS_to_alpha(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_set_tRNS_to_alpha\n"); nuclear@26: png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); nuclear@26: png_ptr->flags &= ~PNG_FLAG_ROW_INIT; nuclear@26: } nuclear@26: #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */ nuclear@26: nuclear@26: #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) nuclear@26: void PNGAPI nuclear@26: png_set_gray_to_rgb(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_set_gray_to_rgb\n"); nuclear@26: png_ptr->transformations |= PNG_GRAY_TO_RGB; nuclear@26: png_ptr->flags &= ~PNG_FLAG_ROW_INIT; nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) nuclear@26: #if defined(PNG_FLOATING_POINT_SUPPORTED) nuclear@26: /* Convert a RGB image to a grayscale of the same width. This allows us, nuclear@26: * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image. nuclear@26: */ nuclear@26: nuclear@26: void PNGAPI nuclear@26: png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red, nuclear@26: double green) nuclear@26: { nuclear@26: int red_fixed = (int)((float)red*100000.0 + 0.5); nuclear@26: int green_fixed = (int)((float)green*100000.0 + 0.5); nuclear@26: if (png_ptr == NULL) return; nuclear@26: png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed); nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: void PNGAPI nuclear@26: png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action, nuclear@26: png_fixed_point red, png_fixed_point green) nuclear@26: { nuclear@26: png_debug(1, "in png_set_rgb_to_gray\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: switch(error_action) nuclear@26: { nuclear@26: case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY; nuclear@26: break; nuclear@26: case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; nuclear@26: break; nuclear@26: case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; nuclear@26: } nuclear@26: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@26: #if defined(PNG_READ_EXPAND_SUPPORTED) nuclear@26: png_ptr->transformations |= PNG_EXPAND; nuclear@26: #else nuclear@26: { nuclear@26: png_warning(png_ptr, nuclear@26: "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED."); nuclear@26: png_ptr->transformations &= ~PNG_RGB_TO_GRAY; nuclear@26: } nuclear@26: #endif nuclear@26: { nuclear@26: png_uint_16 red_int, green_int; nuclear@26: if (red < 0 || green < 0) nuclear@26: { nuclear@26: red_int = 6968; /* .212671 * 32768 + .5 */ nuclear@26: green_int = 23434; /* .715160 * 32768 + .5 */ nuclear@26: } nuclear@26: else if (red + green < 100000L) nuclear@26: { nuclear@26: red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L); nuclear@26: green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L); nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients"); nuclear@26: red_int = 6968; nuclear@26: green_int = 23434; nuclear@26: } nuclear@26: png_ptr->rgb_to_gray_red_coeff = red_int; nuclear@26: png_ptr->rgb_to_gray_green_coeff = green_int; nuclear@26: png_ptr->rgb_to_gray_blue_coeff = nuclear@26: (png_uint_16)(32768 - red_int - green_int); nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ nuclear@26: defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \ nuclear@26: defined(PNG_LEGACY_SUPPORTED) nuclear@26: void PNGAPI nuclear@26: png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr nuclear@26: read_user_transform_fn) nuclear@26: { nuclear@26: png_debug(1, "in png_set_read_user_transform_fn\n"); nuclear@26: if (png_ptr == NULL) return; nuclear@26: #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) nuclear@26: png_ptr->transformations |= PNG_USER_TRANSFORM; nuclear@26: png_ptr->read_user_transform_fn = read_user_transform_fn; nuclear@26: #endif nuclear@26: #ifdef PNG_LEGACY_SUPPORTED nuclear@26: if (read_user_transform_fn) nuclear@26: png_warning(png_ptr, nuclear@26: "This version of libpng does not support user transforms"); nuclear@26: #endif nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: /* Initialize everything needed for the read. This includes modifying nuclear@26: * the palette. nuclear@26: */ nuclear@26: void /* PRIVATE */ nuclear@26: png_init_read_transformations(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_init_read_transformations\n"); nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: if (png_ptr != NULL) nuclear@26: #endif nuclear@26: { nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \ nuclear@26: || defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: int color_type = png_ptr->color_type; nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: nuclear@26: #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) nuclear@26: /* Detect gray background and attempt to enable optimization nuclear@26: * for gray --> RGB case */ nuclear@26: /* Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or nuclear@26: * RGB_ALPHA (in which case need_expand is superfluous anyway), the nuclear@26: * background color might actually be gray yet not be flagged as such. nuclear@26: * This is not a problem for the current code, which uses nuclear@26: * PNG_BACKGROUND_IS_GRAY only to decide when to do the nuclear@26: * png_do_gray_to_rgb() transformation. nuclear@26: */ nuclear@26: if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && nuclear@26: !(color_type & PNG_COLOR_MASK_COLOR)) nuclear@26: { nuclear@26: png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; nuclear@26: } else if ((png_ptr->transformations & PNG_BACKGROUND) && nuclear@26: !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) && nuclear@26: (png_ptr->transformations & PNG_GRAY_TO_RGB) && nuclear@26: png_ptr->background.red == png_ptr->background.green && nuclear@26: png_ptr->background.red == png_ptr->background.blue) nuclear@26: { nuclear@26: png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; nuclear@26: png_ptr->background.gray = png_ptr->background.red; nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && nuclear@26: (png_ptr->transformations & PNG_EXPAND)) nuclear@26: { nuclear@26: if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */ nuclear@26: { nuclear@26: /* expand background and tRNS chunks */ nuclear@26: switch (png_ptr->bit_depth) nuclear@26: { nuclear@26: case 1: nuclear@26: png_ptr->background.gray *= (png_uint_16)0xff; nuclear@26: png_ptr->background.red = png_ptr->background.green nuclear@26: = png_ptr->background.blue = png_ptr->background.gray; nuclear@26: if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) nuclear@26: { nuclear@26: png_ptr->trans_values.gray *= (png_uint_16)0xff; nuclear@26: png_ptr->trans_values.red = png_ptr->trans_values.green nuclear@26: = png_ptr->trans_values.blue = png_ptr->trans_values.gray; nuclear@26: } nuclear@26: break; nuclear@26: case 2: nuclear@26: png_ptr->background.gray *= (png_uint_16)0x55; nuclear@26: png_ptr->background.red = png_ptr->background.green nuclear@26: = png_ptr->background.blue = png_ptr->background.gray; nuclear@26: if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) nuclear@26: { nuclear@26: png_ptr->trans_values.gray *= (png_uint_16)0x55; nuclear@26: png_ptr->trans_values.red = png_ptr->trans_values.green nuclear@26: = png_ptr->trans_values.blue = png_ptr->trans_values.gray; nuclear@26: } nuclear@26: break; nuclear@26: case 4: nuclear@26: png_ptr->background.gray *= (png_uint_16)0x11; nuclear@26: png_ptr->background.red = png_ptr->background.green nuclear@26: = png_ptr->background.blue = png_ptr->background.gray; nuclear@26: if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) nuclear@26: { nuclear@26: png_ptr->trans_values.gray *= (png_uint_16)0x11; nuclear@26: png_ptr->trans_values.red = png_ptr->trans_values.green nuclear@26: = png_ptr->trans_values.blue = png_ptr->trans_values.gray; nuclear@26: } nuclear@26: break; nuclear@26: case 8: nuclear@26: case 16: nuclear@26: png_ptr->background.red = png_ptr->background.green nuclear@26: = png_ptr->background.blue = png_ptr->background.gray; nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: else if (color_type == PNG_COLOR_TYPE_PALETTE) nuclear@26: { nuclear@26: png_ptr->background.red = nuclear@26: png_ptr->palette[png_ptr->background.index].red; nuclear@26: png_ptr->background.green = nuclear@26: png_ptr->palette[png_ptr->background.index].green; nuclear@26: png_ptr->background.blue = nuclear@26: png_ptr->palette[png_ptr->background.index].blue; nuclear@26: nuclear@26: #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_INVERT_ALPHA) nuclear@26: { nuclear@26: #if defined(PNG_READ_EXPAND_SUPPORTED) nuclear@26: if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) nuclear@26: #endif nuclear@26: { nuclear@26: /* invert the alpha channel (in tRNS) unless the pixels are nuclear@26: going to be expanded, in which case leave it for later */ nuclear@26: int i, istop; nuclear@26: istop=(int)png_ptr->num_trans; nuclear@26: for (i=0; itrans[i] = (png_byte)(255 - png_ptr->trans[i]); nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: png_ptr->background_1 = png_ptr->background; nuclear@26: #endif nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) nuclear@26: nuclear@26: if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0) nuclear@26: && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0) nuclear@26: < PNG_GAMMA_THRESHOLD)) nuclear@26: { nuclear@26: int i, k; nuclear@26: k=0; nuclear@26: for (i=0; inum_trans; i++) nuclear@26: { nuclear@26: if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff) nuclear@26: k=1; /* partial transparency is present */ nuclear@26: } nuclear@26: if (k == 0) nuclear@26: png_ptr->transformations &= ~PNG_GAMMA; nuclear@26: } nuclear@26: nuclear@26: if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) && nuclear@26: png_ptr->gamma != 0.0) nuclear@26: { nuclear@26: png_build_gamma_table(png_ptr); nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_BACKGROUND) nuclear@26: { nuclear@26: if (color_type == PNG_COLOR_TYPE_PALETTE) nuclear@26: { nuclear@26: /* could skip if no transparency and nuclear@26: */ nuclear@26: png_color back, back_1; nuclear@26: png_colorp palette = png_ptr->palette; nuclear@26: int num_palette = png_ptr->num_palette; nuclear@26: int i; nuclear@26: if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) nuclear@26: { nuclear@26: back.red = png_ptr->gamma_table[png_ptr->background.red]; nuclear@26: back.green = png_ptr->gamma_table[png_ptr->background.green]; nuclear@26: back.blue = png_ptr->gamma_table[png_ptr->background.blue]; nuclear@26: nuclear@26: back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; nuclear@26: back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; nuclear@26: back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: double g, gs; nuclear@26: nuclear@26: switch (png_ptr->background_gamma_type) nuclear@26: { nuclear@26: case PNG_BACKGROUND_GAMMA_SCREEN: nuclear@26: g = (png_ptr->screen_gamma); nuclear@26: gs = 1.0; nuclear@26: break; nuclear@26: case PNG_BACKGROUND_GAMMA_FILE: nuclear@26: g = 1.0 / (png_ptr->gamma); nuclear@26: gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); nuclear@26: break; nuclear@26: case PNG_BACKGROUND_GAMMA_UNIQUE: nuclear@26: g = 1.0 / (png_ptr->background_gamma); nuclear@26: gs = 1.0 / (png_ptr->background_gamma * nuclear@26: png_ptr->screen_gamma); nuclear@26: break; nuclear@26: default: nuclear@26: g = 1.0; /* back_1 */ nuclear@26: gs = 1.0; /* back */ nuclear@26: } nuclear@26: nuclear@26: if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD) nuclear@26: { nuclear@26: back.red = (png_byte)png_ptr->background.red; nuclear@26: back.green = (png_byte)png_ptr->background.green; nuclear@26: back.blue = (png_byte)png_ptr->background.blue; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: back.red = (png_byte)(pow( nuclear@26: (double)png_ptr->background.red/255, gs) * 255.0 + .5); nuclear@26: back.green = (png_byte)(pow( nuclear@26: (double)png_ptr->background.green/255, gs) * 255.0 + .5); nuclear@26: back.blue = (png_byte)(pow( nuclear@26: (double)png_ptr->background.blue/255, gs) * 255.0 + .5); nuclear@26: } nuclear@26: nuclear@26: back_1.red = (png_byte)(pow( nuclear@26: (double)png_ptr->background.red/255, g) * 255.0 + .5); nuclear@26: back_1.green = (png_byte)(pow( nuclear@26: (double)png_ptr->background.green/255, g) * 255.0 + .5); nuclear@26: back_1.blue = (png_byte)(pow( nuclear@26: (double)png_ptr->background.blue/255, g) * 255.0 + .5); nuclear@26: } nuclear@26: for (i = 0; i < num_palette; i++) nuclear@26: { nuclear@26: if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff) nuclear@26: { nuclear@26: if (png_ptr->trans[i] == 0) nuclear@26: { nuclear@26: palette[i] = back; nuclear@26: } nuclear@26: else /* if (png_ptr->trans[i] != 0xff) */ nuclear@26: { nuclear@26: png_byte v, w; nuclear@26: nuclear@26: v = png_ptr->gamma_to_1[palette[i].red]; nuclear@26: png_composite(w, v, png_ptr->trans[i], back_1.red); nuclear@26: palette[i].red = png_ptr->gamma_from_1[w]; nuclear@26: nuclear@26: v = png_ptr->gamma_to_1[palette[i].green]; nuclear@26: png_composite(w, v, png_ptr->trans[i], back_1.green); nuclear@26: palette[i].green = png_ptr->gamma_from_1[w]; nuclear@26: nuclear@26: v = png_ptr->gamma_to_1[palette[i].blue]; nuclear@26: png_composite(w, v, png_ptr->trans[i], back_1.blue); nuclear@26: palette[i].blue = png_ptr->gamma_from_1[w]; nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: palette[i].red = png_ptr->gamma_table[palette[i].red]; nuclear@26: palette[i].green = png_ptr->gamma_table[palette[i].green]; nuclear@26: palette[i].blue = png_ptr->gamma_table[palette[i].blue]; nuclear@26: } nuclear@26: } nuclear@26: /* Prevent the transformations being done again, and make sure nuclear@26: * that the now spurious alpha channel is stripped - the code nuclear@26: * has just reduced background composition and gamma correction nuclear@26: * to a simple alpha channel strip. nuclear@26: */ nuclear@26: png_ptr->transformations &= ~PNG_BACKGROUND; nuclear@26: png_ptr->transformations &= ~PNG_GAMMA; nuclear@26: png_ptr->transformations |= PNG_STRIP_ALPHA; nuclear@26: } nuclear@26: /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */ nuclear@26: else nuclear@26: /* color_type != PNG_COLOR_TYPE_PALETTE */ nuclear@26: { nuclear@26: double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1); nuclear@26: double g = 1.0; nuclear@26: double gs = 1.0; nuclear@26: nuclear@26: switch (png_ptr->background_gamma_type) nuclear@26: { nuclear@26: case PNG_BACKGROUND_GAMMA_SCREEN: nuclear@26: g = (png_ptr->screen_gamma); nuclear@26: gs = 1.0; nuclear@26: break; nuclear@26: case PNG_BACKGROUND_GAMMA_FILE: nuclear@26: g = 1.0 / (png_ptr->gamma); nuclear@26: gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); nuclear@26: break; nuclear@26: case PNG_BACKGROUND_GAMMA_UNIQUE: nuclear@26: g = 1.0 / (png_ptr->background_gamma); nuclear@26: gs = 1.0 / (png_ptr->background_gamma * nuclear@26: png_ptr->screen_gamma); nuclear@26: break; nuclear@26: } nuclear@26: nuclear@26: png_ptr->background_1.gray = (png_uint_16)(pow( nuclear@26: (double)png_ptr->background.gray / m, g) * m + .5); nuclear@26: png_ptr->background.gray = (png_uint_16)(pow( nuclear@26: (double)png_ptr->background.gray / m, gs) * m + .5); nuclear@26: nuclear@26: if ((png_ptr->background.red != png_ptr->background.green) || nuclear@26: (png_ptr->background.red != png_ptr->background.blue) || nuclear@26: (png_ptr->background.red != png_ptr->background.gray)) nuclear@26: { nuclear@26: /* RGB or RGBA with color background */ nuclear@26: png_ptr->background_1.red = (png_uint_16)(pow( nuclear@26: (double)png_ptr->background.red / m, g) * m + .5); nuclear@26: png_ptr->background_1.green = (png_uint_16)(pow( nuclear@26: (double)png_ptr->background.green / m, g) * m + .5); nuclear@26: png_ptr->background_1.blue = (png_uint_16)(pow( nuclear@26: (double)png_ptr->background.blue / m, g) * m + .5); nuclear@26: png_ptr->background.red = (png_uint_16)(pow( nuclear@26: (double)png_ptr->background.red / m, gs) * m + .5); nuclear@26: png_ptr->background.green = (png_uint_16)(pow( nuclear@26: (double)png_ptr->background.green / m, gs) * m + .5); nuclear@26: png_ptr->background.blue = (png_uint_16)(pow( nuclear@26: (double)png_ptr->background.blue / m, gs) * m + .5); nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */ nuclear@26: png_ptr->background_1.red = png_ptr->background_1.green nuclear@26: = png_ptr->background_1.blue = png_ptr->background_1.gray; nuclear@26: png_ptr->background.red = png_ptr->background.green nuclear@26: = png_ptr->background.blue = png_ptr->background.gray; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: /* transformation does not include PNG_BACKGROUND */ nuclear@26: #endif /* PNG_READ_BACKGROUND_SUPPORTED */ nuclear@26: if (color_type == PNG_COLOR_TYPE_PALETTE) nuclear@26: { nuclear@26: png_colorp palette = png_ptr->palette; nuclear@26: int num_palette = png_ptr->num_palette; nuclear@26: int i; nuclear@26: nuclear@26: for (i = 0; i < num_palette; i++) nuclear@26: { nuclear@26: palette[i].red = png_ptr->gamma_table[palette[i].red]; nuclear@26: palette[i].green = png_ptr->gamma_table[palette[i].green]; nuclear@26: palette[i].blue = png_ptr->gamma_table[palette[i].blue]; nuclear@26: } nuclear@26: nuclear@26: /* Done the gamma correction. */ nuclear@26: png_ptr->transformations &= ~PNG_GAMMA; nuclear@26: } nuclear@26: } nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: else nuclear@26: #endif nuclear@26: #endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */ nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: /* No GAMMA transformation */ nuclear@26: if ((png_ptr->transformations & PNG_BACKGROUND) && nuclear@26: (color_type == PNG_COLOR_TYPE_PALETTE)) nuclear@26: { nuclear@26: int i; nuclear@26: int istop = (int)png_ptr->num_trans; nuclear@26: png_color back; nuclear@26: png_colorp palette = png_ptr->palette; nuclear@26: nuclear@26: back.red = (png_byte)png_ptr->background.red; nuclear@26: back.green = (png_byte)png_ptr->background.green; nuclear@26: back.blue = (png_byte)png_ptr->background.blue; nuclear@26: nuclear@26: for (i = 0; i < istop; i++) nuclear@26: { nuclear@26: if (png_ptr->trans[i] == 0) nuclear@26: { nuclear@26: palette[i] = back; nuclear@26: } nuclear@26: else if (png_ptr->trans[i] != 0xff) nuclear@26: { nuclear@26: /* The png_composite() macro is defined in png.h */ nuclear@26: png_composite(palette[i].red, palette[i].red, nuclear@26: png_ptr->trans[i], back.red); nuclear@26: png_composite(palette[i].green, palette[i].green, nuclear@26: png_ptr->trans[i], back.green); nuclear@26: png_composite(palette[i].blue, palette[i].blue, nuclear@26: png_ptr->trans[i], back.blue); nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: /* Handled alpha, still need to strip the channel. */ nuclear@26: png_ptr->transformations &= ~PNG_BACKGROUND; nuclear@26: png_ptr->transformations |= PNG_STRIP_ALPHA; nuclear@26: } nuclear@26: #endif /* PNG_READ_BACKGROUND_SUPPORTED */ nuclear@26: nuclear@26: #if defined(PNG_READ_SHIFT_SUPPORTED) nuclear@26: if ((png_ptr->transformations & PNG_SHIFT) && nuclear@26: (color_type == PNG_COLOR_TYPE_PALETTE)) nuclear@26: { nuclear@26: png_uint_16 i; nuclear@26: png_uint_16 istop = png_ptr->num_palette; nuclear@26: int sr = 8 - png_ptr->sig_bit.red; nuclear@26: int sg = 8 - png_ptr->sig_bit.green; nuclear@26: int sb = 8 - png_ptr->sig_bit.blue; nuclear@26: nuclear@26: if (sr < 0 || sr > 8) nuclear@26: sr = 0; nuclear@26: if (sg < 0 || sg > 8) nuclear@26: sg = 0; nuclear@26: if (sb < 0 || sb > 8) nuclear@26: sb = 0; nuclear@26: for (i = 0; i < istop; i++) nuclear@26: { nuclear@26: png_ptr->palette[i].red >>= sr; nuclear@26: png_ptr->palette[i].green >>= sg; nuclear@26: png_ptr->palette[i].blue >>= sb; nuclear@26: } nuclear@26: } nuclear@26: #endif /* PNG_READ_SHIFT_SUPPORTED */ nuclear@26: } nuclear@26: #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \ nuclear@26: && !defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: if (png_ptr) nuclear@26: return; nuclear@26: #endif nuclear@26: } nuclear@26: nuclear@26: /* Modify the info structure to reflect the transformations. The nuclear@26: * info should be updated so a PNG file could be written with it, nuclear@26: * assuming the transformations result in valid PNG data. nuclear@26: */ nuclear@26: void /* PRIVATE */ nuclear@26: png_read_transform_info(png_structp png_ptr, png_infop info_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_read_transform_info\n"); nuclear@26: #if defined(PNG_READ_EXPAND_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_EXPAND) nuclear@26: { nuclear@26: if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@26: { nuclear@26: if (png_ptr->num_trans && nuclear@26: (png_ptr->transformations & PNG_EXPAND_tRNS)) nuclear@26: info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; nuclear@26: else nuclear@26: info_ptr->color_type = PNG_COLOR_TYPE_RGB; nuclear@26: info_ptr->bit_depth = 8; nuclear@26: info_ptr->num_trans = 0; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: if (png_ptr->num_trans) nuclear@26: { nuclear@26: if (png_ptr->transformations & PNG_EXPAND_tRNS) nuclear@26: info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; nuclear@26: #if 0 /* Removed from libpng-1.2.27 */ nuclear@26: else nuclear@26: info_ptr->color_type |= PNG_COLOR_MASK_COLOR; nuclear@26: #endif nuclear@26: } nuclear@26: if (info_ptr->bit_depth < 8) nuclear@26: info_ptr->bit_depth = 8; nuclear@26: info_ptr->num_trans = 0; nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_BACKGROUND) nuclear@26: { nuclear@26: info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA; nuclear@26: info_ptr->num_trans = 0; nuclear@26: info_ptr->background = png_ptr->background; nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_GAMMA) nuclear@26: { nuclear@26: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@26: info_ptr->gamma = png_ptr->gamma; nuclear@26: #endif nuclear@26: #ifdef PNG_FIXED_POINT_SUPPORTED nuclear@26: info_ptr->int_gamma = png_ptr->int_gamma; nuclear@26: #endif nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_16_TO_8_SUPPORTED) nuclear@26: if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16)) nuclear@26: info_ptr->bit_depth = 8; nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_GRAY_TO_RGB) nuclear@26: info_ptr->color_type |= PNG_COLOR_MASK_COLOR; nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_RGB_TO_GRAY) nuclear@26: info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR; nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_DITHER_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_DITHER) nuclear@26: { nuclear@26: if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || nuclear@26: (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && nuclear@26: png_ptr->palette_lookup && info_ptr->bit_depth == 8) nuclear@26: { nuclear@26: info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_PACK_SUPPORTED) nuclear@26: if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8)) nuclear@26: info_ptr->bit_depth = 8; nuclear@26: #endif nuclear@26: nuclear@26: if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@26: info_ptr->channels = 1; nuclear@26: else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) nuclear@26: info_ptr->channels = 3; nuclear@26: else nuclear@26: info_ptr->channels = 1; nuclear@26: nuclear@26: #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) nuclear@26: if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA) nuclear@26: info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA; nuclear@26: #endif nuclear@26: nuclear@26: if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) nuclear@26: info_ptr->channels++; nuclear@26: nuclear@26: #if defined(PNG_READ_FILLER_SUPPORTED) nuclear@26: /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ nuclear@26: if ((png_ptr->transformations & PNG_FILLER) && nuclear@26: ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || nuclear@26: (info_ptr->color_type == PNG_COLOR_TYPE_GRAY))) nuclear@26: { nuclear@26: info_ptr->channels++; nuclear@26: /* if adding a true alpha channel not just filler */ nuclear@26: #if !defined(PNG_1_0_X) nuclear@26: if (png_ptr->transformations & PNG_ADD_ALPHA) nuclear@26: info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; nuclear@26: #endif nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ nuclear@26: defined(PNG_READ_USER_TRANSFORM_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_USER_TRANSFORM) nuclear@26: { nuclear@26: if (info_ptr->bit_depth < png_ptr->user_transform_depth) nuclear@26: info_ptr->bit_depth = png_ptr->user_transform_depth; nuclear@26: if (info_ptr->channels < png_ptr->user_transform_channels) nuclear@26: info_ptr->channels = png_ptr->user_transform_channels; nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: info_ptr->pixel_depth = (png_byte)(info_ptr->channels * nuclear@26: info_ptr->bit_depth); nuclear@26: nuclear@26: info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width); nuclear@26: nuclear@26: #if !defined(PNG_READ_EXPAND_SUPPORTED) nuclear@26: if (png_ptr) nuclear@26: return; nuclear@26: #endif nuclear@26: } nuclear@26: nuclear@26: /* Transform the row. The order of transformations is significant, nuclear@26: * and is very touchy. If you add a transformation, take care to nuclear@26: * decide how it fits in with the other transformations here. nuclear@26: */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_read_transformations(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_do_read_transformations\n"); nuclear@26: if (png_ptr->row_buf == NULL) nuclear@26: { nuclear@26: #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) nuclear@26: char msg[50]; nuclear@26: nuclear@26: png_snprintf2(msg, 50, nuclear@26: "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number, nuclear@26: png_ptr->pass); nuclear@26: png_error(png_ptr, msg); nuclear@26: #else nuclear@26: png_error(png_ptr, "NULL row buffer"); nuclear@26: #endif nuclear@26: } nuclear@26: #ifdef PNG_WARN_UNINITIALIZED_ROW nuclear@26: if (!(png_ptr->flags & PNG_FLAG_ROW_INIT)) nuclear@26: /* Application has failed to call either png_read_start_image() nuclear@26: * or png_read_update_info() after setting transforms that expand nuclear@26: * pixels. This check added to libpng-1.2.19 */ nuclear@26: #if (PNG_WARN_UNINITIALIZED_ROW==1) nuclear@26: png_error(png_ptr, "Uninitialized row"); nuclear@26: #else nuclear@26: png_warning(png_ptr, "Uninitialized row"); nuclear@26: #endif nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_EXPAND_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_EXPAND) nuclear@26: { nuclear@26: if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE) nuclear@26: { nuclear@26: png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1, nuclear@26: png_ptr->palette, png_ptr->trans, png_ptr->num_trans); nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: if (png_ptr->num_trans && nuclear@26: (png_ptr->transformations & PNG_EXPAND_tRNS)) nuclear@26: png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1, nuclear@26: &(png_ptr->trans_values)); nuclear@26: else nuclear@26: png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1, nuclear@26: NULL); nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED) nuclear@26: if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA) nuclear@26: png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, nuclear@26: PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_RGB_TO_GRAY) nuclear@26: { nuclear@26: int rgb_error = nuclear@26: png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: if (rgb_error) nuclear@26: { nuclear@26: png_ptr->rgb_to_gray_status=1; nuclear@26: if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == nuclear@26: PNG_RGB_TO_GRAY_WARN) nuclear@26: png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); nuclear@26: if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == nuclear@26: PNG_RGB_TO_GRAY_ERR) nuclear@26: png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: /* nuclear@26: From Andreas Dilger e-mail to png-implement, 26 March 1998: nuclear@26: nuclear@26: In most cases, the "simple transparency" should be done prior to doing nuclear@26: gray-to-RGB, or you will have to test 3x as many bytes to check if a nuclear@26: pixel is transparent. You would also need to make sure that the nuclear@26: transparency information is upgraded to RGB. nuclear@26: nuclear@26: To summarize, the current flow is: nuclear@26: - Gray + simple transparency -> compare 1 or 2 gray bytes and composite nuclear@26: with background "in place" if transparent, nuclear@26: convert to RGB if necessary nuclear@26: - Gray + alpha -> composite with gray background and remove alpha bytes, nuclear@26: convert to RGB if necessary nuclear@26: nuclear@26: To support RGB backgrounds for gray images we need: nuclear@26: - Gray + simple transparency -> convert to RGB + simple transparency, compare nuclear@26: 3 or 6 bytes and composite with background nuclear@26: "in place" if transparent (3x compare/pixel nuclear@26: compared to doing composite with gray bkgrnd) nuclear@26: - Gray + alpha -> convert to RGB + alpha, composite with background and nuclear@26: remove alpha bytes (3x float operations/pixel nuclear@26: compared with composite on gray background) nuclear@26: nuclear@26: Greg's change will do this. The reason it wasn't done before is for nuclear@26: performance, as this increases the per-pixel operations. If we would check nuclear@26: in advance if the background was gray or RGB, and position the gray-to-RGB nuclear@26: transform appropriately, then it would save a lot of work/time. nuclear@26: */ nuclear@26: nuclear@26: #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) nuclear@26: /* if gray -> RGB, do so now only if background is non-gray; else do later nuclear@26: * for performance reasons */ nuclear@26: if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && nuclear@26: !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) nuclear@26: png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: if ((png_ptr->transformations & PNG_BACKGROUND) && nuclear@26: ((png_ptr->num_trans != 0 ) || nuclear@26: (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) nuclear@26: png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1, nuclear@26: &(png_ptr->trans_values), &(png_ptr->background) nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: , &(png_ptr->background_1), nuclear@26: png_ptr->gamma_table, png_ptr->gamma_from_1, nuclear@26: png_ptr->gamma_to_1, png_ptr->gamma_16_table, nuclear@26: png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1, nuclear@26: png_ptr->gamma_shift nuclear@26: #endif nuclear@26: ); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if ((png_ptr->transformations & PNG_GAMMA) && nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: !((png_ptr->transformations & PNG_BACKGROUND) && nuclear@26: ((png_ptr->num_trans != 0) || nuclear@26: (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) && nuclear@26: #endif nuclear@26: (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) nuclear@26: png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1, nuclear@26: png_ptr->gamma_table, png_ptr->gamma_16_table, nuclear@26: png_ptr->gamma_shift); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_16_TO_8_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_16_TO_8) nuclear@26: png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_DITHER_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_DITHER) nuclear@26: { nuclear@26: png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1, nuclear@26: png_ptr->palette_lookup, png_ptr->dither_index); nuclear@26: if (png_ptr->row_info.rowbytes == (png_uint_32)0) nuclear@26: png_error(png_ptr, "png_do_dither returned rowbytes=0"); nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_INVERT_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_INVERT_MONO) nuclear@26: png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_SHIFT_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_SHIFT) nuclear@26: png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1, nuclear@26: &(png_ptr->shift)); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_PACK_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_PACK) nuclear@26: png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_BGR_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_BGR) nuclear@26: png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_PACKSWAP_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_PACKSWAP) nuclear@26: png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) nuclear@26: /* if gray -> RGB, do so now only if we did not do so above */ nuclear@26: if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && nuclear@26: (png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) nuclear@26: png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_FILLER_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_FILLER) nuclear@26: png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, nuclear@26: (png_uint_32)png_ptr->filler, png_ptr->flags); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_INVERT_ALPHA) nuclear@26: png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_SWAP_ALPHA) nuclear@26: png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_SWAP_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_SWAP_BYTES) nuclear@26: png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_USER_TRANSFORM) nuclear@26: { nuclear@26: if (png_ptr->read_user_transform_fn != NULL) nuclear@26: (*(png_ptr->read_user_transform_fn)) /* user read transform function */ nuclear@26: (png_ptr, /* png_ptr */ nuclear@26: &(png_ptr->row_info), /* row_info: */ nuclear@26: /* png_uint_32 width; width of row */ nuclear@26: /* png_uint_32 rowbytes; number of bytes in row */ nuclear@26: /* png_byte color_type; color type of pixels */ nuclear@26: /* png_byte bit_depth; bit depth of samples */ nuclear@26: /* png_byte channels; number of channels (1-4) */ nuclear@26: /* png_byte pixel_depth; bits per pixel (depth*channels) */ nuclear@26: png_ptr->row_buf + 1); /* start of pixel data for row */ nuclear@26: #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) nuclear@26: if (png_ptr->user_transform_depth) nuclear@26: png_ptr->row_info.bit_depth = png_ptr->user_transform_depth; nuclear@26: if (png_ptr->user_transform_channels) nuclear@26: png_ptr->row_info.channels = png_ptr->user_transform_channels; nuclear@26: #endif nuclear@26: png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth * nuclear@26: png_ptr->row_info.channels); nuclear@26: png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth, nuclear@26: png_ptr->row_info.width); nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: } nuclear@26: nuclear@26: #if defined(PNG_READ_PACK_SUPPORTED) nuclear@26: /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, nuclear@26: * without changing the actual values. Thus, if you had a row with nuclear@26: * a bit depth of 1, you would end up with bytes that only contained nuclear@26: * the numbers 0 or 1. If you would rather they contain 0 and 255, use nuclear@26: * png_do_shift() after this. nuclear@26: */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_unpack(png_row_infop row_info, png_bytep row) nuclear@26: { nuclear@26: png_debug(1, "in png_do_unpack\n"); nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: if (row != NULL && row_info != NULL && row_info->bit_depth < 8) nuclear@26: #else nuclear@26: if (row_info->bit_depth < 8) nuclear@26: #endif nuclear@26: { nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 row_width=row_info->width; nuclear@26: nuclear@26: switch (row_info->bit_depth) nuclear@26: { nuclear@26: case 1: nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)((row_width - 1) >> 3); nuclear@26: png_bytep dp = row + (png_size_t)row_width - 1; nuclear@26: png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07); nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *dp = (png_byte)((*sp >> shift) & 0x01); nuclear@26: if (shift == 7) nuclear@26: { nuclear@26: shift = 0; nuclear@26: sp--; nuclear@26: } nuclear@26: else nuclear@26: shift++; nuclear@26: nuclear@26: dp--; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 2: nuclear@26: { nuclear@26: nuclear@26: png_bytep sp = row + (png_size_t)((row_width - 1) >> 2); nuclear@26: png_bytep dp = row + (png_size_t)row_width - 1; nuclear@26: png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *dp = (png_byte)((*sp >> shift) & 0x03); nuclear@26: if (shift == 6) nuclear@26: { nuclear@26: shift = 0; nuclear@26: sp--; nuclear@26: } nuclear@26: else nuclear@26: shift += 2; nuclear@26: nuclear@26: dp--; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 4: nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)((row_width - 1) >> 1); nuclear@26: png_bytep dp = row + (png_size_t)row_width - 1; nuclear@26: png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *dp = (png_byte)((*sp >> shift) & 0x0f); nuclear@26: if (shift == 4) nuclear@26: { nuclear@26: shift = 0; nuclear@26: sp--; nuclear@26: } nuclear@26: else nuclear@26: shift = 4; nuclear@26: nuclear@26: dp--; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: row_info->bit_depth = 8; nuclear@26: row_info->pixel_depth = (png_byte)(8 * row_info->channels); nuclear@26: row_info->rowbytes = row_width * row_info->channels; nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_SHIFT_SUPPORTED) nuclear@26: /* Reverse the effects of png_do_shift. This routine merely shifts the nuclear@26: * pixels back to their significant bits values. Thus, if you have nuclear@26: * a row of bit depth 8, but only 5 are significant, this will shift nuclear@26: * the values back to 0 through 31. nuclear@26: */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits) nuclear@26: { nuclear@26: png_debug(1, "in png_do_unshift\n"); nuclear@26: if ( nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: row != NULL && row_info != NULL && sig_bits != NULL && nuclear@26: #endif nuclear@26: row_info->color_type != PNG_COLOR_TYPE_PALETTE) nuclear@26: { nuclear@26: int shift[4]; nuclear@26: int channels = 0; nuclear@26: int c; nuclear@26: png_uint_16 value = 0; nuclear@26: png_uint_32 row_width = row_info->width; nuclear@26: nuclear@26: if (row_info->color_type & PNG_COLOR_MASK_COLOR) nuclear@26: { nuclear@26: shift[channels++] = row_info->bit_depth - sig_bits->red; nuclear@26: shift[channels++] = row_info->bit_depth - sig_bits->green; nuclear@26: shift[channels++] = row_info->bit_depth - sig_bits->blue; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: shift[channels++] = row_info->bit_depth - sig_bits->gray; nuclear@26: } nuclear@26: if (row_info->color_type & PNG_COLOR_MASK_ALPHA) nuclear@26: { nuclear@26: shift[channels++] = row_info->bit_depth - sig_bits->alpha; nuclear@26: } nuclear@26: nuclear@26: for (c = 0; c < channels; c++) nuclear@26: { nuclear@26: if (shift[c] <= 0) nuclear@26: shift[c] = 0; nuclear@26: else nuclear@26: value = 1; nuclear@26: } nuclear@26: nuclear@26: if (!value) nuclear@26: return; nuclear@26: nuclear@26: switch (row_info->bit_depth) nuclear@26: { nuclear@26: case 2: nuclear@26: { nuclear@26: png_bytep bp; nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 istop = row_info->rowbytes; nuclear@26: nuclear@26: for (bp = row, i = 0; i < istop; i++) nuclear@26: { nuclear@26: *bp >>= 1; nuclear@26: *bp++ &= 0x55; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 4: nuclear@26: { nuclear@26: png_bytep bp = row; nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 istop = row_info->rowbytes; nuclear@26: png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) | nuclear@26: (png_byte)((int)0xf >> shift[0])); nuclear@26: nuclear@26: for (i = 0; i < istop; i++) nuclear@26: { nuclear@26: *bp >>= shift[0]; nuclear@26: *bp++ &= mask; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 8: nuclear@26: { nuclear@26: png_bytep bp = row; nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 istop = row_width * channels; nuclear@26: nuclear@26: for (i = 0; i < istop; i++) nuclear@26: { nuclear@26: *bp++ >>= shift[i%channels]; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 16: nuclear@26: { nuclear@26: png_bytep bp = row; nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 istop = channels * row_width; nuclear@26: nuclear@26: for (i = 0; i < istop; i++) nuclear@26: { nuclear@26: value = (png_uint_16)((*bp << 8) + *(bp + 1)); nuclear@26: value >>= shift[i%channels]; nuclear@26: *bp++ = (png_byte)(value >> 8); nuclear@26: *bp++ = (png_byte)(value & 0xff); nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_16_TO_8_SUPPORTED) nuclear@26: /* chop rows of bit depth 16 down to 8 */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_chop(png_row_infop row_info, png_bytep row) nuclear@26: { nuclear@26: png_debug(1, "in png_do_chop\n"); nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: if (row != NULL && row_info != NULL && row_info->bit_depth == 16) nuclear@26: #else nuclear@26: if (row_info->bit_depth == 16) nuclear@26: #endif nuclear@26: { nuclear@26: png_bytep sp = row; nuclear@26: png_bytep dp = row; nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 istop = row_info->width * row_info->channels; nuclear@26: nuclear@26: for (i = 0; i> 8)) >> 8; nuclear@26: * nuclear@26: * Approximate calculation with shift/add instead of multiply/divide: nuclear@26: * *dp = ((((png_uint_32)(*sp) << 8) | nuclear@26: * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8; nuclear@26: * nuclear@26: * What we actually do to avoid extra shifting and conversion: nuclear@26: */ nuclear@26: nuclear@26: *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0); nuclear@26: #else nuclear@26: /* Simply discard the low order byte */ nuclear@26: *dp = *sp; nuclear@26: #endif nuclear@26: } nuclear@26: row_info->bit_depth = 8; nuclear@26: row_info->pixel_depth = (png_byte)(8 * row_info->channels); nuclear@26: row_info->rowbytes = row_info->width * row_info->channels; nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) nuclear@26: void /* PRIVATE */ nuclear@26: png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) nuclear@26: { nuclear@26: png_debug(1, "in png_do_read_swap_alpha\n"); nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: if (row != NULL && row_info != NULL) nuclear@26: #endif nuclear@26: { nuclear@26: png_uint_32 row_width = row_info->width; nuclear@26: if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) nuclear@26: { nuclear@26: /* This converts from RGBA to ARGB */ nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: png_bytep sp = row + row_info->rowbytes; nuclear@26: png_bytep dp = sp; nuclear@26: png_byte save; nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: save = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = save; nuclear@26: } nuclear@26: } nuclear@26: /* This converts from RRGGBBAA to AARRGGBB */ nuclear@26: else nuclear@26: { nuclear@26: png_bytep sp = row + row_info->rowbytes; nuclear@26: png_bytep dp = sp; nuclear@26: png_byte save[2]; nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: save[0] = *(--sp); nuclear@26: save[1] = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = save[0]; nuclear@26: *(--dp) = save[1]; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) nuclear@26: { nuclear@26: /* This converts from GA to AG */ nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: png_bytep sp = row + row_info->rowbytes; nuclear@26: png_bytep dp = sp; nuclear@26: png_byte save; nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: save = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = save; nuclear@26: } nuclear@26: } nuclear@26: /* This converts from GGAA to AAGG */ nuclear@26: else nuclear@26: { nuclear@26: png_bytep sp = row + row_info->rowbytes; nuclear@26: png_bytep dp = sp; nuclear@26: png_byte save[2]; nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: save[0] = *(--sp); nuclear@26: save[1] = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = save[0]; nuclear@26: *(--dp) = save[1]; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) nuclear@26: void /* PRIVATE */ nuclear@26: png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) nuclear@26: { nuclear@26: png_debug(1, "in png_do_read_invert_alpha\n"); nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: if (row != NULL && row_info != NULL) nuclear@26: #endif nuclear@26: { nuclear@26: png_uint_32 row_width = row_info->width; nuclear@26: if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) nuclear@26: { nuclear@26: /* This inverts the alpha channel in RGBA */ nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: png_bytep sp = row + row_info->rowbytes; nuclear@26: png_bytep dp = sp; nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = (png_byte)(255 - *(--sp)); nuclear@26: nuclear@26: /* This does nothing: nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: We can replace it with: nuclear@26: */ nuclear@26: sp-=3; nuclear@26: dp=sp; nuclear@26: } nuclear@26: } nuclear@26: /* This inverts the alpha channel in RRGGBBAA */ nuclear@26: else nuclear@26: { nuclear@26: png_bytep sp = row + row_info->rowbytes; nuclear@26: png_bytep dp = sp; nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = (png_byte)(255 - *(--sp)); nuclear@26: *(--dp) = (png_byte)(255 - *(--sp)); nuclear@26: nuclear@26: /* This does nothing: nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: We can replace it with: nuclear@26: */ nuclear@26: sp-=6; nuclear@26: dp=sp; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) nuclear@26: { nuclear@26: /* This inverts the alpha channel in GA */ nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: png_bytep sp = row + row_info->rowbytes; nuclear@26: png_bytep dp = sp; nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = (png_byte)(255 - *(--sp)); nuclear@26: *(--dp) = *(--sp); nuclear@26: } nuclear@26: } nuclear@26: /* This inverts the alpha channel in GGAA */ nuclear@26: else nuclear@26: { nuclear@26: png_bytep sp = row + row_info->rowbytes; nuclear@26: png_bytep dp = sp; nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = (png_byte)(255 - *(--sp)); nuclear@26: *(--dp) = (png_byte)(255 - *(--sp)); nuclear@26: /* nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: */ nuclear@26: sp-=2; nuclear@26: dp=sp; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_FILLER_SUPPORTED) nuclear@26: /* Add filler channel if we have RGB color */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_read_filler(png_row_infop row_info, png_bytep row, nuclear@26: png_uint_32 filler, png_uint_32 flags) nuclear@26: { nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 row_width = row_info->width; nuclear@26: nuclear@26: png_byte hi_filler = (png_byte)((filler>>8) & 0xff); nuclear@26: png_byte lo_filler = (png_byte)(filler & 0xff); nuclear@26: nuclear@26: png_debug(1, "in png_do_read_filler\n"); nuclear@26: if ( nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: row != NULL && row_info != NULL && nuclear@26: #endif nuclear@26: row_info->color_type == PNG_COLOR_TYPE_GRAY) nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: /* This changes the data from G to GX */ nuclear@26: if (flags & PNG_FLAG_FILLER_AFTER) nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width; nuclear@26: png_bytep dp = sp + (png_size_t)row_width; nuclear@26: for (i = 1; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = lo_filler; nuclear@26: *(--dp) = *(--sp); nuclear@26: } nuclear@26: *(--dp) = lo_filler; nuclear@26: row_info->channels = 2; nuclear@26: row_info->pixel_depth = 16; nuclear@26: row_info->rowbytes = row_width * 2; nuclear@26: } nuclear@26: /* This changes the data from G to XG */ nuclear@26: else nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width; nuclear@26: png_bytep dp = sp + (png_size_t)row_width; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = lo_filler; nuclear@26: } nuclear@26: row_info->channels = 2; nuclear@26: row_info->pixel_depth = 16; nuclear@26: row_info->rowbytes = row_width * 2; nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->bit_depth == 16) nuclear@26: { nuclear@26: /* This changes the data from GG to GGXX */ nuclear@26: if (flags & PNG_FLAG_FILLER_AFTER) nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width * 2; nuclear@26: png_bytep dp = sp + (png_size_t)row_width * 2; nuclear@26: for (i = 1; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = hi_filler; nuclear@26: *(--dp) = lo_filler; nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: } nuclear@26: *(--dp) = hi_filler; nuclear@26: *(--dp) = lo_filler; nuclear@26: row_info->channels = 2; nuclear@26: row_info->pixel_depth = 32; nuclear@26: row_info->rowbytes = row_width * 4; nuclear@26: } nuclear@26: /* This changes the data from GG to XXGG */ nuclear@26: else nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width * 2; nuclear@26: png_bytep dp = sp + (png_size_t)row_width * 2; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = hi_filler; nuclear@26: *(--dp) = lo_filler; nuclear@26: } nuclear@26: row_info->channels = 2; nuclear@26: row_info->pixel_depth = 32; nuclear@26: row_info->rowbytes = row_width * 4; nuclear@26: } nuclear@26: } nuclear@26: } /* COLOR_TYPE == GRAY */ nuclear@26: else if (row_info->color_type == PNG_COLOR_TYPE_RGB) nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: /* This changes the data from RGB to RGBX */ nuclear@26: if (flags & PNG_FLAG_FILLER_AFTER) nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width * 3; nuclear@26: png_bytep dp = sp + (png_size_t)row_width; nuclear@26: for (i = 1; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = lo_filler; nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: } nuclear@26: *(--dp) = lo_filler; nuclear@26: row_info->channels = 4; nuclear@26: row_info->pixel_depth = 32; nuclear@26: row_info->rowbytes = row_width * 4; nuclear@26: } nuclear@26: /* This changes the data from RGB to XRGB */ nuclear@26: else nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width * 3; nuclear@26: png_bytep dp = sp + (png_size_t)row_width; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = lo_filler; nuclear@26: } nuclear@26: row_info->channels = 4; nuclear@26: row_info->pixel_depth = 32; nuclear@26: row_info->rowbytes = row_width * 4; nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->bit_depth == 16) nuclear@26: { nuclear@26: /* This changes the data from RRGGBB to RRGGBBXX */ nuclear@26: if (flags & PNG_FLAG_FILLER_AFTER) nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width * 6; nuclear@26: png_bytep dp = sp + (png_size_t)row_width * 2; nuclear@26: for (i = 1; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = hi_filler; nuclear@26: *(--dp) = lo_filler; nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: } nuclear@26: *(--dp) = hi_filler; nuclear@26: *(--dp) = lo_filler; nuclear@26: row_info->channels = 4; nuclear@26: row_info->pixel_depth = 64; nuclear@26: row_info->rowbytes = row_width * 8; nuclear@26: } nuclear@26: /* This changes the data from RRGGBB to XXRRGGBB */ nuclear@26: else nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width * 6; nuclear@26: png_bytep dp = sp + (png_size_t)row_width * 2; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = *(--sp); nuclear@26: *(--dp) = hi_filler; nuclear@26: *(--dp) = lo_filler; nuclear@26: } nuclear@26: row_info->channels = 4; nuclear@26: row_info->pixel_depth = 64; nuclear@26: row_info->rowbytes = row_width * 8; nuclear@26: } nuclear@26: } nuclear@26: } /* COLOR_TYPE == RGB */ nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) nuclear@26: /* expand grayscale files to RGB, with or without alpha */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) nuclear@26: { nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 row_width = row_info->width; nuclear@26: nuclear@26: png_debug(1, "in png_do_gray_to_rgb\n"); nuclear@26: if (row_info->bit_depth >= 8 && nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: row != NULL && row_info != NULL && nuclear@26: #endif nuclear@26: !(row_info->color_type & PNG_COLOR_MASK_COLOR)) nuclear@26: { nuclear@26: if (row_info->color_type == PNG_COLOR_TYPE_GRAY) nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width - 1; nuclear@26: png_bytep dp = sp + (png_size_t)row_width * 2; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(dp--) = *sp; nuclear@26: *(dp--) = *sp; nuclear@26: *(dp--) = *(sp--); nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width * 2 - 1; nuclear@26: png_bytep dp = sp + (png_size_t)row_width * 4; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(dp--) = *sp; nuclear@26: *(dp--) = *(sp - 1); nuclear@26: *(dp--) = *sp; nuclear@26: *(dp--) = *(sp - 1); nuclear@26: *(dp--) = *(sp--); nuclear@26: *(dp--) = *(sp--); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width * 2 - 1; nuclear@26: png_bytep dp = sp + (png_size_t)row_width * 2; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(dp--) = *(sp--); nuclear@26: *(dp--) = *sp; nuclear@26: *(dp--) = *sp; nuclear@26: *(dp--) = *(sp--); nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_bytep sp = row + (png_size_t)row_width * 4 - 1; nuclear@26: png_bytep dp = sp + (png_size_t)row_width * 4; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *(dp--) = *(sp--); nuclear@26: *(dp--) = *(sp--); nuclear@26: *(dp--) = *sp; nuclear@26: *(dp--) = *(sp - 1); nuclear@26: *(dp--) = *sp; nuclear@26: *(dp--) = *(sp - 1); nuclear@26: *(dp--) = *(sp--); nuclear@26: *(dp--) = *(sp--); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: row_info->channels += (png_byte)2; nuclear@26: row_info->color_type |= PNG_COLOR_MASK_COLOR; nuclear@26: row_info->pixel_depth = (png_byte)(row_info->channels * nuclear@26: row_info->bit_depth); nuclear@26: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) nuclear@26: /* reduce RGB files to grayscale, with or without alpha nuclear@26: * using the equation given in Poynton's ColorFAQ at nuclear@26: * (THIS LINK IS DEAD June 2008) nuclear@26: * New link: nuclear@26: * nuclear@26: * Charles Poynton poynton at poynton.com nuclear@26: * nuclear@26: * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B nuclear@26: * nuclear@26: * We approximate this with nuclear@26: * nuclear@26: * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B nuclear@26: * nuclear@26: * which can be expressed with integers as nuclear@26: * nuclear@26: * Y = (6969 * R + 23434 * G + 2365 * B)/32768 nuclear@26: * nuclear@26: * The calculation is to be done in a linear colorspace. nuclear@26: * nuclear@26: * Other integer coefficents can be used via png_set_rgb_to_gray(). nuclear@26: */ nuclear@26: int /* PRIVATE */ nuclear@26: png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row) nuclear@26: nuclear@26: { nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: png_uint_32 row_width = row_info->width; nuclear@26: int rgb_error = 0; nuclear@26: nuclear@26: png_debug(1, "in png_do_rgb_to_gray\n"); nuclear@26: if ( nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: row != NULL && row_info != NULL && nuclear@26: #endif nuclear@26: (row_info->color_type & PNG_COLOR_MASK_COLOR)) nuclear@26: { nuclear@26: png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; nuclear@26: png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; nuclear@26: png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff; nuclear@26: nuclear@26: if (row_info->color_type == PNG_COLOR_TYPE_RGB) nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) nuclear@26: { nuclear@26: png_bytep sp = row; nuclear@26: png_bytep dp = row; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_byte red = png_ptr->gamma_to_1[*(sp++)]; nuclear@26: png_byte green = png_ptr->gamma_to_1[*(sp++)]; nuclear@26: png_byte blue = png_ptr->gamma_to_1[*(sp++)]; nuclear@26: if (red != green || red != blue) nuclear@26: { nuclear@26: rgb_error |= 1; nuclear@26: *(dp++) = png_ptr->gamma_from_1[ nuclear@26: (rc*red + gc*green + bc*blue)>>15]; nuclear@26: } nuclear@26: else nuclear@26: *(dp++) = *(sp - 1); nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: png_bytep sp = row; nuclear@26: png_bytep dp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_byte red = *(sp++); nuclear@26: png_byte green = *(sp++); nuclear@26: png_byte blue = *(sp++); nuclear@26: if (red != green || red != blue) nuclear@26: { nuclear@26: rgb_error |= 1; nuclear@26: *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); nuclear@26: } nuclear@26: else nuclear@26: *(dp++) = *(sp - 1); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: else /* RGB bit_depth == 16 */ nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: if (png_ptr->gamma_16_to_1 != NULL && nuclear@26: png_ptr->gamma_16_from_1 != NULL) nuclear@26: { nuclear@26: png_bytep sp = row; nuclear@26: png_bytep dp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_uint_16 red, green, blue, w; nuclear@26: nuclear@26: red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; nuclear@26: green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; nuclear@26: blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; nuclear@26: nuclear@26: if (red == green && red == blue) nuclear@26: w = red; nuclear@26: else nuclear@26: { nuclear@26: png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >> nuclear@26: png_ptr->gamma_shift][red>>8]; nuclear@26: png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >> nuclear@26: png_ptr->gamma_shift][green>>8]; nuclear@26: png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >> nuclear@26: png_ptr->gamma_shift][blue>>8]; nuclear@26: png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 nuclear@26: + bc*blue_1)>>15); nuclear@26: w = png_ptr->gamma_16_from_1[(gray16&0xff) >> nuclear@26: png_ptr->gamma_shift][gray16 >> 8]; nuclear@26: rgb_error |= 1; nuclear@26: } nuclear@26: nuclear@26: *(dp++) = (png_byte)((w>>8) & 0xff); nuclear@26: *(dp++) = (png_byte)(w & 0xff); nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: png_bytep sp = row; nuclear@26: png_bytep dp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_uint_16 red, green, blue, gray16; nuclear@26: nuclear@26: red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; nuclear@26: green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; nuclear@26: blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; nuclear@26: nuclear@26: if (red != green || red != blue) nuclear@26: rgb_error |= 1; nuclear@26: gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); nuclear@26: *(dp++) = (png_byte)((gray16>>8) & 0xff); nuclear@26: *(dp++) = (png_byte)(gray16 & 0xff); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) nuclear@26: { nuclear@26: png_bytep sp = row; nuclear@26: png_bytep dp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_byte red = png_ptr->gamma_to_1[*(sp++)]; nuclear@26: png_byte green = png_ptr->gamma_to_1[*(sp++)]; nuclear@26: png_byte blue = png_ptr->gamma_to_1[*(sp++)]; nuclear@26: if (red != green || red != blue) nuclear@26: rgb_error |= 1; nuclear@26: *(dp++) = png_ptr->gamma_from_1 nuclear@26: [(rc*red + gc*green + bc*blue)>>15]; nuclear@26: *(dp++) = *(sp++); /* alpha */ nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: png_bytep sp = row; nuclear@26: png_bytep dp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_byte red = *(sp++); nuclear@26: png_byte green = *(sp++); nuclear@26: png_byte blue = *(sp++); nuclear@26: if (red != green || red != blue) nuclear@26: rgb_error |= 1; nuclear@26: *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); nuclear@26: *(dp++) = *(sp++); /* alpha */ nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else /* RGBA bit_depth == 16 */ nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: if (png_ptr->gamma_16_to_1 != NULL && nuclear@26: png_ptr->gamma_16_from_1 != NULL) nuclear@26: { nuclear@26: png_bytep sp = row; nuclear@26: png_bytep dp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_uint_16 red, green, blue, w; nuclear@26: nuclear@26: red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; nuclear@26: green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; nuclear@26: blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; nuclear@26: nuclear@26: if (red == green && red == blue) nuclear@26: w = red; nuclear@26: else nuclear@26: { nuclear@26: png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >> nuclear@26: png_ptr->gamma_shift][red>>8]; nuclear@26: png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >> nuclear@26: png_ptr->gamma_shift][green>>8]; nuclear@26: png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >> nuclear@26: png_ptr->gamma_shift][blue>>8]; nuclear@26: png_uint_16 gray16 = (png_uint_16)((rc * red_1 nuclear@26: + gc * green_1 + bc * blue_1)>>15); nuclear@26: w = png_ptr->gamma_16_from_1[(gray16&0xff) >> nuclear@26: png_ptr->gamma_shift][gray16 >> 8]; nuclear@26: rgb_error |= 1; nuclear@26: } nuclear@26: nuclear@26: *(dp++) = (png_byte)((w>>8) & 0xff); nuclear@26: *(dp++) = (png_byte)(w & 0xff); nuclear@26: *(dp++) = *(sp++); /* alpha */ nuclear@26: *(dp++) = *(sp++); nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: png_bytep sp = row; nuclear@26: png_bytep dp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_uint_16 red, green, blue, gray16; nuclear@26: red = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; nuclear@26: green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; nuclear@26: blue = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; nuclear@26: if (red != green || red != blue) nuclear@26: rgb_error |= 1; nuclear@26: gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); nuclear@26: *(dp++) = (png_byte)((gray16>>8) & 0xff); nuclear@26: *(dp++) = (png_byte)(gray16 & 0xff); nuclear@26: *(dp++) = *(sp++); /* alpha */ nuclear@26: *(dp++) = *(sp++); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: row_info->channels -= (png_byte)2; nuclear@26: row_info->color_type &= ~PNG_COLOR_MASK_COLOR; nuclear@26: row_info->pixel_depth = (png_byte)(row_info->channels * nuclear@26: row_info->bit_depth); nuclear@26: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); nuclear@26: } nuclear@26: return rgb_error; nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth nuclear@26: * large of png_color. This lets grayscale images be treated as nuclear@26: * paletted. Most useful for gamma correction and simplification nuclear@26: * of code. nuclear@26: */ nuclear@26: void PNGAPI nuclear@26: png_build_grayscale_palette(int bit_depth, png_colorp palette) nuclear@26: { nuclear@26: int num_palette; nuclear@26: int color_inc; nuclear@26: int i; nuclear@26: int v; nuclear@26: nuclear@26: png_debug(1, "in png_do_build_grayscale_palette\n"); nuclear@26: if (palette == NULL) nuclear@26: return; nuclear@26: nuclear@26: switch (bit_depth) nuclear@26: { nuclear@26: case 1: nuclear@26: num_palette = 2; nuclear@26: color_inc = 0xff; nuclear@26: break; nuclear@26: case 2: nuclear@26: num_palette = 4; nuclear@26: color_inc = 0x55; nuclear@26: break; nuclear@26: case 4: nuclear@26: num_palette = 16; nuclear@26: color_inc = 0x11; nuclear@26: break; nuclear@26: case 8: nuclear@26: num_palette = 256; nuclear@26: color_inc = 1; nuclear@26: break; nuclear@26: default: nuclear@26: num_palette = 0; nuclear@26: color_inc = 0; nuclear@26: break; nuclear@26: } nuclear@26: nuclear@26: for (i = 0, v = 0; i < num_palette; i++, v += color_inc) nuclear@26: { nuclear@26: palette[i].red = (png_byte)v; nuclear@26: palette[i].green = (png_byte)v; nuclear@26: palette[i].blue = (png_byte)v; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: /* This function is currently unused. Do we really need it? */ nuclear@26: #if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED) nuclear@26: void /* PRIVATE */ nuclear@26: png_correct_palette(png_structp png_ptr, png_colorp palette, nuclear@26: int num_palette) nuclear@26: { nuclear@26: png_debug(1, "in png_correct_palette\n"); nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ nuclear@26: defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) nuclear@26: if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND)) nuclear@26: { nuclear@26: png_color back, back_1; nuclear@26: nuclear@26: if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) nuclear@26: { nuclear@26: back.red = png_ptr->gamma_table[png_ptr->background.red]; nuclear@26: back.green = png_ptr->gamma_table[png_ptr->background.green]; nuclear@26: back.blue = png_ptr->gamma_table[png_ptr->background.blue]; nuclear@26: nuclear@26: back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; nuclear@26: back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; nuclear@26: back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: double g; nuclear@26: nuclear@26: g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma); nuclear@26: nuclear@26: if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN || nuclear@26: fabs(g - 1.0) < PNG_GAMMA_THRESHOLD) nuclear@26: { nuclear@26: back.red = png_ptr->background.red; nuclear@26: back.green = png_ptr->background.green; nuclear@26: back.blue = png_ptr->background.blue; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: back.red = nuclear@26: (png_byte)(pow((double)png_ptr->background.red/255, g) * nuclear@26: 255.0 + 0.5); nuclear@26: back.green = nuclear@26: (png_byte)(pow((double)png_ptr->background.green/255, g) * nuclear@26: 255.0 + 0.5); nuclear@26: back.blue = nuclear@26: (png_byte)(pow((double)png_ptr->background.blue/255, g) * nuclear@26: 255.0 + 0.5); nuclear@26: } nuclear@26: nuclear@26: g = 1.0 / png_ptr->background_gamma; nuclear@26: nuclear@26: back_1.red = nuclear@26: (png_byte)(pow((double)png_ptr->background.red/255, g) * nuclear@26: 255.0 + 0.5); nuclear@26: back_1.green = nuclear@26: (png_byte)(pow((double)png_ptr->background.green/255, g) * nuclear@26: 255.0 + 0.5); nuclear@26: back_1.blue = nuclear@26: (png_byte)(pow((double)png_ptr->background.blue/255, g) * nuclear@26: 255.0 + 0.5); nuclear@26: } nuclear@26: nuclear@26: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@26: { nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: for (i = 0; i < (png_uint_32)num_palette; i++) nuclear@26: { nuclear@26: if (i < png_ptr->num_trans && png_ptr->trans[i] == 0) nuclear@26: { nuclear@26: palette[i] = back; nuclear@26: } nuclear@26: else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff) nuclear@26: { nuclear@26: png_byte v, w; nuclear@26: nuclear@26: v = png_ptr->gamma_to_1[png_ptr->palette[i].red]; nuclear@26: png_composite(w, v, png_ptr->trans[i], back_1.red); nuclear@26: palette[i].red = png_ptr->gamma_from_1[w]; nuclear@26: nuclear@26: v = png_ptr->gamma_to_1[png_ptr->palette[i].green]; nuclear@26: png_composite(w, v, png_ptr->trans[i], back_1.green); nuclear@26: palette[i].green = png_ptr->gamma_from_1[w]; nuclear@26: nuclear@26: v = png_ptr->gamma_to_1[png_ptr->palette[i].blue]; nuclear@26: png_composite(w, v, png_ptr->trans[i], back_1.blue); nuclear@26: palette[i].blue = png_ptr->gamma_from_1[w]; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: palette[i].red = png_ptr->gamma_table[palette[i].red]; nuclear@26: palette[i].green = png_ptr->gamma_table[palette[i].green]; nuclear@26: palette[i].blue = png_ptr->gamma_table[palette[i].blue]; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: int i; nuclear@26: nuclear@26: for (i = 0; i < num_palette; i++) nuclear@26: { nuclear@26: if (palette[i].red == (png_byte)png_ptr->trans_values.gray) nuclear@26: { nuclear@26: palette[i] = back; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: palette[i].red = png_ptr->gamma_table[palette[i].red]; nuclear@26: palette[i].green = png_ptr->gamma_table[palette[i].green]; nuclear@26: palette[i].blue = png_ptr->gamma_table[palette[i].blue]; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_GAMMA) nuclear@26: { nuclear@26: int i; nuclear@26: nuclear@26: for (i = 0; i < num_palette; i++) nuclear@26: { nuclear@26: palette[i].red = png_ptr->gamma_table[palette[i].red]; nuclear@26: palette[i].green = png_ptr->gamma_table[palette[i].green]; nuclear@26: palette[i].blue = png_ptr->gamma_table[palette[i].blue]; nuclear@26: } nuclear@26: } nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: else nuclear@26: #endif nuclear@26: #endif nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: if (png_ptr->transformations & PNG_BACKGROUND) nuclear@26: { nuclear@26: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@26: { nuclear@26: png_color back; nuclear@26: nuclear@26: back.red = (png_byte)png_ptr->background.red; nuclear@26: back.green = (png_byte)png_ptr->background.green; nuclear@26: back.blue = (png_byte)png_ptr->background.blue; nuclear@26: nuclear@26: for (i = 0; i < (int)png_ptr->num_trans; i++) nuclear@26: { nuclear@26: if (png_ptr->trans[i] == 0) nuclear@26: { nuclear@26: palette[i].red = back.red; nuclear@26: palette[i].green = back.green; nuclear@26: palette[i].blue = back.blue; nuclear@26: } nuclear@26: else if (png_ptr->trans[i] != 0xff) nuclear@26: { nuclear@26: png_composite(palette[i].red, png_ptr->palette[i].red, nuclear@26: png_ptr->trans[i], back.red); nuclear@26: png_composite(palette[i].green, png_ptr->palette[i].green, nuclear@26: png_ptr->trans[i], back.green); nuclear@26: png_composite(palette[i].blue, png_ptr->palette[i].blue, nuclear@26: png_ptr->trans[i], back.blue); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else /* assume grayscale palette (what else could it be?) */ nuclear@26: { nuclear@26: int i; nuclear@26: nuclear@26: for (i = 0; i < num_palette; i++) nuclear@26: { nuclear@26: if (i == (png_byte)png_ptr->trans_values.gray) nuclear@26: { nuclear@26: palette[i].red = (png_byte)png_ptr->background.red; nuclear@26: palette[i].green = (png_byte)png_ptr->background.green; nuclear@26: palette[i].blue = (png_byte)png_ptr->background.blue; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) nuclear@26: /* Replace any alpha or transparency with the supplied background color. nuclear@26: * "background" is already in the screen gamma, while "background_1" is nuclear@26: * at a gamma of 1.0. Paletted files have already been taken care of. nuclear@26: */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_background(png_row_infop row_info, png_bytep row, nuclear@26: png_color_16p trans_values, png_color_16p background nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: , png_color_16p background_1, nuclear@26: png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1, nuclear@26: png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1, nuclear@26: png_uint_16pp gamma_16_to_1, int gamma_shift nuclear@26: #endif nuclear@26: ) nuclear@26: { nuclear@26: png_bytep sp, dp; nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 row_width=row_info->width; nuclear@26: int shift; nuclear@26: nuclear@26: png_debug(1, "in png_do_background\n"); nuclear@26: if (background != NULL && nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: row != NULL && row_info != NULL && nuclear@26: #endif nuclear@26: (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) || nuclear@26: (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values))) nuclear@26: { nuclear@26: switch (row_info->color_type) nuclear@26: { nuclear@26: case PNG_COLOR_TYPE_GRAY: nuclear@26: { nuclear@26: switch (row_info->bit_depth) nuclear@26: { nuclear@26: case 1: nuclear@26: { nuclear@26: sp = row; nuclear@26: shift = 7; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if ((png_uint_16)((*sp >> shift) & 0x01) nuclear@26: == trans_values->gray) nuclear@26: { nuclear@26: *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff); nuclear@26: *sp |= (png_byte)(background->gray << shift); nuclear@26: } nuclear@26: if (!shift) nuclear@26: { nuclear@26: shift = 7; nuclear@26: sp++; nuclear@26: } nuclear@26: else nuclear@26: shift--; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 2: nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (gamma_table != NULL) nuclear@26: { nuclear@26: sp = row; nuclear@26: shift = 6; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if ((png_uint_16)((*sp >> shift) & 0x03) nuclear@26: == trans_values->gray) nuclear@26: { nuclear@26: *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); nuclear@26: *sp |= (png_byte)(background->gray << shift); nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_byte p = (png_byte)((*sp >> shift) & 0x03); nuclear@26: png_byte g = (png_byte)((gamma_table [p | (p << 2) | nuclear@26: (p << 4) | (p << 6)] >> 6) & 0x03); nuclear@26: *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); nuclear@26: *sp |= (png_byte)(g << shift); nuclear@26: } nuclear@26: if (!shift) nuclear@26: { nuclear@26: shift = 6; nuclear@26: sp++; nuclear@26: } nuclear@26: else nuclear@26: shift -= 2; nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: sp = row; nuclear@26: shift = 6; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if ((png_uint_16)((*sp >> shift) & 0x03) nuclear@26: == trans_values->gray) nuclear@26: { nuclear@26: *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff); nuclear@26: *sp |= (png_byte)(background->gray << shift); nuclear@26: } nuclear@26: if (!shift) nuclear@26: { nuclear@26: shift = 6; nuclear@26: sp++; nuclear@26: } nuclear@26: else nuclear@26: shift -= 2; nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 4: nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (gamma_table != NULL) nuclear@26: { nuclear@26: sp = row; nuclear@26: shift = 4; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if ((png_uint_16)((*sp >> shift) & 0x0f) nuclear@26: == trans_values->gray) nuclear@26: { nuclear@26: *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); nuclear@26: *sp |= (png_byte)(background->gray << shift); nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_byte p = (png_byte)((*sp >> shift) & 0x0f); nuclear@26: png_byte g = (png_byte)((gamma_table[p | nuclear@26: (p << 4)] >> 4) & 0x0f); nuclear@26: *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); nuclear@26: *sp |= (png_byte)(g << shift); nuclear@26: } nuclear@26: if (!shift) nuclear@26: { nuclear@26: shift = 4; nuclear@26: sp++; nuclear@26: } nuclear@26: else nuclear@26: shift -= 4; nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: sp = row; nuclear@26: shift = 4; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if ((png_uint_16)((*sp >> shift) & 0x0f) nuclear@26: == trans_values->gray) nuclear@26: { nuclear@26: *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff); nuclear@26: *sp |= (png_byte)(background->gray << shift); nuclear@26: } nuclear@26: if (!shift) nuclear@26: { nuclear@26: shift = 4; nuclear@26: sp++; nuclear@26: } nuclear@26: else nuclear@26: shift -= 4; nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 8: nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (gamma_table != NULL) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++, sp++) nuclear@26: { nuclear@26: if (*sp == trans_values->gray) nuclear@26: { nuclear@26: *sp = (png_byte)background->gray; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: *sp = gamma_table[*sp]; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++, sp++) nuclear@26: { nuclear@26: if (*sp == trans_values->gray) nuclear@26: { nuclear@26: *sp = (png_byte)background->gray; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 16: nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (gamma_16 != NULL) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 2) nuclear@26: { nuclear@26: png_uint_16 v; nuclear@26: nuclear@26: v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); nuclear@26: if (v == trans_values->gray) nuclear@26: { nuclear@26: /* background is already in screen gamma */ nuclear@26: *sp = (png_byte)((background->gray >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(background->gray & 0xff); nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *sp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(v & 0xff); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 2) nuclear@26: { nuclear@26: png_uint_16 v; nuclear@26: nuclear@26: v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); nuclear@26: if (v == trans_values->gray) nuclear@26: { nuclear@26: *sp = (png_byte)((background->gray >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(background->gray & 0xff); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case PNG_COLOR_TYPE_RGB: nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (gamma_table != NULL) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 3) nuclear@26: { nuclear@26: if (*sp == trans_values->red && nuclear@26: *(sp + 1) == trans_values->green && nuclear@26: *(sp + 2) == trans_values->blue) nuclear@26: { nuclear@26: *sp = (png_byte)background->red; nuclear@26: *(sp + 1) = (png_byte)background->green; nuclear@26: *(sp + 2) = (png_byte)background->blue; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: *sp = gamma_table[*sp]; nuclear@26: *(sp + 1) = gamma_table[*(sp + 1)]; nuclear@26: *(sp + 2) = gamma_table[*(sp + 2)]; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 3) nuclear@26: { nuclear@26: if (*sp == trans_values->red && nuclear@26: *(sp + 1) == trans_values->green && nuclear@26: *(sp + 2) == trans_values->blue) nuclear@26: { nuclear@26: *sp = (png_byte)background->red; nuclear@26: *(sp + 1) = (png_byte)background->green; nuclear@26: *(sp + 2) = (png_byte)background->blue; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else /* if (row_info->bit_depth == 16) */ nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (gamma_16 != NULL) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 6) nuclear@26: { nuclear@26: png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); nuclear@26: png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); nuclear@26: png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); nuclear@26: if (r == trans_values->red && g == trans_values->green && nuclear@26: b == trans_values->blue) nuclear@26: { nuclear@26: /* background is already in screen gamma */ nuclear@26: *sp = (png_byte)((background->red >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(background->red & 0xff); nuclear@26: *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); nuclear@26: *(sp + 3) = (png_byte)(background->green & 0xff); nuclear@26: *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); nuclear@26: *(sp + 5) = (png_byte)(background->blue & 0xff); nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *sp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(v & 0xff); nuclear@26: v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; nuclear@26: *(sp + 2) = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 3) = (png_byte)(v & 0xff); nuclear@26: v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; nuclear@26: *(sp + 4) = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 5) = (png_byte)(v & 0xff); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 6) nuclear@26: { nuclear@26: png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1)); nuclear@26: png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); nuclear@26: png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5)); nuclear@26: nuclear@26: if (r == trans_values->red && g == trans_values->green && nuclear@26: b == trans_values->blue) nuclear@26: { nuclear@26: *sp = (png_byte)((background->red >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(background->red & 0xff); nuclear@26: *(sp + 2) = (png_byte)((background->green >> 8) & 0xff); nuclear@26: *(sp + 3) = (png_byte)(background->green & 0xff); nuclear@26: *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff); nuclear@26: *(sp + 5) = (png_byte)(background->blue & 0xff); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case PNG_COLOR_TYPE_GRAY_ALPHA: nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (gamma_to_1 != NULL && gamma_from_1 != NULL && nuclear@26: gamma_table != NULL) nuclear@26: { nuclear@26: sp = row; nuclear@26: dp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 2, dp++) nuclear@26: { nuclear@26: png_uint_16 a = *(sp + 1); nuclear@26: nuclear@26: if (a == 0xff) nuclear@26: { nuclear@26: *dp = gamma_table[*sp]; nuclear@26: } nuclear@26: else if (a == 0) nuclear@26: { nuclear@26: /* background is already in screen gamma */ nuclear@26: *dp = (png_byte)background->gray; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_byte v, w; nuclear@26: nuclear@26: v = gamma_to_1[*sp]; nuclear@26: png_composite(w, v, a, background_1->gray); nuclear@26: *dp = gamma_from_1[w]; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: sp = row; nuclear@26: dp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 2, dp++) nuclear@26: { nuclear@26: png_byte a = *(sp + 1); nuclear@26: nuclear@26: if (a == 0xff) nuclear@26: { nuclear@26: *dp = *sp; nuclear@26: } nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: else if (a == 0) nuclear@26: { nuclear@26: *dp = (png_byte)background->gray; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_composite(*dp, *sp, a, background_1->gray); nuclear@26: } nuclear@26: #else nuclear@26: *dp = (png_byte)background->gray; nuclear@26: #endif nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else /* if (png_ptr->bit_depth == 16) */ nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (gamma_16 != NULL && gamma_16_from_1 != NULL && nuclear@26: gamma_16_to_1 != NULL) nuclear@26: { nuclear@26: sp = row; nuclear@26: dp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 4, dp += 2) nuclear@26: { nuclear@26: png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); nuclear@26: nuclear@26: if (a == (png_uint_16)0xffff) nuclear@26: { nuclear@26: png_uint_16 v; nuclear@26: nuclear@26: v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *dp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(dp + 1) = (png_byte)(v & 0xff); nuclear@26: } nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: else if (a == 0) nuclear@26: #else nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: /* background is already in screen gamma */ nuclear@26: *dp = (png_byte)((background->gray >> 8) & 0xff); nuclear@26: *(dp + 1) = (png_byte)(background->gray & 0xff); nuclear@26: } nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: else nuclear@26: { nuclear@26: png_uint_16 g, v, w; nuclear@26: nuclear@26: g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: png_composite_16(v, g, a, background_1->gray); nuclear@26: w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8]; nuclear@26: *dp = (png_byte)((w >> 8) & 0xff); nuclear@26: *(dp + 1) = (png_byte)(w & 0xff); nuclear@26: } nuclear@26: #endif nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: sp = row; nuclear@26: dp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 4, dp += 2) nuclear@26: { nuclear@26: png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3)); nuclear@26: if (a == (png_uint_16)0xffff) nuclear@26: { nuclear@26: png_memcpy(dp, sp, 2); nuclear@26: } nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: else if (a == 0) nuclear@26: #else nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: *dp = (png_byte)((background->gray >> 8) & 0xff); nuclear@26: *(dp + 1) = (png_byte)(background->gray & 0xff); nuclear@26: } nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: else nuclear@26: { nuclear@26: png_uint_16 g, v; nuclear@26: nuclear@26: g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); nuclear@26: png_composite_16(v, g, a, background_1->gray); nuclear@26: *dp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(dp + 1) = (png_byte)(v & 0xff); nuclear@26: } nuclear@26: #endif nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case PNG_COLOR_TYPE_RGB_ALPHA: nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (gamma_to_1 != NULL && gamma_from_1 != NULL && nuclear@26: gamma_table != NULL) nuclear@26: { nuclear@26: sp = row; nuclear@26: dp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 4, dp += 3) nuclear@26: { nuclear@26: png_byte a = *(sp + 3); nuclear@26: nuclear@26: if (a == 0xff) nuclear@26: { nuclear@26: *dp = gamma_table[*sp]; nuclear@26: *(dp + 1) = gamma_table[*(sp + 1)]; nuclear@26: *(dp + 2) = gamma_table[*(sp + 2)]; nuclear@26: } nuclear@26: else if (a == 0) nuclear@26: { nuclear@26: /* background is already in screen gamma */ nuclear@26: *dp = (png_byte)background->red; nuclear@26: *(dp + 1) = (png_byte)background->green; nuclear@26: *(dp + 2) = (png_byte)background->blue; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_byte v, w; nuclear@26: nuclear@26: v = gamma_to_1[*sp]; nuclear@26: png_composite(w, v, a, background_1->red); nuclear@26: *dp = gamma_from_1[w]; nuclear@26: v = gamma_to_1[*(sp + 1)]; nuclear@26: png_composite(w, v, a, background_1->green); nuclear@26: *(dp + 1) = gamma_from_1[w]; nuclear@26: v = gamma_to_1[*(sp + 2)]; nuclear@26: png_composite(w, v, a, background_1->blue); nuclear@26: *(dp + 2) = gamma_from_1[w]; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: sp = row; nuclear@26: dp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 4, dp += 3) nuclear@26: { nuclear@26: png_byte a = *(sp + 3); nuclear@26: nuclear@26: if (a == 0xff) nuclear@26: { nuclear@26: *dp = *sp; nuclear@26: *(dp + 1) = *(sp + 1); nuclear@26: *(dp + 2) = *(sp + 2); nuclear@26: } nuclear@26: else if (a == 0) nuclear@26: { nuclear@26: *dp = (png_byte)background->red; nuclear@26: *(dp + 1) = (png_byte)background->green; nuclear@26: *(dp + 2) = (png_byte)background->blue; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_composite(*dp, *sp, a, background->red); nuclear@26: png_composite(*(dp + 1), *(sp + 1), a, nuclear@26: background->green); nuclear@26: png_composite(*(dp + 2), *(sp + 2), a, nuclear@26: background->blue); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else /* if (row_info->bit_depth == 16) */ nuclear@26: { nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: if (gamma_16 != NULL && gamma_16_from_1 != NULL && nuclear@26: gamma_16_to_1 != NULL) nuclear@26: { nuclear@26: sp = row; nuclear@26: dp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 8, dp += 6) nuclear@26: { nuclear@26: png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) nuclear@26: << 8) + (png_uint_16)(*(sp + 7))); nuclear@26: if (a == (png_uint_16)0xffff) nuclear@26: { nuclear@26: png_uint_16 v; nuclear@26: nuclear@26: v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *dp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(dp + 1) = (png_byte)(v & 0xff); nuclear@26: v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; nuclear@26: *(dp + 2) = (png_byte)((v >> 8) & 0xff); nuclear@26: *(dp + 3) = (png_byte)(v & 0xff); nuclear@26: v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; nuclear@26: *(dp + 4) = (png_byte)((v >> 8) & 0xff); nuclear@26: *(dp + 5) = (png_byte)(v & 0xff); nuclear@26: } nuclear@26: else if (a == 0) nuclear@26: { nuclear@26: /* background is already in screen gamma */ nuclear@26: *dp = (png_byte)((background->red >> 8) & 0xff); nuclear@26: *(dp + 1) = (png_byte)(background->red & 0xff); nuclear@26: *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); nuclear@26: *(dp + 3) = (png_byte)(background->green & 0xff); nuclear@26: *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); nuclear@26: *(dp + 5) = (png_byte)(background->blue & 0xff); nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_uint_16 v, w, x; nuclear@26: nuclear@26: v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: png_composite_16(w, v, a, background_1->red); nuclear@26: x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; nuclear@26: *dp = (png_byte)((x >> 8) & 0xff); nuclear@26: *(dp + 1) = (png_byte)(x & 0xff); nuclear@26: v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; nuclear@26: png_composite_16(w, v, a, background_1->green); nuclear@26: x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8]; nuclear@26: *(dp + 2) = (png_byte)((x >> 8) & 0xff); nuclear@26: *(dp + 3) = (png_byte)(x & 0xff); nuclear@26: v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; nuclear@26: png_composite_16(w, v, a, background_1->blue); nuclear@26: x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8]; nuclear@26: *(dp + 4) = (png_byte)((x >> 8) & 0xff); nuclear@26: *(dp + 5) = (png_byte)(x & 0xff); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: #endif nuclear@26: { nuclear@26: sp = row; nuclear@26: dp = row; nuclear@26: for (i = 0; i < row_width; i++, sp += 8, dp += 6) nuclear@26: { nuclear@26: png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) nuclear@26: << 8) + (png_uint_16)(*(sp + 7))); nuclear@26: if (a == (png_uint_16)0xffff) nuclear@26: { nuclear@26: png_memcpy(dp, sp, 6); nuclear@26: } nuclear@26: else if (a == 0) nuclear@26: { nuclear@26: *dp = (png_byte)((background->red >> 8) & 0xff); nuclear@26: *(dp + 1) = (png_byte)(background->red & 0xff); nuclear@26: *(dp + 2) = (png_byte)((background->green >> 8) & 0xff); nuclear@26: *(dp + 3) = (png_byte)(background->green & 0xff); nuclear@26: *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff); nuclear@26: *(dp + 5) = (png_byte)(background->blue & 0xff); nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: png_uint_16 v; nuclear@26: nuclear@26: png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); nuclear@26: png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) nuclear@26: + *(sp + 3)); nuclear@26: png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) nuclear@26: + *(sp + 5)); nuclear@26: nuclear@26: png_composite_16(v, r, a, background->red); nuclear@26: *dp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(dp + 1) = (png_byte)(v & 0xff); nuclear@26: png_composite_16(v, g, a, background->green); nuclear@26: *(dp + 2) = (png_byte)((v >> 8) & 0xff); nuclear@26: *(dp + 3) = (png_byte)(v & 0xff); nuclear@26: png_composite_16(v, b, a, background->blue); nuclear@26: *(dp + 4) = (png_byte)((v >> 8) & 0xff); nuclear@26: *(dp + 5) = (png_byte)(v & 0xff); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: if (row_info->color_type & PNG_COLOR_MASK_ALPHA) nuclear@26: { nuclear@26: row_info->color_type &= ~PNG_COLOR_MASK_ALPHA; nuclear@26: row_info->channels--; nuclear@26: row_info->pixel_depth = (png_byte)(row_info->channels * nuclear@26: row_info->bit_depth); nuclear@26: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: /* Gamma correct the image, avoiding the alpha channel. Make sure nuclear@26: * you do this after you deal with the transparency issue on grayscale nuclear@26: * or RGB images. If your bit depth is 8, use gamma_table, if it nuclear@26: * is 16, use gamma_16_table and gamma_shift. Build these with nuclear@26: * build_gamma_table(). nuclear@26: */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_gamma(png_row_infop row_info, png_bytep row, nuclear@26: png_bytep gamma_table, png_uint_16pp gamma_16_table, nuclear@26: int gamma_shift) nuclear@26: { nuclear@26: png_bytep sp; nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 row_width=row_info->width; nuclear@26: nuclear@26: png_debug(1, "in png_do_gamma\n"); nuclear@26: if ( nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: row != NULL && row_info != NULL && nuclear@26: #endif nuclear@26: ((row_info->bit_depth <= 8 && gamma_table != NULL) || nuclear@26: (row_info->bit_depth == 16 && gamma_16_table != NULL))) nuclear@26: { nuclear@26: switch (row_info->color_type) nuclear@26: { nuclear@26: case PNG_COLOR_TYPE_RGB: nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *sp = gamma_table[*sp]; nuclear@26: sp++; nuclear@26: *sp = gamma_table[*sp]; nuclear@26: sp++; nuclear@26: *sp = gamma_table[*sp]; nuclear@26: sp++; nuclear@26: } nuclear@26: } nuclear@26: else /* if (row_info->bit_depth == 16) */ nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_uint_16 v; nuclear@26: nuclear@26: v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *sp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(v & 0xff); nuclear@26: sp += 2; nuclear@26: v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *sp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(v & 0xff); nuclear@26: sp += 2; nuclear@26: v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *sp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(v & 0xff); nuclear@26: sp += 2; nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case PNG_COLOR_TYPE_RGB_ALPHA: nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *sp = gamma_table[*sp]; nuclear@26: sp++; nuclear@26: *sp = gamma_table[*sp]; nuclear@26: sp++; nuclear@26: *sp = gamma_table[*sp]; nuclear@26: sp++; nuclear@26: sp++; nuclear@26: } nuclear@26: } nuclear@26: else /* if (row_info->bit_depth == 16) */ nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *sp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(v & 0xff); nuclear@26: sp += 2; nuclear@26: v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *sp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(v & 0xff); nuclear@26: sp += 2; nuclear@26: v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *sp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(v & 0xff); nuclear@26: sp += 4; nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case PNG_COLOR_TYPE_GRAY_ALPHA: nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *sp = gamma_table[*sp]; nuclear@26: sp += 2; nuclear@26: } nuclear@26: } nuclear@26: else /* if (row_info->bit_depth == 16) */ nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *sp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(v & 0xff); nuclear@26: sp += 4; nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case PNG_COLOR_TYPE_GRAY: nuclear@26: { nuclear@26: if (row_info->bit_depth == 2) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i += 4) nuclear@26: { nuclear@26: int a = *sp & 0xc0; nuclear@26: int b = *sp & 0x30; nuclear@26: int c = *sp & 0x0c; nuclear@26: int d = *sp & 0x03; nuclear@26: nuclear@26: *sp = (png_byte)( nuclear@26: ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)| nuclear@26: ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)| nuclear@26: ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)| nuclear@26: ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); nuclear@26: sp++; nuclear@26: } nuclear@26: } nuclear@26: if (row_info->bit_depth == 4) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i += 2) nuclear@26: { nuclear@26: int msb = *sp & 0xf0; nuclear@26: int lsb = *sp & 0x0f; nuclear@26: nuclear@26: *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) nuclear@26: | (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); nuclear@26: sp++; nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *sp = gamma_table[*sp]; nuclear@26: sp++; nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->bit_depth == 16) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; nuclear@26: *sp = (png_byte)((v >> 8) & 0xff); nuclear@26: *(sp + 1) = (png_byte)(v & 0xff); nuclear@26: sp += 2; nuclear@26: } nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_EXPAND_SUPPORTED) nuclear@26: /* Expands a palette row to an RGB or RGBA row depending nuclear@26: * upon whether you supply trans and num_trans. nuclear@26: */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_expand_palette(png_row_infop row_info, png_bytep row, nuclear@26: png_colorp palette, png_bytep trans, int num_trans) nuclear@26: { nuclear@26: int shift, value; nuclear@26: png_bytep sp, dp; nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 row_width=row_info->width; nuclear@26: nuclear@26: png_debug(1, "in png_do_expand_palette\n"); nuclear@26: if ( nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: row != NULL && row_info != NULL && nuclear@26: #endif nuclear@26: row_info->color_type == PNG_COLOR_TYPE_PALETTE) nuclear@26: { nuclear@26: if (row_info->bit_depth < 8) nuclear@26: { nuclear@26: switch (row_info->bit_depth) nuclear@26: { nuclear@26: case 1: nuclear@26: { nuclear@26: sp = row + (png_size_t)((row_width - 1) >> 3); nuclear@26: dp = row + (png_size_t)row_width - 1; nuclear@26: shift = 7 - (int)((row_width + 7) & 0x07); nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if ((*sp >> shift) & 0x01) nuclear@26: *dp = 1; nuclear@26: else nuclear@26: *dp = 0; nuclear@26: if (shift == 7) nuclear@26: { nuclear@26: shift = 0; nuclear@26: sp--; nuclear@26: } nuclear@26: else nuclear@26: shift++; nuclear@26: nuclear@26: dp--; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 2: nuclear@26: { nuclear@26: sp = row + (png_size_t)((row_width - 1) >> 2); nuclear@26: dp = row + (png_size_t)row_width - 1; nuclear@26: shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: value = (*sp >> shift) & 0x03; nuclear@26: *dp = (png_byte)value; nuclear@26: if (shift == 6) nuclear@26: { nuclear@26: shift = 0; nuclear@26: sp--; nuclear@26: } nuclear@26: else nuclear@26: shift += 2; nuclear@26: nuclear@26: dp--; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 4: nuclear@26: { nuclear@26: sp = row + (png_size_t)((row_width - 1) >> 1); nuclear@26: dp = row + (png_size_t)row_width - 1; nuclear@26: shift = (int)((row_width & 0x01) << 2); nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: value = (*sp >> shift) & 0x0f; nuclear@26: *dp = (png_byte)value; nuclear@26: if (shift == 4) nuclear@26: { nuclear@26: shift = 0; nuclear@26: sp--; nuclear@26: } nuclear@26: else nuclear@26: shift += 4; nuclear@26: nuclear@26: dp--; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: row_info->bit_depth = 8; nuclear@26: row_info->pixel_depth = 8; nuclear@26: row_info->rowbytes = row_width; nuclear@26: } nuclear@26: switch (row_info->bit_depth) nuclear@26: { nuclear@26: case 8: nuclear@26: { nuclear@26: if (trans != NULL) nuclear@26: { nuclear@26: sp = row + (png_size_t)row_width - 1; nuclear@26: dp = row + (png_size_t)(row_width << 2) - 1; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if ((int)(*sp) >= num_trans) nuclear@26: *dp-- = 0xff; nuclear@26: else nuclear@26: *dp-- = trans[*sp]; nuclear@26: *dp-- = palette[*sp].blue; nuclear@26: *dp-- = palette[*sp].green; nuclear@26: *dp-- = palette[*sp].red; nuclear@26: sp--; nuclear@26: } nuclear@26: row_info->bit_depth = 8; nuclear@26: row_info->pixel_depth = 32; nuclear@26: row_info->rowbytes = row_width * 4; nuclear@26: row_info->color_type = 6; nuclear@26: row_info->channels = 4; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: sp = row + (png_size_t)row_width - 1; nuclear@26: dp = row + (png_size_t)(row_width * 3) - 1; nuclear@26: nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: *dp-- = palette[*sp].blue; nuclear@26: *dp-- = palette[*sp].green; nuclear@26: *dp-- = palette[*sp].red; nuclear@26: sp--; nuclear@26: } nuclear@26: row_info->bit_depth = 8; nuclear@26: row_info->pixel_depth = 24; nuclear@26: row_info->rowbytes = row_width * 3; nuclear@26: row_info->color_type = 2; nuclear@26: row_info->channels = 3; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: /* If the bit depth < 8, it is expanded to 8. Also, if the already nuclear@26: * expanded transparency value is supplied, an alpha channel is built. nuclear@26: */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_expand(png_row_infop row_info, png_bytep row, nuclear@26: png_color_16p trans_value) nuclear@26: { nuclear@26: int shift, value; nuclear@26: png_bytep sp, dp; nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 row_width=row_info->width; nuclear@26: nuclear@26: png_debug(1, "in png_do_expand\n"); nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: if (row != NULL && row_info != NULL) nuclear@26: #endif nuclear@26: { nuclear@26: if (row_info->color_type == PNG_COLOR_TYPE_GRAY) nuclear@26: { nuclear@26: png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0); nuclear@26: nuclear@26: if (row_info->bit_depth < 8) nuclear@26: { nuclear@26: switch (row_info->bit_depth) nuclear@26: { nuclear@26: case 1: nuclear@26: { nuclear@26: gray = (png_uint_16)((gray&0x01)*0xff); nuclear@26: sp = row + (png_size_t)((row_width - 1) >> 3); nuclear@26: dp = row + (png_size_t)row_width - 1; nuclear@26: shift = 7 - (int)((row_width + 7) & 0x07); nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if ((*sp >> shift) & 0x01) nuclear@26: *dp = 0xff; nuclear@26: else nuclear@26: *dp = 0; nuclear@26: if (shift == 7) nuclear@26: { nuclear@26: shift = 0; nuclear@26: sp--; nuclear@26: } nuclear@26: else nuclear@26: shift++; nuclear@26: nuclear@26: dp--; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 2: nuclear@26: { nuclear@26: gray = (png_uint_16)((gray&0x03)*0x55); nuclear@26: sp = row + (png_size_t)((row_width - 1) >> 2); nuclear@26: dp = row + (png_size_t)row_width - 1; nuclear@26: shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: value = (*sp >> shift) & 0x03; nuclear@26: *dp = (png_byte)(value | (value << 2) | (value << 4) | nuclear@26: (value << 6)); nuclear@26: if (shift == 6) nuclear@26: { nuclear@26: shift = 0; nuclear@26: sp--; nuclear@26: } nuclear@26: else nuclear@26: shift += 2; nuclear@26: nuclear@26: dp--; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: case 4: nuclear@26: { nuclear@26: gray = (png_uint_16)((gray&0x0f)*0x11); nuclear@26: sp = row + (png_size_t)((row_width - 1) >> 1); nuclear@26: dp = row + (png_size_t)row_width - 1; nuclear@26: shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: value = (*sp >> shift) & 0x0f; nuclear@26: *dp = (png_byte)(value | (value << 4)); nuclear@26: if (shift == 4) nuclear@26: { nuclear@26: shift = 0; nuclear@26: sp--; nuclear@26: } nuclear@26: else nuclear@26: shift = 4; nuclear@26: nuclear@26: dp--; nuclear@26: } nuclear@26: break; nuclear@26: } nuclear@26: } nuclear@26: row_info->bit_depth = 8; nuclear@26: row_info->pixel_depth = 8; nuclear@26: row_info->rowbytes = row_width; nuclear@26: } nuclear@26: nuclear@26: if (trans_value != NULL) nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: gray = gray & 0xff; nuclear@26: sp = row + (png_size_t)row_width - 1; nuclear@26: dp = row + (png_size_t)(row_width << 1) - 1; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if (*sp == gray) nuclear@26: *dp-- = 0; nuclear@26: else nuclear@26: *dp-- = 0xff; nuclear@26: *dp-- = *sp--; nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->bit_depth == 16) nuclear@26: { nuclear@26: png_byte gray_high = (gray >> 8) & 0xff; nuclear@26: png_byte gray_low = gray & 0xff; nuclear@26: sp = row + row_info->rowbytes - 1; nuclear@26: dp = row + (row_info->rowbytes << 1) - 1; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if (*(sp - 1) == gray_high && *(sp) == gray_low) nuclear@26: { nuclear@26: *dp-- = 0; nuclear@26: *dp-- = 0; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: *dp-- = 0xff; nuclear@26: *dp-- = 0xff; nuclear@26: } nuclear@26: *dp-- = *sp--; nuclear@26: *dp-- = *sp--; nuclear@26: } nuclear@26: } nuclear@26: row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; nuclear@26: row_info->channels = 2; nuclear@26: row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); nuclear@26: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, nuclear@26: row_width); nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value) nuclear@26: { nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: png_byte red = trans_value->red & 0xff; nuclear@26: png_byte green = trans_value->green & 0xff; nuclear@26: png_byte blue = trans_value->blue & 0xff; nuclear@26: sp = row + (png_size_t)row_info->rowbytes - 1; nuclear@26: dp = row + (png_size_t)(row_width << 2) - 1; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue) nuclear@26: *dp-- = 0; nuclear@26: else nuclear@26: *dp-- = 0xff; nuclear@26: *dp-- = *sp--; nuclear@26: *dp-- = *sp--; nuclear@26: *dp-- = *sp--; nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->bit_depth == 16) nuclear@26: { nuclear@26: png_byte red_high = (trans_value->red >> 8) & 0xff; nuclear@26: png_byte green_high = (trans_value->green >> 8) & 0xff; nuclear@26: png_byte blue_high = (trans_value->blue >> 8) & 0xff; nuclear@26: png_byte red_low = trans_value->red & 0xff; nuclear@26: png_byte green_low = trans_value->green & 0xff; nuclear@26: png_byte blue_low = trans_value->blue & 0xff; nuclear@26: sp = row + row_info->rowbytes - 1; nuclear@26: dp = row + (png_size_t)(row_width << 3) - 1; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: if (*(sp - 5) == red_high && nuclear@26: *(sp - 4) == red_low && nuclear@26: *(sp - 3) == green_high && nuclear@26: *(sp - 2) == green_low && nuclear@26: *(sp - 1) == blue_high && nuclear@26: *(sp ) == blue_low) nuclear@26: { nuclear@26: *dp-- = 0; nuclear@26: *dp-- = 0; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: *dp-- = 0xff; nuclear@26: *dp-- = 0xff; nuclear@26: } nuclear@26: *dp-- = *sp--; nuclear@26: *dp-- = *sp--; nuclear@26: *dp-- = *sp--; nuclear@26: *dp-- = *sp--; nuclear@26: *dp-- = *sp--; nuclear@26: *dp-- = *sp--; nuclear@26: } nuclear@26: } nuclear@26: row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA; nuclear@26: row_info->channels = 4; nuclear@26: row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); nuclear@26: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_READ_DITHER_SUPPORTED) nuclear@26: void /* PRIVATE */ nuclear@26: png_do_dither(png_row_infop row_info, png_bytep row, nuclear@26: png_bytep palette_lookup, png_bytep dither_lookup) nuclear@26: { nuclear@26: png_bytep sp, dp; nuclear@26: png_uint_32 i; nuclear@26: png_uint_32 row_width=row_info->width; nuclear@26: nuclear@26: png_debug(1, "in png_do_dither\n"); nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: if (row != NULL && row_info != NULL) nuclear@26: #endif nuclear@26: { nuclear@26: if (row_info->color_type == PNG_COLOR_TYPE_RGB && nuclear@26: palette_lookup && row_info->bit_depth == 8) nuclear@26: { nuclear@26: int r, g, b, p; nuclear@26: sp = row; nuclear@26: dp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: r = *sp++; nuclear@26: g = *sp++; nuclear@26: b = *sp++; nuclear@26: nuclear@26: /* this looks real messy, but the compiler will reduce nuclear@26: it down to a reasonable formula. For example, with nuclear@26: 5 bits per color, we get: nuclear@26: p = (((r >> 3) & 0x1f) << 10) | nuclear@26: (((g >> 3) & 0x1f) << 5) | nuclear@26: ((b >> 3) & 0x1f); nuclear@26: */ nuclear@26: p = (((r >> (8 - PNG_DITHER_RED_BITS)) & nuclear@26: ((1 << PNG_DITHER_RED_BITS) - 1)) << nuclear@26: (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | nuclear@26: (((g >> (8 - PNG_DITHER_GREEN_BITS)) & nuclear@26: ((1 << PNG_DITHER_GREEN_BITS) - 1)) << nuclear@26: (PNG_DITHER_BLUE_BITS)) | nuclear@26: ((b >> (8 - PNG_DITHER_BLUE_BITS)) & nuclear@26: ((1 << PNG_DITHER_BLUE_BITS) - 1)); nuclear@26: nuclear@26: *dp++ = palette_lookup[p]; nuclear@26: } nuclear@26: row_info->color_type = PNG_COLOR_TYPE_PALETTE; nuclear@26: row_info->channels = 1; nuclear@26: row_info->pixel_depth = row_info->bit_depth; nuclear@26: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); nuclear@26: } nuclear@26: else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && nuclear@26: palette_lookup != NULL && row_info->bit_depth == 8) nuclear@26: { nuclear@26: int r, g, b, p; nuclear@26: sp = row; nuclear@26: dp = row; nuclear@26: for (i = 0; i < row_width; i++) nuclear@26: { nuclear@26: r = *sp++; nuclear@26: g = *sp++; nuclear@26: b = *sp++; nuclear@26: sp++; nuclear@26: nuclear@26: p = (((r >> (8 - PNG_DITHER_RED_BITS)) & nuclear@26: ((1 << PNG_DITHER_RED_BITS) - 1)) << nuclear@26: (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) | nuclear@26: (((g >> (8 - PNG_DITHER_GREEN_BITS)) & nuclear@26: ((1 << PNG_DITHER_GREEN_BITS) - 1)) << nuclear@26: (PNG_DITHER_BLUE_BITS)) | nuclear@26: ((b >> (8 - PNG_DITHER_BLUE_BITS)) & nuclear@26: ((1 << PNG_DITHER_BLUE_BITS) - 1)); nuclear@26: nuclear@26: *dp++ = palette_lookup[p]; nuclear@26: } nuclear@26: row_info->color_type = PNG_COLOR_TYPE_PALETTE; nuclear@26: row_info->channels = 1; nuclear@26: row_info->pixel_depth = row_info->bit_depth; nuclear@26: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); nuclear@26: } nuclear@26: else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && nuclear@26: dither_lookup && row_info->bit_depth == 8) nuclear@26: { nuclear@26: sp = row; nuclear@26: for (i = 0; i < row_width; i++, sp++) nuclear@26: { nuclear@26: *sp = dither_lookup[*sp]; nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: nuclear@26: #ifdef PNG_FLOATING_POINT_SUPPORTED nuclear@26: #if defined(PNG_READ_GAMMA_SUPPORTED) nuclear@26: static PNG_CONST int png_gamma_shift[] = nuclear@26: {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00}; nuclear@26: nuclear@26: /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit nuclear@26: * tables, we don't make a full table if we are reducing to 8-bit in nuclear@26: * the future. Note also how the gamma_16 tables are segmented so that nuclear@26: * we don't need to allocate > 64K chunks for a full 16-bit table. nuclear@26: */ nuclear@26: void /* PRIVATE */ nuclear@26: png_build_gamma_table(png_structp png_ptr) nuclear@26: { nuclear@26: png_debug(1, "in png_build_gamma_table\n"); nuclear@26: nuclear@26: if (png_ptr->bit_depth <= 8) nuclear@26: { nuclear@26: int i; nuclear@26: double g; nuclear@26: nuclear@26: if (png_ptr->screen_gamma > .000001) nuclear@26: g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); nuclear@26: else nuclear@26: g = 1.0; nuclear@26: nuclear@26: png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr, nuclear@26: (png_uint_32)256); nuclear@26: nuclear@26: for (i = 0; i < 256; i++) nuclear@26: { nuclear@26: png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0, nuclear@26: g) * 255.0 + .5); nuclear@26: } nuclear@26: nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ nuclear@26: defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) nuclear@26: if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY)) nuclear@26: { nuclear@26: nuclear@26: g = 1.0 / (png_ptr->gamma); nuclear@26: nuclear@26: png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr, nuclear@26: (png_uint_32)256); nuclear@26: nuclear@26: for (i = 0; i < 256; i++) nuclear@26: { nuclear@26: png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0, nuclear@26: g) * 255.0 + .5); nuclear@26: } nuclear@26: nuclear@26: nuclear@26: png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr, nuclear@26: (png_uint_32)256); nuclear@26: nuclear@26: if (png_ptr->screen_gamma > 0.000001) nuclear@26: g = 1.0 / png_ptr->screen_gamma; nuclear@26: else nuclear@26: g = png_ptr->gamma; /* probably doing rgb_to_gray */ nuclear@26: nuclear@26: for (i = 0; i < 256; i++) nuclear@26: { nuclear@26: png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0, nuclear@26: g) * 255.0 + .5); nuclear@26: nuclear@26: } nuclear@26: } nuclear@26: #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: double g; nuclear@26: int i, j, shift, num; nuclear@26: int sig_bit; nuclear@26: png_uint_32 ig; nuclear@26: nuclear@26: if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) nuclear@26: { nuclear@26: sig_bit = (int)png_ptr->sig_bit.red; nuclear@26: if ((int)png_ptr->sig_bit.green > sig_bit) nuclear@26: sig_bit = png_ptr->sig_bit.green; nuclear@26: if ((int)png_ptr->sig_bit.blue > sig_bit) nuclear@26: sig_bit = png_ptr->sig_bit.blue; nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: sig_bit = (int)png_ptr->sig_bit.gray; nuclear@26: } nuclear@26: nuclear@26: if (sig_bit > 0) nuclear@26: shift = 16 - sig_bit; nuclear@26: else nuclear@26: shift = 0; nuclear@26: nuclear@26: if (png_ptr->transformations & PNG_16_TO_8) nuclear@26: { nuclear@26: if (shift < (16 - PNG_MAX_GAMMA_8)) nuclear@26: shift = (16 - PNG_MAX_GAMMA_8); nuclear@26: } nuclear@26: nuclear@26: if (shift > 8) nuclear@26: shift = 8; nuclear@26: if (shift < 0) nuclear@26: shift = 0; nuclear@26: nuclear@26: png_ptr->gamma_shift = (png_byte)shift; nuclear@26: nuclear@26: num = (1 << (8 - shift)); nuclear@26: nuclear@26: if (png_ptr->screen_gamma > .000001) nuclear@26: g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); nuclear@26: else nuclear@26: g = 1.0; nuclear@26: nuclear@26: png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr, nuclear@26: (png_uint_32)(num * png_sizeof(png_uint_16p))); nuclear@26: nuclear@26: if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND)) nuclear@26: { nuclear@26: double fin, fout; nuclear@26: png_uint_32 last, max; nuclear@26: nuclear@26: for (i = 0; i < num; i++) nuclear@26: { nuclear@26: png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, nuclear@26: (png_uint_32)(256 * png_sizeof(png_uint_16))); nuclear@26: } nuclear@26: nuclear@26: g = 1.0 / g; nuclear@26: last = 0; nuclear@26: for (i = 0; i < 256; i++) nuclear@26: { nuclear@26: fout = ((double)i + 0.5) / 256.0; nuclear@26: fin = pow(fout, g); nuclear@26: max = (png_uint_32)(fin * (double)((png_uint_32)num << 8)); nuclear@26: while (last <= max) nuclear@26: { nuclear@26: png_ptr->gamma_16_table[(int)(last & (0xff >> shift))] nuclear@26: [(int)(last >> (8 - shift))] = (png_uint_16)( nuclear@26: (png_uint_16)i | ((png_uint_16)i << 8)); nuclear@26: last++; nuclear@26: } nuclear@26: } nuclear@26: while (last < ((png_uint_32)num << 8)) nuclear@26: { nuclear@26: png_ptr->gamma_16_table[(int)(last & (0xff >> shift))] nuclear@26: [(int)(last >> (8 - shift))] = (png_uint_16)65535L; nuclear@26: last++; nuclear@26: } nuclear@26: } nuclear@26: else nuclear@26: { nuclear@26: for (i = 0; i < num; i++) nuclear@26: { nuclear@26: png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, nuclear@26: (png_uint_32)(256 * png_sizeof(png_uint_16))); nuclear@26: nuclear@26: ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4); nuclear@26: for (j = 0; j < 256; j++) nuclear@26: { nuclear@26: png_ptr->gamma_16_table[i][j] = nuclear@26: (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / nuclear@26: 65535.0, g) * 65535.0 + .5); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ nuclear@26: defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) nuclear@26: if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY)) nuclear@26: { nuclear@26: nuclear@26: g = 1.0 / (png_ptr->gamma); nuclear@26: nuclear@26: png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr, nuclear@26: (png_uint_32)(num * png_sizeof(png_uint_16p ))); nuclear@26: nuclear@26: for (i = 0; i < num; i++) nuclear@26: { nuclear@26: png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr, nuclear@26: (png_uint_32)(256 * png_sizeof(png_uint_16))); nuclear@26: nuclear@26: ig = (((png_uint_32)i * nuclear@26: (png_uint_32)png_gamma_shift[shift]) >> 4); nuclear@26: for (j = 0; j < 256; j++) nuclear@26: { nuclear@26: png_ptr->gamma_16_to_1[i][j] = nuclear@26: (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / nuclear@26: 65535.0, g) * 65535.0 + .5); nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: if (png_ptr->screen_gamma > 0.000001) nuclear@26: g = 1.0 / png_ptr->screen_gamma; nuclear@26: else nuclear@26: g = png_ptr->gamma; /* probably doing rgb_to_gray */ nuclear@26: nuclear@26: png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr, nuclear@26: (png_uint_32)(num * png_sizeof(png_uint_16p))); nuclear@26: nuclear@26: for (i = 0; i < num; i++) nuclear@26: { nuclear@26: png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr, nuclear@26: (png_uint_32)(256 * png_sizeof(png_uint_16))); nuclear@26: nuclear@26: ig = (((png_uint_32)i * nuclear@26: (png_uint_32)png_gamma_shift[shift]) >> 4); nuclear@26: for (j = 0; j < 256; j++) nuclear@26: { nuclear@26: png_ptr->gamma_16_from_1[i][j] = nuclear@26: (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) / nuclear@26: 65535.0, g) * 65535.0 + .5); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ nuclear@26: } nuclear@26: } nuclear@26: #endif nuclear@26: /* To do: install integer version of png_build_gamma_table here */ nuclear@26: #endif nuclear@26: nuclear@26: #if defined(PNG_MNG_FEATURES_SUPPORTED) nuclear@26: /* undoes intrapixel differencing */ nuclear@26: void /* PRIVATE */ nuclear@26: png_do_read_intrapixel(png_row_infop row_info, png_bytep row) nuclear@26: { nuclear@26: png_debug(1, "in png_do_read_intrapixel\n"); nuclear@26: if ( nuclear@26: #if defined(PNG_USELESS_TESTS_SUPPORTED) nuclear@26: row != NULL && row_info != NULL && nuclear@26: #endif nuclear@26: (row_info->color_type & PNG_COLOR_MASK_COLOR)) nuclear@26: { nuclear@26: int bytes_per_pixel; nuclear@26: png_uint_32 row_width = row_info->width; nuclear@26: if (row_info->bit_depth == 8) nuclear@26: { nuclear@26: png_bytep rp; nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: if (row_info->color_type == PNG_COLOR_TYPE_RGB) nuclear@26: bytes_per_pixel = 3; nuclear@26: else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) nuclear@26: bytes_per_pixel = 4; nuclear@26: else nuclear@26: return; nuclear@26: nuclear@26: for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) nuclear@26: { nuclear@26: *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff); nuclear@26: *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff); nuclear@26: } nuclear@26: } nuclear@26: else if (row_info->bit_depth == 16) nuclear@26: { nuclear@26: png_bytep rp; nuclear@26: png_uint_32 i; nuclear@26: nuclear@26: if (row_info->color_type == PNG_COLOR_TYPE_RGB) nuclear@26: bytes_per_pixel = 6; nuclear@26: else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) nuclear@26: bytes_per_pixel = 8; nuclear@26: else nuclear@26: return; nuclear@26: nuclear@26: for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) nuclear@26: { nuclear@26: png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); nuclear@26: png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); nuclear@26: png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); nuclear@26: png_uint_32 red = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL); nuclear@26: png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL); nuclear@26: *(rp ) = (png_byte)((red >> 8) & 0xff); nuclear@26: *(rp+1) = (png_byte)(red & 0xff); nuclear@26: *(rp+4) = (png_byte)((blue >> 8) & 0xff); nuclear@26: *(rp+5) = (png_byte)(blue & 0xff); nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: } nuclear@26: #endif /* PNG_MNG_FEATURES_SUPPORTED */ nuclear@26: #endif /* PNG_READ_SUPPORTED */