media/libpng/pngtrans.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libpng/pngtrans.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,843 @@
     1.4 +
     1.5 +/* pngtrans.c - transforms the data in a row (used by both readers and writers)
     1.6 + *
     1.7 + * Last changed in libpng 1.6.9 [February 6, 2014]
     1.8 + * Copyright (c) 1998-2014 Glenn Randers-Pehrson
     1.9 + * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
    1.10 + * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
    1.11 + *
    1.12 + * This code is released under the libpng license.
    1.13 + * For conditions of distribution and use, see the disclaimer
    1.14 + * and license in png.h
    1.15 + */
    1.16 +
    1.17 +#include "pngpriv.h"
    1.18 +
    1.19 +#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
    1.20 +
    1.21 +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
    1.22 +/* Turn on BGR-to-RGB mapping */
    1.23 +void PNGAPI
    1.24 +png_set_bgr(png_structrp png_ptr)
    1.25 +{
    1.26 +   png_debug(1, "in png_set_bgr");
    1.27 +
    1.28 +   if (png_ptr == NULL)
    1.29 +      return;
    1.30 +
    1.31 +   png_ptr->transformations |= PNG_BGR;
    1.32 +}
    1.33 +#endif
    1.34 +
    1.35 +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
    1.36 +/* Turn on 16 bit byte swapping */
    1.37 +void PNGAPI
    1.38 +png_set_swap(png_structrp png_ptr)
    1.39 +{
    1.40 +   png_debug(1, "in png_set_swap");
    1.41 +
    1.42 +   if (png_ptr == NULL)
    1.43 +      return;
    1.44 +
    1.45 +   if (png_ptr->bit_depth == 16)
    1.46 +      png_ptr->transformations |= PNG_SWAP_BYTES;
    1.47 +}
    1.48 +#endif
    1.49 +
    1.50 +#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
    1.51 +/* Turn on pixel packing */
    1.52 +void PNGAPI
    1.53 +png_set_packing(png_structrp png_ptr)
    1.54 +{
    1.55 +   png_debug(1, "in png_set_packing");
    1.56 +
    1.57 +   if (png_ptr == NULL)
    1.58 +      return;
    1.59 +
    1.60 +   if (png_ptr->bit_depth < 8)
    1.61 +   {
    1.62 +      png_ptr->transformations |= PNG_PACK;
    1.63 +#     ifdef PNG_WRITE_SUPPORTED
    1.64 +         png_ptr->usr_bit_depth = 8;
    1.65 +#     endif
    1.66 +   }
    1.67 +}
    1.68 +#endif
    1.69 +
    1.70 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
    1.71 +/* Turn on packed pixel swapping */
    1.72 +void PNGAPI
    1.73 +png_set_packswap(png_structrp png_ptr)
    1.74 +{
    1.75 +   png_debug(1, "in png_set_packswap");
    1.76 +
    1.77 +   if (png_ptr == NULL)
    1.78 +      return;
    1.79 +
    1.80 +   if (png_ptr->bit_depth < 8)
    1.81 +      png_ptr->transformations |= PNG_PACKSWAP;
    1.82 +}
    1.83 +#endif
    1.84 +
    1.85 +#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
    1.86 +void PNGAPI
    1.87 +png_set_shift(png_structrp png_ptr, png_const_color_8p true_bits)
    1.88 +{
    1.89 +   png_debug(1, "in png_set_shift");
    1.90 +
    1.91 +   if (png_ptr == NULL)
    1.92 +      return;
    1.93 +
    1.94 +   png_ptr->transformations |= PNG_SHIFT;
    1.95 +   png_ptr->shift = *true_bits;
    1.96 +}
    1.97 +#endif
    1.98 +
    1.99 +#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
   1.100 +    defined(PNG_WRITE_INTERLACING_SUPPORTED)
   1.101 +int PNGAPI
   1.102 +png_set_interlace_handling(png_structrp png_ptr)
   1.103 +{
   1.104 +   png_debug(1, "in png_set_interlace handling");
   1.105 +
   1.106 +   if (png_ptr && png_ptr->interlaced)
   1.107 +   {
   1.108 +      png_ptr->transformations |= PNG_INTERLACE;
   1.109 +      return (7);
   1.110 +   }
   1.111 +
   1.112 +   return (1);
   1.113 +}
   1.114 +#endif
   1.115 +
   1.116 +#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
   1.117 +/* Add a filler byte on read, or remove a filler or alpha byte on write.
   1.118 + * The filler type has changed in v0.95 to allow future 2-byte fillers
   1.119 + * for 48-bit input data, as well as to avoid problems with some compilers
   1.120 + * that don't like bytes as parameters.
   1.121 + */
   1.122 +void PNGAPI
   1.123 +png_set_filler(png_structrp png_ptr, png_uint_32 filler, int filler_loc)
   1.124 +{
   1.125 +   png_debug(1, "in png_set_filler");
   1.126 +
   1.127 +   if (png_ptr == NULL)
   1.128 +      return;
   1.129 +
   1.130 +   /* In libpng 1.6 it is possible to determine whether this is a read or write
   1.131 +    * operation and therefore to do more checking here for a valid call.
   1.132 +    */
   1.133 +   if (png_ptr->mode & PNG_IS_READ_STRUCT)
   1.134 +   {
   1.135 +#     ifdef PNG_READ_FILLER_SUPPORTED
   1.136 +         /* On read png_set_filler is always valid, regardless of the base PNG
   1.137 +          * format, because other transformations can give a format where the
   1.138 +          * filler code can execute (basically an 8 or 16-bit component RGB or G
   1.139 +          * format.)
   1.140 +          *
   1.141 +          * NOTE: usr_channels is not used by the read code!  (This has led to
   1.142 +          * confusion in the past.)  The filler is only used in the read code.
   1.143 +          */
   1.144 +         png_ptr->filler = (png_uint_16)filler;
   1.145 +#     else
   1.146 +         png_app_error(png_ptr, "png_set_filler not supported on read");
   1.147 +         PNG_UNUSED(filler) /* not used in the write case */
   1.148 +         return;
   1.149 +#     endif
   1.150 +   }
   1.151 +
   1.152 +   else /* write */
   1.153 +   {
   1.154 +#     ifdef PNG_WRITE_FILLER_SUPPORTED
   1.155 +         /* On write the usr_channels parameter must be set correctly at the
   1.156 +          * start to record the number of channels in the app-supplied data.
   1.157 +          */
   1.158 +         switch (png_ptr->color_type)
   1.159 +         {
   1.160 +            case PNG_COLOR_TYPE_RGB:
   1.161 +               png_ptr->usr_channels = 4;
   1.162 +               break;
   1.163 +
   1.164 +            case PNG_COLOR_TYPE_GRAY:
   1.165 +               if (png_ptr->bit_depth >= 8)
   1.166 +               {
   1.167 +                  png_ptr->usr_channels = 2;
   1.168 +                  break;
   1.169 +               }
   1.170 +
   1.171 +               else
   1.172 +               {
   1.173 +                  /* There simply isn't any code in libpng to strip out bits
   1.174 +                   * from bytes when the components are less than a byte in
   1.175 +                   * size!
   1.176 +                   */
   1.177 +                  png_app_error(png_ptr,
   1.178 +                     "png_set_filler is invalid for low bit depth gray output");
   1.179 +                  return;
   1.180 +               }
   1.181 +
   1.182 +            default:
   1.183 +               png_app_error(png_ptr,
   1.184 +                  "png_set_filler: inappropriate color type");
   1.185 +               return;
   1.186 +         }
   1.187 +#     else
   1.188 +         png_app_error(png_ptr, "png_set_filler not supported on write");
   1.189 +         return;
   1.190 +#     endif
   1.191 +   }
   1.192 +
   1.193 +   /* Here on success - libpng supports the operation, set the transformation
   1.194 +    * and the flag to say where the filler channel is.
   1.195 +    */
   1.196 +   png_ptr->transformations |= PNG_FILLER;
   1.197 +
   1.198 +   if (filler_loc == PNG_FILLER_AFTER)
   1.199 +      png_ptr->flags |= PNG_FLAG_FILLER_AFTER;
   1.200 +
   1.201 +   else
   1.202 +      png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;
   1.203 +}
   1.204 +
   1.205 +/* Added to libpng-1.2.7 */
   1.206 +void PNGAPI
   1.207 +png_set_add_alpha(png_structrp png_ptr, png_uint_32 filler, int filler_loc)
   1.208 +{
   1.209 +   png_debug(1, "in png_set_add_alpha");
   1.210 +
   1.211 +   if (png_ptr == NULL)
   1.212 +      return;
   1.213 +
   1.214 +   png_set_filler(png_ptr, filler, filler_loc);
   1.215 +   /* The above may fail to do anything. */
   1.216 +   if (png_ptr->transformations & PNG_FILLER)
   1.217 +      png_ptr->transformations |= PNG_ADD_ALPHA;
   1.218 +}
   1.219 +
   1.220 +#endif
   1.221 +
   1.222 +#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \
   1.223 +    defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
   1.224 +void PNGAPI
   1.225 +png_set_swap_alpha(png_structrp png_ptr)
   1.226 +{
   1.227 +   png_debug(1, "in png_set_swap_alpha");
   1.228 +
   1.229 +   if (png_ptr == NULL)
   1.230 +      return;
   1.231 +
   1.232 +   png_ptr->transformations |= PNG_SWAP_ALPHA;
   1.233 +}
   1.234 +#endif
   1.235 +
   1.236 +#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \
   1.237 +    defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
   1.238 +void PNGAPI
   1.239 +png_set_invert_alpha(png_structrp png_ptr)
   1.240 +{
   1.241 +   png_debug(1, "in png_set_invert_alpha");
   1.242 +
   1.243 +   if (png_ptr == NULL)
   1.244 +      return;
   1.245 +
   1.246 +   png_ptr->transformations |= PNG_INVERT_ALPHA;
   1.247 +}
   1.248 +#endif
   1.249 +
   1.250 +#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED)
   1.251 +void PNGAPI
   1.252 +png_set_invert_mono(png_structrp png_ptr)
   1.253 +{
   1.254 +   png_debug(1, "in png_set_invert_mono");
   1.255 +
   1.256 +   if (png_ptr == NULL)
   1.257 +      return;
   1.258 +
   1.259 +   png_ptr->transformations |= PNG_INVERT_MONO;
   1.260 +}
   1.261 +
   1.262 +/* Invert monochrome grayscale data */
   1.263 +void /* PRIVATE */
   1.264 +png_do_invert(png_row_infop row_info, png_bytep row)
   1.265 +{
   1.266 +   png_debug(1, "in png_do_invert");
   1.267 +
   1.268 +  /* This test removed from libpng version 1.0.13 and 1.2.0:
   1.269 +   *   if (row_info->bit_depth == 1 &&
   1.270 +   */
   1.271 +   if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
   1.272 +   {
   1.273 +      png_bytep rp = row;
   1.274 +      png_size_t i;
   1.275 +      png_size_t istop = row_info->rowbytes;
   1.276 +
   1.277 +      for (i = 0; i < istop; i++)
   1.278 +      {
   1.279 +         *rp = (png_byte)(~(*rp));
   1.280 +         rp++;
   1.281 +      }
   1.282 +   }
   1.283 +
   1.284 +   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
   1.285 +      row_info->bit_depth == 8)
   1.286 +   {
   1.287 +      png_bytep rp = row;
   1.288 +      png_size_t i;
   1.289 +      png_size_t istop = row_info->rowbytes;
   1.290 +
   1.291 +      for (i = 0; i < istop; i += 2)
   1.292 +      {
   1.293 +         *rp = (png_byte)(~(*rp));
   1.294 +         rp += 2;
   1.295 +      }
   1.296 +   }
   1.297 +
   1.298 +#ifdef PNG_16BIT_SUPPORTED
   1.299 +   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
   1.300 +      row_info->bit_depth == 16)
   1.301 +   {
   1.302 +      png_bytep rp = row;
   1.303 +      png_size_t i;
   1.304 +      png_size_t istop = row_info->rowbytes;
   1.305 +
   1.306 +      for (i = 0; i < istop; i += 4)
   1.307 +      {
   1.308 +         *rp = (png_byte)(~(*rp));
   1.309 +         *(rp + 1) = (png_byte)(~(*(rp + 1)));
   1.310 +         rp += 4;
   1.311 +      }
   1.312 +   }
   1.313 +#endif
   1.314 +}
   1.315 +#endif
   1.316 +
   1.317 +#ifdef PNG_16BIT_SUPPORTED
   1.318 +#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
   1.319 +/* Swaps byte order on 16 bit depth images */
   1.320 +void /* PRIVATE */
   1.321 +png_do_swap(png_row_infop row_info, png_bytep row)
   1.322 +{
   1.323 +   png_debug(1, "in png_do_swap");
   1.324 +
   1.325 +   if (row_info->bit_depth == 16)
   1.326 +   {
   1.327 +      png_bytep rp = row;
   1.328 +      png_uint_32 i;
   1.329 +      png_uint_32 istop= row_info->width * row_info->channels;
   1.330 +
   1.331 +      for (i = 0; i < istop; i++, rp += 2)
   1.332 +      {
   1.333 +         png_byte t = *rp;
   1.334 +         *rp = *(rp + 1);
   1.335 +         *(rp + 1) = t;
   1.336 +      }
   1.337 +   }
   1.338 +}
   1.339 +#endif
   1.340 +#endif
   1.341 +
   1.342 +#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED)
   1.343 +static PNG_CONST png_byte onebppswaptable[256] = {
   1.344 +   0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
   1.345 +   0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
   1.346 +   0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
   1.347 +   0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8,
   1.348 +   0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4,
   1.349 +   0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4,
   1.350 +   0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC,
   1.351 +   0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC,
   1.352 +   0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2,
   1.353 +   0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2,
   1.354 +   0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA,
   1.355 +   0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA,
   1.356 +   0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6,
   1.357 +   0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6,
   1.358 +   0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE,
   1.359 +   0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE,
   1.360 +   0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1,
   1.361 +   0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1,
   1.362 +   0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9,
   1.363 +   0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9,
   1.364 +   0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5,
   1.365 +   0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5,
   1.366 +   0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED,
   1.367 +   0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD,
   1.368 +   0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3,
   1.369 +   0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3,
   1.370 +   0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB,
   1.371 +   0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB,
   1.372 +   0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7,
   1.373 +   0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7,
   1.374 +   0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF,
   1.375 +   0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
   1.376 +};
   1.377 +
   1.378 +static PNG_CONST png_byte twobppswaptable[256] = {
   1.379 +   0x00, 0x40, 0x80, 0xC0, 0x10, 0x50, 0x90, 0xD0,
   1.380 +   0x20, 0x60, 0xA0, 0xE0, 0x30, 0x70, 0xB0, 0xF0,
   1.381 +   0x04, 0x44, 0x84, 0xC4, 0x14, 0x54, 0x94, 0xD4,
   1.382 +   0x24, 0x64, 0xA4, 0xE4, 0x34, 0x74, 0xB4, 0xF4,
   1.383 +   0x08, 0x48, 0x88, 0xC8, 0x18, 0x58, 0x98, 0xD8,
   1.384 +   0x28, 0x68, 0xA8, 0xE8, 0x38, 0x78, 0xB8, 0xF8,
   1.385 +   0x0C, 0x4C, 0x8C, 0xCC, 0x1C, 0x5C, 0x9C, 0xDC,
   1.386 +   0x2C, 0x6C, 0xAC, 0xEC, 0x3C, 0x7C, 0xBC, 0xFC,
   1.387 +   0x01, 0x41, 0x81, 0xC1, 0x11, 0x51, 0x91, 0xD1,
   1.388 +   0x21, 0x61, 0xA1, 0xE1, 0x31, 0x71, 0xB1, 0xF1,
   1.389 +   0x05, 0x45, 0x85, 0xC5, 0x15, 0x55, 0x95, 0xD5,
   1.390 +   0x25, 0x65, 0xA5, 0xE5, 0x35, 0x75, 0xB5, 0xF5,
   1.391 +   0x09, 0x49, 0x89, 0xC9, 0x19, 0x59, 0x99, 0xD9,
   1.392 +   0x29, 0x69, 0xA9, 0xE9, 0x39, 0x79, 0xB9, 0xF9,
   1.393 +   0x0D, 0x4D, 0x8D, 0xCD, 0x1D, 0x5D, 0x9D, 0xDD,
   1.394 +   0x2D, 0x6D, 0xAD, 0xED, 0x3D, 0x7D, 0xBD, 0xFD,
   1.395 +   0x02, 0x42, 0x82, 0xC2, 0x12, 0x52, 0x92, 0xD2,
   1.396 +   0x22, 0x62, 0xA2, 0xE2, 0x32, 0x72, 0xB2, 0xF2,
   1.397 +   0x06, 0x46, 0x86, 0xC6, 0x16, 0x56, 0x96, 0xD6,
   1.398 +   0x26, 0x66, 0xA6, 0xE6, 0x36, 0x76, 0xB6, 0xF6,
   1.399 +   0x0A, 0x4A, 0x8A, 0xCA, 0x1A, 0x5A, 0x9A, 0xDA,
   1.400 +   0x2A, 0x6A, 0xAA, 0xEA, 0x3A, 0x7A, 0xBA, 0xFA,
   1.401 +   0x0E, 0x4E, 0x8E, 0xCE, 0x1E, 0x5E, 0x9E, 0xDE,
   1.402 +   0x2E, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE,
   1.403 +   0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3,
   1.404 +   0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3,
   1.405 +   0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7,
   1.406 +   0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7,
   1.407 +   0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB,
   1.408 +   0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB,
   1.409 +   0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF,
   1.410 +   0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF
   1.411 +};
   1.412 +
   1.413 +static PNG_CONST png_byte fourbppswaptable[256] = {
   1.414 +   0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
   1.415 +   0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0,
   1.416 +   0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71,
   1.417 +   0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1,
   1.418 +   0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72,
   1.419 +   0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2,
   1.420 +   0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73,
   1.421 +   0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3,
   1.422 +   0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74,
   1.423 +   0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4,
   1.424 +   0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75,
   1.425 +   0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5,
   1.426 +   0x06, 0x16, 0x26, 0x36, 0x46, 0x56, 0x66, 0x76,
   1.427 +   0x86, 0x96, 0xA6, 0xB6, 0xC6, 0xD6, 0xE6, 0xF6,
   1.428 +   0x07, 0x17, 0x27, 0x37, 0x47, 0x57, 0x67, 0x77,
   1.429 +   0x87, 0x97, 0xA7, 0xB7, 0xC7, 0xD7, 0xE7, 0xF7,
   1.430 +   0x08, 0x18, 0x28, 0x38, 0x48, 0x58, 0x68, 0x78,
   1.431 +   0x88, 0x98, 0xA8, 0xB8, 0xC8, 0xD8, 0xE8, 0xF8,
   1.432 +   0x09, 0x19, 0x29, 0x39, 0x49, 0x59, 0x69, 0x79,
   1.433 +   0x89, 0x99, 0xA9, 0xB9, 0xC9, 0xD9, 0xE9, 0xF9,
   1.434 +   0x0A, 0x1A, 0x2A, 0x3A, 0x4A, 0x5A, 0x6A, 0x7A,
   1.435 +   0x8A, 0x9A, 0xAA, 0xBA, 0xCA, 0xDA, 0xEA, 0xFA,
   1.436 +   0x0B, 0x1B, 0x2B, 0x3B, 0x4B, 0x5B, 0x6B, 0x7B,
   1.437 +   0x8B, 0x9B, 0xAB, 0xBB, 0xCB, 0xDB, 0xEB, 0xFB,
   1.438 +   0x0C, 0x1C, 0x2C, 0x3C, 0x4C, 0x5C, 0x6C, 0x7C,
   1.439 +   0x8C, 0x9C, 0xAC, 0xBC, 0xCC, 0xDC, 0xEC, 0xFC,
   1.440 +   0x0D, 0x1D, 0x2D, 0x3D, 0x4D, 0x5D, 0x6D, 0x7D,
   1.441 +   0x8D, 0x9D, 0xAD, 0xBD, 0xCD, 0xDD, 0xED, 0xFD,
   1.442 +   0x0E, 0x1E, 0x2E, 0x3E, 0x4E, 0x5E, 0x6E, 0x7E,
   1.443 +   0x8E, 0x9E, 0xAE, 0xBE, 0xCE, 0xDE, 0xEE, 0xFE,
   1.444 +   0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F, 0x7F,
   1.445 +   0x8F, 0x9F, 0xAF, 0xBF, 0xCF, 0xDF, 0xEF, 0xFF
   1.446 +};
   1.447 +
   1.448 +/* Swaps pixel packing order within bytes */
   1.449 +void /* PRIVATE */
   1.450 +png_do_packswap(png_row_infop row_info, png_bytep row)
   1.451 +{
   1.452 +   png_debug(1, "in png_do_packswap");
   1.453 +
   1.454 +   if (row_info->bit_depth < 8)
   1.455 +   {
   1.456 +      png_bytep rp;
   1.457 +      png_const_bytep end, table;
   1.458 +
   1.459 +      end = row + row_info->rowbytes;
   1.460 +
   1.461 +      if (row_info->bit_depth == 1)
   1.462 +         table = onebppswaptable;
   1.463 +
   1.464 +      else if (row_info->bit_depth == 2)
   1.465 +         table = twobppswaptable;
   1.466 +
   1.467 +      else if (row_info->bit_depth == 4)
   1.468 +         table = fourbppswaptable;
   1.469 +
   1.470 +      else
   1.471 +         return;
   1.472 +
   1.473 +      for (rp = row; rp < end; rp++)
   1.474 +         *rp = table[*rp];
   1.475 +   }
   1.476 +}
   1.477 +#endif /* PNG_READ_PACKSWAP_SUPPORTED or PNG_WRITE_PACKSWAP_SUPPORTED */
   1.478 +
   1.479 +#if defined(PNG_WRITE_FILLER_SUPPORTED) || \
   1.480 +    defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
   1.481 +/* Remove a channel - this used to be 'png_do_strip_filler' but it used a
   1.482 + * somewhat weird combination of flags to determine what to do.  All the calls
   1.483 + * to png_do_strip_filler are changed in 1.5.2 to call this instead with the
   1.484 + * correct arguments.
   1.485 + *
   1.486 + * The routine isn't general - the channel must be the channel at the start or
   1.487 + * end (not in the middle) of each pixel.
   1.488 + */
   1.489 +void /* PRIVATE */
   1.490 +png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start)
   1.491 +{
   1.492 +   png_bytep sp = row; /* source pointer */
   1.493 +   png_bytep dp = row; /* destination pointer */
   1.494 +   png_bytep ep = row + row_info->rowbytes; /* One beyond end of row */
   1.495 +
   1.496 +   /* At the start sp will point to the first byte to copy and dp to where
   1.497 +    * it is copied to.  ep always points just beyond the end of the row, so
   1.498 +    * the loop simply copies (channels-1) channels until sp reaches ep.
   1.499 +    *
   1.500 +    * at_start:        0 -- convert AG, XG, ARGB, XRGB, AAGG, XXGG, etc.
   1.501 +    *            nonzero -- convert GA, GX, RGBA, RGBX, GGAA, RRGGBBXX, etc.
   1.502 +    */
   1.503 +
   1.504 +   /* GA, GX, XG cases */
   1.505 +   if (row_info->channels == 2)
   1.506 +   {
   1.507 +      if (row_info->bit_depth == 8)
   1.508 +      {
   1.509 +         if (at_start) /* Skip initial filler */
   1.510 +            ++sp;
   1.511 +         else          /* Skip initial channel and, for sp, the filler */
   1.512 +            sp += 2, ++dp;
   1.513 +
   1.514 +         /* For a 1 pixel wide image there is nothing to do */
   1.515 +         while (sp < ep)
   1.516 +            *dp++ = *sp, sp += 2;
   1.517 +
   1.518 +         row_info->pixel_depth = 8;
   1.519 +      }
   1.520 +
   1.521 +      else if (row_info->bit_depth == 16)
   1.522 +      {
   1.523 +         if (at_start) /* Skip initial filler */
   1.524 +            sp += 2;
   1.525 +         else          /* Skip initial channel and, for sp, the filler */
   1.526 +            sp += 4, dp += 2;
   1.527 +
   1.528 +         while (sp < ep)
   1.529 +            *dp++ = *sp++, *dp++ = *sp, sp += 3;
   1.530 +
   1.531 +         row_info->pixel_depth = 16;
   1.532 +      }
   1.533 +
   1.534 +      else
   1.535 +         return; /* bad bit depth */
   1.536 +
   1.537 +      row_info->channels = 1;
   1.538 +
   1.539 +      /* Finally fix the color type if it records an alpha channel */
   1.540 +      if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
   1.541 +         row_info->color_type = PNG_COLOR_TYPE_GRAY;
   1.542 +   }
   1.543 +
   1.544 +   /* RGBA, RGBX, XRGB cases */
   1.545 +   else if (row_info->channels == 4)
   1.546 +   {
   1.547 +      if (row_info->bit_depth == 8)
   1.548 +      {
   1.549 +         if (at_start) /* Skip initial filler */
   1.550 +            ++sp;
   1.551 +         else          /* Skip initial channels and, for sp, the filler */
   1.552 +            sp += 4, dp += 3;
   1.553 +
   1.554 +         /* Note that the loop adds 3 to dp and 4 to sp each time. */
   1.555 +         while (sp < ep)
   1.556 +            *dp++ = *sp++, *dp++ = *sp++, *dp++ = *sp, sp += 2;
   1.557 +
   1.558 +         row_info->pixel_depth = 24;
   1.559 +      }
   1.560 +
   1.561 +      else if (row_info->bit_depth == 16)
   1.562 +      {
   1.563 +         if (at_start) /* Skip initial filler */
   1.564 +            sp += 2;
   1.565 +         else          /* Skip initial channels and, for sp, the filler */
   1.566 +            sp += 8, dp += 6;
   1.567 +
   1.568 +         while (sp < ep)
   1.569 +         {
   1.570 +            /* Copy 6 bytes, skip 2 */
   1.571 +            *dp++ = *sp++, *dp++ = *sp++;
   1.572 +            *dp++ = *sp++, *dp++ = *sp++;
   1.573 +            *dp++ = *sp++, *dp++ = *sp, sp += 3;
   1.574 +         }
   1.575 +
   1.576 +         row_info->pixel_depth = 48;
   1.577 +      }
   1.578 +
   1.579 +      else
   1.580 +         return; /* bad bit depth */
   1.581 +
   1.582 +      row_info->channels = 3;
   1.583 +
   1.584 +      /* Finally fix the color type if it records an alpha channel */
   1.585 +      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
   1.586 +         row_info->color_type = PNG_COLOR_TYPE_RGB;
   1.587 +   }
   1.588 +
   1.589 +   else
   1.590 +      return; /* The filler channel has gone already */
   1.591 +
   1.592 +   /* Fix the rowbytes value. */
   1.593 +   row_info->rowbytes = dp-row;
   1.594 +}
   1.595 +#endif
   1.596 +
   1.597 +#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
   1.598 +/* Swaps red and blue bytes within a pixel */
   1.599 +void /* PRIVATE */
   1.600 +png_do_bgr(png_row_infop row_info, png_bytep row)
   1.601 +{
   1.602 +   png_debug(1, "in png_do_bgr");
   1.603 +
   1.604 +   if ((row_info->color_type & PNG_COLOR_MASK_COLOR))
   1.605 +   {
   1.606 +      png_uint_32 row_width = row_info->width;
   1.607 +      if (row_info->bit_depth == 8)
   1.608 +      {
   1.609 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
   1.610 +         {
   1.611 +            png_bytep rp;
   1.612 +            png_uint_32 i;
   1.613 +
   1.614 +            for (i = 0, rp = row; i < row_width; i++, rp += 3)
   1.615 +            {
   1.616 +               png_byte save = *rp;
   1.617 +               *rp = *(rp + 2);
   1.618 +               *(rp + 2) = save;
   1.619 +            }
   1.620 +         }
   1.621 +
   1.622 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
   1.623 +         {
   1.624 +            png_bytep rp;
   1.625 +            png_uint_32 i;
   1.626 +
   1.627 +            for (i = 0, rp = row; i < row_width; i++, rp += 4)
   1.628 +            {
   1.629 +               png_byte save = *rp;
   1.630 +               *rp = *(rp + 2);
   1.631 +               *(rp + 2) = save;
   1.632 +            }
   1.633 +         }
   1.634 +      }
   1.635 +
   1.636 +#ifdef PNG_16BIT_SUPPORTED
   1.637 +      else if (row_info->bit_depth == 16)
   1.638 +      {
   1.639 +         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
   1.640 +         {
   1.641 +            png_bytep rp;
   1.642 +            png_uint_32 i;
   1.643 +
   1.644 +            for (i = 0, rp = row; i < row_width; i++, rp += 6)
   1.645 +            {
   1.646 +               png_byte save = *rp;
   1.647 +               *rp = *(rp + 4);
   1.648 +               *(rp + 4) = save;
   1.649 +               save = *(rp + 1);
   1.650 +               *(rp + 1) = *(rp + 5);
   1.651 +               *(rp + 5) = save;
   1.652 +            }
   1.653 +         }
   1.654 +
   1.655 +         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
   1.656 +         {
   1.657 +            png_bytep rp;
   1.658 +            png_uint_32 i;
   1.659 +
   1.660 +            for (i = 0, rp = row; i < row_width; i++, rp += 8)
   1.661 +            {
   1.662 +               png_byte save = *rp;
   1.663 +               *rp = *(rp + 4);
   1.664 +               *(rp + 4) = save;
   1.665 +               save = *(rp + 1);
   1.666 +               *(rp + 1) = *(rp + 5);
   1.667 +               *(rp + 5) = save;
   1.668 +            }
   1.669 +         }
   1.670 +      }
   1.671 +#endif
   1.672 +   }
   1.673 +}
   1.674 +#endif /* PNG_READ_BGR_SUPPORTED or PNG_WRITE_BGR_SUPPORTED */
   1.675 +
   1.676 +#if defined(PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED) || \
   1.677 +    defined(PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED)
   1.678 +/* Added at libpng-1.5.10 */
   1.679 +void /* PRIVATE */
   1.680 +png_do_check_palette_indexes(png_structrp png_ptr, png_row_infop row_info)
   1.681 +{
   1.682 +   if (png_ptr->num_palette < (1 << row_info->bit_depth) &&
   1.683 +      png_ptr->num_palette > 0) /* num_palette can be 0 in MNG files */
   1.684 +   {
   1.685 +      /* Calculations moved outside switch in an attempt to stop different
   1.686 +       * compiler warnings.  'padding' is in *bits* within the last byte, it is
   1.687 +       * an 'int' because pixel_depth becomes an 'int' in the expression below,
   1.688 +       * and this calculation is used because it avoids warnings that other
   1.689 +       * forms produced on either GCC or MSVC.
   1.690 +       */
   1.691 +      int padding = (-row_info->pixel_depth * row_info->width) & 7;
   1.692 +      png_bytep rp = png_ptr->row_buf + row_info->rowbytes;
   1.693 +
   1.694 +      switch (row_info->bit_depth)
   1.695 +      {
   1.696 +         case 1:
   1.697 +         {
   1.698 +            /* in this case, all bytes must be 0 so we don't need
   1.699 +             * to unpack the pixels except for the rightmost one.
   1.700 +             */
   1.701 +            for (; rp > png_ptr->row_buf; rp--)
   1.702 +            {
   1.703 +              if (*rp >> padding != 0)
   1.704 +                 png_ptr->num_palette_max = 1;
   1.705 +              padding = 0;
   1.706 +            }
   1.707 +
   1.708 +            break;
   1.709 +         }
   1.710 +
   1.711 +         case 2:
   1.712 +         {
   1.713 +            for (; rp > png_ptr->row_buf; rp--)
   1.714 +            {
   1.715 +              int i = ((*rp >> padding) & 0x03);
   1.716 +
   1.717 +              if (i > png_ptr->num_palette_max)
   1.718 +                 png_ptr->num_palette_max = i;
   1.719 +
   1.720 +              i = (((*rp >> padding) >> 2) & 0x03);
   1.721 +
   1.722 +              if (i > png_ptr->num_palette_max)
   1.723 +                 png_ptr->num_palette_max = i;
   1.724 +
   1.725 +              i = (((*rp >> padding) >> 4) & 0x03);
   1.726 +
   1.727 +              if (i > png_ptr->num_palette_max)
   1.728 +                 png_ptr->num_palette_max = i;
   1.729 +
   1.730 +              i = (((*rp >> padding) >> 6) & 0x03);
   1.731 +
   1.732 +              if (i > png_ptr->num_palette_max)
   1.733 +                 png_ptr->num_palette_max = i;
   1.734 +
   1.735 +              padding = 0;
   1.736 +            }
   1.737 +
   1.738 +            break;
   1.739 +         }
   1.740 +
   1.741 +         case 4:
   1.742 +         {
   1.743 +            for (; rp > png_ptr->row_buf; rp--)
   1.744 +            {
   1.745 +              int i = ((*rp >> padding) & 0x0f);
   1.746 +
   1.747 +              if (i > png_ptr->num_palette_max)
   1.748 +                 png_ptr->num_palette_max = i;
   1.749 +
   1.750 +              i = (((*rp >> padding) >> 4) & 0x0f);
   1.751 +
   1.752 +              if (i > png_ptr->num_palette_max)
   1.753 +                 png_ptr->num_palette_max = i;
   1.754 +
   1.755 +              padding = 0;
   1.756 +            }
   1.757 +
   1.758 +            break;
   1.759 +         }
   1.760 +
   1.761 +         case 8:
   1.762 +         {
   1.763 +            for (; rp > png_ptr->row_buf; rp--)
   1.764 +            {
   1.765 +               if (*rp > png_ptr->num_palette_max)
   1.766 +                  png_ptr->num_palette_max = (int) *rp;
   1.767 +            }
   1.768 +
   1.769 +            break;
   1.770 +         }
   1.771 +
   1.772 +         default:
   1.773 +            break;
   1.774 +      }
   1.775 +   }
   1.776 +}
   1.777 +#endif /* PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED */
   1.778 +
   1.779 +#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
   1.780 +    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
   1.781 +#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
   1.782 +void PNGAPI
   1.783 +png_set_user_transform_info(png_structrp png_ptr, png_voidp
   1.784 +   user_transform_ptr, int user_transform_depth, int user_transform_channels)
   1.785 +{
   1.786 +   png_debug(1, "in png_set_user_transform_info");
   1.787 +
   1.788 +   if (png_ptr == NULL)
   1.789 +      return;
   1.790 +
   1.791 +#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
   1.792 +   if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
   1.793 +      (png_ptr->flags & PNG_FLAG_ROW_INIT) != 0)
   1.794 +   {
   1.795 +      png_app_error(png_ptr,
   1.796 +            "info change after png_start_read_image or png_read_update_info");
   1.797 +      return;
   1.798 +   }
   1.799 +#endif
   1.800 +
   1.801 +   png_ptr->user_transform_ptr = user_transform_ptr;
   1.802 +   png_ptr->user_transform_depth = (png_byte)user_transform_depth;
   1.803 +   png_ptr->user_transform_channels = (png_byte)user_transform_channels;
   1.804 +}
   1.805 +#endif
   1.806 +
   1.807 +/* This function returns a pointer to the user_transform_ptr associated with
   1.808 + * the user transform functions.  The application should free any memory
   1.809 + * associated with this pointer before png_write_destroy and png_read_destroy
   1.810 + * are called.
   1.811 + */
   1.812 +#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
   1.813 +png_voidp PNGAPI
   1.814 +png_get_user_transform_ptr(png_const_structrp png_ptr)
   1.815 +{
   1.816 +   if (png_ptr == NULL)
   1.817 +      return (NULL);
   1.818 +
   1.819 +   return png_ptr->user_transform_ptr;
   1.820 +}
   1.821 +#endif
   1.822 +
   1.823 +#ifdef PNG_USER_TRANSFORM_INFO_SUPPORTED
   1.824 +png_uint_32 PNGAPI
   1.825 +png_get_current_row_number(png_const_structrp png_ptr)
   1.826 +{
   1.827 +   /* See the comments in png.h - this is the sub-image row when reading and
   1.828 +    * interlaced image.
   1.829 +    */
   1.830 +   if (png_ptr != NULL)
   1.831 +      return png_ptr->row_number;
   1.832 +
   1.833 +   return PNG_UINT_32_MAX; /* help the app not to fail silently */
   1.834 +}
   1.835 +
   1.836 +png_byte PNGAPI
   1.837 +png_get_current_pass_number(png_const_structrp png_ptr)
   1.838 +{
   1.839 +   if (png_ptr != NULL)
   1.840 +      return png_ptr->pass;
   1.841 +   return 8; /* invalid */
   1.842 +}
   1.843 +#endif /* PNG_USER_TRANSFORM_INFO_SUPPORTED */
   1.844 +#endif /* PNG_READ_USER_TRANSFORM_SUPPORTED ||
   1.845 +          PNG_WRITE_USER_TRANSFORM_SUPPORTED */
   1.846 +#endif /* PNG_READ_SUPPORTED || PNG_WRITE_SUPPORTED */

mercurial