3dphotoshoot

annotate libs/libpng/pngrutil.c @ 14:06dc8b9b4f89

added libimago, libjpeg and libpng
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Jun 2015 17:25:49 +0300
parents
children
rev   line source
nuclear@14 1
nuclear@14 2 /* pngrutil.c - utilities to read a PNG file
nuclear@14 3 *
nuclear@14 4 * Last changed in libpng 1.2.33 [October 31, 2008]
nuclear@14 5 * For conditions of distribution and use, see copyright notice in png.h
nuclear@14 6 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
nuclear@14 7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
nuclear@14 8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
nuclear@14 9 *
nuclear@14 10 * This file contains routines that are only called from within
nuclear@14 11 * libpng itself during the course of reading an image.
nuclear@14 12 */
nuclear@14 13
nuclear@14 14 #define PNG_INTERNAL
nuclear@14 15 #include "png.h"
nuclear@14 16 #if defined(PNG_READ_SUPPORTED)
nuclear@14 17
nuclear@14 18 #if defined(_WIN32_WCE) && (_WIN32_WCE<0x500)
nuclear@14 19 # define WIN32_WCE_OLD
nuclear@14 20 #endif
nuclear@14 21
nuclear@14 22 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 23 # if defined(WIN32_WCE_OLD)
nuclear@14 24 /* strtod() function is not supported on WindowsCE */
nuclear@14 25 __inline double png_strtod(png_structp png_ptr, PNG_CONST char *nptr, char **endptr)
nuclear@14 26 {
nuclear@14 27 double result = 0;
nuclear@14 28 int len;
nuclear@14 29 wchar_t *str, *end;
nuclear@14 30
nuclear@14 31 len = MultiByteToWideChar(CP_ACP, 0, nptr, -1, NULL, 0);
nuclear@14 32 str = (wchar_t *)png_malloc(png_ptr, len * png_sizeof(wchar_t));
nuclear@14 33 if ( NULL != str )
nuclear@14 34 {
nuclear@14 35 MultiByteToWideChar(CP_ACP, 0, nptr, -1, str, len);
nuclear@14 36 result = wcstod(str, &end);
nuclear@14 37 len = WideCharToMultiByte(CP_ACP, 0, end, -1, NULL, 0, NULL, NULL);
nuclear@14 38 *endptr = (char *)nptr + (png_strlen(nptr) - len + 1);
nuclear@14 39 png_free(png_ptr, str);
nuclear@14 40 }
nuclear@14 41 return result;
nuclear@14 42 }
nuclear@14 43 # else
nuclear@14 44 # define png_strtod(p,a,b) strtod(a,b)
nuclear@14 45 # endif
nuclear@14 46 #endif
nuclear@14 47
nuclear@14 48 png_uint_32 PNGAPI
nuclear@14 49 png_get_uint_31(png_structp png_ptr, png_bytep buf)
nuclear@14 50 {
nuclear@14 51 #ifdef PNG_READ_BIG_ENDIAN_SUPPORTED
nuclear@14 52 png_uint_32 i = png_get_uint_32(buf);
nuclear@14 53 #else
nuclear@14 54 /* Avoid an extra function call by inlining the result. */
nuclear@14 55 png_uint_32 i = ((png_uint_32)(*buf) << 24) +
nuclear@14 56 ((png_uint_32)(*(buf + 1)) << 16) +
nuclear@14 57 ((png_uint_32)(*(buf + 2)) << 8) +
nuclear@14 58 (png_uint_32)(*(buf + 3));
nuclear@14 59 #endif
nuclear@14 60 if (i > PNG_UINT_31_MAX)
nuclear@14 61 png_error(png_ptr, "PNG unsigned integer out of range.");
nuclear@14 62 return (i);
nuclear@14 63 }
nuclear@14 64 #ifndef PNG_READ_BIG_ENDIAN_SUPPORTED
nuclear@14 65 /* Grab an unsigned 32-bit integer from a buffer in big-endian format. */
nuclear@14 66 png_uint_32 PNGAPI
nuclear@14 67 png_get_uint_32(png_bytep buf)
nuclear@14 68 {
nuclear@14 69 png_uint_32 i = ((png_uint_32)(*buf) << 24) +
nuclear@14 70 ((png_uint_32)(*(buf + 1)) << 16) +
nuclear@14 71 ((png_uint_32)(*(buf + 2)) << 8) +
nuclear@14 72 (png_uint_32)(*(buf + 3));
nuclear@14 73
nuclear@14 74 return (i);
nuclear@14 75 }
nuclear@14 76
nuclear@14 77 /* Grab a signed 32-bit integer from a buffer in big-endian format. The
nuclear@14 78 * data is stored in the PNG file in two's complement format, and it is
nuclear@14 79 * assumed that the machine format for signed integers is the same. */
nuclear@14 80 png_int_32 PNGAPI
nuclear@14 81 png_get_int_32(png_bytep buf)
nuclear@14 82 {
nuclear@14 83 png_int_32 i = ((png_int_32)(*buf) << 24) +
nuclear@14 84 ((png_int_32)(*(buf + 1)) << 16) +
nuclear@14 85 ((png_int_32)(*(buf + 2)) << 8) +
nuclear@14 86 (png_int_32)(*(buf + 3));
nuclear@14 87
nuclear@14 88 return (i);
nuclear@14 89 }
nuclear@14 90
nuclear@14 91 /* Grab an unsigned 16-bit integer from a buffer in big-endian format. */
nuclear@14 92 png_uint_16 PNGAPI
nuclear@14 93 png_get_uint_16(png_bytep buf)
nuclear@14 94 {
nuclear@14 95 png_uint_16 i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
nuclear@14 96 (png_uint_16)(*(buf + 1)));
nuclear@14 97
nuclear@14 98 return (i);
nuclear@14 99 }
nuclear@14 100 #endif /* PNG_READ_BIG_ENDIAN_SUPPORTED */
nuclear@14 101
nuclear@14 102 /* Read the chunk header (length + type name).
nuclear@14 103 * Put the type name into png_ptr->chunk_name, and return the length.
nuclear@14 104 */
nuclear@14 105 png_uint_32 /* PRIVATE */
nuclear@14 106 png_read_chunk_header(png_structp png_ptr)
nuclear@14 107 {
nuclear@14 108 png_byte buf[8];
nuclear@14 109 png_uint_32 length;
nuclear@14 110
nuclear@14 111 /* read the length and the chunk name */
nuclear@14 112 png_read_data(png_ptr, buf, 8);
nuclear@14 113 length = png_get_uint_31(png_ptr, buf);
nuclear@14 114
nuclear@14 115 /* put the chunk name into png_ptr->chunk_name */
nuclear@14 116 png_memcpy(png_ptr->chunk_name, buf + 4, 4);
nuclear@14 117
nuclear@14 118 png_debug2(0, "Reading %s chunk, length = %lu\n",
nuclear@14 119 png_ptr->chunk_name, length);
nuclear@14 120
nuclear@14 121 /* reset the crc and run it over the chunk name */
nuclear@14 122 png_reset_crc(png_ptr);
nuclear@14 123 png_calculate_crc(png_ptr, png_ptr->chunk_name, 4);
nuclear@14 124
nuclear@14 125 /* check to see if chunk name is valid */
nuclear@14 126 png_check_chunk_name(png_ptr, png_ptr->chunk_name);
nuclear@14 127
nuclear@14 128 return length;
nuclear@14 129 }
nuclear@14 130
nuclear@14 131 /* Read data, and (optionally) run it through the CRC. */
nuclear@14 132 void /* PRIVATE */
nuclear@14 133 png_crc_read(png_structp png_ptr, png_bytep buf, png_size_t length)
nuclear@14 134 {
nuclear@14 135 if (png_ptr == NULL) return;
nuclear@14 136 png_read_data(png_ptr, buf, length);
nuclear@14 137 png_calculate_crc(png_ptr, buf, length);
nuclear@14 138 }
nuclear@14 139
nuclear@14 140 /* Optionally skip data and then check the CRC. Depending on whether we
nuclear@14 141 are reading a ancillary or critical chunk, and how the program has set
nuclear@14 142 things up, we may calculate the CRC on the data and print a message.
nuclear@14 143 Returns '1' if there was a CRC error, '0' otherwise. */
nuclear@14 144 int /* PRIVATE */
nuclear@14 145 png_crc_finish(png_structp png_ptr, png_uint_32 skip)
nuclear@14 146 {
nuclear@14 147 png_size_t i;
nuclear@14 148 png_size_t istop = png_ptr->zbuf_size;
nuclear@14 149
nuclear@14 150 for (i = (png_size_t)skip; i > istop; i -= istop)
nuclear@14 151 {
nuclear@14 152 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
nuclear@14 153 }
nuclear@14 154 if (i)
nuclear@14 155 {
nuclear@14 156 png_crc_read(png_ptr, png_ptr->zbuf, i);
nuclear@14 157 }
nuclear@14 158
nuclear@14 159 if (png_crc_error(png_ptr))
nuclear@14 160 {
nuclear@14 161 if (((png_ptr->chunk_name[0] & 0x20) && /* Ancillary */
nuclear@14 162 !(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)) ||
nuclear@14 163 (!(png_ptr->chunk_name[0] & 0x20) && /* Critical */
nuclear@14 164 (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE)))
nuclear@14 165 {
nuclear@14 166 png_chunk_warning(png_ptr, "CRC error");
nuclear@14 167 }
nuclear@14 168 else
nuclear@14 169 {
nuclear@14 170 png_chunk_error(png_ptr, "CRC error");
nuclear@14 171 }
nuclear@14 172 return (1);
nuclear@14 173 }
nuclear@14 174
nuclear@14 175 return (0);
nuclear@14 176 }
nuclear@14 177
nuclear@14 178 /* Compare the CRC stored in the PNG file with that calculated by libpng from
nuclear@14 179 the data it has read thus far. */
nuclear@14 180 int /* PRIVATE */
nuclear@14 181 png_crc_error(png_structp png_ptr)
nuclear@14 182 {
nuclear@14 183 png_byte crc_bytes[4];
nuclear@14 184 png_uint_32 crc;
nuclear@14 185 int need_crc = 1;
nuclear@14 186
nuclear@14 187 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
nuclear@14 188 {
nuclear@14 189 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
nuclear@14 190 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
nuclear@14 191 need_crc = 0;
nuclear@14 192 }
nuclear@14 193 else /* critical */
nuclear@14 194 {
nuclear@14 195 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
nuclear@14 196 need_crc = 0;
nuclear@14 197 }
nuclear@14 198
nuclear@14 199 png_read_data(png_ptr, crc_bytes, 4);
nuclear@14 200
nuclear@14 201 if (need_crc)
nuclear@14 202 {
nuclear@14 203 crc = png_get_uint_32(crc_bytes);
nuclear@14 204 return ((int)(crc != png_ptr->crc));
nuclear@14 205 }
nuclear@14 206 else
nuclear@14 207 return (0);
nuclear@14 208 }
nuclear@14 209
nuclear@14 210 #if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) || \
nuclear@14 211 defined(PNG_READ_iCCP_SUPPORTED)
nuclear@14 212 /*
nuclear@14 213 * Decompress trailing data in a chunk. The assumption is that chunkdata
nuclear@14 214 * points at an allocated area holding the contents of a chunk with a
nuclear@14 215 * trailing compressed part. What we get back is an allocated area
nuclear@14 216 * holding the original prefix part and an uncompressed version of the
nuclear@14 217 * trailing part (the malloc area passed in is freed).
nuclear@14 218 */
nuclear@14 219 void /* PRIVATE */
nuclear@14 220 png_decompress_chunk(png_structp png_ptr, int comp_type,
nuclear@14 221 png_size_t chunklength,
nuclear@14 222 png_size_t prefix_size, png_size_t *newlength)
nuclear@14 223 {
nuclear@14 224 static PNG_CONST char msg[] = "Error decoding compressed text";
nuclear@14 225 png_charp text;
nuclear@14 226 png_size_t text_size;
nuclear@14 227
nuclear@14 228 if (comp_type == PNG_COMPRESSION_TYPE_BASE)
nuclear@14 229 {
nuclear@14 230 int ret = Z_OK;
nuclear@14 231 png_ptr->zstream.next_in = (png_bytep)(png_ptr->chunkdata + prefix_size);
nuclear@14 232 png_ptr->zstream.avail_in = (uInt)(chunklength - prefix_size);
nuclear@14 233 png_ptr->zstream.next_out = png_ptr->zbuf;
nuclear@14 234 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
nuclear@14 235
nuclear@14 236 text_size = 0;
nuclear@14 237 text = NULL;
nuclear@14 238
nuclear@14 239 while (png_ptr->zstream.avail_in)
nuclear@14 240 {
nuclear@14 241 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
nuclear@14 242 if (ret != Z_OK && ret != Z_STREAM_END)
nuclear@14 243 {
nuclear@14 244 if (png_ptr->zstream.msg != NULL)
nuclear@14 245 png_warning(png_ptr, png_ptr->zstream.msg);
nuclear@14 246 else
nuclear@14 247 png_warning(png_ptr, msg);
nuclear@14 248 inflateReset(&png_ptr->zstream);
nuclear@14 249 png_ptr->zstream.avail_in = 0;
nuclear@14 250
nuclear@14 251 if (text == NULL)
nuclear@14 252 {
nuclear@14 253 text_size = prefix_size + png_sizeof(msg) + 1;
nuclear@14 254 text = (png_charp)png_malloc_warn(png_ptr, text_size);
nuclear@14 255 if (text == NULL)
nuclear@14 256 {
nuclear@14 257 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 258 png_ptr->chunkdata = NULL;
nuclear@14 259 png_error(png_ptr, "Not enough memory to decompress chunk");
nuclear@14 260 }
nuclear@14 261 png_memcpy(text, png_ptr->chunkdata, prefix_size);
nuclear@14 262 }
nuclear@14 263
nuclear@14 264 text[text_size - 1] = 0x00;
nuclear@14 265
nuclear@14 266 /* Copy what we can of the error message into the text chunk */
nuclear@14 267 text_size = (png_size_t)(chunklength -
nuclear@14 268 (text - png_ptr->chunkdata) - 1);
nuclear@14 269 if (text_size > png_sizeof(msg))
nuclear@14 270 text_size = png_sizeof(msg);
nuclear@14 271 png_memcpy(text + prefix_size, msg, text_size);
nuclear@14 272 break;
nuclear@14 273 }
nuclear@14 274 if (!png_ptr->zstream.avail_out || ret == Z_STREAM_END)
nuclear@14 275 {
nuclear@14 276 if (text == NULL)
nuclear@14 277 {
nuclear@14 278 text_size = prefix_size +
nuclear@14 279 png_ptr->zbuf_size - png_ptr->zstream.avail_out;
nuclear@14 280 text = (png_charp)png_malloc_warn(png_ptr, text_size + 1);
nuclear@14 281 if (text == NULL)
nuclear@14 282 {
nuclear@14 283 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 284 png_ptr->chunkdata = NULL;
nuclear@14 285 png_error(png_ptr,
nuclear@14 286 "Not enough memory to decompress chunk.");
nuclear@14 287 }
nuclear@14 288 png_memcpy(text + prefix_size, png_ptr->zbuf,
nuclear@14 289 text_size - prefix_size);
nuclear@14 290 png_memcpy(text, png_ptr->chunkdata, prefix_size);
nuclear@14 291 *(text + text_size) = 0x00;
nuclear@14 292 }
nuclear@14 293 else
nuclear@14 294 {
nuclear@14 295 png_charp tmp;
nuclear@14 296
nuclear@14 297 tmp = text;
nuclear@14 298 text = (png_charp)png_malloc_warn(png_ptr,
nuclear@14 299 (png_uint_32)(text_size +
nuclear@14 300 png_ptr->zbuf_size - png_ptr->zstream.avail_out + 1));
nuclear@14 301 if (text == NULL)
nuclear@14 302 {
nuclear@14 303 png_free(png_ptr, tmp);
nuclear@14 304 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 305 png_ptr->chunkdata = NULL;
nuclear@14 306 png_error(png_ptr,
nuclear@14 307 "Not enough memory to decompress chunk..");
nuclear@14 308 }
nuclear@14 309 png_memcpy(text, tmp, text_size);
nuclear@14 310 png_free(png_ptr, tmp);
nuclear@14 311 png_memcpy(text + text_size, png_ptr->zbuf,
nuclear@14 312 (png_ptr->zbuf_size - png_ptr->zstream.avail_out));
nuclear@14 313 text_size += png_ptr->zbuf_size - png_ptr->zstream.avail_out;
nuclear@14 314 *(text + text_size) = 0x00;
nuclear@14 315 }
nuclear@14 316 if (ret == Z_STREAM_END)
nuclear@14 317 break;
nuclear@14 318 else
nuclear@14 319 {
nuclear@14 320 png_ptr->zstream.next_out = png_ptr->zbuf;
nuclear@14 321 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
nuclear@14 322 }
nuclear@14 323 }
nuclear@14 324 }
nuclear@14 325 if (ret != Z_STREAM_END)
nuclear@14 326 {
nuclear@14 327 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
nuclear@14 328 char umsg[52];
nuclear@14 329
nuclear@14 330 if (ret == Z_BUF_ERROR)
nuclear@14 331 png_snprintf(umsg, 52,
nuclear@14 332 "Buffer error in compressed datastream in %s chunk",
nuclear@14 333 png_ptr->chunk_name);
nuclear@14 334 else if (ret == Z_DATA_ERROR)
nuclear@14 335 png_snprintf(umsg, 52,
nuclear@14 336 "Data error in compressed datastream in %s chunk",
nuclear@14 337 png_ptr->chunk_name);
nuclear@14 338 else
nuclear@14 339 png_snprintf(umsg, 52,
nuclear@14 340 "Incomplete compressed datastream in %s chunk",
nuclear@14 341 png_ptr->chunk_name);
nuclear@14 342 png_warning(png_ptr, umsg);
nuclear@14 343 #else
nuclear@14 344 png_warning(png_ptr,
nuclear@14 345 "Incomplete compressed datastream in chunk other than IDAT");
nuclear@14 346 #endif
nuclear@14 347 text_size = prefix_size;
nuclear@14 348 if (text == NULL)
nuclear@14 349 {
nuclear@14 350 text = (png_charp)png_malloc_warn(png_ptr, text_size+1);
nuclear@14 351 if (text == NULL)
nuclear@14 352 {
nuclear@14 353 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 354 png_ptr->chunkdata = NULL;
nuclear@14 355 png_error(png_ptr, "Not enough memory for text.");
nuclear@14 356 }
nuclear@14 357 png_memcpy(text, png_ptr->chunkdata, prefix_size);
nuclear@14 358 }
nuclear@14 359 *(text + text_size) = 0x00;
nuclear@14 360 }
nuclear@14 361
nuclear@14 362 inflateReset(&png_ptr->zstream);
nuclear@14 363 png_ptr->zstream.avail_in = 0;
nuclear@14 364
nuclear@14 365 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 366 png_ptr->chunkdata = text;
nuclear@14 367 *newlength=text_size;
nuclear@14 368 }
nuclear@14 369 else /* if (comp_type != PNG_COMPRESSION_TYPE_BASE) */
nuclear@14 370 {
nuclear@14 371 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
nuclear@14 372 char umsg[50];
nuclear@14 373
nuclear@14 374 png_snprintf(umsg, 50, "Unknown zTXt compression type %d", comp_type);
nuclear@14 375 png_warning(png_ptr, umsg);
nuclear@14 376 #else
nuclear@14 377 png_warning(png_ptr, "Unknown zTXt compression type");
nuclear@14 378 #endif
nuclear@14 379
nuclear@14 380 *(png_ptr->chunkdata + prefix_size) = 0x00;
nuclear@14 381 *newlength = prefix_size;
nuclear@14 382 }
nuclear@14 383 }
nuclear@14 384 #endif
nuclear@14 385
nuclear@14 386 /* read and check the IDHR chunk */
nuclear@14 387 void /* PRIVATE */
nuclear@14 388 png_handle_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 389 {
nuclear@14 390 png_byte buf[13];
nuclear@14 391 png_uint_32 width, height;
nuclear@14 392 int bit_depth, color_type, compression_type, filter_type;
nuclear@14 393 int interlace_type;
nuclear@14 394
nuclear@14 395 png_debug(1, "in png_handle_IHDR\n");
nuclear@14 396
nuclear@14 397 if (png_ptr->mode & PNG_HAVE_IHDR)
nuclear@14 398 png_error(png_ptr, "Out of place IHDR");
nuclear@14 399
nuclear@14 400 /* check the length */
nuclear@14 401 if (length != 13)
nuclear@14 402 png_error(png_ptr, "Invalid IHDR chunk");
nuclear@14 403
nuclear@14 404 png_ptr->mode |= PNG_HAVE_IHDR;
nuclear@14 405
nuclear@14 406 png_crc_read(png_ptr, buf, 13);
nuclear@14 407 png_crc_finish(png_ptr, 0);
nuclear@14 408
nuclear@14 409 width = png_get_uint_31(png_ptr, buf);
nuclear@14 410 height = png_get_uint_31(png_ptr, buf + 4);
nuclear@14 411 bit_depth = buf[8];
nuclear@14 412 color_type = buf[9];
nuclear@14 413 compression_type = buf[10];
nuclear@14 414 filter_type = buf[11];
nuclear@14 415 interlace_type = buf[12];
nuclear@14 416
nuclear@14 417 /* set internal variables */
nuclear@14 418 png_ptr->width = width;
nuclear@14 419 png_ptr->height = height;
nuclear@14 420 png_ptr->bit_depth = (png_byte)bit_depth;
nuclear@14 421 png_ptr->interlaced = (png_byte)interlace_type;
nuclear@14 422 png_ptr->color_type = (png_byte)color_type;
nuclear@14 423 #if defined(PNG_MNG_FEATURES_SUPPORTED)
nuclear@14 424 png_ptr->filter_type = (png_byte)filter_type;
nuclear@14 425 #endif
nuclear@14 426 png_ptr->compression_type = (png_byte)compression_type;
nuclear@14 427
nuclear@14 428 /* find number of channels */
nuclear@14 429 switch (png_ptr->color_type)
nuclear@14 430 {
nuclear@14 431 case PNG_COLOR_TYPE_GRAY:
nuclear@14 432 case PNG_COLOR_TYPE_PALETTE:
nuclear@14 433 png_ptr->channels = 1;
nuclear@14 434 break;
nuclear@14 435 case PNG_COLOR_TYPE_RGB:
nuclear@14 436 png_ptr->channels = 3;
nuclear@14 437 break;
nuclear@14 438 case PNG_COLOR_TYPE_GRAY_ALPHA:
nuclear@14 439 png_ptr->channels = 2;
nuclear@14 440 break;
nuclear@14 441 case PNG_COLOR_TYPE_RGB_ALPHA:
nuclear@14 442 png_ptr->channels = 4;
nuclear@14 443 break;
nuclear@14 444 }
nuclear@14 445
nuclear@14 446 /* set up other useful info */
nuclear@14 447 png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
nuclear@14 448 png_ptr->channels);
nuclear@14 449 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
nuclear@14 450 png_debug1(3, "bit_depth = %d\n", png_ptr->bit_depth);
nuclear@14 451 png_debug1(3, "channels = %d\n", png_ptr->channels);
nuclear@14 452 png_debug1(3, "rowbytes = %lu\n", png_ptr->rowbytes);
nuclear@14 453 png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
nuclear@14 454 color_type, interlace_type, compression_type, filter_type);
nuclear@14 455 }
nuclear@14 456
nuclear@14 457 /* read and check the palette */
nuclear@14 458 void /* PRIVATE */
nuclear@14 459 png_handle_PLTE(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 460 {
nuclear@14 461 png_color palette[PNG_MAX_PALETTE_LENGTH];
nuclear@14 462 int num, i;
nuclear@14 463 #ifndef PNG_NO_POINTER_INDEXING
nuclear@14 464 png_colorp pal_ptr;
nuclear@14 465 #endif
nuclear@14 466
nuclear@14 467 png_debug(1, "in png_handle_PLTE\n");
nuclear@14 468
nuclear@14 469 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 470 png_error(png_ptr, "Missing IHDR before PLTE");
nuclear@14 471 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 472 {
nuclear@14 473 png_warning(png_ptr, "Invalid PLTE after IDAT");
nuclear@14 474 png_crc_finish(png_ptr, length);
nuclear@14 475 return;
nuclear@14 476 }
nuclear@14 477 else if (png_ptr->mode & PNG_HAVE_PLTE)
nuclear@14 478 png_error(png_ptr, "Duplicate PLTE chunk");
nuclear@14 479
nuclear@14 480 png_ptr->mode |= PNG_HAVE_PLTE;
nuclear@14 481
nuclear@14 482 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
nuclear@14 483 {
nuclear@14 484 png_warning(png_ptr,
nuclear@14 485 "Ignoring PLTE chunk in grayscale PNG");
nuclear@14 486 png_crc_finish(png_ptr, length);
nuclear@14 487 return;
nuclear@14 488 }
nuclear@14 489 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
nuclear@14 490 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
nuclear@14 491 {
nuclear@14 492 png_crc_finish(png_ptr, length);
nuclear@14 493 return;
nuclear@14 494 }
nuclear@14 495 #endif
nuclear@14 496
nuclear@14 497 if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
nuclear@14 498 {
nuclear@14 499 if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
nuclear@14 500 {
nuclear@14 501 png_warning(png_ptr, "Invalid palette chunk");
nuclear@14 502 png_crc_finish(png_ptr, length);
nuclear@14 503 return;
nuclear@14 504 }
nuclear@14 505 else
nuclear@14 506 {
nuclear@14 507 png_error(png_ptr, "Invalid palette chunk");
nuclear@14 508 }
nuclear@14 509 }
nuclear@14 510
nuclear@14 511 num = (int)length / 3;
nuclear@14 512
nuclear@14 513 #ifndef PNG_NO_POINTER_INDEXING
nuclear@14 514 for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
nuclear@14 515 {
nuclear@14 516 png_byte buf[3];
nuclear@14 517
nuclear@14 518 png_crc_read(png_ptr, buf, 3);
nuclear@14 519 pal_ptr->red = buf[0];
nuclear@14 520 pal_ptr->green = buf[1];
nuclear@14 521 pal_ptr->blue = buf[2];
nuclear@14 522 }
nuclear@14 523 #else
nuclear@14 524 for (i = 0; i < num; i++)
nuclear@14 525 {
nuclear@14 526 png_byte buf[3];
nuclear@14 527
nuclear@14 528 png_crc_read(png_ptr, buf, 3);
nuclear@14 529 /* don't depend upon png_color being any order */
nuclear@14 530 palette[i].red = buf[0];
nuclear@14 531 palette[i].green = buf[1];
nuclear@14 532 palette[i].blue = buf[2];
nuclear@14 533 }
nuclear@14 534 #endif
nuclear@14 535
nuclear@14 536 /* If we actually NEED the PLTE chunk (ie for a paletted image), we do
nuclear@14 537 whatever the normal CRC configuration tells us. However, if we
nuclear@14 538 have an RGB image, the PLTE can be considered ancillary, so
nuclear@14 539 we will act as though it is. */
nuclear@14 540 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
nuclear@14 541 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
nuclear@14 542 #endif
nuclear@14 543 {
nuclear@14 544 png_crc_finish(png_ptr, 0);
nuclear@14 545 }
nuclear@14 546 #if !defined(PNG_READ_OPT_PLTE_SUPPORTED)
nuclear@14 547 else if (png_crc_error(png_ptr)) /* Only if we have a CRC error */
nuclear@14 548 {
nuclear@14 549 /* If we don't want to use the data from an ancillary chunk,
nuclear@14 550 we have two options: an error abort, or a warning and we
nuclear@14 551 ignore the data in this chunk (which should be OK, since
nuclear@14 552 it's considered ancillary for a RGB or RGBA image). */
nuclear@14 553 if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE))
nuclear@14 554 {
nuclear@14 555 if (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN)
nuclear@14 556 {
nuclear@14 557 png_chunk_error(png_ptr, "CRC error");
nuclear@14 558 }
nuclear@14 559 else
nuclear@14 560 {
nuclear@14 561 png_chunk_warning(png_ptr, "CRC error");
nuclear@14 562 return;
nuclear@14 563 }
nuclear@14 564 }
nuclear@14 565 /* Otherwise, we (optionally) emit a warning and use the chunk. */
nuclear@14 566 else if (!(png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN))
nuclear@14 567 {
nuclear@14 568 png_chunk_warning(png_ptr, "CRC error");
nuclear@14 569 }
nuclear@14 570 }
nuclear@14 571 #endif
nuclear@14 572
nuclear@14 573 png_set_PLTE(png_ptr, info_ptr, palette, num);
nuclear@14 574
nuclear@14 575 #if defined(PNG_READ_tRNS_SUPPORTED)
nuclear@14 576 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
nuclear@14 577 {
nuclear@14 578 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
nuclear@14 579 {
nuclear@14 580 if (png_ptr->num_trans > (png_uint_16)num)
nuclear@14 581 {
nuclear@14 582 png_warning(png_ptr, "Truncating incorrect tRNS chunk length");
nuclear@14 583 png_ptr->num_trans = (png_uint_16)num;
nuclear@14 584 }
nuclear@14 585 if (info_ptr->num_trans > (png_uint_16)num)
nuclear@14 586 {
nuclear@14 587 png_warning(png_ptr, "Truncating incorrect info tRNS chunk length");
nuclear@14 588 info_ptr->num_trans = (png_uint_16)num;
nuclear@14 589 }
nuclear@14 590 }
nuclear@14 591 }
nuclear@14 592 #endif
nuclear@14 593
nuclear@14 594 }
nuclear@14 595
nuclear@14 596 void /* PRIVATE */
nuclear@14 597 png_handle_IEND(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 598 {
nuclear@14 599 png_debug(1, "in png_handle_IEND\n");
nuclear@14 600
nuclear@14 601 if (!(png_ptr->mode & PNG_HAVE_IHDR) || !(png_ptr->mode & PNG_HAVE_IDAT))
nuclear@14 602 {
nuclear@14 603 png_error(png_ptr, "No image in file");
nuclear@14 604 }
nuclear@14 605
nuclear@14 606 png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
nuclear@14 607
nuclear@14 608 if (length != 0)
nuclear@14 609 {
nuclear@14 610 png_warning(png_ptr, "Incorrect IEND chunk length");
nuclear@14 611 }
nuclear@14 612 png_crc_finish(png_ptr, length);
nuclear@14 613
nuclear@14 614 info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
nuclear@14 615 }
nuclear@14 616
nuclear@14 617 #if defined(PNG_READ_gAMA_SUPPORTED)
nuclear@14 618 void /* PRIVATE */
nuclear@14 619 png_handle_gAMA(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 620 {
nuclear@14 621 png_fixed_point igamma;
nuclear@14 622 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 623 float file_gamma;
nuclear@14 624 #endif
nuclear@14 625 png_byte buf[4];
nuclear@14 626
nuclear@14 627 png_debug(1, "in png_handle_gAMA\n");
nuclear@14 628
nuclear@14 629 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 630 png_error(png_ptr, "Missing IHDR before gAMA");
nuclear@14 631 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 632 {
nuclear@14 633 png_warning(png_ptr, "Invalid gAMA after IDAT");
nuclear@14 634 png_crc_finish(png_ptr, length);
nuclear@14 635 return;
nuclear@14 636 }
nuclear@14 637 else if (png_ptr->mode & PNG_HAVE_PLTE)
nuclear@14 638 /* Should be an error, but we can cope with it */
nuclear@14 639 png_warning(png_ptr, "Out of place gAMA chunk");
nuclear@14 640
nuclear@14 641 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA)
nuclear@14 642 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@14 643 && !(info_ptr->valid & PNG_INFO_sRGB)
nuclear@14 644 #endif
nuclear@14 645 )
nuclear@14 646 {
nuclear@14 647 png_warning(png_ptr, "Duplicate gAMA chunk");
nuclear@14 648 png_crc_finish(png_ptr, length);
nuclear@14 649 return;
nuclear@14 650 }
nuclear@14 651
nuclear@14 652 if (length != 4)
nuclear@14 653 {
nuclear@14 654 png_warning(png_ptr, "Incorrect gAMA chunk length");
nuclear@14 655 png_crc_finish(png_ptr, length);
nuclear@14 656 return;
nuclear@14 657 }
nuclear@14 658
nuclear@14 659 png_crc_read(png_ptr, buf, 4);
nuclear@14 660 if (png_crc_finish(png_ptr, 0))
nuclear@14 661 return;
nuclear@14 662
nuclear@14 663 igamma = (png_fixed_point)png_get_uint_32(buf);
nuclear@14 664 /* check for zero gamma */
nuclear@14 665 if (igamma == 0)
nuclear@14 666 {
nuclear@14 667 png_warning(png_ptr,
nuclear@14 668 "Ignoring gAMA chunk with gamma=0");
nuclear@14 669 return;
nuclear@14 670 }
nuclear@14 671
nuclear@14 672 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@14 673 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
nuclear@14 674 if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
nuclear@14 675 {
nuclear@14 676 png_warning(png_ptr,
nuclear@14 677 "Ignoring incorrect gAMA value when sRGB is also present");
nuclear@14 678 #ifndef PNG_NO_CONSOLE_IO
nuclear@14 679 fprintf(stderr, "gamma = (%d/100000)\n", (int)igamma);
nuclear@14 680 #endif
nuclear@14 681 return;
nuclear@14 682 }
nuclear@14 683 #endif /* PNG_READ_sRGB_SUPPORTED */
nuclear@14 684
nuclear@14 685 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 686 file_gamma = (float)igamma / (float)100000.0;
nuclear@14 687 # ifdef PNG_READ_GAMMA_SUPPORTED
nuclear@14 688 png_ptr->gamma = file_gamma;
nuclear@14 689 # endif
nuclear@14 690 png_set_gAMA(png_ptr, info_ptr, file_gamma);
nuclear@14 691 #endif
nuclear@14 692 #ifdef PNG_FIXED_POINT_SUPPORTED
nuclear@14 693 png_set_gAMA_fixed(png_ptr, info_ptr, igamma);
nuclear@14 694 #endif
nuclear@14 695 }
nuclear@14 696 #endif
nuclear@14 697
nuclear@14 698 #if defined(PNG_READ_sBIT_SUPPORTED)
nuclear@14 699 void /* PRIVATE */
nuclear@14 700 png_handle_sBIT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 701 {
nuclear@14 702 png_size_t truelen;
nuclear@14 703 png_byte buf[4];
nuclear@14 704
nuclear@14 705 png_debug(1, "in png_handle_sBIT\n");
nuclear@14 706
nuclear@14 707 buf[0] = buf[1] = buf[2] = buf[3] = 0;
nuclear@14 708
nuclear@14 709 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 710 png_error(png_ptr, "Missing IHDR before sBIT");
nuclear@14 711 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 712 {
nuclear@14 713 png_warning(png_ptr, "Invalid sBIT after IDAT");
nuclear@14 714 png_crc_finish(png_ptr, length);
nuclear@14 715 return;
nuclear@14 716 }
nuclear@14 717 else if (png_ptr->mode & PNG_HAVE_PLTE)
nuclear@14 718 {
nuclear@14 719 /* Should be an error, but we can cope with it */
nuclear@14 720 png_warning(png_ptr, "Out of place sBIT chunk");
nuclear@14 721 }
nuclear@14 722 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT))
nuclear@14 723 {
nuclear@14 724 png_warning(png_ptr, "Duplicate sBIT chunk");
nuclear@14 725 png_crc_finish(png_ptr, length);
nuclear@14 726 return;
nuclear@14 727 }
nuclear@14 728
nuclear@14 729 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
nuclear@14 730 truelen = 3;
nuclear@14 731 else
nuclear@14 732 truelen = (png_size_t)png_ptr->channels;
nuclear@14 733
nuclear@14 734 if (length != truelen || length > 4)
nuclear@14 735 {
nuclear@14 736 png_warning(png_ptr, "Incorrect sBIT chunk length");
nuclear@14 737 png_crc_finish(png_ptr, length);
nuclear@14 738 return;
nuclear@14 739 }
nuclear@14 740
nuclear@14 741 png_crc_read(png_ptr, buf, truelen);
nuclear@14 742 if (png_crc_finish(png_ptr, 0))
nuclear@14 743 return;
nuclear@14 744
nuclear@14 745 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
nuclear@14 746 {
nuclear@14 747 png_ptr->sig_bit.red = buf[0];
nuclear@14 748 png_ptr->sig_bit.green = buf[1];
nuclear@14 749 png_ptr->sig_bit.blue = buf[2];
nuclear@14 750 png_ptr->sig_bit.alpha = buf[3];
nuclear@14 751 }
nuclear@14 752 else
nuclear@14 753 {
nuclear@14 754 png_ptr->sig_bit.gray = buf[0];
nuclear@14 755 png_ptr->sig_bit.red = buf[0];
nuclear@14 756 png_ptr->sig_bit.green = buf[0];
nuclear@14 757 png_ptr->sig_bit.blue = buf[0];
nuclear@14 758 png_ptr->sig_bit.alpha = buf[1];
nuclear@14 759 }
nuclear@14 760 png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
nuclear@14 761 }
nuclear@14 762 #endif
nuclear@14 763
nuclear@14 764 #if defined(PNG_READ_cHRM_SUPPORTED)
nuclear@14 765 void /* PRIVATE */
nuclear@14 766 png_handle_cHRM(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 767 {
nuclear@14 768 png_byte buf[32];
nuclear@14 769 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 770 float white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y;
nuclear@14 771 #endif
nuclear@14 772 png_fixed_point int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
nuclear@14 773 int_y_green, int_x_blue, int_y_blue;
nuclear@14 774
nuclear@14 775 png_uint_32 uint_x, uint_y;
nuclear@14 776
nuclear@14 777 png_debug(1, "in png_handle_cHRM\n");
nuclear@14 778
nuclear@14 779 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 780 png_error(png_ptr, "Missing IHDR before cHRM");
nuclear@14 781 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 782 {
nuclear@14 783 png_warning(png_ptr, "Invalid cHRM after IDAT");
nuclear@14 784 png_crc_finish(png_ptr, length);
nuclear@14 785 return;
nuclear@14 786 }
nuclear@14 787 else if (png_ptr->mode & PNG_HAVE_PLTE)
nuclear@14 788 /* Should be an error, but we can cope with it */
nuclear@14 789 png_warning(png_ptr, "Missing PLTE before cHRM");
nuclear@14 790
nuclear@14 791 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM)
nuclear@14 792 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@14 793 && !(info_ptr->valid & PNG_INFO_sRGB)
nuclear@14 794 #endif
nuclear@14 795 )
nuclear@14 796 {
nuclear@14 797 png_warning(png_ptr, "Duplicate cHRM chunk");
nuclear@14 798 png_crc_finish(png_ptr, length);
nuclear@14 799 return;
nuclear@14 800 }
nuclear@14 801
nuclear@14 802 if (length != 32)
nuclear@14 803 {
nuclear@14 804 png_warning(png_ptr, "Incorrect cHRM chunk length");
nuclear@14 805 png_crc_finish(png_ptr, length);
nuclear@14 806 return;
nuclear@14 807 }
nuclear@14 808
nuclear@14 809 png_crc_read(png_ptr, buf, 32);
nuclear@14 810 if (png_crc_finish(png_ptr, 0))
nuclear@14 811 return;
nuclear@14 812
nuclear@14 813 uint_x = png_get_uint_32(buf);
nuclear@14 814 uint_y = png_get_uint_32(buf + 4);
nuclear@14 815 if (uint_x > 80000L || uint_y > 80000L ||
nuclear@14 816 uint_x + uint_y > 100000L)
nuclear@14 817 {
nuclear@14 818 png_warning(png_ptr, "Invalid cHRM white point");
nuclear@14 819 return;
nuclear@14 820 }
nuclear@14 821 int_x_white = (png_fixed_point)uint_x;
nuclear@14 822 int_y_white = (png_fixed_point)uint_y;
nuclear@14 823
nuclear@14 824 uint_x = png_get_uint_32(buf + 8);
nuclear@14 825 uint_y = png_get_uint_32(buf + 12);
nuclear@14 826 if (uint_x + uint_y > 100000L)
nuclear@14 827 {
nuclear@14 828 png_warning(png_ptr, "Invalid cHRM red point");
nuclear@14 829 return;
nuclear@14 830 }
nuclear@14 831 int_x_red = (png_fixed_point)uint_x;
nuclear@14 832 int_y_red = (png_fixed_point)uint_y;
nuclear@14 833
nuclear@14 834 uint_x = png_get_uint_32(buf + 16);
nuclear@14 835 uint_y = png_get_uint_32(buf + 20);
nuclear@14 836 if (uint_x + uint_y > 100000L)
nuclear@14 837 {
nuclear@14 838 png_warning(png_ptr, "Invalid cHRM green point");
nuclear@14 839 return;
nuclear@14 840 }
nuclear@14 841 int_x_green = (png_fixed_point)uint_x;
nuclear@14 842 int_y_green = (png_fixed_point)uint_y;
nuclear@14 843
nuclear@14 844 uint_x = png_get_uint_32(buf + 24);
nuclear@14 845 uint_y = png_get_uint_32(buf + 28);
nuclear@14 846 if (uint_x + uint_y > 100000L)
nuclear@14 847 {
nuclear@14 848 png_warning(png_ptr, "Invalid cHRM blue point");
nuclear@14 849 return;
nuclear@14 850 }
nuclear@14 851 int_x_blue = (png_fixed_point)uint_x;
nuclear@14 852 int_y_blue = (png_fixed_point)uint_y;
nuclear@14 853
nuclear@14 854 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 855 white_x = (float)int_x_white / (float)100000.0;
nuclear@14 856 white_y = (float)int_y_white / (float)100000.0;
nuclear@14 857 red_x = (float)int_x_red / (float)100000.0;
nuclear@14 858 red_y = (float)int_y_red / (float)100000.0;
nuclear@14 859 green_x = (float)int_x_green / (float)100000.0;
nuclear@14 860 green_y = (float)int_y_green / (float)100000.0;
nuclear@14 861 blue_x = (float)int_x_blue / (float)100000.0;
nuclear@14 862 blue_y = (float)int_y_blue / (float)100000.0;
nuclear@14 863 #endif
nuclear@14 864
nuclear@14 865 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@14 866 if ((info_ptr != NULL) && (info_ptr->valid & PNG_INFO_sRGB))
nuclear@14 867 {
nuclear@14 868 if (PNG_OUT_OF_RANGE(int_x_white, 31270, 1000) ||
nuclear@14 869 PNG_OUT_OF_RANGE(int_y_white, 32900, 1000) ||
nuclear@14 870 PNG_OUT_OF_RANGE(int_x_red, 64000L, 1000) ||
nuclear@14 871 PNG_OUT_OF_RANGE(int_y_red, 33000, 1000) ||
nuclear@14 872 PNG_OUT_OF_RANGE(int_x_green, 30000, 1000) ||
nuclear@14 873 PNG_OUT_OF_RANGE(int_y_green, 60000L, 1000) ||
nuclear@14 874 PNG_OUT_OF_RANGE(int_x_blue, 15000, 1000) ||
nuclear@14 875 PNG_OUT_OF_RANGE(int_y_blue, 6000, 1000))
nuclear@14 876 {
nuclear@14 877 png_warning(png_ptr,
nuclear@14 878 "Ignoring incorrect cHRM value when sRGB is also present");
nuclear@14 879 #ifndef PNG_NO_CONSOLE_IO
nuclear@14 880 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 881 fprintf(stderr, "wx=%f, wy=%f, rx=%f, ry=%f\n",
nuclear@14 882 white_x, white_y, red_x, red_y);
nuclear@14 883 fprintf(stderr, "gx=%f, gy=%f, bx=%f, by=%f\n",
nuclear@14 884 green_x, green_y, blue_x, blue_y);
nuclear@14 885 #else
nuclear@14 886 fprintf(stderr, "wx=%ld, wy=%ld, rx=%ld, ry=%ld\n",
nuclear@14 887 int_x_white, int_y_white, int_x_red, int_y_red);
nuclear@14 888 fprintf(stderr, "gx=%ld, gy=%ld, bx=%ld, by=%ld\n",
nuclear@14 889 int_x_green, int_y_green, int_x_blue, int_y_blue);
nuclear@14 890 #endif
nuclear@14 891 #endif /* PNG_NO_CONSOLE_IO */
nuclear@14 892 }
nuclear@14 893 return;
nuclear@14 894 }
nuclear@14 895 #endif /* PNG_READ_sRGB_SUPPORTED */
nuclear@14 896
nuclear@14 897 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 898 png_set_cHRM(png_ptr, info_ptr,
nuclear@14 899 white_x, white_y, red_x, red_y, green_x, green_y, blue_x, blue_y);
nuclear@14 900 #endif
nuclear@14 901 #ifdef PNG_FIXED_POINT_SUPPORTED
nuclear@14 902 png_set_cHRM_fixed(png_ptr, info_ptr,
nuclear@14 903 int_x_white, int_y_white, int_x_red, int_y_red, int_x_green,
nuclear@14 904 int_y_green, int_x_blue, int_y_blue);
nuclear@14 905 #endif
nuclear@14 906 }
nuclear@14 907 #endif
nuclear@14 908
nuclear@14 909 #if defined(PNG_READ_sRGB_SUPPORTED)
nuclear@14 910 void /* PRIVATE */
nuclear@14 911 png_handle_sRGB(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 912 {
nuclear@14 913 int intent;
nuclear@14 914 png_byte buf[1];
nuclear@14 915
nuclear@14 916 png_debug(1, "in png_handle_sRGB\n");
nuclear@14 917
nuclear@14 918 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 919 png_error(png_ptr, "Missing IHDR before sRGB");
nuclear@14 920 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 921 {
nuclear@14 922 png_warning(png_ptr, "Invalid sRGB after IDAT");
nuclear@14 923 png_crc_finish(png_ptr, length);
nuclear@14 924 return;
nuclear@14 925 }
nuclear@14 926 else if (png_ptr->mode & PNG_HAVE_PLTE)
nuclear@14 927 /* Should be an error, but we can cope with it */
nuclear@14 928 png_warning(png_ptr, "Out of place sRGB chunk");
nuclear@14 929
nuclear@14 930 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sRGB))
nuclear@14 931 {
nuclear@14 932 png_warning(png_ptr, "Duplicate sRGB chunk");
nuclear@14 933 png_crc_finish(png_ptr, length);
nuclear@14 934 return;
nuclear@14 935 }
nuclear@14 936
nuclear@14 937 if (length != 1)
nuclear@14 938 {
nuclear@14 939 png_warning(png_ptr, "Incorrect sRGB chunk length");
nuclear@14 940 png_crc_finish(png_ptr, length);
nuclear@14 941 return;
nuclear@14 942 }
nuclear@14 943
nuclear@14 944 png_crc_read(png_ptr, buf, 1);
nuclear@14 945 if (png_crc_finish(png_ptr, 0))
nuclear@14 946 return;
nuclear@14 947
nuclear@14 948 intent = buf[0];
nuclear@14 949 /* check for bad intent */
nuclear@14 950 if (intent >= PNG_sRGB_INTENT_LAST)
nuclear@14 951 {
nuclear@14 952 png_warning(png_ptr, "Unknown sRGB intent");
nuclear@14 953 return;
nuclear@14 954 }
nuclear@14 955
nuclear@14 956 #if defined(PNG_READ_gAMA_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
nuclear@14 957 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_gAMA))
nuclear@14 958 {
nuclear@14 959 png_fixed_point igamma;
nuclear@14 960 #ifdef PNG_FIXED_POINT_SUPPORTED
nuclear@14 961 igamma=info_ptr->int_gamma;
nuclear@14 962 #else
nuclear@14 963 # ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 964 igamma=(png_fixed_point)(info_ptr->gamma * 100000.);
nuclear@14 965 # endif
nuclear@14 966 #endif
nuclear@14 967 if (PNG_OUT_OF_RANGE(igamma, 45500L, 500))
nuclear@14 968 {
nuclear@14 969 png_warning(png_ptr,
nuclear@14 970 "Ignoring incorrect gAMA value when sRGB is also present");
nuclear@14 971 #ifndef PNG_NO_CONSOLE_IO
nuclear@14 972 # ifdef PNG_FIXED_POINT_SUPPORTED
nuclear@14 973 fprintf(stderr, "incorrect gamma=(%d/100000)\n",
nuclear@14 974 (int)png_ptr->int_gamma);
nuclear@14 975 # else
nuclear@14 976 # ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 977 fprintf(stderr, "incorrect gamma=%f\n", png_ptr->gamma);
nuclear@14 978 # endif
nuclear@14 979 # endif
nuclear@14 980 #endif
nuclear@14 981 }
nuclear@14 982 }
nuclear@14 983 #endif /* PNG_READ_gAMA_SUPPORTED */
nuclear@14 984
nuclear@14 985 #ifdef PNG_READ_cHRM_SUPPORTED
nuclear@14 986 #ifdef PNG_FIXED_POINT_SUPPORTED
nuclear@14 987 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_cHRM))
nuclear@14 988 if (PNG_OUT_OF_RANGE(info_ptr->int_x_white, 31270, 1000) ||
nuclear@14 989 PNG_OUT_OF_RANGE(info_ptr->int_y_white, 32900, 1000) ||
nuclear@14 990 PNG_OUT_OF_RANGE(info_ptr->int_x_red, 64000L, 1000) ||
nuclear@14 991 PNG_OUT_OF_RANGE(info_ptr->int_y_red, 33000, 1000) ||
nuclear@14 992 PNG_OUT_OF_RANGE(info_ptr->int_x_green, 30000, 1000) ||
nuclear@14 993 PNG_OUT_OF_RANGE(info_ptr->int_y_green, 60000L, 1000) ||
nuclear@14 994 PNG_OUT_OF_RANGE(info_ptr->int_x_blue, 15000, 1000) ||
nuclear@14 995 PNG_OUT_OF_RANGE(info_ptr->int_y_blue, 6000, 1000))
nuclear@14 996 {
nuclear@14 997 png_warning(png_ptr,
nuclear@14 998 "Ignoring incorrect cHRM value when sRGB is also present");
nuclear@14 999 }
nuclear@14 1000 #endif /* PNG_FIXED_POINT_SUPPORTED */
nuclear@14 1001 #endif /* PNG_READ_cHRM_SUPPORTED */
nuclear@14 1002
nuclear@14 1003 png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, intent);
nuclear@14 1004 }
nuclear@14 1005 #endif /* PNG_READ_sRGB_SUPPORTED */
nuclear@14 1006
nuclear@14 1007 #if defined(PNG_READ_iCCP_SUPPORTED)
nuclear@14 1008 void /* PRIVATE */
nuclear@14 1009 png_handle_iCCP(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1010 /* Note: this does not properly handle chunks that are > 64K under DOS */
nuclear@14 1011 {
nuclear@14 1012 png_byte compression_type;
nuclear@14 1013 png_bytep pC;
nuclear@14 1014 png_charp profile;
nuclear@14 1015 png_uint_32 skip = 0;
nuclear@14 1016 png_uint_32 profile_size, profile_length;
nuclear@14 1017 png_size_t slength, prefix_length, data_length;
nuclear@14 1018
nuclear@14 1019 png_debug(1, "in png_handle_iCCP\n");
nuclear@14 1020
nuclear@14 1021 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1022 png_error(png_ptr, "Missing IHDR before iCCP");
nuclear@14 1023 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1024 {
nuclear@14 1025 png_warning(png_ptr, "Invalid iCCP after IDAT");
nuclear@14 1026 png_crc_finish(png_ptr, length);
nuclear@14 1027 return;
nuclear@14 1028 }
nuclear@14 1029 else if (png_ptr->mode & PNG_HAVE_PLTE)
nuclear@14 1030 /* Should be an error, but we can cope with it */
nuclear@14 1031 png_warning(png_ptr, "Out of place iCCP chunk");
nuclear@14 1032
nuclear@14 1033 if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_iCCP))
nuclear@14 1034 {
nuclear@14 1035 png_warning(png_ptr, "Duplicate iCCP chunk");
nuclear@14 1036 png_crc_finish(png_ptr, length);
nuclear@14 1037 return;
nuclear@14 1038 }
nuclear@14 1039
nuclear@14 1040 #ifdef PNG_MAX_MALLOC_64K
nuclear@14 1041 if (length > (png_uint_32)65535L)
nuclear@14 1042 {
nuclear@14 1043 png_warning(png_ptr, "iCCP chunk too large to fit in memory");
nuclear@14 1044 skip = length - (png_uint_32)65535L;
nuclear@14 1045 length = (png_uint_32)65535L;
nuclear@14 1046 }
nuclear@14 1047 #endif
nuclear@14 1048
nuclear@14 1049 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1050 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
nuclear@14 1051 slength = (png_size_t)length;
nuclear@14 1052 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
nuclear@14 1053
nuclear@14 1054 if (png_crc_finish(png_ptr, skip))
nuclear@14 1055 {
nuclear@14 1056 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1057 png_ptr->chunkdata = NULL;
nuclear@14 1058 return;
nuclear@14 1059 }
nuclear@14 1060
nuclear@14 1061 png_ptr->chunkdata[slength] = 0x00;
nuclear@14 1062
nuclear@14 1063 for (profile = png_ptr->chunkdata; *profile; profile++)
nuclear@14 1064 /* empty loop to find end of name */ ;
nuclear@14 1065
nuclear@14 1066 ++profile;
nuclear@14 1067
nuclear@14 1068 /* there should be at least one zero (the compression type byte)
nuclear@14 1069 following the separator, and we should be on it */
nuclear@14 1070 if ( profile >= png_ptr->chunkdata + slength - 1)
nuclear@14 1071 {
nuclear@14 1072 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1073 png_ptr->chunkdata = NULL;
nuclear@14 1074 png_warning(png_ptr, "Malformed iCCP chunk");
nuclear@14 1075 return;
nuclear@14 1076 }
nuclear@14 1077
nuclear@14 1078 /* compression_type should always be zero */
nuclear@14 1079 compression_type = *profile++;
nuclear@14 1080 if (compression_type)
nuclear@14 1081 {
nuclear@14 1082 png_warning(png_ptr, "Ignoring nonzero compression type in iCCP chunk");
nuclear@14 1083 compression_type = 0x00; /* Reset it to zero (libpng-1.0.6 through 1.0.8
nuclear@14 1084 wrote nonzero) */
nuclear@14 1085 }
nuclear@14 1086
nuclear@14 1087 prefix_length = profile - png_ptr->chunkdata;
nuclear@14 1088 png_decompress_chunk(png_ptr, compression_type,
nuclear@14 1089 slength, prefix_length, &data_length);
nuclear@14 1090
nuclear@14 1091 profile_length = data_length - prefix_length;
nuclear@14 1092
nuclear@14 1093 if ( prefix_length > data_length || profile_length < 4)
nuclear@14 1094 {
nuclear@14 1095 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1096 png_ptr->chunkdata = NULL;
nuclear@14 1097 png_warning(png_ptr, "Profile size field missing from iCCP chunk");
nuclear@14 1098 return;
nuclear@14 1099 }
nuclear@14 1100
nuclear@14 1101 /* Check the profile_size recorded in the first 32 bits of the ICC profile */
nuclear@14 1102 pC = (png_bytep)(png_ptr->chunkdata + prefix_length);
nuclear@14 1103 profile_size = ((*(pC ))<<24) |
nuclear@14 1104 ((*(pC + 1))<<16) |
nuclear@14 1105 ((*(pC + 2))<< 8) |
nuclear@14 1106 ((*(pC + 3)) );
nuclear@14 1107
nuclear@14 1108 if (profile_size < profile_length)
nuclear@14 1109 profile_length = profile_size;
nuclear@14 1110
nuclear@14 1111 if (profile_size > profile_length)
nuclear@14 1112 {
nuclear@14 1113 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1114 png_ptr->chunkdata = NULL;
nuclear@14 1115 png_warning(png_ptr, "Ignoring truncated iCCP profile.");
nuclear@14 1116 return;
nuclear@14 1117 }
nuclear@14 1118
nuclear@14 1119 png_set_iCCP(png_ptr, info_ptr, png_ptr->chunkdata,
nuclear@14 1120 compression_type, png_ptr->chunkdata + prefix_length, profile_length);
nuclear@14 1121 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1122 png_ptr->chunkdata = NULL;
nuclear@14 1123 }
nuclear@14 1124 #endif /* PNG_READ_iCCP_SUPPORTED */
nuclear@14 1125
nuclear@14 1126 #if defined(PNG_READ_sPLT_SUPPORTED)
nuclear@14 1127 void /* PRIVATE */
nuclear@14 1128 png_handle_sPLT(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1129 /* Note: this does not properly handle chunks that are > 64K under DOS */
nuclear@14 1130 {
nuclear@14 1131 png_bytep entry_start;
nuclear@14 1132 png_sPLT_t new_palette;
nuclear@14 1133 #ifdef PNG_NO_POINTER_INDEXING
nuclear@14 1134 png_sPLT_entryp pp;
nuclear@14 1135 #endif
nuclear@14 1136 int data_length, entry_size, i;
nuclear@14 1137 png_uint_32 skip = 0;
nuclear@14 1138 png_size_t slength;
nuclear@14 1139
nuclear@14 1140 png_debug(1, "in png_handle_sPLT\n");
nuclear@14 1141
nuclear@14 1142 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1143 png_error(png_ptr, "Missing IHDR before sPLT");
nuclear@14 1144 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1145 {
nuclear@14 1146 png_warning(png_ptr, "Invalid sPLT after IDAT");
nuclear@14 1147 png_crc_finish(png_ptr, length);
nuclear@14 1148 return;
nuclear@14 1149 }
nuclear@14 1150
nuclear@14 1151 #ifdef PNG_MAX_MALLOC_64K
nuclear@14 1152 if (length > (png_uint_32)65535L)
nuclear@14 1153 {
nuclear@14 1154 png_warning(png_ptr, "sPLT chunk too large to fit in memory");
nuclear@14 1155 skip = length - (png_uint_32)65535L;
nuclear@14 1156 length = (png_uint_32)65535L;
nuclear@14 1157 }
nuclear@14 1158 #endif
nuclear@14 1159
nuclear@14 1160 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1161 png_ptr->chunkdata = (png_charp)png_malloc(png_ptr, length + 1);
nuclear@14 1162 slength = (png_size_t)length;
nuclear@14 1163 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
nuclear@14 1164
nuclear@14 1165 if (png_crc_finish(png_ptr, skip))
nuclear@14 1166 {
nuclear@14 1167 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1168 png_ptr->chunkdata = NULL;
nuclear@14 1169 return;
nuclear@14 1170 }
nuclear@14 1171
nuclear@14 1172 png_ptr->chunkdata[slength] = 0x00;
nuclear@14 1173
nuclear@14 1174 for (entry_start = (png_bytep)png_ptr->chunkdata; *entry_start; entry_start++)
nuclear@14 1175 /* empty loop to find end of name */ ;
nuclear@14 1176 ++entry_start;
nuclear@14 1177
nuclear@14 1178 /* a sample depth should follow the separator, and we should be on it */
nuclear@14 1179 if (entry_start > (png_bytep)png_ptr->chunkdata + slength - 2)
nuclear@14 1180 {
nuclear@14 1181 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1182 png_ptr->chunkdata = NULL;
nuclear@14 1183 png_warning(png_ptr, "malformed sPLT chunk");
nuclear@14 1184 return;
nuclear@14 1185 }
nuclear@14 1186
nuclear@14 1187 new_palette.depth = *entry_start++;
nuclear@14 1188 entry_size = (new_palette.depth == 8 ? 6 : 10);
nuclear@14 1189 data_length = (slength - (entry_start - (png_bytep)png_ptr->chunkdata));
nuclear@14 1190
nuclear@14 1191 /* integrity-check the data length */
nuclear@14 1192 if (data_length % entry_size)
nuclear@14 1193 {
nuclear@14 1194 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1195 png_ptr->chunkdata = NULL;
nuclear@14 1196 png_warning(png_ptr, "sPLT chunk has bad length");
nuclear@14 1197 return;
nuclear@14 1198 }
nuclear@14 1199
nuclear@14 1200 new_palette.nentries = (png_int_32) ( data_length / entry_size);
nuclear@14 1201 if ((png_uint_32) new_palette.nentries >
nuclear@14 1202 (png_uint_32) (PNG_SIZE_MAX / png_sizeof(png_sPLT_entry)))
nuclear@14 1203 {
nuclear@14 1204 png_warning(png_ptr, "sPLT chunk too long");
nuclear@14 1205 return;
nuclear@14 1206 }
nuclear@14 1207 new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
nuclear@14 1208 png_ptr, new_palette.nentries * png_sizeof(png_sPLT_entry));
nuclear@14 1209 if (new_palette.entries == NULL)
nuclear@14 1210 {
nuclear@14 1211 png_warning(png_ptr, "sPLT chunk requires too much memory");
nuclear@14 1212 return;
nuclear@14 1213 }
nuclear@14 1214
nuclear@14 1215 #ifndef PNG_NO_POINTER_INDEXING
nuclear@14 1216 for (i = 0; i < new_palette.nentries; i++)
nuclear@14 1217 {
nuclear@14 1218 png_sPLT_entryp pp = new_palette.entries + i;
nuclear@14 1219
nuclear@14 1220 if (new_palette.depth == 8)
nuclear@14 1221 {
nuclear@14 1222 pp->red = *entry_start++;
nuclear@14 1223 pp->green = *entry_start++;
nuclear@14 1224 pp->blue = *entry_start++;
nuclear@14 1225 pp->alpha = *entry_start++;
nuclear@14 1226 }
nuclear@14 1227 else
nuclear@14 1228 {
nuclear@14 1229 pp->red = png_get_uint_16(entry_start); entry_start += 2;
nuclear@14 1230 pp->green = png_get_uint_16(entry_start); entry_start += 2;
nuclear@14 1231 pp->blue = png_get_uint_16(entry_start); entry_start += 2;
nuclear@14 1232 pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
nuclear@14 1233 }
nuclear@14 1234 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
nuclear@14 1235 }
nuclear@14 1236 #else
nuclear@14 1237 pp = new_palette.entries;
nuclear@14 1238 for (i = 0; i < new_palette.nentries; i++)
nuclear@14 1239 {
nuclear@14 1240
nuclear@14 1241 if (new_palette.depth == 8)
nuclear@14 1242 {
nuclear@14 1243 pp[i].red = *entry_start++;
nuclear@14 1244 pp[i].green = *entry_start++;
nuclear@14 1245 pp[i].blue = *entry_start++;
nuclear@14 1246 pp[i].alpha = *entry_start++;
nuclear@14 1247 }
nuclear@14 1248 else
nuclear@14 1249 {
nuclear@14 1250 pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
nuclear@14 1251 pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
nuclear@14 1252 pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
nuclear@14 1253 pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
nuclear@14 1254 }
nuclear@14 1255 pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
nuclear@14 1256 }
nuclear@14 1257 #endif
nuclear@14 1258
nuclear@14 1259 /* discard all chunk data except the name and stash that */
nuclear@14 1260 new_palette.name = png_ptr->chunkdata;
nuclear@14 1261
nuclear@14 1262 png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
nuclear@14 1263
nuclear@14 1264 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1265 png_ptr->chunkdata = NULL;
nuclear@14 1266 png_free(png_ptr, new_palette.entries);
nuclear@14 1267 }
nuclear@14 1268 #endif /* PNG_READ_sPLT_SUPPORTED */
nuclear@14 1269
nuclear@14 1270 #if defined(PNG_READ_tRNS_SUPPORTED)
nuclear@14 1271 void /* PRIVATE */
nuclear@14 1272 png_handle_tRNS(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1273 {
nuclear@14 1274 png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
nuclear@14 1275
nuclear@14 1276 png_debug(1, "in png_handle_tRNS\n");
nuclear@14 1277
nuclear@14 1278 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1279 png_error(png_ptr, "Missing IHDR before tRNS");
nuclear@14 1280 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1281 {
nuclear@14 1282 png_warning(png_ptr, "Invalid tRNS after IDAT");
nuclear@14 1283 png_crc_finish(png_ptr, length);
nuclear@14 1284 return;
nuclear@14 1285 }
nuclear@14 1286 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS))
nuclear@14 1287 {
nuclear@14 1288 png_warning(png_ptr, "Duplicate tRNS chunk");
nuclear@14 1289 png_crc_finish(png_ptr, length);
nuclear@14 1290 return;
nuclear@14 1291 }
nuclear@14 1292
nuclear@14 1293 if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
nuclear@14 1294 {
nuclear@14 1295 png_byte buf[2];
nuclear@14 1296
nuclear@14 1297 if (length != 2)
nuclear@14 1298 {
nuclear@14 1299 png_warning(png_ptr, "Incorrect tRNS chunk length");
nuclear@14 1300 png_crc_finish(png_ptr, length);
nuclear@14 1301 return;
nuclear@14 1302 }
nuclear@14 1303
nuclear@14 1304 png_crc_read(png_ptr, buf, 2);
nuclear@14 1305 png_ptr->num_trans = 1;
nuclear@14 1306 png_ptr->trans_values.gray = png_get_uint_16(buf);
nuclear@14 1307 }
nuclear@14 1308 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
nuclear@14 1309 {
nuclear@14 1310 png_byte buf[6];
nuclear@14 1311
nuclear@14 1312 if (length != 6)
nuclear@14 1313 {
nuclear@14 1314 png_warning(png_ptr, "Incorrect tRNS chunk length");
nuclear@14 1315 png_crc_finish(png_ptr, length);
nuclear@14 1316 return;
nuclear@14 1317 }
nuclear@14 1318 png_crc_read(png_ptr, buf, (png_size_t)length);
nuclear@14 1319 png_ptr->num_trans = 1;
nuclear@14 1320 png_ptr->trans_values.red = png_get_uint_16(buf);
nuclear@14 1321 png_ptr->trans_values.green = png_get_uint_16(buf + 2);
nuclear@14 1322 png_ptr->trans_values.blue = png_get_uint_16(buf + 4);
nuclear@14 1323 }
nuclear@14 1324 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
nuclear@14 1325 {
nuclear@14 1326 if (!(png_ptr->mode & PNG_HAVE_PLTE))
nuclear@14 1327 {
nuclear@14 1328 /* Should be an error, but we can cope with it. */
nuclear@14 1329 png_warning(png_ptr, "Missing PLTE before tRNS");
nuclear@14 1330 }
nuclear@14 1331 if (length > (png_uint_32)png_ptr->num_palette ||
nuclear@14 1332 length > PNG_MAX_PALETTE_LENGTH)
nuclear@14 1333 {
nuclear@14 1334 png_warning(png_ptr, "Incorrect tRNS chunk length");
nuclear@14 1335 png_crc_finish(png_ptr, length);
nuclear@14 1336 return;
nuclear@14 1337 }
nuclear@14 1338 if (length == 0)
nuclear@14 1339 {
nuclear@14 1340 png_warning(png_ptr, "Zero length tRNS chunk");
nuclear@14 1341 png_crc_finish(png_ptr, length);
nuclear@14 1342 return;
nuclear@14 1343 }
nuclear@14 1344 png_crc_read(png_ptr, readbuf, (png_size_t)length);
nuclear@14 1345 png_ptr->num_trans = (png_uint_16)length;
nuclear@14 1346 }
nuclear@14 1347 else
nuclear@14 1348 {
nuclear@14 1349 png_warning(png_ptr, "tRNS chunk not allowed with alpha channel");
nuclear@14 1350 png_crc_finish(png_ptr, length);
nuclear@14 1351 return;
nuclear@14 1352 }
nuclear@14 1353
nuclear@14 1354 if (png_crc_finish(png_ptr, 0))
nuclear@14 1355 {
nuclear@14 1356 png_ptr->num_trans = 0;
nuclear@14 1357 return;
nuclear@14 1358 }
nuclear@14 1359
nuclear@14 1360 png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
nuclear@14 1361 &(png_ptr->trans_values));
nuclear@14 1362 }
nuclear@14 1363 #endif
nuclear@14 1364
nuclear@14 1365 #if defined(PNG_READ_bKGD_SUPPORTED)
nuclear@14 1366 void /* PRIVATE */
nuclear@14 1367 png_handle_bKGD(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1368 {
nuclear@14 1369 png_size_t truelen;
nuclear@14 1370 png_byte buf[6];
nuclear@14 1371
nuclear@14 1372 png_debug(1, "in png_handle_bKGD\n");
nuclear@14 1373
nuclear@14 1374 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1375 png_error(png_ptr, "Missing IHDR before bKGD");
nuclear@14 1376 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1377 {
nuclear@14 1378 png_warning(png_ptr, "Invalid bKGD after IDAT");
nuclear@14 1379 png_crc_finish(png_ptr, length);
nuclear@14 1380 return;
nuclear@14 1381 }
nuclear@14 1382 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
nuclear@14 1383 !(png_ptr->mode & PNG_HAVE_PLTE))
nuclear@14 1384 {
nuclear@14 1385 png_warning(png_ptr, "Missing PLTE before bKGD");
nuclear@14 1386 png_crc_finish(png_ptr, length);
nuclear@14 1387 return;
nuclear@14 1388 }
nuclear@14 1389 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD))
nuclear@14 1390 {
nuclear@14 1391 png_warning(png_ptr, "Duplicate bKGD chunk");
nuclear@14 1392 png_crc_finish(png_ptr, length);
nuclear@14 1393 return;
nuclear@14 1394 }
nuclear@14 1395
nuclear@14 1396 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
nuclear@14 1397 truelen = 1;
nuclear@14 1398 else if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
nuclear@14 1399 truelen = 6;
nuclear@14 1400 else
nuclear@14 1401 truelen = 2;
nuclear@14 1402
nuclear@14 1403 if (length != truelen)
nuclear@14 1404 {
nuclear@14 1405 png_warning(png_ptr, "Incorrect bKGD chunk length");
nuclear@14 1406 png_crc_finish(png_ptr, length);
nuclear@14 1407 return;
nuclear@14 1408 }
nuclear@14 1409
nuclear@14 1410 png_crc_read(png_ptr, buf, truelen);
nuclear@14 1411 if (png_crc_finish(png_ptr, 0))
nuclear@14 1412 return;
nuclear@14 1413
nuclear@14 1414 /* We convert the index value into RGB components so that we can allow
nuclear@14 1415 * arbitrary RGB values for background when we have transparency, and
nuclear@14 1416 * so it is easy to determine the RGB values of the background color
nuclear@14 1417 * from the info_ptr struct. */
nuclear@14 1418 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
nuclear@14 1419 {
nuclear@14 1420 png_ptr->background.index = buf[0];
nuclear@14 1421 if (info_ptr && info_ptr->num_palette)
nuclear@14 1422 {
nuclear@14 1423 if (buf[0] > info_ptr->num_palette)
nuclear@14 1424 {
nuclear@14 1425 png_warning(png_ptr, "Incorrect bKGD chunk index value");
nuclear@14 1426 return;
nuclear@14 1427 }
nuclear@14 1428 png_ptr->background.red =
nuclear@14 1429 (png_uint_16)png_ptr->palette[buf[0]].red;
nuclear@14 1430 png_ptr->background.green =
nuclear@14 1431 (png_uint_16)png_ptr->palette[buf[0]].green;
nuclear@14 1432 png_ptr->background.blue =
nuclear@14 1433 (png_uint_16)png_ptr->palette[buf[0]].blue;
nuclear@14 1434 }
nuclear@14 1435 }
nuclear@14 1436 else if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) /* GRAY */
nuclear@14 1437 {
nuclear@14 1438 png_ptr->background.red =
nuclear@14 1439 png_ptr->background.green =
nuclear@14 1440 png_ptr->background.blue =
nuclear@14 1441 png_ptr->background.gray = png_get_uint_16(buf);
nuclear@14 1442 }
nuclear@14 1443 else
nuclear@14 1444 {
nuclear@14 1445 png_ptr->background.red = png_get_uint_16(buf);
nuclear@14 1446 png_ptr->background.green = png_get_uint_16(buf + 2);
nuclear@14 1447 png_ptr->background.blue = png_get_uint_16(buf + 4);
nuclear@14 1448 }
nuclear@14 1449
nuclear@14 1450 png_set_bKGD(png_ptr, info_ptr, &(png_ptr->background));
nuclear@14 1451 }
nuclear@14 1452 #endif
nuclear@14 1453
nuclear@14 1454 #if defined(PNG_READ_hIST_SUPPORTED)
nuclear@14 1455 void /* PRIVATE */
nuclear@14 1456 png_handle_hIST(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1457 {
nuclear@14 1458 unsigned int num, i;
nuclear@14 1459 png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
nuclear@14 1460
nuclear@14 1461 png_debug(1, "in png_handle_hIST\n");
nuclear@14 1462
nuclear@14 1463 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1464 png_error(png_ptr, "Missing IHDR before hIST");
nuclear@14 1465 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1466 {
nuclear@14 1467 png_warning(png_ptr, "Invalid hIST after IDAT");
nuclear@14 1468 png_crc_finish(png_ptr, length);
nuclear@14 1469 return;
nuclear@14 1470 }
nuclear@14 1471 else if (!(png_ptr->mode & PNG_HAVE_PLTE))
nuclear@14 1472 {
nuclear@14 1473 png_warning(png_ptr, "Missing PLTE before hIST");
nuclear@14 1474 png_crc_finish(png_ptr, length);
nuclear@14 1475 return;
nuclear@14 1476 }
nuclear@14 1477 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST))
nuclear@14 1478 {
nuclear@14 1479 png_warning(png_ptr, "Duplicate hIST chunk");
nuclear@14 1480 png_crc_finish(png_ptr, length);
nuclear@14 1481 return;
nuclear@14 1482 }
nuclear@14 1483
nuclear@14 1484 num = length / 2 ;
nuclear@14 1485 if (num != (unsigned int) png_ptr->num_palette || num >
nuclear@14 1486 (unsigned int) PNG_MAX_PALETTE_LENGTH)
nuclear@14 1487 {
nuclear@14 1488 png_warning(png_ptr, "Incorrect hIST chunk length");
nuclear@14 1489 png_crc_finish(png_ptr, length);
nuclear@14 1490 return;
nuclear@14 1491 }
nuclear@14 1492
nuclear@14 1493 for (i = 0; i < num; i++)
nuclear@14 1494 {
nuclear@14 1495 png_byte buf[2];
nuclear@14 1496
nuclear@14 1497 png_crc_read(png_ptr, buf, 2);
nuclear@14 1498 readbuf[i] = png_get_uint_16(buf);
nuclear@14 1499 }
nuclear@14 1500
nuclear@14 1501 if (png_crc_finish(png_ptr, 0))
nuclear@14 1502 return;
nuclear@14 1503
nuclear@14 1504 png_set_hIST(png_ptr, info_ptr, readbuf);
nuclear@14 1505 }
nuclear@14 1506 #endif
nuclear@14 1507
nuclear@14 1508 #if defined(PNG_READ_pHYs_SUPPORTED)
nuclear@14 1509 void /* PRIVATE */
nuclear@14 1510 png_handle_pHYs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1511 {
nuclear@14 1512 png_byte buf[9];
nuclear@14 1513 png_uint_32 res_x, res_y;
nuclear@14 1514 int unit_type;
nuclear@14 1515
nuclear@14 1516 png_debug(1, "in png_handle_pHYs\n");
nuclear@14 1517
nuclear@14 1518 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1519 png_error(png_ptr, "Missing IHDR before pHYs");
nuclear@14 1520 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1521 {
nuclear@14 1522 png_warning(png_ptr, "Invalid pHYs after IDAT");
nuclear@14 1523 png_crc_finish(png_ptr, length);
nuclear@14 1524 return;
nuclear@14 1525 }
nuclear@14 1526 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs))
nuclear@14 1527 {
nuclear@14 1528 png_warning(png_ptr, "Duplicate pHYs chunk");
nuclear@14 1529 png_crc_finish(png_ptr, length);
nuclear@14 1530 return;
nuclear@14 1531 }
nuclear@14 1532
nuclear@14 1533 if (length != 9)
nuclear@14 1534 {
nuclear@14 1535 png_warning(png_ptr, "Incorrect pHYs chunk length");
nuclear@14 1536 png_crc_finish(png_ptr, length);
nuclear@14 1537 return;
nuclear@14 1538 }
nuclear@14 1539
nuclear@14 1540 png_crc_read(png_ptr, buf, 9);
nuclear@14 1541 if (png_crc_finish(png_ptr, 0))
nuclear@14 1542 return;
nuclear@14 1543
nuclear@14 1544 res_x = png_get_uint_32(buf);
nuclear@14 1545 res_y = png_get_uint_32(buf + 4);
nuclear@14 1546 unit_type = buf[8];
nuclear@14 1547 png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
nuclear@14 1548 }
nuclear@14 1549 #endif
nuclear@14 1550
nuclear@14 1551 #if defined(PNG_READ_oFFs_SUPPORTED)
nuclear@14 1552 void /* PRIVATE */
nuclear@14 1553 png_handle_oFFs(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1554 {
nuclear@14 1555 png_byte buf[9];
nuclear@14 1556 png_int_32 offset_x, offset_y;
nuclear@14 1557 int unit_type;
nuclear@14 1558
nuclear@14 1559 png_debug(1, "in png_handle_oFFs\n");
nuclear@14 1560
nuclear@14 1561 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1562 png_error(png_ptr, "Missing IHDR before oFFs");
nuclear@14 1563 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1564 {
nuclear@14 1565 png_warning(png_ptr, "Invalid oFFs after IDAT");
nuclear@14 1566 png_crc_finish(png_ptr, length);
nuclear@14 1567 return;
nuclear@14 1568 }
nuclear@14 1569 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs))
nuclear@14 1570 {
nuclear@14 1571 png_warning(png_ptr, "Duplicate oFFs chunk");
nuclear@14 1572 png_crc_finish(png_ptr, length);
nuclear@14 1573 return;
nuclear@14 1574 }
nuclear@14 1575
nuclear@14 1576 if (length != 9)
nuclear@14 1577 {
nuclear@14 1578 png_warning(png_ptr, "Incorrect oFFs chunk length");
nuclear@14 1579 png_crc_finish(png_ptr, length);
nuclear@14 1580 return;
nuclear@14 1581 }
nuclear@14 1582
nuclear@14 1583 png_crc_read(png_ptr, buf, 9);
nuclear@14 1584 if (png_crc_finish(png_ptr, 0))
nuclear@14 1585 return;
nuclear@14 1586
nuclear@14 1587 offset_x = png_get_int_32(buf);
nuclear@14 1588 offset_y = png_get_int_32(buf + 4);
nuclear@14 1589 unit_type = buf[8];
nuclear@14 1590 png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
nuclear@14 1591 }
nuclear@14 1592 #endif
nuclear@14 1593
nuclear@14 1594 #if defined(PNG_READ_pCAL_SUPPORTED)
nuclear@14 1595 /* read the pCAL chunk (described in the PNG Extensions document) */
nuclear@14 1596 void /* PRIVATE */
nuclear@14 1597 png_handle_pCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1598 {
nuclear@14 1599 png_int_32 X0, X1;
nuclear@14 1600 png_byte type, nparams;
nuclear@14 1601 png_charp buf, units, endptr;
nuclear@14 1602 png_charpp params;
nuclear@14 1603 png_size_t slength;
nuclear@14 1604 int i;
nuclear@14 1605
nuclear@14 1606 png_debug(1, "in png_handle_pCAL\n");
nuclear@14 1607
nuclear@14 1608 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1609 png_error(png_ptr, "Missing IHDR before pCAL");
nuclear@14 1610 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1611 {
nuclear@14 1612 png_warning(png_ptr, "Invalid pCAL after IDAT");
nuclear@14 1613 png_crc_finish(png_ptr, length);
nuclear@14 1614 return;
nuclear@14 1615 }
nuclear@14 1616 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL))
nuclear@14 1617 {
nuclear@14 1618 png_warning(png_ptr, "Duplicate pCAL chunk");
nuclear@14 1619 png_crc_finish(png_ptr, length);
nuclear@14 1620 return;
nuclear@14 1621 }
nuclear@14 1622
nuclear@14 1623 png_debug1(2, "Allocating and reading pCAL chunk data (%lu bytes)\n",
nuclear@14 1624 length + 1);
nuclear@14 1625 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1626 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
nuclear@14 1627 if (png_ptr->chunkdata == NULL)
nuclear@14 1628 {
nuclear@14 1629 png_warning(png_ptr, "No memory for pCAL purpose.");
nuclear@14 1630 return;
nuclear@14 1631 }
nuclear@14 1632 slength = (png_size_t)length;
nuclear@14 1633 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
nuclear@14 1634
nuclear@14 1635 if (png_crc_finish(png_ptr, 0))
nuclear@14 1636 {
nuclear@14 1637 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1638 png_ptr->chunkdata = NULL;
nuclear@14 1639 return;
nuclear@14 1640 }
nuclear@14 1641
nuclear@14 1642 png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
nuclear@14 1643
nuclear@14 1644 png_debug(3, "Finding end of pCAL purpose string\n");
nuclear@14 1645 for (buf = png_ptr->chunkdata; *buf; buf++)
nuclear@14 1646 /* empty loop */ ;
nuclear@14 1647
nuclear@14 1648 endptr = png_ptr->chunkdata + slength;
nuclear@14 1649
nuclear@14 1650 /* We need to have at least 12 bytes after the purpose string
nuclear@14 1651 in order to get the parameter information. */
nuclear@14 1652 if (endptr <= buf + 12)
nuclear@14 1653 {
nuclear@14 1654 png_warning(png_ptr, "Invalid pCAL data");
nuclear@14 1655 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1656 png_ptr->chunkdata = NULL;
nuclear@14 1657 return;
nuclear@14 1658 }
nuclear@14 1659
nuclear@14 1660 png_debug(3, "Reading pCAL X0, X1, type, nparams, and units\n");
nuclear@14 1661 X0 = png_get_int_32((png_bytep)buf+1);
nuclear@14 1662 X1 = png_get_int_32((png_bytep)buf+5);
nuclear@14 1663 type = buf[9];
nuclear@14 1664 nparams = buf[10];
nuclear@14 1665 units = buf + 11;
nuclear@14 1666
nuclear@14 1667 png_debug(3, "Checking pCAL equation type and number of parameters\n");
nuclear@14 1668 /* Check that we have the right number of parameters for known
nuclear@14 1669 equation types. */
nuclear@14 1670 if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
nuclear@14 1671 (type == PNG_EQUATION_BASE_E && nparams != 3) ||
nuclear@14 1672 (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
nuclear@14 1673 (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
nuclear@14 1674 {
nuclear@14 1675 png_warning(png_ptr, "Invalid pCAL parameters for equation type");
nuclear@14 1676 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1677 png_ptr->chunkdata = NULL;
nuclear@14 1678 return;
nuclear@14 1679 }
nuclear@14 1680 else if (type >= PNG_EQUATION_LAST)
nuclear@14 1681 {
nuclear@14 1682 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
nuclear@14 1683 }
nuclear@14 1684
nuclear@14 1685 for (buf = units; *buf; buf++)
nuclear@14 1686 /* Empty loop to move past the units string. */ ;
nuclear@14 1687
nuclear@14 1688 png_debug(3, "Allocating pCAL parameters array\n");
nuclear@14 1689 params = (png_charpp)png_malloc_warn(png_ptr,
nuclear@14 1690 (png_uint_32)(nparams * png_sizeof(png_charp))) ;
nuclear@14 1691 if (params == NULL)
nuclear@14 1692 {
nuclear@14 1693 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1694 png_ptr->chunkdata = NULL;
nuclear@14 1695 png_warning(png_ptr, "No memory for pCAL params.");
nuclear@14 1696 return;
nuclear@14 1697 }
nuclear@14 1698
nuclear@14 1699 /* Get pointers to the start of each parameter string. */
nuclear@14 1700 for (i = 0; i < (int)nparams; i++)
nuclear@14 1701 {
nuclear@14 1702 buf++; /* Skip the null string terminator from previous parameter. */
nuclear@14 1703
nuclear@14 1704 png_debug1(3, "Reading pCAL parameter %d\n", i);
nuclear@14 1705 for (params[i] = buf; buf <= endptr && *buf != 0x00; buf++)
nuclear@14 1706 /* Empty loop to move past each parameter string */ ;
nuclear@14 1707
nuclear@14 1708 /* Make sure we haven't run out of data yet */
nuclear@14 1709 if (buf > endptr)
nuclear@14 1710 {
nuclear@14 1711 png_warning(png_ptr, "Invalid pCAL data");
nuclear@14 1712 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1713 png_ptr->chunkdata = NULL;
nuclear@14 1714 png_free(png_ptr, params);
nuclear@14 1715 return;
nuclear@14 1716 }
nuclear@14 1717 }
nuclear@14 1718
nuclear@14 1719 png_set_pCAL(png_ptr, info_ptr, png_ptr->chunkdata, X0, X1, type, nparams,
nuclear@14 1720 units, params);
nuclear@14 1721
nuclear@14 1722 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1723 png_ptr->chunkdata = NULL;
nuclear@14 1724 png_free(png_ptr, params);
nuclear@14 1725 }
nuclear@14 1726 #endif
nuclear@14 1727
nuclear@14 1728 #if defined(PNG_READ_sCAL_SUPPORTED)
nuclear@14 1729 /* read the sCAL chunk */
nuclear@14 1730 void /* PRIVATE */
nuclear@14 1731 png_handle_sCAL(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1732 {
nuclear@14 1733 png_charp ep;
nuclear@14 1734 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 1735 double width, height;
nuclear@14 1736 png_charp vp;
nuclear@14 1737 #else
nuclear@14 1738 #ifdef PNG_FIXED_POINT_SUPPORTED
nuclear@14 1739 png_charp swidth, sheight;
nuclear@14 1740 #endif
nuclear@14 1741 #endif
nuclear@14 1742 png_size_t slength;
nuclear@14 1743
nuclear@14 1744 png_debug(1, "in png_handle_sCAL\n");
nuclear@14 1745
nuclear@14 1746 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1747 png_error(png_ptr, "Missing IHDR before sCAL");
nuclear@14 1748 else if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1749 {
nuclear@14 1750 png_warning(png_ptr, "Invalid sCAL after IDAT");
nuclear@14 1751 png_crc_finish(png_ptr, length);
nuclear@14 1752 return;
nuclear@14 1753 }
nuclear@14 1754 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL))
nuclear@14 1755 {
nuclear@14 1756 png_warning(png_ptr, "Duplicate sCAL chunk");
nuclear@14 1757 png_crc_finish(png_ptr, length);
nuclear@14 1758 return;
nuclear@14 1759 }
nuclear@14 1760
nuclear@14 1761 png_debug1(2, "Allocating and reading sCAL chunk data (%lu bytes)\n",
nuclear@14 1762 length + 1);
nuclear@14 1763 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
nuclear@14 1764 if (png_ptr->chunkdata == NULL)
nuclear@14 1765 {
nuclear@14 1766 png_warning(png_ptr, "Out of memory while processing sCAL chunk");
nuclear@14 1767 return;
nuclear@14 1768 }
nuclear@14 1769 slength = (png_size_t)length;
nuclear@14 1770 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
nuclear@14 1771
nuclear@14 1772 if (png_crc_finish(png_ptr, 0))
nuclear@14 1773 {
nuclear@14 1774 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1775 png_ptr->chunkdata = NULL;
nuclear@14 1776 return;
nuclear@14 1777 }
nuclear@14 1778
nuclear@14 1779 png_ptr->chunkdata[slength] = 0x00; /* null terminate the last string */
nuclear@14 1780
nuclear@14 1781 ep = png_ptr->chunkdata + 1; /* skip unit byte */
nuclear@14 1782
nuclear@14 1783 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 1784 width = png_strtod(png_ptr, ep, &vp);
nuclear@14 1785 if (*vp)
nuclear@14 1786 {
nuclear@14 1787 png_warning(png_ptr, "malformed width string in sCAL chunk");
nuclear@14 1788 return;
nuclear@14 1789 }
nuclear@14 1790 #else
nuclear@14 1791 #ifdef PNG_FIXED_POINT_SUPPORTED
nuclear@14 1792 swidth = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
nuclear@14 1793 if (swidth == NULL)
nuclear@14 1794 {
nuclear@14 1795 png_warning(png_ptr, "Out of memory while processing sCAL chunk width");
nuclear@14 1796 return;
nuclear@14 1797 }
nuclear@14 1798 png_memcpy(swidth, ep, (png_size_t)png_strlen(ep));
nuclear@14 1799 #endif
nuclear@14 1800 #endif
nuclear@14 1801
nuclear@14 1802 for (ep = png_ptr->chunkdata; *ep; ep++)
nuclear@14 1803 /* empty loop */ ;
nuclear@14 1804 ep++;
nuclear@14 1805
nuclear@14 1806 if (png_ptr->chunkdata + slength < ep)
nuclear@14 1807 {
nuclear@14 1808 png_warning(png_ptr, "Truncated sCAL chunk");
nuclear@14 1809 #if defined(PNG_FIXED_POINT_SUPPORTED) && \
nuclear@14 1810 !defined(PNG_FLOATING_POINT_SUPPORTED)
nuclear@14 1811 png_free(png_ptr, swidth);
nuclear@14 1812 #endif
nuclear@14 1813 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1814 png_ptr->chunkdata = NULL;
nuclear@14 1815 return;
nuclear@14 1816 }
nuclear@14 1817
nuclear@14 1818 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 1819 height = png_strtod(png_ptr, ep, &vp);
nuclear@14 1820 if (*vp)
nuclear@14 1821 {
nuclear@14 1822 png_warning(png_ptr, "malformed height string in sCAL chunk");
nuclear@14 1823 return;
nuclear@14 1824 }
nuclear@14 1825 #else
nuclear@14 1826 #ifdef PNG_FIXED_POINT_SUPPORTED
nuclear@14 1827 sheight = (png_charp)png_malloc_warn(png_ptr, png_strlen(ep) + 1);
nuclear@14 1828 if (sheight == NULL)
nuclear@14 1829 {
nuclear@14 1830 png_warning(png_ptr, "Out of memory while processing sCAL chunk height");
nuclear@14 1831 return;
nuclear@14 1832 }
nuclear@14 1833 png_memcpy(sheight, ep, (png_size_t)png_strlen(ep));
nuclear@14 1834 #endif
nuclear@14 1835 #endif
nuclear@14 1836
nuclear@14 1837 if (png_ptr->chunkdata + slength < ep
nuclear@14 1838 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 1839 || width <= 0. || height <= 0.
nuclear@14 1840 #endif
nuclear@14 1841 )
nuclear@14 1842 {
nuclear@14 1843 png_warning(png_ptr, "Invalid sCAL data");
nuclear@14 1844 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1845 png_ptr->chunkdata = NULL;
nuclear@14 1846 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
nuclear@14 1847 png_free(png_ptr, swidth);
nuclear@14 1848 png_free(png_ptr, sheight);
nuclear@14 1849 #endif
nuclear@14 1850 return;
nuclear@14 1851 }
nuclear@14 1852
nuclear@14 1853
nuclear@14 1854 #ifdef PNG_FLOATING_POINT_SUPPORTED
nuclear@14 1855 png_set_sCAL(png_ptr, info_ptr, png_ptr->chunkdata[0], width, height);
nuclear@14 1856 #else
nuclear@14 1857 #ifdef PNG_FIXED_POINT_SUPPORTED
nuclear@14 1858 png_set_sCAL_s(png_ptr, info_ptr, png_ptr->chunkdata[0], swidth, sheight);
nuclear@14 1859 #endif
nuclear@14 1860 #endif
nuclear@14 1861
nuclear@14 1862 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1863 png_ptr->chunkdata = NULL;
nuclear@14 1864 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
nuclear@14 1865 png_free(png_ptr, swidth);
nuclear@14 1866 png_free(png_ptr, sheight);
nuclear@14 1867 #endif
nuclear@14 1868 }
nuclear@14 1869 #endif
nuclear@14 1870
nuclear@14 1871 #if defined(PNG_READ_tIME_SUPPORTED)
nuclear@14 1872 void /* PRIVATE */
nuclear@14 1873 png_handle_tIME(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1874 {
nuclear@14 1875 png_byte buf[7];
nuclear@14 1876 png_time mod_time;
nuclear@14 1877
nuclear@14 1878 png_debug(1, "in png_handle_tIME\n");
nuclear@14 1879
nuclear@14 1880 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1881 png_error(png_ptr, "Out of place tIME chunk");
nuclear@14 1882 else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME))
nuclear@14 1883 {
nuclear@14 1884 png_warning(png_ptr, "Duplicate tIME chunk");
nuclear@14 1885 png_crc_finish(png_ptr, length);
nuclear@14 1886 return;
nuclear@14 1887 }
nuclear@14 1888
nuclear@14 1889 if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1890 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@14 1891
nuclear@14 1892 if (length != 7)
nuclear@14 1893 {
nuclear@14 1894 png_warning(png_ptr, "Incorrect tIME chunk length");
nuclear@14 1895 png_crc_finish(png_ptr, length);
nuclear@14 1896 return;
nuclear@14 1897 }
nuclear@14 1898
nuclear@14 1899 png_crc_read(png_ptr, buf, 7);
nuclear@14 1900 if (png_crc_finish(png_ptr, 0))
nuclear@14 1901 return;
nuclear@14 1902
nuclear@14 1903 mod_time.second = buf[6];
nuclear@14 1904 mod_time.minute = buf[5];
nuclear@14 1905 mod_time.hour = buf[4];
nuclear@14 1906 mod_time.day = buf[3];
nuclear@14 1907 mod_time.month = buf[2];
nuclear@14 1908 mod_time.year = png_get_uint_16(buf);
nuclear@14 1909
nuclear@14 1910 png_set_tIME(png_ptr, info_ptr, &mod_time);
nuclear@14 1911 }
nuclear@14 1912 #endif
nuclear@14 1913
nuclear@14 1914 #if defined(PNG_READ_tEXt_SUPPORTED)
nuclear@14 1915 /* Note: this does not properly handle chunks that are > 64K under DOS */
nuclear@14 1916 void /* PRIVATE */
nuclear@14 1917 png_handle_tEXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 1918 {
nuclear@14 1919 png_textp text_ptr;
nuclear@14 1920 png_charp key;
nuclear@14 1921 png_charp text;
nuclear@14 1922 png_uint_32 skip = 0;
nuclear@14 1923 png_size_t slength;
nuclear@14 1924 int ret;
nuclear@14 1925
nuclear@14 1926 png_debug(1, "in png_handle_tEXt\n");
nuclear@14 1927
nuclear@14 1928 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 1929 png_error(png_ptr, "Missing IHDR before tEXt");
nuclear@14 1930
nuclear@14 1931 if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 1932 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@14 1933
nuclear@14 1934 #ifdef PNG_MAX_MALLOC_64K
nuclear@14 1935 if (length > (png_uint_32)65535L)
nuclear@14 1936 {
nuclear@14 1937 png_warning(png_ptr, "tEXt chunk too large to fit in memory");
nuclear@14 1938 skip = length - (png_uint_32)65535L;
nuclear@14 1939 length = (png_uint_32)65535L;
nuclear@14 1940 }
nuclear@14 1941 #endif
nuclear@14 1942
nuclear@14 1943 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1944 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
nuclear@14 1945 if (png_ptr->chunkdata == NULL)
nuclear@14 1946 {
nuclear@14 1947 png_warning(png_ptr, "No memory to process text chunk.");
nuclear@14 1948 return;
nuclear@14 1949 }
nuclear@14 1950 slength = (png_size_t)length;
nuclear@14 1951 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
nuclear@14 1952
nuclear@14 1953 if (png_crc_finish(png_ptr, skip))
nuclear@14 1954 {
nuclear@14 1955 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1956 png_ptr->chunkdata = NULL;
nuclear@14 1957 return;
nuclear@14 1958 }
nuclear@14 1959
nuclear@14 1960 key = png_ptr->chunkdata;
nuclear@14 1961 key[slength] = 0x00;
nuclear@14 1962
nuclear@14 1963 for (text = key; *text; text++)
nuclear@14 1964 /* empty loop to find end of key */ ;
nuclear@14 1965
nuclear@14 1966 if (text != key + slength)
nuclear@14 1967 text++;
nuclear@14 1968
nuclear@14 1969 text_ptr = (png_textp)png_malloc_warn(png_ptr,
nuclear@14 1970 (png_uint_32)png_sizeof(png_text));
nuclear@14 1971 if (text_ptr == NULL)
nuclear@14 1972 {
nuclear@14 1973 png_warning(png_ptr, "Not enough memory to process text chunk.");
nuclear@14 1974 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1975 png_ptr->chunkdata = NULL;
nuclear@14 1976 return;
nuclear@14 1977 }
nuclear@14 1978 text_ptr->compression = PNG_TEXT_COMPRESSION_NONE;
nuclear@14 1979 text_ptr->key = key;
nuclear@14 1980 #ifdef PNG_iTXt_SUPPORTED
nuclear@14 1981 text_ptr->lang = NULL;
nuclear@14 1982 text_ptr->lang_key = NULL;
nuclear@14 1983 text_ptr->itxt_length = 0;
nuclear@14 1984 #endif
nuclear@14 1985 text_ptr->text = text;
nuclear@14 1986 text_ptr->text_length = png_strlen(text);
nuclear@14 1987
nuclear@14 1988 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
nuclear@14 1989
nuclear@14 1990 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 1991 png_ptr->chunkdata = NULL;
nuclear@14 1992 png_free(png_ptr, text_ptr);
nuclear@14 1993 if (ret)
nuclear@14 1994 png_warning(png_ptr, "Insufficient memory to process text chunk.");
nuclear@14 1995 }
nuclear@14 1996 #endif
nuclear@14 1997
nuclear@14 1998 #if defined(PNG_READ_zTXt_SUPPORTED)
nuclear@14 1999 /* note: this does not correctly handle chunks that are > 64K under DOS */
nuclear@14 2000 void /* PRIVATE */
nuclear@14 2001 png_handle_zTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 2002 {
nuclear@14 2003 png_textp text_ptr;
nuclear@14 2004 png_charp text;
nuclear@14 2005 int comp_type;
nuclear@14 2006 int ret;
nuclear@14 2007 png_size_t slength, prefix_len, data_len;
nuclear@14 2008
nuclear@14 2009 png_debug(1, "in png_handle_zTXt\n");
nuclear@14 2010 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 2011 png_error(png_ptr, "Missing IHDR before zTXt");
nuclear@14 2012
nuclear@14 2013 if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 2014 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@14 2015
nuclear@14 2016 #ifdef PNG_MAX_MALLOC_64K
nuclear@14 2017 /* We will no doubt have problems with chunks even half this size, but
nuclear@14 2018 there is no hard and fast rule to tell us where to stop. */
nuclear@14 2019 if (length > (png_uint_32)65535L)
nuclear@14 2020 {
nuclear@14 2021 png_warning(png_ptr, "zTXt chunk too large to fit in memory");
nuclear@14 2022 png_crc_finish(png_ptr, length);
nuclear@14 2023 return;
nuclear@14 2024 }
nuclear@14 2025 #endif
nuclear@14 2026
nuclear@14 2027 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2028 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
nuclear@14 2029 if (png_ptr->chunkdata == NULL)
nuclear@14 2030 {
nuclear@14 2031 png_warning(png_ptr, "Out of memory processing zTXt chunk.");
nuclear@14 2032 return;
nuclear@14 2033 }
nuclear@14 2034 slength = (png_size_t)length;
nuclear@14 2035 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
nuclear@14 2036 if (png_crc_finish(png_ptr, 0))
nuclear@14 2037 {
nuclear@14 2038 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2039 png_ptr->chunkdata = NULL;
nuclear@14 2040 return;
nuclear@14 2041 }
nuclear@14 2042
nuclear@14 2043 png_ptr->chunkdata[slength] = 0x00;
nuclear@14 2044
nuclear@14 2045 for (text = png_ptr->chunkdata; *text; text++)
nuclear@14 2046 /* empty loop */ ;
nuclear@14 2047
nuclear@14 2048 /* zTXt must have some text after the chunkdataword */
nuclear@14 2049 if (text >= png_ptr->chunkdata + slength - 2)
nuclear@14 2050 {
nuclear@14 2051 png_warning(png_ptr, "Truncated zTXt chunk");
nuclear@14 2052 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2053 png_ptr->chunkdata = NULL;
nuclear@14 2054 return;
nuclear@14 2055 }
nuclear@14 2056 else
nuclear@14 2057 {
nuclear@14 2058 comp_type = *(++text);
nuclear@14 2059 if (comp_type != PNG_TEXT_COMPRESSION_zTXt)
nuclear@14 2060 {
nuclear@14 2061 png_warning(png_ptr, "Unknown compression type in zTXt chunk");
nuclear@14 2062 comp_type = PNG_TEXT_COMPRESSION_zTXt;
nuclear@14 2063 }
nuclear@14 2064 text++; /* skip the compression_method byte */
nuclear@14 2065 }
nuclear@14 2066 prefix_len = text - png_ptr->chunkdata;
nuclear@14 2067
nuclear@14 2068 png_decompress_chunk(png_ptr, comp_type,
nuclear@14 2069 (png_size_t)length, prefix_len, &data_len);
nuclear@14 2070
nuclear@14 2071 text_ptr = (png_textp)png_malloc_warn(png_ptr,
nuclear@14 2072 (png_uint_32)png_sizeof(png_text));
nuclear@14 2073 if (text_ptr == NULL)
nuclear@14 2074 {
nuclear@14 2075 png_warning(png_ptr, "Not enough memory to process zTXt chunk.");
nuclear@14 2076 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2077 png_ptr->chunkdata = NULL;
nuclear@14 2078 return;
nuclear@14 2079 }
nuclear@14 2080 text_ptr->compression = comp_type;
nuclear@14 2081 text_ptr->key = png_ptr->chunkdata;
nuclear@14 2082 #ifdef PNG_iTXt_SUPPORTED
nuclear@14 2083 text_ptr->lang = NULL;
nuclear@14 2084 text_ptr->lang_key = NULL;
nuclear@14 2085 text_ptr->itxt_length = 0;
nuclear@14 2086 #endif
nuclear@14 2087 text_ptr->text = png_ptr->chunkdata + prefix_len;
nuclear@14 2088 text_ptr->text_length = data_len;
nuclear@14 2089
nuclear@14 2090 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
nuclear@14 2091
nuclear@14 2092 png_free(png_ptr, text_ptr);
nuclear@14 2093 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2094 png_ptr->chunkdata = NULL;
nuclear@14 2095 if (ret)
nuclear@14 2096 png_error(png_ptr, "Insufficient memory to store zTXt chunk.");
nuclear@14 2097 }
nuclear@14 2098 #endif
nuclear@14 2099
nuclear@14 2100 #if defined(PNG_READ_iTXt_SUPPORTED)
nuclear@14 2101 /* note: this does not correctly handle chunks that are > 64K under DOS */
nuclear@14 2102 void /* PRIVATE */
nuclear@14 2103 png_handle_iTXt(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 2104 {
nuclear@14 2105 png_textp text_ptr;
nuclear@14 2106 png_charp key, lang, text, lang_key;
nuclear@14 2107 int comp_flag;
nuclear@14 2108 int comp_type = 0;
nuclear@14 2109 int ret;
nuclear@14 2110 png_size_t slength, prefix_len, data_len;
nuclear@14 2111
nuclear@14 2112 png_debug(1, "in png_handle_iTXt\n");
nuclear@14 2113
nuclear@14 2114 if (!(png_ptr->mode & PNG_HAVE_IHDR))
nuclear@14 2115 png_error(png_ptr, "Missing IHDR before iTXt");
nuclear@14 2116
nuclear@14 2117 if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 2118 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@14 2119
nuclear@14 2120 #ifdef PNG_MAX_MALLOC_64K
nuclear@14 2121 /* We will no doubt have problems with chunks even half this size, but
nuclear@14 2122 there is no hard and fast rule to tell us where to stop. */
nuclear@14 2123 if (length > (png_uint_32)65535L)
nuclear@14 2124 {
nuclear@14 2125 png_warning(png_ptr, "iTXt chunk too large to fit in memory");
nuclear@14 2126 png_crc_finish(png_ptr, length);
nuclear@14 2127 return;
nuclear@14 2128 }
nuclear@14 2129 #endif
nuclear@14 2130
nuclear@14 2131 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2132 png_ptr->chunkdata = (png_charp)png_malloc_warn(png_ptr, length + 1);
nuclear@14 2133 if (png_ptr->chunkdata == NULL)
nuclear@14 2134 {
nuclear@14 2135 png_warning(png_ptr, "No memory to process iTXt chunk.");
nuclear@14 2136 return;
nuclear@14 2137 }
nuclear@14 2138 slength = (png_size_t)length;
nuclear@14 2139 png_crc_read(png_ptr, (png_bytep)png_ptr->chunkdata, slength);
nuclear@14 2140 if (png_crc_finish(png_ptr, 0))
nuclear@14 2141 {
nuclear@14 2142 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2143 png_ptr->chunkdata = NULL;
nuclear@14 2144 return;
nuclear@14 2145 }
nuclear@14 2146
nuclear@14 2147 png_ptr->chunkdata[slength] = 0x00;
nuclear@14 2148
nuclear@14 2149 for (lang = png_ptr->chunkdata; *lang; lang++)
nuclear@14 2150 /* empty loop */ ;
nuclear@14 2151 lang++; /* skip NUL separator */
nuclear@14 2152
nuclear@14 2153 /* iTXt must have a language tag (possibly empty), two compression bytes,
nuclear@14 2154 translated keyword (possibly empty), and possibly some text after the
nuclear@14 2155 keyword */
nuclear@14 2156
nuclear@14 2157 if (lang >= png_ptr->chunkdata + slength - 3)
nuclear@14 2158 {
nuclear@14 2159 png_warning(png_ptr, "Truncated iTXt chunk");
nuclear@14 2160 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2161 png_ptr->chunkdata = NULL;
nuclear@14 2162 return;
nuclear@14 2163 }
nuclear@14 2164 else
nuclear@14 2165 {
nuclear@14 2166 comp_flag = *lang++;
nuclear@14 2167 comp_type = *lang++;
nuclear@14 2168 }
nuclear@14 2169
nuclear@14 2170 for (lang_key = lang; *lang_key; lang_key++)
nuclear@14 2171 /* empty loop */ ;
nuclear@14 2172 lang_key++; /* skip NUL separator */
nuclear@14 2173
nuclear@14 2174 if (lang_key >= png_ptr->chunkdata + slength)
nuclear@14 2175 {
nuclear@14 2176 png_warning(png_ptr, "Truncated iTXt chunk");
nuclear@14 2177 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2178 png_ptr->chunkdata = NULL;
nuclear@14 2179 return;
nuclear@14 2180 }
nuclear@14 2181
nuclear@14 2182 for (text = lang_key; *text; text++)
nuclear@14 2183 /* empty loop */ ;
nuclear@14 2184 text++; /* skip NUL separator */
nuclear@14 2185 if (text >= png_ptr->chunkdata + slength)
nuclear@14 2186 {
nuclear@14 2187 png_warning(png_ptr, "Malformed iTXt chunk");
nuclear@14 2188 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2189 png_ptr->chunkdata = NULL;
nuclear@14 2190 return;
nuclear@14 2191 }
nuclear@14 2192
nuclear@14 2193 prefix_len = text - png_ptr->chunkdata;
nuclear@14 2194
nuclear@14 2195 key=png_ptr->chunkdata;
nuclear@14 2196 if (comp_flag)
nuclear@14 2197 png_decompress_chunk(png_ptr, comp_type,
nuclear@14 2198 (size_t)length, prefix_len, &data_len);
nuclear@14 2199 else
nuclear@14 2200 data_len = png_strlen(png_ptr->chunkdata + prefix_len);
nuclear@14 2201 text_ptr = (png_textp)png_malloc_warn(png_ptr,
nuclear@14 2202 (png_uint_32)png_sizeof(png_text));
nuclear@14 2203 if (text_ptr == NULL)
nuclear@14 2204 {
nuclear@14 2205 png_warning(png_ptr, "Not enough memory to process iTXt chunk.");
nuclear@14 2206 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2207 png_ptr->chunkdata = NULL;
nuclear@14 2208 return;
nuclear@14 2209 }
nuclear@14 2210 text_ptr->compression = (int)comp_flag + 1;
nuclear@14 2211 text_ptr->lang_key = png_ptr->chunkdata + (lang_key - key);
nuclear@14 2212 text_ptr->lang = png_ptr->chunkdata + (lang - key);
nuclear@14 2213 text_ptr->itxt_length = data_len;
nuclear@14 2214 text_ptr->text_length = 0;
nuclear@14 2215 text_ptr->key = png_ptr->chunkdata;
nuclear@14 2216 text_ptr->text = png_ptr->chunkdata + prefix_len;
nuclear@14 2217
nuclear@14 2218 ret = png_set_text_2(png_ptr, info_ptr, text_ptr, 1);
nuclear@14 2219
nuclear@14 2220 png_free(png_ptr, text_ptr);
nuclear@14 2221 png_free(png_ptr, png_ptr->chunkdata);
nuclear@14 2222 png_ptr->chunkdata = NULL;
nuclear@14 2223 if (ret)
nuclear@14 2224 png_error(png_ptr, "Insufficient memory to store iTXt chunk.");
nuclear@14 2225 }
nuclear@14 2226 #endif
nuclear@14 2227
nuclear@14 2228 /* This function is called when we haven't found a handler for a
nuclear@14 2229 chunk. If there isn't a problem with the chunk itself (ie bad
nuclear@14 2230 chunk name, CRC, or a critical chunk), the chunk is silently ignored
nuclear@14 2231 -- unless the PNG_FLAG_UNKNOWN_CHUNKS_SUPPORTED flag is on in which
nuclear@14 2232 case it will be saved away to be written out later. */
nuclear@14 2233 void /* PRIVATE */
nuclear@14 2234 png_handle_unknown(png_structp png_ptr, png_infop info_ptr, png_uint_32 length)
nuclear@14 2235 {
nuclear@14 2236 png_uint_32 skip = 0;
nuclear@14 2237
nuclear@14 2238 png_debug(1, "in png_handle_unknown\n");
nuclear@14 2239
nuclear@14 2240 if (png_ptr->mode & PNG_HAVE_IDAT)
nuclear@14 2241 {
nuclear@14 2242 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@14 2243 PNG_CONST PNG_IDAT;
nuclear@14 2244 #endif
nuclear@14 2245 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4)) /* not an IDAT */
nuclear@14 2246 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@14 2247 }
nuclear@14 2248
nuclear@14 2249 if (!(png_ptr->chunk_name[0] & 0x20))
nuclear@14 2250 {
nuclear@14 2251 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
nuclear@14 2252 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
nuclear@14 2253 PNG_HANDLE_CHUNK_ALWAYS
nuclear@14 2254 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
nuclear@14 2255 && png_ptr->read_user_chunk_fn == NULL
nuclear@14 2256 #endif
nuclear@14 2257 )
nuclear@14 2258 #endif
nuclear@14 2259 png_chunk_error(png_ptr, "unknown critical chunk");
nuclear@14 2260 }
nuclear@14 2261
nuclear@14 2262 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
nuclear@14 2263 if ((png_ptr->flags & PNG_FLAG_KEEP_UNKNOWN_CHUNKS) ||
nuclear@14 2264 (png_ptr->read_user_chunk_fn != NULL))
nuclear@14 2265 {
nuclear@14 2266 #ifdef PNG_MAX_MALLOC_64K
nuclear@14 2267 if (length > (png_uint_32)65535L)
nuclear@14 2268 {
nuclear@14 2269 png_warning(png_ptr, "unknown chunk too large to fit in memory");
nuclear@14 2270 skip = length - (png_uint_32)65535L;
nuclear@14 2271 length = (png_uint_32)65535L;
nuclear@14 2272 }
nuclear@14 2273 #endif
nuclear@14 2274 png_memcpy((png_charp)png_ptr->unknown_chunk.name,
nuclear@14 2275 (png_charp)png_ptr->chunk_name,
nuclear@14 2276 png_sizeof(png_ptr->unknown_chunk.name));
nuclear@14 2277 png_ptr->unknown_chunk.name[png_sizeof(png_ptr->unknown_chunk.name)-1] = '\0';
nuclear@14 2278 png_ptr->unknown_chunk.size = (png_size_t)length;
nuclear@14 2279 if (length == 0)
nuclear@14 2280 png_ptr->unknown_chunk.data = NULL;
nuclear@14 2281 else
nuclear@14 2282 {
nuclear@14 2283 png_ptr->unknown_chunk.data = (png_bytep)png_malloc(png_ptr, length);
nuclear@14 2284 png_crc_read(png_ptr, (png_bytep)png_ptr->unknown_chunk.data, length);
nuclear@14 2285 }
nuclear@14 2286 #if defined(PNG_READ_USER_CHUNKS_SUPPORTED)
nuclear@14 2287 if (png_ptr->read_user_chunk_fn != NULL)
nuclear@14 2288 {
nuclear@14 2289 /* callback to user unknown chunk handler */
nuclear@14 2290 int ret;
nuclear@14 2291 ret = (*(png_ptr->read_user_chunk_fn))
nuclear@14 2292 (png_ptr, &png_ptr->unknown_chunk);
nuclear@14 2293 if (ret < 0)
nuclear@14 2294 png_chunk_error(png_ptr, "error in user chunk");
nuclear@14 2295 if (ret == 0)
nuclear@14 2296 {
nuclear@14 2297 if (!(png_ptr->chunk_name[0] & 0x20))
nuclear@14 2298 if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name) !=
nuclear@14 2299 PNG_HANDLE_CHUNK_ALWAYS)
nuclear@14 2300 png_chunk_error(png_ptr, "unknown critical chunk");
nuclear@14 2301 png_set_unknown_chunks(png_ptr, info_ptr,
nuclear@14 2302 &png_ptr->unknown_chunk, 1);
nuclear@14 2303 }
nuclear@14 2304 }
nuclear@14 2305 else
nuclear@14 2306 #endif
nuclear@14 2307 png_set_unknown_chunks(png_ptr, info_ptr, &png_ptr->unknown_chunk, 1);
nuclear@14 2308 png_free(png_ptr, png_ptr->unknown_chunk.data);
nuclear@14 2309 png_ptr->unknown_chunk.data = NULL;
nuclear@14 2310 }
nuclear@14 2311 else
nuclear@14 2312 #endif
nuclear@14 2313 skip = length;
nuclear@14 2314
nuclear@14 2315 png_crc_finish(png_ptr, skip);
nuclear@14 2316
nuclear@14 2317 #if !defined(PNG_READ_USER_CHUNKS_SUPPORTED)
nuclear@14 2318 info_ptr = info_ptr; /* quiet compiler warnings about unused info_ptr */
nuclear@14 2319 #endif
nuclear@14 2320 }
nuclear@14 2321
nuclear@14 2322 /* This function is called to verify that a chunk name is valid.
nuclear@14 2323 This function can't have the "critical chunk check" incorporated
nuclear@14 2324 into it, since in the future we will need to be able to call user
nuclear@14 2325 functions to handle unknown critical chunks after we check that
nuclear@14 2326 the chunk name itself is valid. */
nuclear@14 2327
nuclear@14 2328 #define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97))
nuclear@14 2329
nuclear@14 2330 void /* PRIVATE */
nuclear@14 2331 png_check_chunk_name(png_structp png_ptr, png_bytep chunk_name)
nuclear@14 2332 {
nuclear@14 2333 png_debug(1, "in png_check_chunk_name\n");
nuclear@14 2334 if (isnonalpha(chunk_name[0]) || isnonalpha(chunk_name[1]) ||
nuclear@14 2335 isnonalpha(chunk_name[2]) || isnonalpha(chunk_name[3]))
nuclear@14 2336 {
nuclear@14 2337 png_chunk_error(png_ptr, "invalid chunk type");
nuclear@14 2338 }
nuclear@14 2339 }
nuclear@14 2340
nuclear@14 2341 /* Combines the row recently read in with the existing pixels in the
nuclear@14 2342 row. This routine takes care of alpha and transparency if requested.
nuclear@14 2343 This routine also handles the two methods of progressive display
nuclear@14 2344 of interlaced images, depending on the mask value.
nuclear@14 2345 The mask value describes which pixels are to be combined with
nuclear@14 2346 the row. The pattern always repeats every 8 pixels, so just 8
nuclear@14 2347 bits are needed. A one indicates the pixel is to be combined,
nuclear@14 2348 a zero indicates the pixel is to be skipped. This is in addition
nuclear@14 2349 to any alpha or transparency value associated with the pixel. If
nuclear@14 2350 you want all pixels to be combined, pass 0xff (255) in mask. */
nuclear@14 2351
nuclear@14 2352 void /* PRIVATE */
nuclear@14 2353 png_combine_row(png_structp png_ptr, png_bytep row, int mask)
nuclear@14 2354 {
nuclear@14 2355 png_debug(1, "in png_combine_row\n");
nuclear@14 2356 if (mask == 0xff)
nuclear@14 2357 {
nuclear@14 2358 png_memcpy(row, png_ptr->row_buf + 1,
nuclear@14 2359 PNG_ROWBYTES(png_ptr->row_info.pixel_depth, png_ptr->width));
nuclear@14 2360 }
nuclear@14 2361 else
nuclear@14 2362 {
nuclear@14 2363 switch (png_ptr->row_info.pixel_depth)
nuclear@14 2364 {
nuclear@14 2365 case 1:
nuclear@14 2366 {
nuclear@14 2367 png_bytep sp = png_ptr->row_buf + 1;
nuclear@14 2368 png_bytep dp = row;
nuclear@14 2369 int s_inc, s_start, s_end;
nuclear@14 2370 int m = 0x80;
nuclear@14 2371 int shift;
nuclear@14 2372 png_uint_32 i;
nuclear@14 2373 png_uint_32 row_width = png_ptr->width;
nuclear@14 2374
nuclear@14 2375 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
nuclear@14 2376 if (png_ptr->transformations & PNG_PACKSWAP)
nuclear@14 2377 {
nuclear@14 2378 s_start = 0;
nuclear@14 2379 s_end = 7;
nuclear@14 2380 s_inc = 1;
nuclear@14 2381 }
nuclear@14 2382 else
nuclear@14 2383 #endif
nuclear@14 2384 {
nuclear@14 2385 s_start = 7;
nuclear@14 2386 s_end = 0;
nuclear@14 2387 s_inc = -1;
nuclear@14 2388 }
nuclear@14 2389
nuclear@14 2390 shift = s_start;
nuclear@14 2391
nuclear@14 2392 for (i = 0; i < row_width; i++)
nuclear@14 2393 {
nuclear@14 2394 if (m & mask)
nuclear@14 2395 {
nuclear@14 2396 int value;
nuclear@14 2397
nuclear@14 2398 value = (*sp >> shift) & 0x01;
nuclear@14 2399 *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
nuclear@14 2400 *dp |= (png_byte)(value << shift);
nuclear@14 2401 }
nuclear@14 2402
nuclear@14 2403 if (shift == s_end)
nuclear@14 2404 {
nuclear@14 2405 shift = s_start;
nuclear@14 2406 sp++;
nuclear@14 2407 dp++;
nuclear@14 2408 }
nuclear@14 2409 else
nuclear@14 2410 shift += s_inc;
nuclear@14 2411
nuclear@14 2412 if (m == 1)
nuclear@14 2413 m = 0x80;
nuclear@14 2414 else
nuclear@14 2415 m >>= 1;
nuclear@14 2416 }
nuclear@14 2417 break;
nuclear@14 2418 }
nuclear@14 2419 case 2:
nuclear@14 2420 {
nuclear@14 2421 png_bytep sp = png_ptr->row_buf + 1;
nuclear@14 2422 png_bytep dp = row;
nuclear@14 2423 int s_start, s_end, s_inc;
nuclear@14 2424 int m = 0x80;
nuclear@14 2425 int shift;
nuclear@14 2426 png_uint_32 i;
nuclear@14 2427 png_uint_32 row_width = png_ptr->width;
nuclear@14 2428 int value;
nuclear@14 2429
nuclear@14 2430 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
nuclear@14 2431 if (png_ptr->transformations & PNG_PACKSWAP)
nuclear@14 2432 {
nuclear@14 2433 s_start = 0;
nuclear@14 2434 s_end = 6;
nuclear@14 2435 s_inc = 2;
nuclear@14 2436 }
nuclear@14 2437 else
nuclear@14 2438 #endif
nuclear@14 2439 {
nuclear@14 2440 s_start = 6;
nuclear@14 2441 s_end = 0;
nuclear@14 2442 s_inc = -2;
nuclear@14 2443 }
nuclear@14 2444
nuclear@14 2445 shift = s_start;
nuclear@14 2446
nuclear@14 2447 for (i = 0; i < row_width; i++)
nuclear@14 2448 {
nuclear@14 2449 if (m & mask)
nuclear@14 2450 {
nuclear@14 2451 value = (*sp >> shift) & 0x03;
nuclear@14 2452 *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
nuclear@14 2453 *dp |= (png_byte)(value << shift);
nuclear@14 2454 }
nuclear@14 2455
nuclear@14 2456 if (shift == s_end)
nuclear@14 2457 {
nuclear@14 2458 shift = s_start;
nuclear@14 2459 sp++;
nuclear@14 2460 dp++;
nuclear@14 2461 }
nuclear@14 2462 else
nuclear@14 2463 shift += s_inc;
nuclear@14 2464 if (m == 1)
nuclear@14 2465 m = 0x80;
nuclear@14 2466 else
nuclear@14 2467 m >>= 1;
nuclear@14 2468 }
nuclear@14 2469 break;
nuclear@14 2470 }
nuclear@14 2471 case 4:
nuclear@14 2472 {
nuclear@14 2473 png_bytep sp = png_ptr->row_buf + 1;
nuclear@14 2474 png_bytep dp = row;
nuclear@14 2475 int s_start, s_end, s_inc;
nuclear@14 2476 int m = 0x80;
nuclear@14 2477 int shift;
nuclear@14 2478 png_uint_32 i;
nuclear@14 2479 png_uint_32 row_width = png_ptr->width;
nuclear@14 2480 int value;
nuclear@14 2481
nuclear@14 2482 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
nuclear@14 2483 if (png_ptr->transformations & PNG_PACKSWAP)
nuclear@14 2484 {
nuclear@14 2485 s_start = 0;
nuclear@14 2486 s_end = 4;
nuclear@14 2487 s_inc = 4;
nuclear@14 2488 }
nuclear@14 2489 else
nuclear@14 2490 #endif
nuclear@14 2491 {
nuclear@14 2492 s_start = 4;
nuclear@14 2493 s_end = 0;
nuclear@14 2494 s_inc = -4;
nuclear@14 2495 }
nuclear@14 2496 shift = s_start;
nuclear@14 2497
nuclear@14 2498 for (i = 0; i < row_width; i++)
nuclear@14 2499 {
nuclear@14 2500 if (m & mask)
nuclear@14 2501 {
nuclear@14 2502 value = (*sp >> shift) & 0xf;
nuclear@14 2503 *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
nuclear@14 2504 *dp |= (png_byte)(value << shift);
nuclear@14 2505 }
nuclear@14 2506
nuclear@14 2507 if (shift == s_end)
nuclear@14 2508 {
nuclear@14 2509 shift = s_start;
nuclear@14 2510 sp++;
nuclear@14 2511 dp++;
nuclear@14 2512 }
nuclear@14 2513 else
nuclear@14 2514 shift += s_inc;
nuclear@14 2515 if (m == 1)
nuclear@14 2516 m = 0x80;
nuclear@14 2517 else
nuclear@14 2518 m >>= 1;
nuclear@14 2519 }
nuclear@14 2520 break;
nuclear@14 2521 }
nuclear@14 2522 default:
nuclear@14 2523 {
nuclear@14 2524 png_bytep sp = png_ptr->row_buf + 1;
nuclear@14 2525 png_bytep dp = row;
nuclear@14 2526 png_size_t pixel_bytes = (png_ptr->row_info.pixel_depth >> 3);
nuclear@14 2527 png_uint_32 i;
nuclear@14 2528 png_uint_32 row_width = png_ptr->width;
nuclear@14 2529 png_byte m = 0x80;
nuclear@14 2530
nuclear@14 2531
nuclear@14 2532 for (i = 0; i < row_width; i++)
nuclear@14 2533 {
nuclear@14 2534 if (m & mask)
nuclear@14 2535 {
nuclear@14 2536 png_memcpy(dp, sp, pixel_bytes);
nuclear@14 2537 }
nuclear@14 2538
nuclear@14 2539 sp += pixel_bytes;
nuclear@14 2540 dp += pixel_bytes;
nuclear@14 2541
nuclear@14 2542 if (m == 1)
nuclear@14 2543 m = 0x80;
nuclear@14 2544 else
nuclear@14 2545 m >>= 1;
nuclear@14 2546 }
nuclear@14 2547 break;
nuclear@14 2548 }
nuclear@14 2549 }
nuclear@14 2550 }
nuclear@14 2551 }
nuclear@14 2552
nuclear@14 2553 #ifdef PNG_READ_INTERLACING_SUPPORTED
nuclear@14 2554 /* OLD pre-1.0.9 interface:
nuclear@14 2555 void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass,
nuclear@14 2556 png_uint_32 transformations)
nuclear@14 2557 */
nuclear@14 2558 void /* PRIVATE */
nuclear@14 2559 png_do_read_interlace(png_structp png_ptr)
nuclear@14 2560 {
nuclear@14 2561 png_row_infop row_info = &(png_ptr->row_info);
nuclear@14 2562 png_bytep row = png_ptr->row_buf + 1;
nuclear@14 2563 int pass = png_ptr->pass;
nuclear@14 2564 png_uint_32 transformations = png_ptr->transformations;
nuclear@14 2565 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@14 2566 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
nuclear@14 2567 /* offset to next interlace block */
nuclear@14 2568 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
nuclear@14 2569 #endif
nuclear@14 2570
nuclear@14 2571 png_debug(1, "in png_do_read_interlace\n");
nuclear@14 2572 if (row != NULL && row_info != NULL)
nuclear@14 2573 {
nuclear@14 2574 png_uint_32 final_width;
nuclear@14 2575
nuclear@14 2576 final_width = row_info->width * png_pass_inc[pass];
nuclear@14 2577
nuclear@14 2578 switch (row_info->pixel_depth)
nuclear@14 2579 {
nuclear@14 2580 case 1:
nuclear@14 2581 {
nuclear@14 2582 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
nuclear@14 2583 png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
nuclear@14 2584 int sshift, dshift;
nuclear@14 2585 int s_start, s_end, s_inc;
nuclear@14 2586 int jstop = png_pass_inc[pass];
nuclear@14 2587 png_byte v;
nuclear@14 2588 png_uint_32 i;
nuclear@14 2589 int j;
nuclear@14 2590
nuclear@14 2591 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
nuclear@14 2592 if (transformations & PNG_PACKSWAP)
nuclear@14 2593 {
nuclear@14 2594 sshift = (int)((row_info->width + 7) & 0x07);
nuclear@14 2595 dshift = (int)((final_width + 7) & 0x07);
nuclear@14 2596 s_start = 7;
nuclear@14 2597 s_end = 0;
nuclear@14 2598 s_inc = -1;
nuclear@14 2599 }
nuclear@14 2600 else
nuclear@14 2601 #endif
nuclear@14 2602 {
nuclear@14 2603 sshift = 7 - (int)((row_info->width + 7) & 0x07);
nuclear@14 2604 dshift = 7 - (int)((final_width + 7) & 0x07);
nuclear@14 2605 s_start = 0;
nuclear@14 2606 s_end = 7;
nuclear@14 2607 s_inc = 1;
nuclear@14 2608 }
nuclear@14 2609
nuclear@14 2610 for (i = 0; i < row_info->width; i++)
nuclear@14 2611 {
nuclear@14 2612 v = (png_byte)((*sp >> sshift) & 0x01);
nuclear@14 2613 for (j = 0; j < jstop; j++)
nuclear@14 2614 {
nuclear@14 2615 *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
nuclear@14 2616 *dp |= (png_byte)(v << dshift);
nuclear@14 2617 if (dshift == s_end)
nuclear@14 2618 {
nuclear@14 2619 dshift = s_start;
nuclear@14 2620 dp--;
nuclear@14 2621 }
nuclear@14 2622 else
nuclear@14 2623 dshift += s_inc;
nuclear@14 2624 }
nuclear@14 2625 if (sshift == s_end)
nuclear@14 2626 {
nuclear@14 2627 sshift = s_start;
nuclear@14 2628 sp--;
nuclear@14 2629 }
nuclear@14 2630 else
nuclear@14 2631 sshift += s_inc;
nuclear@14 2632 }
nuclear@14 2633 break;
nuclear@14 2634 }
nuclear@14 2635 case 2:
nuclear@14 2636 {
nuclear@14 2637 png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
nuclear@14 2638 png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
nuclear@14 2639 int sshift, dshift;
nuclear@14 2640 int s_start, s_end, s_inc;
nuclear@14 2641 int jstop = png_pass_inc[pass];
nuclear@14 2642 png_uint_32 i;
nuclear@14 2643
nuclear@14 2644 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
nuclear@14 2645 if (transformations & PNG_PACKSWAP)
nuclear@14 2646 {
nuclear@14 2647 sshift = (int)(((row_info->width + 3) & 0x03) << 1);
nuclear@14 2648 dshift = (int)(((final_width + 3) & 0x03) << 1);
nuclear@14 2649 s_start = 6;
nuclear@14 2650 s_end = 0;
nuclear@14 2651 s_inc = -2;
nuclear@14 2652 }
nuclear@14 2653 else
nuclear@14 2654 #endif
nuclear@14 2655 {
nuclear@14 2656 sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
nuclear@14 2657 dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
nuclear@14 2658 s_start = 0;
nuclear@14 2659 s_end = 6;
nuclear@14 2660 s_inc = 2;
nuclear@14 2661 }
nuclear@14 2662
nuclear@14 2663 for (i = 0; i < row_info->width; i++)
nuclear@14 2664 {
nuclear@14 2665 png_byte v;
nuclear@14 2666 int j;
nuclear@14 2667
nuclear@14 2668 v = (png_byte)((*sp >> sshift) & 0x03);
nuclear@14 2669 for (j = 0; j < jstop; j++)
nuclear@14 2670 {
nuclear@14 2671 *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
nuclear@14 2672 *dp |= (png_byte)(v << dshift);
nuclear@14 2673 if (dshift == s_end)
nuclear@14 2674 {
nuclear@14 2675 dshift = s_start;
nuclear@14 2676 dp--;
nuclear@14 2677 }
nuclear@14 2678 else
nuclear@14 2679 dshift += s_inc;
nuclear@14 2680 }
nuclear@14 2681 if (sshift == s_end)
nuclear@14 2682 {
nuclear@14 2683 sshift = s_start;
nuclear@14 2684 sp--;
nuclear@14 2685 }
nuclear@14 2686 else
nuclear@14 2687 sshift += s_inc;
nuclear@14 2688 }
nuclear@14 2689 break;
nuclear@14 2690 }
nuclear@14 2691 case 4:
nuclear@14 2692 {
nuclear@14 2693 png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
nuclear@14 2694 png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
nuclear@14 2695 int sshift, dshift;
nuclear@14 2696 int s_start, s_end, s_inc;
nuclear@14 2697 png_uint_32 i;
nuclear@14 2698 int jstop = png_pass_inc[pass];
nuclear@14 2699
nuclear@14 2700 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
nuclear@14 2701 if (transformations & PNG_PACKSWAP)
nuclear@14 2702 {
nuclear@14 2703 sshift = (int)(((row_info->width + 1) & 0x01) << 2);
nuclear@14 2704 dshift = (int)(((final_width + 1) & 0x01) << 2);
nuclear@14 2705 s_start = 4;
nuclear@14 2706 s_end = 0;
nuclear@14 2707 s_inc = -4;
nuclear@14 2708 }
nuclear@14 2709 else
nuclear@14 2710 #endif
nuclear@14 2711 {
nuclear@14 2712 sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
nuclear@14 2713 dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
nuclear@14 2714 s_start = 0;
nuclear@14 2715 s_end = 4;
nuclear@14 2716 s_inc = 4;
nuclear@14 2717 }
nuclear@14 2718
nuclear@14 2719 for (i = 0; i < row_info->width; i++)
nuclear@14 2720 {
nuclear@14 2721 png_byte v = (png_byte)((*sp >> sshift) & 0xf);
nuclear@14 2722 int j;
nuclear@14 2723
nuclear@14 2724 for (j = 0; j < jstop; j++)
nuclear@14 2725 {
nuclear@14 2726 *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
nuclear@14 2727 *dp |= (png_byte)(v << dshift);
nuclear@14 2728 if (dshift == s_end)
nuclear@14 2729 {
nuclear@14 2730 dshift = s_start;
nuclear@14 2731 dp--;
nuclear@14 2732 }
nuclear@14 2733 else
nuclear@14 2734 dshift += s_inc;
nuclear@14 2735 }
nuclear@14 2736 if (sshift == s_end)
nuclear@14 2737 {
nuclear@14 2738 sshift = s_start;
nuclear@14 2739 sp--;
nuclear@14 2740 }
nuclear@14 2741 else
nuclear@14 2742 sshift += s_inc;
nuclear@14 2743 }
nuclear@14 2744 break;
nuclear@14 2745 }
nuclear@14 2746 default:
nuclear@14 2747 {
nuclear@14 2748 png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
nuclear@14 2749 png_bytep sp = row + (png_size_t)(row_info->width - 1) * pixel_bytes;
nuclear@14 2750 png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
nuclear@14 2751
nuclear@14 2752 int jstop = png_pass_inc[pass];
nuclear@14 2753 png_uint_32 i;
nuclear@14 2754
nuclear@14 2755 for (i = 0; i < row_info->width; i++)
nuclear@14 2756 {
nuclear@14 2757 png_byte v[8];
nuclear@14 2758 int j;
nuclear@14 2759
nuclear@14 2760 png_memcpy(v, sp, pixel_bytes);
nuclear@14 2761 for (j = 0; j < jstop; j++)
nuclear@14 2762 {
nuclear@14 2763 png_memcpy(dp, v, pixel_bytes);
nuclear@14 2764 dp -= pixel_bytes;
nuclear@14 2765 }
nuclear@14 2766 sp -= pixel_bytes;
nuclear@14 2767 }
nuclear@14 2768 break;
nuclear@14 2769 }
nuclear@14 2770 }
nuclear@14 2771 row_info->width = final_width;
nuclear@14 2772 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
nuclear@14 2773 }
nuclear@14 2774 #if !defined(PNG_READ_PACKSWAP_SUPPORTED)
nuclear@14 2775 transformations = transformations; /* silence compiler warning */
nuclear@14 2776 #endif
nuclear@14 2777 }
nuclear@14 2778 #endif /* PNG_READ_INTERLACING_SUPPORTED */
nuclear@14 2779
nuclear@14 2780 void /* PRIVATE */
nuclear@14 2781 png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
nuclear@14 2782 png_bytep prev_row, int filter)
nuclear@14 2783 {
nuclear@14 2784 png_debug(1, "in png_read_filter_row\n");
nuclear@14 2785 png_debug2(2, "row = %lu, filter = %d\n", png_ptr->row_number, filter);
nuclear@14 2786 switch (filter)
nuclear@14 2787 {
nuclear@14 2788 case PNG_FILTER_VALUE_NONE:
nuclear@14 2789 break;
nuclear@14 2790 case PNG_FILTER_VALUE_SUB:
nuclear@14 2791 {
nuclear@14 2792 png_uint_32 i;
nuclear@14 2793 png_uint_32 istop = row_info->rowbytes;
nuclear@14 2794 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
nuclear@14 2795 png_bytep rp = row + bpp;
nuclear@14 2796 png_bytep lp = row;
nuclear@14 2797
nuclear@14 2798 for (i = bpp; i < istop; i++)
nuclear@14 2799 {
nuclear@14 2800 *rp = (png_byte)(((int)(*rp) + (int)(*lp++)) & 0xff);
nuclear@14 2801 rp++;
nuclear@14 2802 }
nuclear@14 2803 break;
nuclear@14 2804 }
nuclear@14 2805 case PNG_FILTER_VALUE_UP:
nuclear@14 2806 {
nuclear@14 2807 png_uint_32 i;
nuclear@14 2808 png_uint_32 istop = row_info->rowbytes;
nuclear@14 2809 png_bytep rp = row;
nuclear@14 2810 png_bytep pp = prev_row;
nuclear@14 2811
nuclear@14 2812 for (i = 0; i < istop; i++)
nuclear@14 2813 {
nuclear@14 2814 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
nuclear@14 2815 rp++;
nuclear@14 2816 }
nuclear@14 2817 break;
nuclear@14 2818 }
nuclear@14 2819 case PNG_FILTER_VALUE_AVG:
nuclear@14 2820 {
nuclear@14 2821 png_uint_32 i;
nuclear@14 2822 png_bytep rp = row;
nuclear@14 2823 png_bytep pp = prev_row;
nuclear@14 2824 png_bytep lp = row;
nuclear@14 2825 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
nuclear@14 2826 png_uint_32 istop = row_info->rowbytes - bpp;
nuclear@14 2827
nuclear@14 2828 for (i = 0; i < bpp; i++)
nuclear@14 2829 {
nuclear@14 2830 *rp = (png_byte)(((int)(*rp) +
nuclear@14 2831 ((int)(*pp++) / 2 )) & 0xff);
nuclear@14 2832 rp++;
nuclear@14 2833 }
nuclear@14 2834
nuclear@14 2835 for (i = 0; i < istop; i++)
nuclear@14 2836 {
nuclear@14 2837 *rp = (png_byte)(((int)(*rp) +
nuclear@14 2838 (int)(*pp++ + *lp++) / 2 ) & 0xff);
nuclear@14 2839 rp++;
nuclear@14 2840 }
nuclear@14 2841 break;
nuclear@14 2842 }
nuclear@14 2843 case PNG_FILTER_VALUE_PAETH:
nuclear@14 2844 {
nuclear@14 2845 png_uint_32 i;
nuclear@14 2846 png_bytep rp = row;
nuclear@14 2847 png_bytep pp = prev_row;
nuclear@14 2848 png_bytep lp = row;
nuclear@14 2849 png_bytep cp = prev_row;
nuclear@14 2850 png_uint_32 bpp = (row_info->pixel_depth + 7) >> 3;
nuclear@14 2851 png_uint_32 istop=row_info->rowbytes - bpp;
nuclear@14 2852
nuclear@14 2853 for (i = 0; i < bpp; i++)
nuclear@14 2854 {
nuclear@14 2855 *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff);
nuclear@14 2856 rp++;
nuclear@14 2857 }
nuclear@14 2858
nuclear@14 2859 for (i = 0; i < istop; i++) /* use leftover rp,pp */
nuclear@14 2860 {
nuclear@14 2861 int a, b, c, pa, pb, pc, p;
nuclear@14 2862
nuclear@14 2863 a = *lp++;
nuclear@14 2864 b = *pp++;
nuclear@14 2865 c = *cp++;
nuclear@14 2866
nuclear@14 2867 p = b - c;
nuclear@14 2868 pc = a - c;
nuclear@14 2869
nuclear@14 2870 #ifdef PNG_USE_ABS
nuclear@14 2871 pa = abs(p);
nuclear@14 2872 pb = abs(pc);
nuclear@14 2873 pc = abs(p + pc);
nuclear@14 2874 #else
nuclear@14 2875 pa = p < 0 ? -p : p;
nuclear@14 2876 pb = pc < 0 ? -pc : pc;
nuclear@14 2877 pc = (p + pc) < 0 ? -(p + pc) : p + pc;
nuclear@14 2878 #endif
nuclear@14 2879
nuclear@14 2880 /*
nuclear@14 2881 if (pa <= pb && pa <= pc)
nuclear@14 2882 p = a;
nuclear@14 2883 else if (pb <= pc)
nuclear@14 2884 p = b;
nuclear@14 2885 else
nuclear@14 2886 p = c;
nuclear@14 2887 */
nuclear@14 2888
nuclear@14 2889 p = (pa <= pb && pa <= pc) ? a : (pb <= pc) ? b : c;
nuclear@14 2890
nuclear@14 2891 *rp = (png_byte)(((int)(*rp) + p) & 0xff);
nuclear@14 2892 rp++;
nuclear@14 2893 }
nuclear@14 2894 break;
nuclear@14 2895 }
nuclear@14 2896 default:
nuclear@14 2897 png_warning(png_ptr, "Ignoring bad adaptive filter type");
nuclear@14 2898 *row = 0;
nuclear@14 2899 break;
nuclear@14 2900 }
nuclear@14 2901 }
nuclear@14 2902
nuclear@14 2903 void /* PRIVATE */
nuclear@14 2904 png_read_finish_row(png_structp png_ptr)
nuclear@14 2905 {
nuclear@14 2906 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@14 2907 #ifdef PNG_READ_INTERLACING_SUPPORTED
nuclear@14 2908 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
nuclear@14 2909
nuclear@14 2910 /* start of interlace block */
nuclear@14 2911 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
nuclear@14 2912
nuclear@14 2913 /* offset to next interlace block */
nuclear@14 2914 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
nuclear@14 2915
nuclear@14 2916 /* start of interlace block in the y direction */
nuclear@14 2917 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
nuclear@14 2918
nuclear@14 2919 /* offset to next interlace block in the y direction */
nuclear@14 2920 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
nuclear@14 2921 #endif /* PNG_READ_INTERLACING_SUPPORTED */
nuclear@14 2922 #endif
nuclear@14 2923
nuclear@14 2924 png_debug(1, "in png_read_finish_row\n");
nuclear@14 2925 png_ptr->row_number++;
nuclear@14 2926 if (png_ptr->row_number < png_ptr->num_rows)
nuclear@14 2927 return;
nuclear@14 2928
nuclear@14 2929 #ifdef PNG_READ_INTERLACING_SUPPORTED
nuclear@14 2930 if (png_ptr->interlaced)
nuclear@14 2931 {
nuclear@14 2932 png_ptr->row_number = 0;
nuclear@14 2933 png_memset_check(png_ptr, png_ptr->prev_row, 0,
nuclear@14 2934 png_ptr->rowbytes + 1);
nuclear@14 2935 do
nuclear@14 2936 {
nuclear@14 2937 png_ptr->pass++;
nuclear@14 2938 if (png_ptr->pass >= 7)
nuclear@14 2939 break;
nuclear@14 2940 png_ptr->iwidth = (png_ptr->width +
nuclear@14 2941 png_pass_inc[png_ptr->pass] - 1 -
nuclear@14 2942 png_pass_start[png_ptr->pass]) /
nuclear@14 2943 png_pass_inc[png_ptr->pass];
nuclear@14 2944
nuclear@14 2945 png_ptr->irowbytes = PNG_ROWBYTES(png_ptr->pixel_depth,
nuclear@14 2946 png_ptr->iwidth) + 1;
nuclear@14 2947
nuclear@14 2948 if (!(png_ptr->transformations & PNG_INTERLACE))
nuclear@14 2949 {
nuclear@14 2950 png_ptr->num_rows = (png_ptr->height +
nuclear@14 2951 png_pass_yinc[png_ptr->pass] - 1 -
nuclear@14 2952 png_pass_ystart[png_ptr->pass]) /
nuclear@14 2953 png_pass_yinc[png_ptr->pass];
nuclear@14 2954 if (!(png_ptr->num_rows))
nuclear@14 2955 continue;
nuclear@14 2956 }
nuclear@14 2957 else /* if (png_ptr->transformations & PNG_INTERLACE) */
nuclear@14 2958 break;
nuclear@14 2959 } while (png_ptr->iwidth == 0);
nuclear@14 2960
nuclear@14 2961 if (png_ptr->pass < 7)
nuclear@14 2962 return;
nuclear@14 2963 }
nuclear@14 2964 #endif /* PNG_READ_INTERLACING_SUPPORTED */
nuclear@14 2965
nuclear@14 2966 if (!(png_ptr->flags & PNG_FLAG_ZLIB_FINISHED))
nuclear@14 2967 {
nuclear@14 2968 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@14 2969 PNG_CONST PNG_IDAT;
nuclear@14 2970 #endif
nuclear@14 2971 char extra;
nuclear@14 2972 int ret;
nuclear@14 2973
nuclear@14 2974 png_ptr->zstream.next_out = (Byte *)&extra;
nuclear@14 2975 png_ptr->zstream.avail_out = (uInt)1;
nuclear@14 2976 for (;;)
nuclear@14 2977 {
nuclear@14 2978 if (!(png_ptr->zstream.avail_in))
nuclear@14 2979 {
nuclear@14 2980 while (!png_ptr->idat_size)
nuclear@14 2981 {
nuclear@14 2982 png_byte chunk_length[4];
nuclear@14 2983
nuclear@14 2984 png_crc_finish(png_ptr, 0);
nuclear@14 2985
nuclear@14 2986 png_read_data(png_ptr, chunk_length, 4);
nuclear@14 2987 png_ptr->idat_size = png_get_uint_31(png_ptr, chunk_length);
nuclear@14 2988 png_reset_crc(png_ptr);
nuclear@14 2989 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
nuclear@14 2990 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
nuclear@14 2991 png_error(png_ptr, "Not enough image data");
nuclear@14 2992
nuclear@14 2993 }
nuclear@14 2994 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
nuclear@14 2995 png_ptr->zstream.next_in = png_ptr->zbuf;
nuclear@14 2996 if (png_ptr->zbuf_size > png_ptr->idat_size)
nuclear@14 2997 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
nuclear@14 2998 png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream.avail_in);
nuclear@14 2999 png_ptr->idat_size -= png_ptr->zstream.avail_in;
nuclear@14 3000 }
nuclear@14 3001 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
nuclear@14 3002 if (ret == Z_STREAM_END)
nuclear@14 3003 {
nuclear@14 3004 if (!(png_ptr->zstream.avail_out) || png_ptr->zstream.avail_in ||
nuclear@14 3005 png_ptr->idat_size)
nuclear@14 3006 png_warning(png_ptr, "Extra compressed data");
nuclear@14 3007 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@14 3008 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
nuclear@14 3009 break;
nuclear@14 3010 }
nuclear@14 3011 if (ret != Z_OK)
nuclear@14 3012 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
nuclear@14 3013 "Decompression Error");
nuclear@14 3014
nuclear@14 3015 if (!(png_ptr->zstream.avail_out))
nuclear@14 3016 {
nuclear@14 3017 png_warning(png_ptr, "Extra compressed data.");
nuclear@14 3018 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@14 3019 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
nuclear@14 3020 break;
nuclear@14 3021 }
nuclear@14 3022
nuclear@14 3023 }
nuclear@14 3024 png_ptr->zstream.avail_out = 0;
nuclear@14 3025 }
nuclear@14 3026
nuclear@14 3027 if (png_ptr->idat_size || png_ptr->zstream.avail_in)
nuclear@14 3028 png_warning(png_ptr, "Extra compression data");
nuclear@14 3029
nuclear@14 3030 inflateReset(&png_ptr->zstream);
nuclear@14 3031
nuclear@14 3032 png_ptr->mode |= PNG_AFTER_IDAT;
nuclear@14 3033 }
nuclear@14 3034
nuclear@14 3035 void /* PRIVATE */
nuclear@14 3036 png_read_start_row(png_structp png_ptr)
nuclear@14 3037 {
nuclear@14 3038 #ifdef PNG_USE_LOCAL_ARRAYS
nuclear@14 3039 #ifdef PNG_READ_INTERLACING_SUPPORTED
nuclear@14 3040 /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
nuclear@14 3041
nuclear@14 3042 /* start of interlace block */
nuclear@14 3043 PNG_CONST int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
nuclear@14 3044
nuclear@14 3045 /* offset to next interlace block */
nuclear@14 3046 PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
nuclear@14 3047
nuclear@14 3048 /* start of interlace block in the y direction */
nuclear@14 3049 PNG_CONST int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
nuclear@14 3050
nuclear@14 3051 /* offset to next interlace block in the y direction */
nuclear@14 3052 PNG_CONST int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
nuclear@14 3053 #endif
nuclear@14 3054 #endif
nuclear@14 3055
nuclear@14 3056 int max_pixel_depth;
nuclear@14 3057 png_size_t row_bytes;
nuclear@14 3058
nuclear@14 3059 png_debug(1, "in png_read_start_row\n");
nuclear@14 3060 png_ptr->zstream.avail_in = 0;
nuclear@14 3061 png_init_read_transformations(png_ptr);
nuclear@14 3062 #ifdef PNG_READ_INTERLACING_SUPPORTED
nuclear@14 3063 if (png_ptr->interlaced)
nuclear@14 3064 {
nuclear@14 3065 if (!(png_ptr->transformations & PNG_INTERLACE))
nuclear@14 3066 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
nuclear@14 3067 png_pass_ystart[0]) / png_pass_yinc[0];
nuclear@14 3068 else
nuclear@14 3069 png_ptr->num_rows = png_ptr->height;
nuclear@14 3070
nuclear@14 3071 png_ptr->iwidth = (png_ptr->width +
nuclear@14 3072 png_pass_inc[png_ptr->pass] - 1 -
nuclear@14 3073 png_pass_start[png_ptr->pass]) /
nuclear@14 3074 png_pass_inc[png_ptr->pass];
nuclear@14 3075
nuclear@14 3076 png_ptr->irowbytes =
nuclear@14 3077 PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1;
nuclear@14 3078 }
nuclear@14 3079 else
nuclear@14 3080 #endif /* PNG_READ_INTERLACING_SUPPORTED */
nuclear@14 3081 {
nuclear@14 3082 png_ptr->num_rows = png_ptr->height;
nuclear@14 3083 png_ptr->iwidth = png_ptr->width;
nuclear@14 3084 png_ptr->irowbytes = png_ptr->rowbytes + 1;
nuclear@14 3085 }
nuclear@14 3086 max_pixel_depth = png_ptr->pixel_depth;
nuclear@14 3087
nuclear@14 3088 #if defined(PNG_READ_PACK_SUPPORTED)
nuclear@14 3089 if ((png_ptr->transformations & PNG_PACK) && png_ptr->bit_depth < 8)
nuclear@14 3090 max_pixel_depth = 8;
nuclear@14 3091 #endif
nuclear@14 3092
nuclear@14 3093 #if defined(PNG_READ_EXPAND_SUPPORTED)
nuclear@14 3094 if (png_ptr->transformations & PNG_EXPAND)
nuclear@14 3095 {
nuclear@14 3096 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
nuclear@14 3097 {
nuclear@14 3098 if (png_ptr->num_trans)
nuclear@14 3099 max_pixel_depth = 32;
nuclear@14 3100 else
nuclear@14 3101 max_pixel_depth = 24;
nuclear@14 3102 }
nuclear@14 3103 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
nuclear@14 3104 {
nuclear@14 3105 if (max_pixel_depth < 8)
nuclear@14 3106 max_pixel_depth = 8;
nuclear@14 3107 if (png_ptr->num_trans)
nuclear@14 3108 max_pixel_depth *= 2;
nuclear@14 3109 }
nuclear@14 3110 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
nuclear@14 3111 {
nuclear@14 3112 if (png_ptr->num_trans)
nuclear@14 3113 {
nuclear@14 3114 max_pixel_depth *= 4;
nuclear@14 3115 max_pixel_depth /= 3;
nuclear@14 3116 }
nuclear@14 3117 }
nuclear@14 3118 }
nuclear@14 3119 #endif
nuclear@14 3120
nuclear@14 3121 #if defined(PNG_READ_FILLER_SUPPORTED)
nuclear@14 3122 if (png_ptr->transformations & (PNG_FILLER))
nuclear@14 3123 {
nuclear@14 3124 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
nuclear@14 3125 max_pixel_depth = 32;
nuclear@14 3126 else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
nuclear@14 3127 {
nuclear@14 3128 if (max_pixel_depth <= 8)
nuclear@14 3129 max_pixel_depth = 16;
nuclear@14 3130 else
nuclear@14 3131 max_pixel_depth = 32;
nuclear@14 3132 }
nuclear@14 3133 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
nuclear@14 3134 {
nuclear@14 3135 if (max_pixel_depth <= 32)
nuclear@14 3136 max_pixel_depth = 32;
nuclear@14 3137 else
nuclear@14 3138 max_pixel_depth = 64;
nuclear@14 3139 }
nuclear@14 3140 }
nuclear@14 3141 #endif
nuclear@14 3142
nuclear@14 3143 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
nuclear@14 3144 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
nuclear@14 3145 {
nuclear@14 3146 if (
nuclear@14 3147 #if defined(PNG_READ_EXPAND_SUPPORTED)
nuclear@14 3148 (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
nuclear@14 3149 #endif
nuclear@14 3150 #if defined(PNG_READ_FILLER_SUPPORTED)
nuclear@14 3151 (png_ptr->transformations & (PNG_FILLER)) ||
nuclear@14 3152 #endif
nuclear@14 3153 png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
nuclear@14 3154 {
nuclear@14 3155 if (max_pixel_depth <= 16)
nuclear@14 3156 max_pixel_depth = 32;
nuclear@14 3157 else
nuclear@14 3158 max_pixel_depth = 64;
nuclear@14 3159 }
nuclear@14 3160 else
nuclear@14 3161 {
nuclear@14 3162 if (max_pixel_depth <= 8)
nuclear@14 3163 {
nuclear@14 3164 if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
nuclear@14 3165 max_pixel_depth = 32;
nuclear@14 3166 else
nuclear@14 3167 max_pixel_depth = 24;
nuclear@14 3168 }
nuclear@14 3169 else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
nuclear@14 3170 max_pixel_depth = 64;
nuclear@14 3171 else
nuclear@14 3172 max_pixel_depth = 48;
nuclear@14 3173 }
nuclear@14 3174 }
nuclear@14 3175 #endif
nuclear@14 3176
nuclear@14 3177 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
nuclear@14 3178 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
nuclear@14 3179 if (png_ptr->transformations & PNG_USER_TRANSFORM)
nuclear@14 3180 {
nuclear@14 3181 int user_pixel_depth = png_ptr->user_transform_depth*
nuclear@14 3182 png_ptr->user_transform_channels;
nuclear@14 3183 if (user_pixel_depth > max_pixel_depth)
nuclear@14 3184 max_pixel_depth=user_pixel_depth;
nuclear@14 3185 }
nuclear@14 3186 #endif
nuclear@14 3187
nuclear@14 3188 /* align the width on the next larger 8 pixels. Mainly used
nuclear@14 3189 for interlacing */
nuclear@14 3190 row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
nuclear@14 3191 /* calculate the maximum bytes needed, adding a byte and a pixel
nuclear@14 3192 for safety's sake */
nuclear@14 3193 row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
nuclear@14 3194 1 + ((max_pixel_depth + 7) >> 3);
nuclear@14 3195 #ifdef PNG_MAX_MALLOC_64K
nuclear@14 3196 if (row_bytes > (png_uint_32)65536L)
nuclear@14 3197 png_error(png_ptr, "This image requires a row greater than 64KB");
nuclear@14 3198 #endif
nuclear@14 3199
nuclear@14 3200 if (row_bytes + 64 > png_ptr->old_big_row_buf_size)
nuclear@14 3201 {
nuclear@14 3202 png_free(png_ptr, png_ptr->big_row_buf);
nuclear@14 3203 png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes+64);
nuclear@14 3204 png_ptr->row_buf = png_ptr->big_row_buf+32;
nuclear@14 3205 png_ptr->old_big_row_buf_size = row_bytes+64;
nuclear@14 3206 }
nuclear@14 3207
nuclear@14 3208 #ifdef PNG_MAX_MALLOC_64K
nuclear@14 3209 if ((png_uint_32)png_ptr->rowbytes + 1 > (png_uint_32)65536L)
nuclear@14 3210 png_error(png_ptr, "This image requires a row greater than 64KB");
nuclear@14 3211 #endif
nuclear@14 3212 if ((png_uint_32)png_ptr->rowbytes > (png_uint_32)(PNG_SIZE_MAX - 1))
nuclear@14 3213 png_error(png_ptr, "Row has too many bytes to allocate in memory.");
nuclear@14 3214
nuclear@14 3215 if (png_ptr->rowbytes+1 > png_ptr->old_prev_row_size)
nuclear@14 3216 {
nuclear@14 3217 png_free(png_ptr, png_ptr->prev_row);
nuclear@14 3218 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)(
nuclear@14 3219 png_ptr->rowbytes + 1));
nuclear@14 3220 png_ptr->old_prev_row_size = png_ptr->rowbytes+1;
nuclear@14 3221 }
nuclear@14 3222
nuclear@14 3223 png_memset_check(png_ptr, png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
nuclear@14 3224
nuclear@14 3225 png_debug1(3, "width = %lu,\n", png_ptr->width);
nuclear@14 3226 png_debug1(3, "height = %lu,\n", png_ptr->height);
nuclear@14 3227 png_debug1(3, "iwidth = %lu,\n", png_ptr->iwidth);
nuclear@14 3228 png_debug1(3, "num_rows = %lu\n", png_ptr->num_rows);
nuclear@14 3229 png_debug1(3, "rowbytes = %lu,\n", png_ptr->rowbytes);
nuclear@14 3230 png_debug1(3, "irowbytes = %lu,\n", png_ptr->irowbytes);
nuclear@14 3231
nuclear@14 3232 png_ptr->flags |= PNG_FLAG_ROW_INIT;
nuclear@14 3233 }
nuclear@14 3234 #endif /* PNG_READ_SUPPORTED */