diff options
Diffstat (limited to 'png/pngtrans.c')
| -rw-r--r-- | png/pngtrans.c | 176 |
1 files changed, 111 insertions, 65 deletions
diff --git a/png/pngtrans.c b/png/pngtrans.c index 6e1870c59..137f555a6 100644 --- a/png/pngtrans.c +++ b/png/pngtrans.c @@ -1,8 +1,8 @@ /* pngtrans.c - transforms the data in a row (used by both readers and writers) * - * Last changed in libpng 1.2.36 [May 14, 2009] - * Copyright (c) 1998-2009 Glenn Randers-Pehrson + * Last changed in libpng 1.5.1 [February 3, 2011] + * Copyright (c) 1998-2011 Glenn Randers-Pehrson * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) * @@ -11,8 +11,8 @@ * and license in png.h */ -#define PNG_INTERNAL -#include "png.h" +#include "pngpriv.h" + #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) @@ -21,8 +21,10 @@ void PNGAPI png_set_bgr(png_structp png_ptr) { png_debug(1, "in png_set_bgr"); + if (png_ptr == NULL) return; + png_ptr->transformations |= PNG_BGR; } #endif @@ -33,8 +35,10 @@ void PNGAPI png_set_swap(png_structp png_ptr) { png_debug(1, "in png_set_swap"); + if (png_ptr == NULL) return; + if (png_ptr->bit_depth == 16) png_ptr->transformations |= PNG_SWAP_BYTES; } @@ -46,8 +50,10 @@ void PNGAPI png_set_packing(png_structp png_ptr) { png_debug(1, "in png_set_packing"); + if (png_ptr == NULL) return; + if (png_ptr->bit_depth < 8) { png_ptr->transformations |= PNG_PACK; @@ -62,8 +68,10 @@ void PNGAPI png_set_packswap(png_structp png_ptr) { png_debug(1, "in png_set_packswap"); + if (png_ptr == NULL) return; + if (png_ptr->bit_depth < 8) png_ptr->transformations |= PNG_PACKSWAP; } @@ -71,11 +79,13 @@ png_set_packswap(png_structp png_ptr) #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) void PNGAPI -png_set_shift(png_structp png_ptr, png_color_8p true_bits) +png_set_shift(png_structp png_ptr, png_const_color_8p true_bits) { png_debug(1, "in png_set_shift"); + if (png_ptr == NULL) return; + png_ptr->transformations |= PNG_SHIFT; png_ptr->shift = *true_bits; } @@ -87,6 +97,7 @@ int PNGAPI png_set_interlace_handling(png_structp png_ptr) { png_debug(1, "in png_set_interlace handling"); + if (png_ptr && png_ptr->interlaced) { png_ptr->transformations |= PNG_INTERLACE; @@ -107,12 +118,16 @@ void PNGAPI png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc) { png_debug(1, "in png_set_filler"); + if (png_ptr == NULL) return; + png_ptr->transformations |= PNG_FILLER; - png_ptr->filler = (png_byte)filler; + png_ptr->filler = (png_uint_16)filler; + if (filler_loc == PNG_FILLER_AFTER) png_ptr->flags |= PNG_FLAG_FILLER_AFTER; + else png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER; @@ -127,7 +142,7 @@ png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc) } /* Also I added this in libpng-1.0.2a (what happens when we expand - * a less-than-8-bit grayscale to GA? */ + * a less-than-8-bit grayscale to GA?) */ if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8) { @@ -135,18 +150,18 @@ png_set_filler(png_structp png_ptr, png_uint_32 filler, int filler_loc) } } -#if !defined(PNG_1_0_X) /* Added to libpng-1.2.7 */ void PNGAPI png_set_add_alpha(png_structp png_ptr, png_uint_32 filler, int filler_loc) { png_debug(1, "in png_set_add_alpha"); + if (png_ptr == NULL) return; + png_set_filler(png_ptr, filler, filler_loc); png_ptr->transformations |= PNG_ADD_ALPHA; } -#endif #endif @@ -156,8 +171,10 @@ void PNGAPI png_set_swap_alpha(png_structp png_ptr) { png_debug(1, "in png_set_swap_alpha"); + if (png_ptr == NULL) return; + png_ptr->transformations |= PNG_SWAP_ALPHA; } #endif @@ -168,8 +185,10 @@ void PNGAPI png_set_invert_alpha(png_structp png_ptr) { png_debug(1, "in png_set_invert_alpha"); + if (png_ptr == NULL) return; + png_ptr->transformations |= PNG_INVERT_ALPHA; } #endif @@ -179,8 +198,10 @@ void PNGAPI png_set_invert_mono(png_structp png_ptr) { png_debug(1, "in png_set_invert_mono"); + if (png_ptr == NULL) return; + png_ptr->transformations |= PNG_INVERT_MONO; } @@ -189,18 +210,15 @@ void /* PRIVATE */ png_do_invert(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_invert"); + /* This test removed from libpng version 1.0.13 and 1.2.0: * if (row_info->bit_depth == 1 && */ -#if defined(PNG_USELESS_TESTS_SUPPORTED) - if (row == NULL || row_info == NULL) - return; -#endif if (row_info->color_type == PNG_COLOR_TYPE_GRAY) { png_bytep rp = row; - png_uint_32 i; - png_uint_32 istop = row_info->rowbytes; + png_size_t i; + png_size_t istop = row_info->rowbytes; for (i = 0; i < istop; i++) { @@ -208,47 +226,49 @@ png_do_invert(png_row_infop row_info, png_bytep row) rp++; } } + else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && row_info->bit_depth == 8) { png_bytep rp = row; - png_uint_32 i; - png_uint_32 istop = row_info->rowbytes; + png_size_t i; + png_size_t istop = row_info->rowbytes; - for (i = 0; i < istop; i+=2) + for (i = 0; i < istop; i += 2) { *rp = (png_byte)(~(*rp)); - rp+=2; + rp += 2; } } + +#ifdef PNG_16BIT_SUPPORTED else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && row_info->bit_depth == 16) { png_bytep rp = row; - png_uint_32 i; - png_uint_32 istop = row_info->rowbytes; + png_size_t i; + png_size_t istop = row_info->rowbytes; - for (i = 0; i < istop; i+=4) + for (i = 0; i < istop; i += 4) { *rp = (png_byte)(~(*rp)); - *(rp+1) = (png_byte)(~(*(rp+1))); - rp+=4; + *(rp + 1) = (png_byte)(~(*(rp + 1))); + rp += 4; } } +#endif } #endif +#ifdef PNG_16BIT_SUPPORTED #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) /* Swaps byte order on 16 bit depth images */ void /* PRIVATE */ png_do_swap(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_swap"); - if ( -#if defined(PNG_USELESS_TESTS_SUPPORTED) - row != NULL && row_info != NULL && -#endif - row_info->bit_depth == 16) + + if (row_info->bit_depth == 16) { png_bytep rp = row; png_uint_32 i; @@ -263,6 +283,7 @@ png_do_swap(png_row_infop row_info, png_bytep row) } } #endif +#endif #if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED) static PNG_CONST png_byte onebppswaptable[256] = { @@ -375,22 +396,23 @@ void /* PRIVATE */ png_do_packswap(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_packswap"); - if ( -#if defined(PNG_USELESS_TESTS_SUPPORTED) - row != NULL && row_info != NULL && -#endif - row_info->bit_depth < 8) + + if (row_info->bit_depth < 8) { - png_bytep rp, end, table; + png_bytep rp; + png_const_bytep end, table; end = row + row_info->rowbytes; if (row_info->bit_depth == 1) - table = (png_bytep)onebppswaptable; + table = onebppswaptable; + else if (row_info->bit_depth == 2) - table = (png_bytep)twobppswaptable; + table = twobppswaptable; + else if (row_info->bit_depth == 4) - table = (png_bytep)fourbppswaptable; + table = fourbppswaptable; + else return; @@ -407,13 +429,11 @@ void /* PRIVATE */ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) { png_debug(1, "in png_do_strip_filler"); -#if defined(PNG_USELESS_TESTS_SUPPORTED) - if (row != NULL && row_info != NULL) -#endif + { - png_bytep sp=row; - png_bytep dp=row; - png_uint_32 row_width=row_info->width; + png_bytep sp = row; + png_bytep dp = row; + png_uint_32 row_width = row_info->width; png_uint_32 i; if ((row_info->color_type == PNG_COLOR_TYPE_RGB || @@ -426,7 +446,7 @@ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) /* This converts from RGBX or RGBA to RGB */ if (flags & PNG_FLAG_FILLER_AFTER) { - dp+=3; sp+=4; + dp += 3; sp += 4; for (i = 1; i < row_width; i++) { *dp++ = *sp++; @@ -435,6 +455,7 @@ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) sp++; } } + /* This converts from XRGB or ARGB to RGB */ else { @@ -449,6 +470,7 @@ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) row_info->pixel_depth = 24; row_info->rowbytes = row_width * 3; } + else /* if (row_info->bit_depth == 16) */ { if (flags & PNG_FLAG_FILLER_AFTER) @@ -472,6 +494,7 @@ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) sp += 2; } } + else { /* This converts from XXRRGGBB or AARRGGBB to RRGGBB */ @@ -483,7 +506,7 @@ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) dp += 6; */ - sp+=2; + sp += 2; *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp++; @@ -492,11 +515,13 @@ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) *dp++ = *sp++; } } + row_info->pixel_depth = 48; row_info->rowbytes = row_width * 6; } row_info->channels = 3; } + else if ((row_info->color_type == PNG_COLOR_TYPE_GRAY || (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && (flags & PNG_FLAG_STRIP_ALPHA))) && @@ -504,27 +529,30 @@ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) { if (row_info->bit_depth == 8) { - /* This converts from GX or GA to G */ if (flags & PNG_FLAG_FILLER_AFTER) { + /* This converts from GX or GA to G */ for (i = 0; i < row_width; i++) { *dp++ = *sp++; sp++; } } - /* This converts from XG or AG to G */ + else { + /* This converts from XG or AG to G */ for (i = 0; i < row_width; i++) { sp++; *dp++ = *sp++; } } + row_info->pixel_depth = 8; row_info->rowbytes = row_width; } + else /* if (row_info->bit_depth == 16) */ { if (flags & PNG_FLAG_FILLER_AFTER) @@ -538,6 +566,7 @@ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) sp += 2; } } + else { /* This converts from XXGG or AAGG to GG */ @@ -548,13 +577,16 @@ png_do_strip_filler(png_row_infop row_info, png_bytep row, png_uint_32 flags) *dp++ = *sp++; } } + row_info->pixel_depth = 16; row_info->rowbytes = row_width * 2; } row_info->channels = 1; } + if (flags & PNG_FLAG_STRIP_ALPHA) - row_info->color_type &= ~PNG_COLOR_MASK_ALPHA; + row_info->color_type = (png_byte)(row_info->color_type & + ~PNG_COLOR_MASK_ALPHA); } } #endif @@ -565,11 +597,8 @@ void /* PRIVATE */ png_do_bgr(png_row_infop row_info, png_bytep row) { png_debug(1, "in png_do_bgr"); - if ( -#if defined(PNG_USELESS_TESTS_SUPPORTED) - row != NULL && row_info != NULL && -#endif - (row_info->color_type & PNG_COLOR_MASK_COLOR)) + + if ((row_info->color_type & PNG_COLOR_MASK_COLOR)) { png_uint_32 row_width = row_info->width; if (row_info->bit_depth == 8) @@ -586,6 +615,7 @@ png_do_bgr(png_row_infop row_info, png_bytep row) *(rp + 2) = save; } } + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) { png_bytep rp; @@ -599,6 +629,8 @@ png_do_bgr(png_row_infop row_info, png_bytep row) } } } + +#ifdef PNG_16BIT_SUPPORTED else if (row_info->bit_depth == 16) { if (row_info->color_type == PNG_COLOR_TYPE_RGB) @@ -616,6 +648,7 @@ png_do_bgr(png_row_infop row_info, png_bytep row) *(rp + 5) = save; } } + else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) { png_bytep rp; @@ -632,29 +665,25 @@ png_do_bgr(png_row_infop row_info, png_bytep row) } } } +#endif } } #endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */ #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ - defined(PNG_LEGACY_SUPPORTED) || \ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) +#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED void PNGAPI png_set_user_transform_info(png_structp png_ptr, png_voidp user_transform_ptr, int user_transform_depth, int user_transform_channels) { png_debug(1, "in png_set_user_transform_info"); + if (png_ptr == NULL) return; -#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) png_ptr->user_transform_ptr = user_transform_ptr; png_ptr->user_transform_depth = (png_byte)user_transform_depth; png_ptr->user_transform_channels = (png_byte)user_transform_channels; -#else - if (user_transform_ptr || user_transform_depth || user_transform_channels) - png_warning(png_ptr, - "This version of libpng does not support user transform info"); -#endif } #endif @@ -663,15 +692,32 @@ png_set_user_transform_info(png_structp png_ptr, png_voidp * associated with this pointer before png_write_destroy and png_read_destroy * are called. */ +#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED png_voidp PNGAPI -png_get_user_transform_ptr(png_structp png_ptr) +png_get_user_transform_ptr(png_const_structp png_ptr) { if (png_ptr == NULL) return (NULL); -#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) + return ((png_voidp)png_ptr->user_transform_ptr); -#else - return (NULL); +} #endif + +png_uint_32 PNGAPI +png_get_current_row_number(png_const_structp png_ptr) +{ + if (png_ptr != NULL) + return png_ptr->row_number; + return PNG_UINT_32_MAX; /* help the app not to fail silently */ +} + +png_byte PNGAPI +png_get_current_pass_number(png_const_structp png_ptr) +{ + if (png_ptr != NULL) + return png_ptr->pass; + return 8; /* invalid */ } +#endif /* PNG_READ_USER_TRANSFORM_SUPPORTED || + PNG_WRITE_USER_TRANSFORM_SUPPORTED */ #endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */ |
