| OLD | NEW |
| 1 | 1 |
| 2 /* pngwtran.c - transforms the data in a row for PNG writers | 2 /* pngwtran.c - transforms the data in a row for PNG writers |
| 3 * | 3 * |
| 4 * Last changed in libpng 1.2.37 [June 4, 2009] | 4 * Last changed in libpng 1.2.43 [February 25, 2010] |
| 5 * For conditions of distribution and use, see copyright notice in png.h | 5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson |
| 6 * Copyright (c) 1998-2009 Glenn Randers-Pehrson | |
| 7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) | 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) | 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
| 8 * |
| 9 * This code is released under the libpng license. |
| 10 * For conditions of distribution and use, see the disclaimer |
| 11 * and license in png.h |
| 9 */ | 12 */ |
| 10 | 13 |
| 11 #define PNG_INTERNAL | 14 #define PNG_INTERNAL |
| 15 #define PNG_NO_PEDANTIC_WARNINGS |
| 12 #include "png.h" | 16 #include "png.h" |
| 13 #ifdef PNG_WRITE_SUPPORTED | 17 #ifdef PNG_WRITE_SUPPORTED |
| 14 | 18 |
| 15 /* Transform the data according to the user's wishes. The order of | 19 /* Transform the data according to the user's wishes. The order of |
| 16 * transformations is significant. | 20 * transformations is significant. |
| 17 */ | 21 */ |
| 18 void /* PRIVATE */ | 22 void /* PRIVATE */ |
| 19 png_do_write_transformations(png_structp png_ptr) | 23 png_do_write_transformations(png_structp png_ptr) |
| 20 { | 24 { |
| 21 png_debug(1, "in png_do_write_transformations"); | 25 png_debug(1, "in png_do_write_transformations"); |
| 22 | 26 |
| 23 if (png_ptr == NULL) | 27 if (png_ptr == NULL) |
| 24 return; | 28 return; |
| 25 | 29 |
| 26 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) | 30 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED |
| 27 if (png_ptr->transformations & PNG_USER_TRANSFORM) | 31 if (png_ptr->transformations & PNG_USER_TRANSFORM) |
| 28 if (png_ptr->write_user_transform_fn != NULL) | 32 if (png_ptr->write_user_transform_fn != NULL) |
| 29 (*(png_ptr->write_user_transform_fn)) /* User write transform function *
/ | 33 (*(png_ptr->write_user_transform_fn)) /* User write transform |
| 34 function */ |
| 30 (png_ptr, /* png_ptr */ | 35 (png_ptr, /* png_ptr */ |
| 31 &(png_ptr->row_info), /* row_info: */ | 36 &(png_ptr->row_info), /* row_info: */ |
| 32 /* png_uint_32 width; width of row */ | 37 /* png_uint_32 width; width of row */ |
| 33 /* png_uint_32 rowbytes; number of bytes in row */ | 38 /* png_uint_32 rowbytes; number of bytes in row */ |
| 34 /* png_byte color_type; color type of pixels */ | 39 /* png_byte color_type; color type of pixels */ |
| 35 /* png_byte bit_depth; bit depth of samples */ | 40 /* png_byte bit_depth; bit depth of samples */ |
| 36 /* png_byte channels; number of channels (1-4) */ | 41 /* png_byte channels; number of channels (1-4) */ |
| 37 /* png_byte pixel_depth; bits per pixel (depth*channels) */ | 42 /* png_byte pixel_depth; bits per pixel (depth*channels) */ |
| 38 png_ptr->row_buf + 1); /* start of pixel data for row */ | 43 png_ptr->row_buf + 1); /* start of pixel data for row */ |
| 39 #endif | 44 #endif |
| 40 #if defined(PNG_WRITE_FILLER_SUPPORTED) | 45 #ifdef PNG_WRITE_FILLER_SUPPORTED |
| 41 if (png_ptr->transformations & PNG_FILLER) | 46 if (png_ptr->transformations & PNG_FILLER) |
| 42 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, | 47 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 43 png_ptr->flags); | 48 png_ptr->flags); |
| 44 #endif | 49 #endif |
| 45 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) | 50 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED |
| 46 if (png_ptr->transformations & PNG_PACKSWAP) | 51 if (png_ptr->transformations & PNG_PACKSWAP) |
| 47 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); | 52 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 48 #endif | 53 #endif |
| 49 #if defined(PNG_WRITE_PACK_SUPPORTED) | 54 #ifdef PNG_WRITE_PACK_SUPPORTED |
| 50 if (png_ptr->transformations & PNG_PACK) | 55 if (png_ptr->transformations & PNG_PACK) |
| 51 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1, | 56 png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 52 (png_uint_32)png_ptr->bit_depth); | 57 (png_uint_32)png_ptr->bit_depth); |
| 53 #endif | 58 #endif |
| 54 #if defined(PNG_WRITE_SWAP_SUPPORTED) | 59 #ifdef PNG_WRITE_SWAP_SUPPORTED |
| 55 if (png_ptr->transformations & PNG_SWAP_BYTES) | 60 if (png_ptr->transformations & PNG_SWAP_BYTES) |
| 56 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); | 61 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 57 #endif | 62 #endif |
| 58 #if defined(PNG_WRITE_SHIFT_SUPPORTED) | 63 #ifdef PNG_WRITE_SHIFT_SUPPORTED |
| 59 if (png_ptr->transformations & PNG_SHIFT) | 64 if (png_ptr->transformations & PNG_SHIFT) |
| 60 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1, | 65 png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1, |
| 61 &(png_ptr->shift)); | 66 &(png_ptr->shift)); |
| 62 #endif | 67 #endif |
| 63 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) | 68 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED |
| 64 if (png_ptr->transformations & PNG_SWAP_ALPHA) | 69 if (png_ptr->transformations & PNG_SWAP_ALPHA) |
| 65 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); | 70 png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 66 #endif | 71 #endif |
| 67 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) | 72 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
| 68 if (png_ptr->transformations & PNG_INVERT_ALPHA) | 73 if (png_ptr->transformations & PNG_INVERT_ALPHA) |
| 69 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); | 74 png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 70 #endif | 75 #endif |
| 71 #if defined(PNG_WRITE_BGR_SUPPORTED) | 76 #ifdef PNG_WRITE_BGR_SUPPORTED |
| 72 if (png_ptr->transformations & PNG_BGR) | 77 if (png_ptr->transformations & PNG_BGR) |
| 73 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); | 78 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 74 #endif | 79 #endif |
| 75 #if defined(PNG_WRITE_INVERT_SUPPORTED) | 80 #ifdef PNG_WRITE_INVERT_SUPPORTED |
| 76 if (png_ptr->transformations & PNG_INVERT_MONO) | 81 if (png_ptr->transformations & PNG_INVERT_MONO) |
| 77 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); | 82 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1); |
| 78 #endif | 83 #endif |
| 79 } | 84 } |
| 80 | 85 |
| 81 #if defined(PNG_WRITE_PACK_SUPPORTED) | 86 #ifdef PNG_WRITE_PACK_SUPPORTED |
| 82 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The | 87 /* Pack pixels into bytes. Pass the true bit depth in bit_depth. The |
| 83 * row_info bit depth should be 8 (one pixel per byte). The channels | 88 * row_info bit depth should be 8 (one pixel per byte). The channels |
| 84 * should be 1 (this only happens on grayscale and paletted images). | 89 * should be 1 (this only happens on grayscale and paletted images). |
| 85 */ | 90 */ |
| 86 void /* PRIVATE */ | 91 void /* PRIVATE */ |
| 87 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) | 92 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) |
| 88 { | 93 { |
| 89 png_debug(1, "in png_do_pack"); | 94 png_debug(1, "in png_do_pack"); |
| 95 |
| 90 if (row_info->bit_depth == 8 && | 96 if (row_info->bit_depth == 8 && |
| 91 #if defined(PNG_USELESS_TESTS_SUPPORTED) | 97 #ifdef PNG_USELESS_TESTS_SUPPORTED |
| 92 row != NULL && row_info != NULL && | 98 row != NULL && row_info != NULL && |
| 93 #endif | 99 #endif |
| 94 row_info->channels == 1) | 100 row_info->channels == 1) |
| 95 { | 101 { |
| 96 switch ((int)bit_depth) | 102 switch ((int)bit_depth) |
| 97 { | 103 { |
| 98 case 1: | 104 case 1: |
| 99 { | 105 { |
| 100 png_bytep sp, dp; | 106 png_bytep sp, dp; |
| 101 int mask, v; | 107 int mask, v; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 } | 200 } |
| 195 } | 201 } |
| 196 row_info->bit_depth = (png_byte)bit_depth; | 202 row_info->bit_depth = (png_byte)bit_depth; |
| 197 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels); | 203 row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels); |
| 198 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, | 204 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, |
| 199 row_info->width); | 205 row_info->width); |
| 200 } | 206 } |
| 201 } | 207 } |
| 202 #endif | 208 #endif |
| 203 | 209 |
| 204 #if defined(PNG_WRITE_SHIFT_SUPPORTED) | 210 #ifdef PNG_WRITE_SHIFT_SUPPORTED |
| 205 /* Shift pixel values to take advantage of whole range. Pass the | 211 /* Shift pixel values to take advantage of whole range. Pass the |
| 206 * true number of bits in bit_depth. The row should be packed | 212 * true number of bits in bit_depth. The row should be packed |
| 207 * according to row_info->bit_depth. Thus, if you had a row of | 213 * according to row_info->bit_depth. Thus, if you had a row of |
| 208 * bit depth 4, but the pixels only had values from 0 to 7, you | 214 * bit depth 4, but the pixels only had values from 0 to 7, you |
| 209 * would pass 3 as bit_depth, and this routine would translate the | 215 * would pass 3 as bit_depth, and this routine would translate the |
| 210 * data to 0 to 15. | 216 * data to 0 to 15. |
| 211 */ | 217 */ |
| 212 void /* PRIVATE */ | 218 void /* PRIVATE */ |
| 213 png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth) | 219 png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth) |
| 214 { | 220 { |
| 215 png_debug(1, "in png_do_shift"); | 221 png_debug(1, "in png_do_shift"); |
| 216 #if defined(PNG_USELESS_TESTS_SUPPORTED) | 222 |
| 223 #ifdef PNG_USELESS_TESTS_SUPPORTED |
| 217 if (row != NULL && row_info != NULL && | 224 if (row != NULL && row_info != NULL && |
| 218 #else | 225 #else |
| 219 if ( | 226 if ( |
| 220 #endif | 227 #endif |
| 221 row_info->color_type != PNG_COLOR_TYPE_PALETTE) | 228 row_info->color_type != PNG_COLOR_TYPE_PALETTE) |
| 222 { | 229 { |
| 223 int shift_start[4], shift_dec[4]; | 230 int shift_start[4], shift_dec[4]; |
| 224 int channels = 0; | 231 int channels = 0; |
| 225 | 232 |
| 226 if (row_info->color_type & PNG_COLOR_MASK_COLOR) | 233 if (row_info->color_type & PNG_COLOR_MASK_COLOR) |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff); | 332 value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff); |
| 326 } | 333 } |
| 327 *bp++ = (png_byte)(value >> 8); | 334 *bp++ = (png_byte)(value >> 8); |
| 328 *bp++ = (png_byte)(value & 0xff); | 335 *bp++ = (png_byte)(value & 0xff); |
| 329 } | 336 } |
| 330 } | 337 } |
| 331 } | 338 } |
| 332 } | 339 } |
| 333 #endif | 340 #endif |
| 334 | 341 |
| 335 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) | 342 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED |
| 336 void /* PRIVATE */ | 343 void /* PRIVATE */ |
| 337 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) | 344 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) |
| 338 { | 345 { |
| 339 png_debug(1, "in png_do_write_swap_alpha"); | 346 png_debug(1, "in png_do_write_swap_alpha"); |
| 340 #if defined(PNG_USELESS_TESTS_SUPPORTED) | 347 |
| 348 #ifdef PNG_USELESS_TESTS_SUPPORTED |
| 341 if (row != NULL && row_info != NULL) | 349 if (row != NULL && row_info != NULL) |
| 342 #endif | 350 #endif |
| 343 { | 351 { |
| 344 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | 352 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 345 { | 353 { |
| 346 /* This converts from ARGB to RGBA */ | 354 /* This converts from ARGB to RGBA */ |
| 347 if (row_info->bit_depth == 8) | 355 if (row_info->bit_depth == 8) |
| 348 { | 356 { |
| 349 png_bytep sp, dp; | 357 png_bytep sp, dp; |
| 350 png_uint_32 i; | 358 png_uint_32 i; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 *(dp++) = *(sp++); | 421 *(dp++) = *(sp++); |
| 414 *(dp++) = save[0]; | 422 *(dp++) = save[0]; |
| 415 *(dp++) = save[1]; | 423 *(dp++) = save[1]; |
| 416 } | 424 } |
| 417 } | 425 } |
| 418 } | 426 } |
| 419 } | 427 } |
| 420 } | 428 } |
| 421 #endif | 429 #endif |
| 422 | 430 |
| 423 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) | 431 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED |
| 424 void /* PRIVATE */ | 432 void /* PRIVATE */ |
| 425 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) | 433 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) |
| 426 { | 434 { |
| 427 png_debug(1, "in png_do_write_invert_alpha"); | 435 png_debug(1, "in png_do_write_invert_alpha"); |
| 428 #if defined(PNG_USELESS_TESTS_SUPPORTED) | 436 |
| 437 #ifdef PNG_USELESS_TESTS_SUPPORTED |
| 429 if (row != NULL && row_info != NULL) | 438 if (row != NULL && row_info != NULL) |
| 430 #endif | 439 #endif |
| 431 { | 440 { |
| 432 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) | 441 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 433 { | 442 { |
| 434 /* This inverts the alpha channel in RGBA */ | 443 /* This inverts the alpha channel in RGBA */ |
| 435 if (row_info->bit_depth == 8) | 444 if (row_info->bit_depth == 8) |
| 436 { | 445 { |
| 437 png_bytep sp, dp; | 446 png_bytep sp, dp; |
| 438 png_uint_32 i; | 447 png_uint_32 i; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 sp+=2; dp = sp; | 511 sp+=2; dp = sp; |
| 503 *(dp++) = (png_byte)(255 - *(sp++)); | 512 *(dp++) = (png_byte)(255 - *(sp++)); |
| 504 *(dp++) = (png_byte)(255 - *(sp++)); | 513 *(dp++) = (png_byte)(255 - *(sp++)); |
| 505 } | 514 } |
| 506 } | 515 } |
| 507 } | 516 } |
| 508 } | 517 } |
| 509 } | 518 } |
| 510 #endif | 519 #endif |
| 511 | 520 |
| 512 #if defined(PNG_MNG_FEATURES_SUPPORTED) | 521 #ifdef PNG_MNG_FEATURES_SUPPORTED |
| 513 /* Undoes intrapixel differencing */ | 522 /* Undoes intrapixel differencing */ |
| 514 void /* PRIVATE */ | 523 void /* PRIVATE */ |
| 515 png_do_write_intrapixel(png_row_infop row_info, png_bytep row) | 524 png_do_write_intrapixel(png_row_infop row_info, png_bytep row) |
| 516 { | 525 { |
| 517 png_debug(1, "in png_do_write_intrapixel"); | 526 png_debug(1, "in png_do_write_intrapixel"); |
| 527 |
| 518 if ( | 528 if ( |
| 519 #if defined(PNG_USELESS_TESTS_SUPPORTED) | 529 #ifdef PNG_USELESS_TESTS_SUPPORTED |
| 520 row != NULL && row_info != NULL && | 530 row != NULL && row_info != NULL && |
| 521 #endif | 531 #endif |
| 522 (row_info->color_type & PNG_COLOR_MASK_COLOR)) | 532 (row_info->color_type & PNG_COLOR_MASK_COLOR)) |
| 523 { | 533 { |
| 524 int bytes_per_pixel; | 534 int bytes_per_pixel; |
| 525 png_uint_32 row_width = row_info->width; | 535 png_uint_32 row_width = row_info->width; |
| 526 if (row_info->bit_depth == 8) | 536 if (row_info->bit_depth == 8) |
| 527 { | 537 { |
| 528 png_bytep rp; | 538 png_bytep rp; |
| 529 png_uint_32 i; | 539 png_uint_32 i; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 563 *(rp ) = (png_byte)((red >> 8) & 0xff); | 573 *(rp ) = (png_byte)((red >> 8) & 0xff); |
| 564 *(rp+1) = (png_byte)(red & 0xff); | 574 *(rp+1) = (png_byte)(red & 0xff); |
| 565 *(rp+4) = (png_byte)((blue >> 8) & 0xff); | 575 *(rp+4) = (png_byte)((blue >> 8) & 0xff); |
| 566 *(rp+5) = (png_byte)(blue & 0xff); | 576 *(rp+5) = (png_byte)(blue & 0xff); |
| 567 } | 577 } |
| 568 } | 578 } |
| 569 } | 579 } |
| 570 } | 580 } |
| 571 #endif /* PNG_MNG_FEATURES_SUPPORTED */ | 581 #endif /* PNG_MNG_FEATURES_SUPPORTED */ |
| 572 #endif /* PNG_WRITE_SUPPORTED */ | 582 #endif /* PNG_WRITE_SUPPORTED */ |
| OLD | NEW |