istereo2

annotate libs/libpng/pngrutil.c @ 20:2b85d05df3f2

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