Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(227)

Side by Side Diff: third_party/libpng/pngwutil.c

Issue 1118002: libpng: update to 1.2.43 (Closed)
Patch Set: Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« third_party/libpng/pngrutil.c ('K') | « third_party/libpng/pngwtran.c ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* pngwutil.c - utilities to write a PNG file 2 /* pngwutil.c - utilities to write a PNG file
3 * 3 *
4 * Last changed in libpng 1.2.36 [June 4, 2009] 4 * Last changed in libpng 1.2.43 [February 25, 2010]
5 * For conditions of distribution and use, see copyright notice in png.h 5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
6 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
7 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 *
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
9 */ 12 */
10 13
11 #define PNG_INTERNAL 14 #define PNG_INTERNAL
15 #define PNG_NO_PEDANTIC_WARNINGS
12 #include "png.h" 16 #include "png.h"
13 #ifdef PNG_WRITE_SUPPORTED 17 #ifdef PNG_WRITE_SUPPORTED
14 18
15 /* Place a 32-bit number into a buffer in PNG byte order. We work 19 /* Place a 32-bit number into a buffer in PNG byte order. We work
16 * with unsigned numbers for convenience, although one supported 20 * with unsigned numbers for convenience, although one supported
17 * ancillary chunk uses signed (two's complement) numbers. 21 * ancillary chunk uses signed (two's complement) numbers.
18 */ 22 */
19 void PNGAPI 23 void PNGAPI
20 png_save_uint_32(png_bytep buf, png_uint_32 i) 24 png_save_uint_32(png_bytep buf, png_uint_32 i)
21 { 25 {
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 * passing in png_write_chunk_data(). 96 * passing in png_write_chunk_data().
93 */ 97 */
94 void PNGAPI 98 void PNGAPI
95 png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name, 99 png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
96 png_uint_32 length) 100 png_uint_32 length)
97 { 101 {
98 png_byte buf[8]; 102 png_byte buf[8];
99 103
100 png_debug2(0, "Writing %s chunk, length = %lu", chunk_name, 104 png_debug2(0, "Writing %s chunk, length = %lu", chunk_name,
101 (unsigned long)length); 105 (unsigned long)length);
106
102 if (png_ptr == NULL) 107 if (png_ptr == NULL)
103 return; 108 return;
104 109
110
105 /* Write the length and the chunk name */ 111 /* Write the length and the chunk name */
106 png_save_uint_32(buf, length); 112 png_save_uint_32(buf, length);
107 png_memcpy(buf + 4, chunk_name, 4); 113 png_memcpy(buf + 4, chunk_name, 4);
108 png_write_data(png_ptr, buf, (png_size_t)8); 114 png_write_data(png_ptr, buf, (png_size_t)8);
109 /* Put the chunk name into png_ptr->chunk_name */ 115 /* Put the chunk name into png_ptr->chunk_name */
110 png_memcpy(png_ptr->chunk_name, chunk_name, 4); 116 png_memcpy(png_ptr->chunk_name, chunk_name, 4);
111 /* Reset the crc and run it over the chunk name */ 117 /* Reset the crc and run it over the chunk name */
112 png_reset_crc(png_ptr); 118 png_reset_crc(png_ptr);
113 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4); 119 png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
114 } 120 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 /* We may just want to pass the text right through */ 187 /* We may just want to pass the text right through */
182 if (compression == PNG_TEXT_COMPRESSION_NONE) 188 if (compression == PNG_TEXT_COMPRESSION_NONE)
183 { 189 {
184 comp->input = text; 190 comp->input = text;
185 comp->input_len = text_len; 191 comp->input_len = text_len;
186 return((int)text_len); 192 return((int)text_len);
187 } 193 }
188 194
189 if (compression >= PNG_TEXT_COMPRESSION_LAST) 195 if (compression >= PNG_TEXT_COMPRESSION_LAST)
190 { 196 {
191 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) 197 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
192 char msg[50]; 198 char msg[50];
193 png_snprintf(msg, 50, "Unknown compression type %d", compression); 199 png_snprintf(msg, 50, "Unknown compression type %d", compression);
194 png_warning(png_ptr, msg); 200 png_warning(png_ptr, msg);
195 #else 201 #else
196 png_warning(png_ptr, "Unknown compression type"); 202 png_warning(png_ptr, "Unknown compression type");
197 #endif 203 #endif
198 } 204 }
199 205
200 /* We can't write the chunk until we find out how much data we have, 206 /* We can't write the chunk until we find out how much data we have,
201 * which means we need to run the compressor first and save the 207 * which means we need to run the compressor first and save the
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 int interlace_type) 394 int interlace_type)
389 { 395 {
390 #ifdef PNG_USE_LOCAL_ARRAYS 396 #ifdef PNG_USE_LOCAL_ARRAYS
391 PNG_IHDR; 397 PNG_IHDR;
392 #endif 398 #endif
393 int ret; 399 int ret;
394 400
395 png_byte buf[13]; /* Buffer to store the IHDR info */ 401 png_byte buf[13]; /* Buffer to store the IHDR info */
396 402
397 png_debug(1, "in png_write_IHDR"); 403 png_debug(1, "in png_write_IHDR");
404
398 /* Check that we have valid input data from the application info */ 405 /* Check that we have valid input data from the application info */
399 switch (color_type) 406 switch (color_type)
400 { 407 {
401 case PNG_COLOR_TYPE_GRAY: 408 case PNG_COLOR_TYPE_GRAY:
402 switch (bit_depth) 409 switch (bit_depth)
403 { 410 {
404 case 1: 411 case 1:
405 case 2: 412 case 2:
406 case 4: 413 case 4:
407 case 8: 414 case 8:
408 case 16: png_ptr->channels = 1; break; 415 case 16: png_ptr->channels = 1; break;
409 default: png_error(png_ptr, "Invalid bit depth for grayscale image") ; 416 default: png_error(png_ptr,
417 "Invalid bit depth for grayscale image");
410 } 418 }
411 break; 419 break;
412 case PNG_COLOR_TYPE_RGB: 420 case PNG_COLOR_TYPE_RGB:
413 if (bit_depth != 8 && bit_depth != 16) 421 if (bit_depth != 8 && bit_depth != 16)
414 png_error(png_ptr, "Invalid bit depth for RGB image"); 422 png_error(png_ptr, "Invalid bit depth for RGB image");
415 png_ptr->channels = 3; 423 png_ptr->channels = 3;
416 break; 424 break;
417 case PNG_COLOR_TYPE_PALETTE: 425 case PNG_COLOR_TYPE_PALETTE:
418 switch (bit_depth) 426 switch (bit_depth)
419 { 427 {
(...skipping 27 matching lines...) Expand all
447 /* Write filter_method 64 (intrapixel differencing) only if 455 /* Write filter_method 64 (intrapixel differencing) only if
448 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and 456 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
449 * 2. Libpng did not write a PNG signature (this filter_method is only 457 * 2. Libpng did not write a PNG signature (this filter_method is only
450 * used in PNG datastreams that are embedded in MNG datastreams) and 458 * used in PNG datastreams that are embedded in MNG datastreams) and
451 * 3. The application called png_permit_mng_features with a mask that 459 * 3. The application called png_permit_mng_features with a mask that
452 * included PNG_FLAG_MNG_FILTER_64 and 460 * included PNG_FLAG_MNG_FILTER_64 and
453 * 4. The filter_method is 64 and 461 * 4. The filter_method is 64 and
454 * 5. The color_type is RGB or RGBA 462 * 5. The color_type is RGB or RGBA
455 */ 463 */
456 if ( 464 if (
457 #if defined(PNG_MNG_FEATURES_SUPPORTED) 465 #ifdef PNG_MNG_FEATURES_SUPPORTED
458 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && 466 !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
459 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) && 467 ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
460 (color_type == PNG_COLOR_TYPE_RGB || 468 (color_type == PNG_COLOR_TYPE_RGB ||
461 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && 469 color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
462 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) && 470 (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
463 #endif 471 #endif
464 filter_type != PNG_FILTER_TYPE_BASE) 472 filter_type != PNG_FILTER_TYPE_BASE)
465 { 473 {
466 png_warning(png_ptr, "Invalid filter type specified"); 474 png_warning(png_ptr, "Invalid filter type specified");
467 filter_type = PNG_FILTER_TYPE_BASE; 475 filter_type = PNG_FILTER_TYPE_BASE;
468 } 476 }
469 477
470 #ifdef PNG_WRITE_INTERLACING_SUPPORTED 478 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
471 if (interlace_type != PNG_INTERLACE_NONE && 479 if (interlace_type != PNG_INTERLACE_NONE &&
472 interlace_type != PNG_INTERLACE_ADAM7) 480 interlace_type != PNG_INTERLACE_ADAM7)
473 { 481 {
474 png_warning(png_ptr, "Invalid interlace type specified"); 482 png_warning(png_ptr, "Invalid interlace type specified");
475 interlace_type = PNG_INTERLACE_ADAM7; 483 interlace_type = PNG_INTERLACE_ADAM7;
476 } 484 }
477 #else 485 #else
478 interlace_type=PNG_INTERLACE_NONE; 486 interlace_type=PNG_INTERLACE_NONE;
479 #endif 487 #endif
480 488
481 /* Save the relevent information */ 489 /* Save the relevent information */
482 png_ptr->bit_depth = (png_byte)bit_depth; 490 png_ptr->bit_depth = (png_byte)bit_depth;
483 png_ptr->color_type = (png_byte)color_type; 491 png_ptr->color_type = (png_byte)color_type;
484 png_ptr->interlaced = (png_byte)interlace_type; 492 png_ptr->interlaced = (png_byte)interlace_type;
485 #if defined(PNG_MNG_FEATURES_SUPPORTED) 493 #ifdef PNG_MNG_FEATURES_SUPPORTED
486 png_ptr->filter_type = (png_byte)filter_type; 494 png_ptr->filter_type = (png_byte)filter_type;
487 #endif 495 #endif
488 png_ptr->compression_type = (png_byte)compression_type; 496 png_ptr->compression_type = (png_byte)compression_type;
489 png_ptr->width = width; 497 png_ptr->width = width;
490 png_ptr->height = height; 498 png_ptr->height = height;
491 499
492 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels); 500 png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
493 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width); 501 png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
494 /* Set the usr info, so any transformations can modify it */ 502 /* Set the usr info, so any transformations can modify it */
495 png_ptr->usr_width = png_ptr->width; 503 png_ptr->usr_width = png_ptr->width;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal) 573 png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
566 { 574 {
567 #ifdef PNG_USE_LOCAL_ARRAYS 575 #ifdef PNG_USE_LOCAL_ARRAYS
568 PNG_PLTE; 576 PNG_PLTE;
569 #endif 577 #endif
570 png_uint_32 i; 578 png_uint_32 i;
571 png_colorp pal_ptr; 579 png_colorp pal_ptr;
572 png_byte buf[3]; 580 png_byte buf[3];
573 581
574 png_debug(1, "in png_write_PLTE"); 582 png_debug(1, "in png_write_PLTE");
583
575 if (( 584 if ((
576 #if defined(PNG_MNG_FEATURES_SUPPORTED) 585 #ifdef PNG_MNG_FEATURES_SUPPORTED
577 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) && 586 !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
578 #endif 587 #endif
579 num_pal == 0) || num_pal > 256) 588 num_pal == 0) || num_pal > 256)
580 { 589 {
581 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) 590 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
582 { 591 {
583 png_error(png_ptr, "Invalid number of colors in palette"); 592 png_error(png_ptr, "Invalid number of colors in palette");
584 } 593 }
585 else 594 else
586 { 595 {
587 png_warning(png_ptr, "Invalid number of colors in palette"); 596 png_warning(png_ptr, "Invalid number of colors in palette");
588 return; 597 return;
589 } 598 }
590 } 599 }
591 600
592 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR)) 601 if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
593 { 602 {
594 png_warning(png_ptr, 603 png_warning(png_ptr,
595 "Ignoring request to write a PLTE chunk in grayscale PNG"); 604 "Ignoring request to write a PLTE chunk in grayscale PNG");
596 return; 605 return;
597 } 606 }
598 607
599 png_ptr->num_palette = (png_uint_16)num_pal; 608 png_ptr->num_palette = (png_uint_16)num_pal;
600 png_debug1(3, "num_palette = %d", png_ptr->num_palette); 609 png_debug1(3, "num_palette = %d", png_ptr->num_palette);
601 610
602 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, 611 png_write_chunk_start(png_ptr, (png_bytep)png_PLTE,
603 (png_uint_32)(num_pal * 3)); 612 (png_uint_32)(num_pal * 3));
604 #ifndef PNG_NO_POINTER_INDEXING 613 #ifdef PNG_POINTER_INDEXING_SUPPORTED
605 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++) 614 for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
606 { 615 {
607 buf[0] = pal_ptr->red; 616 buf[0] = pal_ptr->red;
608 buf[1] = pal_ptr->green; 617 buf[1] = pal_ptr->green;
609 buf[2] = pal_ptr->blue; 618 buf[2] = pal_ptr->blue;
610 png_write_chunk_data(png_ptr, buf, (png_size_t)3); 619 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
611 } 620 }
612 #else 621 #else
613 /* This is a little slower but some buggy compilers need to do this instead * / 622 /* This is a little slower but some buggy compilers need to do this
623 * instead
624 */
614 pal_ptr=palette; 625 pal_ptr=palette;
615 for (i = 0; i < num_pal; i++) 626 for (i = 0; i < num_pal; i++)
616 { 627 {
617 buf[0] = pal_ptr[i].red; 628 buf[0] = pal_ptr[i].red;
618 buf[1] = pal_ptr[i].green; 629 buf[1] = pal_ptr[i].green;
619 buf[2] = pal_ptr[i].blue; 630 buf[2] = pal_ptr[i].blue;
620 png_write_chunk_data(png_ptr, buf, (png_size_t)3); 631 png_write_chunk_data(png_ptr, buf, (png_size_t)3);
621 } 632 }
622 #endif 633 #endif
623 png_write_chunk_end(png_ptr); 634 png_write_chunk_end(png_ptr);
624 png_ptr->mode |= PNG_HAVE_PLTE; 635 png_ptr->mode |= PNG_HAVE_PLTE;
625 } 636 }
626 637
627 /* Write an IDAT chunk */ 638 /* Write an IDAT chunk */
628 void /* PRIVATE */ 639 void /* PRIVATE */
629 png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length) 640 png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
630 { 641 {
631 #ifdef PNG_USE_LOCAL_ARRAYS 642 #ifdef PNG_USE_LOCAL_ARRAYS
632 PNG_IDAT; 643 PNG_IDAT;
633 #endif 644 #endif
645
634 png_debug(1, "in png_write_IDAT"); 646 png_debug(1, "in png_write_IDAT");
635 647
636 /* Optimize the CMF field in the zlib stream. */ 648 /* Optimize the CMF field in the zlib stream. */
637 /* This hack of the zlib stream is compliant to the stream specification. */ 649 /* This hack of the zlib stream is compliant to the stream specification. */
638 if (!(png_ptr->mode & PNG_HAVE_IDAT) && 650 if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
639 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) 651 png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
640 { 652 {
641 unsigned int z_cmf = data[0]; /* zlib compression method and flags */ 653 unsigned int z_cmf = data[0]; /* zlib compression method and flags */
642 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) 654 if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
643 { 655 {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
678 png_ptr->mode |= PNG_HAVE_IDAT; 690 png_ptr->mode |= PNG_HAVE_IDAT;
679 } 691 }
680 692
681 /* Write an IEND chunk */ 693 /* Write an IEND chunk */
682 void /* PRIVATE */ 694 void /* PRIVATE */
683 png_write_IEND(png_structp png_ptr) 695 png_write_IEND(png_structp png_ptr)
684 { 696 {
685 #ifdef PNG_USE_LOCAL_ARRAYS 697 #ifdef PNG_USE_LOCAL_ARRAYS
686 PNG_IEND; 698 PNG_IEND;
687 #endif 699 #endif
700
688 png_debug(1, "in png_write_IEND"); 701 png_debug(1, "in png_write_IEND");
702
689 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL, 703 png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
690 (png_size_t)0); 704 (png_size_t)0);
691 png_ptr->mode |= PNG_HAVE_IEND; 705 png_ptr->mode |= PNG_HAVE_IEND;
692 } 706 }
693 707
694 #if defined(PNG_WRITE_gAMA_SUPPORTED) 708 #ifdef PNG_WRITE_gAMA_SUPPORTED
695 /* Write a gAMA chunk */ 709 /* Write a gAMA chunk */
696 #ifdef PNG_FLOATING_POINT_SUPPORTED 710 #ifdef PNG_FLOATING_POINT_SUPPORTED
697 void /* PRIVATE */ 711 void /* PRIVATE */
698 png_write_gAMA(png_structp png_ptr, double file_gamma) 712 png_write_gAMA(png_structp png_ptr, double file_gamma)
699 { 713 {
700 #ifdef PNG_USE_LOCAL_ARRAYS 714 #ifdef PNG_USE_LOCAL_ARRAYS
701 PNG_gAMA; 715 PNG_gAMA;
702 #endif 716 #endif
703 png_uint_32 igamma; 717 png_uint_32 igamma;
704 png_byte buf[4]; 718 png_byte buf[4];
705 719
706 png_debug(1, "in png_write_gAMA"); 720 png_debug(1, "in png_write_gAMA");
721
707 /* file_gamma is saved in 1/100,000ths */ 722 /* file_gamma is saved in 1/100,000ths */
708 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5); 723 igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
709 png_save_uint_32(buf, igamma); 724 png_save_uint_32(buf, igamma);
710 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4); 725 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
711 } 726 }
712 #endif 727 #endif
713 #ifdef PNG_FIXED_POINT_SUPPORTED 728 #ifdef PNG_FIXED_POINT_SUPPORTED
714 void /* PRIVATE */ 729 void /* PRIVATE */
715 png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma) 730 png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
716 { 731 {
717 #ifdef PNG_USE_LOCAL_ARRAYS 732 #ifdef PNG_USE_LOCAL_ARRAYS
718 PNG_gAMA; 733 PNG_gAMA;
719 #endif 734 #endif
720 png_byte buf[4]; 735 png_byte buf[4];
721 736
722 png_debug(1, "in png_write_gAMA"); 737 png_debug(1, "in png_write_gAMA");
738
723 /* file_gamma is saved in 1/100,000ths */ 739 /* file_gamma is saved in 1/100,000ths */
724 png_save_uint_32(buf, (png_uint_32)file_gamma); 740 png_save_uint_32(buf, (png_uint_32)file_gamma);
725 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4); 741 png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
726 } 742 }
727 #endif 743 #endif
728 #endif 744 #endif
729 745
730 #if defined(PNG_WRITE_sRGB_SUPPORTED) 746 #ifdef PNG_WRITE_sRGB_SUPPORTED
731 /* Write a sRGB chunk */ 747 /* Write a sRGB chunk */
732 void /* PRIVATE */ 748 void /* PRIVATE */
733 png_write_sRGB(png_structp png_ptr, int srgb_intent) 749 png_write_sRGB(png_structp png_ptr, int srgb_intent)
734 { 750 {
735 #ifdef PNG_USE_LOCAL_ARRAYS 751 #ifdef PNG_USE_LOCAL_ARRAYS
736 PNG_sRGB; 752 PNG_sRGB;
737 #endif 753 #endif
738 png_byte buf[1]; 754 png_byte buf[1];
739 755
740 png_debug(1, "in png_write_sRGB"); 756 png_debug(1, "in png_write_sRGB");
757
741 if (srgb_intent >= PNG_sRGB_INTENT_LAST) 758 if (srgb_intent >= PNG_sRGB_INTENT_LAST)
742 png_warning(png_ptr, 759 png_warning(png_ptr,
743 "Invalid sRGB rendering intent specified"); 760 "Invalid sRGB rendering intent specified");
744 buf[0]=(png_byte)srgb_intent; 761 buf[0]=(png_byte)srgb_intent;
745 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1); 762 png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
746 } 763 }
747 #endif 764 #endif
748 765
749 #if defined(PNG_WRITE_iCCP_SUPPORTED) 766 #ifdef PNG_WRITE_iCCP_SUPPORTED
750 /* Write an iCCP chunk */ 767 /* Write an iCCP chunk */
751 void /* PRIVATE */ 768 void /* PRIVATE */
752 png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type, 769 png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
753 png_charp profile, int profile_len) 770 png_charp profile, int profile_len)
754 { 771 {
755 #ifdef PNG_USE_LOCAL_ARRAYS 772 #ifdef PNG_USE_LOCAL_ARRAYS
756 PNG_iCCP; 773 PNG_iCCP;
757 #endif 774 #endif
758 png_size_t name_len; 775 png_size_t name_len;
759 png_charp new_name; 776 png_charp new_name;
(...skipping 18 matching lines...) Expand all
778 if (profile == NULL) 795 if (profile == NULL)
779 profile_len = 0; 796 profile_len = 0;
780 797
781 if (profile_len > 3) 798 if (profile_len > 3)
782 embedded_profile_len = 799 embedded_profile_len =
783 ((*( (png_bytep)profile ))<<24) | 800 ((*( (png_bytep)profile ))<<24) |
784 ((*( (png_bytep)profile + 1))<<16) | 801 ((*( (png_bytep)profile + 1))<<16) |
785 ((*( (png_bytep)profile + 2))<< 8) | 802 ((*( (png_bytep)profile + 2))<< 8) |
786 ((*( (png_bytep)profile + 3)) ); 803 ((*( (png_bytep)profile + 3)) );
787 804
805 if (embedded_profile_len < 0)
806 {
807 png_warning(png_ptr,
808 "Embedded profile length in iCCP chunk is negative");
809 png_free(png_ptr, new_name);
810 return;
811 }
812
788 if (profile_len < embedded_profile_len) 813 if (profile_len < embedded_profile_len)
789 { 814 {
790 png_warning(png_ptr, 815 png_warning(png_ptr,
791 "Embedded profile length too large in iCCP chunk"); 816 "Embedded profile length too large in iCCP chunk");
792 png_free(png_ptr, new_name); 817 png_free(png_ptr, new_name);
793 return; 818 return;
794 } 819 }
795 820
796 if (profile_len > embedded_profile_len) 821 if (profile_len > embedded_profile_len)
797 { 822 {
(...skipping 14 matching lines...) Expand all
812 (png_size_t)(name_len + 2)); 837 (png_size_t)(name_len + 2));
813 838
814 if (profile_len) 839 if (profile_len)
815 png_write_compressed_data_out(png_ptr, &comp); 840 png_write_compressed_data_out(png_ptr, &comp);
816 841
817 png_write_chunk_end(png_ptr); 842 png_write_chunk_end(png_ptr);
818 png_free(png_ptr, new_name); 843 png_free(png_ptr, new_name);
819 } 844 }
820 #endif 845 #endif
821 846
822 #if defined(PNG_WRITE_sPLT_SUPPORTED) 847 #ifdef PNG_WRITE_sPLT_SUPPORTED
823 /* Write a sPLT chunk */ 848 /* Write a sPLT chunk */
824 void /* PRIVATE */ 849 void /* PRIVATE */
825 png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette) 850 png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
826 { 851 {
827 #ifdef PNG_USE_LOCAL_ARRAYS 852 #ifdef PNG_USE_LOCAL_ARRAYS
828 PNG_sPLT; 853 PNG_sPLT;
829 #endif 854 #endif
830 png_size_t name_len; 855 png_size_t name_len;
831 png_charp new_name; 856 png_charp new_name;
832 png_byte entrybuf[10]; 857 png_byte entrybuf[10];
833 int entry_size = (spalette->depth == 8 ? 6 : 10); 858 int entry_size = (spalette->depth == 8 ? 6 : 10);
834 int palette_size = entry_size * spalette->nentries; 859 int palette_size = entry_size * spalette->nentries;
835 png_sPLT_entryp ep; 860 png_sPLT_entryp ep;
836 #ifdef PNG_NO_POINTER_INDEXING 861 #ifndef PNG_POINTER_INDEXING_SUPPORTED
837 int i; 862 int i;
838 #endif 863 #endif
839 864
840 png_debug(1, "in png_write_sPLT"); 865 png_debug(1, "in png_write_sPLT");
866
841 if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0) 867 if ((name_len = png_check_keyword(png_ptr,spalette->name, &new_name))==0)
842 return; 868 return;
843 869
844 /* Make sure we include the NULL after the name */ 870 /* Make sure we include the NULL after the name */
845 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT, 871 png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
846 (png_uint_32)(name_len + 2 + palette_size)); 872 (png_uint_32)(name_len + 2 + palette_size));
847 png_write_chunk_data(png_ptr, (png_bytep)new_name, 873 png_write_chunk_data(png_ptr, (png_bytep)new_name,
848 (png_size_t)(name_len + 1)); 874 (png_size_t)(name_len + 1));
849 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1); 875 png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, (png_size_t)1);
850 876
851 /* Loop through each palette entry, writing appropriately */ 877 /* Loop through each palette entry, writing appropriately */
852 #ifndef PNG_NO_POINTER_INDEXING 878 #ifdef PNG_POINTER_INDEXING_SUPPORTED
853 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++) 879 for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
854 { 880 {
855 if (spalette->depth == 8) 881 if (spalette->depth == 8)
856 { 882 {
857 entrybuf[0] = (png_byte)ep->red; 883 entrybuf[0] = (png_byte)ep->red;
858 entrybuf[1] = (png_byte)ep->green; 884 entrybuf[1] = (png_byte)ep->green;
859 entrybuf[2] = (png_byte)ep->blue; 885 entrybuf[2] = (png_byte)ep->blue;
860 entrybuf[3] = (png_byte)ep->alpha; 886 entrybuf[3] = (png_byte)ep->alpha;
861 png_save_uint_16(entrybuf + 4, ep->frequency); 887 png_save_uint_16(entrybuf + 4, ep->frequency);
862 } 888 }
(...skipping 29 matching lines...) Expand all
892 } 918 }
893 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size); 919 png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
894 } 920 }
895 #endif 921 #endif
896 922
897 png_write_chunk_end(png_ptr); 923 png_write_chunk_end(png_ptr);
898 png_free(png_ptr, new_name); 924 png_free(png_ptr, new_name);
899 } 925 }
900 #endif 926 #endif
901 927
902 #if defined(PNG_WRITE_sBIT_SUPPORTED) 928 #ifdef PNG_WRITE_sBIT_SUPPORTED
903 /* Write the sBIT chunk */ 929 /* Write the sBIT chunk */
904 void /* PRIVATE */ 930 void /* PRIVATE */
905 png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type) 931 png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
906 { 932 {
907 #ifdef PNG_USE_LOCAL_ARRAYS 933 #ifdef PNG_USE_LOCAL_ARRAYS
908 PNG_sBIT; 934 PNG_sBIT;
909 #endif 935 #endif
910 png_byte buf[4]; 936 png_byte buf[4];
911 png_size_t size; 937 png_size_t size;
912 938
913 png_debug(1, "in png_write_sBIT"); 939 png_debug(1, "in png_write_sBIT");
940
914 /* Make sure we don't depend upon the order of PNG_COLOR_8 */ 941 /* Make sure we don't depend upon the order of PNG_COLOR_8 */
915 if (color_type & PNG_COLOR_MASK_COLOR) 942 if (color_type & PNG_COLOR_MASK_COLOR)
916 { 943 {
917 png_byte maxbits; 944 png_byte maxbits;
918 945
919 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 : 946 maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
920 png_ptr->usr_bit_depth); 947 png_ptr->usr_bit_depth);
921 if (sbit->red == 0 || sbit->red > maxbits || 948 if (sbit->red == 0 || sbit->red > maxbits ||
922 sbit->green == 0 || sbit->green > maxbits || 949 sbit->green == 0 || sbit->green > maxbits ||
923 sbit->blue == 0 || sbit->blue > maxbits) 950 sbit->blue == 0 || sbit->blue > maxbits)
(...skipping 24 matching lines...) Expand all
948 png_warning(png_ptr, "Invalid sBIT depth specified"); 975 png_warning(png_ptr, "Invalid sBIT depth specified");
949 return; 976 return;
950 } 977 }
951 buf[size++] = sbit->alpha; 978 buf[size++] = sbit->alpha;
952 } 979 }
953 980
954 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size); 981 png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
955 } 982 }
956 #endif 983 #endif
957 984
958 #if defined(PNG_WRITE_cHRM_SUPPORTED) 985 #ifdef PNG_WRITE_cHRM_SUPPORTED
959 /* Write the cHRM chunk */ 986 /* Write the cHRM chunk */
960 #ifdef PNG_FLOATING_POINT_SUPPORTED 987 #ifdef PNG_FLOATING_POINT_SUPPORTED
961 void /* PRIVATE */ 988 void /* PRIVATE */
962 png_write_cHRM(png_structp png_ptr, double white_x, double white_y, 989 png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
963 double red_x, double red_y, double green_x, double green_y, 990 double red_x, double red_y, double green_x, double green_y,
964 double blue_x, double blue_y) 991 double blue_x, double blue_y)
965 { 992 {
966 #ifdef PNG_USE_LOCAL_ARRAYS 993 #ifdef PNG_USE_LOCAL_ARRAYS
967 PNG_cHRM; 994 PNG_cHRM;
968 #endif 995 #endif
969 png_byte buf[32]; 996 png_byte buf[32];
970 997
971 png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y, 998 png_fixed_point int_white_x, int_white_y, int_red_x, int_red_y,
972 int_green_x, int_green_y, int_blue_x, int_blue_y; 999 int_green_x, int_green_y, int_blue_x, int_blue_y;
973 1000
974 png_debug(1, "in png_write_cHRM"); 1001 png_debug(1, "in png_write_cHRM");
975 1002
976 int_white_x = (png_uint_32)(white_x * 100000.0 + 0.5); 1003 int_white_x = (png_uint_32)(white_x * 100000.0 + 0.5);
977 int_white_y = (png_uint_32)(white_y * 100000.0 + 0.5); 1004 int_white_y = (png_uint_32)(white_y * 100000.0 + 0.5);
978 int_red_x = (png_uint_32)(red_x * 100000.0 + 0.5); 1005 int_red_x = (png_uint_32)(red_x * 100000.0 + 0.5);
979 int_red_y = (png_uint_32)(red_y * 100000.0 + 0.5); 1006 int_red_y = (png_uint_32)(red_y * 100000.0 + 0.5);
980 int_green_x = (png_uint_32)(green_x * 100000.0 + 0.5); 1007 int_green_x = (png_uint_32)(green_x * 100000.0 + 0.5);
981 int_green_y = (png_uint_32)(green_y * 100000.0 + 0.5); 1008 int_green_y = (png_uint_32)(green_y * 100000.0 + 0.5);
982 int_blue_x = (png_uint_32)(blue_x * 100000.0 + 0.5); 1009 int_blue_x = (png_uint_32)(blue_x * 100000.0 + 0.5);
983 int_blue_y = (png_uint_32)(blue_y * 100000.0 + 0.5); 1010 int_blue_y = (png_uint_32)(blue_y * 100000.0 + 0.5);
984 1011
985 #if !defined(PNG_NO_CHECK_cHRM) 1012 #ifdef PNG_CHECK_cHRM_SUPPORTED
986 if (png_check_cHRM_fixed(png_ptr, int_white_x, int_white_y, 1013 if (png_check_cHRM_fixed(png_ptr, int_white_x, int_white_y,
987 int_red_x, int_red_y, int_green_x, int_green_y, int_blue_x, int_blue_y)) 1014 int_red_x, int_red_y, int_green_x, int_green_y, int_blue_x, int_blue_y))
988 #endif 1015 #endif
989 { 1016 {
990 /* Each value is saved in 1/100,000ths */ 1017 /* Each value is saved in 1/100,000ths */
991 1018
992 png_save_uint_32(buf, int_white_x); 1019 png_save_uint_32(buf, int_white_x);
993 png_save_uint_32(buf + 4, int_white_y); 1020 png_save_uint_32(buf + 4, int_white_y);
994 1021
995 png_save_uint_32(buf + 8, int_red_x); 1022 png_save_uint_32(buf + 8, int_red_x);
996 png_save_uint_32(buf + 12, int_red_y); 1023 png_save_uint_32(buf + 12, int_red_y);
997 1024
998 png_save_uint_32(buf + 16, int_green_x); 1025 png_save_uint_32(buf + 16, int_green_x);
999 png_save_uint_32(buf + 20, int_green_y); 1026 png_save_uint_32(buf + 20, int_green_y);
1000 1027
1001 png_save_uint_32(buf + 24, int_blue_x); 1028 png_save_uint_32(buf + 24, int_blue_x);
1002 png_save_uint_32(buf + 28, int_blue_y); 1029 png_save_uint_32(buf + 28, int_blue_y);
1003 1030
1004 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32); 1031 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1005 } 1032 }
1006 } 1033 }
1007 #endif 1034 #endif
1008 #ifdef PNG_FIXED_POINT_SUPPORTED 1035 #ifdef PNG_FIXED_POINT_SUPPORTED
1009 void /* PRIVATE */ 1036 void /* PRIVATE */
1010 png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x, 1037 png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
1011 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y, 1038 png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
1012 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x, 1039 png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
1013 png_fixed_point blue_y) 1040 png_fixed_point blue_y)
1014 { 1041 {
1015 #ifdef PNG_USE_LOCAL_ARRAYS 1042 #ifdef PNG_USE_LOCAL_ARRAYS
1016 PNG_cHRM; 1043 PNG_cHRM;
1017 #endif 1044 #endif
1018 png_byte buf[32]; 1045 png_byte buf[32];
1019 1046
1020 png_debug(1, "in png_write_cHRM"); 1047 png_debug(1, "in png_write_cHRM");
1048
1021 /* Each value is saved in 1/100,000ths */ 1049 /* Each value is saved in 1/100,000ths */
1022 #if !defined(PNG_NO_CHECK_cHRM) 1050 #ifdef PNG_CHECK_cHRM_SUPPORTED
1023 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y, 1051 if (png_check_cHRM_fixed(png_ptr, white_x, white_y, red_x, red_y,
1024 green_x, green_y, blue_x, blue_y)) 1052 green_x, green_y, blue_x, blue_y))
1025 #endif 1053 #endif
1026 { 1054 {
1027 png_save_uint_32(buf, (png_uint_32)white_x); 1055 png_save_uint_32(buf, (png_uint_32)white_x);
1028 png_save_uint_32(buf + 4, (png_uint_32)white_y); 1056 png_save_uint_32(buf + 4, (png_uint_32)white_y);
1029 1057
1030 png_save_uint_32(buf + 8, (png_uint_32)red_x); 1058 png_save_uint_32(buf + 8, (png_uint_32)red_x);
1031 png_save_uint_32(buf + 12, (png_uint_32)red_y); 1059 png_save_uint_32(buf + 12, (png_uint_32)red_y);
1032 1060
1033 png_save_uint_32(buf + 16, (png_uint_32)green_x); 1061 png_save_uint_32(buf + 16, (png_uint_32)green_x);
1034 png_save_uint_32(buf + 20, (png_uint_32)green_y); 1062 png_save_uint_32(buf + 20, (png_uint_32)green_y);
1035 1063
1036 png_save_uint_32(buf + 24, (png_uint_32)blue_x); 1064 png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1037 png_save_uint_32(buf + 28, (png_uint_32)blue_y); 1065 png_save_uint_32(buf + 28, (png_uint_32)blue_y);
1038 1066
1039 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32); 1067 png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
1040 } 1068 }
1041 } 1069 }
1042 #endif 1070 #endif
1043 #endif 1071 #endif
1044 1072
1045 #if defined(PNG_WRITE_tRNS_SUPPORTED) 1073 #ifdef PNG_WRITE_tRNS_SUPPORTED
1046 /* Write the tRNS chunk */ 1074 /* Write the tRNS chunk */
1047 void /* PRIVATE */ 1075 void /* PRIVATE */
1048 png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran, 1076 png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
1049 int num_trans, int color_type) 1077 int num_trans, int color_type)
1050 { 1078 {
1051 #ifdef PNG_USE_LOCAL_ARRAYS 1079 #ifdef PNG_USE_LOCAL_ARRAYS
1052 PNG_tRNS; 1080 PNG_tRNS;
1053 #endif 1081 #endif
1054 png_byte buf[6]; 1082 png_byte buf[6];
1055 1083
1056 png_debug(1, "in png_write_tRNS"); 1084 png_debug(1, "in png_write_tRNS");
1085
1057 if (color_type == PNG_COLOR_TYPE_PALETTE) 1086 if (color_type == PNG_COLOR_TYPE_PALETTE)
1058 { 1087 {
1059 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette) 1088 if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
1060 { 1089 {
1061 png_warning(png_ptr, "Invalid number of transparent colors specified"); 1090 png_warning(png_ptr, "Invalid number of transparent colors specified");
1062 return; 1091 return;
1063 } 1092 }
1064 /* Write the chunk out as it is */ 1093 /* Write the chunk out as it is */
1065 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, 1094 png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans,
1066 (png_size_t)num_trans); 1095 (png_size_t)num_trans);
(...skipping 24 matching lines...) Expand all
1091 } 1120 }
1092 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6); 1121 png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
1093 } 1122 }
1094 else 1123 else
1095 { 1124 {
1096 png_warning(png_ptr, "Can't write tRNS with an alpha channel"); 1125 png_warning(png_ptr, "Can't write tRNS with an alpha channel");
1097 } 1126 }
1098 } 1127 }
1099 #endif 1128 #endif
1100 1129
1101 #if defined(PNG_WRITE_bKGD_SUPPORTED) 1130 #ifdef PNG_WRITE_bKGD_SUPPORTED
1102 /* Write the background chunk */ 1131 /* Write the background chunk */
1103 void /* PRIVATE */ 1132 void /* PRIVATE */
1104 png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type) 1133 png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
1105 { 1134 {
1106 #ifdef PNG_USE_LOCAL_ARRAYS 1135 #ifdef PNG_USE_LOCAL_ARRAYS
1107 PNG_bKGD; 1136 PNG_bKGD;
1108 #endif 1137 #endif
1109 png_byte buf[6]; 1138 png_byte buf[6];
1110 1139
1111 png_debug(1, "in png_write_bKGD"); 1140 png_debug(1, "in png_write_bKGD");
1141
1112 if (color_type == PNG_COLOR_TYPE_PALETTE) 1142 if (color_type == PNG_COLOR_TYPE_PALETTE)
1113 { 1143 {
1114 if ( 1144 if (
1115 #if defined(PNG_MNG_FEATURES_SUPPORTED) 1145 #ifdef PNG_MNG_FEATURES_SUPPORTED
1116 (png_ptr->num_palette || 1146 (png_ptr->num_palette ||
1117 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) && 1147 (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
1118 #endif 1148 #endif
1119 back->index >= png_ptr->num_palette) 1149 back->index >= png_ptr->num_palette)
1120 { 1150 {
1121 png_warning(png_ptr, "Invalid background palette index"); 1151 png_warning(png_ptr, "Invalid background palette index");
1122 return; 1152 return;
1123 } 1153 }
1124 buf[0] = back->index; 1154 buf[0] = back->index;
1125 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1); 1155 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
(...skipping 18 matching lines...) Expand all
1144 png_warning(png_ptr, 1174 png_warning(png_ptr,
1145 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth"); 1175 "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1146 return; 1176 return;
1147 } 1177 }
1148 png_save_uint_16(buf, back->gray); 1178 png_save_uint_16(buf, back->gray);
1149 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2); 1179 png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
1150 } 1180 }
1151 } 1181 }
1152 #endif 1182 #endif
1153 1183
1154 #if defined(PNG_WRITE_hIST_SUPPORTED) 1184 #ifdef PNG_WRITE_hIST_SUPPORTED
1155 /* Write the histogram */ 1185 /* Write the histogram */
1156 void /* PRIVATE */ 1186 void /* PRIVATE */
1157 png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist) 1187 png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
1158 { 1188 {
1159 #ifdef PNG_USE_LOCAL_ARRAYS 1189 #ifdef PNG_USE_LOCAL_ARRAYS
1160 PNG_hIST; 1190 PNG_hIST;
1161 #endif 1191 #endif
1162 int i; 1192 int i;
1163 png_byte buf[3]; 1193 png_byte buf[3];
1164 1194
1165 png_debug(1, "in png_write_hIST"); 1195 png_debug(1, "in png_write_hIST");
1196
1166 if (num_hist > (int)png_ptr->num_palette) 1197 if (num_hist > (int)png_ptr->num_palette)
1167 { 1198 {
1168 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist, 1199 png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
1169 png_ptr->num_palette); 1200 png_ptr->num_palette);
1170 png_warning(png_ptr, "Invalid number of histogram entries specified"); 1201 png_warning(png_ptr, "Invalid number of histogram entries specified");
1171 return; 1202 return;
1172 } 1203 }
1173 1204
1174 png_write_chunk_start(png_ptr, (png_bytep)png_hIST, 1205 png_write_chunk_start(png_ptr, (png_bytep)png_hIST,
1175 (png_uint_32)(num_hist * 2)); 1206 (png_uint_32)(num_hist * 2));
(...skipping 20 matching lines...) Expand all
1196 */ 1227 */
1197 png_size_t /* PRIVATE */ 1228 png_size_t /* PRIVATE */
1198 png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key) 1229 png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
1199 { 1230 {
1200 png_size_t key_len; 1231 png_size_t key_len;
1201 png_charp kp, dp; 1232 png_charp kp, dp;
1202 int kflag; 1233 int kflag;
1203 int kwarn=0; 1234 int kwarn=0;
1204 1235
1205 png_debug(1, "in png_check_keyword"); 1236 png_debug(1, "in png_check_keyword");
1237
1206 *new_key = NULL; 1238 *new_key = NULL;
1207 1239
1208 if (key == NULL || (key_len = png_strlen(key)) == 0) 1240 if (key == NULL || (key_len = png_strlen(key)) == 0)
1209 { 1241 {
1210 png_warning(png_ptr, "zero length keyword"); 1242 png_warning(png_ptr, "zero length keyword");
1211 return ((png_size_t)0); 1243 return ((png_size_t)0);
1212 } 1244 }
1213 1245
1214 png_debug1(2, "Keyword to be checked is '%s'", key); 1246 png_debug1(2, "Keyword to be checked is '%s'", key);
1215 1247
1216 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2)); 1248 *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
1217 if (*new_key == NULL) 1249 if (*new_key == NULL)
1218 { 1250 {
1219 png_warning(png_ptr, "Out of memory while procesing keyword"); 1251 png_warning(png_ptr, "Out of memory while procesing keyword");
1220 return ((png_size_t)0); 1252 return ((png_size_t)0);
1221 } 1253 }
1222 1254
1223 /* Replace non-printing characters with a blank and print a warning */ 1255 /* Replace non-printing characters with a blank and print a warning */
1224 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++) 1256 for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
1225 { 1257 {
1226 if ((png_byte)*kp < 0x20 || 1258 if ((png_byte)*kp < 0x20 ||
1227 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1)) 1259 ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
1228 { 1260 {
1229 #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE) 1261 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
1230 char msg[40]; 1262 char msg[40];
1231 1263
1232 png_snprintf(msg, 40, 1264 png_snprintf(msg, 40,
1233 "invalid keyword character 0x%02X", (png_byte)*kp); 1265 "invalid keyword character 0x%02X", (png_byte)*kp);
1234 png_warning(png_ptr, msg); 1266 png_warning(png_ptr, msg);
1235 #else 1267 #else
1236 png_warning(png_ptr, "invalid character in keyword"); 1268 png_warning(png_ptr, "invalid character in keyword");
1237 #endif 1269 #endif
1238 *dp = ' '; 1270 *dp = ' ';
1239 } 1271 }
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1306 { 1338 {
1307 png_warning(png_ptr, "keyword length must be 1 - 79 characters"); 1339 png_warning(png_ptr, "keyword length must be 1 - 79 characters");
1308 (*new_key)[79] = '\0'; 1340 (*new_key)[79] = '\0';
1309 key_len = 79; 1341 key_len = 79;
1310 } 1342 }
1311 1343
1312 return (key_len); 1344 return (key_len);
1313 } 1345 }
1314 #endif 1346 #endif
1315 1347
1316 #if defined(PNG_WRITE_tEXt_SUPPORTED) 1348 #ifdef PNG_WRITE_tEXt_SUPPORTED
1317 /* Write a tEXt chunk */ 1349 /* Write a tEXt chunk */
1318 void /* PRIVATE */ 1350 void /* PRIVATE */
1319 png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text, 1351 png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
1320 png_size_t text_len) 1352 png_size_t text_len)
1321 { 1353 {
1322 #ifdef PNG_USE_LOCAL_ARRAYS 1354 #ifdef PNG_USE_LOCAL_ARRAYS
1323 PNG_tEXt; 1355 PNG_tEXt;
1324 #endif 1356 #endif
1325 png_size_t key_len; 1357 png_size_t key_len;
1326 png_charp new_key; 1358 png_charp new_key;
1327 1359
1328 png_debug(1, "in png_write_tEXt"); 1360 png_debug(1, "in png_write_tEXt");
1361
1329 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0) 1362 if ((key_len = png_check_keyword(png_ptr, key, &new_key))==0)
1330 return; 1363 return;
1331 1364
1332 if (text == NULL || *text == '\0') 1365 if (text == NULL || *text == '\0')
1333 text_len = 0; 1366 text_len = 0;
1334 else 1367 else
1335 text_len = png_strlen(text); 1368 text_len = png_strlen(text);
1336 1369
1337 /* Make sure we include the 0 after the key */ 1370 /* Make sure we include the 0 after the key */
1338 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, 1371 png_write_chunk_start(png_ptr, (png_bytep)png_tEXt,
1339 (png_uint_32)(key_len + text_len + 1)); 1372 (png_uint_32)(key_len + text_len + 1));
1340 /* 1373 /*
1341 * We leave it to the application to meet PNG-1.0 requirements on the 1374 * We leave it to the application to meet PNG-1.0 requirements on the
1342 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of 1375 * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1343 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. 1376 * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1344 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. 1377 * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1345 */ 1378 */
1346 png_write_chunk_data(png_ptr, (png_bytep)new_key, 1379 png_write_chunk_data(png_ptr, (png_bytep)new_key,
1347 (png_size_t)(key_len + 1)); 1380 (png_size_t)(key_len + 1));
1348 if (text_len) 1381 if (text_len)
1349 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len); 1382 png_write_chunk_data(png_ptr, (png_bytep)text, (png_size_t)text_len);
1350 1383
1351 png_write_chunk_end(png_ptr); 1384 png_write_chunk_end(png_ptr);
1352 png_free(png_ptr, new_key); 1385 png_free(png_ptr, new_key);
1353 } 1386 }
1354 #endif 1387 #endif
1355 1388
1356 #if defined(PNG_WRITE_zTXt_SUPPORTED) 1389 #ifdef PNG_WRITE_zTXt_SUPPORTED
1357 /* Write a compressed text chunk */ 1390 /* Write a compressed text chunk */
1358 void /* PRIVATE */ 1391 void /* PRIVATE */
1359 png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text, 1392 png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
1360 png_size_t text_len, int compression) 1393 png_size_t text_len, int compression)
1361 { 1394 {
1362 #ifdef PNG_USE_LOCAL_ARRAYS 1395 #ifdef PNG_USE_LOCAL_ARRAYS
1363 PNG_zTXt; 1396 PNG_zTXt;
1364 #endif 1397 #endif
1365 png_size_t key_len; 1398 png_size_t key_len;
1366 char buf[1]; 1399 char buf[1];
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
1406 /* Write compression */ 1439 /* Write compression */
1407 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1); 1440 png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
1408 /* Write the compressed data */ 1441 /* Write the compressed data */
1409 png_write_compressed_data_out(png_ptr, &comp); 1442 png_write_compressed_data_out(png_ptr, &comp);
1410 1443
1411 /* Close the chunk */ 1444 /* Close the chunk */
1412 png_write_chunk_end(png_ptr); 1445 png_write_chunk_end(png_ptr);
1413 } 1446 }
1414 #endif 1447 #endif
1415 1448
1416 #if defined(PNG_WRITE_iTXt_SUPPORTED) 1449 #ifdef PNG_WRITE_iTXt_SUPPORTED
1417 /* Write an iTXt chunk */ 1450 /* Write an iTXt chunk */
1418 void /* PRIVATE */ 1451 void /* PRIVATE */
1419 png_write_iTXt(png_structp png_ptr, int compression, png_charp key, 1452 png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
1420 png_charp lang, png_charp lang_key, png_charp text) 1453 png_charp lang, png_charp lang_key, png_charp text)
1421 { 1454 {
1422 #ifdef PNG_USE_LOCAL_ARRAYS 1455 #ifdef PNG_USE_LOCAL_ARRAYS
1423 PNG_iTXt; 1456 PNG_iTXt;
1424 #endif 1457 #endif
1425 png_size_t lang_len, key_len, lang_key_len, text_len; 1458 png_size_t lang_len, key_len, lang_key_len, text_len;
1426 png_charp new_lang; 1459 png_charp new_lang;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1495 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), 1528 png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf),
1496 (png_size_t)(lang_key_len + 1)); 1529 (png_size_t)(lang_key_len + 1));
1497 png_write_compressed_data_out(png_ptr, &comp); 1530 png_write_compressed_data_out(png_ptr, &comp);
1498 1531
1499 png_write_chunk_end(png_ptr); 1532 png_write_chunk_end(png_ptr);
1500 png_free(png_ptr, new_key); 1533 png_free(png_ptr, new_key);
1501 png_free(png_ptr, new_lang); 1534 png_free(png_ptr, new_lang);
1502 } 1535 }
1503 #endif 1536 #endif
1504 1537
1505 #if defined(PNG_WRITE_oFFs_SUPPORTED) 1538 #ifdef PNG_WRITE_oFFs_SUPPORTED
1506 /* Write the oFFs chunk */ 1539 /* Write the oFFs chunk */
1507 void /* PRIVATE */ 1540 void /* PRIVATE */
1508 png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset, 1541 png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
1509 int unit_type) 1542 int unit_type)
1510 { 1543 {
1511 #ifdef PNG_USE_LOCAL_ARRAYS 1544 #ifdef PNG_USE_LOCAL_ARRAYS
1512 PNG_oFFs; 1545 PNG_oFFs;
1513 #endif 1546 #endif
1514 png_byte buf[9]; 1547 png_byte buf[9];
1515 1548
1516 png_debug(1, "in png_write_oFFs"); 1549 png_debug(1, "in png_write_oFFs");
1550
1517 if (unit_type >= PNG_OFFSET_LAST) 1551 if (unit_type >= PNG_OFFSET_LAST)
1518 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk"); 1552 png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1519 1553
1520 png_save_int_32(buf, x_offset); 1554 png_save_int_32(buf, x_offset);
1521 png_save_int_32(buf + 4, y_offset); 1555 png_save_int_32(buf + 4, y_offset);
1522 buf[8] = (png_byte)unit_type; 1556 buf[8] = (png_byte)unit_type;
1523 1557
1524 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9); 1558 png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
1525 } 1559 }
1526 #endif 1560 #endif
1527 #if defined(PNG_WRITE_pCAL_SUPPORTED) 1561 #ifdef PNG_WRITE_pCAL_SUPPORTED
1528 /* Write the pCAL chunk (described in the PNG extensions document) */ 1562 /* Write the pCAL chunk (described in the PNG extensions document) */
1529 void /* PRIVATE */ 1563 void /* PRIVATE */
1530 png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0, 1564 png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
1531 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params) 1565 png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
1532 { 1566 {
1533 #ifdef PNG_USE_LOCAL_ARRAYS 1567 #ifdef PNG_USE_LOCAL_ARRAYS
1534 PNG_pCAL; 1568 PNG_pCAL;
1535 #endif 1569 #endif
1536 png_size_t purpose_len, units_len, total_len; 1570 png_size_t purpose_len, units_len, total_len;
1537 png_uint_32p params_len; 1571 png_uint_32p params_len;
1538 png_byte buf[10]; 1572 png_byte buf[10];
1539 png_charp new_purpose; 1573 png_charp new_purpose;
1540 int i; 1574 int i;
1541 1575
1542 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams); 1576 png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
1577
1543 if (type >= PNG_EQUATION_LAST) 1578 if (type >= PNG_EQUATION_LAST)
1544 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk"); 1579 png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
1545 1580
1546 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1; 1581 purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
1547 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len); 1582 png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
1548 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1); 1583 units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
1549 png_debug1(3, "pCAL units length = %d", (int)units_len); 1584 png_debug1(3, "pCAL units length = %d", (int)units_len);
1550 total_len = purpose_len + units_len + 10; 1585 total_len = purpose_len + units_len + 10;
1551 1586
1552 params_len = (png_uint_32p)png_malloc(png_ptr, 1587 params_len = (png_uint_32p)png_malloc(png_ptr,
(...skipping 26 matching lines...) Expand all
1579 { 1614 {
1580 png_write_chunk_data(png_ptr, (png_bytep)params[i], 1615 png_write_chunk_data(png_ptr, (png_bytep)params[i],
1581 (png_size_t)params_len[i]); 1616 (png_size_t)params_len[i]);
1582 } 1617 }
1583 1618
1584 png_free(png_ptr, params_len); 1619 png_free(png_ptr, params_len);
1585 png_write_chunk_end(png_ptr); 1620 png_write_chunk_end(png_ptr);
1586 } 1621 }
1587 #endif 1622 #endif
1588 1623
1589 #if defined(PNG_WRITE_sCAL_SUPPORTED) 1624 #ifdef PNG_WRITE_sCAL_SUPPORTED
1590 /* Write the sCAL chunk */ 1625 /* Write the sCAL chunk */
1591 #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO) 1626 #if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
1592 void /* PRIVATE */ 1627 void /* PRIVATE */
1593 png_write_sCAL(png_structp png_ptr, int unit, double width, double height) 1628 png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
1594 { 1629 {
1595 #ifdef PNG_USE_LOCAL_ARRAYS 1630 #ifdef PNG_USE_LOCAL_ARRAYS
1596 PNG_sCAL; 1631 PNG_sCAL;
1597 #endif 1632 #endif
1598 char buf[64]; 1633 char buf[64];
1599 png_size_t total_len; 1634 png_size_t total_len;
1600 1635
1601 png_debug(1, "in png_write_sCAL"); 1636 png_debug(1, "in png_write_sCAL");
1602 1637
1603 buf[0] = (char)unit; 1638 buf[0] = (char)unit;
1604 #if defined(_WIN32_WCE) 1639 #ifdef _WIN32_WCE
1605 /* sprintf() function is not supported on WindowsCE */ 1640 /* sprintf() function is not supported on WindowsCE */
1606 { 1641 {
1607 wchar_t wc_buf[32]; 1642 wchar_t wc_buf[32];
1608 size_t wc_len; 1643 size_t wc_len;
1609 swprintf(wc_buf, TEXT("%12.12e"), width); 1644 swprintf(wc_buf, TEXT("%12.12e"), width);
1610 wc_len = wcslen(wc_buf); 1645 wc_len = wcslen(wc_buf);
1611 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL); 1646 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL,
1647 NULL);
1612 total_len = wc_len + 2; 1648 total_len = wc_len + 2;
1613 swprintf(wc_buf, TEXT("%12.12e"), height); 1649 swprintf(wc_buf, TEXT("%12.12e"), height);
1614 wc_len = wcslen(wc_buf); 1650 wc_len = wcslen(wc_buf);
1615 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len, 1651 WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
1616 NULL, NULL); 1652 NULL, NULL);
1617 total_len += wc_len; 1653 total_len += wc_len;
1618 } 1654 }
1619 #else 1655 #else
1620 png_snprintf(buf + 1, 63, "%12.12e", width); 1656 png_snprintf(buf + 1, 63, "%12.12e", width);
1621 total_len = 1 + png_strlen(buf + 1) + 1; 1657 total_len = 1 + png_strlen(buf + 1) + 1;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1653 png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */ 1689 png_memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
1654 png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */ 1690 png_memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
1655 1691
1656 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len); 1692 png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
1657 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len); 1693 png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
1658 } 1694 }
1659 #endif 1695 #endif
1660 #endif 1696 #endif
1661 #endif 1697 #endif
1662 1698
1663 #if defined(PNG_WRITE_pHYs_SUPPORTED) 1699 #ifdef PNG_WRITE_pHYs_SUPPORTED
1664 /* Write the pHYs chunk */ 1700 /* Write the pHYs chunk */
1665 void /* PRIVATE */ 1701 void /* PRIVATE */
1666 png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit, 1702 png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
1667 png_uint_32 y_pixels_per_unit, 1703 png_uint_32 y_pixels_per_unit,
1668 int unit_type) 1704 int unit_type)
1669 { 1705 {
1670 #ifdef PNG_USE_LOCAL_ARRAYS 1706 #ifdef PNG_USE_LOCAL_ARRAYS
1671 PNG_pHYs; 1707 PNG_pHYs;
1672 #endif 1708 #endif
1673 png_byte buf[9]; 1709 png_byte buf[9];
1674 1710
1675 png_debug(1, "in png_write_pHYs"); 1711 png_debug(1, "in png_write_pHYs");
1712
1676 if (unit_type >= PNG_RESOLUTION_LAST) 1713 if (unit_type >= PNG_RESOLUTION_LAST)
1677 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk"); 1714 png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1678 1715
1679 png_save_uint_32(buf, x_pixels_per_unit); 1716 png_save_uint_32(buf, x_pixels_per_unit);
1680 png_save_uint_32(buf + 4, y_pixels_per_unit); 1717 png_save_uint_32(buf + 4, y_pixels_per_unit);
1681 buf[8] = (png_byte)unit_type; 1718 buf[8] = (png_byte)unit_type;
1682 1719
1683 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9); 1720 png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
1684 } 1721 }
1685 #endif 1722 #endif
1686 1723
1687 #if defined(PNG_WRITE_tIME_SUPPORTED) 1724 #ifdef PNG_WRITE_tIME_SUPPORTED
1688 /* Write the tIME chunk. Use either png_convert_from_struct_tm() 1725 /* Write the tIME chunk. Use either png_convert_from_struct_tm()
1689 * or png_convert_from_time_t(), or fill in the structure yourself. 1726 * or png_convert_from_time_t(), or fill in the structure yourself.
1690 */ 1727 */
1691 void /* PRIVATE */ 1728 void /* PRIVATE */
1692 png_write_tIME(png_structp png_ptr, png_timep mod_time) 1729 png_write_tIME(png_structp png_ptr, png_timep mod_time)
1693 { 1730 {
1694 #ifdef PNG_USE_LOCAL_ARRAYS 1731 #ifdef PNG_USE_LOCAL_ARRAYS
1695 PNG_tIME; 1732 PNG_tIME;
1696 #endif 1733 #endif
1697 png_byte buf[7]; 1734 png_byte buf[7];
1698 1735
1699 png_debug(1, "in png_write_tIME"); 1736 png_debug(1, "in png_write_tIME");
1737
1700 if (mod_time->month > 12 || mod_time->month < 1 || 1738 if (mod_time->month > 12 || mod_time->month < 1 ||
1701 mod_time->day > 31 || mod_time->day < 1 || 1739 mod_time->day > 31 || mod_time->day < 1 ||
1702 mod_time->hour > 23 || mod_time->second > 60) 1740 mod_time->hour > 23 || mod_time->second > 60)
1703 { 1741 {
1704 png_warning(png_ptr, "Invalid time specified for tIME chunk"); 1742 png_warning(png_ptr, "Invalid time specified for tIME chunk");
1705 return; 1743 return;
1706 } 1744 }
1707 1745
1708 png_save_uint_16(buf, mod_time->year); 1746 png_save_uint_16(buf, mod_time->year);
1709 buf[2] = mod_time->month; 1747 buf[2] = mod_time->month;
1710 buf[3] = mod_time->day; 1748 buf[3] = mod_time->day;
1711 buf[4] = mod_time->hour; 1749 buf[4] = mod_time->hour;
1712 buf[5] = mod_time->minute; 1750 buf[5] = mod_time->minute;
1713 buf[6] = mod_time->second; 1751 buf[6] = mod_time->second;
1714 1752
1715 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7); 1753 png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
1716 } 1754 }
1717 #endif 1755 #endif
1718 1756
1719 /* Initializes the row writing capability of libpng */ 1757 /* Initializes the row writing capability of libpng */
1720 void /* PRIVATE */ 1758 void /* PRIVATE */
1721 png_write_start_row(png_structp png_ptr) 1759 png_write_start_row(png_structp png_ptr)
1722 { 1760 {
1723 #ifdef PNG_WRITE_INTERLACING_SUPPORTED 1761 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1724 #ifdef PNG_USE_LOCAL_ARRAYS
1725 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 1762 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1726 1763
1727 /* Start of interlace block */ 1764 /* Start of interlace block */
1728 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; 1765 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1729 1766
1730 /* Offset to next interlace block */ 1767 /* Offset to next interlace block */
1731 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; 1768 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1732 1769
1733 /* Start of interlace block in the y direction */ 1770 /* Start of interlace block in the y direction */
1734 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; 1771 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1735 1772
1736 /* Offset to next interlace block in the y direction */ 1773 /* Offset to next interlace block in the y direction */
1737 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; 1774 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1738 #endif 1775 #endif
1739 #endif
1740 1776
1741 png_size_t buf_size; 1777 png_size_t buf_size;
1742 1778
1743 png_debug(1, "in png_write_start_row"); 1779 png_debug(1, "in png_write_start_row");
1780
1744 buf_size = (png_size_t)(PNG_ROWBYTES( 1781 buf_size = (png_size_t)(PNG_ROWBYTES(
1745 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1); 1782 png_ptr->usr_channels*png_ptr->usr_bit_depth, png_ptr->width) + 1);
1746 1783
1747 /* Set up row buffer */ 1784 /* Set up row buffer */
1748 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, 1785 png_ptr->row_buf = (png_bytep)png_malloc(png_ptr,
1749 (png_uint_32)buf_size); 1786 (png_uint_32)buf_size);
1750 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; 1787 png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
1751 1788
1752 #ifndef PNG_NO_WRITE_FILTER 1789 #ifdef PNG_WRITE_FILTER_SUPPORTED
1753 /* Set up filtering buffer, if using this filter */ 1790 /* Set up filtering buffer, if using this filter */
1754 if (png_ptr->do_filter & PNG_FILTER_SUB) 1791 if (png_ptr->do_filter & PNG_FILTER_SUB)
1755 { 1792 {
1756 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, 1793 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1757 (png_uint_32)(png_ptr->rowbytes + 1)); 1794 (png_uint_32)(png_ptr->rowbytes + 1));
1758 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB; 1795 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1759 } 1796 }
1760 1797
1761 /* We only need to keep the previous row if we are using one of these. */ 1798 /* We only need to keep the previous row if we are using one of these. */
1762 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) 1799 if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
1763 { 1800 {
1764 /* Set up previous row buffer */ 1801 /* Set up previous row buffer */
1765 png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, 1802 png_ptr->prev_row = (png_bytep)png_calloc(png_ptr,
1766 (png_uint_32)buf_size); 1803 (png_uint_32)buf_size);
1767 png_memset(png_ptr->prev_row, 0, buf_size);
1768 1804
1769 if (png_ptr->do_filter & PNG_FILTER_UP) 1805 if (png_ptr->do_filter & PNG_FILTER_UP)
1770 { 1806 {
1771 png_ptr->up_row = (png_bytep)png_malloc(png_ptr, 1807 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1772 (png_uint_32)(png_ptr->rowbytes + 1)); 1808 (png_uint_32)(png_ptr->rowbytes + 1));
1773 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP; 1809 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1774 } 1810 }
1775 1811
1776 if (png_ptr->do_filter & PNG_FILTER_AVG) 1812 if (png_ptr->do_filter & PNG_FILTER_AVG)
1777 { 1813 {
1778 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr, 1814 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1779 (png_uint_32)(png_ptr->rowbytes + 1)); 1815 (png_uint_32)(png_ptr->rowbytes + 1));
1780 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG; 1816 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1781 } 1817 }
1782 1818
1783 if (png_ptr->do_filter & PNG_FILTER_PAETH) 1819 if (png_ptr->do_filter & PNG_FILTER_PAETH)
1784 { 1820 {
1785 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr, 1821 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
1786 (png_uint_32)(png_ptr->rowbytes + 1)); 1822 (png_uint_32)(png_ptr->rowbytes + 1));
1787 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH; 1823 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1788 } 1824 }
1789 } 1825 }
1790 #endif /* PNG_NO_WRITE_FILTER */ 1826 #endif /* PNG_WRITE_FILTER_SUPPORTED */
1791 1827
1792 #ifdef PNG_WRITE_INTERLACING_SUPPORTED 1828 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1793 /* If interlaced, we need to set up width and height of pass */ 1829 /* If interlaced, we need to set up width and height of pass */
1794 if (png_ptr->interlaced) 1830 if (png_ptr->interlaced)
1795 { 1831 {
1796 if (!(png_ptr->transformations & PNG_INTERLACE)) 1832 if (!(png_ptr->transformations & PNG_INTERLACE))
1797 { 1833 {
1798 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - 1834 png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
1799 png_pass_ystart[0]) / png_pass_yinc[0]; 1835 png_pass_ystart[0]) / png_pass_yinc[0];
1800 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 - 1836 png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
(...skipping 13 matching lines...) Expand all
1814 } 1850 }
1815 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size; 1851 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1816 png_ptr->zstream.next_out = png_ptr->zbuf; 1852 png_ptr->zstream.next_out = png_ptr->zbuf;
1817 } 1853 }
1818 1854
1819 /* Internal use only. Called when finished processing a row of data. */ 1855 /* Internal use only. Called when finished processing a row of data. */
1820 void /* PRIVATE */ 1856 void /* PRIVATE */
1821 png_write_finish_row(png_structp png_ptr) 1857 png_write_finish_row(png_structp png_ptr)
1822 { 1858 {
1823 #ifdef PNG_WRITE_INTERLACING_SUPPORTED 1859 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1824 #ifdef PNG_USE_LOCAL_ARRAYS
1825 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 1860 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1826 1861
1827 /* Start of interlace block */ 1862 /* Start of interlace block */
1828 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; 1863 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1829 1864
1830 /* Offset to next interlace block */ 1865 /* Offset to next interlace block */
1831 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; 1866 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1832 1867
1833 /* Start of interlace block in the y direction */ 1868 /* Start of interlace block in the y direction */
1834 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; 1869 int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1835 1870
1836 /* Offset to next interlace block in the y direction */ 1871 /* Offset to next interlace block in the y direction */
1837 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; 1872 int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1838 #endif 1873 #endif
1839 #endif
1840 1874
1841 int ret; 1875 int ret;
1842 1876
1843 png_debug(1, "in png_write_finish_row"); 1877 png_debug(1, "in png_write_finish_row");
1878
1844 /* Next row */ 1879 /* Next row */
1845 png_ptr->row_number++; 1880 png_ptr->row_number++;
1846 1881
1847 /* See if we are done */ 1882 /* See if we are done */
1848 if (png_ptr->row_number < png_ptr->num_rows) 1883 if (png_ptr->row_number < png_ptr->num_rows)
1849 return; 1884 return;
1850 1885
1851 #ifdef PNG_WRITE_INTERLACING_SUPPORTED 1886 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1852 /* If interlaced, go to next pass */ 1887 /* If interlaced, go to next pass */
1853 if (png_ptr->interlaced) 1888 if (png_ptr->interlaced)
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1921 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size) 1956 if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
1922 { 1957 {
1923 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size - 1958 png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
1924 png_ptr->zstream.avail_out); 1959 png_ptr->zstream.avail_out);
1925 } 1960 }
1926 1961
1927 deflateReset(&png_ptr->zstream); 1962 deflateReset(&png_ptr->zstream);
1928 png_ptr->zstream.data_type = Z_BINARY; 1963 png_ptr->zstream.data_type = Z_BINARY;
1929 } 1964 }
1930 1965
1931 #if defined(PNG_WRITE_INTERLACING_SUPPORTED) 1966 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1932 /* Pick out the correct pixels for the interlace pass. 1967 /* Pick out the correct pixels for the interlace pass.
1933 * The basic idea here is to go through the row with a source 1968 * The basic idea here is to go through the row with a source
1934 * pointer and a destination pointer (sp and dp), and copy the 1969 * pointer and a destination pointer (sp and dp), and copy the
1935 * correct pixels for the pass. As the row gets compacted, 1970 * correct pixels for the pass. As the row gets compacted,
1936 * sp will always be >= dp, so we should never overwrite anything. 1971 * sp will always be >= dp, so we should never overwrite anything.
1937 * See the default: case for the easiest code to understand. 1972 * See the default: case for the easiest code to understand.
1938 */ 1973 */
1939 void /* PRIVATE */ 1974 void /* PRIVATE */
1940 png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) 1975 png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
1941 { 1976 {
1942 #ifdef PNG_USE_LOCAL_ARRAYS
1943 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ 1977 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1944 1978
1945 /* Start of interlace block */ 1979 /* Start of interlace block */
1946 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; 1980 int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1947 1981
1948 /* Offset to next interlace block */ 1982 /* Offset to next interlace block */
1949 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; 1983 int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1950 #endif
1951 1984
1952 png_debug(1, "in png_do_write_interlace"); 1985 png_debug(1, "in png_do_write_interlace");
1986
1953 /* We don't have to do anything on the last pass (6) */ 1987 /* We don't have to do anything on the last pass (6) */
1954 #if defined(PNG_USELESS_TESTS_SUPPORTED) 1988 #ifdef PNG_USELESS_TESTS_SUPPORTED
1955 if (row != NULL && row_info != NULL && pass < 6) 1989 if (row != NULL && row_info != NULL && pass < 6)
1956 #else 1990 #else
1957 if (pass < 6) 1991 if (pass < 6)
1958 #endif 1992 #endif
1959 { 1993 {
1960 /* Each pixel depth is handled separately */ 1994 /* Each pixel depth is handled separately */
1961 switch (row_info->pixel_depth) 1995 switch (row_info->pixel_depth)
1962 { 1996 {
1963 case 1: 1997 case 1:
1964 { 1998 {
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
2104 * chosen filter. 2138 * chosen filter.
2105 */ 2139 */
2106 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1) 2140 #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
2107 #define PNG_HISHIFT 10 2141 #define PNG_HISHIFT 10
2108 #define PNG_LOMASK ((png_uint_32)0xffffL) 2142 #define PNG_LOMASK ((png_uint_32)0xffffL)
2109 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT)) 2143 #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
2110 void /* PRIVATE */ 2144 void /* PRIVATE */
2111 png_write_find_filter(png_structp png_ptr, png_row_infop row_info) 2145 png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
2112 { 2146 {
2113 png_bytep best_row; 2147 png_bytep best_row;
2114 #ifndef PNG_NO_WRITE_FILTER 2148 #ifdef PNG_WRITE_FILTER_SUPPORTED
2115 png_bytep prev_row, row_buf; 2149 png_bytep prev_row, row_buf;
2116 png_uint_32 mins, bpp; 2150 png_uint_32 mins, bpp;
2117 png_byte filter_to_do = png_ptr->do_filter; 2151 png_byte filter_to_do = png_ptr->do_filter;
2118 png_uint_32 row_bytes = row_info->rowbytes; 2152 png_uint_32 row_bytes = row_info->rowbytes;
2119 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2153 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2120 int num_p_filters = (int)png_ptr->num_prev_filters; 2154 int num_p_filters = (int)png_ptr->num_prev_filters;
2121 #endif 2155 #endif
2122 2156
2123 png_debug(1, "in png_write_find_filter"); 2157 png_debug(1, "in png_write_find_filter");
2158
2159 #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2160 if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
2161 {
2162 /* These will never be selected so we need not test them. */
2163 filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
2164 }
2165 #endif
2166
2124 /* Find out how many bytes offset each pixel is */ 2167 /* Find out how many bytes offset each pixel is */
2125 bpp = (row_info->pixel_depth + 7) >> 3; 2168 bpp = (row_info->pixel_depth + 7) >> 3;
2126 2169
2127 prev_row = png_ptr->prev_row; 2170 prev_row = png_ptr->prev_row;
2128 #endif 2171 #endif
2129 best_row = png_ptr->row_buf; 2172 best_row = png_ptr->row_buf;
2130 #ifndef PNG_NO_WRITE_FILTER 2173 #ifdef PNG_WRITE_FILTER_SUPPORTED
2131 row_buf = best_row; 2174 row_buf = best_row;
2132 mins = PNG_MAXSUM; 2175 mins = PNG_MAXSUM;
2133 2176
2134 /* The prediction method we use is to find which method provides the 2177 /* The prediction method we use is to find which method provides the
2135 * smallest value when summing the absolute values of the distances 2178 * smallest value when summing the absolute values of the distances
2136 * from zero, using anything >= 128 as negative numbers. This is known 2179 * from zero, using anything >= 128 as negative numbers. This is known
2137 * as the "minimum sum of absolute differences" heuristic. Other 2180 * as the "minimum sum of absolute differences" heuristic. Other
2138 * heuristics are the "weighted minimum sum of absolute differences" 2181 * heuristics are the "weighted minimum sum of absolute differences"
2139 * (experimental and can in theory improve compression), and the "zlib 2182 * (experimental and can in theory improve compression), and the "zlib
2140 * predictive" method (not implemented yet), which does test compressions 2183 * predictive" method (not implemented yet), which does test compressions
(...skipping 22 matching lines...) Expand all
2163 png_uint_32 sum = 0; 2206 png_uint_32 sum = 0;
2164 png_uint_32 i; 2207 png_uint_32 i;
2165 int v; 2208 int v;
2166 2209
2167 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) 2210 for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
2168 { 2211 {
2169 v = *rp; 2212 v = *rp;
2170 sum += (v < 128) ? v : 256 - v; 2213 sum += (v < 128) ? v : 256 - v;
2171 } 2214 }
2172 2215
2173 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2216 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2174 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) 2217 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2175 { 2218 {
2176 png_uint_32 sumhi, sumlo; 2219 png_uint_32 sumhi, sumlo;
2177 int j; 2220 int j;
2178 sumlo = sum & PNG_LOMASK; 2221 sumlo = sum & PNG_LOMASK;
2179 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */ 2222 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2180 2223
2181 /* Reduce the sum if we match any of the previous rows */ 2224 /* Reduce the sum if we match any of the previous rows */
2182 for (j = 0; j < num_p_filters; j++) 2225 for (j = 0; j < num_p_filters; j++)
2183 { 2226 {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2227 best_row = png_ptr->sub_row; 2270 best_row = png_ptr->sub_row;
2228 } 2271 }
2229 2272
2230 else if (filter_to_do & PNG_FILTER_SUB) 2273 else if (filter_to_do & PNG_FILTER_SUB)
2231 { 2274 {
2232 png_bytep rp, dp, lp; 2275 png_bytep rp, dp, lp;
2233 png_uint_32 sum = 0, lmins = mins; 2276 png_uint_32 sum = 0, lmins = mins;
2234 png_uint_32 i; 2277 png_uint_32 i;
2235 int v; 2278 int v;
2236 2279
2237 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2280 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2238 /* We temporarily increase the "minimum sum" by the factor we 2281 /* We temporarily increase the "minimum sum" by the factor we
2239 * would reduce the sum of this filter, so that we can do the 2282 * would reduce the sum of this filter, so that we can do the
2240 * early exit comparison without scaling the sum each time. 2283 * early exit comparison without scaling the sum each time.
2241 */ 2284 */
2242 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) 2285 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2243 { 2286 {
2244 int j; 2287 int j;
2245 png_uint_32 lmhi, lmlo; 2288 png_uint_32 lmhi, lmlo;
2246 lmlo = lmins & PNG_LOMASK; 2289 lmlo = lmins & PNG_LOMASK;
2247 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; 2290 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2280 i++, rp++, lp++, dp++) 2323 i++, rp++, lp++, dp++)
2281 { 2324 {
2282 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); 2325 v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2283 2326
2284 sum += (v < 128) ? v : 256 - v; 2327 sum += (v < 128) ? v : 256 - v;
2285 2328
2286 if (sum > lmins) /* We are already worse, don't continue. */ 2329 if (sum > lmins) /* We are already worse, don't continue. */
2287 break; 2330 break;
2288 } 2331 }
2289 2332
2290 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2333 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2291 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) 2334 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2292 { 2335 {
2293 int j; 2336 int j;
2294 png_uint_32 sumhi, sumlo; 2337 png_uint_32 sumhi, sumlo;
2295 sumlo = sum & PNG_LOMASK; 2338 sumlo = sum & PNG_LOMASK;
2296 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; 2339 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2297 2340
2298 for (j = 0; j < num_p_filters; j++) 2341 for (j = 0; j < num_p_filters; j++)
2299 { 2342 {
2300 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB) 2343 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
2341 } 2384 }
2342 2385
2343 else if (filter_to_do & PNG_FILTER_UP) 2386 else if (filter_to_do & PNG_FILTER_UP)
2344 { 2387 {
2345 png_bytep rp, dp, pp; 2388 png_bytep rp, dp, pp;
2346 png_uint_32 sum = 0, lmins = mins; 2389 png_uint_32 sum = 0, lmins = mins;
2347 png_uint_32 i; 2390 png_uint_32 i;
2348 int v; 2391 int v;
2349 2392
2350 2393
2351 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2394 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2352 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) 2395 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2353 { 2396 {
2354 int j; 2397 int j;
2355 png_uint_32 lmhi, lmlo; 2398 png_uint_32 lmhi, lmlo;
2356 lmlo = lmins & PNG_LOMASK; 2399 lmlo = lmins & PNG_LOMASK;
2357 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; 2400 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2358 2401
2359 for (j = 0; j < num_p_filters; j++) 2402 for (j = 0; j < num_p_filters; j++)
2360 { 2403 {
2361 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) 2404 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
(...skipping 21 matching lines...) Expand all
2383 pp = prev_row + 1; i < row_bytes; i++) 2426 pp = prev_row + 1; i < row_bytes; i++)
2384 { 2427 {
2385 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); 2428 v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2386 2429
2387 sum += (v < 128) ? v : 256 - v; 2430 sum += (v < 128) ? v : 256 - v;
2388 2431
2389 if (sum > lmins) /* We are already worse, don't continue. */ 2432 if (sum > lmins) /* We are already worse, don't continue. */
2390 break; 2433 break;
2391 } 2434 }
2392 2435
2393 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2436 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2394 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) 2437 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2395 { 2438 {
2396 int j; 2439 int j;
2397 png_uint_32 sumhi, sumlo; 2440 png_uint_32 sumhi, sumlo;
2398 sumlo = sum & PNG_LOMASK; 2441 sumlo = sum & PNG_LOMASK;
2399 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; 2442 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2400 2443
2401 for (j = 0; j < num_p_filters; j++) 2444 for (j = 0; j < num_p_filters; j++)
2402 { 2445 {
2403 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP) 2446 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
2446 best_row = png_ptr->avg_row; 2489 best_row = png_ptr->avg_row;
2447 } 2490 }
2448 2491
2449 else if (filter_to_do & PNG_FILTER_AVG) 2492 else if (filter_to_do & PNG_FILTER_AVG)
2450 { 2493 {
2451 png_bytep rp, dp, pp, lp; 2494 png_bytep rp, dp, pp, lp;
2452 png_uint_32 sum = 0, lmins = mins; 2495 png_uint_32 sum = 0, lmins = mins;
2453 png_uint_32 i; 2496 png_uint_32 i;
2454 int v; 2497 int v;
2455 2498
2456 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2499 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2457 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) 2500 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2458 { 2501 {
2459 int j; 2502 int j;
2460 png_uint_32 lmhi, lmlo; 2503 png_uint_32 lmhi, lmlo;
2461 lmlo = lmins & PNG_LOMASK; 2504 lmlo = lmins & PNG_LOMASK;
2462 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; 2505 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2463 2506
2464 for (j = 0; j < num_p_filters; j++) 2507 for (j = 0; j < num_p_filters; j++)
2465 { 2508 {
2466 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG) 2509 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
(...skipping 28 matching lines...) Expand all
2495 { 2538 {
2496 v = *dp++ = 2539 v = *dp++ =
2497 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff); 2540 (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
2498 2541
2499 sum += (v < 128) ? v : 256 - v; 2542 sum += (v < 128) ? v : 256 - v;
2500 2543
2501 if (sum > lmins) /* We are already worse, don't continue. */ 2544 if (sum > lmins) /* We are already worse, don't continue. */
2502 break; 2545 break;
2503 } 2546 }
2504 2547
2505 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2548 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2506 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) 2549 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2507 { 2550 {
2508 int j; 2551 int j;
2509 png_uint_32 sumhi, sumlo; 2552 png_uint_32 sumhi, sumlo;
2510 sumlo = sum & PNG_LOMASK; 2553 sumlo = sum & PNG_LOMASK;
2511 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; 2554 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2512 2555
2513 for (j = 0; j < num_p_filters; j++) 2556 for (j = 0; j < num_p_filters; j++)
2514 { 2557 {
2515 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE) 2558 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
2579 best_row = png_ptr->paeth_row; 2622 best_row = png_ptr->paeth_row;
2580 } 2623 }
2581 2624
2582 else if (filter_to_do & PNG_FILTER_PAETH) 2625 else if (filter_to_do & PNG_FILTER_PAETH)
2583 { 2626 {
2584 png_bytep rp, dp, pp, cp, lp; 2627 png_bytep rp, dp, pp, cp, lp;
2585 png_uint_32 sum = 0, lmins = mins; 2628 png_uint_32 sum = 0, lmins = mins;
2586 png_uint_32 i; 2629 png_uint_32 i;
2587 int v; 2630 int v;
2588 2631
2589 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2632 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2590 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) 2633 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2591 { 2634 {
2592 int j; 2635 int j;
2593 png_uint_32 lmhi, lmlo; 2636 png_uint_32 lmhi, lmlo;
2594 lmlo = lmins & PNG_LOMASK; 2637 lmlo = lmins & PNG_LOMASK;
2595 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK; 2638 lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2596 2639
2597 for (j = 0; j < num_p_filters; j++) 2640 for (j = 0; j < num_p_filters; j++)
2598 { 2641 {
2599 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) 2642 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
2660 #endif /* PNG_SLOW_PAETH */ 2703 #endif /* PNG_SLOW_PAETH */
2661 2704
2662 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); 2705 v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2663 2706
2664 sum += (v < 128) ? v : 256 - v; 2707 sum += (v < 128) ? v : 256 - v;
2665 2708
2666 if (sum > lmins) /* We are already worse, don't continue. */ 2709 if (sum > lmins) /* We are already worse, don't continue. */
2667 break; 2710 break;
2668 } 2711 }
2669 2712
2670 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2713 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2671 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED) 2714 if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2672 { 2715 {
2673 int j; 2716 int j;
2674 png_uint_32 sumhi, sumlo; 2717 png_uint_32 sumhi, sumlo;
2675 sumlo = sum & PNG_LOMASK; 2718 sumlo = sum & PNG_LOMASK;
2676 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; 2719 sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2677 2720
2678 for (j = 0; j < num_p_filters; j++) 2721 for (j = 0; j < num_p_filters; j++)
2679 { 2722 {
2680 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH) 2723 if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
(...skipping 15 matching lines...) Expand all
2696 else 2739 else
2697 sum = (sumhi << PNG_HISHIFT) + sumlo; 2740 sum = (sumhi << PNG_HISHIFT) + sumlo;
2698 } 2741 }
2699 #endif 2742 #endif
2700 2743
2701 if (sum < mins) 2744 if (sum < mins)
2702 { 2745 {
2703 best_row = png_ptr->paeth_row; 2746 best_row = png_ptr->paeth_row;
2704 } 2747 }
2705 } 2748 }
2706 #endif /* PNG_NO_WRITE_FILTER */ 2749 #endif /* PNG_WRITE_FILTER_SUPPORTED */
2707 /* Do the actual writing of the filtered row data from the chosen filter. */ 2750 /* Do the actual writing of the filtered row data from the chosen filter. */
2708 2751
2709 png_write_filtered_row(png_ptr, best_row); 2752 png_write_filtered_row(png_ptr, best_row);
2710 2753
2711 #ifndef PNG_NO_WRITE_FILTER 2754 #ifdef PNG_WRITE_FILTER_SUPPORTED
2712 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) 2755 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2713 /* Save the type of filter we picked this time for future calculations */ 2756 /* Save the type of filter we picked this time for future calculations */
2714 if (png_ptr->num_prev_filters > 0) 2757 if (png_ptr->num_prev_filters > 0)
2715 { 2758 {
2716 int j; 2759 int j;
2717 for (j = 1; j < num_p_filters; j++) 2760 for (j = 1; j < num_p_filters; j++)
2718 { 2761 {
2719 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1]; 2762 png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
2720 } 2763 }
2721 png_ptr->prev_filters[j] = best_row[0]; 2764 png_ptr->prev_filters[j] = best_row[0];
2722 } 2765 }
2723 #endif 2766 #endif
2724 #endif /* PNG_NO_WRITE_FILTER */ 2767 #endif /* PNG_WRITE_FILTER_SUPPORTED */
2725 } 2768 }
2726 2769
2727 2770
2728 /* Do the actual writing of a previously filtered row. */ 2771 /* Do the actual writing of a previously filtered row. */
2729 void /* PRIVATE */ 2772 void /* PRIVATE */
2730 png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row) 2773 png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
2731 { 2774 {
2732 png_debug(1, "in png_write_filtered_row"); 2775 png_debug(1, "in png_write_filtered_row");
2776
2733 png_debug1(2, "filter = %d", filtered_row[0]); 2777 png_debug1(2, "filter = %d", filtered_row[0]);
2734 /* Set up the zlib input buffer */ 2778 /* Set up the zlib input buffer */
2735 2779
2736 png_ptr->zstream.next_in = filtered_row; 2780 png_ptr->zstream.next_in = filtered_row;
2737 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1; 2781 png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
2738 /* Repeat until we have compressed all the data */ 2782 /* Repeat until we have compressed all the data */
2739 do 2783 do
2740 { 2784 {
2741 int ret; /* Return of zlib */ 2785 int ret; /* Return of zlib */
2742 2786
(...skipping 25 matching lines...) Expand all
2768 png_bytep tptr; 2812 png_bytep tptr;
2769 2813
2770 tptr = png_ptr->prev_row; 2814 tptr = png_ptr->prev_row;
2771 png_ptr->prev_row = png_ptr->row_buf; 2815 png_ptr->prev_row = png_ptr->row_buf;
2772 png_ptr->row_buf = tptr; 2816 png_ptr->row_buf = tptr;
2773 } 2817 }
2774 2818
2775 /* Finish row - updates counters and flushes zlib if last row */ 2819 /* Finish row - updates counters and flushes zlib if last row */
2776 png_write_finish_row(png_ptr); 2820 png_write_finish_row(png_ptr);
2777 2821
2778 #if defined(PNG_WRITE_FLUSH_SUPPORTED) 2822 #ifdef PNG_WRITE_FLUSH_SUPPORTED
2779 png_ptr->flush_rows++; 2823 png_ptr->flush_rows++;
2780 2824
2781 if (png_ptr->flush_dist > 0 && 2825 if (png_ptr->flush_dist > 0 &&
2782 png_ptr->flush_rows >= png_ptr->flush_dist) 2826 png_ptr->flush_rows >= png_ptr->flush_dist)
2783 { 2827 {
2784 png_write_flush(png_ptr); 2828 png_write_flush(png_ptr);
2785 } 2829 }
2786 #endif 2830 #endif
2787 } 2831 }
2788 #endif /* PNG_WRITE_SUPPORTED */ 2832 #endif /* PNG_WRITE_SUPPORTED */
OLDNEW
« third_party/libpng/pngrutil.c ('K') | « third_party/libpng/pngwtran.c ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698