src/3rdparty/libpng/pngwtran.c
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
     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.4.0 [January 3, 2010]
     5  * Copyright (c) 1998-2009 Glenn Randers-Pehrson
     5  * Copyright (c) 1998-2010 Glenn Randers-Pehrson
     6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
     6  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
     7  * (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  *
     8  *
     9  * This code is released under the libpng license.
     9  * This code is released under the libpng license.
    10  * For conditions of distribution and use, see the disclaimer
    10  * For conditions of distribution and use, see the disclaimer
    11  * and license in png.h
    11  * and license in png.h
    12  */
    12  */
    13 
    13 
    14 #define PNG_INTERNAL
    14 #define PNG_NO_PEDANTIC_WARNINGS
    15 #include "png.h"
    15 #include "png.h"
    16 #ifdef PNG_WRITE_SUPPORTED
    16 #ifdef PNG_WRITE_SUPPORTED
       
    17 #include "pngpriv.h"
    17 
    18 
    18 /* 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
    19  * transformations is significant.
    20  * transformations is significant.
    20  */
    21  */
    21 void /* PRIVATE */
    22 void /* PRIVATE */
    24    png_debug(1, "in png_do_write_transformations");
    25    png_debug(1, "in png_do_write_transformations");
    25 
    26 
    26    if (png_ptr == NULL)
    27    if (png_ptr == NULL)
    27       return;
    28       return;
    28 
    29 
    29 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
    30 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
    30    if (png_ptr->transformations & PNG_USER_TRANSFORM)
    31    if (png_ptr->transformations & PNG_USER_TRANSFORM)
    31       if (png_ptr->write_user_transform_fn != NULL)
    32       if (png_ptr->write_user_transform_fn != NULL)
    32         (*(png_ptr->write_user_transform_fn)) /* User write transform function */
    33         (*(png_ptr->write_user_transform_fn)) /* User write transform function */
    33           (png_ptr,                    /* png_ptr */
    34           (png_ptr,                    /* png_ptr */
    34            &(png_ptr->row_info),       /* row_info:     */
    35            &(png_ptr->row_info),       /* row_info:     */
    38              /*  png_byte bit_depth;         bit depth of samples */
    39              /*  png_byte bit_depth;         bit depth of samples */
    39              /*  png_byte channels;          number of channels (1-4) */
    40              /*  png_byte channels;          number of channels (1-4) */
    40              /*  png_byte pixel_depth;       bits per pixel (depth*channels) */
    41              /*  png_byte pixel_depth;       bits per pixel (depth*channels) */
    41            png_ptr->row_buf + 1);      /* start of pixel data for row */
    42            png_ptr->row_buf + 1);      /* start of pixel data for row */
    42 #endif
    43 #endif
    43 #if defined(PNG_WRITE_FILLER_SUPPORTED)
    44 #ifdef PNG_WRITE_FILLER_SUPPORTED
    44    if (png_ptr->transformations & PNG_FILLER)
    45    if (png_ptr->transformations & PNG_FILLER)
    45       png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
    46       png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
    46          png_ptr->flags);
    47          png_ptr->flags);
    47 #endif
    48 #endif
    48 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
    49 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
    49    if (png_ptr->transformations & PNG_PACKSWAP)
    50    if (png_ptr->transformations & PNG_PACKSWAP)
    50       png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
    51       png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
    51 #endif
    52 #endif
    52 #if defined(PNG_WRITE_PACK_SUPPORTED)
    53 #ifdef PNG_WRITE_PACK_SUPPORTED
    53    if (png_ptr->transformations & PNG_PACK)
    54    if (png_ptr->transformations & PNG_PACK)
    54       png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
    55       png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
    55          (png_uint_32)png_ptr->bit_depth);
    56          (png_uint_32)png_ptr->bit_depth);
    56 #endif
    57 #endif
    57 #if defined(PNG_WRITE_SWAP_SUPPORTED)
    58 #ifdef PNG_WRITE_SWAP_SUPPORTED
    58    if (png_ptr->transformations & PNG_SWAP_BYTES)
    59    if (png_ptr->transformations & PNG_SWAP_BYTES)
    59       png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
    60       png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
    60 #endif
    61 #endif
    61 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
    62 #ifdef PNG_WRITE_SHIFT_SUPPORTED
    62    if (png_ptr->transformations & PNG_SHIFT)
    63    if (png_ptr->transformations & PNG_SHIFT)
    63       png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
    64       png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
    64          &(png_ptr->shift));
    65          &(png_ptr->shift));
    65 #endif
    66 #endif
    66 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
    67 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
    67    if (png_ptr->transformations & PNG_SWAP_ALPHA)
    68    if (png_ptr->transformations & PNG_SWAP_ALPHA)
    68       png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
    69       png_do_write_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
    69 #endif
    70 #endif
    70 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
    71 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
    71    if (png_ptr->transformations & PNG_INVERT_ALPHA)
    72    if (png_ptr->transformations & PNG_INVERT_ALPHA)
    72       png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
    73       png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
    73 #endif
    74 #endif
    74 #if defined(PNG_WRITE_BGR_SUPPORTED)
    75 #ifdef PNG_WRITE_BGR_SUPPORTED
    75    if (png_ptr->transformations & PNG_BGR)
    76    if (png_ptr->transformations & PNG_BGR)
    76       png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
    77       png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
    77 #endif
    78 #endif
    78 #if defined(PNG_WRITE_INVERT_SUPPORTED)
    79 #ifdef PNG_WRITE_INVERT_SUPPORTED
    79    if (png_ptr->transformations & PNG_INVERT_MONO)
    80    if (png_ptr->transformations & PNG_INVERT_MONO)
    80       png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
    81       png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
    81 #endif
    82 #endif
    82 }
    83 }
    83 
    84 
    84 #if defined(PNG_WRITE_PACK_SUPPORTED)
    85 #ifdef PNG_WRITE_PACK_SUPPORTED
    85 /* Pack pixels into bytes.  Pass the true bit depth in bit_depth.  The
    86 /* Pack pixels into bytes.  Pass the true bit depth in bit_depth.  The
    86  * row_info bit depth should be 8 (one pixel per byte).  The channels
    87  * row_info bit depth should be 8 (one pixel per byte).  The channels
    87  * should be 1 (this only happens on grayscale and paletted images).
    88  * should be 1 (this only happens on grayscale and paletted images).
    88  */
    89  */
    89 void /* PRIVATE */
    90 void /* PRIVATE */
    90 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
    91 png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
    91 {
    92 {
    92    png_debug(1, "in png_do_pack");
    93    png_debug(1, "in png_do_pack");
       
    94 
    93    if (row_info->bit_depth == 8 &&
    95    if (row_info->bit_depth == 8 &&
    94 #if defined(PNG_USELESS_TESTS_SUPPORTED)
       
    95        row != NULL && row_info != NULL &&
       
    96 #endif
       
    97       row_info->channels == 1)
    96       row_info->channels == 1)
    98    {
    97    {
    99       switch ((int)bit_depth)
    98       switch ((int)bit_depth)
   100       {
    99       {
   101          case 1:
   100          case 1:
   202          row_info->width);
   201          row_info->width);
   203    }
   202    }
   204 }
   203 }
   205 #endif
   204 #endif
   206 
   205 
   207 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
   206 #ifdef PNG_WRITE_SHIFT_SUPPORTED
   208 /* Shift pixel values to take advantage of whole range.  Pass the
   207 /* Shift pixel values to take advantage of whole range.  Pass the
   209  * true number of bits in bit_depth.  The row should be packed
   208  * true number of bits in bit_depth.  The row should be packed
   210  * according to row_info->bit_depth.  Thus, if you had a row of
   209  * according to row_info->bit_depth.  Thus, if you had a row of
   211  * bit depth 4, but the pixels only had values from 0 to 7, you
   210  * bit depth 4, but the pixels only had values from 0 to 7, you
   212  * would pass 3 as bit_depth, and this routine would translate the
   211  * would pass 3 as bit_depth, and this routine would translate the
   214  */
   213  */
   215 void /* PRIVATE */
   214 void /* PRIVATE */
   216 png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
   215 png_do_shift(png_row_infop row_info, png_bytep row, png_color_8p bit_depth)
   217 {
   216 {
   218    png_debug(1, "in png_do_shift");
   217    png_debug(1, "in png_do_shift");
   219 #if defined(PNG_USELESS_TESTS_SUPPORTED)
   218 
   220    if (row != NULL && row_info != NULL &&
       
   221 #else
       
   222    if (
   219    if (
   223 #endif
       
   224       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
   220       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
   225    {
   221    {
   226       int shift_start[4], shift_dec[4];
   222       int shift_start[4], shift_dec[4];
   227       int channels = 0;
   223       int channels = 0;
   228 
   224 
   333       }
   329       }
   334    }
   330    }
   335 }
   331 }
   336 #endif
   332 #endif
   337 
   333 
   338 #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
   334 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
   339 void /* PRIVATE */
   335 void /* PRIVATE */
   340 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
   336 png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
   341 {
   337 {
   342    png_debug(1, "in png_do_write_swap_alpha");
   338    png_debug(1, "in png_do_write_swap_alpha");
   343 #if defined(PNG_USELESS_TESTS_SUPPORTED)
   339 
   344    if (row != NULL && row_info != NULL)
       
   345 #endif
       
   346    {
   340    {
   347       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
   341       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
   348       {
   342       {
   349          /* This converts from ARGB to RGBA */
   343          /* This converts from ARGB to RGBA */
   350          if (row_info->bit_depth == 8)
   344          if (row_info->bit_depth == 8)
   421       }
   415       }
   422    }
   416    }
   423 }
   417 }
   424 #endif
   418 #endif
   425 
   419 
   426 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
   420 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
   427 void /* PRIVATE */
   421 void /* PRIVATE */
   428 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
   422 png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
   429 {
   423 {
   430    png_debug(1, "in png_do_write_invert_alpha");
   424    png_debug(1, "in png_do_write_invert_alpha");
   431 #if defined(PNG_USELESS_TESTS_SUPPORTED)
   425 
   432    if (row != NULL && row_info != NULL)
       
   433 #endif
       
   434    {
   426    {
   435       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
   427       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
   436       {
   428       {
   437          /* This inverts the alpha channel in RGBA */
   429          /* This inverts the alpha channel in RGBA */
   438          if (row_info->bit_depth == 8)
   430          if (row_info->bit_depth == 8)
   510       }
   502       }
   511    }
   503    }
   512 }
   504 }
   513 #endif
   505 #endif
   514 
   506 
   515 #if defined(PNG_MNG_FEATURES_SUPPORTED)
   507 #ifdef PNG_MNG_FEATURES_SUPPORTED
   516 /* Undoes intrapixel differencing  */
   508 /* Undoes intrapixel differencing  */
   517 void /* PRIVATE */
   509 void /* PRIVATE */
   518 png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
   510 png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
   519 {
   511 {
   520    png_debug(1, "in png_do_write_intrapixel");
   512    png_debug(1, "in png_do_write_intrapixel");
       
   513 
   521    if (
   514    if (
   522 #if defined(PNG_USELESS_TESTS_SUPPORTED)
       
   523        row != NULL && row_info != NULL &&
       
   524 #endif
       
   525        (row_info->color_type & PNG_COLOR_MASK_COLOR))
   515        (row_info->color_type & PNG_COLOR_MASK_COLOR))
   526    {
   516    {
   527       int bytes_per_pixel;
   517       int bytes_per_pixel;
   528       png_uint_32 row_width = row_info->width;
   518       png_uint_32 row_width = row_info->width;
   529       if (row_info->bit_depth == 8)
   519       if (row_info->bit_depth == 8)