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