istereo

annotate libs/libpng/pngrutil.c @ 43:73813c1176de

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