vrshoot

annotate libs/libpng/pngrutil.c @ 2:334d17aed7de

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