Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2 | /* png.c - location for general purpose libpng functions |
| 3 | * |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame^] | 4 | * libpng version 1.0.5d - November 29, 1999 |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 5 | * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. |
| 6 | * Copyright (c) 1996, 1997 Andreas Dilger |
Glenn Randers-Pehrson | 87544ac | 1999-01-13 22:06:39 -0600 | [diff] [blame] | 7 | * Copyright (c) 1998, 1999 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 8 | * |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 9 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 10 | |
| 11 | #define PNG_INTERNAL |
| 12 | #define PNG_NO_EXTERN |
| 13 | #include "png.h" |
| 14 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 15 | /* Version information for C files. This had better match the version |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 16 | * string defined in png.h. |
| 17 | */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 18 | |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame^] | 19 | #ifdef PNG_USE_GLOBAL_ARRAYS |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 20 | /* png_libpng_ver was changed to a function in version 1.0.5c */ |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame^] | 21 | char png_libpng_ver[12] = "1.0.5d"; |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 22 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 23 | /* png_sig was changed to a function in version 1.0.5c */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 24 | /* Place to hold the signature string for a PNG file. */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 25 | png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10}; |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 26 | |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame^] | 27 | /* Invoke global declarations for constant strings for known chunk types */ |
| 28 | PNG_IHDR; |
| 29 | PNG_IDAT; |
| 30 | PNG_IEND; |
| 31 | PNG_PLTE; |
| 32 | PNG_bKGD; |
| 33 | PNG_cHRM; |
| 34 | PNG_gAMA; |
| 35 | PNG_hIST; |
| 36 | PNG_oFFs; |
| 37 | PNG_pCAL; |
| 38 | PNG_pHYs; |
| 39 | PNG_sBIT; |
| 40 | PNG_sRGB; |
| 41 | PNG_tEXt; |
| 42 | PNG_tIME; |
| 43 | PNG_tRNS; |
| 44 | PNG_zTXt; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 45 | |
| 46 | /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
| 47 | |
| 48 | /* start of interlace block */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 49 | int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 50 | |
| 51 | /* offset to next interlace block */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 52 | int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 53 | |
| 54 | /* start of interlace block in the y direction */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 55 | int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 56 | |
| 57 | /* offset to next interlace block in the y direction */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 58 | int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 59 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 60 | /* Width of interlace block. This is not currently used - if you need |
| 61 | * it, uncomment it here and in png.h |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 62 | int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1}; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 63 | */ |
| 64 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 65 | /* Height of interlace block. This is not currently used - if you need |
| 66 | * it, uncomment it here and in png.h |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 67 | int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 68 | */ |
| 69 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 70 | /* Mask to determine which pixels are valid in a pass */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 71 | int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 72 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 73 | /* Mask to determine which pixels to overwrite while displaying */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 74 | int FARDATA png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff}; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 75 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 76 | #endif |
| 77 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 78 | /* Tells libpng that we have already handled the first "num_bytes" bytes |
| 79 | * of the PNG file signature. If the PNG data is embedded into another |
| 80 | * stream we can set num_bytes = 8 so that libpng will not attempt to read |
| 81 | * or write any of the magic bytes before it starts on the IHDR. |
| 82 | */ |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 83 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 84 | void |
| 85 | png_set_sig_bytes(png_structp png_ptr, int num_bytes) |
| 86 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 87 | png_debug(1, "in png_set_sig_bytes\n"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 88 | if (num_bytes > 8) |
| 89 | png_error(png_ptr, "Too many bytes for PNG signature."); |
| 90 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 91 | png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* Checks whether the supplied bytes match the PNG signature. We allow |
| 95 | * checking less than the full 8-byte signature so that those apps that |
| 96 | * already read the first few bytes of a file to determine the file type |
| 97 | * can simply check the remaining bytes for extra assurance. Returns |
| 98 | * an integer less than, equal to, or greater than zero if sig is found, |
| 99 | * respectively, to be less than, to match, or be greater than the correct |
| 100 | * PNG signature (this is the same behaviour as strcmp, memcmp, etc). |
| 101 | */ |
| 102 | int |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 103 | png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 104 | { |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 105 | png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 106 | if (num_to_check > 8) |
| 107 | num_to_check = 8; |
| 108 | else if (num_to_check < 1) |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 109 | return (0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 110 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 111 | if (start > 7) |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 112 | return (0); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 113 | |
| 114 | if (start + num_to_check > 8) |
| 115 | num_to_check = 8 - start; |
| 116 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 117 | return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check))); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* (Obsolete) function to check signature bytes. It does not allow one |
Glenn Randers-Pehrson | c944229 | 1999-01-06 21:50:16 -0600 | [diff] [blame] | 121 | * to check a partial signature. This function might be removed in the |
| 122 | * future - use png_sig_cmp(). Returns true (nonzero) if the file is a PNG. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 123 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 124 | int |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 125 | png_check_sig(png_bytep sig, int num) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 126 | { |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 127 | return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* Function to allocate memory for zlib. */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 131 | voidpf |
| 132 | png_zalloc(voidpf png_ptr, uInt items, uInt size) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 133 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 134 | png_uint_32 num_bytes = (png_uint_32)items * size; |
| 135 | png_voidp ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 136 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 137 | if (num_bytes > (png_uint_32)0x8000L) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 138 | { |
| 139 | png_memset(ptr, 0, (png_size_t)0x8000L); |
| 140 | png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0, |
| 141 | (png_size_t)(num_bytes - (png_uint_32)0x8000L)); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | png_memset(ptr, 0, (png_size_t)num_bytes); |
| 146 | } |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 147 | return ((voidpf)ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /* function to free memory for zlib */ |
| 151 | void |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 152 | png_zfree(voidpf png_ptr, voidpf ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 153 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 154 | png_free((png_structp)png_ptr, (png_voidp)ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 155 | } |
| 156 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 157 | /* Reset the CRC variable to 32 bits of 1's. Care must be taken |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 158 | * in case CRC is > 32 bits to leave the top bits 0. |
| 159 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 160 | void |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 161 | png_reset_crc(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 162 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 163 | png_ptr->crc = crc32(0, Z_NULL, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 164 | } |
| 165 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 166 | /* Calculate the CRC over a section of data. We can only pass as |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 167 | * much data to this routine as the largest single buffer size. We |
| 168 | * also check that this data will actually be used before going to the |
| 169 | * trouble of calculating it. |
| 170 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 171 | void |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 172 | png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 173 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 174 | int need_crc = 1; |
| 175 | |
| 176 | if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ |
| 177 | { |
| 178 | if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == |
| 179 | (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 180 | need_crc = 0; |
| 181 | } |
| 182 | else /* critical */ |
| 183 | { |
| 184 | if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) |
| 185 | need_crc = 0; |
| 186 | } |
| 187 | |
| 188 | if (need_crc) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 189 | png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 190 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 191 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 192 | /* Allocate the memory for an info_struct for the application. We don't |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 193 | * really need the png_ptr, but it could potentially be useful in the |
| 194 | * future. This should be used in favour of malloc(sizeof(png_info)) |
| 195 | * and png_info_init() so that applications that want to use a shared |
| 196 | * libpng don't have to be recompiled if png_info changes size. |
| 197 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 198 | png_infop |
| 199 | png_create_info_struct(png_structp png_ptr) |
| 200 | { |
| 201 | png_infop info_ptr; |
| 202 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 203 | png_debug(1, "in png_create_info_struct\n"); |
Glenn Randers-Pehrson | a357b99 | 1998-02-08 20:56:40 -0600 | [diff] [blame] | 204 | if(png_ptr == NULL) return (NULL); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 205 | #ifdef PNG_USER_MEM_SUPPORTED |
| 206 | if ((info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO, |
| 207 | png_ptr->malloc_fn)) != NULL) |
| 208 | #else |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 209 | if ((info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO)) != NULL) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 210 | #endif |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 211 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 212 | png_info_init(info_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 213 | } |
| 214 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 215 | return (info_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 218 | /* This function frees the memory associated with a single info struct. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 219 | * Normally, one would use either png_destroy_read_struct() or |
| 220 | * png_destroy_write_struct() to free an info struct, but this may be |
| 221 | * useful for some applications. |
| 222 | */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 223 | void |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 224 | png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr) |
| 225 | { |
| 226 | png_infop info_ptr = NULL; |
| 227 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 228 | png_debug(1, "in png_destroy_info_struct\n"); |
| 229 | if (info_ptr_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 230 | info_ptr = *info_ptr_ptr; |
| 231 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 232 | if (info_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 233 | { |
| 234 | png_info_destroy(png_ptr, info_ptr); |
| 235 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 236 | #ifdef PNG_USER_MEM_SUPPORTED |
| 237 | png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn); |
| 238 | #else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 239 | png_destroy_struct((png_voidp)info_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 240 | #endif |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 241 | *info_ptr_ptr = (png_infop)NULL; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* Initialize the info structure. This is now an internal function (0.89) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 246 | * and applications using it are urged to use png_create_info_struct() |
| 247 | * instead. |
| 248 | */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 249 | void |
| 250 | png_info_init(png_infop info_ptr) |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 251 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 252 | png_debug(1, "in png_info_init\n"); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 253 | /* set everything to 0 */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 254 | png_memset(info_ptr, 0, sizeof (png_info)); |
| 255 | } |
| 256 | |
| 257 | /* This is an internal routine to free any memory that the info struct is |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 258 | * pointing to before re-using it or freeing the struct itself. Recall |
| 259 | * that png_free() checks for NULL pointers for us. |
| 260 | */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 261 | void |
| 262 | png_info_destroy(png_structp png_ptr, png_infop info_ptr) |
| 263 | { |
| 264 | #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 265 | png_debug(1, "in png_info_destroy\n"); |
| 266 | if (info_ptr->text != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 267 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 268 | int i; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 269 | for (i = 0; i < info_ptr->num_text; i++) |
| 270 | { |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 271 | if(info_ptr->text[i].key != NULL) |
| 272 | { |
| 273 | png_free(png_ptr, info_ptr->text[i].key); |
| 274 | info_ptr->text[i].key = NULL; |
| 275 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 276 | } |
| 277 | png_free(png_ptr, info_ptr->text); |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 278 | info_ptr->text = NULL; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 279 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 280 | #endif |
| 281 | #if defined(PNG_READ_pCAL_SUPPORTED) |
| 282 | png_free(png_ptr, info_ptr->pcal_purpose); |
| 283 | png_free(png_ptr, info_ptr->pcal_units); |
| 284 | if (info_ptr->pcal_params != NULL) |
| 285 | { |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 286 | int i; |
Glenn Randers-Pehrson | 0f881d6 | 1998-02-07 10:20:57 -0600 | [diff] [blame] | 287 | for (i = 0; i < (int)info_ptr->pcal_nparams; i++) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 288 | { |
| 289 | png_free(png_ptr, info_ptr->pcal_params[i]); |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 290 | info_ptr->pcal_params[i]=NULL; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 291 | } |
| 292 | png_free(png_ptr, info_ptr->pcal_params); |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 293 | info_ptr->pcal_params = NULL; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 294 | } |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 295 | #endif |
| 296 | |
| 297 | png_info_init(info_ptr); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 298 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 299 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 300 | /* This function returns a pointer to the io_ptr associated with the user |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 301 | * functions. The application should free any memory associated with this |
| 302 | * pointer before png_write_destroy() or png_read_destroy() are called. |
| 303 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 304 | png_voidp |
| 305 | png_get_io_ptr(png_structp png_ptr) |
| 306 | { |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 307 | return (png_ptr->io_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 308 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 309 | |
| 310 | #if !defined(PNG_NO_STDIO) |
| 311 | /* Initialize the default input/output functions for the PNG file. If you |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 312 | * use your own read or write routines, you can call either png_set_read_fn() |
| 313 | * or png_set_write_fn() instead of png_init_io(). |
| 314 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 315 | void |
| 316 | png_init_io(png_structp png_ptr, FILE *fp) |
| 317 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 318 | png_debug(1, "in png_init_io\n"); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 319 | png_ptr->io_ptr = (png_voidp)fp; |
| 320 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 321 | #endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 322 | |
| 323 | #if defined(PNG_TIME_RFC1123_SUPPORTED) |
| 324 | /* Convert the supplied time into an RFC 1123 string suitable for use in |
| 325 | * a "Creation Time" or other text-based time string. |
| 326 | */ |
| 327 | png_charp |
| 328 | png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime) |
| 329 | { |
| 330 | static PNG_CONST char short_months[12][4] = |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 331 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 332 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 333 | |
| 334 | if (png_ptr->time_buffer == NULL) |
| 335 | { |
| 336 | png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29* |
| 337 | sizeof(char))); |
| 338 | } |
| 339 | |
| 340 | #ifdef USE_FAR_KEYWORD |
| 341 | { |
| 342 | char near_time_buf[29]; |
| 343 | sprintf(near_time_buf, "%d %s %d %02d:%02d:%02d +0000", |
| 344 | ptime->day % 32, short_months[(ptime->month - 1) % 12], |
| 345 | ptime->year, ptime->hour % 24, ptime->minute % 60, |
| 346 | ptime->second % 61); |
| 347 | png_memcpy(png_ptr->time_buffer, near_time_buf, |
| 348 | 29*sizeof(char)); |
| 349 | } |
| 350 | #else |
| 351 | sprintf(png_ptr->time_buffer, "%d %s %d %02d:%02d:%02d +0000", |
| 352 | ptime->day % 32, short_months[(ptime->month - 1) % 12], |
| 353 | ptime->year, ptime->hour % 24, ptime->minute % 60, |
| 354 | ptime->second % 61); |
| 355 | #endif |
| 356 | return ((png_charp)png_ptr->time_buffer); |
| 357 | } |
| 358 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 359 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 360 | /* Signature string for a PNG file. */ |
| 361 | png_bytep |
| 362 | png_sig_bytes(png_structp png_ptr) |
| 363 | { |
| 364 | const png_byte png_sig_numbers[9] = {137, 80, 78, 71, 13, 10, 26, 10, 0}; |
| 365 | if (png_ptr == NULL) /* silence compiler warning */ |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame^] | 366 | return ((png_bytep) strdup((png_const_charp)png_sig_numbers)); |
| 367 | return ((png_bytep) strdup((png_const_charp)png_sig_numbers)); |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 368 | } |
| 369 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 370 | png_charp |
| 371 | png_get_copyright(png_structp png_ptr) |
| 372 | { |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 373 | if (png_ptr != NULL || png_ptr == NULL) /* silence compiler warning */ |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame^] | 374 | return ("\n libpng version 1.0.5d - November 29, 1999\n\ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 375 | Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.\n\ |
| 376 | Copyright (c) 1996, 1997 Andreas Dilger\n\ |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 377 | Copyright (c) 1998, 1999 Glenn Randers-Pehrson\n"); |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 378 | return (""); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 379 | } |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 380 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 381 | /* The following return the library version as a short string in the |
| 382 | * format 1.0.0 through 99.99.99zz. To get the version of *.h files used |
| 383 | * with your application, print out PNG_LIBPNG_VER_STRING, which is defined |
| 384 | * in png.h. |
| 385 | */ |
| 386 | |
| 387 | png_charp |
| 388 | png_get_libpng_ver(png_structp png_ptr) |
| 389 | { |
| 390 | /* Version of *.c files used when building libpng */ |
| 391 | if(png_ptr != NULL) /* silence compiler warning about unused png_ptr */ |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame^] | 392 | return("1.0.5d"); |
| 393 | return("1.0.5d"); |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | png_charp |
| 397 | png_get_header_ver(png_structp png_ptr) |
| 398 | { |
| 399 | /* Version of *.h files used when building libpng */ |
| 400 | if(png_ptr != NULL) /* silence compiler warning about unused png_ptr */ |
| 401 | return(PNG_LIBPNG_VER_STRING); |
| 402 | return(PNG_LIBPNG_VER_STRING); |
| 403 | } |
| 404 | |
| 405 | png_charp |
| 406 | png_get_header_version(png_structp png_ptr) |
| 407 | { |
| 408 | /* Returns longer string containing both version and date */ |
| 409 | if(png_ptr != NULL) /* silence compiler warning about unused png_ptr */ |
| 410 | return(PNG_HEADER_VERSION_STRING); |
| 411 | return(PNG_HEADER_VERSION_STRING); |
| 412 | } |
| 413 | |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 414 | /* Generate a compiler error if there is an old png.h in the search path. */ |
| 415 | void |
| 416 | png_check_version |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame^] | 417 | (version_1_0_5d png_h_is_not_version_1_0_5d) |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 418 | { |
Glenn Randers-Pehrson | 074af5e | 1999-11-28 23:32:18 -0600 | [diff] [blame^] | 419 | if(png_h_is_not_version_1_0_5d == NULL) |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 420 | return; |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 421 | } |