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