Changeset 561 for trunk/src/3rdparty/libpng/pngrtran.c
- Timestamp:
- Feb 11, 2010, 11:19:06 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
-
Property svn:mergeinfo
set to (toggle deleted branches)
/branches/vendor/nokia/qt/4.6.1 merged eligible /branches/vendor/nokia/qt/current merged eligible /branches/vendor/trolltech/qt/current 3-149
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/3rdparty/libpng/pngrtran.c
r2 r561 2 2 /* pngrtran.c - transforms the data in a row for PNG readers 3 3 * 4 * Last changed in libpng 1.2.27 [April 29, 2008] 5 * For conditions of distribution and use, see copyright notice in png.h 6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson 4 * Last changed in libpng 1.2.38 [July 16, 2009] 5 * Copyright (c) 1998-2009 Glenn Randers-Pehrson 7 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 8 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 * This file contains functions optionally called by an application … … 16 19 #define PNG_INTERNAL 17 20 #include "png.h" 18 19 21 #if defined(PNG_READ_SUPPORTED) 20 22 … … 23 25 png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action) 24 26 { 25 png_debug(1, "in png_set_crc_action \n");27 png_debug(1, "in png_set_crc_action"); 26 28 /* Tell libpng how we react to CRC errors in critical chunks */ 27 if(png_ptr == NULL) return; 29 if (png_ptr == NULL) 30 return; 28 31 switch (crit_action) 29 32 { 30 case PNG_CRC_NO_CHANGE: /* leave setting as is */33 case PNG_CRC_NO_CHANGE: /* Leave setting as is */ 31 34 break; 32 case PNG_CRC_WARN_USE: /* warn/use data */ 35 36 case PNG_CRC_WARN_USE: /* Warn/use data */ 33 37 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; 34 38 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; 35 39 break; 36 case PNG_CRC_QUIET_USE: /* quiet/use data */ 40 41 case PNG_CRC_QUIET_USE: /* Quiet/use data */ 37 42 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; 38 43 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | 39 44 PNG_FLAG_CRC_CRITICAL_IGNORE; 40 45 break; 41 case PNG_CRC_WARN_DISCARD: /* not a valid action for critical data */ 42 png_warning(png_ptr, "Can't discard critical data on CRC error."); 43 case PNG_CRC_ERROR_QUIT: /* error/quit */ 46 47 case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */ 48 png_warning(png_ptr, 49 "Can't discard critical data on CRC error."); 50 case PNG_CRC_ERROR_QUIT: /* Error/quit */ 51 44 52 case PNG_CRC_DEFAULT: 45 53 default: … … 50 58 switch (ancil_action) 51 59 { 52 case PNG_CRC_NO_CHANGE: /* leave setting as is */60 case PNG_CRC_NO_CHANGE: /* Leave setting as is */ 53 61 break; 54 case PNG_CRC_WARN_USE: /* warn/use data */ 62 63 case PNG_CRC_WARN_USE: /* Warn/use data */ 55 64 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; 56 65 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; 57 66 break; 58 case PNG_CRC_QUIET_USE: /* quiet/use data */ 67 68 case PNG_CRC_QUIET_USE: /* Quiet/use data */ 59 69 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; 60 70 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | 61 71 PNG_FLAG_CRC_ANCILLARY_NOWARN; 62 72 break; 63 case PNG_CRC_ERROR_QUIT: /* error/quit */ 73 74 case PNG_CRC_ERROR_QUIT: /* Error/quit */ 64 75 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; 65 76 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; 66 77 break; 67 case PNG_CRC_WARN_DISCARD: /* warn/discard data */ 78 79 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */ 80 68 81 case PNG_CRC_DEFAULT: 69 82 default: … … 75 88 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ 76 89 defined(PNG_FLOATING_POINT_SUPPORTED) 77 /* handle alpha and tRNS via a background color */90 /* Handle alpha and tRNS via a background color */ 78 91 void PNGAPI 79 92 png_set_background(png_structp png_ptr, … … 81 94 int need_expand, double background_gamma) 82 95 { 83 png_debug(1, "in png_set_background\n"); 84 if(png_ptr == NULL) return; 96 png_debug(1, "in png_set_background"); 97 if (png_ptr == NULL) 98 return; 85 99 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) 86 100 { … … 99 113 100 114 #if defined(PNG_READ_16_TO_8_SUPPORTED) 101 /* strip 16 bit depth files to 8 bit depth */115 /* Strip 16 bit depth files to 8 bit depth */ 102 116 void PNGAPI 103 117 png_set_strip_16(png_structp png_ptr) 104 118 { 105 png_debug(1, "in png_set_strip_16\n"); 106 if(png_ptr == NULL) return; 119 png_debug(1, "in png_set_strip_16"); 120 if (png_ptr == NULL) 121 return; 107 122 png_ptr->transformations |= PNG_16_TO_8; 108 123 } … … 113 128 png_set_strip_alpha(png_structp png_ptr) 114 129 { 115 png_debug(1, "in png_set_strip_alpha\n"); 116 if(png_ptr == NULL) return; 130 png_debug(1, "in png_set_strip_alpha"); 131 if (png_ptr == NULL) 132 return; 117 133 png_ptr->flags |= PNG_FLAG_STRIP_ALPHA; 118 134 } … … 143 159 int full_dither) 144 160 { 145 png_debug(1, "in png_set_dither\n"); 146 if(png_ptr == NULL) return; 161 png_debug(1, "in png_set_dither"); 162 if (png_ptr == NULL) 163 return; 147 164 png_ptr->transformations |= PNG_DITHER; 148 165 … … 152 169 153 170 png_ptr->dither_index = (png_bytep)png_malloc(png_ptr, 154 (png_uint_32)(num_palette * png_sizeof 171 (png_uint_32)(num_palette * png_sizeof(png_byte))); 155 172 for (i = 0; i < num_palette; i++) 156 173 png_ptr->dither_index[i] = (png_byte)i; … … 162 179 { 163 180 /* This is easy enough, just throw out the least used colors. 164 Perhaps not the best solution, but good enough. */ 181 * Perhaps not the best solution, but good enough. 182 */ 165 183 166 184 int i; 167 185 168 /* initialize an array to sort colors */186 /* Initialize an array to sort colors */ 169 187 png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr, 170 (png_uint_32)(num_palette * png_sizeof 171 172 /* initialize the dither_sort array */188 (png_uint_32)(num_palette * png_sizeof(png_byte))); 189 190 /* Initialize the dither_sort array */ 173 191 for (i = 0; i < num_palette; i++) 174 192 png_ptr->dither_sort[i] = (png_byte)i; 175 193 176 194 /* Find the least used palette entries by starting a 177 bubble sort, and running it until we have sorted 178 out enough colors. Note that we don't care about 179 sorting all the colors, just finding which are 180 least used. */ 195 * bubble sort, and running it until we have sorted 196 * out enough colors. Note that we don't care about 197 * sorting all the colors, just finding which are 198 * least used. 199 */ 181 200 182 201 for (i = num_palette - 1; i >= maximum_colors; i--) 183 202 { 184 int done; /* to stop early if the list is pre-sorted */203 int done; /* To stop early if the list is pre-sorted */ 185 204 int j; 186 205 … … 203 222 } 204 223 205 /* swap the palette around, and set up a table, if necessary */224 /* Swap the palette around, and set up a table, if necessary */ 206 225 if (full_dither) 207 226 { 208 227 int j = num_palette; 209 228 210 /* put all the useful colors within the max, but don't 211 move the others */ 229 /* Put all the useful colors within the max, but don't 230 * move the others. 231 */ 212 232 for (i = 0; i < maximum_colors; i++) 213 233 { … … 225 245 int j = num_palette; 226 246 227 /* move all the used colors inside the max limit, and 228 develop a translation table */ 247 /* Move all the used colors inside the max limit, and 248 * develop a translation table. 249 */ 229 250 for (i = 0; i < maximum_colors; i++) 230 251 { 231 /* only move the colors we need to */252 /* Only move the colors we need to */ 232 253 if ((int)png_ptr->dither_sort[i] >= maximum_colors) 233 254 { … … 241 262 palette[j] = palette[i]; 242 263 palette[i] = tmp_color; 243 /* indicate where the color went */264 /* Indicate where the color went */ 244 265 png_ptr->dither_index[j] = (png_byte)i; 245 266 png_ptr->dither_index[i] = (png_byte)j; … … 247 268 } 248 269 249 /* find closest color for those colors we are not using */270 /* Find closest color for those colors we are not using */ 250 271 for (i = 0; i < num_palette; i++) 251 272 { … … 254 275 int min_d, k, min_k, d_index; 255 276 256 /* find the closest color to one we threw out */277 /* Find the closest color to one we threw out */ 257 278 d_index = png_ptr->dither_index[i]; 258 279 min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); … … 269 290 } 270 291 } 271 /* point to closest color */292 /* Point to closest color */ 272 293 png_ptr->dither_index[i] = (png_byte)min_k; 273 294 } … … 275 296 } 276 297 png_free(png_ptr, png_ptr->dither_sort); 277 png_ptr->dither_sort =NULL;298 png_ptr->dither_sort = NULL; 278 299 } 279 300 else 280 301 { 281 302 /* This is much harder to do simply (and quickly). Perhaps 282 283 284 285 286 287 288 303 * we need to go through a median cut routine, but those 304 * don't always behave themselves with only a few colors 305 * as input. So we will just find the closest two colors, 306 * and throw out one of them (chosen somewhat randomly). 307 * [We don't understand this at all, so if someone wants to 308 * work on improving it, be our guest - AED, GRP] 309 */ 289 310 int i; 290 311 int max_d; … … 293 314 png_dsortpp hash; 294 315 295 t =NULL;296 297 /* initialize palette index arrays */316 t = NULL; 317 318 /* Initialize palette index arrays */ 298 319 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, 299 (png_uint_32)(num_palette * png_sizeof 320 (png_uint_32)(num_palette * png_sizeof(png_byte))); 300 321 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, 301 (png_uint_32)(num_palette * png_sizeof 302 303 /* initialize the sort array */322 (png_uint_32)(num_palette * png_sizeof(png_byte))); 323 324 /* Initialize the sort array */ 304 325 for (i = 0; i < num_palette; i++) 305 326 { … … 309 330 310 331 hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 * 311 png_sizeof (png_dsortp))); 312 for (i = 0; i < 769; i++) 313 hash[i] = NULL; 314 /* png_memset(hash, 0, 769 * png_sizeof (png_dsortp)); */ 332 png_sizeof(png_dsortp))); 333 png_memset(hash, 0, 769 * png_sizeof(png_dsortp)); 315 334 316 335 num_new_palette = num_palette; 317 336 318 /* initial wild guess at how far apart the farthest pixel319 320 321 322 323 324 325 337 /* Initial wild guess at how far apart the farthest pixel 338 * pair we will be eliminating will be. Larger 339 * numbers mean more areas will be allocated, Smaller 340 * numbers run the risk of not saving enough data, and 341 * having to do this all over again. 342 * 343 * I have not done extensive checking on this number. 344 */ 326 345 max_d = 96; 327 346 … … 437 456 png_free(png_ptr, png_ptr->palette_to_index); 438 457 png_free(png_ptr, png_ptr->index_to_palette); 439 png_ptr->palette_to_index =NULL;440 png_ptr->index_to_palette =NULL;458 png_ptr->palette_to_index = NULL; 459 png_ptr->index_to_palette = NULL; 441 460 } 442 461 num_palette = maximum_colors; … … 458 477 int num_blue = (1 << PNG_DITHER_BLUE_BITS); 459 478 png_size_t num_entries = ((png_size_t)1 << total_bits); 460 461 479 png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr, 462 (png_uint_32)(num_entries * png_sizeof (png_byte))); 463 480 (png_uint_32)(num_entries * png_sizeof(png_byte))); 464 481 png_memset(png_ptr->palette_lookup, 0, num_entries * 465 png_sizeof 482 png_sizeof(png_byte)); 466 483 467 484 distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries * … … 527 544 png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma) 528 545 { 529 png_debug(1, "in png_set_gamma\n"); 530 if(png_ptr == NULL) return; 546 png_debug(1, "in png_set_gamma"); 547 if (png_ptr == NULL) 548 return; 531 549 if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) || 532 550 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) || … … 546 564 png_set_expand(png_structp png_ptr) 547 565 { 548 png_debug(1, "in png_set_expand\n"); 549 if(png_ptr == NULL) return; 566 png_debug(1, "in png_set_expand"); 567 if (png_ptr == NULL) 568 return; 550 569 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); 551 570 png_ptr->flags &= ~PNG_FLAG_ROW_INIT; … … 573 592 png_set_palette_to_rgb(png_structp png_ptr) 574 593 { 575 png_debug(1, "in png_set_palette_to_rgb\n"); 576 if(png_ptr == NULL) return; 594 png_debug(1, "in png_set_palette_to_rgb"); 595 if (png_ptr == NULL) 596 return; 577 597 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); 578 598 png_ptr->flags &= ~PNG_FLAG_ROW_INIT; … … 584 604 png_set_expand_gray_1_2_4_to_8(png_structp png_ptr) 585 605 { 586 png_debug(1, "in png_set_expand_gray_1_2_4_to_8\n"); 587 if(png_ptr == NULL) return; 606 png_debug(1, "in png_set_expand_gray_1_2_4_to_8"); 607 if (png_ptr == NULL) 608 return; 588 609 png_ptr->transformations |= PNG_EXPAND; 589 610 png_ptr->flags &= ~PNG_FLAG_ROW_INIT; … … 597 618 png_set_gray_1_2_4_to_8(png_structp png_ptr) 598 619 { 599 png_debug(1, "in png_set_gray_1_2_4_to_8\n"); 600 if(png_ptr == NULL) return; 620 png_debug(1, "in png_set_gray_1_2_4_to_8"); 621 if (png_ptr == NULL) 622 return; 601 623 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); 602 624 } … … 608 630 png_set_tRNS_to_alpha(png_structp png_ptr) 609 631 { 610 png_debug(1, "in png_set_tRNS_to_alpha \n");632 png_debug(1, "in png_set_tRNS_to_alpha"); 611 633 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); 612 634 png_ptr->flags &= ~PNG_FLAG_ROW_INIT; … … 618 640 png_set_gray_to_rgb(png_structp png_ptr) 619 641 { 620 png_debug(1, "in png_set_gray_to_rgb \n");642 png_debug(1, "in png_set_gray_to_rgb"); 621 643 png_ptr->transformations |= PNG_GRAY_TO_RGB; 622 644 png_ptr->flags &= ~PNG_FLAG_ROW_INIT; … … 634 656 double green) 635 657 { 636 int red_fixed = (int)((float)red*100000.0 + 0.5); 637 int green_fixed = (int)((float)green*100000.0 + 0.5); 638 if(png_ptr == NULL) return; 639 png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed); 658 int red_fixed = (int)((float)red*100000.0 + 0.5); 659 int green_fixed = (int)((float)green*100000.0 + 0.5); 660 if (png_ptr == NULL) 661 return; 662 png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed); 640 663 } 641 664 #endif … … 645 668 png_fixed_point red, png_fixed_point green) 646 669 { 647 png_debug(1, "in png_set_rgb_to_gray\n"); 648 if(png_ptr == NULL) return; 670 png_debug(1, "in png_set_rgb_to_gray"); 671 if (png_ptr == NULL) 672 return; 649 673 switch(error_action) 650 674 { 651 675 case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY; 652 676 break; 677 653 678 case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; 654 679 break; 680 655 681 case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; 656 682 } … … 660 686 #else 661 687 { 662 png_warning(png_ptr, "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED."); 688 png_warning(png_ptr, 689 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED."); 663 690 png_ptr->transformations &= ~PNG_RGB_TO_GRAY; 664 691 } … … 666 693 { 667 694 png_uint_16 red_int, green_int; 668 if (red < 0 || green < 0)695 if (red < 0 || green < 0) 669 696 { 670 697 red_int = 6968; /* .212671 * 32768 + .5 */ 671 698 green_int = 23434; /* .715160 * 32768 + .5 */ 672 699 } 673 else if (red + green < 100000L)674 { 675 red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);676 green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);700 else if (red + green < 100000L) 701 { 702 red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L); 703 green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L); 677 704 } 678 705 else … … 684 711 png_ptr->rgb_to_gray_red_coeff = red_int; 685 712 png_ptr->rgb_to_gray_green_coeff = green_int; 686 png_ptr->rgb_to_gray_blue_coeff = (png_uint_16)(32768-red_int-green_int); 713 png_ptr->rgb_to_gray_blue_coeff = 714 (png_uint_16)(32768 - red_int - green_int); 687 715 } 688 716 } … … 690 718 691 719 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ 692 defined(PNG_ WRITE_USER_TRANSFORM_SUPPORTED) || \693 defined(PNG_ LEGACY_SUPPORTED)720 defined(PNG_LEGACY_SUPPORTED) || \ 721 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) 694 722 void PNGAPI 695 723 png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr 696 724 read_user_transform_fn) 697 725 { 698 png_debug(1, "in png_set_read_user_transform_fn\n"); 699 if(png_ptr == NULL) return; 726 png_debug(1, "in png_set_read_user_transform_fn"); 727 if (png_ptr == NULL) 728 return; 700 729 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) 701 730 png_ptr->transformations |= PNG_USER_TRANSFORM; … … 703 732 #endif 704 733 #ifdef PNG_LEGACY_SUPPORTED 705 if (read_user_transform_fn)734 if (read_user_transform_fn) 706 735 png_warning(png_ptr, 707 736 "This version of libpng does not support user transforms"); … … 716 745 png_init_read_transformations(png_structp png_ptr) 717 746 { 718 png_debug(1, "in png_init_read_transformations \n");747 png_debug(1, "in png_init_read_transformations"); 719 748 #if defined(PNG_USELESS_TESTS_SUPPORTED) 720 if (png_ptr != NULL)749 if (png_ptr != NULL) 721 750 #endif 722 751 { … … 730 759 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) 731 760 /* Detect gray background and attempt to enable optimization 732 * for gray --> RGB case */ 733 /* Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or 761 * for gray --> RGB case 762 * 763 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or 734 764 * RGB_ALPHA (in which case need_expand is superfluous anyway), the 735 765 * background color might actually be gray yet not be flagged as such. … … 758 788 if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */ 759 789 { 760 /* expand background and tRNS chunks */790 /* Expand background and tRNS chunks */ 761 791 switch (png_ptr->bit_depth) 762 792 { … … 772 802 } 773 803 break; 804 774 805 case 2: 775 806 png_ptr->background.gray *= (png_uint_16)0x55; … … 783 814 } 784 815 break; 816 785 817 case 4: 786 818 png_ptr->background.gray *= (png_uint_16)0x11; … … 794 826 } 795 827 break; 828 796 829 case 8: 830 797 831 case 16: 798 832 png_ptr->background.red = png_ptr->background.green … … 817 851 #endif 818 852 { 819 /* invert the alpha channel (in tRNS) unless the pixels are 820 going to be expanded, in which case leave it for later */ 821 int i,istop; 853 /* Invert the alpha channel (in tRNS) unless the pixels are 854 * going to be expanded, in which case leave it for later 855 */ 856 int i, istop; 822 857 istop=(int)png_ptr->num_trans; 823 858 for (i=0; i<istop; i++) … … 840 875 < PNG_GAMMA_THRESHOLD)) 841 876 { 842 int i, k;877 int i, k; 843 878 k=0; 844 879 for (i=0; i<png_ptr->num_trans; i++) 845 880 { 846 881 if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff) 847 k=1; /* partial transparency is present */882 k=1; /* Partial transparency is present */ 848 883 } 849 884 if (k == 0) … … 860 895 if (color_type == PNG_COLOR_TYPE_PALETTE) 861 896 { 862 /* could skip if no transparency and 863 */ 897 /* Could skip if no transparency */ 864 898 png_color back, back_1; 865 899 png_colorp palette = png_ptr->palette; … … 886 920 gs = 1.0; 887 921 break; 922 888 923 case PNG_BACKGROUND_GAMMA_FILE: 889 924 g = 1.0 / (png_ptr->gamma); 890 925 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); 891 926 break; 927 892 928 case PNG_BACKGROUND_GAMMA_UNIQUE: 893 929 g = 1.0 / (png_ptr->background_gamma); … … 978 1014 gs = 1.0; 979 1015 break; 1016 980 1017 case PNG_BACKGROUND_GAMMA_FILE: 981 1018 g = 1.0 / (png_ptr->gamma); 982 1019 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); 983 1020 break; 1021 984 1022 case PNG_BACKGROUND_GAMMA_UNIQUE: 985 1023 g = 1.0 / (png_ptr->background_gamma); … … 1023 1061 } 1024 1062 else 1025 /* transformation does not include PNG_BACKGROUND */1063 /* Transformation does not include PNG_BACKGROUND */ 1026 1064 #endif /* PNG_READ_BACKGROUND_SUPPORTED */ 1027 1065 if (color_type == PNG_COLOR_TYPE_PALETTE) … … 1111 1149 #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \ 1112 1150 && !defined(PNG_READ_BACKGROUND_SUPPORTED) 1113 if (png_ptr)1151 if (png_ptr) 1114 1152 return; 1115 1153 #endif … … 1123 1161 png_read_transform_info(png_structp png_ptr, png_infop info_ptr) 1124 1162 { 1125 png_debug(1, "in png_read_transform_info \n");1163 png_debug(1, "in png_read_transform_info"); 1126 1164 #if defined(PNG_READ_EXPAND_SUPPORTED) 1127 1165 if (png_ptr->transformations & PNG_EXPAND) … … 1143 1181 if (png_ptr->transformations & PNG_EXPAND_tRNS) 1144 1182 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; 1145 #if 0 /* Removed from libpng-1.2.27 */1146 else1147 info_ptr->color_type |= PNG_COLOR_MASK_COLOR;1148 #endif1149 1183 } 1150 1184 if (info_ptr->bit_depth < 8) … … 1195 1229 { 1196 1230 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || 1197 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&1198 png_ptr->palette_lookup && info_ptr->bit_depth == 8)1231 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && 1232 png_ptr->palette_lookup && info_ptr->bit_depth == 8) 1199 1233 { 1200 1234 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; … … 1230 1264 { 1231 1265 info_ptr->channels++; 1232 /* if adding a true alpha channel not just filler */1266 /* If adding a true alpha channel not just filler */ 1233 1267 #if !defined(PNG_1_0_X) 1234 1268 if (png_ptr->transformations & PNG_ADD_ALPHA) … … 1240 1274 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ 1241 1275 defined(PNG_READ_USER_TRANSFORM_SUPPORTED) 1242 if (png_ptr->transformations & PNG_USER_TRANSFORM)1276 if (png_ptr->transformations & PNG_USER_TRANSFORM) 1243 1277 { 1244 if (info_ptr->bit_depth < png_ptr->user_transform_depth)1278 if (info_ptr->bit_depth < png_ptr->user_transform_depth) 1245 1279 info_ptr->bit_depth = png_ptr->user_transform_depth; 1246 if (info_ptr->channels < png_ptr->user_transform_channels)1280 if (info_ptr->channels < png_ptr->user_transform_channels) 1247 1281 info_ptr->channels = png_ptr->user_transform_channels; 1248 1282 } … … 1252 1286 info_ptr->bit_depth); 1253 1287 1254 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);1288 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width); 1255 1289 1256 1290 #if !defined(PNG_READ_EXPAND_SUPPORTED) 1257 if (png_ptr)1291 if (png_ptr) 1258 1292 return; 1259 1293 #endif … … 1267 1301 png_do_read_transformations(png_structp png_ptr) 1268 1302 { 1269 png_debug(1, "in png_do_read_transformations \n");1303 png_debug(1, "in png_do_read_transformations"); 1270 1304 if (png_ptr->row_buf == NULL) 1271 1305 { … … 1274 1308 1275 1309 png_snprintf2(msg, 50, 1276 "NULL row buffer for row %ld, pass %d", png_ptr->row_number,1310 "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number, 1277 1311 png_ptr->pass); 1278 1312 png_error(png_ptr, msg); … … 1285 1319 /* Application has failed to call either png_read_start_image() 1286 1320 * or png_read_update_info() after setting transforms that expand 1287 * pixels. This check added to libpng-1.2.19 */ 1321 * pixels. This check added to libpng-1.2.19 1322 */ 1288 1323 #if (PNG_WARN_UNINITIALIZED_ROW==1) 1289 1324 png_error(png_ptr, "Uninitialized row"); … … 1325 1360 int rgb_error = 1326 1361 png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1); 1327 if (rgb_error)1362 if (rgb_error) 1328 1363 { 1329 1364 png_ptr->rgb_to_gray_status=1; 1330 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==1365 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 1331 1366 PNG_RGB_TO_GRAY_WARN) 1332 1367 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); 1333 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==1368 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 1334 1369 PNG_RGB_TO_GRAY_ERR) 1335 1370 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); … … 1338 1373 #endif 1339 1374 1340 /* 1341 From Andreas Dilger e-mail to png-implement, 26 March 1998: 1342 1343 In most cases, the "simple transparency" should be done prior to doing 1344 gray-to-RGB, or you will have to test 3x as many bytes to check if a 1345 pixel is transparent. You would also need to make sure that the 1346 transparency information is upgraded to RGB. 1347 1348 To summarize, the current flow is: 1349 - Gray + simple transparency -> compare 1 or 2 gray bytes and composite 1350 with background "in place" if transparent, 1351 convert to RGB if necessary 1352 - Gray + alpha -> composite with gray background and remove alpha bytes, 1353 convert to RGB if necessary 1354 1355 To support RGB backgrounds for gray images we need: 1356 - Gray + simple transparency -> convert to RGB + simple transparency, compare 1357 3 or 6 bytes and composite with background 1358 "in place" if transparent (3x compare/pixel 1359 compared to doing composite with gray bkgrnd) 1360 - Gray + alpha -> convert to RGB + alpha, composite with background and 1361 remove alpha bytes (3x float operations/pixel 1362 compared with composite on gray background) 1363 1364 Greg's change will do this. The reason it wasn't done before is for 1365 performance, as this increases the per-pixel operations. If we would check 1366 in advance if the background was gray or RGB, and position the gray-to-RGB 1367 transform appropriately, then it would save a lot of work/time. 1375 /* From Andreas Dilger e-mail to png-implement, 26 March 1998: 1376 * 1377 * In most cases, the "simple transparency" should be done prior to doing 1378 * gray-to-RGB, or you will have to test 3x as many bytes to check if a 1379 * pixel is transparent. You would also need to make sure that the 1380 * transparency information is upgraded to RGB. 1381 * 1382 * To summarize, the current flow is: 1383 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite 1384 * with background "in place" if transparent, 1385 * convert to RGB if necessary 1386 * - Gray + alpha -> composite with gray background and remove alpha bytes, 1387 * convert to RGB if necessary 1388 * 1389 * To support RGB backgrounds for gray images we need: 1390 * - Gray + simple transparency -> convert to RGB + simple transparency, 1391 * compare 3 or 6 bytes and composite with 1392 * background "in place" if transparent 1393 * (3x compare/pixel compared to doing 1394 * composite with gray bkgrnd) 1395 * - Gray + alpha -> convert to RGB + alpha, composite with background and 1396 * remove alpha bytes (3x float 1397 * operations/pixel compared with composite 1398 * on gray background) 1399 * 1400 * Greg's change will do this. The reason it wasn't done before is for 1401 * performance, as this increases the per-pixel operations. If we would check 1402 * in advance if the background was gray or RGB, and position the gray-to-RGB 1403 * transform appropriately, then it would save a lot of work/time. 1368 1404 */ 1369 1405 1370 1406 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) 1371 /* if gray -> RGB, do so now only if background is non-gray; else do later 1372 * for performance reasons */ 1407 /* If gray -> RGB, do so now only if background is non-gray; else do later 1408 * for performance reasons 1409 */ 1373 1410 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && 1374 1411 !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) … … 1395 1432 if ((png_ptr->transformations & PNG_GAMMA) && 1396 1433 #if defined(PNG_READ_BACKGROUND_SUPPORTED) 1397 !((png_ptr->transformations & PNG_BACKGROUND) &&1398 ((png_ptr->num_trans != 0) ||1399 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&1400 #endif 1401 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))1434 !((png_ptr->transformations & PNG_BACKGROUND) && 1435 ((png_ptr->num_trans != 0) || 1436 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) && 1437 #endif 1438 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) 1402 1439 png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1, 1403 png_ptr->gamma_table, png_ptr->gamma_16_table,1404 png_ptr->gamma_shift);1440 png_ptr->gamma_table, png_ptr->gamma_16_table, 1441 png_ptr->gamma_shift); 1405 1442 #endif 1406 1443 … … 1415 1452 png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1, 1416 1453 png_ptr->palette_lookup, png_ptr->dither_index); 1417 if (png_ptr->row_info.rowbytes == (png_uint_32)0)1454 if (png_ptr->row_info.rowbytes == (png_uint_32)0) 1418 1455 png_error(png_ptr, "png_do_dither returned rowbytes=0"); 1419 1456 } … … 1447 1484 1448 1485 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) 1449 /* if gray -> RGB, do so now only if we did not do so above */1486 /* If gray -> RGB, do so now only if we did not do so above */ 1450 1487 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) && 1451 1488 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY)) … … 1477 1514 if (png_ptr->transformations & PNG_USER_TRANSFORM) 1478 1515 { 1479 if (png_ptr->read_user_transform_fn != NULL)1480 (*(png_ptr->read_user_transform_fn)) /* user read transform function */1481 (png_ptr, /* png_ptr */1482 &(png_ptr->row_info), /* row_info:*/1483 /* png_uint_32 width;width of row */1484 /* png_uint_32 rowbytes;number of bytes in row */1485 /* png_byte color_type;color type of pixels */1486 /* png_byte bit_depth;bit depth of samples */1487 /* png_byte channels;number of channels (1-4) */1488 /* png_byte pixel_depth;bits per pixel (depth*channels) */1489 png_ptr->row_buf + 1);/* start of pixel data for row */1516 if (png_ptr->read_user_transform_fn != NULL) 1517 (*(png_ptr->read_user_transform_fn)) /* User read transform function */ 1518 (png_ptr, /* png_ptr */ 1519 &(png_ptr->row_info), /* row_info: */ 1520 /* png_uint_32 width; width of row */ 1521 /* png_uint_32 rowbytes; number of bytes in row */ 1522 /* png_byte color_type; color type of pixels */ 1523 /* png_byte bit_depth; bit depth of samples */ 1524 /* png_byte channels; number of channels (1-4) */ 1525 /* png_byte pixel_depth; bits per pixel (depth*channels) */ 1526 png_ptr->row_buf + 1); /* start of pixel data for row */ 1490 1527 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) 1491 if (png_ptr->user_transform_depth)1528 if (png_ptr->user_transform_depth) 1492 1529 png_ptr->row_info.bit_depth = png_ptr->user_transform_depth; 1493 if (png_ptr->user_transform_channels)1530 if (png_ptr->user_transform_channels) 1494 1531 png_ptr->row_info.channels = png_ptr->user_transform_channels; 1495 1532 #endif … … 1513 1550 png_do_unpack(png_row_infop row_info, png_bytep row) 1514 1551 { 1515 png_debug(1, "in png_do_unpack \n");1552 png_debug(1, "in png_do_unpack"); 1516 1553 #if defined(PNG_USELESS_TESTS_SUPPORTED) 1517 1554 if (row != NULL && row_info != NULL && row_info->bit_depth < 8) … … 1545 1582 break; 1546 1583 } 1584 1547 1585 case 2: 1548 1586 { … … 1566 1604 break; 1567 1605 } 1606 1568 1607 case 4: 1569 1608 { … … 1603 1642 png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits) 1604 1643 { 1605 png_debug(1, "in png_do_unshift \n");1644 png_debug(1, "in png_do_unshift"); 1606 1645 if ( 1607 1646 #if defined(PNG_USELESS_TESTS_SUPPORTED) … … 1657 1696 break; 1658 1697 } 1698 1659 1699 case 4: 1660 1700 { … … 1672 1712 break; 1673 1713 } 1714 1674 1715 case 8: 1675 1716 { … … 1684 1725 break; 1685 1726 } 1727 1686 1728 case 16: 1687 1729 { … … 1705 1747 1706 1748 #if defined(PNG_READ_16_TO_8_SUPPORTED) 1707 /* chop rows of bit depth 16 down to 8 */1749 /* Chop rows of bit depth 16 down to 8 */ 1708 1750 void /* PRIVATE */ 1709 1751 png_do_chop(png_row_infop row_info, png_bytep row) 1710 1752 { 1711 png_debug(1, "in png_do_chop \n");1753 png_debug(1, "in png_do_chop"); 1712 1754 #if defined(PNG_USELESS_TESTS_SUPPORTED) 1713 1755 if (row != NULL && row_info != NULL && row_info->bit_depth == 16) … … 1729 1771 * What the ideal calculation should be: 1730 1772 * *dp = (((((png_uint_32)(*sp) << 8) | 1731 * (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L; 1773 * (png_uint_32)(*(sp + 1))) * 255 + 127) 1774 * / (png_uint_32)65535L; 1732 1775 * 1733 1776 * GRR: no, I think this is what it really should be: 1734 1777 * *dp = (((((png_uint_32)(*sp) << 8) | 1735 * (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L; 1778 * (png_uint_32)(*(sp + 1))) + 128L) 1779 * / (png_uint_32)257L; 1736 1780 * 1737 1781 * GRR: here's the exact calculation with shifts: 1738 * temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L; 1782 * temp = (((png_uint_32)(*sp) << 8) | 1783 * (png_uint_32)(*(sp + 1))) + 128L; 1739 1784 * *dp = (temp - (temp >> 8)) >> 8; 1740 1785 * … … 1763 1808 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) 1764 1809 { 1765 png_debug(1, "in png_do_read_swap_alpha \n");1810 png_debug(1, "in png_do_read_swap_alpha"); 1766 1811 #if defined(PNG_USELESS_TESTS_SUPPORTED) 1767 1812 if (row != NULL && row_info != NULL) … … 1855 1900 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) 1856 1901 { 1857 png_debug(1, "in png_do_read_invert_alpha \n");1902 png_debug(1, "in png_do_read_invert_alpha"); 1858 1903 #if defined(PNG_USELESS_TESTS_SUPPORTED) 1859 1904 if (row != NULL && row_info != NULL) … … 1961 2006 png_byte lo_filler = (png_byte)(filler & 0xff); 1962 2007 1963 png_debug(1, "in png_do_read_filler \n");2008 png_debug(1, "in png_do_read_filler"); 1964 2009 if ( 1965 2010 #if defined(PNG_USELESS_TESTS_SUPPORTED) … … 1968 2013 row_info->color_type == PNG_COLOR_TYPE_GRAY) 1969 2014 { 1970 if (row_info->bit_depth == 8)2015 if (row_info->bit_depth == 8) 1971 2016 { 1972 2017 /* This changes the data from G to GX */ … … 2000 2045 } 2001 2046 } 2002 else if (row_info->bit_depth == 16)2047 else if (row_info->bit_depth == 16) 2003 2048 { 2004 2049 /* This changes the data from GG to GGXX */ … … 2040 2085 else if (row_info->color_type == PNG_COLOR_TYPE_RGB) 2041 2086 { 2042 if (row_info->bit_depth == 8)2087 if (row_info->bit_depth == 8) 2043 2088 { 2044 2089 /* This changes the data from RGB to RGBX */ … … 2076 2121 } 2077 2122 } 2078 else if (row_info->bit_depth == 16)2123 else if (row_info->bit_depth == 16) 2079 2124 { 2080 2125 /* This changes the data from RRGGBB to RRGGBBXX */ … … 2126 2171 2127 2172 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED) 2128 /* expand grayscale files to RGB, with or without alpha */2173 /* Expand grayscale files to RGB, with or without alpha */ 2129 2174 void /* PRIVATE */ 2130 2175 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) … … 2133 2178 png_uint_32 row_width = row_info->width; 2134 2179 2135 png_debug(1, "in png_do_gray_to_rgb \n");2180 png_debug(1, "in png_do_gray_to_rgb"); 2136 2181 if (row_info->bit_depth >= 8 && 2137 2182 #if defined(PNG_USELESS_TESTS_SUPPORTED) … … 2203 2248 row_info->pixel_depth = (png_byte)(row_info->channels * 2204 2249 row_info->bit_depth); 2205 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);2250 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); 2206 2251 } 2207 2252 } … … 2209 2254 2210 2255 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) 2211 /* reduce RGB files to grayscale, with or without alpha2256 /* Reduce RGB files to grayscale, with or without alpha 2212 2257 * using the equation given in Poynton's ColorFAQ at 2213 * <http://www.inforamp.net/~poynton/> 2214 * Copyright (c) 1998-01-04 Charles Poynton poynton at inforamp.net 2258 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008) 2259 * New link: 2260 * <http://www.poynton.com/notes/colour_and_gamma/> 2261 * Charles Poynton poynton at poynton.com 2215 2262 * 2216 2263 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B … … 2237 2284 int rgb_error = 0; 2238 2285 2239 png_debug(1, "in png_do_rgb_to_gray \n");2286 png_debug(1, "in png_do_rgb_to_gray"); 2240 2287 if ( 2241 2288 #if defined(PNG_USELESS_TESTS_SUPPORTED) … … 2263 2310 png_byte green = png_ptr->gamma_to_1[*(sp++)]; 2264 2311 png_byte blue = png_ptr->gamma_to_1[*(sp++)]; 2265 if (red != green || red != blue)2312 if (red != green || red != blue) 2266 2313 { 2267 2314 rgb_error |= 1; 2268 2315 *(dp++) = png_ptr->gamma_from_1[ 2269 (rc*red +gc*green+bc*blue)>>15];2316 (rc*red + gc*green + bc*blue)>>15]; 2270 2317 } 2271 2318 else 2272 *(dp++) = *(sp -1);2319 *(dp++) = *(sp - 1); 2273 2320 } 2274 2321 } … … 2283 2330 png_byte green = *(sp++); 2284 2331 png_byte blue = *(sp++); 2285 if (red != green || red != blue)2332 if (red != green || red != blue) 2286 2333 { 2287 2334 rgb_error |= 1; 2288 *(dp++) = (png_byte)((rc*red +gc*green+bc*blue)>>15);2335 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); 2289 2336 } 2290 2337 else 2291 *(dp++) = *(sp -1);2338 *(dp++) = *(sp - 1); 2292 2339 } 2293 2340 } … … 2310 2357 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; 2311 2358 2312 if (red == green && red == blue)2359 if (red == green && red == blue) 2313 2360 w = red; 2314 2361 else … … 2344 2391 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; 2345 2392 2346 if (red != green || red != blue)2393 if (red != green || red != blue) 2347 2394 rgb_error |= 1; 2348 2395 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); … … 2367 2414 png_byte green = png_ptr->gamma_to_1[*(sp++)]; 2368 2415 png_byte blue = png_ptr->gamma_to_1[*(sp++)]; 2369 if (red != green || red != blue)2416 if (red != green || red != blue) 2370 2417 rgb_error |= 1; 2371 2418 *(dp++) = png_ptr->gamma_from_1 … … 2384 2431 png_byte green = *(sp++); 2385 2432 png_byte blue = *(sp++); 2386 if (red != green || red != blue)2433 if (red != green || red != blue) 2387 2434 rgb_error |= 1; 2388 2435 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); … … 2407 2454 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2; 2408 2455 2409 if (red == green && red == blue)2456 if (red == green && red == blue) 2410 2457 w = red; 2411 2458 else … … 2441 2488 green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; 2442 2489 blue = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2; 2443 if (red != green || red != blue)2490 if (red != green || red != blue) 2444 2491 rgb_error |= 1; 2445 2492 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15); … … 2456 2503 row_info->pixel_depth = (png_byte)(row_info->channels * 2457 2504 row_info->bit_depth); 2458 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);2505 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); 2459 2506 } 2460 2507 return rgb_error; … … 2475 2522 int v; 2476 2523 2477 png_debug(1, "in png_do_build_grayscale_palette \n");2524 png_debug(1, "in png_do_build_grayscale_palette"); 2478 2525 if (palette == NULL) 2479 2526 return; … … 2485 2532 color_inc = 0xff; 2486 2533 break; 2534 2487 2535 case 2: 2488 2536 num_palette = 4; 2489 2537 color_inc = 0x55; 2490 2538 break; 2539 2491 2540 case 4: 2492 2541 num_palette = 16; 2493 2542 color_inc = 0x11; 2494 2543 break; 2544 2495 2545 case 8: 2496 2546 num_palette = 256; 2497 2547 color_inc = 1; 2498 2548 break; 2549 2499 2550 default: 2500 2551 num_palette = 0; … … 2517 2568 int num_palette) 2518 2569 { 2519 png_debug(1, "in png_correct_palette \n");2570 png_debug(1, "in png_correct_palette"); 2520 2571 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ 2521 2572 defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED) … … 2674 2725 } 2675 2726 } 2676 else /* assume grayscale palette (what else could it be?) */2727 else /* Assume grayscale palette (what else could it be?) */ 2677 2728 { 2678 2729 int i; … … 2714 2765 int shift; 2715 2766 2716 png_debug(1, "in png_do_background \n");2767 png_debug(1, "in png_do_background"); 2717 2768 if (background != NULL && 2718 2769 #if defined(PNG_USELESS_TESTS_SUPPORTED) … … 2750 2801 break; 2751 2802 } 2803 2752 2804 case 2: 2753 2805 { … … 2806 2858 break; 2807 2859 } 2860 2808 2861 case 4: 2809 2862 { … … 2862 2915 break; 2863 2916 } 2917 2864 2918 case 8: 2865 2919 { … … 2894 2948 break; 2895 2949 } 2950 2896 2951 case 16: 2897 2952 { … … 2907 2962 if (v == trans_values->gray) 2908 2963 { 2909 /* background is already in screen gamma */2964 /* Background is already in screen gamma */ 2910 2965 *sp = (png_byte)((background->gray >> 8) & 0xff); 2911 2966 *(sp + 1) = (png_byte)(background->gray & 0xff); … … 2940 2995 break; 2941 2996 } 2997 2942 2998 case PNG_COLOR_TYPE_RGB: 2943 2999 { … … 2997 3053 b == trans_values->blue) 2998 3054 { 2999 /* background is already in screen gamma */3055 /* Background is already in screen gamma */ 3000 3056 *sp = (png_byte)((background->red >> 8) & 0xff); 3001 3057 *(sp + 1) = (png_byte)(background->red & 0xff); … … 3044 3100 break; 3045 3101 } 3102 3046 3103 case PNG_COLOR_TYPE_GRAY_ALPHA: 3047 3104 { … … 3064 3121 else if (a == 0) 3065 3122 { 3066 /* background is already in screen gamma */3123 /* Background is already in screen gamma */ 3067 3124 *dp = (png_byte)background->gray; 3068 3125 } … … 3131 3188 #endif 3132 3189 { 3133 /* background is already in screen gamma */3190 /* Background is already in screen gamma */ 3134 3191 *dp = (png_byte)((background->gray >> 8) & 0xff); 3135 3192 *(dp + 1) = (png_byte)(background->gray & 0xff); … … 3186 3243 break; 3187 3244 } 3245 3188 3246 case PNG_COLOR_TYPE_RGB_ALPHA: 3189 3247 { … … 3208 3266 else if (a == 0) 3209 3267 { 3210 /* background is already in screen gamma */3268 /* Background is already in screen gamma */ 3211 3269 *dp = (png_byte)background->red; 3212 3270 *(dp + 1) = (png_byte)background->green; … … 3289 3347 else if (a == 0) 3290 3348 { 3291 /* background is already in screen gamma */3349 /* Background is already in screen gamma */ 3292 3350 *dp = (png_byte)((background->red >> 8) & 0xff); 3293 3351 *(dp + 1) = (png_byte)(background->red & 0xff); … … 3374 3432 row_info->pixel_depth = (png_byte)(row_info->channels * 3375 3433 row_info->bit_depth); 3376 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);3434 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); 3377 3435 } 3378 3436 } … … 3396 3454 png_uint_32 row_width=row_info->width; 3397 3455 3398 png_debug(1, "in png_do_gamma \n");3456 png_debug(1, "in png_do_gamma"); 3399 3457 if ( 3400 3458 #if defined(PNG_USELESS_TESTS_SUPPORTED) … … 3444 3502 break; 3445 3503 } 3504 3446 3505 case PNG_COLOR_TYPE_RGB_ALPHA: 3447 3506 { … … 3481 3540 break; 3482 3541 } 3542 3483 3543 case PNG_COLOR_TYPE_GRAY_ALPHA: 3484 3544 { … … 3505 3565 break; 3506 3566 } 3567 3507 3568 case PNG_COLOR_TYPE_GRAY: 3508 3569 { … … 3525 3586 } 3526 3587 } 3588 3527 3589 if (row_info->bit_depth == 4) 3528 3590 { … … 3538 3600 } 3539 3601 } 3602 3540 3603 else if (row_info->bit_depth == 8) 3541 3604 { … … 3547 3610 } 3548 3611 } 3612 3549 3613 else if (row_info->bit_depth == 16) 3550 3614 { … … 3578 3642 png_uint_32 row_width=row_info->width; 3579 3643 3580 png_debug(1, "in png_do_expand_palette \n");3644 png_debug(1, "in png_do_expand_palette"); 3581 3645 if ( 3582 3646 #if defined(PNG_USELESS_TESTS_SUPPORTED) … … 3612 3676 break; 3613 3677 } 3678 3614 3679 case 2: 3615 3680 { … … 3633 3698 break; 3634 3699 } 3700 3635 3701 case 4: 3636 3702 { … … 3697 3763 sp--; 3698 3764 } 3765 3699 3766 row_info->bit_depth = 8; 3700 3767 row_info->pixel_depth = 24; … … 3721 3788 png_uint_32 row_width=row_info->width; 3722 3789 3723 png_debug(1, "in png_do_expand \n");3790 png_debug(1, "in png_do_expand"); 3724 3791 #if defined(PNG_USELESS_TESTS_SUPPORTED) 3725 3792 if (row != NULL && row_info != NULL) … … 3758 3825 break; 3759 3826 } 3827 3760 3828 case 2: 3761 3829 { … … 3781 3849 break; 3782 3850 } 3851 3783 3852 case 4: 3784 3853 { … … 3804 3873 } 3805 3874 } 3875 3806 3876 row_info->bit_depth = 8; 3807 3877 row_info->pixel_depth = 8; … … 3825 3895 } 3826 3896 } 3897 3827 3898 else if (row_info->bit_depth == 16) 3828 3899 { … … 3833 3904 for (i = 0; i < row_width; i++) 3834 3905 { 3835 if (*(sp -1) == gray_high && *(sp) == gray_low)3906 if (*(sp - 1) == gray_high && *(sp) == gray_low) 3836 3907 { 3837 3908 *dp-- = 0; … … 3847 3918 } 3848 3919 } 3920 3849 3921 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; 3850 3922 row_info->channels = 2; … … 3912 3984 row_info->channels = 4; 3913 3985 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); 3914 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);3986 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); 3915 3987 } 3916 3988 } … … 3927 3999 png_uint_32 row_width=row_info->width; 3928 4000 3929 png_debug(1, "in png_do_dither \n");4001 png_debug(1, "in png_do_dither"); 3930 4002 #if defined(PNG_USELESS_TESTS_SUPPORTED) 3931 4003 if (row != NULL && row_info != NULL) … … 3944 4016 b = *sp++; 3945 4017 3946 /* this looks real messy, but the compiler will reduce3947 3948 3949 3950 3951 3952 4018 /* This looks real messy, but the compiler will reduce 4019 * it down to a reasonable formula. For example, with 4020 * 5 bits per color, we get: 4021 * p = (((r >> 3) & 0x1f) << 10) | 4022 * (((g >> 3) & 0x1f) << 5) | 4023 * ((b >> 3) & 0x1f); 4024 */ 3953 4025 p = (((r >> (8 - PNG_DITHER_RED_BITS)) & 3954 4026 ((1 << PNG_DITHER_RED_BITS) - 1)) << … … 3965 4037 row_info->channels = 1; 3966 4038 row_info->pixel_depth = row_info->bit_depth; 3967 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);4039 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); 3968 4040 } 3969 4041 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && … … 3994 4066 row_info->channels = 1; 3995 4067 row_info->pixel_depth = row_info->bit_depth; 3996 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);4068 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); 3997 4069 } 3998 4070 else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && … … 4022 4094 png_build_gamma_table(png_structp png_ptr) 4023 4095 { 4024 png_debug(1, "in png_build_gamma_table \n");4096 png_debug(1, "in png_build_gamma_table"); 4025 4097 4026 4098 if (png_ptr->bit_depth <= 8) … … 4031 4103 if (png_ptr->screen_gamma > .000001) 4032 4104 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma); 4105 4033 4106 else 4034 4107 g = 1.0; … … 4063 4136 (png_uint_32)256); 4064 4137 4065 if (png_ptr->screen_gamma > 0.000001)4138 if (png_ptr->screen_gamma > 0.000001) 4066 4139 g = 1.0 / png_ptr->screen_gamma; 4140 4067 4141 else 4068 g = png_ptr->gamma; /* probably doing rgb_to_gray */4142 g = png_ptr->gamma; /* Probably doing rgb_to_gray */ 4069 4143 4070 4144 for (i = 0; i < 256; i++) … … 4087 4161 { 4088 4162 sig_bit = (int)png_ptr->sig_bit.red; 4163 4089 4164 if ((int)png_ptr->sig_bit.green > sig_bit) 4090 4165 sig_bit = png_ptr->sig_bit.green; 4166 4091 4167 if ((int)png_ptr->sig_bit.blue > sig_bit) 4092 4168 sig_bit = png_ptr->sig_bit.blue; … … 4099 4175 if (sig_bit > 0) 4100 4176 shift = 16 - sig_bit; 4177 4101 4178 else 4102 4179 shift = 0; … … 4110 4187 if (shift > 8) 4111 4188 shift = 8; 4189 4112 4190 if (shift < 0) 4113 4191 shift = 0; … … 4123 4201 4124 4202 png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr, 4125 (png_uint_32)(num * png_sizeof (png_uint_16p))); 4203 (png_uint_32)(num * png_sizeof(png_uint_16p))); 4204 png_memset(png_ptr->gamma_16_table, 0, num * png_sizeof(png_uint_16p)); 4126 4205 4127 4206 if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND)) … … 4133 4212 { 4134 4213 png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, 4135 (png_uint_32)(256 * png_sizeof 4214 (png_uint_32)(256 * png_sizeof(png_uint_16))); 4136 4215 } 4137 4216 … … 4163 4242 { 4164 4243 png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr, 4165 (png_uint_32)(256 * png_sizeof 4244 (png_uint_32)(256 * png_sizeof(png_uint_16))); 4166 4245 4167 4246 ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4); 4247 4168 4248 for (j = 0; j < 256; j++) 4169 4249 { … … 4183 4263 4184 4264 png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr, 4185 (png_uint_32)(num * png_sizeof (png_uint_16p ))); 4265 (png_uint_32)(num * png_sizeof(png_uint_16p ))); 4266 png_memset(png_ptr->gamma_16_to_1, 0, num * png_sizeof(png_uint_16p)); 4186 4267 4187 4268 for (i = 0; i < num; i++) 4188 4269 { 4189 4270 png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr, 4190 (png_uint_32)(256 * png_sizeof 4271 (png_uint_32)(256 * png_sizeof(png_uint_16))); 4191 4272 4192 4273 ig = (((png_uint_32)i * … … 4200 4281 } 4201 4282 4202 if (png_ptr->screen_gamma > 0.000001)4283 if (png_ptr->screen_gamma > 0.000001) 4203 4284 g = 1.0 / png_ptr->screen_gamma; 4285 4204 4286 else 4205 g = png_ptr->gamma; /* probably doing rgb_to_gray */4287 g = png_ptr->gamma; /* Probably doing rgb_to_gray */ 4206 4288 4207 4289 png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr, 4208 (png_uint_32)(num * png_sizeof (png_uint_16p))); 4290 (png_uint_32)(num * png_sizeof(png_uint_16p))); 4291 png_memset(png_ptr->gamma_16_from_1, 0, 4292 num * png_sizeof(png_uint_16p)); 4209 4293 4210 4294 for (i = 0; i < num; i++) 4211 4295 { 4212 4296 png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr, 4213 (png_uint_32)(256 * png_sizeof 4297 (png_uint_32)(256 * png_sizeof(png_uint_16))); 4214 4298 4215 4299 ig = (((png_uint_32)i * 4216 4300 (png_uint_32)png_gamma_shift[shift]) >> 4); 4301 4217 4302 for (j = 0; j < 256; j++) 4218 4303 { … … 4231 4316 4232 4317 #if defined(PNG_MNG_FEATURES_SUPPORTED) 4233 /* undoes intrapixel differencing */4318 /* Undoes intrapixel differencing */ 4234 4319 void /* PRIVATE */ 4235 4320 png_do_read_intrapixel(png_row_infop row_info, png_bytep row) 4236 4321 { 4237 png_debug(1, "in png_do_read_intrapixel \n");4322 png_debug(1, "in png_do_read_intrapixel"); 4238 4323 if ( 4239 4324 #if defined(PNG_USELESS_TESTS_SUPPORTED) … … 4251 4336 if (row_info->color_type == PNG_COLOR_TYPE_RGB) 4252 4337 bytes_per_pixel = 3; 4338 4253 4339 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) 4254 4340 bytes_per_pixel = 4; 4341 4255 4342 else 4256 4343 return; … … 4269 4356 if (row_info->color_type == PNG_COLOR_TYPE_RGB) 4270 4357 bytes_per_pixel = 6; 4358 4271 4359 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) 4272 4360 bytes_per_pixel = 8; 4361 4273 4362 else 4274 4363 return; … … 4276 4365 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) 4277 4366 { 4278 png_uint_32 s0 = (*(rp ) << 8) | *(rp+1);4279 png_uint_32 s1 = (*(rp +2) << 8) | *(rp+3);4280 png_uint_32 s2 = (*(rp +4) << 8) | *(rp+5);4281 png_uint_32 red = (png_uint_32)((s0 +s1+65536L) & 0xffffL);4282 png_uint_32 blue = (png_uint_32)((s2 +s1+65536L) & 0xffffL);4367 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1); 4368 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3); 4369 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5); 4370 png_uint_32 red = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL); 4371 png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL); 4283 4372 *(rp ) = (png_byte)((red >> 8) & 0xff); 4284 4373 *(rp+1) = (png_byte)(red & 0xff);
Note:
See TracChangeset
for help on using the changeset viewer.