~ubuntu-branches/debian/sid/gdal/sid

« back to all changes in this revision

Viewing changes to frmts/png/libpng/png.c

  • Committer: Package Import Robot
  • Author(s): Francesco Paolo Lovergine
  • Date: 2012-05-07 15:04:42 UTC
  • mfrom: (5.5.16 experimental)
  • Revision ID: package-import@ubuntu.com-20120507150442-2eks97loeh6rq005
Tags: 1.9.0-1
* Ready for sid, starting transition.
* All symfiles updated to latest builds.
* Added dh_numpy call in debian/rules to depend on numpy ABI.
* Policy bumped to 3.9.3, no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/* png.c - location for general purpose libpng functions
3
3
 *
4
 
 * Last changed in libpng 1.2.34 [December 18, 2008]
5
 
 * For conditions of distribution and use, see copyright notice in png.h
6
 
 * Copyright (c) 1998-2008 Glenn Randers-Pehrson
 
4
 * Last changed in libpng 1.2.46 [February 25, 2011]
 
5
 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
7
6
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
8
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
14
#define PNG_INTERNAL
12
15
#define PNG_NO_EXTERN
 
16
#define PNG_NO_PEDANTIC_WARNINGS
13
17
#include "png.h"
14
18
 
15
19
/* Generate a compiler error if there is an old png.h in the search path. */
16
 
typedef version_1_2_35 Your_png_h_is_not_version_1_2_35;
 
20
typedef version_1_2_46 Your_png_h_is_not_version_1_2_46;
17
21
 
18
22
/* Version information for C files.  This had better match the version
19
 
 * string defined in png.h.  */
 
23
 * string defined in png.h.
 
24
 */
20
25
 
21
26
#ifdef PNG_USE_GLOBAL_ARRAYS
22
27
/* png_libpng_ver was changed to a function in version 1.0.5c */
53
58
PNG_zTXt;
54
59
 
55
60
#ifdef PNG_READ_SUPPORTED
56
 
/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
 
61
/* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
57
62
 
58
 
/* start of interlace block */
 
63
/* Start of interlace block */
59
64
PNG_CONST int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
60
65
 
61
 
/* offset to next interlace block */
 
66
/* Offset to next interlace block */
62
67
PNG_CONST int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
63
68
 
64
 
/* start of interlace block in the y direction */
 
69
/* Start of interlace block in the y direction */
65
70
PNG_CONST int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
66
71
 
67
 
/* offset to next interlace block in the y direction */
 
72
/* Offset to next interlace block in the y direction */
68
73
PNG_CONST int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
69
74
 
70
75
/* Height of interlace block.  This is not currently used - if you need
73
78
*/
74
79
 
75
80
/* Mask to determine which pixels are valid in a pass */
76
 
PNG_CONST int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
 
81
PNG_CONST int FARDATA png_pass_mask[] =
 
82
    {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
77
83
 
78
84
/* Mask to determine which pixels to overwrite while displaying */
79
85
PNG_CONST int FARDATA png_pass_dsp_mask[]
92
98
void PNGAPI
93
99
png_set_sig_bytes(png_structp png_ptr, int num_bytes)
94
100
{
95
 
   if (png_ptr == NULL) return;
96
101
   png_debug(1, "in png_set_sig_bytes");
 
102
 
 
103
   if (png_ptr == NULL)
 
104
      return;
 
105
 
97
106
   if (num_bytes > 8)
98
107
      png_error(png_ptr, "Too many bytes for PNG signature.");
99
108
 
144
153
#ifdef PNG_1_0_X
145
154
voidpf PNGAPI
146
155
#else
147
 
voidpf /* private */
 
156
voidpf /* PRIVATE */
148
157
#endif
149
158
png_zalloc(voidpf png_ptr, uInt items, uInt size)
150
159
{
153
162
   png_uint_32 save_flags=p->flags;
154
163
   png_uint_32 num_bytes;
155
164
 
156
 
   if (png_ptr == NULL) return (NULL);
 
165
   if (png_ptr == NULL)
 
166
      return (NULL);
157
167
   if (items > PNG_UINT_32_MAX/size)
158
168
   {
159
169
     png_warning (p, "Potential overflow in png_zalloc()");
183
193
   return ((voidpf)ptr);
184
194
}
185
195
 
186
 
/* function to free memory for zlib */
 
196
/* Function to free memory for zlib */
187
197
#ifdef PNG_1_0_X
188
198
void PNGAPI
189
199
#else
190
 
void /* private */
 
200
void /* PRIVATE */
191
201
#endif
192
202
png_zfree(voidpf png_ptr, voidpf ptr)
193
203
{
241
251
   png_infop info_ptr;
242
252
 
243
253
   png_debug(1, "in png_create_info_struct");
244
 
   if (png_ptr == NULL) return (NULL);
 
254
 
 
255
   if (png_ptr == NULL)
 
256
      return (NULL);
 
257
 
245
258
#ifdef PNG_USER_MEM_SUPPORTED
246
259
   info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
247
260
      png_ptr->malloc_fn, png_ptr->mem_ptr);
263
276
png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
264
277
{
265
278
   png_infop info_ptr = NULL;
266
 
   if (png_ptr == NULL) return;
267
279
 
268
280
   png_debug(1, "in png_destroy_info_struct");
 
281
 
 
282
   if (png_ptr == NULL)
 
283
      return;
 
284
 
269
285
   if (info_ptr_ptr != NULL)
270
286
      info_ptr = *info_ptr_ptr;
271
287
 
302
318
{
303
319
   png_infop info_ptr = *ptr_ptr;
304
320
 
305
 
   if (info_ptr == NULL) return;
306
 
 
307
321
   png_debug(1, "in png_info_init_3");
308
322
 
 
323
   if (info_ptr == NULL)
 
324
      return;
 
325
 
309
326
   if (png_sizeof(png_info) > png_info_struct_size)
310
 
     {
311
 
       png_destroy_struct(info_ptr);
312
 
       info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
313
 
       *ptr_ptr = info_ptr;
314
 
     }
 
327
   {
 
328
      png_destroy_struct(info_ptr);
 
329
      info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
 
330
      *ptr_ptr = info_ptr;
 
331
   }
315
332
 
316
 
   /* set everything to 0 */
 
333
   /* Set everything to 0 */
317
334
   png_memset(info_ptr, 0, png_sizeof(png_info));
318
335
}
319
336
 
323
340
   int freer, png_uint_32 mask)
324
341
{
325
342
   png_debug(1, "in png_data_freer");
 
343
 
326
344
   if (png_ptr == NULL || info_ptr == NULL)
327
345
      return;
 
346
 
328
347
   if (freer == PNG_DESTROY_WILL_FREE_DATA)
329
348
      info_ptr->free_me |= mask;
330
349
   else if (freer == PNG_USER_WILL_FREE_DATA)
340
359
   int num)
341
360
{
342
361
   png_debug(1, "in png_free_data");
 
362
 
343
363
   if (png_ptr == NULL || info_ptr == NULL)
344
364
      return;
345
365
 
346
 
#if defined(PNG_TEXT_SUPPORTED)
347
 
/* free text item num or (if num == -1) all text items */
 
366
#ifdef PNG_TEXT_SUPPORTED
 
367
   /* Free text item num or (if num == -1) all text items */
348
368
#ifdef PNG_FREE_ME_SUPPORTED
349
 
if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
 
369
   if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
350
370
#else
351
 
if (mask & PNG_FREE_TEXT)
 
371
   if (mask & PNG_FREE_TEXT)
352
372
#endif
353
 
{
354
 
   if (num != -1)
355
 
   {
356
 
     if (info_ptr->text && info_ptr->text[num].key)
357
 
     {
358
 
         png_free(png_ptr, info_ptr->text[num].key);
359
 
         info_ptr->text[num].key = NULL;
360
 
     }
361
 
   }
362
 
   else
363
 
   {
364
 
       int i;
365
 
       for (i = 0; i < info_ptr->num_text; i++)
366
 
           png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
367
 
       png_free(png_ptr, info_ptr->text);
368
 
       info_ptr->text = NULL;
369
 
       info_ptr->num_text=0;
370
 
   }
371
 
}
 
373
   {
 
374
      if (num != -1)
 
375
      {
 
376
         if (info_ptr->text && info_ptr->text[num].key)
 
377
         {
 
378
            png_free(png_ptr, info_ptr->text[num].key);
 
379
            info_ptr->text[num].key = NULL;
 
380
         }
 
381
      }
 
382
      else
 
383
      {
 
384
         int i;
 
385
         for (i = 0; i < info_ptr->num_text; i++)
 
386
             png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
 
387
         png_free(png_ptr, info_ptr->text);
 
388
         info_ptr->text = NULL;
 
389
         info_ptr->num_text=0;
 
390
      }
 
391
   }
372
392
#endif
373
393
 
374
 
#if defined(PNG_tRNS_SUPPORTED)
375
 
/* free any tRNS entry */
 
394
#ifdef PNG_tRNS_SUPPORTED
 
395
   /* Free any tRNS entry */
376
396
#ifdef PNG_FREE_ME_SUPPORTED
377
 
if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
 
397
   if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
378
398
#else
379
 
if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
 
399
   if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
380
400
#endif
381
 
{
382
 
    png_free(png_ptr, info_ptr->trans);
383
 
    info_ptr->trans = NULL;
384
 
    info_ptr->valid &= ~PNG_INFO_tRNS;
 
401
   {
 
402
      png_free(png_ptr, info_ptr->trans);
 
403
      info_ptr->trans = NULL;
 
404
      info_ptr->valid &= ~PNG_INFO_tRNS;
385
405
#ifndef PNG_FREE_ME_SUPPORTED
386
 
    png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
 
406
      png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
387
407
#endif
388
 
}
 
408
   }
389
409
#endif
390
410
 
391
 
#if defined(PNG_sCAL_SUPPORTED)
392
 
/* free any sCAL entry */
 
411
#ifdef PNG_sCAL_SUPPORTED
 
412
   /* Free any sCAL entry */
393
413
#ifdef PNG_FREE_ME_SUPPORTED
394
 
if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
 
414
   if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
395
415
#else
396
 
if (mask & PNG_FREE_SCAL)
 
416
   if (mask & PNG_FREE_SCAL)
397
417
#endif
398
 
{
 
418
   {
399
419
#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
400
 
    png_free(png_ptr, info_ptr->scal_s_width);
401
 
    png_free(png_ptr, info_ptr->scal_s_height);
402
 
    info_ptr->scal_s_width = NULL;
403
 
    info_ptr->scal_s_height = NULL;
404
 
#endif
405
 
    info_ptr->valid &= ~PNG_INFO_sCAL;
406
 
}
407
 
#endif
408
 
 
409
 
#if defined(PNG_pCAL_SUPPORTED)
410
 
/* free any pCAL entry */
411
 
#ifdef PNG_FREE_ME_SUPPORTED
412
 
if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
413
 
#else
414
 
if (mask & PNG_FREE_PCAL)
415
 
#endif
416
 
{
417
 
    png_free(png_ptr, info_ptr->pcal_purpose);
418
 
    png_free(png_ptr, info_ptr->pcal_units);
419
 
    info_ptr->pcal_purpose = NULL;
420
 
    info_ptr->pcal_units = NULL;
421
 
    if (info_ptr->pcal_params != NULL)
422
 
    {
423
 
        int i;
424
 
        for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
425
 
        {
426
 
          png_free(png_ptr, info_ptr->pcal_params[i]);
427
 
          info_ptr->pcal_params[i]=NULL;
428
 
        }
429
 
        png_free(png_ptr, info_ptr->pcal_params);
430
 
        info_ptr->pcal_params = NULL;
431
 
    }
432
 
    info_ptr->valid &= ~PNG_INFO_pCAL;
433
 
}
434
 
#endif
435
 
 
436
 
#if defined(PNG_iCCP_SUPPORTED)
437
 
/* free any iCCP entry */
438
 
#ifdef PNG_FREE_ME_SUPPORTED
439
 
if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
440
 
#else
441
 
if (mask & PNG_FREE_ICCP)
442
 
#endif
443
 
{
444
 
    png_free(png_ptr, info_ptr->iccp_name);
445
 
    png_free(png_ptr, info_ptr->iccp_profile);
446
 
    info_ptr->iccp_name = NULL;
447
 
    info_ptr->iccp_profile = NULL;
448
 
    info_ptr->valid &= ~PNG_INFO_iCCP;
449
 
}
450
 
#endif
451
 
 
452
 
#if defined(PNG_sPLT_SUPPORTED)
453
 
/* free a given sPLT entry, or (if num == -1) all sPLT entries */
454
 
#ifdef PNG_FREE_ME_SUPPORTED
455
 
if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
456
 
#else
457
 
if (mask & PNG_FREE_SPLT)
458
 
#endif
459
 
{
460
 
   if (num != -1)
461
 
   {
462
 
      if (info_ptr->splt_palettes)
463
 
      {
464
 
          png_free(png_ptr, info_ptr->splt_palettes[num].name);
465
 
          png_free(png_ptr, info_ptr->splt_palettes[num].entries);
466
 
          info_ptr->splt_palettes[num].name = NULL;
467
 
          info_ptr->splt_palettes[num].entries = NULL;
468
 
      }
469
 
   }
470
 
   else
471
 
   {
472
 
       if (info_ptr->splt_palettes_num)
473
 
       {
 
420
      png_free(png_ptr, info_ptr->scal_s_width);
 
421
      png_free(png_ptr, info_ptr->scal_s_height);
 
422
      info_ptr->scal_s_width = NULL;
 
423
      info_ptr->scal_s_height = NULL;
 
424
#endif
 
425
      info_ptr->valid &= ~PNG_INFO_sCAL;
 
426
   }
 
427
#endif
 
428
 
 
429
#ifdef PNG_pCAL_SUPPORTED
 
430
   /* Free any pCAL entry */
 
431
#ifdef PNG_FREE_ME_SUPPORTED
 
432
   if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
 
433
#else
 
434
   if (mask & PNG_FREE_PCAL)
 
435
#endif
 
436
   {
 
437
      png_free(png_ptr, info_ptr->pcal_purpose);
 
438
      png_free(png_ptr, info_ptr->pcal_units);
 
439
      info_ptr->pcal_purpose = NULL;
 
440
      info_ptr->pcal_units = NULL;
 
441
      if (info_ptr->pcal_params != NULL)
 
442
         {
 
443
            int i;
 
444
            for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
 
445
            {
 
446
               png_free(png_ptr, info_ptr->pcal_params[i]);
 
447
               info_ptr->pcal_params[i] = NULL;
 
448
            }
 
449
            png_free(png_ptr, info_ptr->pcal_params);
 
450
            info_ptr->pcal_params = NULL;
 
451
         }
 
452
      info_ptr->valid &= ~PNG_INFO_pCAL;
 
453
   }
 
454
#endif
 
455
 
 
456
#ifdef PNG_iCCP_SUPPORTED
 
457
   /* Free any iCCP entry */
 
458
#ifdef PNG_FREE_ME_SUPPORTED
 
459
   if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
 
460
#else
 
461
   if (mask & PNG_FREE_ICCP)
 
462
#endif
 
463
   {
 
464
      png_free(png_ptr, info_ptr->iccp_name);
 
465
      png_free(png_ptr, info_ptr->iccp_profile);
 
466
      info_ptr->iccp_name = NULL;
 
467
      info_ptr->iccp_profile = NULL;
 
468
      info_ptr->valid &= ~PNG_INFO_iCCP;
 
469
   }
 
470
#endif
 
471
 
 
472
#ifdef PNG_sPLT_SUPPORTED
 
473
   /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
 
474
#ifdef PNG_FREE_ME_SUPPORTED
 
475
   if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
 
476
#else
 
477
   if (mask & PNG_FREE_SPLT)
 
478
#endif
 
479
   {
 
480
      if (num != -1)
 
481
      {
 
482
         if (info_ptr->splt_palettes)
 
483
         {
 
484
            png_free(png_ptr, info_ptr->splt_palettes[num].name);
 
485
            png_free(png_ptr, info_ptr->splt_palettes[num].entries);
 
486
            info_ptr->splt_palettes[num].name = NULL;
 
487
            info_ptr->splt_palettes[num].entries = NULL;
 
488
         }
 
489
      }
 
490
      else
 
491
      {
 
492
         if (info_ptr->splt_palettes_num)
 
493
         {
 
494
            int i;
 
495
            for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
 
496
               png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
 
497
 
 
498
            png_free(png_ptr, info_ptr->splt_palettes);
 
499
            info_ptr->splt_palettes = NULL;
 
500
            info_ptr->splt_palettes_num = 0;
 
501
         }
 
502
         info_ptr->valid &= ~PNG_INFO_sPLT;
 
503
      }
 
504
   }
 
505
#endif
 
506
 
 
507
#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
 
508
   if (png_ptr->unknown_chunk.data)
 
509
   {
 
510
      png_free(png_ptr, png_ptr->unknown_chunk.data);
 
511
      png_ptr->unknown_chunk.data = NULL;
 
512
   }
 
513
 
 
514
#ifdef PNG_FREE_ME_SUPPORTED
 
515
   if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
 
516
#else
 
517
   if (mask & PNG_FREE_UNKN)
 
518
#endif
 
519
   {
 
520
      if (num != -1)
 
521
      {
 
522
          if (info_ptr->unknown_chunks)
 
523
          {
 
524
             png_free(png_ptr, info_ptr->unknown_chunks[num].data);
 
525
             info_ptr->unknown_chunks[num].data = NULL;
 
526
          }
 
527
      }
 
528
      else
 
529
      {
474
530
         int i;
475
 
         for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
476
 
            png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
477
 
 
478
 
         png_free(png_ptr, info_ptr->splt_palettes);
479
 
         info_ptr->splt_palettes = NULL;
480
 
         info_ptr->splt_palettes_num = 0;
481
 
       }
482
 
       info_ptr->valid &= ~PNG_INFO_sPLT;
483
 
   }
484
 
}
485
 
#endif
486
 
 
487
 
#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
488
 
  if (png_ptr->unknown_chunk.data)
489
 
  {
490
 
    png_free(png_ptr, png_ptr->unknown_chunk.data);
491
 
    png_ptr->unknown_chunk.data = NULL;
492
 
  }
493
 
 
494
 
#ifdef PNG_FREE_ME_SUPPORTED
495
 
if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
496
 
#else
497
 
if (mask & PNG_FREE_UNKN)
498
 
#endif
499
 
{
500
 
   if (num != -1)
501
 
   {
502
 
       if (info_ptr->unknown_chunks)
503
 
       {
504
 
          png_free(png_ptr, info_ptr->unknown_chunks[num].data);
505
 
          info_ptr->unknown_chunks[num].data = NULL;
506
 
       }
507
 
   }
508
 
   else
509
 
   {
510
 
       int i;
511
 
 
512
 
       if (info_ptr->unknown_chunks_num)
513
 
       {
514
 
         for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
515
 
            png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
516
 
 
517
 
         png_free(png_ptr, info_ptr->unknown_chunks);
518
 
         info_ptr->unknown_chunks = NULL;
519
 
         info_ptr->unknown_chunks_num = 0;
520
 
       }
521
 
   }
522
 
}
523
 
#endif
524
 
 
525
 
#if defined(PNG_hIST_SUPPORTED)
526
 
/* free any hIST entry */
527
 
#ifdef PNG_FREE_ME_SUPPORTED
528
 
if ((mask & PNG_FREE_HIST)  & info_ptr->free_me)
529
 
#else
530
 
if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
531
 
#endif
532
 
{
533
 
    png_free(png_ptr, info_ptr->hist);
534
 
    info_ptr->hist = NULL;
535
 
    info_ptr->valid &= ~PNG_INFO_hIST;
536
 
#ifndef PNG_FREE_ME_SUPPORTED
537
 
    png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
538
 
#endif
539
 
}
540
 
#endif
541
 
 
542
 
/* free any PLTE entry that was internally allocated */
543
 
#ifdef PNG_FREE_ME_SUPPORTED
544
 
if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
545
 
#else
546
 
if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
547
 
#endif
548
 
{
549
 
    png_zfree(png_ptr, info_ptr->palette);
550
 
    info_ptr->palette = NULL;
551
 
    info_ptr->valid &= ~PNG_INFO_PLTE;
552
 
#ifndef PNG_FREE_ME_SUPPORTED
553
 
    png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
554
 
#endif
555
 
    info_ptr->num_palette = 0;
556
 
}
557
 
 
558
 
#if defined(PNG_INFO_IMAGE_SUPPORTED)
559
 
/* free any image bits attached to the info structure */
560
 
#ifdef PNG_FREE_ME_SUPPORTED
561
 
if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
562
 
#else
563
 
if (mask & PNG_FREE_ROWS)
564
 
#endif
565
 
{
566
 
    if (info_ptr->row_pointers)
567
 
    {
568
 
       int row;
569
 
       for (row = 0; row < (int)info_ptr->height; row++)
570
 
       {
571
 
          png_free(png_ptr, info_ptr->row_pointers[row]);
572
 
          info_ptr->row_pointers[row]=NULL;
573
 
       }
574
 
       png_free(png_ptr, info_ptr->row_pointers);
575
 
       info_ptr->row_pointers=NULL;
576
 
    }
577
 
    info_ptr->valid &= ~PNG_INFO_IDAT;
578
 
}
 
531
 
 
532
         if (info_ptr->unknown_chunks_num)
 
533
         {
 
534
            for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
 
535
               png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
 
536
 
 
537
            png_free(png_ptr, info_ptr->unknown_chunks);
 
538
            info_ptr->unknown_chunks = NULL;
 
539
            info_ptr->unknown_chunks_num = 0;
 
540
         }
 
541
      }
 
542
   }
 
543
#endif
 
544
 
 
545
#ifdef PNG_hIST_SUPPORTED
 
546
   /* Free any hIST entry */
 
547
#ifdef PNG_FREE_ME_SUPPORTED
 
548
   if ((mask & PNG_FREE_HIST)  & info_ptr->free_me)
 
549
#else
 
550
   if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
 
551
#endif
 
552
   {
 
553
      png_free(png_ptr, info_ptr->hist);
 
554
      info_ptr->hist = NULL;
 
555
      info_ptr->valid &= ~PNG_INFO_hIST;
 
556
#ifndef PNG_FREE_ME_SUPPORTED
 
557
      png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
 
558
#endif
 
559
   }
 
560
#endif
 
561
 
 
562
   /* Free any PLTE entry that was internally allocated */
 
563
#ifdef PNG_FREE_ME_SUPPORTED
 
564
   if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
 
565
#else
 
566
   if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
 
567
#endif
 
568
   {
 
569
      png_zfree(png_ptr, info_ptr->palette);
 
570
      info_ptr->palette = NULL;
 
571
      info_ptr->valid &= ~PNG_INFO_PLTE;
 
572
#ifndef PNG_FREE_ME_SUPPORTED
 
573
      png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
 
574
#endif
 
575
      info_ptr->num_palette = 0;
 
576
   }
 
577
 
 
578
#ifdef PNG_INFO_IMAGE_SUPPORTED
 
579
   /* Free any image bits attached to the info structure */
 
580
#ifdef PNG_FREE_ME_SUPPORTED
 
581
   if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
 
582
#else
 
583
   if (mask & PNG_FREE_ROWS)
 
584
#endif
 
585
   {
 
586
      if (info_ptr->row_pointers)
 
587
      {
 
588
         int row;
 
589
         for (row = 0; row < (int)info_ptr->height; row++)
 
590
         {
 
591
            png_free(png_ptr, info_ptr->row_pointers[row]);
 
592
            info_ptr->row_pointers[row] = NULL;
 
593
         }
 
594
         png_free(png_ptr, info_ptr->row_pointers);
 
595
         info_ptr->row_pointers = NULL;
 
596
      }
 
597
      info_ptr->valid &= ~PNG_INFO_IDAT;
 
598
   }
579
599
#endif
580
600
 
581
601
#ifdef PNG_FREE_ME_SUPPORTED
582
602
   if (num == -1)
583
 
     info_ptr->free_me &= ~mask;
 
603
      info_ptr->free_me &= ~mask;
584
604
   else
585
 
     info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
 
605
      info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
586
606
#endif
587
607
}
588
608
 
597
617
 
598
618
   png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
599
619
 
600
 
#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
 
620
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
601
621
   if (png_ptr->num_chunk_list)
602
622
   {
603
 
       png_free(png_ptr, png_ptr->chunk_list);
604
 
       png_ptr->chunk_list=NULL;
605
 
       png_ptr->num_chunk_list = 0;
 
623
      png_free(png_ptr, png_ptr->chunk_list);
 
624
      png_ptr->chunk_list = NULL;
 
625
      png_ptr->num_chunk_list = 0;
606
626
   }
607
627
#endif
608
628
 
617
637
png_voidp PNGAPI
618
638
png_get_io_ptr(png_structp png_ptr)
619
639
{
620
 
   if (png_ptr == NULL) return (NULL);
 
640
   if (png_ptr == NULL)
 
641
      return (NULL);
621
642
   return (png_ptr->io_ptr);
622
643
}
623
644
 
624
645
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
625
 
#if !defined(PNG_NO_STDIO)
 
646
#ifdef PNG_STDIO_SUPPORTED
626
647
/* Initialize the default input/output functions for the PNG file.  If you
627
648
 * use your own read or write routines, you can call either png_set_read_fn()
628
649
 * or png_set_write_fn() instead of png_init_io().  If you have defined
633
654
png_init_io(png_structp png_ptr, png_FILE_p fp)
634
655
{
635
656
   png_debug(1, "in png_init_io");
636
 
   if (png_ptr == NULL) return;
 
657
 
 
658
   if (png_ptr == NULL)
 
659
      return;
 
660
 
637
661
   png_ptr->io_ptr = (png_voidp)fp;
638
662
}
639
663
#endif
640
664
 
641
 
#if defined(PNG_TIME_RFC1123_SUPPORTED)
 
665
#ifdef PNG_TIME_RFC1123_SUPPORTED
642
666
/* Convert the supplied time into an RFC 1123 string suitable for use in
643
667
 * a "Creation Time" or other text-based time string.
644
668
 */
649
673
        {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
650
674
         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
651
675
 
652
 
   if (png_ptr == NULL) return (NULL);
 
676
   if (png_ptr == NULL)
 
677
      return (NULL);
653
678
   if (png_ptr->time_buffer == NULL)
654
679
   {
655
680
      png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
656
681
         png_sizeof(char)));
657
682
   }
658
683
 
659
 
#if defined(_WIN32_WCE)
 
684
#ifdef _WIN32_WCE
660
685
   {
661
686
      wchar_t time_buf[29];
662
687
      wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
663
688
          ptime->day % 32, short_months[(ptime->month - 1) % 12],
664
689
        ptime->year, ptime->hour % 24, ptime->minute % 60,
665
690
          ptime->second % 61);
666
 
      WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
667
 
          NULL, NULL);
 
691
      WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer,
 
692
          29, NULL, NULL);
668
693
   }
669
694
#else
670
695
#ifdef USE_FAR_KEYWORD
693
718
png_charp PNGAPI
694
719
png_get_copyright(png_structp png_ptr)
695
720
{
696
 
   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
697
 
   return ((png_charp) "\n libpng version 1.2.35 - February 14, 2009\n\
698
 
   Copyright (c) 1998-2008 Glenn Randers-Pehrson\n\
699
 
   Copyright (c) 1996-1997 Andreas Dilger\n\
700
 
   Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
 
721
   png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
 
722
#ifdef PNG_STRING_COPYRIGHT
 
723
      return PNG_STRING_COPYRIGHT
 
724
#else
 
725
#ifdef __STDC__
 
726
   return ((png_charp) PNG_STRING_NEWLINE \
 
727
     "libpng version 1.2.46 - July 9, 2011" PNG_STRING_NEWLINE \
 
728
     "Copyright (c) 1998-2011 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
 
729
     "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
 
730
     "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
 
731
     PNG_STRING_NEWLINE);
 
732
#else
 
733
      return ((png_charp) "libpng version 1.2.46 - July 9, 2011\
 
734
      Copyright (c) 1998-2011 Glenn Randers-Pehrson\
 
735
      Copyright (c) 1996-1997 Andreas Dilger\
 
736
      Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
 
737
#endif
 
738
#endif
701
739
}
702
740
 
703
741
/* The following return the library version as a short string in the
712
750
png_get_libpng_ver(png_structp png_ptr)
713
751
{
714
752
   /* Version of *.c files used when building libpng */
715
 
   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
 
753
   png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
716
754
   return ((png_charp) PNG_LIBPNG_VER_STRING);
717
755
}
718
756
 
720
758
png_get_header_ver(png_structp png_ptr)
721
759
{
722
760
   /* Version of *.h files used when building libpng */
723
 
   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
 
761
   png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
724
762
   return ((png_charp) PNG_LIBPNG_VER_STRING);
725
763
}
726
764
 
728
766
png_get_header_version(png_structp png_ptr)
729
767
{
730
768
   /* Returns longer string containing both version and date */
731
 
   png_ptr = png_ptr;  /* silence compiler warning about unused png_ptr */
 
769
   png_ptr = png_ptr;  /* Silence compiler warning about unused png_ptr */
 
770
#ifdef __STDC__
732
771
   return ((png_charp) PNG_HEADER_VERSION_STRING
733
772
#ifndef PNG_READ_SUPPORTED
734
773
   "     (NO READ SUPPORT)"
735
774
#endif
736
 
   "\n");
 
775
   PNG_STRING_NEWLINE);
 
776
#else
 
777
   return ((png_charp) PNG_HEADER_VERSION_STRING);
 
778
#endif
737
779
}
738
780
 
739
781
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
741
783
int PNGAPI
742
784
png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
743
785
{
744
 
   /* check chunk_name and return "keep" value if it's on the list, else 0 */
 
786
   /* Check chunk_name and return "keep" value if it's on the list, else 0 */
745
787
   int i;
746
788
   png_bytep p;
747
789
   if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
758
800
int PNGAPI
759
801
png_reset_zstream(png_structp png_ptr)
760
802
{
761
 
   if (png_ptr == NULL) return Z_STREAM_ERROR;
 
803
   if (png_ptr == NULL)
 
804
      return Z_STREAM_ERROR;
762
805
   return (inflateReset(&png_ptr->zstream));
763
806
}
764
807
#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
773
816
 
774
817
 
775
818
#if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
776
 
#if !defined(PNG_1_0_X)
777
 
/* this function was added to libpng 1.2.0 */
 
819
#ifndef PNG_1_0_X
 
820
/* This function was added to libpng 1.2.0 */
778
821
int PNGAPI
779
822
png_mmx_support(void)
780
823
{
781
 
   /* obsolete, to be removed from libpng-1.4.0 */
 
824
   /* Obsolete, to be removed from libpng-1.4.0 */
782
825
    return -1;
783
826
}
784
827
#endif /* PNG_1_0_X */
791
834
png_size_t PNGAPI
792
835
png_convert_size(size_t size)
793
836
{
794
 
  if (size > (png_size_t)-1)
795
 
     PNG_ABORT();  /* We haven't got access to png_ptr, so no png_error() */
796
 
  return ((png_size_t)size);
 
837
   if (size > (png_size_t)-1)
 
838
      PNG_ABORT();  /* We haven't got access to png_ptr, so no png_error() */
 
839
   return ((png_size_t)size);
797
840
}
798
841
#endif /* PNG_SIZE_T */
799
842
 
800
843
/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
801
 
#if defined(PNG_cHRM_SUPPORTED)
802
 
#if !defined(PNG_NO_CHECK_cHRM)
 
844
#ifdef PNG_cHRM_SUPPORTED
 
845
#ifdef PNG_CHECK_cHRM_SUPPORTED
 
846
 
803
847
/*
804
 
 Multiply two 32-bit numbers, V1 and V2, using 32-bit
805
 
 arithmetic, to produce a 64 bit result in the HI/LO words.
806
 
 
807
 
          A B
808
 
        x C D
809
 
       ------
810
 
      AD || BD
811
 
AC || CB || 0
812
 
 
813
 
 where A and B are the high and low 16-bit words of V1,
814
 
 C and D are the 16-bit words of V2, AD is the product of
815
 
 A and D, and X || Y is (X << 16) + Y.
 
848
 *    Multiply two 32-bit numbers, V1 and V2, using 32-bit
 
849
 *    arithmetic, to produce a 64 bit result in the HI/LO words.
 
850
 *
 
851
 *                  A B
 
852
 *                x C D
 
853
 *               ------
 
854
 *              AD || BD
 
855
 *        AC || CB || 0
 
856
 *
 
857
 *    where A and B are the high and low 16-bit words of V1,
 
858
 *    C and D are the 16-bit words of V2, AD is the product of
 
859
 *    A and D, and X || Y is (X << 16) + Y.
816
860
*/
817
861
 
818
 
void png_64bit_product (long v1, long v2, unsigned long *hi_product,
 
862
void /* PRIVATE */
 
863
png_64bit_product (long v1, long v2, unsigned long *hi_product,
819
864
   unsigned long *lo_product)
820
865
{
821
 
 int a, b, c, d;
822
 
 long lo, hi, x, y;
823
 
 
824
 
 a = (v1 >> 16) & 0xffff;
825
 
 b = v1 & 0xffff;
826
 
 c = (v2 >> 16) & 0xffff;
827
 
 d = v2 & 0xffff;
828
 
 
829
 
 lo = b * d;                   /* BD */
830
 
 x = a * d + c * b;            /* AD + CB */
831
 
 y = ((lo >> 16) & 0xffff) + x;
832
 
 
833
 
 lo = (lo & 0xffff) | ((y & 0xffff) << 16);
834
 
 hi = (y >> 16) & 0xffff;
835
 
 
836
 
 hi += a * c;                  /* AC */
837
 
 
838
 
 *hi_product = (unsigned long)hi;
839
 
 *lo_product = (unsigned long)lo;
 
866
   int a, b, c, d;
 
867
   long lo, hi, x, y;
 
868
 
 
869
   a = (v1 >> 16) & 0xffff;
 
870
   b = v1 & 0xffff;
 
871
   c = (v2 >> 16) & 0xffff;
 
872
   d = v2 & 0xffff;
 
873
 
 
874
   lo = b * d;                   /* BD */
 
875
   x = a * d + c * b;            /* AD + CB */
 
876
   y = ((lo >> 16) & 0xffff) + x;
 
877
 
 
878
   lo = (lo & 0xffff) | ((y & 0xffff) << 16);
 
879
   hi = (y >> 16) & 0xffff;
 
880
 
 
881
   hi += a * c;                  /* AC */
 
882
 
 
883
   *hi_product = (unsigned long)hi;
 
884
   *lo_product = (unsigned long)lo;
840
885
}
841
 
int /* private */
 
886
 
 
887
int /* PRIVATE */
842
888
png_check_cHRM_fixed(png_structp png_ptr,
843
889
   png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
844
890
   png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
848
894
   unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
849
895
 
850
896
   png_debug(1, "in function png_check_cHRM_fixed");
 
897
 
851
898
   if (png_ptr == NULL)
852
899
      return 0;
853
900
 
906
953
 
907
954
   return ret;
908
955
}
909
 
#endif /* NO_PNG_CHECK_cHRM */
 
956
#endif /* PNG_CHECK_cHRM_SUPPORTED */
910
957
#endif /* PNG_cHRM_SUPPORTED */
 
958
 
 
959
void /* PRIVATE */
 
960
png_check_IHDR(png_structp png_ptr,
 
961
   png_uint_32 width, png_uint_32 height, int bit_depth,
 
962
   int color_type, int interlace_type, int compression_type,
 
963
   int filter_type)
 
964
{
 
965
   int error = 0;
 
966
 
 
967
   /* Check for width and height valid values */
 
968
   if (width == 0)
 
969
   {
 
970
      png_warning(png_ptr, "Image width is zero in IHDR");
 
971
      error = 1;
 
972
   }
 
973
 
 
974
   if (height == 0)
 
975
   {
 
976
      png_warning(png_ptr, "Image height is zero in IHDR");
 
977
      error = 1;
 
978
   }
 
979
 
 
980
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
 
981
   if (width > png_ptr->user_width_max || width > PNG_USER_WIDTH_MAX)
 
982
#else
 
983
   if (width > PNG_USER_WIDTH_MAX)
 
984
#endif
 
985
   {
 
986
      png_warning(png_ptr, "Image width exceeds user limit in IHDR");
 
987
      error = 1;
 
988
   }
 
989
 
 
990
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
 
991
   if (height > png_ptr->user_height_max || height > PNG_USER_HEIGHT_MAX)
 
992
#else
 
993
   if (height > PNG_USER_HEIGHT_MAX)
 
994
#endif
 
995
   {
 
996
      png_warning(png_ptr, "Image height exceeds user limit in IHDR");
 
997
      error = 1;
 
998
   }
 
999
 
 
1000
   if (width > PNG_UINT_31_MAX)
 
1001
   {
 
1002
      png_warning(png_ptr, "Invalid image width in IHDR");
 
1003
      error = 1;
 
1004
   }
 
1005
 
 
1006
   if ( height > PNG_UINT_31_MAX)
 
1007
   {
 
1008
      png_warning(png_ptr, "Invalid image height in IHDR");
 
1009
      error = 1;
 
1010
   }
 
1011
 
 
1012
   if ( width > (PNG_UINT_32_MAX
 
1013
                 >> 3)      /* 8-byte RGBA pixels */
 
1014
                 - 64       /* bigrowbuf hack */
 
1015
                 - 1        /* filter byte */
 
1016
                 - 7*8      /* rounding of width to multiple of 8 pixels */
 
1017
                 - 8)       /* extra max_pixel_depth pad */
 
1018
      png_warning(png_ptr, "Width is too large for libpng to process pixels");
 
1019
 
 
1020
   /* Check other values */
 
1021
   if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
 
1022
       bit_depth != 8 && bit_depth != 16)
 
1023
   {
 
1024
      png_warning(png_ptr, "Invalid bit depth in IHDR");
 
1025
      error = 1;
 
1026
   }
 
1027
 
 
1028
   if (color_type < 0 || color_type == 1 ||
 
1029
       color_type == 5 || color_type > 6)
 
1030
   {
 
1031
      png_warning(png_ptr, "Invalid color type in IHDR");
 
1032
      error = 1;
 
1033
   }
 
1034
 
 
1035
   if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
 
1036
       ((color_type == PNG_COLOR_TYPE_RGB ||
 
1037
         color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
 
1038
         color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
 
1039
   {
 
1040
      png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
 
1041
      error = 1;
 
1042
   }
 
1043
 
 
1044
   if (interlace_type >= PNG_INTERLACE_LAST)
 
1045
   {
 
1046
      png_warning(png_ptr, "Unknown interlace method in IHDR");
 
1047
      error = 1;
 
1048
   }
 
1049
 
 
1050
   if (compression_type != PNG_COMPRESSION_TYPE_BASE)
 
1051
   {
 
1052
      png_warning(png_ptr, "Unknown compression method in IHDR");
 
1053
      error = 1;
 
1054
   }
 
1055
 
 
1056
#ifdef PNG_MNG_FEATURES_SUPPORTED
 
1057
   /* Accept filter_method 64 (intrapixel differencing) only if
 
1058
    * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
 
1059
    * 2. Libpng did not read a PNG signature (this filter_method is only
 
1060
    *    used in PNG datastreams that are embedded in MNG datastreams) and
 
1061
    * 3. The application called png_permit_mng_features with a mask that
 
1062
    *    included PNG_FLAG_MNG_FILTER_64 and
 
1063
    * 4. The filter_method is 64 and
 
1064
    * 5. The color_type is RGB or RGBA
 
1065
    */
 
1066
   if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&
 
1067
       png_ptr->mng_features_permitted)
 
1068
      png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
 
1069
 
 
1070
   if (filter_type != PNG_FILTER_TYPE_BASE)
 
1071
   {
 
1072
      if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
 
1073
         (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
 
1074
         ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
 
1075
         (color_type == PNG_COLOR_TYPE_RGB ||
 
1076
         color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
 
1077
      {
 
1078
         png_warning(png_ptr, "Unknown filter method in IHDR");
 
1079
         error = 1;
 
1080
      }
 
1081
 
 
1082
      if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)
 
1083
      {
 
1084
         png_warning(png_ptr, "Invalid filter method in IHDR");
 
1085
         error = 1;
 
1086
      }
 
1087
   }
 
1088
 
 
1089
#else
 
1090
   if (filter_type != PNG_FILTER_TYPE_BASE)
 
1091
   {
 
1092
      png_warning(png_ptr, "Unknown filter method in IHDR");
 
1093
      error = 1;
 
1094
   }
 
1095
#endif
 
1096
 
 
1097
   if (error == 1)
 
1098
      png_error(png_ptr, "Invalid IHDR data");
 
1099
}
911
1100
#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */