~ubuntu-branches/ubuntu/hardy/ghostscript/hardy

« back to all changes in this revision

Viewing changes to libpng/pngwutil.c

  • Committer: Bazaar Package Importer
  • Author(s): Till Kamppeter
  • Date: 2007-11-22 12:17:43 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20071122121743-cd70s3ypq0r243mp
Tags: 8.61.dfsg.1-0ubtuntu1
* New upstream release
  o Final 8.61 release
* debian/patches/09_ijs_krgb_support.dpatch: Adapted to upstream changes.
* debian/rules: Updated CUPS-related variables for "make install" calls.
* debian/rules: Remove /usr/include/ghostscript from the ghostscript
  package, they go into lings-dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/* pngwutil.c - utilities to write a PNG file
3
3
 *
4
 
 * Last changed in libpng 1.2.15 January 5, 2007
 
4
 * Last changed in libpng 1.2.20 Septhember 3, 2007
5
5
 * For conditions of distribution and use, see copyright notice in png.h
6
6
 * Copyright (c) 1998-2007 Glenn Randers-Pehrson
7
7
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
182
182
   {
183
183
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
184
184
      char msg[50];
185
 
      sprintf(msg, "Unknown compression type %d", compression);
 
185
      png_snprintf(msg, 50, "Unknown compression type %d", compression);
186
186
      png_warning(png_ptr, msg);
187
187
#else
188
188
      png_warning(png_ptr, "Unknown compression type");
494
494
   buf[12] = (png_byte)interlace_type;
495
495
 
496
496
   /* write the chunk */
497
 
   png_write_chunk(png_ptr, (png_bytep)png_IHDR, buf, (png_size_t)13);
 
497
   png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
498
498
 
499
499
   /* initialize zlib with PNG info */
500
500
   png_ptr->zstream.zalloc = png_zalloc;
578
578
   png_ptr->num_palette = (png_uint_16)num_pal;
579
579
   png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
580
580
 
581
 
   png_write_chunk_start(png_ptr, (png_bytep)png_PLTE, num_pal * 3);
 
581
   png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3);
582
582
#ifndef PNG_NO_POINTER_INDEXING
583
583
   for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
584
584
   {
650
650
            "Invalid zlib compression method or flags in IDAT");
651
651
   }
652
652
 
653
 
   png_write_chunk(png_ptr, (png_bytep)png_IDAT, data, length);
 
653
   png_write_chunk(png_ptr, png_IDAT, data, length);
654
654
   png_ptr->mode |= PNG_HAVE_IDAT;
655
655
}
656
656
 
662
662
   PNG_IEND;
663
663
#endif
664
664
   png_debug(1, "in png_write_IEND\n");
665
 
   png_write_chunk(png_ptr, (png_bytep)png_IEND, png_bytep_NULL,
 
665
   png_write_chunk(png_ptr, png_IEND, png_bytep_NULL,
666
666
     (png_size_t)0);
667
667
   png_ptr->mode |= PNG_HAVE_IEND;
668
668
}
683
683
   /* file_gamma is saved in 1/100,000ths */
684
684
   igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
685
685
   png_save_uint_32(buf, igamma);
686
 
   png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
 
686
   png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
687
687
}
688
688
#endif
689
689
#ifdef PNG_FIXED_POINT_SUPPORTED
698
698
   png_debug(1, "in png_write_gAMA\n");
699
699
   /* file_gamma is saved in 1/100,000ths */
700
700
   png_save_uint_32(buf, (png_uint_32)file_gamma);
701
 
   png_write_chunk(png_ptr, (png_bytep)png_gAMA, buf, (png_size_t)4);
 
701
   png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
702
702
}
703
703
#endif
704
704
#endif
718
718
         png_warning(png_ptr,
719
719
            "Invalid sRGB rendering intent specified");
720
720
   buf[0]=(png_byte)srgb_intent;
721
 
   png_write_chunk(png_ptr, (png_bytep)png_sRGB, buf, (png_size_t)1);
 
721
   png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
722
722
}
723
723
#endif
724
724
 
783
783
          PNG_COMPRESSION_TYPE_BASE, &comp);
784
784
 
785
785
   /* make sure we include the NULL after the name and the compression type */
786
 
   png_write_chunk_start(png_ptr, (png_bytep)png_iCCP,
 
786
   png_write_chunk_start(png_ptr, png_iCCP,
787
787
          (png_uint_32)name_len+profile_len+2);
788
788
   new_name[name_len+1]=0x00;
789
789
   png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
823
823
   }
824
824
 
825
825
   /* make sure we include the NULL after the name */
826
 
   png_write_chunk_start(png_ptr, (png_bytep)png_sPLT,
 
826
   png_write_chunk_start(png_ptr, png_sPLT,
827
827
          (png_uint_32)(name_len + 2 + palette_size));
828
828
   png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
829
829
   png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
931
931
      buf[size++] = sbit->alpha;
932
932
   }
933
933
 
934
 
   png_write_chunk(png_ptr, (png_bytep)png_sBIT, buf, size);
 
934
   png_write_chunk(png_ptr, png_sBIT, buf, size);
935
935
}
936
936
#endif
937
937
 
995
995
   itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
996
996
   png_save_uint_32(buf + 28, itemp);
997
997
 
998
 
   png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
 
998
   png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
999
999
}
1000
1000
#endif
1001
1001
#ifdef PNG_FIXED_POINT_SUPPORTED
1047
1047
   png_save_uint_32(buf + 24, (png_uint_32)blue_x);
1048
1048
   png_save_uint_32(buf + 28, (png_uint_32)blue_y);
1049
1049
 
1050
 
   png_write_chunk(png_ptr, (png_bytep)png_cHRM, buf, (png_size_t)32);
 
1050
   png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
1051
1051
}
1052
1052
#endif
1053
1053
#endif
1072
1072
         return;
1073
1073
      }
1074
1074
      /* write the chunk out as it is */
1075
 
      png_write_chunk(png_ptr, (png_bytep)png_tRNS, trans, (png_size_t)num_trans);
 
1075
      png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans);
1076
1076
   }
1077
1077
   else if (color_type == PNG_COLOR_TYPE_GRAY)
1078
1078
   {
1084
1084
         return;
1085
1085
      }
1086
1086
      png_save_uint_16(buf, tran->gray);
1087
 
      png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)2);
 
1087
      png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
1088
1088
   }
1089
1089
   else if (color_type == PNG_COLOR_TYPE_RGB)
1090
1090
   {
1098
1098
              "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1099
1099
            return;
1100
1100
         }
1101
 
      png_write_chunk(png_ptr, (png_bytep)png_tRNS, buf, (png_size_t)6);
 
1101
      png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
1102
1102
   }
1103
1103
   else
1104
1104
   {
1131
1131
         return;
1132
1132
      }
1133
1133
      buf[0] = back->index;
1134
 
      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)1);
 
1134
      png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
1135
1135
   }
1136
1136
   else if (color_type & PNG_COLOR_MASK_COLOR)
1137
1137
   {
1144
1144
              "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1145
1145
            return;
1146
1146
         }
1147
 
      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)6);
 
1147
      png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
1148
1148
   }
1149
1149
   else
1150
1150
   {
1155
1155
         return;
1156
1156
      }
1157
1157
      png_save_uint_16(buf, back->gray);
1158
 
      png_write_chunk(png_ptr, (png_bytep)png_bKGD, buf, (png_size_t)2);
 
1158
      png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
1159
1159
   }
1160
1160
}
1161
1161
#endif
1180
1180
      return;
1181
1181
   }
1182
1182
 
1183
 
   png_write_chunk_start(png_ptr, (png_bytep)png_hIST, (png_uint_32)(num_hist * 2));
 
1183
   png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
1184
1184
   for (i = 0; i < num_hist; i++)
1185
1185
   {
1186
1186
      png_save_uint_16(buf, hist[i]);
1231
1231
   /* Replace non-printing characters with a blank and print a warning */
1232
1232
   for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
1233
1233
   {
1234
 
      if (*kp < 0x20 || (*kp > 0x7E && (png_byte)*kp < 0xA1))
 
1234
      if ((png_byte)*kp < 0x20 ||
 
1235
         ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
1235
1236
      {
1236
1237
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
1237
1238
         char msg[40];
1238
1239
 
1239
 
         sprintf(msg, "invalid keyword character 0x%02X", *kp);
 
1240
         png_snprintf(msg, 40,
 
1241
           "invalid keyword character 0x%02X", (png_byte)*kp);
1240
1242
         png_warning(png_ptr, msg);
1241
1243
#else
1242
1244
         png_warning(png_ptr, "invalid character in keyword");
1344
1346
      text_len = png_strlen(text);
1345
1347
 
1346
1348
   /* make sure we include the 0 after the key */
1347
 
   png_write_chunk_start(png_ptr, (png_bytep)png_tEXt, (png_uint_32)key_len+text_len+1);
 
1349
   png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1);
1348
1350
   /*
1349
1351
    * We leave it to the application to meet PNG-1.0 requirements on the
1350
1352
    * contents of the text.  PNG-1.0 through PNG-1.2 discourage the use of
1397
1399
 
1398
1400
   text_len = png_strlen(text);
1399
1401
 
1400
 
   png_free(png_ptr, new_key);
1401
 
 
1402
1402
   /* compute the compressed data; do it now for the length */
1403
1403
   text_len = png_text_compress(png_ptr, text, text_len, compression,
1404
1404
       &comp);
1405
1405
 
1406
1406
   /* write start of chunk */
1407
 
   png_write_chunk_start(png_ptr, (png_bytep)png_zTXt, (png_uint_32)
 
1407
   png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)
1408
1408
      (key_len+text_len+2));
1409
1409
   /* write key */
1410
 
   png_write_chunk_data(png_ptr, (png_bytep)key, key_len + 1);
 
1410
   png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
 
1411
   png_free(png_ptr, new_key);
 
1412
 
1411
1413
   buf[0] = (png_byte)compression;
1412
1414
   /* write compression */
1413
1415
   png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
1470
1472
   /* make sure we include the compression flag, the compression byte,
1471
1473
    * and the NULs after the key, lang, and lang_key parts */
1472
1474
 
1473
 
   png_write_chunk_start(png_ptr, (png_bytep)png_iTXt,
 
1475
   png_write_chunk_start(png_ptr, png_iTXt,
1474
1476
          (png_uint_32)(
1475
1477
        5 /* comp byte, comp flag, terminators for key, lang and lang_key */
1476
1478
        + key_len
1527
1529
   png_save_int_32(buf + 4, y_offset);
1528
1530
   buf[8] = (png_byte)unit_type;
1529
1531
 
1530
 
   png_write_chunk(png_ptr, (png_bytep)png_oFFs, buf, (png_size_t)9);
 
1532
   png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
1531
1533
}
1532
1534
#endif
1533
 
 
1534
1535
#if defined(PNG_WRITE_pCAL_SUPPORTED)
1535
1536
/* write the pCAL chunk (described in the PNG extensions document) */
1536
1537
void /* PRIVATE */
1569
1570
   }
1570
1571
 
1571
1572
   png_debug1(3, "pCAL total length = %d\n", (int)total_len);
1572
 
   png_write_chunk_start(png_ptr, (png_bytep)png_pCAL, (png_uint_32)total_len);
 
1573
   png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
1573
1574
   png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
1574
1575
   png_save_int_32(buf, X0);
1575
1576
   png_save_int_32(buf + 4, X1);
1622
1623
      total_len += wc_len;
1623
1624
   }
1624
1625
#else
1625
 
   sprintf(buf + 1, "%12.12e", width);
 
1626
   png_snprintf(buf + 1, 63, "%12.12e", width);
1626
1627
   total_len = 1 + png_strlen(buf + 1) + 1;
1627
 
   sprintf(buf + total_len, "%12.12e", height);
 
1628
   png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
1628
1629
   total_len += png_strlen(buf + total_len);
1629
1630
#endif
1630
1631
 
1631
1632
   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1632
 
   png_write_chunk(png_ptr, (png_bytep)png_sCAL, (png_bytep)buf, total_len);
 
1633
   png_write_chunk(png_ptr, png_sCAL, (png_bytep)buf, total_len);
1633
1634
}
1634
1635
#else
1635
1636
#ifdef PNG_FIXED_POINT_SUPPORTED
1659
1660
   png_memcpy(buf + wlen + 2, height, hlen);  /* do NOT append the '\0' here */
1660
1661
 
1661
1662
   png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
1662
 
   png_write_chunk(png_ptr, (png_bytep)png_sCAL, buf, total_len);
 
1663
   png_write_chunk(png_ptr, png_sCAL, buf, total_len);
1663
1664
}
1664
1665
#endif
1665
1666
#endif
1685
1686
   png_save_uint_32(buf + 4, y_pixels_per_unit);
1686
1687
   buf[8] = (png_byte)unit_type;
1687
1688
 
1688
 
   png_write_chunk(png_ptr, (png_bytep)png_pHYs, buf, (png_size_t)9);
 
1689
   png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
1689
1690
}
1690
1691
#endif
1691
1692
 
1717
1718
   buf[5] = mod_time->minute;
1718
1719
   buf[6] = mod_time->second;
1719
1720
 
1720
 
   png_write_chunk(png_ptr, (png_bytep)png_tIME, buf, (png_size_t)7);
 
1721
   png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
1721
1722
}
1722
1723
#endif
1723
1724
 
1725
1726
void /* PRIVATE */
1726
1727
png_write_start_row(png_structp png_ptr)
1727
1728
{
 
1729
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
1728
1730
#ifdef PNG_USE_LOCAL_ARRAYS
1729
1731
   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1730
1732
 
1740
1742
   /* offset to next interlace block in the y direction */
1741
1743
   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1742
1744
#endif
 
1745
#endif
1743
1746
 
1744
1747
   png_size_t buf_size;
1745
1748
 
1751
1754
   png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
1752
1755
   png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
1753
1756
 
 
1757
#ifndef PNG_NO_WRITE_FILTERING
1754
1758
   /* set up filtering buffer, if using this filter */
1755
1759
   if (png_ptr->do_filter & PNG_FILTER_SUB)
1756
1760
   {
1768
1772
 
1769
1773
      if (png_ptr->do_filter & PNG_FILTER_UP)
1770
1774
      {
1771
 
         png_ptr->up_row = (png_bytep )png_malloc(png_ptr,
 
1775
         png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1772
1776
            (png_ptr->rowbytes + 1));
1773
1777
         png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1774
1778
      }
1782
1786
 
1783
1787
      if (png_ptr->do_filter & PNG_FILTER_PAETH)
1784
1788
      {
1785
 
         png_ptr->paeth_row = (png_bytep )png_malloc(png_ptr,
 
1789
         png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
1786
1790
            (png_ptr->rowbytes + 1));
1787
1791
         png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1788
1792
      }
 
1793
#endif /* PNG_NO_WRITE_FILTERING */
1789
1794
   }
1790
1795
 
1791
1796
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
1819
1824
void /* PRIVATE */
1820
1825
png_write_finish_row(png_structp png_ptr)
1821
1826
{
 
1827
#ifdef PNG_WRITE_INTERLACING_SUPPORTED
1822
1828
#ifdef PNG_USE_LOCAL_ARRAYS
1823
1829
   /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1824
1830
 
1834
1840
   /* offset to next interlace block in the y direction */
1835
1841
   int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1836
1842
#endif
 
1843
#endif
1837
1844
 
1838
1845
   int ret;
1839
1846
 
2107
2114
void /* PRIVATE */
2108
2115
png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
2109
2116
{
2110
 
   png_bytep prev_row, best_row, row_buf;
 
2117
   png_bytep best_row;
 
2118
#ifndef PNG_NO_WRITE_FILTER
 
2119
   png_bytep prev_row, row_buf;
2111
2120
   png_uint_32 mins, bpp;
2112
2121
   png_byte filter_to_do = png_ptr->do_filter;
2113
2122
   png_uint_32 row_bytes = row_info->rowbytes;
2120
2129
   bpp = (row_info->pixel_depth + 7) >> 3;
2121
2130
 
2122
2131
   prev_row = png_ptr->prev_row;
2123
 
   best_row = row_buf = png_ptr->row_buf;
 
2132
#endif
 
2133
   best_row = png_ptr->row_buf;
 
2134
#ifndef PNG_NO_WRITE_FILTER
 
2135
   row_buf = best_row;
2124
2136
   mins = PNG_MAXSUM;
2125
2137
 
2126
2138
   /* The prediction method we use is to find which method provides the
2695
2707
         best_row = png_ptr->paeth_row;
2696
2708
      }
2697
2709
   }
2698
 
 
 
2710
#endif /* PNG_NO_WRITE_FILTER */
2699
2711
   /* Do the actual writing of the filtered row data from the chosen filter. */
2700
2712
 
2701
2713
   png_write_filtered_row(png_ptr, best_row);
2702
2714
 
 
2715
#ifndef PNG_NO_WRITE_FILTER
2703
2716
#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
2704
2717
   /* Save the type of filter we picked this time for future calculations */
2705
2718
   if (png_ptr->num_prev_filters > 0)
2712
2725
      png_ptr->prev_filters[j] = best_row[0];
2713
2726
   }
2714
2727
#endif
 
2728
#endif /* PNG_NO_WRITE_FILTER */
2715
2729
}
2716
2730
 
2717
2731