dbf-halloween2015

annotate libs/libpng/pngrutil.c @ 4:4316c0c879e9

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