| 1 | /* |
|---|
| 2 | * $Id: //suprahd/releases/suprahd_163/suprahd_ztvapp640_163/drivers/graphics/PNG/lpng102/png.c#1 $ |
|---|
| 3 | * $Revision: #1 $ |
|---|
| 4 | * $DateTime: 2006/02/24 17:51:46 $ |
|---|
| 5 | * $Change: 42566 $ |
|---|
| 6 | * $Author: pryush.sharma $ |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | /* png.c - location for general purpose libpng functions |
|---|
| 11 | * |
|---|
| 12 | * libpng 1.0.2 - June 14, 1998 |
|---|
| 13 | * For conditions of distribution and use, see copyright notice in png.h |
|---|
| 14 | * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. |
|---|
| 15 | * Copyright (c) 1996, 1997 Andreas Dilger |
|---|
| 16 | * Copyright (c) 1998, Glenn Randers-Pehrson |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #define PNG_INTERNAL |
|---|
| 20 | #define PNG_NO_EXTERN |
|---|
| 21 | #include "png.h" |
|---|
| 22 | |
|---|
| 23 | /* Version information for C files. This had better match the version |
|---|
| 24 | * string defined in png.h. |
|---|
| 25 | */ |
|---|
| 26 | char png_libpng_ver[12] = "1.0.2"; |
|---|
| 27 | |
|---|
| 28 | /* Place to hold the signature string for a PNG file. */ |
|---|
| 29 | png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10}; |
|---|
| 30 | |
|---|
| 31 | /* Constant strings for known chunk types. If you need to add a chunk, |
|---|
| 32 | * add a string holding the name here. If you want to make the code |
|---|
| 33 | * portable to EBCDIC machines, use ASCII numbers, not characters. |
|---|
| 34 | */ |
|---|
| 35 | png_byte FARDATA png_IHDR[5] = { 73, 72, 68, 82, '\0'}; |
|---|
| 36 | png_byte FARDATA png_IDAT[5] = { 73, 68, 65, 84, '\0'}; |
|---|
| 37 | png_byte FARDATA png_IEND[5] = { 73, 69, 78, 68, '\0'}; |
|---|
| 38 | png_byte FARDATA png_PLTE[5] = { 80, 76, 84, 69, '\0'}; |
|---|
| 39 | png_byte FARDATA png_bKGD[5] = { 98, 75, 71, 68, '\0'}; |
|---|
| 40 | png_byte FARDATA png_cHRM[5] = { 99, 72, 82, 77, '\0'}; |
|---|
| 41 | png_byte FARDATA png_gAMA[5] = {103, 65, 77, 65, '\0'}; |
|---|
| 42 | png_byte FARDATA png_hIST[5] = {104, 73, 83, 84, '\0'}; |
|---|
| 43 | png_byte FARDATA png_oFFs[5] = {111, 70, 70, 115, '\0'}; |
|---|
| 44 | png_byte FARDATA png_pCAL[5] = {112, 67, 65, 76, '\0'}; |
|---|
| 45 | png_byte FARDATA png_pHYs[5] = {112, 72, 89, 115, '\0'}; |
|---|
| 46 | png_byte FARDATA png_sBIT[5] = {115, 66, 73, 84, '\0'}; |
|---|
| 47 | png_byte FARDATA png_sRGB[5] = {115, 82, 71, 66, '\0'}; |
|---|
| 48 | png_byte FARDATA png_tEXt[5] = {116, 69, 88, 116, '\0'}; |
|---|
| 49 | png_byte FARDATA png_tIME[5] = {116, 73, 77, 69, '\0'}; |
|---|
| 50 | png_byte FARDATA png_tRNS[5] = {116, 82, 78, 83, '\0'}; |
|---|
| 51 | png_byte FARDATA png_zTXt[5] = {122, 84, 88, 116, '\0'}; |
|---|
| 52 | |
|---|
| 53 | /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */ |
|---|
| 54 | |
|---|
| 55 | /* start of interlace block */ |
|---|
| 56 | int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; |
|---|
| 57 | |
|---|
| 58 | /* offset to next interlace block */ |
|---|
| 59 | int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; |
|---|
| 60 | |
|---|
| 61 | /* start of interlace block in the y direction */ |
|---|
| 62 | int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; |
|---|
| 63 | |
|---|
| 64 | /* offset to next interlace block in the y direction */ |
|---|
| 65 | int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; |
|---|
| 66 | |
|---|
| 67 | /* Width of interlace block. This is not currently used - if you need |
|---|
| 68 | * it, uncomment it here and in png.h |
|---|
| 69 | int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1}; |
|---|
| 70 | */ |
|---|
| 71 | |
|---|
| 72 | /* Height of interlace block. This is not currently used - if you need |
|---|
| 73 | * it, uncomment it here and in png.h |
|---|
| 74 | int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; |
|---|
| 75 | */ |
|---|
| 76 | |
|---|
| 77 | /* Mask to determine which pixels are valid in a pass */ |
|---|
| 78 | int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff}; |
|---|
| 79 | |
|---|
| 80 | /* Mask to determine which pixels to overwrite while displaying */ |
|---|
| 81 | int FARDATA png_pass_dsp_mask[] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff}; |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | /* Tells libpng that we have already handled the first "num_bytes" bytes |
|---|
| 85 | * of the PNG file signature. If the PNG data is embedded into another |
|---|
| 86 | * stream we can set num_bytes = 8 so that libpng will not attempt to read |
|---|
| 87 | * or write any of the magic bytes before it starts on the IHDR. |
|---|
| 88 | */ |
|---|
| 89 | void |
|---|
| 90 | png_set_sig_bytes(png_structp png_ptr, int num_bytes) |
|---|
| 91 | { |
|---|
| 92 | png_debug(1, "in png_set_sig_bytes\n"); |
|---|
| 93 | if (num_bytes > 8) |
|---|
| 94 | png_error(png_ptr, "Too many bytes for PNG signature."); |
|---|
| 95 | |
|---|
| 96 | png_ptr->sig_bytes = num_bytes < 0 ? 0 : num_bytes; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /* Checks whether the supplied bytes match the PNG signature. We allow |
|---|
| 100 | * checking less than the full 8-byte signature so that those apps that |
|---|
| 101 | * already read the first few bytes of a file to determine the file type |
|---|
| 102 | * can simply check the remaining bytes for extra assurance. Returns |
|---|
| 103 | * an integer less than, equal to, or greater than zero if sig is found, |
|---|
| 104 | * respectively, to be less than, to match, or be greater than the correct |
|---|
| 105 | * PNG signature (this is the same behaviour as strcmp, memcmp, etc). |
|---|
| 106 | */ |
|---|
| 107 | int |
|---|
| 108 | png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check) |
|---|
| 109 | { |
|---|
| 110 | if (num_to_check > 8) |
|---|
| 111 | num_to_check = 8; |
|---|
| 112 | else if (num_to_check < 1) |
|---|
| 113 | return (0); |
|---|
| 114 | |
|---|
| 115 | if (start > 7) |
|---|
| 116 | return (0); |
|---|
| 117 | |
|---|
| 118 | if (start + num_to_check > 8) |
|---|
| 119 | num_to_check = 8 - start; |
|---|
| 120 | |
|---|
| 121 | return ((int)(png_memcmp(&sig[start], &png_sig[start], num_to_check))); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /* (Obsolete) function to check signature bytes. It does not allow one |
|---|
| 125 | * to check a partial signature. This function will be removed in the |
|---|
| 126 | * future - use png_sig_cmp(). |
|---|
| 127 | */ |
|---|
| 128 | int |
|---|
| 129 | png_check_sig(png_bytep sig, int num) |
|---|
| 130 | { |
|---|
| 131 | return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num)); |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /* Function to allocate memory for zlib. */ |
|---|
| 135 | voidpf |
|---|
| 136 | png_zalloc(voidpf png_ptr, uInt items, uInt size) |
|---|
| 137 | { |
|---|
| 138 | png_uint_32 num_bytes = (png_uint_32)items * size; |
|---|
| 139 | png_voidp ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes); |
|---|
| 140 | |
|---|
| 141 | if (num_bytes > (png_uint_32)0x8000L) |
|---|
| 142 | { |
|---|
| 143 | png_memset(ptr, 0, (png_size_t)0x8000L); |
|---|
| 144 | png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0, |
|---|
| 145 | (png_size_t)(num_bytes - (png_uint_32)0x8000L)); |
|---|
| 146 | } |
|---|
| 147 | else |
|---|
| 148 | { |
|---|
| 149 | png_memset(ptr, 0, (png_size_t)num_bytes); |
|---|
| 150 | } |
|---|
| 151 | return ((voidpf)ptr); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /* function to free memory for zlib */ |
|---|
| 155 | void |
|---|
| 156 | png_zfree(voidpf png_ptr, voidpf ptr) |
|---|
| 157 | { |
|---|
| 158 | png_free((png_structp)png_ptr, (png_voidp)ptr); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /* Reset the CRC variable to 32 bits of 1's. Care must be taken |
|---|
| 162 | * in case CRC is > 32 bits to leave the top bits 0. |
|---|
| 163 | */ |
|---|
| 164 | void |
|---|
| 165 | png_reset_crc(png_structp png_ptr) |
|---|
| 166 | { |
|---|
| 167 | png_ptr->crc = crc32(0, Z_NULL, 0); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | /* Calculate the CRC over a section of data. We can only pass as |
|---|
| 171 | * much data to this routine as the largest single buffer size. We |
|---|
| 172 | * also check that this data will actually be used before going to the |
|---|
| 173 | * trouble of calculating it. |
|---|
| 174 | */ |
|---|
| 175 | void |
|---|
| 176 | png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length) |
|---|
| 177 | { |
|---|
| 178 | int need_crc = 1; |
|---|
| 179 | |
|---|
| 180 | if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ |
|---|
| 181 | { |
|---|
| 182 | if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == |
|---|
| 183 | (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
|---|
| 184 | need_crc = 0; |
|---|
| 185 | } |
|---|
| 186 | else /* critical */ |
|---|
| 187 | { |
|---|
| 188 | if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) |
|---|
| 189 | need_crc = 0; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | if (need_crc) |
|---|
| 193 | png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length); |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | /* Allocate the memory for an info_struct for the application. We don't |
|---|
| 197 | * really need the png_ptr, but it could potentially be useful in the |
|---|
| 198 | * future. This should be used in favour of malloc(sizeof(png_info)) |
|---|
| 199 | * and png_info_init() so that applications that want to use a shared |
|---|
| 200 | * libpng don't have to be recompiled if png_info changes size. |
|---|
| 201 | */ |
|---|
| 202 | png_infop |
|---|
| 203 | png_create_info_struct(png_structp png_ptr) |
|---|
| 204 | { |
|---|
| 205 | png_infop info_ptr; |
|---|
| 206 | |
|---|
| 207 | png_debug(1, "in png_create_info_struct\n"); |
|---|
| 208 | if(png_ptr == NULL) return (NULL); |
|---|
| 209 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 210 | if ((info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO, |
|---|
| 211 | png_ptr->malloc_fn)) != NULL) |
|---|
| 212 | #else |
|---|
| 213 | if ((info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO)) != NULL) |
|---|
| 214 | #endif |
|---|
| 215 | { |
|---|
| 216 | png_info_init(info_ptr); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | return (info_ptr); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | /* This function frees the memory associated with a single info struct. |
|---|
| 223 | * Normally, one would use either png_destroy_read_struct() or |
|---|
| 224 | * png_destroy_write_struct() to free an info struct, but this may be |
|---|
| 225 | * useful for some applications. |
|---|
| 226 | */ |
|---|
| 227 | void |
|---|
| 228 | png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr) |
|---|
| 229 | { |
|---|
| 230 | png_infop info_ptr = NULL; |
|---|
| 231 | |
|---|
| 232 | png_debug(1, "in png_destroy_info_struct\n"); |
|---|
| 233 | if (info_ptr_ptr != NULL) |
|---|
| 234 | info_ptr = *info_ptr_ptr; |
|---|
| 235 | |
|---|
| 236 | if (info_ptr != NULL) |
|---|
| 237 | { |
|---|
| 238 | png_info_destroy(png_ptr, info_ptr); |
|---|
| 239 | |
|---|
| 240 | #ifdef PNG_USER_MEM_SUPPORTED |
|---|
| 241 | png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn); |
|---|
| 242 | #else |
|---|
| 243 | png_destroy_struct((png_voidp)info_ptr); |
|---|
| 244 | #endif |
|---|
| 245 | *info_ptr_ptr = (png_infop)NULL; |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | /* Initialize the info structure. This is now an internal function (0.89) |
|---|
| 250 | * and applications using it are urged to use png_create_info_struct() |
|---|
| 251 | * instead. |
|---|
| 252 | */ |
|---|
| 253 | void |
|---|
| 254 | png_info_init(png_infop info_ptr) |
|---|
| 255 | { |
|---|
| 256 | png_debug(1, "in png_info_init\n"); |
|---|
| 257 | /* set everything to 0 */ |
|---|
| 258 | png_memset(info_ptr, 0, sizeof (png_info)); |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | /* This is an internal routine to free any memory that the info struct is |
|---|
| 262 | * pointing to before re-using it or freeing the struct itself. Recall |
|---|
| 263 | * that png_free() checks for NULL pointers for us. |
|---|
| 264 | */ |
|---|
| 265 | void |
|---|
| 266 | png_info_destroy(png_structp png_ptr, png_infop info_ptr) |
|---|
| 267 | { |
|---|
| 268 | #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED) |
|---|
| 269 | png_debug(1, "in png_info_destroy\n"); |
|---|
| 270 | if (info_ptr->text != NULL) |
|---|
| 271 | { |
|---|
| 272 | int i; |
|---|
| 273 | for (i = 0; i < info_ptr->num_text; i++) |
|---|
| 274 | { |
|---|
| 275 | png_free(png_ptr, info_ptr->text[i].key); |
|---|
| 276 | } |
|---|
| 277 | png_free(png_ptr, info_ptr->text); |
|---|
| 278 | } |
|---|
| 279 | #endif |
|---|
| 280 | #if defined(PNG_READ_pCAL_SUPPORTED) |
|---|
| 281 | png_free(png_ptr, info_ptr->pcal_purpose); |
|---|
| 282 | png_free(png_ptr, info_ptr->pcal_units); |
|---|
| 283 | if (info_ptr->pcal_params != NULL) |
|---|
| 284 | { |
|---|
| 285 | int i; |
|---|
| 286 | for (i = 0; i < (int)info_ptr->pcal_nparams; i++) |
|---|
| 287 | { |
|---|
| 288 | png_free(png_ptr, info_ptr->pcal_params[i]); |
|---|
| 289 | } |
|---|
| 290 | png_free(png_ptr, info_ptr->pcal_params); |
|---|
| 291 | } |
|---|
| 292 | #endif |
|---|
| 293 | |
|---|
| 294 | png_info_init(info_ptr); |
|---|
| 295 | } |
|---|
| 296 | |
|---|
| 297 | /* This function returns a pointer to the io_ptr associated with the user |
|---|
| 298 | * functions. The application should free any memory associated with this |
|---|
| 299 | * pointer before png_write_destroy() or png_read_destroy() are called. |
|---|
| 300 | */ |
|---|
| 301 | png_voidp |
|---|
| 302 | png_get_io_ptr(png_structp png_ptr) |
|---|
| 303 | { |
|---|
| 304 | return (png_ptr->io_ptr); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | #if !defined(PNG_NO_STDIO) |
|---|
| 308 | /* Initialize the default input/output functions for the PNG file. If you |
|---|
| 309 | * use your own read or write routines, you can call either png_set_read_fn() |
|---|
| 310 | * or png_set_write_fn() instead of png_init_io(). |
|---|
| 311 | */ |
|---|
| 312 | void |
|---|
| 313 | png_init_io(png_structp png_ptr, FILE *fp) |
|---|
| 314 | { |
|---|
| 315 | png_debug(1, "in png_init_io\n"); |
|---|
| 316 | png_ptr->io_ptr = (png_voidp)fp; |
|---|
| 317 | } |
|---|
| 318 | #endif |
|---|
| 319 | |
|---|
| 320 | #if defined(PNG_TIME_RFC1123_SUPPORTED) |
|---|
| 321 | /* Convert the supplied time into an RFC 1123 string suitable for use in |
|---|
| 322 | * a "Creation Time" or other text-based time string. |
|---|
| 323 | */ |
|---|
| 324 | png_charp |
|---|
| 325 | png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime) |
|---|
| 326 | { |
|---|
| 327 | static PNG_CONST char short_months[12][4] = |
|---|
| 328 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
|---|
| 329 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
|---|
| 330 | |
|---|
| 331 | if (png_ptr->time_buffer == NULL) |
|---|
| 332 | { |
|---|
| 333 | png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29* |
|---|
| 334 | sizeof(char))); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | #ifdef USE_FAR_KEYWORD |
|---|
| 338 | { |
|---|
| 339 | char near_time_buf[29]; |
|---|
| 340 | sprintf(near_time_buf, "%d %s %d %02d:%02d:%02d +0000", |
|---|
| 341 | ptime->day % 32, short_months[(ptime->month - 1) % 12], |
|---|
| 342 | ptime->year, ptime->hour % 24, ptime->minute % 60, |
|---|
| 343 | ptime->second % 61); |
|---|
| 344 | png_memcpy(png_ptr->time_buffer, near_time_buf, |
|---|
| 345 | 29*sizeof(char)); |
|---|
| 346 | } |
|---|
| 347 | #else |
|---|
| 348 | sprintf(png_ptr->time_buffer, "%d %s %d %02d:%02d:%02d +0000", |
|---|
| 349 | ptime->day % 32, short_months[(ptime->month - 1) % 12], |
|---|
| 350 | ptime->year, ptime->hour % 24, ptime->minute % 60, |
|---|
| 351 | ptime->second % 61); |
|---|
| 352 | #endif |
|---|
| 353 | return ((png_charp)png_ptr->time_buffer); |
|---|
| 354 | } |
|---|
| 355 | #endif /* PNG_TIME_RFC1123_SUPPORTED */ |
|---|