michael@0: michael@0: /* pngrtran.c - transforms the data in a row for PNG readers michael@0: * michael@0: * Last changed in libpng 1.6.10 [March 6, 2014] michael@0: * Copyright (c) 1998-2014 Glenn Randers-Pehrson michael@0: * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) michael@0: * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) michael@0: * michael@0: * This code is released under the libpng license. michael@0: * For conditions of distribution and use, see the disclaimer michael@0: * and license in png.h michael@0: * michael@0: * This file contains functions optionally called by an application michael@0: * in order to tell libpng how to handle data when reading a PNG. michael@0: * Transformations that are used in both reading and writing are michael@0: * in pngtrans.c. michael@0: */ michael@0: michael@0: #include "pngpriv.h" michael@0: michael@0: #ifdef PNG_READ_SUPPORTED michael@0: michael@0: /* Set the action on getting a CRC error for an ancillary or critical chunk. */ michael@0: void PNGAPI michael@0: png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action) michael@0: { michael@0: png_debug(1, "in png_set_crc_action"); michael@0: michael@0: if (png_ptr == NULL) michael@0: return; michael@0: michael@0: /* Tell libpng how we react to CRC errors in critical chunks */ michael@0: switch (crit_action) michael@0: { michael@0: case PNG_CRC_NO_CHANGE: /* Leave setting as is */ michael@0: break; michael@0: michael@0: case PNG_CRC_WARN_USE: /* Warn/use data */ michael@0: png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; michael@0: png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; michael@0: break; michael@0: michael@0: case PNG_CRC_QUIET_USE: /* Quiet/use data */ michael@0: png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; michael@0: png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | michael@0: PNG_FLAG_CRC_CRITICAL_IGNORE; michael@0: break; michael@0: michael@0: case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */ michael@0: png_warning(png_ptr, michael@0: "Can't discard critical data on CRC error"); michael@0: case PNG_CRC_ERROR_QUIT: /* Error/quit */ michael@0: michael@0: case PNG_CRC_DEFAULT: michael@0: default: michael@0: png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; michael@0: break; michael@0: } michael@0: michael@0: /* Tell libpng how we react to CRC errors in ancillary chunks */ michael@0: switch (ancil_action) michael@0: { michael@0: case PNG_CRC_NO_CHANGE: /* Leave setting as is */ michael@0: break; michael@0: michael@0: case PNG_CRC_WARN_USE: /* Warn/use data */ michael@0: png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; michael@0: png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; michael@0: break; michael@0: michael@0: case PNG_CRC_QUIET_USE: /* Quiet/use data */ michael@0: png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; michael@0: png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | michael@0: PNG_FLAG_CRC_ANCILLARY_NOWARN; michael@0: break; michael@0: michael@0: case PNG_CRC_ERROR_QUIT: /* Error/quit */ michael@0: png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; michael@0: png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; michael@0: break; michael@0: michael@0: case PNG_CRC_WARN_DISCARD: /* Warn/discard data */ michael@0: michael@0: case PNG_CRC_DEFAULT: michael@0: default: michael@0: png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_READ_TRANSFORMS_SUPPORTED michael@0: /* Is it OK to set a transformation now? Only if png_start_read_image or michael@0: * png_read_update_info have not been called. It is not necessary for the IHDR michael@0: * to have been read in all cases, the parameter allows for this check too. michael@0: */ michael@0: static int michael@0: png_rtran_ok(png_structrp png_ptr, int need_IHDR) michael@0: { michael@0: if (png_ptr != NULL) michael@0: { michael@0: if (png_ptr->flags & PNG_FLAG_ROW_INIT) michael@0: png_app_error(png_ptr, michael@0: "invalid after png_start_read_image or png_read_update_info"); michael@0: michael@0: else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0) michael@0: png_app_error(png_ptr, "invalid before the PNG header has been read"); michael@0: michael@0: else michael@0: { michael@0: /* Turn on failure to initialize correctly for all transforms. */ michael@0: png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED; michael@0: michael@0: return 1; /* Ok */ michael@0: } michael@0: } michael@0: michael@0: return 0; /* no png_error possible! */ michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_BACKGROUND_SUPPORTED michael@0: /* Handle alpha and tRNS via a background color */ michael@0: void PNGFAPI michael@0: png_set_background_fixed(png_structrp png_ptr, michael@0: png_const_color_16p background_color, int background_gamma_code, michael@0: int need_expand, png_fixed_point background_gamma) michael@0: { michael@0: png_debug(1, "in png_set_background_fixed"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0) || background_color == NULL) michael@0: return; michael@0: michael@0: if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) michael@0: { michael@0: png_warning(png_ptr, "Application must supply a known background gamma"); michael@0: return; michael@0: } michael@0: michael@0: png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA; michael@0: png_ptr->transformations &= ~PNG_ENCODE_ALPHA; michael@0: png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; michael@0: michael@0: png_ptr->background = *background_color; michael@0: png_ptr->background_gamma = background_gamma; michael@0: png_ptr->background_gamma_type = (png_byte)(background_gamma_code); michael@0: if (need_expand) michael@0: png_ptr->transformations |= PNG_BACKGROUND_EXPAND; michael@0: else michael@0: png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; michael@0: } michael@0: michael@0: # ifdef PNG_FLOATING_POINT_SUPPORTED michael@0: void PNGAPI michael@0: png_set_background(png_structrp png_ptr, michael@0: png_const_color_16p background_color, int background_gamma_code, michael@0: int need_expand, double background_gamma) michael@0: { michael@0: png_set_background_fixed(png_ptr, background_color, background_gamma_code, michael@0: need_expand, png_fixed(png_ptr, background_gamma, "png_set_background")); michael@0: } michael@0: # endif /* FLOATING_POINT */ michael@0: #endif /* READ_BACKGROUND */ michael@0: michael@0: /* Scale 16-bit depth files to 8-bit depth. If both of these are set then the michael@0: * one that pngrtran does first (scale) happens. This is necessary to allow the michael@0: * TRANSFORM and API behavior to be somewhat consistent, and it's simpler. michael@0: */ michael@0: #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED michael@0: void PNGAPI michael@0: png_set_scale_16(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_set_scale_16"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: png_ptr->transformations |= PNG_SCALE_16_TO_8; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED michael@0: /* Chop 16-bit depth files to 8-bit depth */ michael@0: void PNGAPI michael@0: png_set_strip_16(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_set_strip_16"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: png_ptr->transformations |= PNG_16_TO_8; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED michael@0: void PNGAPI michael@0: png_set_strip_alpha(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_set_strip_alpha"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: png_ptr->transformations |= PNG_STRIP_ALPHA; michael@0: } michael@0: #endif michael@0: michael@0: #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED) michael@0: static png_fixed_point michael@0: translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma, michael@0: int is_screen) michael@0: { michael@0: /* Check for flag values. The main reason for having the old Mac value as a michael@0: * flag is that it is pretty near impossible to work out what the correct michael@0: * value is from Apple documentation - a working Mac system is needed to michael@0: * discover the value! michael@0: */ michael@0: if (output_gamma == PNG_DEFAULT_sRGB || michael@0: output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB) michael@0: { michael@0: /* If there is no sRGB support this just sets the gamma to the standard michael@0: * sRGB value. (This is a side effect of using this function!) michael@0: */ michael@0: # ifdef PNG_READ_sRGB_SUPPORTED michael@0: png_ptr->flags |= PNG_FLAG_ASSUME_sRGB; michael@0: # else michael@0: PNG_UNUSED(png_ptr) michael@0: # endif michael@0: if (is_screen) michael@0: output_gamma = PNG_GAMMA_sRGB; michael@0: else michael@0: output_gamma = PNG_GAMMA_sRGB_INVERSE; michael@0: } michael@0: michael@0: else if (output_gamma == PNG_GAMMA_MAC_18 || michael@0: output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18) michael@0: { michael@0: if (is_screen) michael@0: output_gamma = PNG_GAMMA_MAC_OLD; michael@0: else michael@0: output_gamma = PNG_GAMMA_MAC_INVERSE; michael@0: } michael@0: michael@0: return output_gamma; michael@0: } michael@0: michael@0: # ifdef PNG_FLOATING_POINT_SUPPORTED michael@0: static png_fixed_point michael@0: convert_gamma_value(png_structrp png_ptr, double output_gamma) michael@0: { michael@0: /* The following silently ignores cases where fixed point (times 100,000) michael@0: * gamma values are passed to the floating point API. This is safe and it michael@0: * means the fixed point constants work just fine with the floating point michael@0: * API. The alternative would just lead to undetected errors and spurious michael@0: * bug reports. Negative values fail inside the _fixed API unless they michael@0: * correspond to the flag values. michael@0: */ michael@0: if (output_gamma > 0 && output_gamma < 128) michael@0: output_gamma *= PNG_FP_1; michael@0: michael@0: /* This preserves -1 and -2 exactly: */ michael@0: output_gamma = floor(output_gamma + .5); michael@0: michael@0: if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN) michael@0: png_fixed_error(png_ptr, "gamma value"); michael@0: michael@0: return (png_fixed_point)output_gamma; michael@0: } michael@0: # endif michael@0: #endif /* READ_ALPHA_MODE || READ_GAMMA */ michael@0: michael@0: #ifdef PNG_READ_ALPHA_MODE_SUPPORTED michael@0: void PNGFAPI michael@0: png_set_alpha_mode_fixed(png_structrp png_ptr, int mode, michael@0: png_fixed_point output_gamma) michael@0: { michael@0: int compose = 0; michael@0: png_fixed_point file_gamma; michael@0: michael@0: png_debug(1, "in png_set_alpha_mode"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/); michael@0: michael@0: /* Validate the value to ensure it is in a reasonable range. The value michael@0: * is expected to be 1 or greater, but this range test allows for some michael@0: * viewing correction values. The intent is to weed out users of this API michael@0: * who use the inverse of the gamma value accidentally! Since some of these michael@0: * values are reasonable this may have to be changed. michael@0: */ michael@0: if (output_gamma < 70000 || output_gamma > 300000) michael@0: png_error(png_ptr, "output gamma out of expected range"); michael@0: michael@0: /* The default file gamma is the inverse of the output gamma; the output michael@0: * gamma may be changed below so get the file value first: michael@0: */ michael@0: file_gamma = png_reciprocal(output_gamma); michael@0: michael@0: /* There are really 8 possibilities here, composed of any combination michael@0: * of: michael@0: * michael@0: * premultiply the color channels michael@0: * do not encode non-opaque pixels michael@0: * encode the alpha as well as the color channels michael@0: * michael@0: * The differences disappear if the input/output ('screen') gamma is 1.0, michael@0: * because then the encoding is a no-op and there is only the choice of michael@0: * premultiplying the color channels or not. michael@0: * michael@0: * png_set_alpha_mode and png_set_background interact because both use michael@0: * png_compose to do the work. Calling both is only useful when michael@0: * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along michael@0: * with a default gamma value. Otherwise PNG_COMPOSE must not be set. michael@0: */ michael@0: switch (mode) michael@0: { michael@0: case PNG_ALPHA_PNG: /* default: png standard */ michael@0: /* No compose, but it may be set by png_set_background! */ michael@0: png_ptr->transformations &= ~PNG_ENCODE_ALPHA; michael@0: png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; michael@0: break; michael@0: michael@0: case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */ michael@0: compose = 1; michael@0: png_ptr->transformations &= ~PNG_ENCODE_ALPHA; michael@0: png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; michael@0: /* The output is linear: */ michael@0: output_gamma = PNG_FP_1; michael@0: break; michael@0: michael@0: case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */ michael@0: compose = 1; michael@0: png_ptr->transformations &= ~PNG_ENCODE_ALPHA; michael@0: png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA; michael@0: /* output_gamma records the encoding of opaque pixels! */ michael@0: break; michael@0: michael@0: case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */ michael@0: compose = 1; michael@0: png_ptr->transformations |= PNG_ENCODE_ALPHA; michael@0: png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "invalid alpha mode"); michael@0: } michael@0: michael@0: /* Only set the default gamma if the file gamma has not been set (this has michael@0: * the side effect that the gamma in a second call to png_set_alpha_mode will michael@0: * be ignored.) michael@0: */ michael@0: if (png_ptr->colorspace.gamma == 0) michael@0: { michael@0: png_ptr->colorspace.gamma = file_gamma; michael@0: png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; michael@0: } michael@0: michael@0: /* But always set the output gamma: */ michael@0: png_ptr->screen_gamma = output_gamma; michael@0: michael@0: /* Finally, if pre-multiplying, set the background fields to achieve the michael@0: * desired result. michael@0: */ michael@0: if (compose) michael@0: { michael@0: /* And obtain alpha pre-multiplication by composing on black: */ michael@0: memset(&png_ptr->background, 0, (sizeof png_ptr->background)); michael@0: png_ptr->background_gamma = png_ptr->colorspace.gamma; /* just in case */ michael@0: png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE; michael@0: png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; michael@0: michael@0: if (png_ptr->transformations & PNG_COMPOSE) michael@0: png_error(png_ptr, michael@0: "conflicting calls to set alpha mode and background"); michael@0: michael@0: png_ptr->transformations |= PNG_COMPOSE; michael@0: } michael@0: } michael@0: michael@0: # ifdef PNG_FLOATING_POINT_SUPPORTED michael@0: void PNGAPI michael@0: png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma) michael@0: { michael@0: png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr, michael@0: output_gamma)); michael@0: } michael@0: # endif michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_QUANTIZE_SUPPORTED michael@0: /* Dither file to 8-bit. Supply a palette, the current number michael@0: * of elements in the palette, the maximum number of elements michael@0: * allowed, and a histogram if possible. If the current number michael@0: * of colors is greater then the maximum number, the palette will be michael@0: * modified to fit in the maximum number. "full_quantize" indicates michael@0: * whether we need a quantizing cube set up for RGB images, or if we michael@0: * simply are reducing the number of colors in a paletted image. michael@0: */ michael@0: michael@0: typedef struct png_dsort_struct michael@0: { michael@0: struct png_dsort_struct * next; michael@0: png_byte left; michael@0: png_byte right; michael@0: } png_dsort; michael@0: typedef png_dsort * png_dsortp; michael@0: typedef png_dsort * * png_dsortpp; michael@0: michael@0: void PNGAPI michael@0: png_set_quantize(png_structrp png_ptr, png_colorp palette, michael@0: int num_palette, int maximum_colors, png_const_uint_16p histogram, michael@0: int full_quantize) michael@0: { michael@0: png_debug(1, "in png_set_quantize"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: png_ptr->transformations |= PNG_QUANTIZE; michael@0: michael@0: if (!full_quantize) michael@0: { michael@0: int i; michael@0: michael@0: png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr, michael@0: (png_uint_32)(num_palette * (sizeof (png_byte)))); michael@0: for (i = 0; i < num_palette; i++) michael@0: png_ptr->quantize_index[i] = (png_byte)i; michael@0: } michael@0: michael@0: if (num_palette > maximum_colors) michael@0: { michael@0: if (histogram != NULL) michael@0: { michael@0: /* This is easy enough, just throw out the least used colors. michael@0: * Perhaps not the best solution, but good enough. michael@0: */ michael@0: michael@0: int i; michael@0: michael@0: /* Initialize an array to sort colors */ michael@0: png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr, michael@0: (png_uint_32)(num_palette * (sizeof (png_byte)))); michael@0: michael@0: /* Initialize the quantize_sort array */ michael@0: for (i = 0; i < num_palette; i++) michael@0: png_ptr->quantize_sort[i] = (png_byte)i; michael@0: michael@0: /* Find the least used palette entries by starting a michael@0: * bubble sort, and running it until we have sorted michael@0: * out enough colors. Note that we don't care about michael@0: * sorting all the colors, just finding which are michael@0: * least used. michael@0: */ michael@0: michael@0: for (i = num_palette - 1; i >= maximum_colors; i--) michael@0: { michael@0: int done; /* To stop early if the list is pre-sorted */ michael@0: int j; michael@0: michael@0: done = 1; michael@0: for (j = 0; j < i; j++) michael@0: { michael@0: if (histogram[png_ptr->quantize_sort[j]] michael@0: < histogram[png_ptr->quantize_sort[j + 1]]) michael@0: { michael@0: png_byte t; michael@0: michael@0: t = png_ptr->quantize_sort[j]; michael@0: png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1]; michael@0: png_ptr->quantize_sort[j + 1] = t; michael@0: done = 0; michael@0: } michael@0: } michael@0: michael@0: if (done) michael@0: break; michael@0: } michael@0: michael@0: /* Swap the palette around, and set up a table, if necessary */ michael@0: if (full_quantize) michael@0: { michael@0: int j = num_palette; michael@0: michael@0: /* Put all the useful colors within the max, but don't michael@0: * move the others. michael@0: */ michael@0: for (i = 0; i < maximum_colors; i++) michael@0: { michael@0: if ((int)png_ptr->quantize_sort[i] >= maximum_colors) michael@0: { michael@0: do michael@0: j--; michael@0: while ((int)png_ptr->quantize_sort[j] >= maximum_colors); michael@0: michael@0: palette[i] = palette[j]; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: { michael@0: int j = num_palette; michael@0: michael@0: /* Move all the used colors inside the max limit, and michael@0: * develop a translation table. michael@0: */ michael@0: for (i = 0; i < maximum_colors; i++) michael@0: { michael@0: /* Only move the colors we need to */ michael@0: if ((int)png_ptr->quantize_sort[i] >= maximum_colors) michael@0: { michael@0: png_color tmp_color; michael@0: michael@0: do michael@0: j--; michael@0: while ((int)png_ptr->quantize_sort[j] >= maximum_colors); michael@0: michael@0: tmp_color = palette[j]; michael@0: palette[j] = palette[i]; michael@0: palette[i] = tmp_color; michael@0: /* Indicate where the color went */ michael@0: png_ptr->quantize_index[j] = (png_byte)i; michael@0: png_ptr->quantize_index[i] = (png_byte)j; michael@0: } michael@0: } michael@0: michael@0: /* Find closest color for those colors we are not using */ michael@0: for (i = 0; i < num_palette; i++) michael@0: { michael@0: if ((int)png_ptr->quantize_index[i] >= maximum_colors) michael@0: { michael@0: int min_d, k, min_k, d_index; michael@0: michael@0: /* Find the closest color to one we threw out */ michael@0: d_index = png_ptr->quantize_index[i]; michael@0: min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); michael@0: for (k = 1, min_k = 0; k < maximum_colors; k++) michael@0: { michael@0: int d; michael@0: michael@0: d = PNG_COLOR_DIST(palette[d_index], palette[k]); michael@0: michael@0: if (d < min_d) michael@0: { michael@0: min_d = d; michael@0: min_k = k; michael@0: } michael@0: } michael@0: /* Point to closest color */ michael@0: png_ptr->quantize_index[i] = (png_byte)min_k; michael@0: } michael@0: } michael@0: } michael@0: png_free(png_ptr, png_ptr->quantize_sort); michael@0: png_ptr->quantize_sort = NULL; michael@0: } michael@0: else michael@0: { michael@0: /* This is much harder to do simply (and quickly). Perhaps michael@0: * we need to go through a median cut routine, but those michael@0: * don't always behave themselves with only a few colors michael@0: * as input. So we will just find the closest two colors, michael@0: * and throw out one of them (chosen somewhat randomly). michael@0: * [We don't understand this at all, so if someone wants to michael@0: * work on improving it, be our guest - AED, GRP] michael@0: */ michael@0: int i; michael@0: int max_d; michael@0: int num_new_palette; michael@0: png_dsortp t; michael@0: png_dsortpp hash; michael@0: michael@0: t = NULL; michael@0: michael@0: /* Initialize palette index arrays */ michael@0: png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, michael@0: (png_uint_32)(num_palette * (sizeof (png_byte)))); michael@0: png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, michael@0: (png_uint_32)(num_palette * (sizeof (png_byte)))); michael@0: michael@0: /* Initialize the sort array */ michael@0: for (i = 0; i < num_palette; i++) michael@0: { michael@0: png_ptr->index_to_palette[i] = (png_byte)i; michael@0: png_ptr->palette_to_index[i] = (png_byte)i; michael@0: } michael@0: michael@0: hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 * michael@0: (sizeof (png_dsortp)))); michael@0: michael@0: num_new_palette = num_palette; michael@0: michael@0: /* Initial wild guess at how far apart the farthest pixel michael@0: * pair we will be eliminating will be. Larger michael@0: * numbers mean more areas will be allocated, Smaller michael@0: * numbers run the risk of not saving enough data, and michael@0: * having to do this all over again. michael@0: * michael@0: * I have not done extensive checking on this number. michael@0: */ michael@0: max_d = 96; michael@0: michael@0: while (num_new_palette > maximum_colors) michael@0: { michael@0: for (i = 0; i < num_new_palette - 1; i++) michael@0: { michael@0: int j; michael@0: michael@0: for (j = i + 1; j < num_new_palette; j++) michael@0: { michael@0: int d; michael@0: michael@0: d = PNG_COLOR_DIST(palette[i], palette[j]); michael@0: michael@0: if (d <= max_d) michael@0: { michael@0: michael@0: t = (png_dsortp)png_malloc_warn(png_ptr, michael@0: (png_uint_32)(sizeof (png_dsort))); michael@0: michael@0: if (t == NULL) michael@0: break; michael@0: michael@0: t->next = hash[d]; michael@0: t->left = (png_byte)i; michael@0: t->right = (png_byte)j; michael@0: hash[d] = t; michael@0: } michael@0: } michael@0: if (t == NULL) michael@0: break; michael@0: } michael@0: michael@0: if (t != NULL) michael@0: for (i = 0; i <= max_d; i++) michael@0: { michael@0: if (hash[i] != NULL) michael@0: { michael@0: png_dsortp p; michael@0: michael@0: for (p = hash[i]; p; p = p->next) michael@0: { michael@0: if ((int)png_ptr->index_to_palette[p->left] michael@0: < num_new_palette && michael@0: (int)png_ptr->index_to_palette[p->right] michael@0: < num_new_palette) michael@0: { michael@0: int j, next_j; michael@0: michael@0: if (num_new_palette & 0x01) michael@0: { michael@0: j = p->left; michael@0: next_j = p->right; michael@0: } michael@0: else michael@0: { michael@0: j = p->right; michael@0: next_j = p->left; michael@0: } michael@0: michael@0: num_new_palette--; michael@0: palette[png_ptr->index_to_palette[j]] michael@0: = palette[num_new_palette]; michael@0: if (!full_quantize) michael@0: { michael@0: int k; michael@0: michael@0: for (k = 0; k < num_palette; k++) michael@0: { michael@0: if (png_ptr->quantize_index[k] == michael@0: png_ptr->index_to_palette[j]) michael@0: png_ptr->quantize_index[k] = michael@0: png_ptr->index_to_palette[next_j]; michael@0: michael@0: if ((int)png_ptr->quantize_index[k] == michael@0: num_new_palette) michael@0: png_ptr->quantize_index[k] = michael@0: png_ptr->index_to_palette[j]; michael@0: } michael@0: } michael@0: michael@0: png_ptr->index_to_palette[png_ptr->palette_to_index michael@0: [num_new_palette]] = png_ptr->index_to_palette[j]; michael@0: michael@0: png_ptr->palette_to_index[png_ptr->index_to_palette[j]] michael@0: = png_ptr->palette_to_index[num_new_palette]; michael@0: michael@0: png_ptr->index_to_palette[j] = michael@0: (png_byte)num_new_palette; michael@0: michael@0: png_ptr->palette_to_index[num_new_palette] = michael@0: (png_byte)j; michael@0: } michael@0: if (num_new_palette <= maximum_colors) michael@0: break; michael@0: } michael@0: if (num_new_palette <= maximum_colors) michael@0: break; michael@0: } michael@0: } michael@0: michael@0: for (i = 0; i < 769; i++) michael@0: { michael@0: if (hash[i] != NULL) michael@0: { michael@0: png_dsortp p = hash[i]; michael@0: while (p) michael@0: { michael@0: t = p->next; michael@0: png_free(png_ptr, p); michael@0: p = t; michael@0: } michael@0: } michael@0: hash[i] = 0; michael@0: } michael@0: max_d += 96; michael@0: } michael@0: png_free(png_ptr, hash); michael@0: png_free(png_ptr, png_ptr->palette_to_index); michael@0: png_free(png_ptr, png_ptr->index_to_palette); michael@0: png_ptr->palette_to_index = NULL; michael@0: png_ptr->index_to_palette = NULL; michael@0: } michael@0: num_palette = maximum_colors; michael@0: } michael@0: if (png_ptr->palette == NULL) michael@0: { michael@0: png_ptr->palette = palette; michael@0: } michael@0: png_ptr->num_palette = (png_uint_16)num_palette; michael@0: michael@0: if (full_quantize) michael@0: { michael@0: int i; michael@0: png_bytep distance; michael@0: int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS + michael@0: PNG_QUANTIZE_BLUE_BITS; michael@0: int num_red = (1 << PNG_QUANTIZE_RED_BITS); michael@0: int num_green = (1 << PNG_QUANTIZE_GREEN_BITS); michael@0: int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS); michael@0: png_size_t num_entries = ((png_size_t)1 << total_bits); michael@0: michael@0: png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr, michael@0: (png_uint_32)(num_entries * (sizeof (png_byte)))); michael@0: michael@0: distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries * michael@0: (sizeof (png_byte)))); michael@0: michael@0: memset(distance, 0xff, num_entries * (sizeof (png_byte))); michael@0: michael@0: for (i = 0; i < num_palette; i++) michael@0: { michael@0: int ir, ig, ib; michael@0: int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS)); michael@0: int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS)); michael@0: int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS)); michael@0: michael@0: for (ir = 0; ir < num_red; ir++) michael@0: { michael@0: /* int dr = abs(ir - r); */ michael@0: int dr = ((ir > r) ? ir - r : r - ir); michael@0: int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS + michael@0: PNG_QUANTIZE_GREEN_BITS)); michael@0: michael@0: for (ig = 0; ig < num_green; ig++) michael@0: { michael@0: /* int dg = abs(ig - g); */ michael@0: int dg = ((ig > g) ? ig - g : g - ig); michael@0: int dt = dr + dg; michael@0: int dm = ((dr > dg) ? dr : dg); michael@0: int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS); michael@0: michael@0: for (ib = 0; ib < num_blue; ib++) michael@0: { michael@0: int d_index = index_g | ib; michael@0: /* int db = abs(ib - b); */ michael@0: int db = ((ib > b) ? ib - b : b - ib); michael@0: int dmax = ((dm > db) ? dm : db); michael@0: int d = dmax + dt + db; michael@0: michael@0: if (d < (int)distance[d_index]) michael@0: { michael@0: distance[d_index] = (png_byte)d; michael@0: png_ptr->palette_lookup[d_index] = (png_byte)i; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: png_free(png_ptr, distance); michael@0: } michael@0: } michael@0: #endif /* PNG_READ_QUANTIZE_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: void PNGFAPI michael@0: png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma, michael@0: png_fixed_point file_gamma) michael@0: { michael@0: png_debug(1, "in png_set_gamma_fixed"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: /* New in libpng-1.5.4 - reserve particular negative values as flags. */ michael@0: scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/); michael@0: file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/); michael@0: michael@0: /* Checking the gamma values for being >0 was added in 1.5.4 along with the michael@0: * premultiplied alpha support; this actually hides an undocumented feature michael@0: * of the previous implementation which allowed gamma processing to be michael@0: * disabled in background handling. There is no evidence (so far) that this michael@0: * was being used; however, png_set_background itself accepted and must still michael@0: * accept '0' for the gamma value it takes, because it isn't always used. michael@0: * michael@0: * Since this is an API change (albeit a very minor one that removes an michael@0: * undocumented API feature) the following checks were only enabled in michael@0: * libpng-1.6.0. michael@0: */ michael@0: if (file_gamma <= 0) michael@0: png_error(png_ptr, "invalid file gamma in png_set_gamma"); michael@0: michael@0: if (scrn_gamma <= 0) michael@0: png_error(png_ptr, "invalid screen gamma in png_set_gamma"); michael@0: michael@0: /* Set the gamma values unconditionally - this overrides the value in the PNG michael@0: * file if a gAMA chunk was present. png_set_alpha_mode provides a michael@0: * different, easier, way to default the file gamma. michael@0: */ michael@0: png_ptr->colorspace.gamma = file_gamma; michael@0: png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; michael@0: png_ptr->screen_gamma = scrn_gamma; michael@0: } michael@0: michael@0: # ifdef PNG_FLOATING_POINT_SUPPORTED michael@0: void PNGAPI michael@0: png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma) michael@0: { michael@0: png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma), michael@0: convert_gamma_value(png_ptr, file_gamma)); michael@0: } michael@0: # endif /* FLOATING_POINT_SUPPORTED */ michael@0: #endif /* READ_GAMMA */ michael@0: michael@0: #ifdef PNG_READ_EXPAND_SUPPORTED michael@0: /* Expand paletted images to RGB, expand grayscale images of michael@0: * less than 8-bit depth to 8-bit depth, and expand tRNS chunks michael@0: * to alpha channels. michael@0: */ michael@0: void PNGAPI michael@0: png_set_expand(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_set_expand"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); michael@0: } michael@0: michael@0: /* GRR 19990627: the following three functions currently are identical michael@0: * to png_set_expand(). However, it is entirely reasonable that someone michael@0: * might wish to expand an indexed image to RGB but *not* expand a single, michael@0: * fully transparent palette entry to a full alpha channel--perhaps instead michael@0: * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace michael@0: * the transparent color with a particular RGB value, or drop tRNS entirely. michael@0: * IOW, a future version of the library may make the transformations flag michael@0: * a bit more fine-grained, with separate bits for each of these three michael@0: * functions. michael@0: * michael@0: * More to the point, these functions make it obvious what libpng will be michael@0: * doing, whereas "expand" can (and does) mean any number of things. michael@0: * michael@0: * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified michael@0: * to expand only the sample depth but not to expand the tRNS to alpha michael@0: * and its name was changed to png_set_expand_gray_1_2_4_to_8(). michael@0: */ michael@0: michael@0: /* Expand paletted images to RGB. */ michael@0: void PNGAPI michael@0: png_set_palette_to_rgb(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_set_palette_to_rgb"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); michael@0: } michael@0: michael@0: /* Expand grayscale images of less than 8-bit depth to 8 bits. */ michael@0: void PNGAPI michael@0: png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_set_expand_gray_1_2_4_to_8"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: png_ptr->transformations |= PNG_EXPAND; michael@0: } michael@0: michael@0: /* Expand tRNS chunks to alpha channels. */ michael@0: void PNGAPI michael@0: png_set_tRNS_to_alpha(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_set_tRNS_to_alpha"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); michael@0: } michael@0: #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */ michael@0: michael@0: #ifdef PNG_READ_EXPAND_16_SUPPORTED michael@0: /* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise michael@0: * it may not work correctly.) michael@0: */ michael@0: void PNGAPI michael@0: png_set_expand_16(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_set_expand_16"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED michael@0: void PNGAPI michael@0: png_set_gray_to_rgb(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_set_gray_to_rgb"); michael@0: michael@0: if (!png_rtran_ok(png_ptr, 0)) michael@0: return; michael@0: michael@0: /* Because rgb must be 8 bits or more: */ michael@0: png_set_expand_gray_1_2_4_to_8(png_ptr); michael@0: png_ptr->transformations |= PNG_GRAY_TO_RGB; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED michael@0: void PNGFAPI michael@0: png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action, michael@0: png_fixed_point red, png_fixed_point green) michael@0: { michael@0: png_debug(1, "in png_set_rgb_to_gray"); michael@0: michael@0: /* Need the IHDR here because of the check on color_type below. */ michael@0: /* TODO: fix this */ michael@0: if (!png_rtran_ok(png_ptr, 1)) michael@0: return; michael@0: michael@0: switch(error_action) michael@0: { michael@0: case PNG_ERROR_ACTION_NONE: michael@0: png_ptr->transformations |= PNG_RGB_TO_GRAY; michael@0: break; michael@0: michael@0: case PNG_ERROR_ACTION_WARN: michael@0: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; michael@0: break; michael@0: michael@0: case PNG_ERROR_ACTION_ERROR: michael@0: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "invalid error action to rgb_to_gray"); michael@0: break; michael@0: } michael@0: michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: #ifdef PNG_READ_EXPAND_SUPPORTED michael@0: png_ptr->transformations |= PNG_EXPAND; michael@0: #else michael@0: { michael@0: /* Make this an error in 1.6 because otherwise the application may assume michael@0: * that it just worked and get a memory overwrite. michael@0: */ michael@0: png_error(png_ptr, michael@0: "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED"); michael@0: michael@0: /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */ michael@0: } michael@0: #endif michael@0: { michael@0: if (red >= 0 && green >= 0 && red + green <= PNG_FP_1) michael@0: { michael@0: png_uint_16 red_int, green_int; michael@0: michael@0: /* NOTE: this calculation does not round, but this behavior is retained michael@0: * for consistency, the inaccuracy is very small. The code here always michael@0: * overwrites the coefficients, regardless of whether they have been michael@0: * defaulted or set already. michael@0: */ michael@0: red_int = (png_uint_16)(((png_uint_32)red*32768)/100000); michael@0: green_int = (png_uint_16)(((png_uint_32)green*32768)/100000); michael@0: michael@0: png_ptr->rgb_to_gray_red_coeff = red_int; michael@0: png_ptr->rgb_to_gray_green_coeff = green_int; michael@0: png_ptr->rgb_to_gray_coefficients_set = 1; michael@0: } michael@0: michael@0: else michael@0: { michael@0: if (red >= 0 && green >= 0) michael@0: png_app_warning(png_ptr, michael@0: "ignoring out of range rgb_to_gray coefficients"); michael@0: michael@0: /* Use the defaults, from the cHRM chunk if set, else the historical michael@0: * values which are close to the sRGB/HDTV/ITU-Rec 709 values. See michael@0: * png_do_rgb_to_gray for more discussion of the values. In this case michael@0: * the coefficients are not marked as 'set' and are not overwritten if michael@0: * something has already provided a default. michael@0: */ michael@0: if (png_ptr->rgb_to_gray_red_coeff == 0 && michael@0: png_ptr->rgb_to_gray_green_coeff == 0) michael@0: { michael@0: png_ptr->rgb_to_gray_red_coeff = 6968; michael@0: png_ptr->rgb_to_gray_green_coeff = 23434; michael@0: /* png_ptr->rgb_to_gray_blue_coeff = 2366; */ michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_FLOATING_POINT_SUPPORTED michael@0: /* Convert a RGB image to a grayscale of the same width. This allows us, michael@0: * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image. michael@0: */ michael@0: michael@0: void PNGAPI michael@0: png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red, michael@0: double green) michael@0: { michael@0: png_set_rgb_to_gray_fixed(png_ptr, error_action, michael@0: png_fixed(png_ptr, red, "rgb to gray red coefficient"), michael@0: png_fixed(png_ptr, green, "rgb to gray green coefficient")); michael@0: } michael@0: #endif /* FLOATING POINT */ michael@0: michael@0: #endif /* RGB_TO_GRAY */ michael@0: michael@0: #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ michael@0: defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) michael@0: void PNGAPI michael@0: png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr michael@0: read_user_transform_fn) michael@0: { michael@0: png_debug(1, "in png_set_read_user_transform_fn"); michael@0: michael@0: #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED michael@0: png_ptr->transformations |= PNG_USER_TRANSFORM; michael@0: png_ptr->read_user_transform_fn = read_user_transform_fn; michael@0: #endif michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_TRANSFORMS_SUPPORTED michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: /* In the case of gamma transformations only do transformations on images where michael@0: * the [file] gamma and screen_gamma are not close reciprocals, otherwise it michael@0: * slows things down slightly, and also needlessly introduces small errors. michael@0: */ michael@0: static int /* PRIVATE */ michael@0: png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma) michael@0: { michael@0: /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma michael@0: * correction as a difference of the overall transform from 1.0 michael@0: * michael@0: * We want to compare the threshold with s*f - 1, if we get michael@0: * overflow here it is because of wacky gamma values so we michael@0: * turn on processing anyway. michael@0: */ michael@0: png_fixed_point gtest; michael@0: return !png_muldiv(>est, screen_gamma, file_gamma, PNG_FP_1) || michael@0: png_gamma_significant(gtest); michael@0: } michael@0: #endif michael@0: michael@0: /* Initialize everything needed for the read. This includes modifying michael@0: * the palette. michael@0: */ michael@0: michael@0: /*For the moment 'png_init_palette_transformations' and michael@0: * 'png_init_rgb_transformations' only do some flag canceling optimizations. michael@0: * The intent is that these two routines should have palette or rgb operations michael@0: * extracted from 'png_init_read_transformations'. michael@0: */ michael@0: static void /* PRIVATE */ michael@0: png_init_palette_transformations(png_structrp png_ptr) michael@0: { michael@0: /* Called to handle the (input) palette case. In png_do_read_transformations michael@0: * the first step is to expand the palette if requested, so this code must michael@0: * take care to only make changes that are invariant with respect to the michael@0: * palette expansion, or only do them if there is no expansion. michael@0: * michael@0: * STRIP_ALPHA has already been handled in the caller (by setting num_trans michael@0: * to 0.) michael@0: */ michael@0: int input_has_alpha = 0; michael@0: int input_has_transparency = 0; michael@0: michael@0: if (png_ptr->num_trans > 0) michael@0: { michael@0: int i; michael@0: michael@0: /* Ignore if all the entries are opaque (unlikely!) */ michael@0: for (i=0; inum_trans; ++i) michael@0: { michael@0: if (png_ptr->trans_alpha[i] == 255) michael@0: continue; michael@0: else if (png_ptr->trans_alpha[i] == 0) michael@0: input_has_transparency = 1; michael@0: else michael@0: { michael@0: input_has_transparency = 1; michael@0: input_has_alpha = 1; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* If no alpha we can optimize. */ michael@0: if (!input_has_alpha) michael@0: { michael@0: /* Any alpha means background and associative alpha processing is michael@0: * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA michael@0: * and ENCODE_ALPHA are irrelevant. michael@0: */ michael@0: png_ptr->transformations &= ~PNG_ENCODE_ALPHA; michael@0: png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; michael@0: michael@0: if (!input_has_transparency) michael@0: png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); michael@0: } michael@0: michael@0: #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) michael@0: /* png_set_background handling - deals with the complexity of whether the michael@0: * background color is in the file format or the screen format in the case michael@0: * where an 'expand' will happen. michael@0: */ michael@0: michael@0: /* The following code cannot be entered in the alpha pre-multiplication case michael@0: * because PNG_BACKGROUND_EXPAND is cancelled below. michael@0: */ michael@0: if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && michael@0: (png_ptr->transformations & PNG_EXPAND)) michael@0: { michael@0: { michael@0: png_ptr->background.red = michael@0: png_ptr->palette[png_ptr->background.index].red; michael@0: png_ptr->background.green = michael@0: png_ptr->palette[png_ptr->background.index].green; michael@0: png_ptr->background.blue = michael@0: png_ptr->palette[png_ptr->background.index].blue; michael@0: michael@0: #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED michael@0: if (png_ptr->transformations & PNG_INVERT_ALPHA) michael@0: { michael@0: if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) michael@0: { michael@0: /* Invert the alpha channel (in tRNS) unless the pixels are michael@0: * going to be expanded, in which case leave it for later michael@0: */ michael@0: int i, istop = png_ptr->num_trans; michael@0: michael@0: for (i=0; itrans_alpha[i] = (png_byte)(255 - michael@0: png_ptr->trans_alpha[i]); michael@0: } michael@0: } michael@0: #endif /* PNG_READ_INVERT_ALPHA_SUPPORTED */ michael@0: } michael@0: } /* background expand and (therefore) no alpha association. */ michael@0: #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */ michael@0: } michael@0: michael@0: static void /* PRIVATE */ michael@0: png_init_rgb_transformations(png_structrp png_ptr) michael@0: { michael@0: /* Added to libpng-1.5.4: check the color type to determine whether there michael@0: * is any alpha or transparency in the image and simply cancel the michael@0: * background and alpha mode stuff if there isn't. michael@0: */ michael@0: int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0; michael@0: int input_has_transparency = png_ptr->num_trans > 0; michael@0: michael@0: /* If no alpha we can optimize. */ michael@0: if (!input_has_alpha) michael@0: { michael@0: /* Any alpha means background and associative alpha processing is michael@0: * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA michael@0: * and ENCODE_ALPHA are irrelevant. michael@0: */ michael@0: # ifdef PNG_READ_ALPHA_MODE_SUPPORTED michael@0: png_ptr->transformations &= ~PNG_ENCODE_ALPHA; michael@0: png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; michael@0: # endif michael@0: michael@0: if (!input_has_transparency) michael@0: png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); michael@0: } michael@0: michael@0: #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) michael@0: /* png_set_background handling - deals with the complexity of whether the michael@0: * background color is in the file format or the screen format in the case michael@0: * where an 'expand' will happen. michael@0: */ michael@0: michael@0: /* The following code cannot be entered in the alpha pre-multiplication case michael@0: * because PNG_BACKGROUND_EXPAND is cancelled below. michael@0: */ michael@0: if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) && michael@0: (png_ptr->transformations & PNG_EXPAND) && michael@0: !(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) michael@0: /* i.e., GRAY or GRAY_ALPHA */ michael@0: { michael@0: { michael@0: /* Expand background and tRNS chunks */ michael@0: int gray = png_ptr->background.gray; michael@0: int trans_gray = png_ptr->trans_color.gray; michael@0: michael@0: switch (png_ptr->bit_depth) michael@0: { michael@0: case 1: michael@0: gray *= 0xff; michael@0: trans_gray *= 0xff; michael@0: break; michael@0: michael@0: case 2: michael@0: gray *= 0x55; michael@0: trans_gray *= 0x55; michael@0: break; michael@0: michael@0: case 4: michael@0: gray *= 0x11; michael@0: trans_gray *= 0x11; michael@0: break; michael@0: michael@0: default: michael@0: michael@0: case 8: michael@0: /* FALL THROUGH (Already 8 bits) */ michael@0: michael@0: case 16: michael@0: /* Already a full 16 bits */ michael@0: break; michael@0: } michael@0: michael@0: png_ptr->background.red = png_ptr->background.green = michael@0: png_ptr->background.blue = (png_uint_16)gray; michael@0: michael@0: if (!(png_ptr->transformations & PNG_EXPAND_tRNS)) michael@0: { michael@0: png_ptr->trans_color.red = png_ptr->trans_color.green = michael@0: png_ptr->trans_color.blue = (png_uint_16)trans_gray; michael@0: } michael@0: } michael@0: } /* background expand and (therefore) no alpha association. */ michael@0: #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */ michael@0: } michael@0: michael@0: void /* PRIVATE */ michael@0: png_init_read_transformations(png_structrp png_ptr) michael@0: { michael@0: png_debug(1, "in png_init_read_transformations"); michael@0: michael@0: /* This internal function is called from png_read_start_row in pngrutil.c michael@0: * and it is called before the 'rowbytes' calculation is done, so the code michael@0: * in here can change or update the transformations flags. michael@0: * michael@0: * First do updates that do not depend on the details of the PNG image data michael@0: * being processed. michael@0: */ michael@0: michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds michael@0: * png_set_alpha_mode and this is another source for a default file gamma so michael@0: * the test needs to be performed later - here. In addition prior to 1.5.4 michael@0: * the tests were repeated for the PALETTE color type here - this is no michael@0: * longer necessary (and doesn't seem to have been necessary before.) michael@0: */ michael@0: { michael@0: /* The following temporary indicates if overall gamma correction is michael@0: * required. michael@0: */ michael@0: int gamma_correction = 0; michael@0: michael@0: if (png_ptr->colorspace.gamma != 0) /* has been set */ michael@0: { michael@0: if (png_ptr->screen_gamma != 0) /* screen set too */ michael@0: gamma_correction = png_gamma_threshold(png_ptr->colorspace.gamma, michael@0: png_ptr->screen_gamma); michael@0: michael@0: else michael@0: /* Assume the output matches the input; a long time default behavior michael@0: * of libpng, although the standard has nothing to say about this. michael@0: */ michael@0: png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma); michael@0: } michael@0: michael@0: else if (png_ptr->screen_gamma != 0) michael@0: /* The converse - assume the file matches the screen, note that this michael@0: * perhaps undesireable default can (from 1.5.4) be changed by calling michael@0: * png_set_alpha_mode (even if the alpha handling mode isn't required michael@0: * or isn't changed from the default.) michael@0: */ michael@0: png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma); michael@0: michael@0: else /* neither are set */ michael@0: /* Just in case the following prevents any processing - file and screen michael@0: * are both assumed to be linear and there is no way to introduce a michael@0: * third gamma value other than png_set_background with 'UNIQUE', and, michael@0: * prior to 1.5.4 michael@0: */ michael@0: png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1; michael@0: michael@0: /* We have a gamma value now. */ michael@0: png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; michael@0: michael@0: /* Now turn the gamma transformation on or off as appropriate. Notice michael@0: * that PNG_GAMMA just refers to the file->screen correction. Alpha michael@0: * composition may independently cause gamma correction because it needs michael@0: * linear data (e.g. if the file has a gAMA chunk but the screen gamma michael@0: * hasn't been specified.) In any case this flag may get turned off in michael@0: * the code immediately below if the transform can be handled outside the michael@0: * row loop. michael@0: */ michael@0: if (gamma_correction) michael@0: png_ptr->transformations |= PNG_GAMMA; michael@0: michael@0: else michael@0: png_ptr->transformations &= ~PNG_GAMMA; michael@0: } michael@0: #endif michael@0: michael@0: /* Certain transformations have the effect of preventing other michael@0: * transformations that happen afterward in png_do_read_transformations, michael@0: * resolve the interdependencies here. From the code of michael@0: * png_do_read_transformations the order is: michael@0: * michael@0: * 1) PNG_EXPAND (including PNG_EXPAND_tRNS) michael@0: * 2) PNG_STRIP_ALPHA (if no compose) michael@0: * 3) PNG_RGB_TO_GRAY michael@0: * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY michael@0: * 5) PNG_COMPOSE michael@0: * 6) PNG_GAMMA michael@0: * 7) PNG_STRIP_ALPHA (if compose) michael@0: * 8) PNG_ENCODE_ALPHA michael@0: * 9) PNG_SCALE_16_TO_8 michael@0: * 10) PNG_16_TO_8 michael@0: * 11) PNG_QUANTIZE (converts to palette) michael@0: * 12) PNG_EXPAND_16 michael@0: * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY michael@0: * 14) PNG_INVERT_MONO michael@0: * 15) PNG_INVERT_ALPHA michael@0: * 16) PNG_SHIFT michael@0: * 17) PNG_PACK michael@0: * 18) PNG_BGR michael@0: * 19) PNG_PACKSWAP michael@0: * 20) PNG_FILLER (includes PNG_ADD_ALPHA) michael@0: * 21) PNG_SWAP_ALPHA michael@0: * 22) PNG_SWAP_BYTES michael@0: * 23) PNG_USER_TRANSFORM [must be last] michael@0: */ michael@0: #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED michael@0: if ((png_ptr->transformations & PNG_STRIP_ALPHA) && michael@0: !(png_ptr->transformations & PNG_COMPOSE)) michael@0: { michael@0: /* Stripping the alpha channel happens immediately after the 'expand' michael@0: * transformations, before all other transformation, so it cancels out michael@0: * the alpha handling. It has the side effect negating the effect of michael@0: * PNG_EXPAND_tRNS too: michael@0: */ michael@0: png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA | michael@0: PNG_EXPAND_tRNS); michael@0: png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; michael@0: michael@0: /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen michael@0: * so transparency information would remain just so long as it wasn't michael@0: * expanded. This produces unexpected API changes if the set of things michael@0: * that do PNG_EXPAND_tRNS changes (perfectly possible given the michael@0: * documentation - which says ask for what you want, accept what you michael@0: * get.) This makes the behavior consistent from 1.5.4: michael@0: */ michael@0: png_ptr->num_trans = 0; michael@0: } michael@0: #endif /* STRIP_ALPHA supported, no COMPOSE */ michael@0: michael@0: #ifdef PNG_READ_ALPHA_MODE_SUPPORTED michael@0: /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA michael@0: * settings will have no effect. michael@0: */ michael@0: if (!png_gamma_significant(png_ptr->screen_gamma)) michael@0: { michael@0: png_ptr->transformations &= ~PNG_ENCODE_ALPHA; michael@0: png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED michael@0: /* Make sure the coefficients for the rgb to gray conversion are set michael@0: * appropriately. michael@0: */ michael@0: if (png_ptr->transformations & PNG_RGB_TO_GRAY) michael@0: png_colorspace_set_rgb_coefficients(png_ptr); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED michael@0: #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) michael@0: /* Detect gray background and attempt to enable optimization for michael@0: * gray --> RGB case. michael@0: * michael@0: * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or michael@0: * RGB_ALPHA (in which case need_expand is superfluous anyway), the michael@0: * background color might actually be gray yet not be flagged as such. michael@0: * This is not a problem for the current code, which uses michael@0: * PNG_BACKGROUND_IS_GRAY only to decide when to do the michael@0: * png_do_gray_to_rgb() transformation. michael@0: * michael@0: * TODO: this code needs to be revised to avoid the complexity and michael@0: * interdependencies. The color type of the background should be recorded in michael@0: * png_set_background, along with the bit depth, then the code has a record michael@0: * of exactly what color space the background is currently in. michael@0: */ michael@0: if (png_ptr->transformations & PNG_BACKGROUND_EXPAND) michael@0: { michael@0: /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if michael@0: * the file was grayscale the background value is gray. michael@0: */ michael@0: if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) michael@0: png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; michael@0: } michael@0: michael@0: else if (png_ptr->transformations & PNG_COMPOSE) michael@0: { michael@0: /* PNG_COMPOSE: png_set_background was called with need_expand false, michael@0: * so the color is in the color space of the output or png_set_alpha_mode michael@0: * was called and the color is black. Ignore RGB_TO_GRAY because that michael@0: * happens before GRAY_TO_RGB. michael@0: */ michael@0: if (png_ptr->transformations & PNG_GRAY_TO_RGB) michael@0: { michael@0: if (png_ptr->background.red == png_ptr->background.green && michael@0: png_ptr->background.red == png_ptr->background.blue) michael@0: { michael@0: png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; michael@0: png_ptr->background.gray = png_ptr->background.red; michael@0: } michael@0: } michael@0: } michael@0: #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */ michael@0: #endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED */ michael@0: michael@0: /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations michael@0: * can be performed directly on the palette, and some (such as rgb to gray) michael@0: * can be optimized inside the palette. This is particularly true of the michael@0: * composite (background and alpha) stuff, which can be pretty much all done michael@0: * in the palette even if the result is expanded to RGB or gray afterward. michael@0: * michael@0: * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and michael@0: * earlier and the palette stuff is actually handled on the first row. This michael@0: * leads to the reported bug that the palette returned by png_get_PLTE is not michael@0: * updated. michael@0: */ michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: png_init_palette_transformations(png_ptr); michael@0: michael@0: else michael@0: png_init_rgb_transformations(png_ptr); michael@0: michael@0: #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ michael@0: defined(PNG_READ_EXPAND_16_SUPPORTED) michael@0: if ((png_ptr->transformations & PNG_EXPAND_16) && michael@0: (png_ptr->transformations & PNG_COMPOSE) && michael@0: !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) && michael@0: png_ptr->bit_depth != 16) michael@0: { michael@0: /* TODO: fix this. Because the expand_16 operation is after the compose michael@0: * handling the background color must be 8, not 16, bits deep, but the michael@0: * application will supply a 16-bit value so reduce it here. michael@0: * michael@0: * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at michael@0: * present, so that case is ok (until do_expand_16 is moved.) michael@0: * michael@0: * NOTE: this discards the low 16 bits of the user supplied background michael@0: * color, but until expand_16 works properly there is no choice! michael@0: */ michael@0: # define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x)) michael@0: CHOP(png_ptr->background.red); michael@0: CHOP(png_ptr->background.green); michael@0: CHOP(png_ptr->background.blue); michael@0: CHOP(png_ptr->background.gray); michael@0: # undef CHOP michael@0: } michael@0: #endif /* PNG_READ_BACKGROUND_SUPPORTED && PNG_READ_EXPAND_16_SUPPORTED */ michael@0: michael@0: #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ michael@0: (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \ michael@0: defined(PNG_READ_STRIP_16_TO_8_SUPPORTED)) michael@0: if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) && michael@0: (png_ptr->transformations & PNG_COMPOSE) && michael@0: !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) && michael@0: png_ptr->bit_depth == 16) michael@0: { michael@0: /* On the other hand, if a 16-bit file is to be reduced to 8-bits per michael@0: * component this will also happen after PNG_COMPOSE and so the background michael@0: * color must be pre-expanded here. michael@0: * michael@0: * TODO: fix this too. michael@0: */ michael@0: png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257); michael@0: png_ptr->background.green = michael@0: (png_uint_16)(png_ptr->background.green * 257); michael@0: png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257); michael@0: png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257); michael@0: } michael@0: #endif michael@0: michael@0: /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the michael@0: * background support (see the comments in scripts/pnglibconf.dfa), this michael@0: * allows pre-multiplication of the alpha channel to be implemented as michael@0: * compositing on black. This is probably sub-optimal and has been done in michael@0: * 1.5.4 betas simply to enable external critique and testing (i.e. to michael@0: * implement the new API quickly, without lots of internal changes.) michael@0: */ michael@0: michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: # ifdef PNG_READ_BACKGROUND_SUPPORTED michael@0: /* Includes ALPHA_MODE */ michael@0: png_ptr->background_1 = png_ptr->background; michael@0: # endif michael@0: michael@0: /* This needs to change - in the palette image case a whole set of tables are michael@0: * built when it would be quicker to just calculate the correct value for michael@0: * each palette entry directly. Also, the test is too tricky - why check michael@0: * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that michael@0: * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the michael@0: * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction michael@0: * the gamma tables will not be built even if composition is required on a michael@0: * gamma encoded value. michael@0: * michael@0: * In 1.5.4 this is addressed below by an additional check on the individual michael@0: * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the michael@0: * tables. michael@0: */ michael@0: if ((png_ptr->transformations & PNG_GAMMA) michael@0: || ((png_ptr->transformations & PNG_RGB_TO_GRAY) michael@0: && (png_gamma_significant(png_ptr->colorspace.gamma) || michael@0: png_gamma_significant(png_ptr->screen_gamma))) michael@0: || ((png_ptr->transformations & PNG_COMPOSE) michael@0: && (png_gamma_significant(png_ptr->colorspace.gamma) michael@0: || png_gamma_significant(png_ptr->screen_gamma) michael@0: # ifdef PNG_READ_BACKGROUND_SUPPORTED michael@0: || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE michael@0: && png_gamma_significant(png_ptr->background_gamma)) michael@0: # endif michael@0: )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) michael@0: && png_gamma_significant(png_ptr->screen_gamma)) michael@0: ) michael@0: { michael@0: png_build_gamma_table(png_ptr, png_ptr->bit_depth); michael@0: michael@0: #ifdef PNG_READ_BACKGROUND_SUPPORTED michael@0: if (png_ptr->transformations & PNG_COMPOSE) michael@0: { michael@0: /* Issue a warning about this combination: because RGB_TO_GRAY is michael@0: * optimized to do the gamma transform if present yet do_background has michael@0: * to do the same thing if both options are set a michael@0: * double-gamma-correction happens. This is true in all versions of michael@0: * libpng to date. michael@0: */ michael@0: if (png_ptr->transformations & PNG_RGB_TO_GRAY) michael@0: png_warning(png_ptr, michael@0: "libpng does not support gamma+background+rgb_to_gray"); michael@0: michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: /* We don't get to here unless there is a tRNS chunk with non-opaque michael@0: * entries - see the checking code at the start of this function. michael@0: */ michael@0: png_color back, back_1; michael@0: png_colorp palette = png_ptr->palette; michael@0: int num_palette = png_ptr->num_palette; michael@0: int i; michael@0: if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) michael@0: { michael@0: michael@0: back.red = png_ptr->gamma_table[png_ptr->background.red]; michael@0: back.green = png_ptr->gamma_table[png_ptr->background.green]; michael@0: back.blue = png_ptr->gamma_table[png_ptr->background.blue]; michael@0: michael@0: back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; michael@0: back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; michael@0: back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; michael@0: } michael@0: else michael@0: { michael@0: png_fixed_point g, gs; michael@0: michael@0: switch (png_ptr->background_gamma_type) michael@0: { michael@0: case PNG_BACKGROUND_GAMMA_SCREEN: michael@0: g = (png_ptr->screen_gamma); michael@0: gs = PNG_FP_1; michael@0: break; michael@0: michael@0: case PNG_BACKGROUND_GAMMA_FILE: michael@0: g = png_reciprocal(png_ptr->colorspace.gamma); michael@0: gs = png_reciprocal2(png_ptr->colorspace.gamma, michael@0: png_ptr->screen_gamma); michael@0: break; michael@0: michael@0: case PNG_BACKGROUND_GAMMA_UNIQUE: michael@0: g = png_reciprocal(png_ptr->background_gamma); michael@0: gs = png_reciprocal2(png_ptr->background_gamma, michael@0: png_ptr->screen_gamma); michael@0: break; michael@0: default: michael@0: g = PNG_FP_1; /* back_1 */ michael@0: gs = PNG_FP_1; /* back */ michael@0: break; michael@0: } michael@0: michael@0: if (png_gamma_significant(gs)) michael@0: { michael@0: back.red = png_gamma_8bit_correct(png_ptr->background.red, michael@0: gs); michael@0: back.green = png_gamma_8bit_correct(png_ptr->background.green, michael@0: gs); michael@0: back.blue = png_gamma_8bit_correct(png_ptr->background.blue, michael@0: gs); michael@0: } michael@0: michael@0: else michael@0: { michael@0: back.red = (png_byte)png_ptr->background.red; michael@0: back.green = (png_byte)png_ptr->background.green; michael@0: back.blue = (png_byte)png_ptr->background.blue; michael@0: } michael@0: michael@0: if (png_gamma_significant(g)) michael@0: { michael@0: back_1.red = png_gamma_8bit_correct(png_ptr->background.red, michael@0: g); michael@0: back_1.green = png_gamma_8bit_correct( michael@0: png_ptr->background.green, g); michael@0: back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue, michael@0: g); michael@0: } michael@0: michael@0: else michael@0: { michael@0: back_1.red = (png_byte)png_ptr->background.red; michael@0: back_1.green = (png_byte)png_ptr->background.green; michael@0: back_1.blue = (png_byte)png_ptr->background.blue; michael@0: } michael@0: } michael@0: michael@0: for (i = 0; i < num_palette; i++) michael@0: { michael@0: if (i < (int)png_ptr->num_trans && michael@0: png_ptr->trans_alpha[i] != 0xff) michael@0: { michael@0: if (png_ptr->trans_alpha[i] == 0) michael@0: { michael@0: palette[i] = back; michael@0: } michael@0: else /* if (png_ptr->trans_alpha[i] != 0xff) */ michael@0: { michael@0: png_byte v, w; michael@0: michael@0: v = png_ptr->gamma_to_1[palette[i].red]; michael@0: png_composite(w, v, png_ptr->trans_alpha[i], back_1.red); michael@0: palette[i].red = png_ptr->gamma_from_1[w]; michael@0: michael@0: v = png_ptr->gamma_to_1[palette[i].green]; michael@0: png_composite(w, v, png_ptr->trans_alpha[i], back_1.green); michael@0: palette[i].green = png_ptr->gamma_from_1[w]; michael@0: michael@0: v = png_ptr->gamma_to_1[palette[i].blue]; michael@0: png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue); michael@0: palette[i].blue = png_ptr->gamma_from_1[w]; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: palette[i].red = png_ptr->gamma_table[palette[i].red]; michael@0: palette[i].green = png_ptr->gamma_table[palette[i].green]; michael@0: palette[i].blue = png_ptr->gamma_table[palette[i].blue]; michael@0: } michael@0: } michael@0: michael@0: /* Prevent the transformations being done again. michael@0: * michael@0: * NOTE: this is highly dubious; it removes the transformations in michael@0: * place. This seems inconsistent with the general treatment of the michael@0: * transformations elsewhere. michael@0: */ michael@0: png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA); michael@0: } /* color_type == PNG_COLOR_TYPE_PALETTE */ michael@0: michael@0: /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */ michael@0: else /* color_type != PNG_COLOR_TYPE_PALETTE */ michael@0: { michael@0: int gs_sig, g_sig; michael@0: png_fixed_point g = PNG_FP_1; /* Correction to linear */ michael@0: png_fixed_point gs = PNG_FP_1; /* Correction to screen */ michael@0: michael@0: switch (png_ptr->background_gamma_type) michael@0: { michael@0: case PNG_BACKGROUND_GAMMA_SCREEN: michael@0: g = png_ptr->screen_gamma; michael@0: /* gs = PNG_FP_1; */ michael@0: break; michael@0: michael@0: case PNG_BACKGROUND_GAMMA_FILE: michael@0: g = png_reciprocal(png_ptr->colorspace.gamma); michael@0: gs = png_reciprocal2(png_ptr->colorspace.gamma, michael@0: png_ptr->screen_gamma); michael@0: break; michael@0: michael@0: case PNG_BACKGROUND_GAMMA_UNIQUE: michael@0: g = png_reciprocal(png_ptr->background_gamma); michael@0: gs = png_reciprocal2(png_ptr->background_gamma, michael@0: png_ptr->screen_gamma); michael@0: break; michael@0: michael@0: default: michael@0: png_error(png_ptr, "invalid background gamma type"); michael@0: } michael@0: michael@0: g_sig = png_gamma_significant(g); michael@0: gs_sig = png_gamma_significant(gs); michael@0: michael@0: if (g_sig) michael@0: png_ptr->background_1.gray = png_gamma_correct(png_ptr, michael@0: png_ptr->background.gray, g); michael@0: michael@0: if (gs_sig) michael@0: png_ptr->background.gray = png_gamma_correct(png_ptr, michael@0: png_ptr->background.gray, gs); michael@0: michael@0: if ((png_ptr->background.red != png_ptr->background.green) || michael@0: (png_ptr->background.red != png_ptr->background.blue) || michael@0: (png_ptr->background.red != png_ptr->background.gray)) michael@0: { michael@0: /* RGB or RGBA with color background */ michael@0: if (g_sig) michael@0: { michael@0: png_ptr->background_1.red = png_gamma_correct(png_ptr, michael@0: png_ptr->background.red, g); michael@0: michael@0: png_ptr->background_1.green = png_gamma_correct(png_ptr, michael@0: png_ptr->background.green, g); michael@0: michael@0: png_ptr->background_1.blue = png_gamma_correct(png_ptr, michael@0: png_ptr->background.blue, g); michael@0: } michael@0: michael@0: if (gs_sig) michael@0: { michael@0: png_ptr->background.red = png_gamma_correct(png_ptr, michael@0: png_ptr->background.red, gs); michael@0: michael@0: png_ptr->background.green = png_gamma_correct(png_ptr, michael@0: png_ptr->background.green, gs); michael@0: michael@0: png_ptr->background.blue = png_gamma_correct(png_ptr, michael@0: png_ptr->background.blue, gs); michael@0: } michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */ michael@0: png_ptr->background_1.red = png_ptr->background_1.green michael@0: = png_ptr->background_1.blue = png_ptr->background_1.gray; michael@0: michael@0: png_ptr->background.red = png_ptr->background.green michael@0: = png_ptr->background.blue = png_ptr->background.gray; michael@0: } michael@0: michael@0: /* The background is now in screen gamma: */ michael@0: png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN; michael@0: } /* color_type != PNG_COLOR_TYPE_PALETTE */ michael@0: }/* png_ptr->transformations & PNG_BACKGROUND */ michael@0: michael@0: else michael@0: /* Transformation does not include PNG_BACKGROUND */ michael@0: #endif /* PNG_READ_BACKGROUND_SUPPORTED */ michael@0: if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE michael@0: #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED michael@0: /* RGB_TO_GRAY needs to have non-gamma-corrected values! */ michael@0: && ((png_ptr->transformations & PNG_EXPAND) == 0 || michael@0: (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) michael@0: #endif michael@0: ) michael@0: { michael@0: png_colorp palette = png_ptr->palette; michael@0: int num_palette = png_ptr->num_palette; michael@0: int i; michael@0: michael@0: /* NOTE: there are other transformations that should probably be in michael@0: * here too. michael@0: */ michael@0: for (i = 0; i < num_palette; i++) michael@0: { michael@0: palette[i].red = png_ptr->gamma_table[palette[i].red]; michael@0: palette[i].green = png_ptr->gamma_table[palette[i].green]; michael@0: palette[i].blue = png_ptr->gamma_table[palette[i].blue]; michael@0: } michael@0: michael@0: /* Done the gamma correction. */ michael@0: png_ptr->transformations &= ~PNG_GAMMA; michael@0: } /* color_type == PALETTE && !PNG_BACKGROUND transformation */ michael@0: } michael@0: #ifdef PNG_READ_BACKGROUND_SUPPORTED michael@0: else michael@0: #endif michael@0: #endif /* PNG_READ_GAMMA_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_BACKGROUND_SUPPORTED michael@0: /* No GAMMA transformation (see the hanging else 4 lines above) */ michael@0: if ((png_ptr->transformations & PNG_COMPOSE) && michael@0: (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) michael@0: { michael@0: int i; michael@0: int istop = (int)png_ptr->num_trans; michael@0: png_color back; michael@0: png_colorp palette = png_ptr->palette; michael@0: michael@0: back.red = (png_byte)png_ptr->background.red; michael@0: back.green = (png_byte)png_ptr->background.green; michael@0: back.blue = (png_byte)png_ptr->background.blue; michael@0: michael@0: for (i = 0; i < istop; i++) michael@0: { michael@0: if (png_ptr->trans_alpha[i] == 0) michael@0: { michael@0: palette[i] = back; michael@0: } michael@0: michael@0: else if (png_ptr->trans_alpha[i] != 0xff) michael@0: { michael@0: /* The png_composite() macro is defined in png.h */ michael@0: png_composite(palette[i].red, palette[i].red, michael@0: png_ptr->trans_alpha[i], back.red); michael@0: michael@0: png_composite(palette[i].green, palette[i].green, michael@0: png_ptr->trans_alpha[i], back.green); michael@0: michael@0: png_composite(palette[i].blue, palette[i].blue, michael@0: png_ptr->trans_alpha[i], back.blue); michael@0: } michael@0: } michael@0: michael@0: png_ptr->transformations &= ~PNG_COMPOSE; michael@0: } michael@0: #endif /* PNG_READ_BACKGROUND_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_SHIFT_SUPPORTED michael@0: if ((png_ptr->transformations & PNG_SHIFT) && michael@0: !(png_ptr->transformations & PNG_EXPAND) && michael@0: (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) michael@0: { michael@0: int i; michael@0: int istop = png_ptr->num_palette; michael@0: int shift = 8 - png_ptr->sig_bit.red; michael@0: michael@0: png_ptr->transformations &= ~PNG_SHIFT; michael@0: michael@0: /* significant bits can be in the range 1 to 7 for a meaninful result, if michael@0: * the number of significant bits is 0 then no shift is done (this is an michael@0: * error condition which is silently ignored.) michael@0: */ michael@0: if (shift > 0 && shift < 8) michael@0: for (i=0; ipalette[i].red; michael@0: michael@0: component >>= shift; michael@0: png_ptr->palette[i].red = (png_byte)component; michael@0: } michael@0: michael@0: shift = 8 - png_ptr->sig_bit.green; michael@0: if (shift > 0 && shift < 8) michael@0: for (i=0; ipalette[i].green; michael@0: michael@0: component >>= shift; michael@0: png_ptr->palette[i].green = (png_byte)component; michael@0: } michael@0: michael@0: shift = 8 - png_ptr->sig_bit.blue; michael@0: if (shift > 0 && shift < 8) michael@0: for (i=0; ipalette[i].blue; michael@0: michael@0: component >>= shift; michael@0: png_ptr->palette[i].blue = (png_byte)component; michael@0: } michael@0: } michael@0: #endif /* PNG_READ_SHIFT_SUPPORTED */ michael@0: } michael@0: michael@0: /* Modify the info structure to reflect the transformations. The michael@0: * info should be updated so a PNG file could be written with it, michael@0: * assuming the transformations result in valid PNG data. michael@0: */ michael@0: void /* PRIVATE */ michael@0: png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr) michael@0: { michael@0: png_debug(1, "in png_read_transform_info"); michael@0: michael@0: #ifdef PNG_READ_EXPAND_SUPPORTED michael@0: if (png_ptr->transformations & PNG_EXPAND) michael@0: { michael@0: if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: /* This check must match what actually happens in michael@0: * png_do_expand_palette; if it ever checks the tRNS chunk to see if michael@0: * it is all opaque we must do the same (at present it does not.) michael@0: */ michael@0: if (png_ptr->num_trans > 0) michael@0: info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; michael@0: michael@0: else michael@0: info_ptr->color_type = PNG_COLOR_TYPE_RGB; michael@0: michael@0: info_ptr->bit_depth = 8; michael@0: info_ptr->num_trans = 0; michael@0: michael@0: if (png_ptr->palette == NULL) michael@0: png_error (png_ptr, "Palette is NULL in indexed image"); michael@0: } michael@0: else michael@0: { michael@0: if (png_ptr->num_trans) michael@0: { michael@0: if (png_ptr->transformations & PNG_EXPAND_tRNS) michael@0: info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; michael@0: } michael@0: if (info_ptr->bit_depth < 8) michael@0: info_ptr->bit_depth = 8; michael@0: michael@0: info_ptr->num_trans = 0; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ michael@0: defined(PNG_READ_ALPHA_MODE_SUPPORTED) michael@0: /* The following is almost certainly wrong unless the background value is in michael@0: * the screen space! michael@0: */ michael@0: if (png_ptr->transformations & PNG_COMPOSE) michael@0: info_ptr->background = png_ptr->background; michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4), michael@0: * however it seems that the code in png_init_read_transformations, which has michael@0: * been called before this from png_read_update_info->png_read_start_row michael@0: * sometimes does the gamma transform and cancels the flag. michael@0: * michael@0: * TODO: this looks wrong; the info_ptr should end up with a gamma equal to michael@0: * the screen_gamma value. The following probably results in weirdness if michael@0: * the info_ptr is used by the app after the rows have been read. michael@0: */ michael@0: info_ptr->colorspace.gamma = png_ptr->colorspace.gamma; michael@0: #endif michael@0: michael@0: if (info_ptr->bit_depth == 16) michael@0: { michael@0: # ifdef PNG_READ_16BIT_SUPPORTED michael@0: # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED michael@0: if (png_ptr->transformations & PNG_SCALE_16_TO_8) michael@0: info_ptr->bit_depth = 8; michael@0: # endif michael@0: michael@0: # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED michael@0: if (png_ptr->transformations & PNG_16_TO_8) michael@0: info_ptr->bit_depth = 8; michael@0: # endif michael@0: michael@0: # else michael@0: /* No 16 bit support: force chopping 16-bit input down to 8, in this case michael@0: * the app program can chose if both APIs are available by setting the michael@0: * correct scaling to use. michael@0: */ michael@0: # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED michael@0: /* For compatibility with previous versions use the strip method by michael@0: * default. This code works because if PNG_SCALE_16_TO_8 is already michael@0: * set the code below will do that in preference to the chop. michael@0: */ michael@0: png_ptr->transformations |= PNG_16_TO_8; michael@0: info_ptr->bit_depth = 8; michael@0: # else michael@0: michael@0: # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED michael@0: png_ptr->transformations |= PNG_SCALE_16_TO_8; michael@0: info_ptr->bit_depth = 8; michael@0: # else michael@0: michael@0: CONFIGURATION ERROR: you must enable at least one 16 to 8 method michael@0: # endif michael@0: # endif michael@0: #endif /* !READ_16BIT_SUPPORTED */ michael@0: } michael@0: michael@0: #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED michael@0: if (png_ptr->transformations & PNG_GRAY_TO_RGB) michael@0: info_ptr->color_type = (png_byte)(info_ptr->color_type | michael@0: PNG_COLOR_MASK_COLOR); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED michael@0: if (png_ptr->transformations & PNG_RGB_TO_GRAY) michael@0: info_ptr->color_type = (png_byte)(info_ptr->color_type & michael@0: ~PNG_COLOR_MASK_COLOR); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_QUANTIZE_SUPPORTED michael@0: if (png_ptr->transformations & PNG_QUANTIZE) michael@0: { michael@0: if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || michael@0: (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && michael@0: png_ptr->palette_lookup && info_ptr->bit_depth == 8) michael@0: { michael@0: info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_EXPAND_16_SUPPORTED michael@0: if (png_ptr->transformations & PNG_EXPAND_16 && info_ptr->bit_depth == 8 && michael@0: info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: info_ptr->bit_depth = 16; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_PACK_SUPPORTED michael@0: if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8)) michael@0: info_ptr->bit_depth = 8; michael@0: #endif michael@0: michael@0: if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: info_ptr->channels = 1; michael@0: michael@0: else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR) michael@0: info_ptr->channels = 3; michael@0: michael@0: else michael@0: info_ptr->channels = 1; michael@0: michael@0: #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED michael@0: if (png_ptr->transformations & PNG_STRIP_ALPHA) michael@0: { michael@0: info_ptr->color_type = (png_byte)(info_ptr->color_type & michael@0: ~PNG_COLOR_MASK_ALPHA); michael@0: info_ptr->num_trans = 0; michael@0: } michael@0: #endif michael@0: michael@0: if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA) michael@0: info_ptr->channels++; michael@0: michael@0: #ifdef PNG_READ_FILLER_SUPPORTED michael@0: /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ michael@0: if ((png_ptr->transformations & PNG_FILLER) && michael@0: ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || michael@0: (info_ptr->color_type == PNG_COLOR_TYPE_GRAY))) michael@0: { michael@0: info_ptr->channels++; michael@0: /* If adding a true alpha channel not just filler */ michael@0: if (png_ptr->transformations & PNG_ADD_ALPHA) michael@0: info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; michael@0: } michael@0: #endif michael@0: michael@0: #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ michael@0: defined(PNG_READ_USER_TRANSFORM_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_USER_TRANSFORM) michael@0: { michael@0: if (info_ptr->bit_depth < png_ptr->user_transform_depth) michael@0: info_ptr->bit_depth = png_ptr->user_transform_depth; michael@0: michael@0: if (info_ptr->channels < png_ptr->user_transform_channels) michael@0: info_ptr->channels = png_ptr->user_transform_channels; michael@0: } michael@0: #endif michael@0: michael@0: info_ptr->pixel_depth = (png_byte)(info_ptr->channels * michael@0: info_ptr->bit_depth); michael@0: michael@0: info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width); michael@0: michael@0: /* Adding in 1.5.4: cache the above value in png_struct so that we can later michael@0: * check in png_rowbytes that the user buffer won't get overwritten. Note michael@0: * that the field is not always set - if png_read_update_info isn't called michael@0: * the application has to either not do any transforms or get the calculation michael@0: * right itself. michael@0: */ michael@0: png_ptr->info_rowbytes = info_ptr->rowbytes; michael@0: michael@0: #ifndef PNG_READ_EXPAND_SUPPORTED michael@0: if (png_ptr) michael@0: return; michael@0: #endif michael@0: } michael@0: michael@0: #ifdef PNG_READ_PACK_SUPPORTED michael@0: /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, michael@0: * without changing the actual values. Thus, if you had a row with michael@0: * a bit depth of 1, you would end up with bytes that only contained michael@0: * the numbers 0 or 1. If you would rather they contain 0 and 255, use michael@0: * png_do_shift() after this. michael@0: */ michael@0: static void michael@0: png_do_unpack(png_row_infop row_info, png_bytep row) michael@0: { michael@0: png_debug(1, "in png_do_unpack"); michael@0: michael@0: if (row_info->bit_depth < 8) michael@0: { michael@0: png_uint_32 i; michael@0: png_uint_32 row_width=row_info->width; michael@0: michael@0: switch (row_info->bit_depth) michael@0: { michael@0: case 1: michael@0: { michael@0: png_bytep sp = row + (png_size_t)((row_width - 1) >> 3); michael@0: png_bytep dp = row + (png_size_t)row_width - 1; michael@0: png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07); michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *dp = (png_byte)((*sp >> shift) & 0x01); michael@0: michael@0: if (shift == 7) michael@0: { michael@0: shift = 0; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: shift++; michael@0: michael@0: dp--; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 2: michael@0: { michael@0: michael@0: png_bytep sp = row + (png_size_t)((row_width - 1) >> 2); michael@0: png_bytep dp = row + (png_size_t)row_width - 1; michael@0: png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *dp = (png_byte)((*sp >> shift) & 0x03); michael@0: michael@0: if (shift == 6) michael@0: { michael@0: shift = 0; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: shift += 2; michael@0: michael@0: dp--; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 4: michael@0: { michael@0: png_bytep sp = row + (png_size_t)((row_width - 1) >> 1); michael@0: png_bytep dp = row + (png_size_t)row_width - 1; michael@0: png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *dp = (png_byte)((*sp >> shift) & 0x0f); michael@0: michael@0: if (shift == 4) michael@0: { michael@0: shift = 0; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: shift = 4; michael@0: michael@0: dp--; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: row_info->bit_depth = 8; michael@0: row_info->pixel_depth = (png_byte)(8 * row_info->channels); michael@0: row_info->rowbytes = row_width * row_info->channels; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_SHIFT_SUPPORTED michael@0: /* Reverse the effects of png_do_shift. This routine merely shifts the michael@0: * pixels back to their significant bits values. Thus, if you have michael@0: * a row of bit depth 8, but only 5 are significant, this will shift michael@0: * the values back to 0 through 31. michael@0: */ michael@0: static void michael@0: png_do_unshift(png_row_infop row_info, png_bytep row, michael@0: png_const_color_8p sig_bits) michael@0: { michael@0: int color_type; michael@0: michael@0: png_debug(1, "in png_do_unshift"); michael@0: michael@0: /* The palette case has already been handled in the _init routine. */ michael@0: color_type = row_info->color_type; michael@0: michael@0: if (color_type != PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: int shift[4]; michael@0: int channels = 0; michael@0: int bit_depth = row_info->bit_depth; michael@0: michael@0: if (color_type & PNG_COLOR_MASK_COLOR) michael@0: { michael@0: shift[channels++] = bit_depth - sig_bits->red; michael@0: shift[channels++] = bit_depth - sig_bits->green; michael@0: shift[channels++] = bit_depth - sig_bits->blue; michael@0: } michael@0: michael@0: else michael@0: { michael@0: shift[channels++] = bit_depth - sig_bits->gray; michael@0: } michael@0: michael@0: if (color_type & PNG_COLOR_MASK_ALPHA) michael@0: { michael@0: shift[channels++] = bit_depth - sig_bits->alpha; michael@0: } michael@0: michael@0: { michael@0: int c, have_shift; michael@0: michael@0: for (c = have_shift = 0; c < channels; ++c) michael@0: { michael@0: /* A shift of more than the bit depth is an error condition but it michael@0: * gets ignored here. michael@0: */ michael@0: if (shift[c] <= 0 || shift[c] >= bit_depth) michael@0: shift[c] = 0; michael@0: michael@0: else michael@0: have_shift = 1; michael@0: } michael@0: michael@0: if (!have_shift) michael@0: return; michael@0: } michael@0: michael@0: switch (bit_depth) michael@0: { michael@0: default: michael@0: /* Must be 1bpp gray: should not be here! */ michael@0: /* NOTREACHED */ michael@0: break; michael@0: michael@0: case 2: michael@0: /* Must be 2bpp gray */ michael@0: /* assert(channels == 1 && shift[0] == 1) */ michael@0: { michael@0: png_bytep bp = row; michael@0: png_bytep bp_end = bp + row_info->rowbytes; michael@0: michael@0: while (bp < bp_end) michael@0: { michael@0: int b = (*bp >> 1) & 0x55; michael@0: *bp++ = (png_byte)b; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 4: michael@0: /* Must be 4bpp gray */ michael@0: /* assert(channels == 1) */ michael@0: { michael@0: png_bytep bp = row; michael@0: png_bytep bp_end = bp + row_info->rowbytes; michael@0: int gray_shift = shift[0]; michael@0: int mask = 0xf >> gray_shift; michael@0: michael@0: mask |= mask << 4; michael@0: michael@0: while (bp < bp_end) michael@0: { michael@0: int b = (*bp >> gray_shift) & mask; michael@0: *bp++ = (png_byte)b; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 8: michael@0: /* Single byte components, G, GA, RGB, RGBA */ michael@0: { michael@0: png_bytep bp = row; michael@0: png_bytep bp_end = bp + row_info->rowbytes; michael@0: int channel = 0; michael@0: michael@0: while (bp < bp_end) michael@0: { michael@0: int b = *bp >> shift[channel]; michael@0: if (++channel >= channels) michael@0: channel = 0; michael@0: *bp++ = (png_byte)b; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: #ifdef PNG_READ_16BIT_SUPPORTED michael@0: case 16: michael@0: /* Double byte components, G, GA, RGB, RGBA */ michael@0: { michael@0: png_bytep bp = row; michael@0: png_bytep bp_end = bp + row_info->rowbytes; michael@0: int channel = 0; michael@0: michael@0: while (bp < bp_end) michael@0: { michael@0: int value = (bp[0] << 8) + bp[1]; michael@0: michael@0: value >>= shift[channel]; michael@0: if (++channel >= channels) michael@0: channel = 0; michael@0: *bp++ = (png_byte)(value >> 8); michael@0: *bp++ = (png_byte)(value & 0xff); michael@0: } michael@0: break; michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED michael@0: /* Scale rows of bit depth 16 down to 8 accurately */ michael@0: static void michael@0: png_do_scale_16_to_8(png_row_infop row_info, png_bytep row) michael@0: { michael@0: png_debug(1, "in png_do_scale_16_to_8"); michael@0: michael@0: if (row_info->bit_depth == 16) michael@0: { michael@0: png_bytep sp = row; /* source */ michael@0: png_bytep dp = row; /* destination */ michael@0: png_bytep ep = sp + row_info->rowbytes; /* end+1 */ michael@0: michael@0: while (sp < ep) michael@0: { michael@0: /* The input is an array of 16 bit components, these must be scaled to michael@0: * 8 bits each. For a 16 bit value V the required value (from the PNG michael@0: * specification) is: michael@0: * michael@0: * (V * 255) / 65535 michael@0: * michael@0: * This reduces to round(V / 257), or floor((V + 128.5)/257) michael@0: * michael@0: * Represent V as the two byte value vhi.vlo. Make a guess that the michael@0: * result is the top byte of V, vhi, then the correction to this value michael@0: * is: michael@0: * michael@0: * error = floor(((V-vhi.vhi) + 128.5) / 257) michael@0: * = floor(((vlo-vhi) + 128.5) / 257) michael@0: * michael@0: * This can be approximated using integer arithmetic (and a signed michael@0: * shift): michael@0: * michael@0: * error = (vlo-vhi+128) >> 8; michael@0: * michael@0: * The approximate differs from the exact answer only when (vlo-vhi) is michael@0: * 128; it then gives a correction of +1 when the exact correction is michael@0: * 0. This gives 128 errors. The exact answer (correct for all 16 bit michael@0: * input values) is: michael@0: * michael@0: * error = (vlo-vhi+128)*65535 >> 24; michael@0: * michael@0: * An alternative arithmetic calculation which also gives no errors is: michael@0: * michael@0: * (V * 255 + 32895) >> 16 michael@0: */ michael@0: michael@0: png_int_32 tmp = *sp++; /* must be signed! */ michael@0: tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24; michael@0: *dp++ = (png_byte)tmp; michael@0: } michael@0: michael@0: row_info->bit_depth = 8; michael@0: row_info->pixel_depth = (png_byte)(8 * row_info->channels); michael@0: row_info->rowbytes = row_info->width * row_info->channels; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED michael@0: static void michael@0: /* Simply discard the low byte. This was the default behavior prior michael@0: * to libpng-1.5.4. michael@0: */ michael@0: png_do_chop(png_row_infop row_info, png_bytep row) michael@0: { michael@0: png_debug(1, "in png_do_chop"); michael@0: michael@0: if (row_info->bit_depth == 16) michael@0: { michael@0: png_bytep sp = row; /* source */ michael@0: png_bytep dp = row; /* destination */ michael@0: png_bytep ep = sp + row_info->rowbytes; /* end+1 */ michael@0: michael@0: while (sp < ep) michael@0: { michael@0: *dp++ = *sp; michael@0: sp += 2; /* skip low byte */ michael@0: } michael@0: michael@0: row_info->bit_depth = 8; michael@0: row_info->pixel_depth = (png_byte)(8 * row_info->channels); michael@0: row_info->rowbytes = row_info->width * row_info->channels; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED michael@0: static void michael@0: png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) michael@0: { michael@0: png_debug(1, "in png_do_read_swap_alpha"); michael@0: michael@0: { michael@0: png_uint_32 row_width = row_info->width; michael@0: if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) michael@0: { michael@0: /* This converts from RGBA to ARGB */ michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: png_bytep sp = row + row_info->rowbytes; michael@0: png_bytep dp = sp; michael@0: png_byte save; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: save = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = save; michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_READ_16BIT_SUPPORTED michael@0: /* This converts from RRGGBBAA to AARRGGBB */ michael@0: else michael@0: { michael@0: png_bytep sp = row + row_info->rowbytes; michael@0: png_bytep dp = sp; michael@0: png_byte save[2]; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: save[0] = *(--sp); michael@0: save[1] = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = save[0]; michael@0: *(--dp) = save[1]; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) michael@0: { michael@0: /* This converts from GA to AG */ michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: png_bytep sp = row + row_info->rowbytes; michael@0: png_bytep dp = sp; michael@0: png_byte save; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: save = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = save; michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_READ_16BIT_SUPPORTED michael@0: /* This converts from GGAA to AAGG */ michael@0: else michael@0: { michael@0: png_bytep sp = row + row_info->rowbytes; michael@0: png_bytep dp = sp; michael@0: png_byte save[2]; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: save[0] = *(--sp); michael@0: save[1] = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = save[0]; michael@0: *(--dp) = save[1]; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED michael@0: static void michael@0: png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) michael@0: { michael@0: png_uint_32 row_width; michael@0: png_debug(1, "in png_do_read_invert_alpha"); michael@0: michael@0: row_width = row_info->width; michael@0: if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: /* This inverts the alpha channel in RGBA */ michael@0: png_bytep sp = row + row_info->rowbytes; michael@0: png_bytep dp = sp; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(--dp) = (png_byte)(255 - *(--sp)); michael@0: michael@0: /* This does nothing: michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: We can replace it with: michael@0: */ michael@0: sp-=3; michael@0: dp=sp; michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_READ_16BIT_SUPPORTED michael@0: /* This inverts the alpha channel in RRGGBBAA */ michael@0: else michael@0: { michael@0: png_bytep sp = row + row_info->rowbytes; michael@0: png_bytep dp = sp; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(--dp) = (png_byte)(255 - *(--sp)); michael@0: *(--dp) = (png_byte)(255 - *(--sp)); michael@0: michael@0: /* This does nothing: michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: We can replace it with: michael@0: */ michael@0: sp-=6; michael@0: dp=sp; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: /* This inverts the alpha channel in GA */ michael@0: png_bytep sp = row + row_info->rowbytes; michael@0: png_bytep dp = sp; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(--dp) = (png_byte)(255 - *(--sp)); michael@0: *(--dp) = *(--sp); michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_READ_16BIT_SUPPORTED michael@0: else michael@0: { michael@0: /* This inverts the alpha channel in GGAA */ michael@0: png_bytep sp = row + row_info->rowbytes; michael@0: png_bytep dp = sp; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(--dp) = (png_byte)(255 - *(--sp)); michael@0: *(--dp) = (png_byte)(255 - *(--sp)); michael@0: /* michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: */ michael@0: sp-=2; michael@0: dp=sp; michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_FILLER_SUPPORTED michael@0: /* Add filler channel if we have RGB color */ michael@0: static void michael@0: png_do_read_filler(png_row_infop row_info, png_bytep row, michael@0: png_uint_32 filler, png_uint_32 flags) michael@0: { michael@0: png_uint_32 i; michael@0: png_uint_32 row_width = row_info->width; michael@0: michael@0: #ifdef PNG_READ_16BIT_SUPPORTED michael@0: png_byte hi_filler = (png_byte)((filler>>8) & 0xff); michael@0: #endif michael@0: png_byte lo_filler = (png_byte)(filler & 0xff); michael@0: michael@0: png_debug(1, "in png_do_read_filler"); michael@0: michael@0: if ( michael@0: row_info->color_type == PNG_COLOR_TYPE_GRAY) michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: if (flags & PNG_FLAG_FILLER_AFTER) michael@0: { michael@0: /* This changes the data from G to GX */ michael@0: png_bytep sp = row + (png_size_t)row_width; michael@0: png_bytep dp = sp + (png_size_t)row_width; michael@0: for (i = 1; i < row_width; i++) michael@0: { michael@0: *(--dp) = lo_filler; michael@0: *(--dp) = *(--sp); michael@0: } michael@0: *(--dp) = lo_filler; michael@0: row_info->channels = 2; michael@0: row_info->pixel_depth = 16; michael@0: row_info->rowbytes = row_width * 2; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* This changes the data from G to XG */ michael@0: png_bytep sp = row + (png_size_t)row_width; michael@0: png_bytep dp = sp + (png_size_t)row_width; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = lo_filler; michael@0: } michael@0: row_info->channels = 2; michael@0: row_info->pixel_depth = 16; michael@0: row_info->rowbytes = row_width * 2; michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_READ_16BIT_SUPPORTED michael@0: else if (row_info->bit_depth == 16) michael@0: { michael@0: if (flags & PNG_FLAG_FILLER_AFTER) michael@0: { michael@0: /* This changes the data from GG to GGXX */ michael@0: png_bytep sp = row + (png_size_t)row_width * 2; michael@0: png_bytep dp = sp + (png_size_t)row_width * 2; michael@0: for (i = 1; i < row_width; i++) michael@0: { michael@0: *(--dp) = hi_filler; michael@0: *(--dp) = lo_filler; michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: } michael@0: *(--dp) = hi_filler; michael@0: *(--dp) = lo_filler; michael@0: row_info->channels = 2; michael@0: row_info->pixel_depth = 32; michael@0: row_info->rowbytes = row_width * 4; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* This changes the data from GG to XXGG */ michael@0: png_bytep sp = row + (png_size_t)row_width * 2; michael@0: png_bytep dp = sp + (png_size_t)row_width * 2; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = hi_filler; michael@0: *(--dp) = lo_filler; michael@0: } michael@0: row_info->channels = 2; michael@0: row_info->pixel_depth = 32; michael@0: row_info->rowbytes = row_width * 4; michael@0: } michael@0: } michael@0: #endif michael@0: } /* COLOR_TYPE == GRAY */ michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_RGB) michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: if (flags & PNG_FLAG_FILLER_AFTER) michael@0: { michael@0: /* This changes the data from RGB to RGBX */ michael@0: png_bytep sp = row + (png_size_t)row_width * 3; michael@0: png_bytep dp = sp + (png_size_t)row_width; michael@0: for (i = 1; i < row_width; i++) michael@0: { michael@0: *(--dp) = lo_filler; michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: } michael@0: *(--dp) = lo_filler; michael@0: row_info->channels = 4; michael@0: row_info->pixel_depth = 32; michael@0: row_info->rowbytes = row_width * 4; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* This changes the data from RGB to XRGB */ michael@0: png_bytep sp = row + (png_size_t)row_width * 3; michael@0: png_bytep dp = sp + (png_size_t)row_width; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = lo_filler; michael@0: } michael@0: row_info->channels = 4; michael@0: row_info->pixel_depth = 32; michael@0: row_info->rowbytes = row_width * 4; michael@0: } michael@0: } michael@0: michael@0: #ifdef PNG_READ_16BIT_SUPPORTED michael@0: else if (row_info->bit_depth == 16) michael@0: { michael@0: if (flags & PNG_FLAG_FILLER_AFTER) michael@0: { michael@0: /* This changes the data from RRGGBB to RRGGBBXX */ michael@0: png_bytep sp = row + (png_size_t)row_width * 6; michael@0: png_bytep dp = sp + (png_size_t)row_width * 2; michael@0: for (i = 1; i < row_width; i++) michael@0: { michael@0: *(--dp) = hi_filler; michael@0: *(--dp) = lo_filler; michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: } michael@0: *(--dp) = hi_filler; michael@0: *(--dp) = lo_filler; michael@0: row_info->channels = 4; michael@0: row_info->pixel_depth = 64; michael@0: row_info->rowbytes = row_width * 8; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* This changes the data from RRGGBB to XXRRGGBB */ michael@0: png_bytep sp = row + (png_size_t)row_width * 6; michael@0: png_bytep dp = sp + (png_size_t)row_width * 2; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = *(--sp); michael@0: *(--dp) = hi_filler; michael@0: *(--dp) = lo_filler; michael@0: } michael@0: michael@0: row_info->channels = 4; michael@0: row_info->pixel_depth = 64; michael@0: row_info->rowbytes = row_width * 8; michael@0: } michael@0: } michael@0: #endif michael@0: } /* COLOR_TYPE == RGB */ michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED michael@0: /* Expand grayscale files to RGB, with or without alpha */ michael@0: static void michael@0: png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) michael@0: { michael@0: png_uint_32 i; michael@0: png_uint_32 row_width = row_info->width; michael@0: michael@0: png_debug(1, "in png_do_gray_to_rgb"); michael@0: michael@0: if (row_info->bit_depth >= 8 && michael@0: !(row_info->color_type & PNG_COLOR_MASK_COLOR)) michael@0: { michael@0: if (row_info->color_type == PNG_COLOR_TYPE_GRAY) michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: /* This changes G to RGB */ michael@0: png_bytep sp = row + (png_size_t)row_width - 1; michael@0: png_bytep dp = sp + (png_size_t)row_width * 2; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(dp--) = *sp; michael@0: *(dp--) = *sp; michael@0: *(dp--) = *(sp--); michael@0: } michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* This changes GG to RRGGBB */ michael@0: png_bytep sp = row + (png_size_t)row_width * 2 - 1; michael@0: png_bytep dp = sp + (png_size_t)row_width * 4; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(dp--) = *sp; michael@0: *(dp--) = *(sp - 1); michael@0: *(dp--) = *sp; michael@0: *(dp--) = *(sp - 1); michael@0: *(dp--) = *(sp--); michael@0: *(dp--) = *(sp--); michael@0: } michael@0: } michael@0: } michael@0: michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: /* This changes GA to RGBA */ michael@0: png_bytep sp = row + (png_size_t)row_width * 2 - 1; michael@0: png_bytep dp = sp + (png_size_t)row_width * 2; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(dp--) = *(sp--); michael@0: *(dp--) = *sp; michael@0: *(dp--) = *sp; michael@0: *(dp--) = *(sp--); michael@0: } michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* This changes GGAA to RRGGBBAA */ michael@0: png_bytep sp = row + (png_size_t)row_width * 4 - 1; michael@0: png_bytep dp = sp + (png_size_t)row_width * 4; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *(dp--) = *(sp--); michael@0: *(dp--) = *(sp--); michael@0: *(dp--) = *sp; michael@0: *(dp--) = *(sp - 1); michael@0: *(dp--) = *sp; michael@0: *(dp--) = *(sp - 1); michael@0: *(dp--) = *(sp--); michael@0: *(dp--) = *(sp--); michael@0: } michael@0: } michael@0: } michael@0: row_info->channels = (png_byte)(row_info->channels + 2); michael@0: row_info->color_type |= PNG_COLOR_MASK_COLOR; michael@0: row_info->pixel_depth = (png_byte)(row_info->channels * michael@0: row_info->bit_depth); michael@0: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED michael@0: /* Reduce RGB files to grayscale, with or without alpha michael@0: * using the equation given in Poynton's ColorFAQ of 1998-01-04 at michael@0: * (THIS LINK IS DEAD June 2008 but michael@0: * versions dated 1998 through November 2002 have been archived at michael@0: * http://web.archive.org/web/20000816232553/http://www.inforamp.net/ michael@0: * ~poynton/notes/colour_and_gamma/ColorFAQ.txt ) michael@0: * Charles Poynton poynton at poynton.com michael@0: * michael@0: * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B michael@0: * michael@0: * which can be expressed with integers as michael@0: * michael@0: * Y = (6969 * R + 23434 * G + 2365 * B)/32768 michael@0: * michael@0: * Poynton's current link (as of January 2003 through July 2011): michael@0: * michael@0: * has changed the numbers slightly: michael@0: * michael@0: * Y = 0.2126*R + 0.7152*G + 0.0722*B michael@0: * michael@0: * which can be expressed with integers as michael@0: * michael@0: * Y = (6966 * R + 23436 * G + 2366 * B)/32768 michael@0: * michael@0: * Historically, however, libpng uses numbers derived from the ITU-R Rec 709 michael@0: * end point chromaticities and the D65 white point. Depending on the michael@0: * precision used for the D65 white point this produces a variety of different michael@0: * numbers, however if the four decimal place value used in ITU-R Rec 709 is michael@0: * used (0.3127,0.3290) the Y calculation would be: michael@0: * michael@0: * Y = (6968 * R + 23435 * G + 2366 * B)/32768 michael@0: * michael@0: * While this is correct the rounding results in an overflow for white, because michael@0: * the sum of the rounded coefficients is 32769, not 32768. Consequently michael@0: * libpng uses, instead, the closest non-overflowing approximation: michael@0: * michael@0: * Y = (6968 * R + 23434 * G + 2366 * B)/32768 michael@0: * michael@0: * Starting with libpng-1.5.5, if the image being converted has a cHRM chunk michael@0: * (including an sRGB chunk) then the chromaticities are used to calculate the michael@0: * coefficients. See the chunk handling in pngrutil.c for more information. michael@0: * michael@0: * In all cases the calculation is to be done in a linear colorspace. If no michael@0: * gamma information is available to correct the encoding of the original RGB michael@0: * values this results in an implicit assumption that the original PNG RGB michael@0: * values were linear. michael@0: * michael@0: * Other integer coefficents can be used via png_set_rgb_to_gray(). Because michael@0: * the API takes just red and green coefficients the blue coefficient is michael@0: * calculated to make the sum 32768. This will result in different rounding michael@0: * to that used above. michael@0: */ michael@0: static int michael@0: png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row) michael@0: michael@0: { michael@0: int rgb_error = 0; michael@0: michael@0: png_debug(1, "in png_do_rgb_to_gray"); michael@0: michael@0: if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) && michael@0: (row_info->color_type & PNG_COLOR_MASK_COLOR)) michael@0: { michael@0: PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; michael@0: PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; michael@0: PNG_CONST png_uint_32 bc = 32768 - rc - gc; michael@0: PNG_CONST png_uint_32 row_width = row_info->width; michael@0: PNG_CONST int have_alpha = michael@0: (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0; michael@0: michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: /* Notice that gamma to/from 1 are not necessarily inverses (if michael@0: * there is an overall gamma correction). Prior to 1.5.5 this code michael@0: * checked the linearized values for equality; this doesn't match michael@0: * the documentation, the original values must be checked. michael@0: */ michael@0: if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) michael@0: { michael@0: png_bytep sp = row; michael@0: png_bytep dp = row; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: png_byte red = *(sp++); michael@0: png_byte green = *(sp++); michael@0: png_byte blue = *(sp++); michael@0: michael@0: if (red != green || red != blue) michael@0: { michael@0: red = png_ptr->gamma_to_1[red]; michael@0: green = png_ptr->gamma_to_1[green]; michael@0: blue = png_ptr->gamma_to_1[blue]; michael@0: michael@0: rgb_error |= 1; michael@0: *(dp++) = png_ptr->gamma_from_1[ michael@0: (rc*red + gc*green + bc*blue + 16384)>>15]; michael@0: } michael@0: michael@0: else michael@0: { michael@0: /* If there is no overall correction the table will not be michael@0: * set. michael@0: */ michael@0: if (png_ptr->gamma_table != NULL) michael@0: red = png_ptr->gamma_table[red]; michael@0: michael@0: *(dp++) = red; michael@0: } michael@0: michael@0: if (have_alpha) michael@0: *(dp++) = *(sp++); michael@0: } michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: png_bytep sp = row; michael@0: png_bytep dp = row; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: png_byte red = *(sp++); michael@0: png_byte green = *(sp++); michael@0: png_byte blue = *(sp++); michael@0: michael@0: if (red != green || red != blue) michael@0: { michael@0: rgb_error |= 1; michael@0: /* NOTE: this is the historical approach which simply michael@0: * truncates the results. michael@0: */ michael@0: *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); michael@0: } michael@0: michael@0: else michael@0: *(dp++) = red; michael@0: michael@0: if (have_alpha) michael@0: *(dp++) = *(sp++); michael@0: } michael@0: } michael@0: } michael@0: michael@0: else /* RGB bit_depth == 16 */ michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL) michael@0: { michael@0: png_bytep sp = row; michael@0: png_bytep dp = row; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: png_uint_16 red, green, blue, w; michael@0: michael@0: red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; michael@0: green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; michael@0: blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; michael@0: michael@0: if (red == green && red == blue) michael@0: { michael@0: if (png_ptr->gamma_16_table != NULL) michael@0: w = png_ptr->gamma_16_table[(red&0xff) michael@0: >> png_ptr->gamma_shift][red>>8]; michael@0: michael@0: else michael@0: w = red; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) michael@0: >> png_ptr->gamma_shift][red>>8]; michael@0: png_uint_16 green_1 = michael@0: png_ptr->gamma_16_to_1[(green&0xff) >> michael@0: png_ptr->gamma_shift][green>>8]; michael@0: png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) michael@0: >> png_ptr->gamma_shift][blue>>8]; michael@0: png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 michael@0: + bc*blue_1 + 16384)>>15); michael@0: w = png_ptr->gamma_16_from_1[(gray16&0xff) >> michael@0: png_ptr->gamma_shift][gray16 >> 8]; michael@0: rgb_error |= 1; michael@0: } michael@0: michael@0: *(dp++) = (png_byte)((w>>8) & 0xff); michael@0: *(dp++) = (png_byte)(w & 0xff); michael@0: michael@0: if (have_alpha) michael@0: { michael@0: *(dp++) = *(sp++); michael@0: *(dp++) = *(sp++); michael@0: } michael@0: } michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: png_bytep sp = row; michael@0: png_bytep dp = row; michael@0: png_uint_32 i; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: png_uint_16 red, green, blue, gray16; michael@0: michael@0: red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; michael@0: green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; michael@0: blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2; michael@0: michael@0: if (red != green || red != blue) michael@0: rgb_error |= 1; michael@0: michael@0: /* From 1.5.5 in the 16 bit case do the accurate conversion even michael@0: * in the 'fast' case - this is because this is where the code michael@0: * ends up when handling linear 16 bit data. michael@0: */ michael@0: gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >> michael@0: 15); michael@0: *(dp++) = (png_byte)((gray16>>8) & 0xff); michael@0: *(dp++) = (png_byte)(gray16 & 0xff); michael@0: michael@0: if (have_alpha) michael@0: { michael@0: *(dp++) = *(sp++); michael@0: *(dp++) = *(sp++); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: row_info->channels = (png_byte)(row_info->channels - 2); michael@0: row_info->color_type = (png_byte)(row_info->color_type & michael@0: ~PNG_COLOR_MASK_COLOR); michael@0: row_info->pixel_depth = (png_byte)(row_info->channels * michael@0: row_info->bit_depth); michael@0: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); michael@0: } michael@0: return rgb_error; michael@0: } michael@0: #endif michael@0: michael@0: #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ michael@0: defined(PNG_READ_ALPHA_MODE_SUPPORTED) michael@0: /* Replace any alpha or transparency with the supplied background color. michael@0: * "background" is already in the screen gamma, while "background_1" is michael@0: * at a gamma of 1.0. Paletted files have already been taken care of. michael@0: */ michael@0: static void michael@0: png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr) michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: png_const_bytep gamma_table = png_ptr->gamma_table; michael@0: png_const_bytep gamma_from_1 = png_ptr->gamma_from_1; michael@0: png_const_bytep gamma_to_1 = png_ptr->gamma_to_1; michael@0: png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table; michael@0: png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1; michael@0: png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1; michael@0: int gamma_shift = png_ptr->gamma_shift; michael@0: int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0; michael@0: #endif michael@0: michael@0: png_bytep sp; michael@0: png_uint_32 i; michael@0: png_uint_32 row_width = row_info->width; michael@0: int shift; michael@0: michael@0: png_debug(1, "in png_do_compose"); michael@0: michael@0: { michael@0: switch (row_info->color_type) michael@0: { michael@0: case PNG_COLOR_TYPE_GRAY: michael@0: { michael@0: switch (row_info->bit_depth) michael@0: { michael@0: case 1: michael@0: { michael@0: sp = row; michael@0: shift = 7; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if ((png_uint_16)((*sp >> shift) & 0x01) michael@0: == png_ptr->trans_color.gray) michael@0: { michael@0: unsigned int tmp = *sp & (0x7f7f >> (7 - shift)); michael@0: tmp |= png_ptr->background.gray << shift; michael@0: *sp = (png_byte)(tmp & 0xff); michael@0: } michael@0: michael@0: if (!shift) michael@0: { michael@0: shift = 7; michael@0: sp++; michael@0: } michael@0: michael@0: else michael@0: shift--; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 2: michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (gamma_table != NULL) michael@0: { michael@0: sp = row; michael@0: shift = 6; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if ((png_uint_16)((*sp >> shift) & 0x03) michael@0: == png_ptr->trans_color.gray) michael@0: { michael@0: unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); michael@0: tmp |= png_ptr->background.gray << shift; michael@0: *sp = (png_byte)(tmp & 0xff); michael@0: } michael@0: michael@0: else michael@0: { michael@0: unsigned int p = (*sp >> shift) & 0x03; michael@0: unsigned int g = (gamma_table [p | (p << 2) | michael@0: (p << 4) | (p << 6)] >> 6) & 0x03; michael@0: unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); michael@0: tmp |= g << shift; michael@0: *sp = (png_byte)(tmp & 0xff); michael@0: } michael@0: michael@0: if (!shift) michael@0: { michael@0: shift = 6; michael@0: sp++; michael@0: } michael@0: michael@0: else michael@0: shift -= 2; michael@0: } michael@0: } michael@0: michael@0: else michael@0: #endif michael@0: { michael@0: sp = row; michael@0: shift = 6; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if ((png_uint_16)((*sp >> shift) & 0x03) michael@0: == png_ptr->trans_color.gray) michael@0: { michael@0: unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); michael@0: tmp |= png_ptr->background.gray << shift; michael@0: *sp = (png_byte)(tmp & 0xff); michael@0: } michael@0: michael@0: if (!shift) michael@0: { michael@0: shift = 6; michael@0: sp++; michael@0: } michael@0: michael@0: else michael@0: shift -= 2; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 4: michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (gamma_table != NULL) michael@0: { michael@0: sp = row; michael@0: shift = 4; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if ((png_uint_16)((*sp >> shift) & 0x0f) michael@0: == png_ptr->trans_color.gray) michael@0: { michael@0: unsigned int tmp = *sp & (0xf0f >> (4 - shift)); michael@0: tmp |= png_ptr->background.gray << shift; michael@0: *sp = (png_byte)(tmp & 0xff); michael@0: } michael@0: michael@0: else michael@0: { michael@0: unsigned int p = (*sp >> shift) & 0x0f; michael@0: unsigned int g = (gamma_table[p | (p << 4)] >> 4) & michael@0: 0x0f; michael@0: unsigned int tmp = *sp & (0xf0f >> (4 - shift)); michael@0: tmp |= g << shift; michael@0: *sp = (png_byte)(tmp & 0xff); michael@0: } michael@0: michael@0: if (!shift) michael@0: { michael@0: shift = 4; michael@0: sp++; michael@0: } michael@0: michael@0: else michael@0: shift -= 4; michael@0: } michael@0: } michael@0: michael@0: else michael@0: #endif michael@0: { michael@0: sp = row; michael@0: shift = 4; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if ((png_uint_16)((*sp >> shift) & 0x0f) michael@0: == png_ptr->trans_color.gray) michael@0: { michael@0: unsigned int tmp = *sp & (0xf0f >> (4 - shift)); michael@0: tmp |= png_ptr->background.gray << shift; michael@0: *sp = (png_byte)(tmp & 0xff); michael@0: } michael@0: michael@0: if (!shift) michael@0: { michael@0: shift = 4; michael@0: sp++; michael@0: } michael@0: michael@0: else michael@0: shift -= 4; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 8: michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (gamma_table != NULL) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp++) michael@0: { michael@0: if (*sp == png_ptr->trans_color.gray) michael@0: *sp = (png_byte)png_ptr->background.gray; michael@0: michael@0: else michael@0: *sp = gamma_table[*sp]; michael@0: } michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp++) michael@0: { michael@0: if (*sp == png_ptr->trans_color.gray) michael@0: *sp = (png_byte)png_ptr->background.gray; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 16: michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (gamma_16 != NULL) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 2) michael@0: { michael@0: png_uint_16 v; michael@0: michael@0: v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); michael@0: michael@0: if (v == png_ptr->trans_color.gray) michael@0: { michael@0: /* Background is already in screen gamma */ michael@0: *sp = (png_byte)((png_ptr->background.gray >> 8) michael@0: & 0xff); michael@0: *(sp + 1) = (png_byte)(png_ptr->background.gray michael@0: & 0xff); michael@0: } michael@0: michael@0: else michael@0: { michael@0: v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: } michael@0: } michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 2) michael@0: { michael@0: png_uint_16 v; michael@0: michael@0: v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); michael@0: michael@0: if (v == png_ptr->trans_color.gray) michael@0: { michael@0: *sp = (png_byte)((png_ptr->background.gray >> 8) michael@0: & 0xff); michael@0: *(sp + 1) = (png_byte)(png_ptr->background.gray michael@0: & 0xff); michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case PNG_COLOR_TYPE_RGB: michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (gamma_table != NULL) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 3) michael@0: { michael@0: if (*sp == png_ptr->trans_color.red && michael@0: *(sp + 1) == png_ptr->trans_color.green && michael@0: *(sp + 2) == png_ptr->trans_color.blue) michael@0: { michael@0: *sp = (png_byte)png_ptr->background.red; michael@0: *(sp + 1) = (png_byte)png_ptr->background.green; michael@0: *(sp + 2) = (png_byte)png_ptr->background.blue; michael@0: } michael@0: michael@0: else michael@0: { michael@0: *sp = gamma_table[*sp]; michael@0: *(sp + 1) = gamma_table[*(sp + 1)]; michael@0: *(sp + 2) = gamma_table[*(sp + 2)]; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 3) michael@0: { michael@0: if (*sp == png_ptr->trans_color.red && michael@0: *(sp + 1) == png_ptr->trans_color.green && michael@0: *(sp + 2) == png_ptr->trans_color.blue) michael@0: { michael@0: *sp = (png_byte)png_ptr->background.red; michael@0: *(sp + 1) = (png_byte)png_ptr->background.green; michael@0: *(sp + 2) = (png_byte)png_ptr->background.blue; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else /* if (row_info->bit_depth == 16) */ michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (gamma_16 != NULL) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 6) michael@0: { michael@0: png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); michael@0: michael@0: png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) michael@0: + *(sp + 3)); michael@0: michael@0: png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) michael@0: + *(sp + 5)); michael@0: michael@0: if (r == png_ptr->trans_color.red && michael@0: g == png_ptr->trans_color.green && michael@0: b == png_ptr->trans_color.blue) michael@0: { michael@0: /* Background is already in screen gamma */ michael@0: *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); michael@0: *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) michael@0: & 0xff); michael@0: *(sp + 3) = (png_byte)(png_ptr->background.green michael@0: & 0xff); michael@0: *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) michael@0: & 0xff); michael@0: *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: michael@0: v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; michael@0: *(sp + 2) = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 3) = (png_byte)(v & 0xff); michael@0: michael@0: v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; michael@0: *(sp + 4) = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 5) = (png_byte)(v & 0xff); michael@0: } michael@0: } michael@0: } michael@0: michael@0: else michael@0: #endif michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 6) michael@0: { michael@0: png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); michael@0: michael@0: png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) michael@0: + *(sp + 3)); michael@0: michael@0: png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) michael@0: + *(sp + 5)); michael@0: michael@0: if (r == png_ptr->trans_color.red && michael@0: g == png_ptr->trans_color.green && michael@0: b == png_ptr->trans_color.blue) michael@0: { michael@0: *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); michael@0: *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) michael@0: & 0xff); michael@0: *(sp + 3) = (png_byte)(png_ptr->background.green michael@0: & 0xff); michael@0: *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) michael@0: & 0xff); michael@0: *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case PNG_COLOR_TYPE_GRAY_ALPHA: michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (gamma_to_1 != NULL && gamma_from_1 != NULL && michael@0: gamma_table != NULL) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 2) michael@0: { michael@0: png_uint_16 a = *(sp + 1); michael@0: michael@0: if (a == 0xff) michael@0: *sp = gamma_table[*sp]; michael@0: michael@0: else if (a == 0) michael@0: { michael@0: /* Background is already in screen gamma */ michael@0: *sp = (png_byte)png_ptr->background.gray; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_byte v, w; michael@0: michael@0: v = gamma_to_1[*sp]; michael@0: png_composite(w, v, a, png_ptr->background_1.gray); michael@0: if (!optimize) michael@0: w = gamma_from_1[w]; michael@0: *sp = w; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 2) michael@0: { michael@0: png_byte a = *(sp + 1); michael@0: michael@0: if (a == 0) michael@0: *sp = (png_byte)png_ptr->background.gray; michael@0: michael@0: else if (a < 0xff) michael@0: png_composite(*sp, *sp, a, png_ptr->background.gray); michael@0: } michael@0: } michael@0: } michael@0: else /* if (png_ptr->bit_depth == 16) */ michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (gamma_16 != NULL && gamma_16_from_1 != NULL && michael@0: gamma_16_to_1 != NULL) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 4) michael@0: { michael@0: png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8) michael@0: + *(sp + 3)); michael@0: michael@0: if (a == (png_uint_16)0xffff) michael@0: { michael@0: png_uint_16 v; michael@0: michael@0: v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: } michael@0: michael@0: else if (a == 0) michael@0: { michael@0: /* Background is already in screen gamma */ michael@0: *sp = (png_byte)((png_ptr->background.gray >> 8) michael@0: & 0xff); michael@0: *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_uint_16 g, v, w; michael@0: michael@0: g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; michael@0: png_composite_16(v, g, a, png_ptr->background_1.gray); michael@0: if (optimize) michael@0: w = v; michael@0: else michael@0: w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8]; michael@0: *sp = (png_byte)((w >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(w & 0xff); michael@0: } michael@0: } michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 4) michael@0: { michael@0: png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8) michael@0: + *(sp + 3)); michael@0: michael@0: if (a == 0) michael@0: { michael@0: *sp = (png_byte)((png_ptr->background.gray >> 8) michael@0: & 0xff); michael@0: *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); michael@0: } michael@0: michael@0: else if (a < 0xffff) michael@0: { michael@0: png_uint_16 g, v; michael@0: michael@0: g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); michael@0: png_composite_16(v, g, a, png_ptr->background.gray); michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case PNG_COLOR_TYPE_RGB_ALPHA: michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (gamma_to_1 != NULL && gamma_from_1 != NULL && michael@0: gamma_table != NULL) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 4) michael@0: { michael@0: png_byte a = *(sp + 3); michael@0: michael@0: if (a == 0xff) michael@0: { michael@0: *sp = gamma_table[*sp]; michael@0: *(sp + 1) = gamma_table[*(sp + 1)]; michael@0: *(sp + 2) = gamma_table[*(sp + 2)]; michael@0: } michael@0: michael@0: else if (a == 0) michael@0: { michael@0: /* Background is already in screen gamma */ michael@0: *sp = (png_byte)png_ptr->background.red; michael@0: *(sp + 1) = (png_byte)png_ptr->background.green; michael@0: *(sp + 2) = (png_byte)png_ptr->background.blue; michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_byte v, w; michael@0: michael@0: v = gamma_to_1[*sp]; michael@0: png_composite(w, v, a, png_ptr->background_1.red); michael@0: if (!optimize) w = gamma_from_1[w]; michael@0: *sp = w; michael@0: michael@0: v = gamma_to_1[*(sp + 1)]; michael@0: png_composite(w, v, a, png_ptr->background_1.green); michael@0: if (!optimize) w = gamma_from_1[w]; michael@0: *(sp + 1) = w; michael@0: michael@0: v = gamma_to_1[*(sp + 2)]; michael@0: png_composite(w, v, a, png_ptr->background_1.blue); michael@0: if (!optimize) w = gamma_from_1[w]; michael@0: *(sp + 2) = w; michael@0: } michael@0: } michael@0: } michael@0: else michael@0: #endif michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 4) michael@0: { michael@0: png_byte a = *(sp + 3); michael@0: michael@0: if (a == 0) michael@0: { michael@0: *sp = (png_byte)png_ptr->background.red; michael@0: *(sp + 1) = (png_byte)png_ptr->background.green; michael@0: *(sp + 2) = (png_byte)png_ptr->background.blue; michael@0: } michael@0: michael@0: else if (a < 0xff) michael@0: { michael@0: png_composite(*sp, *sp, a, png_ptr->background.red); michael@0: michael@0: png_composite(*(sp + 1), *(sp + 1), a, michael@0: png_ptr->background.green); michael@0: michael@0: png_composite(*(sp + 2), *(sp + 2), a, michael@0: png_ptr->background.blue); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: else /* if (row_info->bit_depth == 16) */ michael@0: { michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if (gamma_16 != NULL && gamma_16_from_1 != NULL && michael@0: gamma_16_to_1 != NULL) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 8) michael@0: { michael@0: png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) michael@0: << 8) + (png_uint_16)(*(sp + 7))); michael@0: michael@0: if (a == (png_uint_16)0xffff) michael@0: { michael@0: png_uint_16 v; michael@0: michael@0: v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: michael@0: v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; michael@0: *(sp + 2) = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 3) = (png_byte)(v & 0xff); michael@0: michael@0: v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; michael@0: *(sp + 4) = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 5) = (png_byte)(v & 0xff); michael@0: } michael@0: michael@0: else if (a == 0) michael@0: { michael@0: /* Background is already in screen gamma */ michael@0: *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); michael@0: *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) michael@0: & 0xff); michael@0: *(sp + 3) = (png_byte)(png_ptr->background.green michael@0: & 0xff); michael@0: *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) michael@0: & 0xff); michael@0: *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); michael@0: } michael@0: michael@0: else michael@0: { michael@0: png_uint_16 v, w; michael@0: michael@0: v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; michael@0: png_composite_16(w, v, a, png_ptr->background_1.red); michael@0: if (!optimize) michael@0: w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> michael@0: 8]; michael@0: *sp = (png_byte)((w >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(w & 0xff); michael@0: michael@0: v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; michael@0: png_composite_16(w, v, a, png_ptr->background_1.green); michael@0: if (!optimize) michael@0: w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> michael@0: 8]; michael@0: michael@0: *(sp + 2) = (png_byte)((w >> 8) & 0xff); michael@0: *(sp + 3) = (png_byte)(w & 0xff); michael@0: michael@0: v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; michael@0: png_composite_16(w, v, a, png_ptr->background_1.blue); michael@0: if (!optimize) michael@0: w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> michael@0: 8]; michael@0: michael@0: *(sp + 4) = (png_byte)((w >> 8) & 0xff); michael@0: *(sp + 5) = (png_byte)(w & 0xff); michael@0: } michael@0: } michael@0: } michael@0: michael@0: else michael@0: #endif michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++, sp += 8) michael@0: { michael@0: png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) michael@0: << 8) + (png_uint_16)(*(sp + 7))); michael@0: michael@0: if (a == 0) michael@0: { michael@0: *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); michael@0: *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) michael@0: & 0xff); michael@0: *(sp + 3) = (png_byte)(png_ptr->background.green michael@0: & 0xff); michael@0: *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) michael@0: & 0xff); michael@0: *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); michael@0: } michael@0: michael@0: else if (a < 0xffff) michael@0: { michael@0: png_uint_16 v; michael@0: michael@0: png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); michael@0: png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) michael@0: + *(sp + 3)); michael@0: png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) michael@0: + *(sp + 5)); michael@0: michael@0: png_composite_16(v, r, a, png_ptr->background.red); michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: michael@0: png_composite_16(v, g, a, png_ptr->background.green); michael@0: *(sp + 2) = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 3) = (png_byte)(v & 0xff); michael@0: michael@0: png_composite_16(v, b, a, png_ptr->background.blue); michael@0: *(sp + 4) = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 5) = (png_byte)(v & 0xff); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_READ_ALPHA_MODE_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: /* Gamma correct the image, avoiding the alpha channel. Make sure michael@0: * you do this after you deal with the transparency issue on grayscale michael@0: * or RGB images. If your bit depth is 8, use gamma_table, if it michael@0: * is 16, use gamma_16_table and gamma_shift. Build these with michael@0: * build_gamma_table(). michael@0: */ michael@0: static void michael@0: png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr) michael@0: { michael@0: png_const_bytep gamma_table = png_ptr->gamma_table; michael@0: png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table; michael@0: int gamma_shift = png_ptr->gamma_shift; michael@0: michael@0: png_bytep sp; michael@0: png_uint_32 i; michael@0: png_uint_32 row_width=row_info->width; michael@0: michael@0: png_debug(1, "in png_do_gamma"); michael@0: michael@0: if (((row_info->bit_depth <= 8 && gamma_table != NULL) || michael@0: (row_info->bit_depth == 16 && gamma_16_table != NULL))) michael@0: { michael@0: switch (row_info->color_type) michael@0: { michael@0: case PNG_COLOR_TYPE_RGB: michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *sp = gamma_table[*sp]; michael@0: sp++; michael@0: *sp = gamma_table[*sp]; michael@0: sp++; michael@0: *sp = gamma_table[*sp]; michael@0: sp++; michael@0: } michael@0: } michael@0: michael@0: else /* if (row_info->bit_depth == 16) */ michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: png_uint_16 v; michael@0: michael@0: v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: sp += 2; michael@0: michael@0: v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: sp += 2; michael@0: michael@0: v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: sp += 2; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case PNG_COLOR_TYPE_RGB_ALPHA: michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *sp = gamma_table[*sp]; michael@0: sp++; michael@0: michael@0: *sp = gamma_table[*sp]; michael@0: sp++; michael@0: michael@0: *sp = gamma_table[*sp]; michael@0: sp++; michael@0: michael@0: sp++; michael@0: } michael@0: } michael@0: michael@0: else /* if (row_info->bit_depth == 16) */ michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: sp += 2; michael@0: michael@0: v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: sp += 2; michael@0: michael@0: v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: sp += 4; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case PNG_COLOR_TYPE_GRAY_ALPHA: michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *sp = gamma_table[*sp]; michael@0: sp += 2; michael@0: } michael@0: } michael@0: michael@0: else /* if (row_info->bit_depth == 16) */ michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: sp += 4; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case PNG_COLOR_TYPE_GRAY: michael@0: { michael@0: if (row_info->bit_depth == 2) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i += 4) michael@0: { michael@0: int a = *sp & 0xc0; michael@0: int b = *sp & 0x30; michael@0: int c = *sp & 0x0c; michael@0: int d = *sp & 0x03; michael@0: michael@0: *sp = (png_byte)( michael@0: ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)| michael@0: ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)| michael@0: ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)| michael@0: ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); michael@0: sp++; michael@0: } michael@0: } michael@0: michael@0: if (row_info->bit_depth == 4) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i += 2) michael@0: { michael@0: int msb = *sp & 0xf0; michael@0: int lsb = *sp & 0x0f; michael@0: michael@0: *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) michael@0: | (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); michael@0: sp++; michael@0: } michael@0: } michael@0: michael@0: else if (row_info->bit_depth == 8) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *sp = gamma_table[*sp]; michael@0: sp++; michael@0: } michael@0: } michael@0: michael@0: else if (row_info->bit_depth == 16) michael@0: { michael@0: sp = row; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; michael@0: *sp = (png_byte)((v >> 8) & 0xff); michael@0: *(sp + 1) = (png_byte)(v & 0xff); michael@0: sp += 2; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_ALPHA_MODE_SUPPORTED michael@0: /* Encode the alpha channel to the output gamma (the input channel is always michael@0: * linear.) Called only with color types that have an alpha channel. Needs the michael@0: * from_1 tables. michael@0: */ michael@0: static void michael@0: png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr) michael@0: { michael@0: png_uint_32 row_width = row_info->width; michael@0: michael@0: png_debug(1, "in png_do_encode_alpha"); michael@0: michael@0: if (row_info->color_type & PNG_COLOR_MASK_ALPHA) michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: PNG_CONST png_bytep table = png_ptr->gamma_from_1; michael@0: michael@0: if (table != NULL) michael@0: { michael@0: PNG_CONST int step = michael@0: (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2; michael@0: michael@0: /* The alpha channel is the last component: */ michael@0: row += step - 1; michael@0: michael@0: for (; row_width > 0; --row_width, row += step) michael@0: *row = table[*row]; michael@0: michael@0: return; michael@0: } michael@0: } michael@0: michael@0: else if (row_info->bit_depth == 16) michael@0: { michael@0: PNG_CONST png_uint_16pp table = png_ptr->gamma_16_from_1; michael@0: PNG_CONST int gamma_shift = png_ptr->gamma_shift; michael@0: michael@0: if (table != NULL) michael@0: { michael@0: PNG_CONST int step = michael@0: (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4; michael@0: michael@0: /* The alpha channel is the last component: */ michael@0: row += step - 2; michael@0: michael@0: for (; row_width > 0; --row_width, row += step) michael@0: { michael@0: png_uint_16 v; michael@0: michael@0: v = table[*(row + 1) >> gamma_shift][*row]; michael@0: *row = (png_byte)((v >> 8) & 0xff); michael@0: *(row + 1) = (png_byte)(v & 0xff); michael@0: } michael@0: michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* Only get to here if called with a weird row_info; no harm has been done, michael@0: * so just issue a warning. michael@0: */ michael@0: png_warning(png_ptr, "png_do_encode_alpha: unexpected call"); michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_EXPAND_SUPPORTED michael@0: /* Expands a palette row to an RGB or RGBA row depending michael@0: * upon whether you supply trans and num_trans. michael@0: */ michael@0: static void michael@0: png_do_expand_palette(png_row_infop row_info, png_bytep row, michael@0: png_const_colorp palette, png_const_bytep trans_alpha, int num_trans) michael@0: { michael@0: int shift, value; michael@0: png_bytep sp, dp; michael@0: png_uint_32 i; michael@0: png_uint_32 row_width=row_info->width; michael@0: michael@0: png_debug(1, "in png_do_expand_palette"); michael@0: michael@0: if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: if (row_info->bit_depth < 8) michael@0: { michael@0: switch (row_info->bit_depth) michael@0: { michael@0: case 1: michael@0: { michael@0: sp = row + (png_size_t)((row_width - 1) >> 3); michael@0: dp = row + (png_size_t)row_width - 1; michael@0: shift = 7 - (int)((row_width + 7) & 0x07); michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if ((*sp >> shift) & 0x01) michael@0: *dp = 1; michael@0: michael@0: else michael@0: *dp = 0; michael@0: michael@0: if (shift == 7) michael@0: { michael@0: shift = 0; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: shift++; michael@0: michael@0: dp--; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 2: michael@0: { michael@0: sp = row + (png_size_t)((row_width - 1) >> 2); michael@0: dp = row + (png_size_t)row_width - 1; michael@0: shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: value = (*sp >> shift) & 0x03; michael@0: *dp = (png_byte)value; michael@0: if (shift == 6) michael@0: { michael@0: shift = 0; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: shift += 2; michael@0: michael@0: dp--; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 4: michael@0: { michael@0: sp = row + (png_size_t)((row_width - 1) >> 1); michael@0: dp = row + (png_size_t)row_width - 1; michael@0: shift = (int)((row_width & 0x01) << 2); michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: value = (*sp >> shift) & 0x0f; michael@0: *dp = (png_byte)value; michael@0: if (shift == 4) michael@0: { michael@0: shift = 0; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: shift += 4; michael@0: michael@0: dp--; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: row_info->bit_depth = 8; michael@0: row_info->pixel_depth = 8; michael@0: row_info->rowbytes = row_width; michael@0: } michael@0: michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: { michael@0: if (num_trans > 0) michael@0: { michael@0: sp = row + (png_size_t)row_width - 1; michael@0: dp = row + (png_size_t)(row_width << 2) - 1; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if ((int)(*sp) >= num_trans) michael@0: *dp-- = 0xff; michael@0: michael@0: else michael@0: *dp-- = trans_alpha[*sp]; michael@0: michael@0: *dp-- = palette[*sp].blue; michael@0: *dp-- = palette[*sp].green; michael@0: *dp-- = palette[*sp].red; michael@0: sp--; michael@0: } michael@0: row_info->bit_depth = 8; michael@0: row_info->pixel_depth = 32; michael@0: row_info->rowbytes = row_width * 4; michael@0: row_info->color_type = 6; michael@0: row_info->channels = 4; michael@0: } michael@0: michael@0: else michael@0: { michael@0: sp = row + (png_size_t)row_width - 1; michael@0: dp = row + (png_size_t)(row_width * 3) - 1; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: *dp-- = palette[*sp].blue; michael@0: *dp-- = palette[*sp].green; michael@0: *dp-- = palette[*sp].red; michael@0: sp--; michael@0: } michael@0: michael@0: row_info->bit_depth = 8; michael@0: row_info->pixel_depth = 24; michael@0: row_info->rowbytes = row_width * 3; michael@0: row_info->color_type = 2; michael@0: row_info->channels = 3; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* If the bit depth < 8, it is expanded to 8. Also, if the already michael@0: * expanded transparency value is supplied, an alpha channel is built. michael@0: */ michael@0: static void michael@0: png_do_expand(png_row_infop row_info, png_bytep row, michael@0: png_const_color_16p trans_color) michael@0: { michael@0: int shift, value; michael@0: png_bytep sp, dp; michael@0: png_uint_32 i; michael@0: png_uint_32 row_width=row_info->width; michael@0: michael@0: png_debug(1, "in png_do_expand"); michael@0: michael@0: { michael@0: if (row_info->color_type == PNG_COLOR_TYPE_GRAY) michael@0: { michael@0: unsigned int gray = trans_color ? trans_color->gray : 0; michael@0: michael@0: if (row_info->bit_depth < 8) michael@0: { michael@0: switch (row_info->bit_depth) michael@0: { michael@0: case 1: michael@0: { michael@0: gray = (gray & 0x01) * 0xff; michael@0: sp = row + (png_size_t)((row_width - 1) >> 3); michael@0: dp = row + (png_size_t)row_width - 1; michael@0: shift = 7 - (int)((row_width + 7) & 0x07); michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if ((*sp >> shift) & 0x01) michael@0: *dp = 0xff; michael@0: michael@0: else michael@0: *dp = 0; michael@0: michael@0: if (shift == 7) michael@0: { michael@0: shift = 0; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: shift++; michael@0: michael@0: dp--; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 2: michael@0: { michael@0: gray = (gray & 0x03) * 0x55; michael@0: sp = row + (png_size_t)((row_width - 1) >> 2); michael@0: dp = row + (png_size_t)row_width - 1; michael@0: shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: value = (*sp >> shift) & 0x03; michael@0: *dp = (png_byte)(value | (value << 2) | (value << 4) | michael@0: (value << 6)); michael@0: if (shift == 6) michael@0: { michael@0: shift = 0; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: shift += 2; michael@0: michael@0: dp--; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: case 4: michael@0: { michael@0: gray = (gray & 0x0f) * 0x11; michael@0: sp = row + (png_size_t)((row_width - 1) >> 1); michael@0: dp = row + (png_size_t)row_width - 1; michael@0: shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: value = (*sp >> shift) & 0x0f; michael@0: *dp = (png_byte)(value | (value << 4)); michael@0: if (shift == 4) michael@0: { michael@0: shift = 0; michael@0: sp--; michael@0: } michael@0: michael@0: else michael@0: shift = 4; michael@0: michael@0: dp--; michael@0: } michael@0: break; michael@0: } michael@0: michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: row_info->bit_depth = 8; michael@0: row_info->pixel_depth = 8; michael@0: row_info->rowbytes = row_width; michael@0: } michael@0: michael@0: if (trans_color != NULL) michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: gray = gray & 0xff; michael@0: sp = row + (png_size_t)row_width - 1; michael@0: dp = row + (png_size_t)(row_width << 1) - 1; michael@0: michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if (*sp == gray) michael@0: *dp-- = 0; michael@0: michael@0: else michael@0: *dp-- = 0xff; michael@0: michael@0: *dp-- = *sp--; michael@0: } michael@0: } michael@0: michael@0: else if (row_info->bit_depth == 16) michael@0: { michael@0: unsigned int gray_high = (gray >> 8) & 0xff; michael@0: unsigned int gray_low = gray & 0xff; michael@0: sp = row + row_info->rowbytes - 1; michael@0: dp = row + (row_info->rowbytes << 1) - 1; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if (*(sp - 1) == gray_high && *(sp) == gray_low) michael@0: { michael@0: *dp-- = 0; michael@0: *dp-- = 0; michael@0: } michael@0: michael@0: else michael@0: { michael@0: *dp-- = 0xff; michael@0: *dp-- = 0xff; michael@0: } michael@0: michael@0: *dp-- = *sp--; michael@0: *dp-- = *sp--; michael@0: } michael@0: } michael@0: michael@0: row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; michael@0: row_info->channels = 2; michael@0: row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); michael@0: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, michael@0: row_width); michael@0: } michael@0: } michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_color) michael@0: { michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: png_byte red = (png_byte)(trans_color->red & 0xff); michael@0: png_byte green = (png_byte)(trans_color->green & 0xff); michael@0: png_byte blue = (png_byte)(trans_color->blue & 0xff); michael@0: sp = row + (png_size_t)row_info->rowbytes - 1; michael@0: dp = row + (png_size_t)(row_width << 2) - 1; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue) michael@0: *dp-- = 0; michael@0: michael@0: else michael@0: *dp-- = 0xff; michael@0: michael@0: *dp-- = *sp--; michael@0: *dp-- = *sp--; michael@0: *dp-- = *sp--; michael@0: } michael@0: } michael@0: else if (row_info->bit_depth == 16) michael@0: { michael@0: png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff); michael@0: png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff); michael@0: png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff); michael@0: png_byte red_low = (png_byte)(trans_color->red & 0xff); michael@0: png_byte green_low = (png_byte)(trans_color->green & 0xff); michael@0: png_byte blue_low = (png_byte)(trans_color->blue & 0xff); michael@0: sp = row + row_info->rowbytes - 1; michael@0: dp = row + (png_size_t)(row_width << 3) - 1; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: if (*(sp - 5) == red_high && michael@0: *(sp - 4) == red_low && michael@0: *(sp - 3) == green_high && michael@0: *(sp - 2) == green_low && michael@0: *(sp - 1) == blue_high && michael@0: *(sp ) == blue_low) michael@0: { michael@0: *dp-- = 0; michael@0: *dp-- = 0; michael@0: } michael@0: michael@0: else michael@0: { michael@0: *dp-- = 0xff; michael@0: *dp-- = 0xff; michael@0: } michael@0: michael@0: *dp-- = *sp--; michael@0: *dp-- = *sp--; michael@0: *dp-- = *sp--; michael@0: *dp-- = *sp--; michael@0: *dp-- = *sp--; michael@0: *dp-- = *sp--; michael@0: } michael@0: } michael@0: row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA; michael@0: row_info->channels = 4; michael@0: row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); michael@0: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_EXPAND_16_SUPPORTED michael@0: /* If the bit depth is 8 and the color type is not a palette type expand the michael@0: * whole row to 16 bits. Has no effect otherwise. michael@0: */ michael@0: static void michael@0: png_do_expand_16(png_row_infop row_info, png_bytep row) michael@0: { michael@0: if (row_info->bit_depth == 8 && michael@0: row_info->color_type != PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: /* The row have a sequence of bytes containing [0..255] and we need michael@0: * to turn it into another row containing [0..65535], to do this we michael@0: * calculate: michael@0: * michael@0: * (input / 255) * 65535 michael@0: * michael@0: * Which happens to be exactly input * 257 and this can be achieved michael@0: * simply by byte replication in place (copying backwards). michael@0: */ michael@0: png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */ michael@0: png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */ michael@0: while (dp > sp) michael@0: dp[-2] = dp[-1] = *--sp, dp -= 2; michael@0: michael@0: row_info->rowbytes *= 2; michael@0: row_info->bit_depth = 16; michael@0: row_info->pixel_depth = (png_byte)(row_info->channels * 16); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_QUANTIZE_SUPPORTED michael@0: static void michael@0: png_do_quantize(png_row_infop row_info, png_bytep row, michael@0: png_const_bytep palette_lookup, png_const_bytep quantize_lookup) michael@0: { michael@0: png_bytep sp, dp; michael@0: png_uint_32 i; michael@0: png_uint_32 row_width=row_info->width; michael@0: michael@0: png_debug(1, "in png_do_quantize"); michael@0: michael@0: if (row_info->bit_depth == 8) michael@0: { michael@0: if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup) michael@0: { michael@0: int r, g, b, p; michael@0: sp = row; michael@0: dp = row; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: r = *sp++; michael@0: g = *sp++; michael@0: b = *sp++; michael@0: michael@0: /* This looks real messy, but the compiler will reduce michael@0: * it down to a reasonable formula. For example, with michael@0: * 5 bits per color, we get: michael@0: * p = (((r >> 3) & 0x1f) << 10) | michael@0: * (((g >> 3) & 0x1f) << 5) | michael@0: * ((b >> 3) & 0x1f); michael@0: */ michael@0: p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) & michael@0: ((1 << PNG_QUANTIZE_RED_BITS) - 1)) << michael@0: (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) | michael@0: (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) & michael@0: ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) << michael@0: (PNG_QUANTIZE_BLUE_BITS)) | michael@0: ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) & michael@0: ((1 << PNG_QUANTIZE_BLUE_BITS) - 1)); michael@0: michael@0: *dp++ = palette_lookup[p]; michael@0: } michael@0: michael@0: row_info->color_type = PNG_COLOR_TYPE_PALETTE; michael@0: row_info->channels = 1; michael@0: row_info->pixel_depth = row_info->bit_depth; michael@0: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); michael@0: } michael@0: michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && michael@0: palette_lookup != NULL) michael@0: { michael@0: int r, g, b, p; michael@0: sp = row; michael@0: dp = row; michael@0: for (i = 0; i < row_width; i++) michael@0: { michael@0: r = *sp++; michael@0: g = *sp++; michael@0: b = *sp++; michael@0: sp++; michael@0: michael@0: p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) & michael@0: ((1 << PNG_QUANTIZE_RED_BITS) - 1)) << michael@0: (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) | michael@0: (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) & michael@0: ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) << michael@0: (PNG_QUANTIZE_BLUE_BITS)) | michael@0: ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) & michael@0: ((1 << PNG_QUANTIZE_BLUE_BITS) - 1)); michael@0: michael@0: *dp++ = palette_lookup[p]; michael@0: } michael@0: michael@0: row_info->color_type = PNG_COLOR_TYPE_PALETTE; michael@0: row_info->channels = 1; michael@0: row_info->pixel_depth = row_info->bit_depth; michael@0: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); michael@0: } michael@0: michael@0: else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && michael@0: quantize_lookup) michael@0: { michael@0: sp = row; michael@0: michael@0: for (i = 0; i < row_width; i++, sp++) michael@0: { michael@0: *sp = quantize_lookup[*sp]; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif /* PNG_READ_QUANTIZE_SUPPORTED */ michael@0: michael@0: /* Transform the row. The order of transformations is significant, michael@0: * and is very touchy. If you add a transformation, take care to michael@0: * decide how it fits in with the other transformations here. michael@0: */ michael@0: void /* PRIVATE */ michael@0: png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info) michael@0: { michael@0: png_debug(1, "in png_do_read_transformations"); michael@0: michael@0: if (png_ptr->row_buf == NULL) michael@0: { michael@0: /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this michael@0: * error is incredibly rare and incredibly easy to debug without this michael@0: * information. michael@0: */ michael@0: png_error(png_ptr, "NULL row buffer"); michael@0: } michael@0: michael@0: /* The following is debugging; prior to 1.5.4 the code was never compiled in; michael@0: * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro michael@0: * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for michael@0: * all transformations, however in practice the ROW_INIT always gets done on michael@0: * demand, if necessary. michael@0: */ michael@0: if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 && michael@0: !(png_ptr->flags & PNG_FLAG_ROW_INIT)) michael@0: { michael@0: /* Application has failed to call either png_read_start_image() or michael@0: * png_read_update_info() after setting transforms that expand pixels. michael@0: * This check added to libpng-1.2.19 (but not enabled until 1.5.4). michael@0: */ michael@0: png_error(png_ptr, "Uninitialized row"); michael@0: } michael@0: michael@0: #ifdef PNG_READ_EXPAND_SUPPORTED michael@0: if (png_ptr->transformations & PNG_EXPAND) michael@0: { michael@0: if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) michael@0: { michael@0: png_do_expand_palette(row_info, png_ptr->row_buf + 1, michael@0: png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans); michael@0: } michael@0: michael@0: else michael@0: { michael@0: if (png_ptr->num_trans && michael@0: (png_ptr->transformations & PNG_EXPAND_tRNS)) michael@0: png_do_expand(row_info, png_ptr->row_buf + 1, michael@0: &(png_ptr->trans_color)); michael@0: michael@0: else michael@0: png_do_expand(row_info, png_ptr->row_buf + 1, michael@0: NULL); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED michael@0: if ((png_ptr->transformations & PNG_STRIP_ALPHA) && michael@0: !(png_ptr->transformations & PNG_COMPOSE) && michael@0: (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || michael@0: row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) michael@0: png_do_strip_channel(row_info, png_ptr->row_buf + 1, michael@0: 0 /* at_start == false, because SWAP_ALPHA happens later */); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED michael@0: if (png_ptr->transformations & PNG_RGB_TO_GRAY) michael@0: { michael@0: int rgb_error = michael@0: png_do_rgb_to_gray(png_ptr, row_info, michael@0: png_ptr->row_buf + 1); michael@0: michael@0: if (rgb_error) michael@0: { michael@0: png_ptr->rgb_to_gray_status=1; michael@0: if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == michael@0: PNG_RGB_TO_GRAY_WARN) michael@0: png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); michael@0: michael@0: if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == michael@0: PNG_RGB_TO_GRAY_ERR) michael@0: png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: /* From Andreas Dilger e-mail to png-implement, 26 March 1998: michael@0: * michael@0: * In most cases, the "simple transparency" should be done prior to doing michael@0: * gray-to-RGB, or you will have to test 3x as many bytes to check if a michael@0: * pixel is transparent. You would also need to make sure that the michael@0: * transparency information is upgraded to RGB. michael@0: * michael@0: * To summarize, the current flow is: michael@0: * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite michael@0: * with background "in place" if transparent, michael@0: * convert to RGB if necessary michael@0: * - Gray + alpha -> composite with gray background and remove alpha bytes, michael@0: * convert to RGB if necessary michael@0: * michael@0: * To support RGB backgrounds for gray images we need: michael@0: * - Gray + simple transparency -> convert to RGB + simple transparency, michael@0: * compare 3 or 6 bytes and composite with michael@0: * background "in place" if transparent michael@0: * (3x compare/pixel compared to doing michael@0: * composite with gray bkgrnd) michael@0: * - Gray + alpha -> convert to RGB + alpha, composite with background and michael@0: * remove alpha bytes (3x float michael@0: * operations/pixel compared with composite michael@0: * on gray background) michael@0: * michael@0: * Greg's change will do this. The reason it wasn't done before is for michael@0: * performance, as this increases the per-pixel operations. If we would check michael@0: * in advance if the background was gray or RGB, and position the gray-to-RGB michael@0: * transform appropriately, then it would save a lot of work/time. michael@0: */ michael@0: michael@0: #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED michael@0: /* If gray -> RGB, do so now only if background is non-gray; else do later michael@0: * for performance reasons michael@0: */ michael@0: if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && michael@0: !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) michael@0: png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ michael@0: defined(PNG_READ_ALPHA_MODE_SUPPORTED) michael@0: if (png_ptr->transformations & PNG_COMPOSE) michael@0: png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_GAMMA_SUPPORTED michael@0: if ((png_ptr->transformations & PNG_GAMMA) && michael@0: #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED michael@0: /* Because RGB_TO_GRAY does the gamma transform. */ michael@0: !(png_ptr->transformations & PNG_RGB_TO_GRAY) && michael@0: #endif michael@0: #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ michael@0: defined(PNG_READ_ALPHA_MODE_SUPPORTED) michael@0: /* Because PNG_COMPOSE does the gamma transform if there is something to michael@0: * do (if there is an alpha channel or transparency.) michael@0: */ michael@0: !((png_ptr->transformations & PNG_COMPOSE) && michael@0: ((png_ptr->num_trans != 0) || michael@0: (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) && michael@0: #endif michael@0: /* Because png_init_read_transformations transforms the palette, unless michael@0: * RGB_TO_GRAY will do the transform. michael@0: */ michael@0: (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) michael@0: png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED michael@0: if ((png_ptr->transformations & PNG_STRIP_ALPHA) && michael@0: (png_ptr->transformations & PNG_COMPOSE) && michael@0: (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || michael@0: row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) michael@0: png_do_strip_channel(row_info, png_ptr->row_buf + 1, michael@0: 0 /* at_start == false, because SWAP_ALPHA happens later */); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_ALPHA_MODE_SUPPORTED michael@0: if ((png_ptr->transformations & PNG_ENCODE_ALPHA) && michael@0: (row_info->color_type & PNG_COLOR_MASK_ALPHA)) michael@0: png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED michael@0: if (png_ptr->transformations & PNG_SCALE_16_TO_8) michael@0: png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED michael@0: /* There is no harm in doing both of these because only one has any effect, michael@0: * by putting the 'scale' option first if the app asks for scale (either by michael@0: * calling the API or in a TRANSFORM flag) this is what happens. michael@0: */ michael@0: if (png_ptr->transformations & PNG_16_TO_8) michael@0: png_do_chop(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_QUANTIZE_SUPPORTED michael@0: if (png_ptr->transformations & PNG_QUANTIZE) michael@0: { michael@0: png_do_quantize(row_info, png_ptr->row_buf + 1, michael@0: png_ptr->palette_lookup, png_ptr->quantize_index); michael@0: michael@0: if (row_info->rowbytes == 0) michael@0: png_error(png_ptr, "png_do_quantize returned rowbytes=0"); michael@0: } michael@0: #endif /* PNG_READ_QUANTIZE_SUPPORTED */ michael@0: michael@0: #ifdef PNG_READ_EXPAND_16_SUPPORTED michael@0: /* Do the expansion now, after all the arithmetic has been done. Notice michael@0: * that previous transformations can handle the PNG_EXPAND_16 flag if this michael@0: * is efficient (particularly true in the case of gamma correction, where michael@0: * better accuracy results faster!) michael@0: */ michael@0: if (png_ptr->transformations & PNG_EXPAND_16) michael@0: png_do_expand_16(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED michael@0: /* NOTE: moved here in 1.5.4 (from much later in this list.) */ michael@0: if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && michael@0: (png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) michael@0: png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_INVERT_SUPPORTED michael@0: if (png_ptr->transformations & PNG_INVERT_MONO) michael@0: png_do_invert(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED michael@0: if (png_ptr->transformations & PNG_INVERT_ALPHA) michael@0: png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_SHIFT_SUPPORTED michael@0: if (png_ptr->transformations & PNG_SHIFT) michael@0: png_do_unshift(row_info, png_ptr->row_buf + 1, michael@0: &(png_ptr->shift)); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_PACK_SUPPORTED michael@0: if (png_ptr->transformations & PNG_PACK) michael@0: png_do_unpack(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED michael@0: /* Added at libpng-1.5.10 */ michael@0: if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && michael@0: png_ptr->num_palette_max >= 0) michael@0: png_do_check_palette_indexes(png_ptr, row_info); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_BGR_SUPPORTED michael@0: if (png_ptr->transformations & PNG_BGR) michael@0: png_do_bgr(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_PACKSWAP_SUPPORTED michael@0: if (png_ptr->transformations & PNG_PACKSWAP) michael@0: png_do_packswap(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_FILLER_SUPPORTED michael@0: if (png_ptr->transformations & PNG_FILLER) michael@0: png_do_read_filler(row_info, png_ptr->row_buf + 1, michael@0: (png_uint_32)png_ptr->filler, png_ptr->flags); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED michael@0: if (png_ptr->transformations & PNG_SWAP_ALPHA) michael@0: png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_16BIT_SUPPORTED michael@0: #ifdef PNG_READ_SWAP_SUPPORTED michael@0: if (png_ptr->transformations & PNG_SWAP_BYTES) michael@0: png_do_swap(row_info, png_ptr->row_buf + 1); michael@0: #endif michael@0: #endif michael@0: michael@0: #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED michael@0: if (png_ptr->transformations & PNG_USER_TRANSFORM) michael@0: { michael@0: if (png_ptr->read_user_transform_fn != NULL) michael@0: (*(png_ptr->read_user_transform_fn)) /* User read transform function */ michael@0: (png_ptr, /* png_ptr */ michael@0: row_info, /* row_info: */ michael@0: /* png_uint_32 width; width of row */ michael@0: /* png_size_t rowbytes; number of bytes in row */ michael@0: /* png_byte color_type; color type of pixels */ michael@0: /* png_byte bit_depth; bit depth of samples */ michael@0: /* png_byte channels; number of channels (1-4) */ michael@0: /* png_byte pixel_depth; bits per pixel (depth*channels) */ michael@0: png_ptr->row_buf + 1); /* start of pixel data for row */ michael@0: #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED michael@0: if (png_ptr->user_transform_depth) michael@0: row_info->bit_depth = png_ptr->user_transform_depth; michael@0: michael@0: if (png_ptr->user_transform_channels) michael@0: row_info->channels = png_ptr->user_transform_channels; michael@0: #endif michael@0: row_info->pixel_depth = (png_byte)(row_info->bit_depth * michael@0: row_info->channels); michael@0: michael@0: row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: #endif /* PNG_READ_TRANSFORMS_SUPPORTED */ michael@0: #endif /* PNG_READ_SUPPORTED */