blob: d0afd9a99c823603df3bdd128f472cbd6d970826 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
2/* png.c - location for general purpose png functions
3
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004 libpng 1.0 beta 2 - version 0.81
Guy Schalnat0d580581995-07-20 02:43:20 -05005 For conditions of distribution and use, see copyright notice in png.h
6 Copyright (c) 1995 Guy Eric Schalnat, Group 42, Inc.
Guy Schalnat51f0eb41995-09-26 05:22:39 -05007 August 24, 1995
Guy Schalnat0d580581995-07-20 02:43:20 -05008 */
9
10#define PNG_INTERNAL
11#define PNG_NO_EXTERN
12#include "png.h"
13
Guy Schalnat51f0eb41995-09-26 05:22:39 -050014/* version information for c files. This better match the version
15 string defined in png.h */
16char png_libpng_ver[] = "0.81";
17
Guy Schalnat0d580581995-07-20 02:43:20 -050018/* place to hold the signiture string for a png file. */
19png_byte png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
20
21/* constant strings for known chunk types. If you need to add a chunk,
22 add a string holding the name here. If you want to make the code
23 portable to EBCDIC machines, use ASCII numbers, not characters. */
24png_byte png_IHDR[4] = { 73, 72, 68, 82};
25png_byte png_IDAT[4] = { 73, 68, 65, 84};
26png_byte png_IEND[4] = { 73, 69, 78, 68};
27png_byte png_PLTE[4] = { 80, 76, 84, 69};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050028#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050029png_byte png_gAMA[4] = {103, 65, 77, 65};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050030#endif
31#if defined(PNG_READ_sBIT_SUPPORTED) || defined(PNG_WRITE_sBIT_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050032png_byte png_sBIT[4] = {115, 66, 73, 84};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050033#endif
34#if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050035png_byte png_cHRM[4] = { 99, 72, 82, 77};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050036#endif
37#if defined(PNG_READ_tRNS_SUPPORTED) || defined(PNG_WRITE_tRNS_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050038png_byte png_tRNS[4] = {116, 82, 78, 83};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050039#endif
40#if defined(PNG_READ_bKGD_SUPPORTED) || defined(PNG_WRITE_bKGD_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050041png_byte png_bKGD[4] = { 98, 75, 71, 68};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050042#endif
43#if defined(PNG_READ_hIST_SUPPORTED) || defined(PNG_WRITE_hIST_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050044png_byte png_hIST[4] = {104, 73, 83, 84};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050045#endif
46#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050047png_byte png_tEXt[4] = {116, 69, 88, 116};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050048#endif
49#if defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050050png_byte png_zTXt[4] = {122, 84, 88, 116};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050051#endif
52#if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050053png_byte png_pHYs[4] = {112, 72, 89, 115};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050054#endif
55#if defined(PNG_READ_oFFs_SUPPORTED) || defined(PNG_WRITE_oFFs_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050056png_byte png_oFFs[4] = {111, 70, 70, 115};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050057#endif
58#if defined(PNG_READ_tIME_SUPPORTED) || defined(PNG_WRITE_tIME_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -050059png_byte png_tIME[4] = {116, 73, 77, 69};
Guy Schalnat51f0eb41995-09-26 05:22:39 -050060#endif
Guy Schalnat0d580581995-07-20 02:43:20 -050061
62/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
63
64/* start of interlace block */
65int png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
66
67/* offset to next interlace block */
68int png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
69
70/* start of interlace block in the y direction */
71int png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
72
73/* offset to next interlace block in the y direction */
74int png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
75
76/* width of interlace block */
77/* this is not currently used - if you need it, uncomment it here and
78 in png.h
79int png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
80*/
81
82/* height of interlace block */
83/* this is not currently used - if you need it, uncomment it here and
84 in png.h
85int png_pass_height[] = {8, 8, 4, 4, 4, 2, 2, 1};
86*/
87
88/* mask to determine which pixels are valid in a pass */
89int png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
90
91/* mask to determine which pixels to overwrite while displaying */
92int png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
93
94
95int
96png_check_sig(png_byte *sig, int num)
97{
98 if (num > 8)
99 num = 8;
100 if (num < 1)
101 return 0;
102
103 return (!memcmp(sig, png_sig, num));
104}
105
106/* Function to allocate memory for zlib. */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500107voidpf
108png_zalloc(voidpf png_ptr, uInt items, uInt size)
Guy Schalnat0d580581995-07-20 02:43:20 -0500109{
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500110 return ((voidpf)png_large_malloc((png_struct FAR *)png_ptr,
Guy Schalnat0d580581995-07-20 02:43:20 -0500111 (png_uint_32)items * (png_uint_32)size));
112}
113
114/* function to free memory for zlib */
115void
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500116png_zfree(voidpf png_ptr, voidpf ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500117{
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500118 png_large_free((png_struct FAR *)png_ptr, (voidpf)ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500119}
120
121/* reset the crc variable to 32 bits of 1's. Care must be taken
122 in case crc is > 32 bits to leave the top bits 0 */
123void
124png_reset_crc(png_struct *png_ptr)
125{
126 /* set crc to all 1's */
127 png_ptr->crc = 0xffffffffL;
128}
129
130/* Note: the crc code below was copied from the sample code in the
131 PNG spec, with appropriate modifications made to ensure the
132 variables are large enough */
133
134/* table of crc's of all 8-bit messages. If you wish to png_malloc this
135 table, turn this into a pointer, and png_malloc it in make_crc_table().
136 You may then want to hook it into png_struct and free it with the
137 destroy functions. */
138static png_uint_32 crc_table[256];
139
140/* Flag: has the table been computed? Initially false. */
141static int crc_table_computed = 0;
142
143/* make the table for a fast crc */
144static void
145make_crc_table(void)
146{
147 png_uint_32 c;
148 int n, k;
149
150 for (n = 0; n < 256; n++)
151 {
152 c = (png_uint_32)n;
153 for (k = 0; k < 8; k++)
154 c = c & 1 ? 0xedb88320L ^ (c >> 1) : c >> 1;
155 crc_table[n] = c;
156 }
157 crc_table_computed = 1;
158}
159
160/* update a running crc with the bytes buf[0..len-1]--the crc should be
161 initialized to all 1's, and the transmitted value is the 1's complement
162 of the final running crc. */
163static png_uint_32
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500164update_crc(png_uint_32 crc, png_bytef *buf, png_uint_32 len)
Guy Schalnat0d580581995-07-20 02:43:20 -0500165{
166 png_uint_32 c;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500167 png_bytef *p;
Guy Schalnat0d580581995-07-20 02:43:20 -0500168 png_uint_32 n;
169
170 c = crc;
171 p = buf;
172 n = len;
173
174 if (!crc_table_computed)
175 {
176 make_crc_table();
177 }
178
179 if (n > 0) do
180 {
181 c = crc_table[(png_byte)((c ^ (*p++)) & 0xff)] ^ (c >> 8);
182 } while (--n);
183
184 return c;
185}
186
187/* calculate the crc over a section of data. Note that while we
188 are passing in a 32 bit value for length, on 16 bit machines, you
189 would need to use huge pointers to access all that data. If you
190 need this, put huge here and above. */
191void
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500192png_calculate_crc(png_struct *png_ptr, png_bytef *ptr,
Guy Schalnat0d580581995-07-20 02:43:20 -0500193 png_uint_32 length)
194{
195 png_ptr->crc = update_crc(png_ptr->crc, ptr, length);
196}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500197void
198png_info_init(png_info *info)
199{
200 /* set everything to 0 */
201 memset(info, 0, sizeof (png_info));
202}
Guy Schalnat0d580581995-07-20 02:43:20 -0500203