~ubuntu-branches/ubuntu/oneiric/imagemagick/oneiric-updates

« back to all changes in this revision

Viewing changes to coders/png.c

  • Committer: Bazaar Package Importer
  • Author(s): Nelson A. de Oliveira
  • Date: 2010-06-20 19:59:55 UTC
  • mfrom: (6.2.8 sid)
  • Revision ID: james.westby@ubuntu.com-20100620195955-n3eq0yenhycw888i
Tags: 7:6.6.2.6-1
* New upstream release;
* Change Recommends on ufraw to ufraw-batch (Closes: #579775);
* Fix FTBFS when using dash to run the configure script, by setting
  CONFIG_SHELL=/bin/bash (Closes: #582073, #583024). Thank you, Niko Tyni!

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
%                               November 1997                                 %
19
19
%                                                                             %
20
20
%                                                                             %
21
 
%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
 
21
%  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
22
22
%  dedicated to making software imaging solutions freely available.           %
23
23
%                                                                             %
24
24
%  You may not use this file except in compliance with the License.  You may  %
48
48
#include "magick/cache.h"
49
49
#include "magick/color.h"
50
50
#include "magick/color-private.h"
 
51
#include "magick/colormap.h"
51
52
#include "magick/colorspace.h"
52
53
#include "magick/constitute.h"
53
54
#include "magick/enhance.h"
76
77
#include "magick/static.h"
77
78
#include "magick/statistic.h"
78
79
#include "magick/string_.h"
 
80
#include "magick/string-private.h"
79
81
#include "magick/transform.h"
80
82
#include "magick/utility.h"
81
83
#if defined(MAGICKCORE_PNG_DELEGATE)
82
84
 
83
85
/* Suppress libpng pedantic warnings that were added in
84
86
 * libpng-1.2.41 and libpng-1.4.0.  If you are working on
85
 
 * migration to libpng-2.0, remove these defines and then
 
87
 * migration to libpng-1.5, remove these defines and then
86
88
 * fix any code that generates warnings.
87
89
 */
88
 
#define PNG_DEPRECATED  /* Use of this function is deprecated */
89
 
#define PNG_USE_RESULT  /* The result of this function must be checked */
90
 
#define PNG_NORETURN    /* This function does not return */
91
 
#define PNG_ALLOCATED   /* The result of the function is new memory */
92
 
#define PNG_DEPSTRUCT   /* Access to this struct member is deprecated */
 
90
/* #define PNG_DEPRECATED   Use of this function is deprecated */
 
91
/* #define PNG_USE_RESULT   The result of this function must be checked */
 
92
/* #define PNG_NORETURN     This function does not return */
 
93
/* #define PNG_ALLOCATED    The result of the function is new memory */
 
94
/* #define PNG_DEPSTRUCT    Access to this struct member is deprecated */
93
95
 
94
96
#include "png.h"
95
97
#include "zlib.h"
97
99
/* ImageMagick differences */
98
100
#define first_scene scene
99
101
 
100
 
#if PNG_LIBPNG_VER < 10400
101
 
#    define trans_color  trans_values   /* Changed at libpng-1.4.0beta35 */
102
 
#    define trans_alpha  trans          /* Changed at libpng-1.4.0beta74 */
103
 
#else
104
 
   /* We could parse PNG_LIBPNG_VER_STRING here but it's too much bother..
105
 
    * Just don't use libpng-1.4.0beta32-34 or beta67-73
106
 
    */
107
 
#  ifndef  PNG_USER_CHUNK_CACHE_MAX     /* Added at libpng-1.4.0beta32 */
108
 
#    define trans_color  trans_values   /* Changed at libpng-1.4.0beta35 */
109
 
#  endif
110
 
#  ifndef  PNG_TRANSFORM_GRAY_TO_RGB    /* Added at libpng-1.4.0beta67 */
111
 
#    define trans_alpha  trans          /* Changed at libpng-1.4.0beta74 */
112
 
#  endif
113
 
#endif
114
 
 
115
 
#if PNG_LIBPNG_VER > 95
 
102
#if PNG_LIBPNG_VER > 10011
116
103
/*
117
104
  Optional declarations. Define or undefine them as you like.
118
105
*/
175
162
  PNG_MNG_FEATURES_SUPPORTED is disabled by default in libpng-1.0.9 and
176
163
  will be enabled by default in libpng-1.2.0.
177
164
*/
178
 
#if (PNG_LIBPNG_VER == 10009)  /* work around libpng-1.0.9 bug */
179
 
#  undef PNG_READ_EMPTY_PLTE_SUPPORTED
180
 
#  undef PNG_WRITE_EMPTY_PLTE_SUPPORTED
181
 
#endif
182
165
#ifdef PNG_MNG_FEATURES_SUPPORTED
183
166
#  ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
184
167
#    define PNG_READ_EMPTY_PLTE_SUPPORTED
189
172
#endif
190
173
 
191
174
/*
192
 
  Maximum valid unsigned long in PNG/MNG chunks is (2^31)-1
 
175
  Maximum valid size_t in PNG/MNG chunks is (2^31)-1
193
176
  This macro is only defined in libpng-1.0.3 and later.
194
177
  Previously it was PNG_MAX_UINT but that was deprecated in libpng-1.2.6
195
178
*/
279
262
typedef struct _MngBuffer
280
263
{
281
264
 
282
 
  unsigned long
 
265
  size_t
283
266
    height,
284
267
    width;
285
268
 
358
341
  int
359
342
    new_number_colors;
360
343
 
361
 
  long
 
344
  ssize_t
362
345
    image_found,
363
346
    loop_count[256],
364
347
    loop_iteration[256],
404
387
  RenderingIntent
405
388
    global_srgb_intent;
406
389
 
407
 
  unsigned long
 
390
  unsigned int
408
391
    delay,
409
392
    global_plte_length,
410
393
    global_trns_length,
436
419
    write_png32;
437
420
 
438
421
#ifdef MNG_BASI_SUPPORTED
439
 
  unsigned long
 
422
  size_t
440
423
    basi_width,
441
424
    basi_height;
442
425
 
483
466
  WriteJNGImage(const ImageInfo *,Image *);
484
467
#endif
485
468
 
486
 
static inline long MagickMax(const long x,const long y)
 
469
static inline ssize_t MagickMax(const ssize_t x,const ssize_t y)
487
470
{
488
471
  if (x > y)
489
472
    return(x);
490
473
  return(y);
491
474
}
492
 
static inline long MagickMin(const long x,const long y)
 
475
static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
493
476
{
494
477
  if (x < y)
495
478
    return(x);
496
479
  return(y);
497
480
}
498
481
 
499
 
#if PNG_LIBPNG_VER > 95
 
482
#if PNG_LIBPNG_VER > 10011
500
483
#if defined(PNG_SORT_PALETTE)
501
484
/*
502
485
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
520
503
%  A description of each parameter follows:
521
504
%
522
505
%    o image: the address of a structure of type Image.
 
506
%      This function updates image->colors and image->colormap.
523
507
%
524
508
*/
525
509
static MagickBooleanType CompressColormapTransFirst(Image *image)
528
512
    remap_needed,
529
513
    k;
530
514
 
531
 
  long
 
515
  ssize_t
532
516
    j,
533
517
    new_number_colors,
534
518
    number_colors,
538
522
    *colormap;
539
523
 
540
524
  register const IndexPacket
541
 
    *indices;
 
525
    *indexes;
542
526
 
543
527
  register const PixelPacket
544
528
    *p;
546
530
  IndexPacket
547
531
    top_used;
548
532
 
549
 
  register long
 
533
  register ssize_t
550
534
    i,
551
535
    x;
552
536
 
564
548
  assert(image != (Image *) NULL);
565
549
  assert(image->signature == MagickSignature);
566
550
  if (image->debug != MagickFalse)
567
 
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
 
551
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
552
      "    CompressColorMapTransFirst %s (%.20g colors)",image->filename,
 
553
      (double) image->colors);
568
554
  if (image->storage_class != PseudoClass || image->colors > 256 ||
569
555
      image->colors < 2)
570
 
    return(MagickFalse);
 
556
    {
 
557
      if (image->debug != MagickFalse)
 
558
        {
 
559
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
560
               "    Could not compress colormap");
 
561
          if (image->colors > 256 || image->colors == 0)
 
562
            return(MagickFalse);
 
563
          else
 
564
            return(MagickTrue);
 
565
        }
 
566
    }
571
567
  marker=(unsigned char *) AcquireQuantumMemory(image->colors,sizeof(*marker));
572
568
  if (marker == (unsigned char *) NULL)
573
569
    ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
582
578
  /*
583
579
    Mark colors that are present.
584
580
  */
585
 
  number_colors=(long) image->colors;
 
581
  number_colors=(ssize_t) image->colors;
586
582
  for (i=0; i < number_colors; i++)
587
583
  {
588
584
    marker[i]=MagickFalse;
589
585
    opacity[i]=OpaqueOpacity;
590
586
  }
591
587
  top_used=0;
592
 
  for (y=0; y < (long) image->rows; y++)
 
588
  for (y=0; y < (ssize_t) image->rows; y++)
593
589
  {
594
590
    p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
595
591
    if (p == (const PixelPacket *) NULL)
596
592
      break;
597
 
    indices=GetVirtualIndexQueue(image);
 
593
    indexes=GetVirtualIndexQueue(image);
598
594
    if (image->matte != MagickFalse)
599
 
      for (x=0; x < (long) image->columns; x++)
 
595
      for (x=0; x < (ssize_t) image->columns; x++)
600
596
      {
601
 
        marker[(int) indices[x]]=MagickTrue;
602
 
        opacity[(int) indices[x]]=p->opacity;
603
 
        if (indices[x] > top_used)
604
 
           top_used=indices[x];
 
597
        marker[(int) indexes[x]]=MagickTrue;
 
598
        opacity[(int) indexes[x]]=GetOpacityPixelComponent(p);
 
599
        if (indexes[x] > top_used)
 
600
           top_used=indexes[x];
605
601
        p++;
606
602
      }
607
603
    else
608
 
      for (x=0; x < (long) image->columns; x++)
 
604
      for (x=0; x < (ssize_t) image->columns; x++)
609
605
      {
610
 
        marker[(int) indices[x]]=MagickTrue;
611
 
        if (indices[x] > top_used)
612
 
           top_used=indices[x];
 
606
        marker[(int) indexes[x]]=MagickTrue;
 
607
        if (indexes[x] > top_used)
 
608
           top_used=indexes[x];
613
609
      }
614
610
  }
615
611
 
663
659
    }
664
660
 
665
661
  remap_needed=MagickFalse;
666
 
  if ((long) top_used >= new_number_colors)
 
662
  if ((ssize_t) top_used >= new_number_colors)
667
663
     remap_needed=MagickTrue;
668
664
 
669
665
  /*
733
729
 
734
730
            temp_colormap=colormap[0];
735
731
            colormap[0]=colormap[(int) map[i]];
736
 
            colormap[(long) map[i]]=temp_colormap;
 
732
            colormap[(ssize_t) map[i]]=temp_colormap;
737
733
            for (j=0; j < number_colors; j++)
738
734
            {
739
735
              if (map[j] == 0)
765
761
        Remap pixels.
766
762
      */
767
763
      exception=(&image->exception);
768
 
      for (y=0; y < (long) image->rows; y++)
 
764
      for (y=0; y < (ssize_t) image->rows; y++)
769
765
      {
770
766
        q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
771
767
        if (q == (PixelPacket *) NULL)
772
768
          break;
773
769
        pixels=GetAuthenticIndexQueue(image);
774
 
        for (x=0; x < (long) image->columns; x++)
 
770
        for (x=0; x < (ssize_t) image->columns; x++)
775
771
        {
776
772
          j=(int) pixels[x];
777
773
          pixels[x]=map[j];
783
779
        image->colormap[i]=colormap[i];
784
780
    }
785
781
  colormap=(PixelPacket *) RelinquishMagickMemory(colormap);
786
 
  image->colors=(unsigned long) new_number_colors;
 
782
  image->colors=(size_t) new_number_colors;
787
783
  map=(IndexPacket *) RelinquishMagickMemory(map);
788
784
  return(MagickTrue);
789
785
}
809
805
  register const PixelPacket
810
806
    *p;
811
807
 
812
 
  register long
 
808
  register ssize_t
813
809
    i,
814
810
    x,
815
811
    y;
821
817
 
822
818
  if (image->storage_class == PseudoClass)
823
819
    {
824
 
      for (i=0; i < (long) image->colors; i++)
 
820
      for (i=0; i < (ssize_t) image->colors; i++)
825
821
        if (IsGray(image->colormap+i) == MagickFalse)
826
822
          return(MagickFalse);
827
823
      return(MagickTrue);
828
824
    }
829
 
  for (y=0; y < (long) image->rows; y++)
 
825
  for (y=0; y < (ssize_t) image->rows; y++)
830
826
  {
831
827
    p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
832
828
    if (p == (const PixelPacket *) NULL)
833
829
      return(MagickFalse);
834
 
    for (x=(long) image->columns-1; x >= 0; x--)
 
830
    for (x=(ssize_t) image->columns-1; x >= 0; x--)
835
831
    {
836
832
       if (IsGray(p) == MagickFalse)
837
833
          return(MagickFalse);
862
858
  register const PixelPacket
863
859
    *p;
864
860
 
865
 
  register long
 
861
  register ssize_t
866
862
    i,
867
863
    x,
868
864
    y;
873
869
    (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
874
870
  if (image->storage_class == PseudoClass)
875
871
    {
876
 
      for (i=0; i < (long) image->colors; i++)
 
872
      for (i=0; i < (ssize_t) image->colors; i++)
877
873
      {
878
874
        if ((IsGray(image->colormap+i) == MagickFalse) ||
879
875
            ((image->colormap[i].red != 0) &&
882
878
      }
883
879
      return(MagickTrue);
884
880
    }
885
 
  for (y=0; y < (long) image->rows; y++)
 
881
  for (y=0; y < (ssize_t) image->rows; y++)
886
882
  {
887
883
    p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
888
884
    if (p == (const PixelPacket *) NULL)
889
885
      return(MagickFalse);
890
 
    for (x=(long) image->columns-1; x >= 0; x--)
 
886
    for (x=(ssize_t) image->columns-1; x >= 0; x--)
891
887
    {
892
888
      if ((p->red != 0) && (p->red != (Quantum) QuantumRange))
893
889
        return(MagickFalse);
901
897
  }
902
898
  return(MagickTrue);
903
899
}
904
 
#endif /* PNG_LIBPNG_VER > 95 */
 
900
#endif /* PNG_LIBPNG_VER > 10011 */
905
901
#endif /* MAGICKCORE_PNG_DELEGATE */
906
902
 
907
903
/*
1013
1009
extern "C" {
1014
1010
#endif
1015
1011
 
1016
 
#if (PNG_LIBPNG_VER > 95)
1017
 
static size_t WriteBlobMSBULong(Image *image,const unsigned long value)
 
1012
#if (PNG_LIBPNG_VER > 10011)
 
1013
static size_t WriteBlobMSBULong(Image *image,const size_t value)
1018
1014
{
1019
1015
  unsigned char
1020
1016
    buffer[4];
1059
1055
{
1060
1056
  if (logging != MagickFalse)
1061
1057
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1062
 
      "  Writing %c%c%c%c chunk, length: %lu",
1063
 
      type[0],type[1],type[2],type[3],(unsigned long) length);
 
1058
      "  Writing %c%c%c%c chunk, length: %.20g",
 
1059
      type[0],type[1],type[2],type[3],(double) length);
1064
1060
}
1065
 
#endif /* PNG_LIBPNG_VER > 95 */
 
1061
#endif /* PNG_LIBPNG_VER > 10011 */
1066
1062
 
1067
1063
#if defined(__cplusplus) || defined(c_plusplus)
1068
1064
}
1069
1065
#endif
1070
1066
 
1071
 
#if PNG_LIBPNG_VER > 95
 
1067
#if PNG_LIBPNG_VER > 10011
1072
1068
/*
1073
1069
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1074
1070
%                                                                             %
1182
1178
            msg[MaxTextExtent];
1183
1179
 
1184
1180
          (void) FormatMagickString(msg,MaxTextExtent,
1185
 
            "Expected %lu bytes; found %lu bytes",(unsigned long) length,
1186
 
            (unsigned long) check);
 
1181
            "Expected %.20g bytes; found %.20g bytes",(double) length,
 
1182
            (double) check);
1187
1183
          png_warning(png_ptr,msg);
1188
1184
          png_error(png_ptr,"Read Exception");
1189
1185
        }
1211
1207
  png_size_t
1212
1208
    check;
1213
1209
 
1214
 
  register long
 
1210
  register ssize_t
1215
1211
    i;
1216
1212
 
1217
1213
  i=0;
1284
1280
      png_size_t
1285
1281
        check;
1286
1282
 
1287
 
      check=(png_size_t) WriteBlob(image,(unsigned long) length,data);
 
1283
      check=(png_size_t) WriteBlob(image,(size_t) length,data);
1288
1284
      if (check != length)
1289
1285
        png_error(png_ptr,"WriteBlob Failed");
1290
1286
    }
1298
1294
#ifdef PNG_WRITE_EMPTY_PLTE_SUPPORTED
1299
1295
static int PalettesAreEqual(Image *a,Image *b)
1300
1296
{
1301
 
  long
 
1297
  ssize_t
1302
1298
    i;
1303
1299
 
1304
1300
  if ((a == (Image *) NULL) || (b == (Image *) NULL))
1307
1303
    return((int) MagickFalse);
1308
1304
  if (a->colors != b->colors)
1309
1305
    return((int) MagickFalse);
1310
 
  for (i=0; i < (long) a->colors; i++)
 
1306
  for (i=0; i < (ssize_t) a->colors; i++)
1311
1307
  {
1312
1308
    if ((a->colormap[i].red != b->colormap[i].red) ||
1313
1309
        (a->colormap[i].green != b->colormap[i].green) ||
1344
1340
      mng_info->x_off[i]=0;
1345
1341
      mng_info->y_off[i]=0;
1346
1342
      mng_info->object_clip[i].left=0;
1347
 
      mng_info->object_clip[i].right=(long) PNG_UINT_31_MAX;
 
1343
      mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX;
1348
1344
      mng_info->object_clip[i].top=0;
1349
 
      mng_info->object_clip[i].bottom=(long) PNG_UINT_31_MAX;
 
1345
      mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX;
1350
1346
    }
1351
1347
}
1352
1348
 
1354
1350
{
1355
1351
  if (*have_mng_structure && (mng_info != (MngInfo *) NULL))
1356
1352
    {
1357
 
      register long
 
1353
      register ssize_t
1358
1354
        i;
1359
1355
 
1360
1356
      for (i=1; i < MNG_MAX_OBJECTS; i++)
1392
1388
  /*
1393
1389
    Read clipping boundaries from DEFI, CLIP, FRAM, or PAST chunk.
1394
1390
  */
1395
 
  box.left=(long) ((p[0]  << 24) | (p[1]  << 16) | (p[2]  << 8) | p[3]);
1396
 
  box.right=(long) ((p[4]  << 24) | (p[5]  << 16) | (p[6]  << 8) | p[7]);
1397
 
  box.top=(long) ((p[8]  << 24) | (p[9]  << 16) | (p[10] << 8) | p[11]);
1398
 
  box.bottom=(long) ((p[12] << 24) | (p[13] << 16) | (p[14] << 8) | p[15]);
 
1391
  box.left=(ssize_t) ((p[0]  << 24) | (p[1]  << 16) | (p[2]  << 8) | p[3]);
 
1392
  box.right=(ssize_t) ((p[4]  << 24) | (p[5]  << 16) | (p[6]  << 8) | p[7]);
 
1393
  box.top=(ssize_t) ((p[8]  << 24) | (p[9]  << 16) | (p[10] << 8) | p[11]);
 
1394
  box.bottom=(ssize_t) ((p[12] << 24) | (p[13] << 16) | (p[14] << 8) | p[15]);
1399
1395
  if (delta_type != 0)
1400
1396
    {
1401
1397
      box.left+=previous_box.left;
1412
1408
  MngPair
1413
1409
    pair;
1414
1410
  /*
1415
 
    Read two longs from CLON, MOVE or PAST chunk
 
1411
    Read two ssize_ts from CLON, MOVE or PAST chunk
1416
1412
  */
1417
1413
  pair.a=(long) ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]);
1418
1414
  pair.b=(long) ((p[4] << 24) | (p[5] << 16) | (p[6] << 8) | p[7]);
1440
1436
      "  libpng-%s error: %s", PNG_LIBPNG_VER_STRING,message);
1441
1437
  (void) ThrowMagickException(&image->exception,GetMagickModule(),CoderError,
1442
1438
    message,"`%s'",image->filename);
 
1439
#if PNG_LIBPNG_VER < 10500
1443
1440
  longjmp(ping->jmpbuf,1);
 
1441
#else
 
1442
  png_longjmp(ping,1);
 
1443
#endif
1444
1444
}
1445
1445
 
1446
1446
static void PNGWarningHandler(png_struct *ping,png_const_charp message)
1453
1453
  image=(Image *) png_get_error_ptr(ping);
1454
1454
  if (image->debug != MagickFalse)
1455
1455
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1456
 
      "  libpng-%s warning: %s", PNG_LIBPNG_VER_STRING,
1457
 
      message);
 
1456
      "  libpng-%s warning: %s", PNG_LIBPNG_VER_STRING,message);
1458
1457
  (void) ThrowMagickException(&image->exception,GetMagickModule(),CoderWarning,
1459
1458
    message,"`%s'",image->filename);
1460
1459
}
1496
1495
png_read_raw_profile(Image *image, const ImageInfo *image_info,
1497
1496
   png_textp text,int ii)
1498
1497
{
1499
 
  register long
 
1498
  register ssize_t
1500
1499
    i;
1501
1500
 
1502
1501
  register unsigned char
1527
1526
  /* look for length */
1528
1527
  while (*sp == '\0' || *sp == ' ' || *sp == '\n')
1529
1528
     sp++;
1530
 
  length=(png_uint_32) atol(sp);
 
1529
  length=(png_uint_32) StringToLong(sp);
1531
1530
  while (*sp != ' ' && *sp != '\n')
1532
1531
     sp++;
1533
1532
  /* allocate space */
1548
1547
  /* copy profile, skipping white space and column 1 "=" signs */
1549
1548
  dp=GetStringInfoDatum(profile);
1550
1549
  nibbles=length*2;
1551
 
  for (i=0; i < (long) nibbles; i++)
 
1550
  for (i=0; i < (ssize_t) nibbles; i++)
1552
1551
  {
1553
1552
    while (*sp < '0' || (*sp > '9' && *sp < 'a') || *sp > 'f')
1554
1553
    {
1606
1605
 
1607
1606
  image=(Image *) png_get_user_chunk_ptr(ping);
1608
1607
 
1609
 
  image->page.width=(unsigned long) ((chunk->data[0] << 24) |
 
1608
  image->page.width=(size_t) ((chunk->data[0] << 24) |
1610
1609
     (chunk->data[1] << 16) | (chunk->data[2] << 8) | chunk->data[3]);
1611
 
  image->page.height=(unsigned long) ((chunk->data[4] << 24) |
 
1610
  image->page.height=(size_t) ((chunk->data[4] << 24) |
1612
1611
     (chunk->data[5] << 16) | (chunk->data[6] << 8) | chunk->data[7]);
1613
1612
 
1614
1613
  /* Return one of the following: */
1663
1662
    logging,
1664
1663
    num_text,
1665
1664
    num_passes,
1666
 
    pass;
 
1665
    pass,
 
1666
    ping_bit_depth,
 
1667
    ping_color_type,
 
1668
    ping_interlace_method,
 
1669
    ping_compression_method,
 
1670
    ping_filter_method,
 
1671
    ping_num_trans;
1667
1672
 
1668
1673
  MagickBooleanType
1669
1674
    status;
1671
1676
  PixelPacket
1672
1677
    transparent_color;
1673
1678
 
 
1679
  png_bytep
 
1680
     ping_trans_alpha;
 
1681
 
 
1682
  png_color_16p
 
1683
     ping_background,
 
1684
     ping_trans_color;
 
1685
 
1674
1686
  png_info
1675
1687
    *end_info,
1676
1688
    *ping_info;
1681
1693
  png_textp
1682
1694
    text;
1683
1695
 
 
1696
  png_uint_32
 
1697
    ping_height,
 
1698
    ping_width,
 
1699
    ping_rowbytes;
 
1700
 
1684
1701
  QuantumInfo
1685
1702
    *quantum_info;
1686
1703
 
1687
1704
  unsigned char
1688
1705
    *png_pixels;
1689
1706
 
1690
 
  long
 
1707
  ssize_t
1691
1708
    y;
1692
1709
 
1693
1710
  register unsigned char
1694
1711
    *p;
1695
1712
 
1696
1713
  register IndexPacket
1697
 
    *indices;
 
1714
    *indexes;
1698
1715
 
1699
 
  register long
 
1716
  register ssize_t
1700
1717
    i,
1701
1718
    x;
1702
1719
 
1706
1723
  size_t
1707
1724
    length;
1708
1725
 
1709
 
  unsigned long
 
1726
  size_t
1710
1727
    row_offset;
1711
1728
 
1712
1729
#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1725
1742
    "  enter ReadOnePNGImage()");
1726
1743
 
1727
1744
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
1728
 
  (void) LockSemaphoreInfo(png_semaphore);
 
1745
  LockSemaphoreInfo(png_semaphore);
1729
1746
#endif
1730
1747
 
1731
 
#if (PNG_LIBPNG_VER < 10007)
 
1748
#if (PNG_LIBPNG_VER < 10200)
1732
1749
  if (image_info->verbose)
1733
1750
    printf("Your PNG library (libpng-%s) is rather old.\n",
1734
1751
       PNG_LIBPNG_VER_STRING);
1775
1792
      ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
1776
1793
    }
1777
1794
  png_pixels=(unsigned char *) NULL;
1778
 
  if (setjmp(ping->jmpbuf))
 
1795
  if (setjmp(png_jmpbuf(ping)))
1779
1796
    {
1780
1797
      /*
1781
1798
        PNG image is corrupt.
1782
1799
      */
1783
1800
      png_destroy_read_struct(&ping,&ping_info,&end_info);
1784
1801
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
1785
 
      (void) UnlockSemaphoreInfo(png_semaphore);
 
1802
      UnlockSemaphoreInfo(png_semaphore);
1786
1803
#endif
1787
1804
      if (logging != MagickFalse)
1788
1805
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1789
1806
          "  exit ReadOnePNGImage() with error.");
1790
1807
      if (image != (Image *) NULL)
1791
 
        image->columns=0;
 
1808
        {
 
1809
          InheritException(exception,&image->exception);
 
1810
          image->columns=0;
 
1811
        }
1792
1812
      return(GetFirstImageInList(image));
1793
1813
    }
1794
1814
  /*
1795
1815
    Prepare PNG for reading.
1796
1816
  */
 
1817
 
1797
1818
  mng_info->image_found++;
1798
1819
  png_set_sig_bytes(ping,8);
1799
1820
  if (LocaleCompare(image_info->magick,"MNG") == 0)
1827
1848
  png_set_read_user_chunk_fn(ping, image, read_vpag_chunk_callback);
1828
1849
#endif
1829
1850
 
1830
 
#if defined(PNG_USE_PNGGCCRD) && defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \
1831
 
 (PNG_LIBPNG_VER >= 10200) && (PNG_LIBPNG_VER < 10220) && defined(__i386__)
 
1851
#if (PNG_LIBPNG_VER < 10400)
 
1852
#  if defined(PNG_USE_PNGGCCRD) && defined(PNG_ASSEMBLER_CODE_SUPPORTED) && \
 
1853
   (PNG_LIBPNG_VER >= 10200) && (PNG_LIBPNG_VER < 10220) && defined(__i386__)
1832
1854
  /* Disable thread-unsafe features of pnggccrd */
1833
1855
  if (png_access_version_number() >= 10200)
1834
1856
  {
1842
1864
    asm_flags=png_get_asm_flags(ping);
1843
1865
    png_set_asm_flags(ping, asm_flags & ~mmx_disable_mask);
1844
1866
  }
 
1867
#  endif
1845
1868
#endif
1846
1869
 
1847
1870
  png_read_info(ping,ping_info);
1848
 
  if (ping_info->bit_depth < 8)
 
1871
 
 
1872
  png_get_IHDR(ping,ping_info,&ping_width,&ping_height,
 
1873
               &ping_bit_depth,&ping_color_type,
 
1874
               &ping_interlace_method,&ping_compression_method,
 
1875
               &ping_filter_method);
 
1876
 
 
1877
  (void) png_get_tRNS(ping, ping_info, &ping_trans_alpha, &ping_num_trans,
 
1878
                      &ping_trans_color);
 
1879
 
 
1880
  (void) png_get_bKGD(ping, ping_info, &ping_background);
 
1881
 
 
1882
  if (ping_bit_depth < 8)
1849
1883
    {
1850
 
      if (((int) ping_info->color_type == PNG_COLOR_TYPE_PALETTE))
1851
 
        png_set_packing(ping);
 
1884
      if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE))
 
1885
        {
 
1886
          png_set_packing(ping);
 
1887
          ping_bit_depth = 8;
 
1888
        }
1852
1889
    }
1853
 
  image->depth=ping_info->bit_depth;
 
1890
 
 
1891
  image->depth=ping_bit_depth;
1854
1892
  image->depth=GetImageQuantumDepth(image,MagickFalse);
1855
 
  image->interlace=ping_info->interlace_type != 0 ? PNGInterlace : NoInterlace;
 
1893
  image->interlace=ping_interlace_method != 0 ? PNGInterlace : NoInterlace;
1856
1894
  if (logging != MagickFalse)
1857
1895
    {
1858
1896
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1859
 
        "    PNG width: %lu, height: %lu",
1860
 
        ping_info->width, ping_info->height);
 
1897
        "    PNG width: %.20g, height: %.20g",
 
1898
        (double) ping_width, (double) ping_height);
1861
1899
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1862
1900
        "    PNG color_type: %d, bit_depth: %d",
1863
 
        ping_info->color_type, ping_info->bit_depth);
 
1901
        ping_color_type, ping_bit_depth);
1864
1902
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1865
1903
        "    PNG compression_method: %d",
1866
 
        ping_info->compression_type);
 
1904
        ping_compression_method);
1867
1905
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1868
1906
        "    PNG interlace_method: %d, filter_method: %d",
1869
 
        ping_info->interlace_type,ping_info->filter_type);
 
1907
        ping_interlace_method,ping_filter_method);
1870
1908
    }
1871
1909
 
1872
 
#if (PNG_LIBPNG_VER > 10008) && defined(PNG_READ_iCCP_SUPPORTED)
1873
 
  if (ping_info->valid & PNG_INFO_iCCP)
 
1910
#ifdef PNG_READ_iCCP_SUPPORTED
 
1911
  if (png_get_valid(ping,ping_info,PNG_INFO_iCCP))
1874
1912
    {
1875
1913
      int
1876
1914
        compression;
1920
1958
     double
1921
1959
        file_gamma;
1922
1960
 
1923
 
     if (mng_info->have_global_gama)
1924
 
       image->gamma=mng_info->global_gamma;
 
1961
     if (!png_get_gAMA(ping,ping_info,&file_gamma))
 
1962
       if (mng_info->have_global_gama)
 
1963
         png_set_gAMA(ping,ping_info,mng_info->global_gamma);
1925
1964
     if (png_get_gAMA(ping,ping_info,&file_gamma))
1926
1965
       {
1927
1966
         image->gamma=(float) file_gamma;
1930
1969
             "    Reading PNG gAMA chunk: gamma: %f",file_gamma);
1931
1970
       }
1932
1971
  }
1933
 
  if (mng_info->have_global_chrm != MagickFalse)
1934
 
    image->chromaticity=mng_info->global_chrm;
1935
 
  if (ping_info->valid & PNG_INFO_cHRM)
 
1972
  if (!png_get_valid(ping,ping_info,PNG_INFO_cHRM))
 
1973
    {
 
1974
      if (mng_info->have_global_chrm != MagickFalse)
 
1975
        {
 
1976
          (void) png_set_cHRM(ping,ping_info,
 
1977
            mng_info->global_chrm.white_point.x,
 
1978
            mng_info->global_chrm.white_point.y,
 
1979
            mng_info->global_chrm.red_primary.x,
 
1980
            mng_info->global_chrm.red_primary.y,
 
1981
            mng_info->global_chrm.green_primary.x,
 
1982
            mng_info->global_chrm.green_primary.y,
 
1983
            mng_info->global_chrm.blue_primary.x,
 
1984
            mng_info->global_chrm.blue_primary.y);
 
1985
        }
 
1986
    }
 
1987
  if (png_get_valid(ping,ping_info,PNG_INFO_cHRM))
1936
1988
    {
1937
1989
      (void) png_get_cHRM(ping,ping_info,
1938
1990
        &image->chromaticity.white_point.x,
1949
2001
    }
1950
2002
  if (image->rendering_intent)
1951
2003
    {
1952
 
      image->gamma=0.45455f;
1953
 
      image->chromaticity.red_primary.x=0.6400f;
1954
 
      image->chromaticity.red_primary.y=0.3300f;
1955
 
      image->chromaticity.green_primary.x=0.3000f;
1956
 
      image->chromaticity.green_primary.y=0.6000f;
1957
 
      image->chromaticity.blue_primary.x=0.1500f;
1958
 
      image->chromaticity.blue_primary.y=0.0600f;
1959
 
      image->chromaticity.white_point.x=0.3127f;
1960
 
      image->chromaticity.white_point.y=0.3290f;
 
2004
      png_set_sRGB(ping,ping_info,image->rendering_intent-1);
 
2005
      png_set_gAMA(ping,ping_info,0.45455f);
 
2006
      png_set_cHRM(ping,ping_info,
 
2007
                  0.6400f, 0.3300f, 0.3000f, 0.6000f,
 
2008
                  0.1500f, 0.0600f, 0.3127f, 0.3290f);
1961
2009
    }
1962
 
  if ((mng_info->have_global_gama != MagickFalse) || image->rendering_intent)
1963
 
    ping_info->valid|=PNG_INFO_gAMA;
1964
 
  if ((mng_info->have_global_chrm != MagickFalse) || image->rendering_intent)
1965
 
    ping_info->valid|=PNG_INFO_cHRM;
1966
2010
#if defined(PNG_oFFs_SUPPORTED)
1967
 
  if (ping_info->valid & PNG_INFO_oFFs)
 
2011
  if (png_get_valid(ping,ping_info,PNG_INFO_oFFs))
1968
2012
    {
1969
2013
      image->page.x=png_get_x_offset_pixels(ping, ping_info);
1970
2014
      image->page.y=png_get_y_offset_pixels(ping, ping_info);
1971
2015
      if (logging != MagickFalse)
1972
2016
        if (image->page.x || image->page.y)
1973
2017
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
1974
 
            "    Reading PNG oFFs chunk: x: %ld, y: %ld.",image->page.x,
1975
 
            image->page.y);
 
2018
            "    Reading PNG oFFs chunk: x: %.20g, y: %.20g.",(double)
 
2019
            image->page.x,(double) image->page.y);
1976
2020
    }
1977
2021
#endif
1978
2022
#if defined(PNG_pHYs_SUPPORTED)
1979
 
  if (ping_info->valid & PNG_INFO_pHYs)
 
2023
  if (!png_get_valid(ping,ping_info,PNG_INFO_pHYs))
 
2024
    {
 
2025
      if (mng_info->have_global_phys)
 
2026
        {
 
2027
          png_set_pHYs(ping,ping_info,
 
2028
                       mng_info->global_x_pixels_per_unit,
 
2029
                       mng_info->global_y_pixels_per_unit,
 
2030
                       mng_info->global_phys_unit_type);
 
2031
        }
 
2032
    }
 
2033
 
 
2034
  if (png_get_valid(ping,ping_info,PNG_INFO_pHYs))
1980
2035
    {
1981
2036
      int
1982
2037
        unit_type;
1989
2044
        Set image resolution.
1990
2045
      */
1991
2046
      (void) png_get_pHYs(ping,ping_info,&x_resolution,&y_resolution,
1992
 
          &unit_type);
1993
 
      image->x_resolution=(float) x_resolution;
1994
 
      image->y_resolution=(float) y_resolution;
 
2047
        &unit_type);
 
2048
      image->x_resolution=(double) x_resolution;
 
2049
      image->y_resolution=(double) y_resolution;
1995
2050
      if (unit_type == PNG_RESOLUTION_METER)
1996
2051
        {
1997
2052
          image->units=PixelsPerCentimeterResolution;
2000
2055
        }
2001
2056
      if (logging != MagickFalse)
2002
2057
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2003
 
          "    Reading PNG pHYs chunk: xres: %lu, yres: %lu, units: %d.",
2004
 
          x_resolution, y_resolution, unit_type);
2005
 
    }
2006
 
  else
2007
 
    {
2008
 
      if (mng_info->have_global_phys)
2009
 
        {
2010
 
          image->x_resolution=(float) mng_info->global_x_pixels_per_unit;
2011
 
          image->y_resolution=(float) mng_info->global_y_pixels_per_unit;
2012
 
          if (mng_info->global_phys_unit_type == PNG_RESOLUTION_METER)
2013
 
            {
2014
 
              image->units=PixelsPerCentimeterResolution;
2015
 
              image->x_resolution=(double)
2016
 
                mng_info->global_x_pixels_per_unit/100.0;
2017
 
              image->y_resolution=(double)
2018
 
                mng_info->global_y_pixels_per_unit/100.0;
2019
 
            }
2020
 
          ping_info->valid|=PNG_INFO_pHYs;
2021
 
        }
 
2058
          "    Reading PNG pHYs chunk: xres: %.20g, yres: %.20g, units: %d.",
 
2059
          (double) x_resolution,(double) y_resolution,unit_type);
2022
2060
    }
2023
2061
#endif
2024
 
  if (ping_info->valid & PNG_INFO_PLTE)
 
2062
  if (png_get_valid(ping,ping_info,PNG_INFO_PLTE))
2025
2063
    {
2026
2064
      int
2027
2065
        number_colors;
2031
2069
 
2032
2070
      (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
2033
2071
      if ((number_colors == 0) &&
2034
 
          ((int) ping_info->color_type == PNG_COLOR_TYPE_PALETTE))
 
2072
          ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE))
2035
2073
        {
2036
2074
          if (mng_info->global_plte_length)
2037
2075
            {
2038
2076
              png_set_PLTE(ping,ping_info,mng_info->global_plte,
2039
2077
                (int) mng_info->global_plte_length);
2040
 
              if ((ping_info->valid & PNG_INFO_tRNS) == 0)
 
2078
              if (!png_get_valid(ping,ping_info,PNG_INFO_tRNS))
2041
2079
                if (mng_info->global_trns_length)
2042
2080
                  {
2043
2081
                    if (mng_info->global_trns_length >
2054
2092
#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
2055
2093
                   mng_info->have_saved_bkgd_index ||
2056
2094
#endif
2057
 
                   ping_info->valid & PNG_INFO_bKGD)
 
2095
                   png_get_valid(ping,ping_info,PNG_INFO_bKGD))
2058
2096
                    {
2059
2097
                      png_color_16
2060
2098
                         background;
2062
2100
#ifndef PNG_READ_EMPTY_PLTE_SUPPORTED
2063
2101
                      if (mng_info->have_saved_bkgd_index)
2064
2102
                        background.index=mng_info->saved_bkgd_index;
2065
 
                      else
2066
2103
#endif
2067
 
                        background.index=ping_info->background.index;
 
2104
                      if (png_get_valid(ping, ping_info, PNG_INFO_bKGD))
 
2105
                        background.index=ping_background->index;
2068
2106
                      background.red=(png_uint_16)
2069
2107
                        mng_info->global_plte[background.index].red;
2070
2108
                      background.green=(png_uint_16)
2083
2121
        }
2084
2122
 
2085
2123
#if defined(PNG_READ_bKGD_SUPPORTED)
2086
 
  if (mng_info->have_global_bkgd && !(ping_info->valid & PNG_INFO_bKGD))
 
2124
  if (mng_info->have_global_bkgd &&
 
2125
          (!png_get_valid(ping,ping_info,PNG_INFO_bKGD)))
2087
2126
      image->background_color=mng_info->mng_global_bkgd;
2088
 
  if (ping_info->valid & PNG_INFO_bKGD)
 
2127
  if (png_get_valid(ping,ping_info,PNG_INFO_bKGD))
2089
2128
    {
2090
2129
      /*
2091
2130
        Set image background color.
2093
2132
      if (logging != MagickFalse)
2094
2133
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2095
2134
          "    Reading PNG bKGD chunk.");
2096
 
      if (ping_info->bit_depth <= MAGICKCORE_QUANTUM_DEPTH)
 
2135
      if (ping_bit_depth == MAGICKCORE_QUANTUM_DEPTH)
2097
2136
        {
2098
 
          image->background_color.red=ping_info->background.red;
2099
 
          image->background_color.green=ping_info->background.green;
2100
 
          image->background_color.blue=ping_info->background.blue;
 
2137
          image->background_color.red=ping_background->red;
 
2138
          image->background_color.green=ping_background->green;
 
2139
          image->background_color.blue=ping_background->blue;
2101
2140
        }
2102
 
      else
 
2141
      else /* Scale background components to 16-bit */
2103
2142
        {
 
2143
          unsigned int
 
2144
            bkgd_scale;
 
2145
 
 
2146
          if (logging != MagickFalse)
 
2147
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
2148
              "    raw ping_background=(%d,%d,%d).",ping_background->red,
 
2149
              ping_background->green,ping_background->blue);
 
2150
 
 
2151
          bkgd_scale = 1;
 
2152
          if (ping_bit_depth == 1)
 
2153
             bkgd_scale = 255;
 
2154
          else if (ping_bit_depth == 2)
 
2155
             bkgd_scale = 85;
 
2156
          else if (ping_bit_depth == 4)
 
2157
             bkgd_scale = 17;
 
2158
          if (ping_bit_depth <= 8)
 
2159
             bkgd_scale *= 257;
 
2160
 
 
2161
          ping_background->red *= bkgd_scale;
 
2162
          ping_background->green *= bkgd_scale;
 
2163
          ping_background->blue *= bkgd_scale;
 
2164
 
 
2165
          if (logging != MagickFalse)
 
2166
            {
 
2167
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
2168
              "    bkgd_scale=%d.",bkgd_scale);
 
2169
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
2170
              "    ping_background=(%d,%d,%d).",ping_background->red,
 
2171
              ping_background->green,ping_background->blue);
 
2172
            }
 
2173
 
2104
2174
          image->background_color.red=
2105
 
            ScaleShortToQuantum(ping_info->background.red);
 
2175
            ScaleShortToQuantum(ping_background->red);
2106
2176
          image->background_color.green=
2107
 
            ScaleShortToQuantum(ping_info->background.green);
 
2177
            ScaleShortToQuantum(ping_background->green);
2108
2178
          image->background_color.blue=
2109
 
            ScaleShortToQuantum(ping_info->background.blue);
 
2179
            ScaleShortToQuantum(ping_background->blue);
 
2180
 
 
2181
          if (logging != MagickFalse)
 
2182
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
2183
              "    image->background_color=(%.20g,%.20g,%.20g).",
 
2184
              (double) image->background_color.red,
 
2185
              (double) image->background_color.green,
 
2186
              (double) image->background_color.blue);
2110
2187
        }
2111
2188
    }
2112
2189
#endif
2114
2191
  transparent_color.green=0;
2115
2192
  transparent_color.blue=0;
2116
2193
  transparent_color.opacity=0;
2117
 
  if (ping_info->valid & PNG_INFO_tRNS)
 
2194
  if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
2118
2195
    {
2119
2196
      /*
2120
2197
        Image has a transparent background.
2122
2199
      int
2123
2200
        max_sample;
2124
2201
 
 
2202
      size_t
 
2203
        one=1;
 
2204
 
2125
2205
      if (logging != MagickFalse)
2126
2206
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2127
2207
          "    Reading PNG tRNS chunk.");
2128
2208
 
2129
 
      max_sample = (1 << ping_info->bit_depth) - 1;
 
2209
      max_sample = (int) ((one << ping_bit_depth) - 1);
2130
2210
 
2131
 
      if ((ping_info->color_type == PNG_COLOR_TYPE_GRAY &&
2132
 
          (int)ping_info->trans_color.gray > max_sample) ||
2133
 
          (ping_info->color_type == PNG_COLOR_TYPE_RGB &&
2134
 
          ((int)ping_info->trans_color.red > max_sample ||
2135
 
          (int)ping_info->trans_color.green > max_sample ||
2136
 
          (int)ping_info->trans_color.blue > max_sample)))
 
2211
      if ((ping_color_type == PNG_COLOR_TYPE_GRAY &&
 
2212
          (int)ping_trans_color->gray > max_sample) ||
 
2213
          (ping_color_type == PNG_COLOR_TYPE_RGB &&
 
2214
          ((int)ping_trans_color->red > max_sample ||
 
2215
          (int)ping_trans_color->green > max_sample ||
 
2216
          (int)ping_trans_color->blue > max_sample)))
2137
2217
        {
2138
2218
          if (logging != MagickFalse)
2139
2219
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2140
2220
              "    Ignoring PNG tRNS chunk with out-of-range sample.");
2141
 
#if (PNG_LIBPNG_VER < 10007)
2142
 
          ping_info->trans_alpha=(unsigned char *) RelinquishMagickMemory(
2143
 
            ping_info->trans_alpha);
2144
 
          ping_info->valid&=(~PNG_INFO_tRNS);
2145
 
#else
2146
2221
          png_free_data(ping, ping_info, PNG_FREE_TRNS, 0);
2147
 
#endif
 
2222
          png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
2148
2223
          image->matte=MagickFalse;
2149
2224
        }
2150
2225
      else
2151
2226
        {
2152
 
          transparent_color.red= (Quantum)(ping_info->trans_color.red);
2153
 
          transparent_color.green= (Quantum) (ping_info->trans_color.green);
2154
 
          transparent_color.blue= (Quantum) (ping_info->trans_color.blue);
2155
 
          transparent_color.opacity= (Quantum) (ping_info->trans_color.gray);
2156
 
          if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
 
2227
          transparent_color.red= (Quantum)(ping_trans_color->red);
 
2228
          transparent_color.green= (Quantum) (ping_trans_color->green);
 
2229
          transparent_color.blue= (Quantum) (ping_trans_color->blue);
 
2230
          transparent_color.opacity= (Quantum) (ping_trans_color->gray);
 
2231
          if (ping_color_type == PNG_COLOR_TYPE_GRAY)
2157
2232
            {
2158
 
              if (ping_info->bit_depth < 8)
 
2233
              if (ping_bit_depth < 8)
2159
2234
                {
2160
2235
                  transparent_color.opacity=(Quantum) (((
2161
 
                    ping_info->trans_color.gray)*255)/max_sample);
 
2236
                    ping_trans_color->gray)*255)/max_sample);
2162
2237
                }
2163
2238
              transparent_color.red=transparent_color.opacity;
2164
2239
              transparent_color.green=transparent_color.opacity;
2169
2244
#if defined(PNG_READ_sBIT_SUPPORTED)
2170
2245
  if (mng_info->have_global_sbit)
2171
2246
    {
2172
 
      if (!(ping_info->valid & PNG_INFO_sBIT))
 
2247
      if (!png_get_valid(ping,ping_info,PNG_INFO_sBIT))
2173
2248
        png_set_sBIT(ping,ping_info,&mng_info->global_sbit);
2174
2249
    }
2175
2250
#endif
2176
2251
  num_passes=png_set_interlace_handling(ping);
 
2252
 
2177
2253
  png_read_update_info(ping,ping_info);
 
2254
 
 
2255
  ping_rowbytes=png_get_rowbytes(ping,ping_info);
 
2256
 
2178
2257
  /*
2179
2258
    Initialize image structure.
2180
2259
  */
2181
2260
  mng_info->image_box.left=0;
2182
 
  mng_info->image_box.right=(long) ping_info->width;
 
2261
  mng_info->image_box.right=(ssize_t) ping_width;
2183
2262
  mng_info->image_box.top=0;
2184
 
  mng_info->image_box.bottom=(long) ping_info->height;
 
2263
  mng_info->image_box.bottom=(ssize_t) ping_height;
2185
2264
  if (mng_info->mng_type == 0)
2186
2265
    {
2187
 
      mng_info->mng_width=ping_info->width;
2188
 
      mng_info->mng_height=ping_info->height;
 
2266
      mng_info->mng_width=ping_width;
 
2267
      mng_info->mng_height=ping_height;
2189
2268
      mng_info->frame=mng_info->image_box;
2190
2269
      mng_info->clip=mng_info->image_box;
2191
2270
    }
2194
2273
      image->page.y=mng_info->y_off[mng_info->object_id];
2195
2274
    }
2196
2275
  image->compression=ZipCompression;
2197
 
  image->columns=ping_info->width;
2198
 
  image->rows=ping_info->height;
2199
 
  if (((int) ping_info->color_type == PNG_COLOR_TYPE_PALETTE) ||
2200
 
      ((int) ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2201
 
      ((int) ping_info->color_type == PNG_COLOR_TYPE_GRAY))
 
2276
  image->columns=ping_width;
 
2277
  image->rows=ping_height;
 
2278
  if (((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) ||
 
2279
      ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
 
2280
      ((int) ping_color_type == PNG_COLOR_TYPE_GRAY))
2202
2281
    {
 
2282
      size_t
 
2283
        one;
 
2284
 
2203
2285
      image->storage_class=PseudoClass;
2204
 
      image->colors=1UL << ping_info->bit_depth;
 
2286
      one=1;
 
2287
      image->colors=one << ping_bit_depth;
2205
2288
#if (MAGICKCORE_QUANTUM_DEPTH == 8)
2206
2289
      if (image->colors > 256)
2207
2290
        image->colors=256;
2209
2292
      if (image->colors > 65536L)
2210
2293
        image->colors=65536L;
2211
2294
#endif
2212
 
      if ((int) ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
 
2295
      if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2213
2296
        {
2214
2297
          int
2215
2298
            number_colors;
2218
2301
            palette;
2219
2302
 
2220
2303
          (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
2221
 
          image->colors=(unsigned long) number_colors;
 
2304
          image->colors=(size_t) number_colors;
2222
2305
          if (logging != MagickFalse)
2223
2306
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2224
2307
              "    Reading PNG PLTE chunk: number_colors: %d.",number_colors);
2232
2315
      */
2233
2316
      if (AcquireImageColormap(image,image->colors) == MagickFalse)
2234
2317
        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2235
 
      if ((int) ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
 
2318
      if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2236
2319
        {
2237
2320
          int
2238
2321
            number_colors;
2241
2324
            palette;
2242
2325
 
2243
2326
          (void) png_get_PLTE(ping,ping_info,&palette,&number_colors);
2244
 
          for (i=0; i < (long) image->colors; i++)
 
2327
          for (i=0; i < (ssize_t) image->colors; i++)
2245
2328
          {
2246
2329
            image->colormap[i].red=ScaleCharToQuantum(palette[i].red);
2247
2330
            image->colormap[i].green=ScaleCharToQuantum(palette[i].green);
2250
2333
        }
2251
2334
      else
2252
2335
        {
2253
 
          unsigned long
 
2336
          size_t
2254
2337
            scale;
2255
2338
 
2256
 
          scale=(QuantumRange/((1UL << ping_info->bit_depth)-1));
 
2339
          scale=(QuantumRange/((1UL << ping_bit_depth)-1));
2257
2340
          if (scale < 1)
2258
2341
             scale=1;
2259
 
          for (i=0; i < (long) image->colors; i++)
 
2342
          for (i=0; i < (ssize_t) image->colors; i++)
2260
2343
          {
2261
2344
            image->colormap[i].red=(Quantum) (i*scale);
2262
2345
            image->colormap[i].green=(Quantum) (i*scale);
2269
2352
  */
2270
2353
  if (image->delay != 0)
2271
2354
    mng_info->scenes_found++;
2272
 
  if ((image_info->number_scenes != 0) && (mng_info->scenes_found > (long)
2273
 
      (image_info->first_scene+image_info->number_scenes)))
 
2355
  if ((image->ping != MagickFalse) || (
 
2356
      (image_info->number_scenes != 0) && (mng_info->scenes_found > (ssize_t)
 
2357
      (image_info->first_scene+image_info->number_scenes))))
2274
2358
    {
2275
2359
      if (logging != MagickFalse)
2276
2360
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2277
 
          "    Skipping PNG image data for scene %ld",
 
2361
          "    Skipping PNG image data for scene %.20g",(double)
2278
2362
          mng_info->scenes_found-1);
2279
2363
      png_destroy_read_struct(&ping,&ping_info,&end_info);
2280
2364
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2281
 
      (void) UnlockSemaphoreInfo(png_semaphore);
 
2365
      UnlockSemaphoreInfo(png_semaphore);
2282
2366
#endif
2283
2367
      if (logging != MagickFalse)
2284
2368
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2290
2374
      "    Reading PNG IDAT chunk(s)");
2291
2375
  if (num_passes > 1)
2292
2376
    png_pixels=(unsigned char *) AcquireQuantumMemory(image->rows,
2293
 
      ping_info->rowbytes*sizeof(*png_pixels));
 
2377
      ping_rowbytes*sizeof(*png_pixels));
2294
2378
  else
2295
 
    png_pixels=(unsigned char *) AcquireQuantumMemory(ping_info->rowbytes,
 
2379
    png_pixels=(unsigned char *) AcquireQuantumMemory(ping_rowbytes,
2296
2380
      sizeof(*png_pixels));
2297
2381
  if (png_pixels == (unsigned char *) NULL)
2298
2382
    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2302
2386
  /*
2303
2387
    Convert PNG pixels to pixel packets.
2304
2388
  */
2305
 
  if (setjmp(ping->jmpbuf))
 
2389
  if (setjmp(png_jmpbuf(ping)))
2306
2390
    {
2307
2391
      /*
2308
2392
        PNG image is corrupt.
2309
2393
      */
2310
2394
      png_destroy_read_struct(&ping,&ping_info,&end_info);
2311
2395
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2312
 
      (void) UnlockSemaphoreInfo(png_semaphore);
 
2396
      UnlockSemaphoreInfo(png_semaphore);
2313
2397
#endif
2314
2398
      if (quantum_info != (QuantumInfo *) NULL)
2315
2399
        quantum_info = DestroyQuantumInfo(quantum_info);
2319
2403
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2320
2404
          "  exit ReadOnePNGImage() with error.");
2321
2405
      if (image != (Image *) NULL)
2322
 
        image->columns=0;
 
2406
        {
 
2407
          InheritException(exception,&image->exception);
 
2408
          image->columns=0;
 
2409
        }
2323
2410
      return(GetFirstImageInList(image));
2324
2411
    }
2325
2412
  quantum_info=AcquireQuantumInfo(image_info,image);
2335
2422
      int
2336
2423
        depth;
2337
2424
 
2338
 
      depth=(long) ping_info->bit_depth;
 
2425
      depth=(ssize_t) ping_bit_depth;
2339
2426
#endif
2340
 
      image->matte=(((int) ping_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ||
2341
 
          ((int) ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
2342
 
          (ping_info->valid & PNG_INFO_tRNS)) ? MagickTrue : MagickFalse;
 
2427
      image->matte=(((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA) ||
 
2428
          ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
 
2429
          (png_get_valid(ping,ping_info,PNG_INFO_tRNS))) ?
 
2430
          MagickTrue : MagickFalse;
2343
2431
 
2344
 
      for (y=0; y < (long) image->rows; y++)
 
2432
      for (y=0; y < (ssize_t) image->rows; y++)
2345
2433
      {
2346
2434
        if (num_passes > 1)
2347
 
          row_offset=ping_info->rowbytes*y;
 
2435
          row_offset=ping_rowbytes*y;
2348
2436
        else
2349
2437
          row_offset=0;
2350
2438
        png_read_row(ping,png_pixels+row_offset,NULL);
2360
2448
 
2361
2449
            r=png_pixels+row_offset;
2362
2450
            p=r;
2363
 
            if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
 
2451
            if (ping_color_type == PNG_COLOR_TYPE_GRAY)
2364
2452
              {
2365
 
                for (x=(long) image->columns-1; x >= 0; x--)
 
2453
                for (x=(ssize_t) image->columns-1; x >= 0; x--)
2366
2454
                {
2367
2455
                  *r++=*p++;
2368
2456
                  p++;
2369
 
                  if ((ping_info->valid & PNG_INFO_tRNS) &&
 
2457
                  if (png_get_valid(ping,ping_info,PNG_INFO_tRNS)) &&
2370
2458
                     (((*(p-2) << 8)|*(p-1)) == transparent_color.opacity))
2371
2459
                    {
2372
2460
                       /* Cheap transparency */
2376
2464
                       *r++=OpaqueOpacity;
2377
2465
                }
2378
2466
              }
2379
 
            else if (ping_info->color_type == PNG_COLOR_TYPE_RGB)
 
2467
            else if (ping_color_type == PNG_COLOR_TYPE_RGB)
2380
2468
              {
2381
 
              if (ping_info->valid & PNG_INFO_tRNS)
2382
 
                for (x=(long) image->columns-1; x >= 0; x--)
 
2469
              if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
 
2470
                for (x=(ssize_t) image->columns-1; x >= 0; x--)
2383
2471
                {
2384
2472
                  *r++=*p++;
2385
2473
                  p++;
2398
2486
                       *r++=OpaqueOpacity;
2399
2487
                }
2400
2488
              else
2401
 
                for (x=(long) image->columns-1; x >= 0; x--)
 
2489
                for (x=(ssize_t) image->columns-1; x >= 0; x--)
2402
2490
                {
2403
2491
                  *r++=*p++;
2404
2492
                  p++;
2409
2497
                  *r++=OpaqueOpacity;
2410
2498
                }
2411
2499
              }
2412
 
            else if (ping_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2413
 
              for (x=(long) (4*image->columns); x != 0; x--)
 
2500
            else if (ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 
2501
              for (x=(ssize_t) (4*image->columns); x != 0; x--)
2414
2502
              {
2415
2503
                *r++=*p++;
2416
2504
                p++;
2417
2505
              }
2418
 
            else if (ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2419
 
              for (x=(long) (2*image->columns); x != 0; x--)
 
2506
            else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 
2507
              for (x=(ssize_t) (2*image->columns); x != 0; x--)
2420
2508
              {
2421
2509
                *r++=*p++;
2422
2510
                p++;
2423
2511
              }
2424
2512
          }
2425
 
        if (depth == 8 && ping_info->color_type == PNG_COLOR_TYPE_GRAY)
 
2513
        if (depth == 8 && ping_color_type == PNG_COLOR_TYPE_GRAY)
2426
2514
          (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2427
2515
            GrayQuantum,png_pixels+row_offset);
2428
 
        if (ping_info->color_type == PNG_COLOR_TYPE_GRAY ||
2429
 
            ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 
2516
        if (ping_color_type == PNG_COLOR_TYPE_GRAY ||
 
2517
            ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2430
2518
          {
2431
2519
            quantum_info->depth=8;
2432
2520
            (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2433
2521
              GrayAlphaQuantum,png_pixels+row_offset);
2434
2522
          }
2435
 
        else if (depth == 8 && ping_info->color_type == PNG_COLOR_TYPE_RGB)
 
2523
        else if (depth == 8 && ping_color_type == PNG_COLOR_TYPE_RGB)
2436
2524
           (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2437
2525
             RGBQuantum,png_pixels+row_offset);
2438
 
        else if (ping_info->color_type == PNG_COLOR_TYPE_RGB ||
2439
 
              ping_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 
2526
        else if (ping_color_type == PNG_COLOR_TYPE_RGB ||
 
2527
              ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2440
2528
          {
2441
2529
            quantum_info->depth=8;
2442
2530
            (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2443
2531
              RGBAQuantum,png_pixels+row_offset);
2444
2532
          }
2445
 
        else if (ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
 
2533
        else if (ping_color_type == PNG_COLOR_TYPE_PALETTE)
2446
2534
            (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2447
2535
              IndexQuantum,png_pixels+row_offset);
2448
2536
#else /* (MAGICKCORE_QUANTUM_DEPTH != 8) */
2449
 
        if ((int) ping_info->color_type == PNG_COLOR_TYPE_GRAY)
 
2537
        if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY)
2450
2538
          (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2451
2539
            GrayQuantum,png_pixels+row_offset,exception);
2452
 
        else if ((int) ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 
2540
        else if ((int) ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2453
2541
          (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2454
2542
            GrayAlphaQuantum,png_pixels+row_offset,exception);
2455
 
        else if ((int) ping_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 
2543
        else if ((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2456
2544
          (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2457
2545
            RGBAQuantum,png_pixels+row_offset,exception);
2458
 
        else if ((int) ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
 
2546
        else if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
2459
2547
          (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2460
2548
            IndexQuantum,png_pixels+row_offset,exception);
2461
2549
        else
2462
2550
          (void) ImportQuantumPixels(image,(CacheView *) NULL,quantum_info,
2463
2551
            RGBQuantum,png_pixels+row_offset,exception);
2464
2552
#endif
 
2553
        if ((image->previous == (Image *) NULL) && (num_passes == 1))
 
2554
          {
 
2555
            status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
 
2556
                image->rows);
 
2557
            if (status == MagickFalse)
 
2558
              break;
 
2559
          }
2465
2560
        if (SyncAuthenticPixels(image,exception) == MagickFalse)
2466
2561
          break;
2467
2562
      }
2468
 
      if (image->previous == (Image *) NULL)
 
2563
      if ((image->previous == (Image *) NULL) && (num_passes != 1))
2469
2564
        {
2470
2565
          status=SetImageProgress(image,LoadImageTag,pass,num_passes);
2471
2566
          if (status == MagickFalse)
2484
2579
      /*
2485
2580
        Convert grayscale image to PseudoClass pixel packets.
2486
2581
      */
2487
 
      image->matte=ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA ?
 
2582
      image->matte=ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ?
2488
2583
        MagickTrue : MagickFalse;
2489
2584
      quantum_scanline=(Quantum *) AcquireQuantumMemory(image->columns,
2490
2585
        (image->matte ?  2 : 1)*sizeof(*quantum_scanline));
2491
2586
      if (quantum_scanline == (Quantum *) NULL)
2492
2587
        ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2493
 
      for (y=0; y < (long) image->rows; y++)
 
2588
      for (y=0; y < (ssize_t) image->rows; y++)
2494
2589
      {
2495
2590
        if (num_passes > 1)
2496
 
          row_offset=ping_info->rowbytes*y;
 
2591
          row_offset=ping_rowbytes*y;
2497
2592
        else
2498
2593
          row_offset=0;
2499
2594
        png_read_row(ping,png_pixels+row_offset,NULL);
2500
2595
        q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
2501
2596
        if (q == (PixelPacket *) NULL)
2502
2597
          break;
2503
 
        indices=GetAuthenticIndexQueue(image);
 
2598
        indexes=GetAuthenticIndexQueue(image);
2504
2599
        p=png_pixels+row_offset;
2505
2600
        r=quantum_scanline;
2506
 
        switch (ping_info->bit_depth)
 
2601
        switch (ping_bit_depth)
2507
2602
        {
2508
2603
          case 1:
2509
2604
          {
2510
 
            register long
 
2605
            register ssize_t
2511
2606
              bit;
2512
2607
 
2513
 
            for (x=(long) image->columns-7; x > 0; x-=8)
 
2608
            for (x=(ssize_t) image->columns-7; x > 0; x-=8)
2514
2609
            {
2515
2610
              for (bit=7; bit >= 0; bit--)
2516
2611
                *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
2518
2613
            }
2519
2614
            if ((image->columns % 8) != 0)
2520
2615
              {
2521
 
                for (bit=7; bit >= (long) (8-(image->columns % 8)); bit--)
 
2616
                for (bit=7; bit >= (ssize_t) (8-(image->columns % 8)); bit--)
2522
2617
                  *r++=(Quantum) ((*p) & (0x01 << bit) ? 0x01 : 0x00);
2523
2618
              }
2524
2619
            break;
2525
2620
          }
2526
2621
          case 2:
2527
2622
          {
2528
 
            for (x=(long) image->columns-3; x > 0; x-=4)
 
2623
            for (x=(ssize_t) image->columns-3; x > 0; x-=4)
2529
2624
            {
2530
2625
              *r++=(*p >> 6) & 0x03;
2531
2626
              *r++=(*p >> 4) & 0x03;
2534
2629
            }
2535
2630
            if ((image->columns % 4) != 0)
2536
2631
              {
2537
 
                for (i=3; i >= (long) (4-(image->columns % 4)); i--)
 
2632
                for (i=3; i >= (ssize_t) (4-(image->columns % 4)); i--)
2538
2633
                  *r++=(Quantum) ((*p >> (i*2)) & 0x03);
2539
2634
              }
2540
2635
            break;
2541
2636
          }
2542
2637
          case 4:
2543
2638
          {
2544
 
            for (x=(long) image->columns-1; x > 0; x-=2)
 
2639
            for (x=(ssize_t) image->columns-1; x > 0; x-=2)
2545
2640
            {
2546
2641
              *r++=(*p >> 4) & 0x0f;
2547
2642
              *r++=(*p++) & 0x0f;
2552
2647
          }
2553
2648
          case 8:
2554
2649
          {
2555
 
            if (ping_info->color_type == 4)
2556
 
              for (x=(long) image->columns-1; x >= 0; x--)
 
2650
            if (ping_color_type == 4)
 
2651
              for (x=(ssize_t) image->columns-1; x >= 0; x--)
2557
2652
              {
2558
2653
                *r++=*p++;
2559
2654
                /* In image.h, OpaqueOpacity is 0
2565
2660
                q++;
2566
2661
              }
2567
2662
            else
2568
 
              for (x=(long) image->columns-1; x >= 0; x--)
 
2663
              for (x=(ssize_t) image->columns-1; x >= 0; x--)
2569
2664
                *r++=*p++;
2570
2665
            break;
2571
2666
          }
2572
2667
          case 16:
2573
2668
          {
2574
 
            for (x=(long) image->columns-1; x >= 0; x--)
 
2669
            for (x=(ssize_t) image->columns-1; x >= 0; x--)
2575
2670
            {
2576
2671
#if (MAGICKCORE_QUANTUM_DEPTH == 16)
2577
 
              unsigned long
 
2672
              size_t
2578
2673
                quantum;
2579
2674
 
2580
2675
              if (image->colors > 256)
2585
2680
              quantum|=(*p++);
2586
2681
              *r=(Quantum) quantum;
2587
2682
              r++;
2588
 
              if (ping_info->color_type == 4)
 
2683
              if (ping_color_type == 4)
2589
2684
                {
2590
2685
                  quantum=((*p++) << 8);
2591
2686
                  quantum|=(*p++);
2594
2689
                }
2595
2690
#else
2596
2691
#if (MAGICKCORE_QUANTUM_DEPTH == 32)
2597
 
              unsigned long
 
2692
              size_t
2598
2693
                quantum;
2599
2694
 
2600
2695
              if (image->colors > 256)
2605
2700
              quantum|=(*p++);
2606
2701
              *r=quantum;
2607
2702
              r++;
2608
 
              if (ping_info->color_type == 4)
 
2703
              if (ping_color_type == 4)
2609
2704
                {
2610
2705
                  q->opacity=(*p << 8) | *(p+1);
2611
2706
                  q->opacity*=65537L;
2612
 
                  q->opacity=(Quantum) (QuantumRange-q->opacity);
 
2707
                  q->opacity=(Quantum) GetAlphaPixelComponent(q);
2613
2708
                  p+=2;
2614
2709
                  q++;
2615
2710
                }
2616
2711
#else /* MAGICKCORE_QUANTUM_DEPTH == 8 */
2617
2712
              *r++=(*p++);
2618
2713
              p++; /* strip low byte */
2619
 
              if (ping_info->color_type == 4)
 
2714
              if (ping_color_type == 4)
2620
2715
                {
2621
2716
                  q->opacity=(Quantum) (QuantumRange-(*p++));
2622
2717
                  p++;
2634
2729
          Transfer image scanline.
2635
2730
        */
2636
2731
        r=quantum_scanline;
2637
 
        for (x=0; x < (long) image->columns; x++)
2638
 
          indices[x]=(*r++);
 
2732
        for (x=0; x < (ssize_t) image->columns; x++)
 
2733
          indexes[x]=(IndexPacket) (*r++);
2639
2734
        if (SyncAuthenticPixels(image,exception) == MagickFalse)
2640
2735
          break;
 
2736
        if ((image->previous == (Image *) NULL) && (num_passes == 1))
 
2737
          {
 
2738
            status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
 
2739
                image->rows);
 
2740
            if (status == MagickFalse)
 
2741
              break;
 
2742
          }
2641
2743
      }
2642
 
      if (image->previous == (Image *) NULL)
 
2744
      if ((image->previous == (Image *) NULL) && (num_passes != 1))
2643
2745
        {
2644
2746
          status=SetImageProgress(image,LoadImageTag,pass,num_passes);
2645
2747
          if (status == MagickFalse)
2650
2752
  if (quantum_info != (QuantumInfo *) NULL)
2651
2753
    quantum_info=DestroyQuantumInfo(quantum_info);
2652
2754
  if (image->storage_class == PseudoClass)
2653
 
    (void) SyncImage(image);
 
2755
    {
 
2756
      MagickBooleanType
 
2757
        matte;
 
2758
 
 
2759
      matte=image->matte;
 
2760
      image->matte=MagickFalse;
 
2761
      (void) SyncImage(image);
 
2762
      image->matte=matte;
 
2763
    }
2654
2764
  png_read_end(ping,ping_info);
2655
2765
 
2656
2766
  if (image_info->number_scenes != 0 && mng_info->scenes_found-1 <
2657
 
      (long) image_info->first_scene && image->delay != 0)
 
2767
      (ssize_t) image_info->first_scene && image->delay != 0)
2658
2768
    {
2659
2769
      png_destroy_read_struct(&ping,&ping_info,&end_info);
2660
2770
      png_pixels=(unsigned char *) RelinquishMagickMemory(png_pixels);
2661
2771
      image->colors=2;
2662
2772
      (void) SetImageBackgroundColor(image);
2663
2773
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2664
 
      (void) UnlockSemaphoreInfo(png_semaphore);
 
2774
      UnlockSemaphoreInfo(png_semaphore);
2665
2775
#endif
2666
2776
      if (logging != MagickFalse)
2667
2777
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
2668
2778
          "  exit ReadOnePNGImage() early.");
2669
2779
      return(image);
2670
2780
    }
2671
 
  if (ping_info->valid & PNG_INFO_tRNS)
 
2781
  if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
2672
2782
    {
2673
2783
      ClassType
2674
2784
        storage_class;
2678
2788
      */
2679
2789
      storage_class=image->storage_class;
2680
2790
      image->matte=MagickTrue;
2681
 
      for (y=0; y < (long) image->rows; y++)
2682
 
      {
2683
 
        image->storage_class=storage_class;
2684
 
        q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
2685
 
        if (q == (PixelPacket *) NULL)
2686
 
          break;
2687
 
        indices=GetAuthenticIndexQueue(image);
 
2791
 
 
2792
#if 1  /* balfour fix */
 
2793
/* From imagemagick discourse server, 5 Feb 2010 */
 
2794
 
 
2795
    if (storage_class == PseudoClass)
 
2796
   {
 
2797
      if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
 
2798
      {
 
2799
         for (x=0; x < ping_num_trans; x++)
 
2800
         {
 
2801
            image->colormap[x].opacity =
 
2802
              ScaleCharToQuantum((unsigned char)(255-ping_trans_alpha[x]));
 
2803
         }
 
2804
      }
 
2805
      else if (ping_color_type == PNG_COLOR_TYPE_GRAY)
 
2806
      {
 
2807
         for (x=0; x < (int) image->colors; x++)
 
2808
         {
 
2809
            if (image->colormap[x].red == transparent_color.opacity)
 
2810
            {
 
2811
               image->colormap[x].opacity = (Quantum) TransparentOpacity;
 
2812
            }
 
2813
         }
 
2814
      }
 
2815
      (void) SyncImage(image);
 
2816
   }
 
2817
   else
 
2818
   {
 
2819
 
 
2820
      for (y=0; y < (ssize_t) image->rows; y++)
 
2821
      {
 
2822
        image->storage_class=storage_class;
 
2823
        q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
 
2824
        if (q == (PixelPacket *) NULL)
 
2825
          break;
 
2826
        indexes=GetAuthenticIndexQueue(image);
 
2827
 
 
2828
          for (x=(ssize_t) image->columns-1; x >= 0; x--)
 
2829
          {
 
2830
            if (ScaleQuantumToChar(q->red) == transparent_color.red &&
 
2831
                ScaleQuantumToChar(q->green) == transparent_color.green &&
 
2832
                ScaleQuantumToChar(q->blue) == transparent_color.blue)
 
2833
               q->opacity=(Quantum) TransparentOpacity;
 
2834
            else
 
2835
              SetOpacityPixelComponent(q,OpaqueOpacity);
 
2836
            q++;
 
2837
          }
 
2838
        if (SyncAuthenticPixels(image,exception) == MagickFalse)
 
2839
          break;
 
2840
      }
 
2841
   }
 
2842
 
 
2843
#else /* not balfour */
 
2844
 
 
2845
 
 
2846
      for (y=0; y < (ssize_t) image->rows; y++)
 
2847
      {
 
2848
        image->storage_class=storage_class;
 
2849
        q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
 
2850
        if (q == (PixelPacket *) NULL)
 
2851
          break;
 
2852
        indexes=GetAuthenticIndexQueue(image);
2688
2853
 
2689
2854
        if (storage_class == PseudoClass)
2690
2855
          {
2691
2856
            IndexPacket
2692
2857
              indexpacket;
2693
2858
 
2694
 
            if ((int) ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
2695
 
              for (x=0; x < (long) image->columns; x++)
 
2859
            if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
 
2860
              for (x=0; x < (ssize_t) image->columns; x++)
2696
2861
              {
2697
 
                indexpacket=indices[x];
2698
 
                if (indexpacket < ping_info->num_trans)
 
2862
                indexpacket=indexes[x];
 
2863
                if (indexpacket < ping_num_trans)
2699
2864
                  q->opacity=ScaleCharToQuantum((unsigned char)
2700
 
                    (255-ping_info->trans_alpha[(long) indexpacket]));
 
2865
                    (255-ping_trans_alpha[(ssize_t) indexpacket]));
2701
2866
                else
2702
 
                  q->opacity=OpaqueOpacity;
 
2867
                  SetOpacityPixelComponent(q,OpaqueOpacity);
2703
2868
                q++;
2704
2869
              }
2705
 
            else if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
2706
 
              for (x=0; x < (long) image->columns; x++)
 
2870
            else if (ping_color_type == PNG_COLOR_TYPE_GRAY)
 
2871
              for (x=0; x < (ssize_t) image->columns; x++)
2707
2872
              {
2708
 
                indexpacket=indices[x];
2709
 
                q->red=image->colormap[(long) indexpacket].red;
 
2873
                indexpacket=indexes[x];
 
2874
                q->red=image->colormap[(ssize_t) indexpacket].red;
2710
2875
                q->green=q->red;
2711
2876
                q->blue=q->red;
2712
2877
                if (q->red == transparent_color.opacity)
2713
2878
                  q->opacity=(Quantum) TransparentOpacity;
2714
2879
                else
2715
 
                  q->opacity=OpaqueOpacity;
 
2880
                  SetOpacityPixelComponent(q,OpaqueOpacity);
2716
2881
                q++;
2717
2882
              }
2718
2883
          }
2719
2884
        else
2720
 
          for (x=(long) image->columns-1; x >= 0; x--)
 
2885
          for (x=(ssize_t) image->columns-1; x >= 0; x--)
2721
2886
          {
2722
2887
            if (ScaleQuantumToChar(q->red) == transparent_color.red &&
2723
2888
                ScaleQuantumToChar(q->green) == transparent_color.green &&
2724
2889
                ScaleQuantumToChar(q->blue) == transparent_color.blue)
2725
2890
               q->opacity=(Quantum) TransparentOpacity;
2726
2891
            else
2727
 
              q->opacity=OpaqueOpacity;
 
2892
              SetOpacityPixelComponent(q,OpaqueOpacity);
2728
2893
            q++;
2729
2894
          }
2730
2895
        if (SyncAuthenticPixels(image,exception) == MagickFalse)
2731
2896
          break;
2732
2897
      }
 
2898
#endif /* not balfour */
2733
2899
      image->storage_class=DirectClass;
2734
2900
    }
2735
2901
#if (MAGICKCORE_QUANTUM_DEPTH == 8)
2737
2903
    image->depth=8;
2738
2904
#endif
2739
2905
  if (png_get_text(ping,ping_info,&text,&num_text) != 0)
2740
 
    for (i=0; i < (long) num_text; i++)
 
2906
    for (i=0; i < (ssize_t) num_text; i++)
2741
2907
    {
2742
2908
      /* Check for a profile */
2743
2909
 
2782
2948
            create a new object buffer.
2783
2949
          */
2784
2950
          mng_info->ob[object_id]=(MngBuffer *)
2785
 
            AcquireMagickMemory(sizeof(MngBuffer));
 
2951
            AcquireAlignedMemory(1,sizeof(MngBuffer));
2786
2952
          if (mng_info->ob[object_id] != (MngBuffer *) NULL)
2787
2953
            {
2788
2954
              mng_info->ob[object_id]->image=(Image *) NULL;
2803
2969
        }
2804
2970
      else
2805
2971
        {
2806
 
          png_uint_32
2807
 
            width,
2808
 
            height;
2809
 
 
2810
 
          int
2811
 
            bit_depth,
2812
 
            color_type,
2813
 
            interlace_method,
2814
 
            compression_method,
2815
 
            filter_method;
2816
2972
 
2817
2973
          if (mng_info->ob[object_id]->image != (Image *) NULL)
2818
2974
            mng_info->ob[object_id]->image=DestroyImage
2825
2981
            (void) ThrowMagickException(&image->exception,GetMagickModule(),
2826
2982
              ResourceLimitError,"Cloning image for object buffer failed",
2827
2983
              "`%s'",image->filename);
2828
 
          png_get_IHDR(ping,ping_info,&width,&height,&bit_depth,&color_type,
2829
 
            &interlace_method,&compression_method,&filter_method);
2830
 
          if (width > 250000L || height > 250000L)
 
2984
          if (ping_width > 250000L || ping_height > 250000L)
2831
2985
             png_error(ping,"PNG Image dimensions are too large.");
2832
 
          mng_info->ob[object_id]->width=width;
2833
 
          mng_info->ob[object_id]->height=height;
2834
 
          mng_info->ob[object_id]->color_type=color_type;
2835
 
          mng_info->ob[object_id]->sample_depth=bit_depth;
2836
 
          mng_info->ob[object_id]->interlace_method=interlace_method;
2837
 
          mng_info->ob[object_id]->compression_method=compression_method;
2838
 
          mng_info->ob[object_id]->filter_method=filter_method;
2839
 
          if (ping_info->valid & PNG_INFO_PLTE)
 
2986
          mng_info->ob[object_id]->width=ping_width;
 
2987
          mng_info->ob[object_id]->height=ping_height;
 
2988
          mng_info->ob[object_id]->color_type=ping_color_type;
 
2989
          mng_info->ob[object_id]->sample_depth=ping_bit_depth;
 
2990
          mng_info->ob[object_id]->interlace_method=ping_interlace_method;
 
2991
          mng_info->ob[object_id]->compression_method=
 
2992
             ping_compression_method;
 
2993
          mng_info->ob[object_id]->filter_method=ping_filter_method;
 
2994
          if (png_get_valid(ping,ping_info,PNG_INFO_PLTE))
2840
2995
            {
2841
2996
              int
2842
2997
                number_colors;
2866
3021
 
2867
3022
  png_pixels=(unsigned char *) RelinquishMagickMemory(png_pixels);
2868
3023
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
2869
 
  (void) UnlockSemaphoreInfo(png_semaphore);
 
3024
  UnlockSemaphoreInfo(png_semaphore);
2870
3025
#endif
2871
3026
 
2872
3027
  if (logging != MagickFalse)
2925
3080
    Allocate a MngInfo structure.
2926
3081
  */
2927
3082
  have_mng_structure=MagickFalse;
2928
 
  mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
 
3083
  mng_info=(MngInfo *) AcquireAlignedMemory(1,sizeof(MngInfo));
2929
3084
  if (mng_info == (MngInfo *) NULL)
2930
3085
    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
2931
3086
  /*
3028
3183
    *alpha_image_info,
3029
3184
    *color_image_info;
3030
3185
 
3031
 
  long
 
3186
  ssize_t
3032
3187
    y;
3033
3188
 
3034
3189
  MagickBooleanType
3051
3206
  register const PixelPacket
3052
3207
    *s;
3053
3208
 
3054
 
  register long
 
3209
  register ssize_t
3055
3210
    i,
3056
3211
    x;
3057
3212
 
3067
3222
    reading_idat,
3068
3223
    skip_to_iend;
3069
3224
 
3070
 
  unsigned long
 
3225
  size_t
3071
3226
    length;
3072
3227
 
3073
3228
  jng_alpha_compression_method=0;
3131
3286
 
3132
3287
    if (logging != MagickFalse)
3133
3288
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3134
 
        "  Reading JNG chunk type %c%c%c%c, length: %lu",
3135
 
        type[0],type[1],type[2],type[3],length);
 
3289
        "  Reading JNG chunk type %c%c%c%c, length: %.20g",
 
3290
        type[0],type[1],type[2],type[3],(double) length);
3136
3291
 
3137
3292
    if (length > PNG_UINT_31_MAX || count == 0)
3138
3293
      ThrowReaderException(CorruptImageError,"CorruptImage");
3143
3298
        chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk));
3144
3299
        if (chunk == (unsigned char *) NULL)
3145
3300
          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3146
 
        for (i=0; i < (long) length; i++)
 
3301
        for (i=0; i < (ssize_t) length; i++)
3147
3302
          chunk[i]=(unsigned char) ReadBlobByte(image);
3148
3303
        p=chunk;
3149
3304
      }
3160
3315
      {
3161
3316
        if (length == 16)
3162
3317
          {
3163
 
            jng_width=(unsigned long) ((p[0] << 24) | (p[1] << 16) |
 
3318
            jng_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
3164
3319
                (p[2] << 8) | p[3]);
3165
 
            jng_height=(unsigned long) ((p[4] << 24) | (p[5] << 16) |
 
3320
            jng_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
3166
3321
                (p[6] << 8) | p[7]);
3167
3322
            jng_color_type=p[8];
3168
3323
            jng_image_sample_depth=p[9];
3177
3332
            if (logging != MagickFalse)
3178
3333
              {
3179
3334
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3180
 
                  "    jng_width:      %16lu",jng_width);
 
3335
                  "    jng_width:      %16lu",(unsigned long) jng_width);
3181
3336
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3182
 
                  "    jng_width:      %16lu",jng_height);
 
3337
                  "    jng_width:      %16lu",(unsigned long) jng_height);
3183
3338
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3184
3339
                  "    jng_color_type: %16d",jng_color_type);
3185
3340
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3222
3377
               open alpha_blob, attached to alpha_image
3223
3378
        */
3224
3379
 
3225
 
        color_image_info=(ImageInfo *)AcquireMagickMemory(sizeof(ImageInfo));
 
3380
        color_image_info=(ImageInfo *)AcquireAlignedMemory(1,sizeof(ImageInfo));
3226
3381
        if (color_image_info == (ImageInfo *) NULL)
3227
3382
          ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3228
3383
        GetImageInfo(color_image_info);
3242
3397
        if ((image_info->ping == MagickFalse) && (jng_color_type >= 12))
3243
3398
          {
3244
3399
            alpha_image_info=(ImageInfo *)
3245
 
              AcquireMagickMemory(sizeof(ImageInfo));
 
3400
              AcquireAlignedMemory(1,sizeof(ImageInfo));
3246
3401
            if (alpha_image_info == (ImageInfo *) NULL)
3247
3402
              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3248
3403
            GetImageInfo(alpha_image_info);
3319
3474
              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3320
3475
                "    Copying IDAT chunk data to alpha_blob.");
3321
3476
 
3322
 
            (void) WriteBlobMSBULong(alpha_image,(unsigned long) length);
 
3477
            (void) WriteBlobMSBULong(alpha_image,(size_t) length);
3323
3478
            PNGType(data,mng_IDAT);
3324
3479
            LogPNGChunk((int) logging,mng_IDAT,length);
3325
3480
            (void) WriteBlob(alpha_image,4,data);
3521
3676
  image->rows=jng_height;
3522
3677
  image->columns=jng_width;
3523
3678
  length=image->columns*sizeof(PixelPacket);
3524
 
  for (y=0; y < (long) image->rows; y++)
 
3679
  for (y=0; y < (ssize_t) image->rows; y++)
3525
3680
  {
3526
3681
    s=GetVirtualPixels(jng_image,0,y,image->columns,1,&image->exception);
3527
3682
    q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
3554
3709
 
3555
3710
         jng_image=ReadImage(alpha_image_info,exception);
3556
3711
         if (jng_image != (Image *) NULL)
3557
 
           for (y=0; y < (long) image->rows; y++)
 
3712
           for (y=0; y < (ssize_t) image->rows; y++)
3558
3713
           {
3559
3714
             s=GetVirtualPixels(jng_image,0,y,image->columns,1,
3560
3715
                &image->exception);
3561
3716
             q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
3562
3717
             if (image->matte != MagickFalse)
3563
 
               for (x=(long) image->columns; x != 0; x--,q++,s++)
 
3718
               for (x=(ssize_t) image->columns; x != 0; x--,q++,s++)
3564
3719
                  q->opacity=(Quantum) QuantumRange-s->red;
3565
3720
             else
3566
 
               for (x=(long) image->columns; x != 0; x--,q++,s++)
 
3721
               for (x=(ssize_t) image->columns; x != 0; x--,q++,s++)
3567
3722
               {
3568
3723
                  q->opacity=(Quantum) QuantumRange-s->red;
3569
3724
                  if (q->opacity != OpaqueOpacity)
3690
3845
    Allocate a MngInfo structure.
3691
3846
  */
3692
3847
  have_mng_structure=MagickFalse;
3693
 
  mng_info=(MngInfo *) AcquireMagickMemory(sizeof(*mng_info));
 
3848
  mng_info=(MngInfo *) AcquireAlignedMemory(1,sizeof(*mng_info));
3694
3849
  if (mng_info == (MngInfo *) NULL)
3695
3850
    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3696
3851
  /*
3748
3903
    term_chunk_found,
3749
3904
    skip_to_iend;
3750
3905
 
3751
 
  volatile long
 
3906
  volatile ssize_t
3752
3907
    image_count=0;
3753
3908
 
3754
3909
  MagickBooleanType
3773
3928
  register unsigned char
3774
3929
    *p;
3775
3930
 
3776
 
  register long
 
3931
  register ssize_t
3777
3932
    i;
3778
3933
 
3779
3934
  size_t
3780
3935
    count;
3781
3936
 
3782
 
  long
 
3937
  ssize_t
3783
3938
    loop_level;
3784
3939
 
3785
3940
  volatile short
3796
3951
#endif
3797
3952
    mng_type=0;   /* 0: PNG or JNG; 1: MNG; 2: MNG-LC; 3: MNG-VLC */
3798
3953
 
3799
 
  unsigned long
 
3954
  size_t
3800
3955
    default_frame_timeout,
3801
3956
    frame_timeout,
3802
3957
#if defined(MNG_INSERT_LAYERS)
3805
3960
#endif
3806
3961
    length;
3807
3962
 
3808
 
  volatile unsigned long
 
3963
  /* These delays are all measured in image ticks_per_second,
 
3964
   * not in MNG ticks_per_second
 
3965
   */
 
3966
  volatile size_t
3809
3967
    default_frame_delay,
3810
3968
    final_delay,
3811
3969
    final_image_delay,
3862
4020
  /*
3863
4021
    Allocate a MngInfo structure.
3864
4022
  */
3865
 
  mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
 
4023
  mng_info=(MngInfo *) AcquireAlignedMemory(1,sizeof(MngInfo));
3866
4024
  if (mng_info == (MngInfo *) NULL)
3867
4025
    ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3868
4026
  /*
3891
4049
      */
3892
4050
      for (i=0; i < MNG_MAX_OBJECTS; i++)
3893
4051
      {
3894
 
        mng_info->object_clip[i].right=(long) PNG_UINT_31_MAX;
3895
 
        mng_info->object_clip[i].bottom=(long) PNG_UINT_31_MAX;
 
4052
        mng_info->object_clip[i].right=(ssize_t) PNG_UINT_31_MAX;
 
4053
        mng_info->object_clip[i].bottom=(ssize_t) PNG_UINT_31_MAX;
3896
4054
      }
3897
4055
      mng_info->exists[0]=MagickTrue;
3898
4056
    }
3938
4096
 
3939
4097
        if (logging != MagickFalse)
3940
4098
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
3941
 
           "  Reading MNG chunk type %c%c%c%c, length: %lu",
3942
 
           type[0],type[1],type[2],type[3],length);
 
4099
           "  Reading MNG chunk type %c%c%c%c, length: %.20g",
 
4100
           type[0],type[1],type[2],type[3],(double) length);
3943
4101
 
3944
4102
        if (length > PNG_UINT_31_MAX)
3945
4103
          status=MagickFalse;
3952
4110
            chunk=(unsigned char *) AcquireQuantumMemory(length,sizeof(*chunk));
3953
4111
            if (chunk == (unsigned char *) NULL)
3954
4112
              ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
3955
 
            for (i=0; i < (long) length; i++)
 
4113
            for (i=0; i < (ssize_t) length; i++)
3956
4114
              chunk[i]=(unsigned char) ReadBlobByte(image);
3957
4115
            p=chunk;
3958
4116
          }
3991
4149
          }
3992
4150
        if (memcmp(type,mng_MHDR,4) == 0)
3993
4151
          {
3994
 
            mng_info->mng_width=(unsigned long) ((p[0] << 24) | (p[1] << 16) |
 
4152
            mng_info->mng_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
3995
4153
                (p[2] << 8) | p[3]);
3996
 
            mng_info->mng_height=(unsigned long) ((p[4] << 24) | (p[5] << 16) |
 
4154
            mng_info->mng_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
3997
4155
                (p[6] << 8) | p[7]);
3998
4156
            if (logging != MagickFalse)
3999
4157
              {
4000
4158
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4001
 
                  "  MNG width: %lu",mng_info->mng_width);
 
4159
                  "  MNG width: %.20g",(double) mng_info->mng_width);
4002
4160
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4003
 
                  "  MNG height: %lu",mng_info->mng_height);
 
4161
                  "  MNG height: %.20g",(double) mng_info->mng_height);
4004
4162
              }
4005
4163
            p+=8;
4006
 
            mng_info->ticks_per_second=(unsigned long) mng_get_long(p);
 
4164
            mng_info->ticks_per_second=(size_t) mng_get_long(p);
4007
4165
            if (mng_info->ticks_per_second == 0)
4008
4166
              default_frame_delay=0;
4009
4167
            else
4014
4172
            if (length > 16)
4015
4173
              {
4016
4174
                p+=16;
4017
 
                simplicity=(unsigned long) mng_get_long(p);
 
4175
                simplicity=(size_t) mng_get_long(p);
4018
4176
              }
4019
4177
            mng_type=1;    /* Full MNG */
4020
4178
            if ((simplicity != 0) && ((simplicity | 11) == 11))
4040
4198
            if ((mng_info->mng_width > 65535L) ||
4041
4199
                (mng_info->mng_height > 65535L))
4042
4200
              ThrowReaderException(ImageError,"WidthOrHeightExceedsLimit");
4043
 
            (void) FormatMagickString(page_geometry,MaxTextExtent,"%lux%lu+0+0",
4044
 
              mng_info->mng_width,mng_info->mng_height);
 
4201
            (void) FormatMagickString(page_geometry,MaxTextExtent,
 
4202
              "%.20gx%.20g+0+0",(double) mng_info->mng_width,(double)
 
4203
              mng_info->mng_height);
4045
4204
            mng_info->frame.left=0;
4046
 
            mng_info->frame.right=(long) mng_info->mng_width;
 
4205
            mng_info->frame.right=(ssize_t) mng_info->mng_width;
4047
4206
            mng_info->frame.top=0;
4048
 
            mng_info->frame.bottom=(long) mng_info->mng_height;
 
4207
            mng_info->frame.bottom=(ssize_t) mng_info->mng_height;
4049
4208
            mng_info->clip=default_fb=previous_fb=mng_info->frame;
4050
4209
            for (i=0; i < MNG_MAX_OBJECTS; i++)
4051
4210
              mng_info->object_clip[i]=mng_info->frame;
4075
4234
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4076
4235
                  "    repeat=%d",repeat);
4077
4236
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4078
 
                  "    final_delay=%ld",final_delay);
 
4237
                  "    final_delay=%.20g",(double) final_delay);
4079
4238
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4080
 
                  "    image->iterations=%ld",image->iterations);
 
4239
                  "    image->iterations=%.20g",(double) image->iterations);
4081
4240
              }
4082
4241
            chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4083
4242
            continue;
4121
4280
            */
4122
4281
            if (length > 11)
4123
4282
              {
4124
 
                mng_info->x_off[object_id]=(long) ((p[4] << 24) | (p[5] << 16) |
 
4283
                mng_info->x_off[object_id]=(ssize_t) ((p[4] << 24) | (p[5] << 16) |
4125
4284
                (p[6] << 8) | p[7]);
4126
 
                mng_info->y_off[object_id]=(long) ((p[8] << 24) | (p[9] << 16) |
 
4285
                mng_info->y_off[object_id]=(ssize_t) ((p[8] << 24) | (p[9] << 16) |
4127
4286
                (p[10] << 8) | p[11]);
4128
4287
                if (logging != MagickFalse)
4129
4288
                  {
4130
4289
                    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4131
 
                      "  x_off[%d]: %lu",object_id,mng_info->x_off[object_id]);
 
4290
                      "  x_off[%d]: %.20g",object_id,(double)
 
4291
                      mng_info->x_off[object_id]);
4132
4292
                    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4133
 
                      "  y_off[%d]: %lu",object_id,mng_info->y_off[object_id]);
 
4293
                      "  y_off[%d]: %.20g",object_id,(double)
 
4294
                      mng_info->y_off[object_id]);
4134
4295
                  }
4135
4296
              }
4136
4297
            /*
4193
4354
                if (mng_info->global_plte == (png_colorp) NULL)
4194
4355
                  mng_info->global_plte=(png_colorp) AcquireQuantumMemory(256,
4195
4356
                    sizeof(*mng_info->global_plte));
4196
 
                for (i=0; i < (long) (length/3); i++)
 
4357
                for (i=0; i < (ssize_t) (length/3); i++)
4197
4358
                {
4198
4359
                  mng_info->global_plte[i].red=p[3*i];
4199
4360
                  mng_info->global_plte[i].green=p[3*i+1];
4200
4361
                  mng_info->global_plte[i].blue=p[3*i+2];
4201
4362
                }
4202
 
                mng_info->global_plte_length=length/3;
 
4363
                mng_info->global_plte_length=(unsigned int) (length/3);
4203
4364
              }
4204
4365
#ifdef MNG_LOOSE
4205
4366
            for ( ; i < 256; i++)
4221
4382
            /* read global tRNS */
4222
4383
 
4223
4384
            if (length < 257)
4224
 
              for (i=0; i < (long) length; i++)
 
4385
              for (i=0; i < (ssize_t) length; i++)
4225
4386
                mng_info->global_trns[i]=p[i];
4226
4387
 
4227
4388
#ifdef MNG_LOOSE
4228
4389
            for ( ; i < 256; i++)
4229
4390
              mng_info->global_trns[i]=255;
4230
4391
#endif
4231
 
            mng_info->global_trns_length=length;
 
4392
            mng_info->global_trns_length=(unsigned int) length;
4232
4393
            chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4233
4394
            continue;
4234
4395
          }
4236
4397
          {
4237
4398
            if (length == 4)
4238
4399
              {
4239
 
                long
 
4400
                ssize_t
4240
4401
                  igamma;
4241
4402
 
4242
4403
                igamma=mng_get_long(p);
4325
4486
                  Note the delay and frame clipping boundaries.
4326
4487
                */
4327
4488
                p++; /* framing mode */
4328
 
                while (*p && ((p-chunk) < (long) length))
 
4489
                while (*p && ((p-chunk) < (ssize_t) length))
4329
4490
                  p++;  /* frame name */
4330
4491
                p++;  /* frame name terminator */
4331
 
                if ((p-chunk) < (long) (length-4))
 
4492
                if ((p-chunk) < (ssize_t) (length-4))
4332
4493
                  {
4333
4494
                    int
4334
4495
                      change_delay,
4341
4502
                    p++; /* change_sync */
4342
4503
                    if (change_delay)
4343
4504
                      {
4344
 
                        frame_delay=(1UL*image->ticks_per_second*
4345
 
                            (mng_get_long(p))/mng_info->ticks_per_second);
 
4505
                        frame_delay=1UL*image->ticks_per_second*
 
4506
                          mng_get_long(p);
 
4507
                        if (mng_info->ticks_per_second != 0)
 
4508
                          frame_delay/=mng_info->ticks_per_second;
 
4509
                        else
 
4510
                          frame_delay=PNG_UINT_31_MAX;
4346
4511
                        if (change_delay == 2)
4347
4512
                          default_frame_delay=frame_delay;
4348
4513
                        p+=4;
4349
4514
                        if (logging != MagickFalse)
4350
4515
                          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4351
 
                            "    Framing_delay=%ld",frame_delay);
 
4516
                            "    Framing_delay=%.20g",(double) frame_delay);
4352
4517
                      }
4353
4518
                    if (change_timeout)
4354
4519
                      {
4355
 
                        frame_timeout=(1UL*image->ticks_per_second*
4356
 
                            (mng_get_long(p))/mng_info->ticks_per_second);
 
4520
                        frame_timeout=1UL*image->ticks_per_second*
 
4521
                          mng_get_long(p);
 
4522
                        if (mng_info->ticks_per_second != 0)
 
4523
                          frame_timeout/=mng_info->ticks_per_second;
 
4524
                        else
 
4525
                          frame_timeout=PNG_UINT_31_MAX;
4357
4526
                        if (change_delay == 2)
4358
4527
                          default_frame_timeout=frame_timeout;
4359
4528
                        p+=4;
4360
4529
                        if (logging != MagickFalse)
4361
4530
                          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4362
 
                            "    Framing_timeout=%ld",frame_timeout);
 
4531
                            "    Framing_timeout=%.20g",(double) frame_timeout);
4363
4532
                      }
4364
4533
                    if (change_clipping)
4365
4534
                      {
4368
4537
                        previous_fb=fb;
4369
4538
                        if (logging != MagickFalse)
4370
4539
                          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4371
 
                            "    Frame_clipping: L=%ld R=%ld T=%ld B=%ld",
4372
 
                              fb.left, fb.right,fb.top,fb.bottom);
 
4540
                            "    Frame_clipping: L=%.20g R=%.20g T=%.20g B=%.20g",
 
4541
                            (double) fb.left,(double) fb.right,(double) fb.top,
 
4542
                            (double) fb.bottom);
4373
4543
                        if (change_clipping == 2)
4374
4544
                          default_fb=fb;
4375
4545
                      }
4377
4547
              }
4378
4548
            mng_info->clip=fb;
4379
4549
            mng_info->clip=mng_minimum_box(fb,mng_info->frame);
4380
 
            subframe_width=(unsigned long) (mng_info->clip.right
 
4550
            subframe_width=(size_t) (mng_info->clip.right
4381
4551
               -mng_info->clip.left);
4382
 
            subframe_height=(unsigned long) (mng_info->clip.bottom
 
4552
            subframe_height=(size_t) (mng_info->clip.bottom
4383
4553
               -mng_info->clip.top);
4384
4554
            /*
4385
4555
              Insert a background layer behind the frame if framing_mode is 4.
4387
4557
#if defined(MNG_INSERT_LAYERS)
4388
4558
            if (logging != MagickFalse)
4389
4559
              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4390
 
                "   subframe_width=%lu, subframe_height=%lu",
4391
 
                subframe_width, subframe_height);
 
4560
                "   subframe_width=%.20g, subframe_height=%.20g",(double)
 
4561
                subframe_width,(double) subframe_height);
4392
4562
            if (insert_layers && (mng_info->framing_mode == 4) &&
4393
4563
                (subframe_width) && (subframe_height))
4394
4564
              {
4427
4597
                (void) SetImageBackgroundColor(image);
4428
4598
                if (logging != MagickFalse)
4429
4599
                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4430
 
                    "  Inserted background layer, L=%ld, R=%ld, T=%ld, B=%ld",
4431
 
                    mng_info->clip.left,mng_info->clip.right,
4432
 
                    mng_info->clip.top,mng_info->clip.bottom);
 
4600
                    "  Inserted background layer, L=%.20g, R=%.20g T=%.20g, B=%.20g",
 
4601
                    (double) mng_info->clip.left,(double) mng_info->clip.right,
 
4602
                    (double) mng_info->clip.top,(double) mng_info->clip.bottom);
4433
4603
              }
4434
4604
#endif
4435
4605
            chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4488
4658
              }
4489
4659
            else
4490
4660
              {
4491
 
                register long
 
4661
                register ssize_t
4492
4662
                  j;
4493
4663
 
4494
 
                for (j=0; j < (long) length; j+=2)
 
4664
                for (j=0; j < (ssize_t) length; j+=2)
4495
4665
                {
4496
4666
                  i=p[j] << 8 | p[j+1];
4497
4667
                  MngInfoDiscardObject(mng_info,i);
4503
4673
          }
4504
4674
        if (memcmp(type,mng_MOVE,4) == 0)
4505
4675
          {
4506
 
            unsigned long
 
4676
            size_t
4507
4677
              first_object,
4508
4678
              last_object;
4509
4679
 
4512
4682
            */
4513
4683
            first_object=(p[0] << 8) | p[1];
4514
4684
            last_object=(p[2] << 8) | p[3];
4515
 
            for (i=(long) first_object; i <= (long) last_object; i++)
 
4685
            for (i=(ssize_t) first_object; i <= (ssize_t) last_object; i++)
4516
4686
            {
4517
4687
              if (mng_info->exists[i] && !mng_info->frozen[i])
4518
4688
                {
4535
4705
 
4536
4706
        if (memcmp(type,mng_LOOP,4) == 0)
4537
4707
          {
4538
 
            long loop_iters=1;
 
4708
            ssize_t loop_iters=1;
4539
4709
            loop_level=chunk[0];
4540
4710
            mng_info->loop_active[loop_level]=1;  /* mark loop active */
4541
4711
            /*
4544
4714
            loop_iters=mng_get_long(&chunk[1]);
4545
4715
            if (logging != MagickFalse)
4546
4716
              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4547
 
                "  LOOP level %ld  has %ld iterations ",loop_level,loop_iters);
 
4717
                "  LOOP level %.20g has %.20g iterations ",(double) loop_level,
 
4718
                (double) loop_iters);
4548
4719
            if (loop_iters == 0)
4549
4720
              skipping_loop=loop_level;
4550
4721
            else
4578
4749
                    mng_info->loop_iteration[loop_level]++;
4579
4750
                    if (logging != MagickFalse)
4580
4751
                      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4581
 
                        "  ENDL: LOOP level %ld  has %ld remaining iterations ",
4582
 
                        loop_level,mng_info->loop_count[loop_level]);
 
4752
                        "  ENDL: LOOP level %.20g has %.20g remaining iterations ",
 
4753
                        (double) loop_level,(double)
 
4754
                        mng_info->loop_count[loop_level]);
4583
4755
                    if (mng_info->loop_count[loop_level] != 0)
4584
4756
                      {
4585
4757
                        offset=SeekBlob(image,mng_info->loop_jump[loop_level],
4761
4933
            if (length > 8)
4762
4934
              {
4763
4935
                mng_info->global_x_pixels_per_unit=
4764
 
                    (unsigned long) mng_get_long(p);
 
4936
                    (size_t) mng_get_long(p);
4765
4937
                mng_info->global_y_pixels_per_unit=
4766
 
                    (unsigned long) mng_get_long(&p[4]);
 
4938
                    (size_t) mng_get_long(&p[4]);
4767
4939
                mng_info->global_phys_unit_type=p[8];
4768
4940
                mng_info->have_global_phys=MagickTrue;
4769
4941
              }
4786
4958
                image->filename);
4787
4959
            mng_info->basi_warning++;
4788
4960
#ifdef MNG_BASI_SUPPORTED
4789
 
            basi_width=(unsigned long) ((p[0] << 24) | (p[1] << 16) |
 
4961
            basi_width=(size_t) ((p[0] << 24) | (p[1] << 16) |
4790
4962
               (p[2] << 8) | p[3]);
4791
 
            basi_height=(unsigned long) ((p[4] << 24) | (p[5] << 16) |
 
4963
            basi_height=(size_t) ((p[4] << 24) | (p[5] << 16) |
4792
4964
               (p[6] << 8) | p[7]);
4793
4965
            basi_color_type=p[8];
4794
4966
            basi_compression_method=p[9];
4852
5024
#if defined(MNG_INSERT_LAYERS)
4853
5025
        if (length < 8)
4854
5026
          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
4855
 
        image_width=(unsigned  long) mng_get_long(p);
4856
 
        image_height=(unsigned  long) mng_get_long(&p[4]);
 
5027
        image_width=(size_t) mng_get_long(p);
 
5028
        image_height=(size_t) mng_get_long(&p[4]);
4857
5029
#endif
4858
5030
        chunk=(unsigned char *) RelinquishMagickMemory(chunk);
4859
5031
 
4866
5038
          {
4867
5039
            if ((mng_info->clip.left > 0) || (mng_info->clip.top > 0) ||
4868
5040
                (image_width < mng_info->mng_width) ||
4869
 
                (mng_info->clip.right < (long) mng_info->mng_width) ||
 
5041
                (mng_info->clip.right < (ssize_t) mng_info->mng_width) ||
4870
5042
                (image_height < mng_info->mng_height) ||
4871
 
                (mng_info->clip.bottom < (long) mng_info->mng_height))
 
5043
                (mng_info->clip.bottom < (ssize_t) mng_info->mng_height))
4872
5044
              {
4873
5045
                if (GetAuthenticPixelQueue(image) != (PixelPacket *) NULL)
4874
5046
                  {
4907
5079
                (void) SetImageBackgroundColor(image);
4908
5080
                if (logging != MagickFalse)
4909
5081
                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4910
 
                    "  Inserted transparent background layer, W=%lud, H=%lud",
4911
 
                    mng_info->mng_width,mng_info->mng_height);
 
5082
                    "  Inserted transparent background layer, W=%.20g, H=%.20g",
 
5083
                    (double) mng_info->mng_width,(double) mng_info->mng_height);
4912
5084
              }
4913
5085
          }
4914
5086
        /*
4954
5126
            (void) SetImageBackgroundColor(image);
4955
5127
            if (logging != MagickFalse)
4956
5128
              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
4957
 
                "  Inserted background layer, L=%ld, R=%ld, T=%ld, B=%ld",
4958
 
                mng_info->clip.left,mng_info->clip.right,
4959
 
                mng_info->clip.top,mng_info->clip.bottom);
 
5129
                "  Inserted background layer, L=%.20g, R=%.20g T=%.20g, B=%.20g",
 
5130
                (double) mng_info->clip.left,(double) mng_info->clip.right,
 
5131
                (double) mng_info->clip.top,(double) mng_info->clip.bottom);
4960
5132
          }
4961
5133
#endif /* MNG_INSERT_LAYERS */
4962
5134
        first_mng_object=MagickFalse;
5005
5177
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5006
5178
            "  Seeking back to beginning of %c%c%c%c chunk",type[0],type[1],
5007
5179
            type[2],type[3]);
5008
 
        offset=SeekBlob(image,-((long) length+12),SEEK_CUR);
 
5180
        offset=SeekBlob(image,-((ssize_t) length+12),SEEK_CUR);
5009
5181
        if (offset < 0)
5010
5182
          ThrowReaderException(CorruptImageError,"ImproperImageHeader");
5011
5183
      }
5062
5234
                if (image->columns > 1)
5063
5235
                   magnified_width += mng_info->magn_mr;
5064
5236
                if (image->columns > 2)
5065
 
                   magnified_width += (image->columns-2)*(mng_info->magn_mx);
 
5237
                   magnified_width += (png_uint_32) ((image->columns-2)*(mng_info->magn_mx));
5066
5238
              }
5067
5239
            else
5068
5240
              {
5069
 
                magnified_width=image->columns;
 
5241
                magnified_width=(png_uint_32) image->columns;
5070
5242
                if (image->columns > 1)
5071
5243
                   magnified_width += mng_info->magn_ml-1;
5072
5244
                if (image->columns > 2)
5073
5245
                   magnified_width += mng_info->magn_mr-1;
5074
5246
                if (image->columns > 3)
5075
 
                   magnified_width += (image->columns-3)*(mng_info->magn_mx-1);
 
5247
                   magnified_width += (png_uint_32) ((image->columns-3)*(mng_info->magn_mx-1));
5076
5248
              }
5077
5249
            if (mng_info->magn_methy == 1)
5078
5250
              {
5084
5256
              }
5085
5257
            else
5086
5258
              {
5087
 
                magnified_height=image->rows;
 
5259
                magnified_height=(png_uint_32) image->rows;
5088
5260
                if (image->rows > 1)
5089
5261
                   magnified_height += mng_info->magn_mt-1;
5090
5262
                if (image->rows > 2)
5091
5263
                   magnified_height += mng_info->magn_mb-1;
5092
5264
                if (image->rows > 3)
5093
 
                   magnified_height += (image->rows-3)*(mng_info->magn_my-1);
 
5265
                   magnified_height += (png_uint_32) ((image->rows-3)*(mng_info->magn_my-1));
5094
5266
              }
5095
5267
            if (magnified_height > image->rows ||
5096
5268
                magnified_width > image->columns)
5101
5273
                int
5102
5274
                  yy;
5103
5275
 
5104
 
                long
 
5276
                ssize_t
5105
5277
                  m,
5106
5278
                  y;
5107
5279
 
5108
 
                register long
 
5280
                register ssize_t
5109
5281
                  x;
5110
5282
 
5111
5283
                register PixelPacket
5150
5322
                     Scale pixels to unsigned shorts to prevent
5151
5323
                     overflow of intermediate values of interpolations
5152
5324
                  */
5153
 
                     for (y=0; y < (long) image->rows; y++)
 
5325
                     for (y=0; y < (ssize_t) image->rows; y++)
5154
5326
                     {
5155
5327
                       q=GetAuthenticPixels(image,0,y,image->columns,1,
5156
5328
                          exception);
5157
 
                       for (x=(long) image->columns-1; x >= 0; x--)
 
5329
                       for (x=(ssize_t) image->columns-1; x >= 0; x--)
5158
5330
                       {
5159
5331
                          q->red=ScaleQuantumToShort(q->red);
5160
5332
                          q->green=ScaleQuantumToShort(q->green);
5190
5362
 
5191
5363
                if (logging != MagickFalse)
5192
5364
                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5193
 
                    "    Magnify the rows to %lu",large_image->rows);
5194
 
                m=(long) mng_info->magn_mt;
 
5365
                    "    Magnify the rows to %.20g",(double) large_image->rows);
 
5366
                m=(ssize_t) mng_info->magn_mt;
5195
5367
                yy=0;
5196
5368
                length=(size_t) image->columns;
5197
5369
                next=(PixelPacket *) AcquireQuantumMemory(length,sizeof(*next));
5206
5378
                  }
5207
5379
                n=GetAuthenticPixels(image,0,0,image->columns,1,exception);
5208
5380
                (void) CopyMagickMemory(next,n,length);
5209
 
                for (y=0; y < (long) image->rows; y++)
 
5381
                for (y=0; y < (ssize_t) image->rows; y++)
5210
5382
                {
5211
5383
                  if (y == 0)
5212
 
                    m=(long) mng_info->magn_mt;
5213
 
                  else if (magn_methy > 1 && y == (long) image->rows-2)
5214
 
                    m=(long) mng_info->magn_mb;
5215
 
                  else if (magn_methy <= 1 && y == (long) image->rows-1)
5216
 
                    m=(long) mng_info->magn_mb;
5217
 
                  else if (magn_methy > 1 && y == (long) image->rows-1)
 
5384
                    m=(ssize_t) mng_info->magn_mt;
 
5385
                  else if (magn_methy > 1 && y == (ssize_t) image->rows-2)
 
5386
                    m=(ssize_t) mng_info->magn_mb;
 
5387
                  else if (magn_methy <= 1 && y == (ssize_t) image->rows-1)
 
5388
                    m=(ssize_t) mng_info->magn_mb;
 
5389
                  else if (magn_methy > 1 && y == (ssize_t) image->rows-1)
5218
5390
                    m=1;
5219
5391
                  else
5220
 
                    m=(long) mng_info->magn_my;
 
5392
                    m=(ssize_t) mng_info->magn_my;
5221
5393
                  n=prev;
5222
5394
                  prev=next;
5223
5395
                  next=n;
5224
 
                  if (y < (long) image->rows-1)
 
5396
                  if (y < (ssize_t) image->rows-1)
5225
5397
                    {
5226
5398
                      n=GetAuthenticPixels(image,0,y+1,image->columns,1,
5227
5399
                          exception);
5232
5404
                    register PixelPacket
5233
5405
                      *pixels;
5234
5406
 
5235
 
                    assert(yy < (long) large_image->rows);
 
5407
                    assert(yy < (ssize_t) large_image->rows);
5236
5408
                    pixels=prev;
5237
5409
                    n=next;
5238
5410
                    q=GetAuthenticPixels(large_image,0,yy,large_image->columns,
5239
5411
                          1,exception);
5240
5412
                    q+=(large_image->columns-image->columns);
5241
 
                    for (x=(long) image->columns-1; x >= 0; x--)
 
5413
                    for (x=(ssize_t) image->columns-1; x >= 0; x--)
5242
5414
                    {
5243
 
                      /* TO DO: get color as function of indices[x] */
 
5415
                      /* TO DO: get color as function of indexes[x] */
5244
5416
                      /*
5245
5417
                      if (image->storage_class == PseudoClass)
5246
5418
                        {
5258
5430
                          else
5259
5431
                            {
5260
5432
                              /* Interpolate */
5261
 
                              (*q).red=(QM) (((long) (2*i*((*n).red
5262
 
                                 -(*pixels).red)+m))/((long) (m*2))
 
5433
                              (*q).red=(QM) (((ssize_t) (2*i*((*n).red
 
5434
                                 -(*pixels).red)+m))/((ssize_t) (m*2))
5263
5435
                                 +(*pixels).red);
5264
 
                              (*q).green=(QM) (((long) (2*i*((*n).green
5265
 
                                 -(*pixels).green)+m))/((long) (m*2))
 
5436
                              (*q).green=(QM) (((ssize_t) (2*i*((*n).green
 
5437
                                 -(*pixels).green)+m))/((ssize_t) (m*2))
5266
5438
                                 +(*pixels).green);
5267
 
                              (*q).blue=(QM) (((long) (2*i*((*n).blue
5268
 
                                 -(*pixels).blue)+m))/((long) (m*2))
 
5439
                              (*q).blue=(QM) (((ssize_t) (2*i*((*n).blue
 
5440
                                 -(*pixels).blue)+m))/((ssize_t) (m*2))
5269
5441
                                 +(*pixels).blue);
5270
5442
                              if (image->matte != MagickFalse)
5271
 
                                 (*q).opacity=(QM) (((long)
 
5443
                                 (*q).opacity=(QM) (((ssize_t)
5272
5444
                                 (2*i*((*n).opacity
5273
5445
                                 -(*pixels).opacity)+m))
5274
 
                                 /((long) (m*2))+(*pixels).opacity);
 
5446
                                 /((ssize_t) (m*2))+(*pixels).opacity);
5275
5447
                            }
5276
5448
                          if (magn_methy == 4)
5277
5449
                            {
5291
5463
                             *q=(*n);
5292
5464
                          if (magn_methy == 5)
5293
5465
                            {
5294
 
                              (*q).opacity=(QM) (((long) (2*i*((*n).opacity
5295
 
                                 -(*pixels).opacity)+m))/((long) (m*2))
 
5466
                              (*q).opacity=(QM) (((ssize_t) (2*i*((*n).opacity
 
5467
                                 -(*pixels).opacity)+m))/((ssize_t) (m*2))
5296
5468
                                 +(*pixels).opacity);
5297
5469
                            }
5298
5470
                        }
5322
5494
                /* magnify the columns */
5323
5495
                if (logging != MagickFalse)
5324
5496
                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5325
 
                    "    Magnify the columns to %lu",image->columns);
 
5497
                    "    Magnify the columns to %.20g",(double) image->columns);
5326
5498
 
5327
 
                for (y=0; y < (long) image->rows; y++)
 
5499
                for (y=0; y < (ssize_t) image->rows; y++)
5328
5500
                {
5329
5501
                  register PixelPacket
5330
5502
                    *pixels;
5332
5504
                  q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
5333
5505
                  pixels=q+(image->columns-length);
5334
5506
                  n=pixels+1;
5335
 
                  for (x=(long) (image->columns-length);
5336
 
                    x < (long) image->columns; x++)
 
5507
                  for (x=(ssize_t) (image->columns-length);
 
5508
                    x < (ssize_t) image->columns; x++)
5337
5509
                  {
5338
 
                    if (x == (long) (image->columns-length))
5339
 
                      m=(long) mng_info->magn_ml;
5340
 
                    else if (magn_methx > 1 && x == (long) image->columns-2)
5341
 
                      m=(long) mng_info->magn_mr;
5342
 
                    else if (magn_methx <= 1 && x == (long) image->columns-1)
5343
 
                      m=(long) mng_info->magn_mr;
5344
 
                    else if (magn_methx > 1 && x == (long) image->columns-1)
 
5510
                    if (x == (ssize_t) (image->columns-length))
 
5511
                      m=(ssize_t) mng_info->magn_ml;
 
5512
                    else if (magn_methx > 1 && x == (ssize_t) image->columns-2)
 
5513
                      m=(ssize_t) mng_info->magn_mr;
 
5514
                    else if (magn_methx <= 1 && x == (ssize_t) image->columns-1)
 
5515
                      m=(ssize_t) mng_info->magn_mr;
 
5516
                    else if (magn_methx > 1 && x == (ssize_t) image->columns-1)
5345
5517
                      m=1;
5346
5518
                    else
5347
 
                      m=(long) mng_info->magn_mx;
 
5519
                      m=(ssize_t) mng_info->magn_mx;
5348
5520
                    for (i=0; i < m; i++)
5349
5521
                    {
5350
5522
                      if (magn_methx <= 1)
5361
5533
                              /* Interpolate */
5362
5534
                              (*q).red=(QM) ((2*i*((*n).red
5363
5535
                                 -(*pixels).red)+m)
5364
 
                                 /((long) (m*2))+(*pixels).red);
 
5536
                                 /((ssize_t) (m*2))+(*pixels).red);
5365
5537
                              (*q).green=(QM) ((2*i*((*n).green
5366
5538
                                 -(*pixels).green)
5367
 
                                 +m)/((long) (m*2))+(*pixels).green);
 
5539
                                 +m)/((ssize_t) (m*2))+(*pixels).green);
5368
5540
                              (*q).blue=(QM) ((2*i*((*n).blue
5369
5541
                                 -(*pixels).blue)+m)
5370
 
                                 /((long) (m*2))+(*pixels).blue);
 
5542
                                 /((ssize_t) (m*2))+(*pixels).blue);
5371
5543
                              if (image->matte != MagickFalse)
5372
5544
                                 (*q).opacity=(QM) ((2*i*((*n).opacity
5373
 
                                   -(*pixels).opacity)+m)/((long) (m*2))
 
5545
                                   -(*pixels).opacity)+m)/((ssize_t) (m*2))
5374
5546
                                   +(*pixels).opacity);
5375
5547
                            }
5376
5548
                          if (magn_methx == 4)
5393
5565
                            {
5394
5566
                              /* Interpolate */
5395
5567
                              (*q).opacity=(QM) ((2*i*((*n).opacity
5396
 
                                 -(*pixels).opacity)+m) /((long) (m*2))
 
5568
                                 -(*pixels).opacity)+m) /((ssize_t) (m*2))
5397
5569
                                 +(*pixels).opacity);
5398
5570
                            }
5399
5571
                        }
5411
5583
                /*
5412
5584
                   Rescale pixels to Quantum
5413
5585
                */
5414
 
                   for (y=0; y < (long) image->rows; y++)
 
5586
                   for (y=0; y < (ssize_t) image->rows; y++)
5415
5587
                   {
5416
5588
                     q=GetAuthenticPixels(image,0,y,image->columns,1,exception);
5417
 
                     for (x=(long) image->columns-1; x >= 0; x--)
 
5589
                     for (x=(ssize_t) image->columns-1; x >= 0; x--)
5418
5590
                     {
5419
5591
                        q->red=ScaleShortToQuantum(q->red);
5420
5592
                        q->green=ScaleShortToQuantum(q->green);
5470
5642
                */
5471
5643
                crop_info.x=(crop_box.left-mng_info->x_off[object_id]);
5472
5644
                crop_info.y=(crop_box.top-mng_info->y_off[object_id]);
5473
 
                crop_info.width=(unsigned long) (crop_box.right-crop_box.left);
5474
 
                crop_info.height=(unsigned long) (crop_box.bottom-crop_box.top);
 
5645
                crop_info.width=(size_t) (crop_box.right-crop_box.left);
 
5646
                crop_info.height=(size_t) (crop_box.bottom-crop_box.top);
5475
5647
                image->page.width=image->columns;
5476
5648
                image->page.height=image->rows;
5477
5649
                image->page.x=0;
5525
5697
        const PixelPacket
5526
5698
          *p;
5527
5699
 
5528
 
        ok_to_reduce=(((((unsigned long) image->background_color.red >> 8) &
 
5700
        ok_to_reduce=(((((size_t) image->background_color.red >> 8) &
5529
5701
                     0xff)
5530
 
          == ((unsigned long) image->background_color.red & 0xff)) &&
5531
 
           ((((unsigned long) image->background_color.green >> 8) & 0xff)
5532
 
          == ((unsigned long) image->background_color.green & 0xff)) &&
5533
 
           ((((unsigned long) image->background_color.blue >> 8) & 0xff)
5534
 
          == ((unsigned long) image->background_color.blue & 0xff)));
 
5702
          == ((size_t) image->background_color.red & 0xff)) &&
 
5703
           ((((size_t) image->background_color.green >> 8) & 0xff)
 
5704
          == ((size_t) image->background_color.green & 0xff)) &&
 
5705
           ((((size_t) image->background_color.blue >> 8) & 0xff)
 
5706
          == ((size_t) image->background_color.blue & 0xff)));
5535
5707
        if (ok_to_reduce && image->storage_class == PseudoClass)
5536
5708
          {
5537
5709
            int indx;
5538
5710
 
5539
 
            for (indx=0; indx < (long) image->colors; indx++)
 
5711
            for (indx=0; indx < (ssize_t) image->colors; indx++)
5540
5712
              {
5541
 
                ok_to_reduce=(((((unsigned long) image->colormap[indx].red >>
 
5713
                ok_to_reduce=(((((size_t) image->colormap[indx].red >>
5542
5714
                    8) & 0xff)
5543
 
                  == ((unsigned long) image->colormap[indx].red & 0xff)) &&
5544
 
                  ((((unsigned long) image->colormap[indx].green >> 8) & 0xff)
5545
 
                  == ((unsigned long) image->colormap[indx].green & 0xff)) &&
5546
 
                  ((((unsigned long) image->colormap[indx].blue >> 8) & 0xff)
5547
 
                  == ((unsigned long) image->colormap[indx].blue & 0xff)));
 
5715
                  == ((size_t) image->colormap[indx].red & 0xff)) &&
 
5716
                  ((((size_t) image->colormap[indx].green >> 8) & 0xff)
 
5717
                  == ((size_t) image->colormap[indx].green & 0xff)) &&
 
5718
                  ((((size_t) image->colormap[indx].blue >> 8) & 0xff)
 
5719
                  == ((size_t) image->colormap[indx].blue & 0xff)));
5548
5720
                if (ok_to_reduce == MagickFalse)
5549
5721
                  break;
5550
5722
              }
5552
5724
        if ((ok_to_reduce != MagickFalse) &&
5553
5725
            (image->storage_class != PseudoClass))
5554
5726
          {
5555
 
            long
 
5727
            ssize_t
5556
5728
              y;
5557
5729
 
5558
 
            register long
 
5730
            register ssize_t
5559
5731
              x;
5560
5732
 
5561
 
            for (y=0; y < (long) image->rows; y++)
 
5733
            for (y=0; y < (ssize_t) image->rows; y++)
5562
5734
            {
5563
5735
              p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
5564
5736
              if (p == (const PixelPacket *) NULL)
5565
5737
                break;
5566
 
              for (x=(long) image->columns-1; x >= 0; x--)
 
5738
              for (x=(ssize_t) image->columns-1; x >= 0; x--)
5567
5739
              {
5568
5740
                ok_to_reduce=((
5569
 
                  (((unsigned long) p->red >> 8) & 0xff) ==
5570
 
                  ((unsigned long) p->red & 0xff)) &&
5571
 
                  ((((unsigned long) p->green >> 8) & 0xff) ==
5572
 
                  ((unsigned long) p->green & 0xff)) &&
5573
 
                  ((((unsigned long) p->blue >> 8) & 0xff) ==
5574
 
                  ((unsigned long) p->blue & 0xff)) &&
 
5741
                  (((size_t) p->red >> 8) & 0xff) ==
 
5742
                  ((size_t) p->red & 0xff)) &&
 
5743
                  ((((size_t) p->green >> 8) & 0xff) ==
 
5744
                  ((size_t) p->green & 0xff)) &&
 
5745
                  ((((size_t) p->blue >> 8) & 0xff) ==
 
5746
                  ((size_t) p->blue & 0xff)) &&
5575
5747
                  (((!image->matte ||
5576
 
                  (((unsigned long) p->opacity >> 8) & 0xff) ==
5577
 
                  ((unsigned long) p->opacity & 0xff)))));
 
5748
                  (((size_t) p->opacity >> 8) & 0xff) ==
 
5749
                  ((size_t) p->opacity & 0xff)))));
5578
5750
                if (ok_to_reduce == 0)
5579
5751
                  break;
5580
5752
                p++;
5596
5768
      if (image_info->number_scenes != 0)
5597
5769
        {
5598
5770
          if (mng_info->scenes_found >
5599
 
             (long) (image_info->first_scene+image_info->number_scenes))
 
5771
             (ssize_t) (image_info->first_scene+image_info->number_scenes))
5600
5772
            break;
5601
5773
        }
5602
5774
      if (logging != MagickFalse)
5714
5886
  image->delay=final_delay;
5715
5887
  if (logging != MagickFalse)
5716
5888
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5717
 
        "  image->delay=%lu, final_delay=%lu",image->delay,final_delay);
 
5889
        "  image->delay=%.20g, final_delay=%.20g",(double) image->delay,
 
5890
        (double) final_delay);
5718
5891
  if (logging != MagickFalse)
5719
5892
    {
5720
5893
      int
5725
5898
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5726
5899
        "  Before coalesce:");
5727
5900
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5728
 
        "    scene 0 delay=%lu",image->delay);
 
5901
        "    scene 0 delay=%.20g",(double) image->delay);
5729
5902
      while (GetNextImageInList(image) != (Image *) NULL)
5730
5903
      {
5731
5904
        image=GetNextImageInList(image);
5732
5905
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5733
 
          "    scene %d delay=%lu",++scene,image->delay);
 
5906
          "    scene %.20g delay=%.20g",(double) scene++,(double) image->delay);
5734
5907
      }
5735
5908
    }
5736
5909
 
5742
5915
        *next_image,
5743
5916
        *next;
5744
5917
 
5745
 
      unsigned long
 
5918
      size_t
5746
5919
        scene;
5747
5920
 
5748
5921
      if (logging != MagickFalse)
5791
5964
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5792
5965
        "  After coalesce:");
5793
5966
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5794
 
        "    scene 0 delay=%lu dispose=%d",image->delay,(int) image->dispose);
 
5967
        "    scene 0 delay=%.20g dispose=%.20g",(double) image->delay,
 
5968
        (double) image->dispose);
5795
5969
      while (GetNextImageInList(image) != (Image *) NULL)
5796
 
        {
5797
 
          image=GetNextImageInList(image);
5798
 
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
5799
 
            "    scene %d delay=%lu dispose=%d",++scene,
5800
 
            image->delay,(int) image->dispose);
5801
 
        }
5802
 
    }
 
5970
      {
 
5971
        image=GetNextImageInList(image);
 
5972
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
5973
          "    scene %.20g delay=%.20g dispose=%.20g",(double) scene++,
 
5974
          (double) image->delay,(double) image->dispose);
 
5975
      }
 
5976
   }
5803
5977
  image=GetFirstImageInList(image);
5804
5978
  MngInfoFreeStruct(mng_info,&have_mng_structure);
5805
5979
  have_mng_structure=MagickFalse;
5807
5981
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit ReadMNGImage()");
5808
5982
  return(GetFirstImageInList(image));
5809
5983
}
5810
 
#else /* PNG_LIBPNG_VER > 95 */
 
5984
#else /* PNG_LIBPNG_VER > 10011 */
5811
5985
static Image *ReadPNGImage(const ImageInfo *image_info,ExceptionInfo *exception)
5812
5986
{
5813
5987
  printf("Your PNG library is too old: You have libpng-%s\n",
5820
5994
{
5821
5995
  return(ReadPNGImage(image_info,exception));
5822
5996
}
5823
 
#endif /* PNG_LIBPNG_VER > 95 */
 
5997
#endif /* PNG_LIBPNG_VER > 10011 */
5824
5998
#endif
5825
5999
 
5826
6000
/*
5843
6017
%
5844
6018
%  The format of the RegisterPNGImage method is:
5845
6019
%
5846
 
%      unsigned long RegisterPNGImage(void)
 
6020
%      size_t RegisterPNGImage(void)
5847
6021
%
5848
6022
*/
5849
 
ModuleExport unsigned long RegisterPNGImage(void)
 
6023
ModuleExport size_t RegisterPNGImage(void)
5850
6024
{
5851
6025
  char
5852
6026
    version[MaxTextExtent];
5874
6048
#if defined(PNG_LIBPNG_VER_STRING)
5875
6049
  (void) ConcatenateMagickString(version,"libpng ",MaxTextExtent);
5876
6050
  (void) ConcatenateMagickString(version,PNG_LIBPNG_VER_STRING,MaxTextExtent);
5877
 
#if (PNG_LIBPNG_VER > 10005)
5878
6051
  if (LocaleCompare(PNG_LIBPNG_VER_STRING,png_get_header_ver(NULL)) != 0)
5879
6052
    {
5880
6053
      (void) ConcatenateMagickString(version,",",MaxTextExtent);
5882
6055
            MaxTextExtent);
5883
6056
    }
5884
6057
#endif
5885
 
#endif
5886
6058
  entry=SetMagickInfo("MNG");
5887
6059
  entry->seekable_stream=MagickTrue;  /* To do: eliminate this. */
5888
6060
#if defined(MAGICKCORE_PNG_DELEGATE)
6004
6176
  (void) UnregisterMagickInfo("PNG32");
6005
6177
  (void) UnregisterMagickInfo("JNG");
6006
6178
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
6007
 
  DestroySemaphoreInfo(&png_semaphore);
 
6179
  if (png_semaphore != (SemaphoreInfo *) NULL)
 
6180
    DestroySemaphoreInfo(&png_semaphore);
6008
6181
#endif
6009
6182
}
6010
6183
 
6011
6184
#if defined(MAGICKCORE_PNG_DELEGATE)
6012
 
#if PNG_LIBPNG_VER > 95
 
6185
#if PNG_LIBPNG_VER > 10011
6013
6186
/*
6014
6187
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6015
6188
%                                                                             %
6087
6260
%    transparent region at the top and/or left.
6088
6261
*/
6089
6262
 
6090
 
#if (PNG_LIBPNG_VER > 99 && PNG_LIBPNG_VER < 10007)
6091
 
/* This function became available in libpng version 1.0.6g. */
6092
 
static void
6093
 
png_set_compression_buffer_size(png_structp png_ptr, png_uint_32 size)
6094
 
{
6095
 
    if (png_ptr->zbuf)
6096
 
       png_free(png_ptr, png_ptr->zbuf); png_ptr->zbuf=NULL;
6097
 
    png_ptr->zbuf_size=(png_size_t) size;
6098
 
    png_ptr->zbuf=(png_bytep) png_malloc(png_ptr, size);
6099
 
    if (png_ptr->zbuf == 0)
6100
 
       png_error(png_ptr,"Unable to allocate zbuf");
6101
 
}
6102
 
#endif
6103
 
 
6104
6263
static void
6105
6264
png_write_raw_profile(const ImageInfo *image_info,png_struct *ping,
6106
6265
   png_info *ping_info, unsigned char *profile_type, unsigned char
6107
6266
   *profile_description, unsigned char *profile_data, png_uint_32 length)
6108
6267
{
6109
 
#if (PNG_LIBPNG_VER > 10005)
6110
6268
   png_textp
6111
6269
     text;
6112
6270
 
6113
 
   register long
 
6271
   register ssize_t
6114
6272
     i;
6115
6273
 
6116
6274
   unsigned char
6125
6283
 
6126
6284
   unsigned char
6127
6285
     hex[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
6128
 
#endif
6129
6286
 
6130
 
#if (PNG_LIBPNG_VER <= 10005)
6131
 
   if (image_info->verbose)
6132
 
     (void) printf("Not ");
6133
 
   image_info=image_info;
6134
 
   ping=ping;
6135
 
   ping_info=ping_info;
6136
 
   profile_type=profile_type;
6137
 
   profile_description=profile_description;
6138
 
   profile_data=profile_data;
6139
 
   length=length;
6140
 
#endif
6141
6287
   if (LocaleNCompare((char *) profile_type+1, "ng-chunk-",9) == 0)
6142
6288
      return;
6143
6289
 
6144
6290
   if (image_info->verbose)
6145
6291
     {
6146
 
     (void) printf("writing raw profile: type=%s, length=%lu\n",
6147
 
       (char *) profile_type, length);
 
6292
     (void) printf("writing raw profile: type=%s, length=%.20g\n",
 
6293
       (char *) profile_type, (double) length);
6148
6294
     }
6149
 
#if (PNG_LIBPNG_VER > 10005)
6150
6295
   text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
6151
6296
   description_length=(png_uint_32) strlen((const char *) profile_description);
6152
6297
   allocated_length=(png_uint_32) (length*2 + (length >> 5) + 20
6165
6310
   dp+=description_length;
6166
6311
   *dp++='\n';
6167
6312
   (void) FormatMagickString(dp,allocated_length-
6168
 
     (png_size_t) (dp-text[0].text),"%8lu ",length);
 
6313
     (png_size_t) (dp-text[0].text),"%8lu ",(unsigned long) length);
6169
6314
   dp+=8;
6170
 
   for (i=0; i < (long) length; i++)
 
6315
   for (i=0; i < (ssize_t) length; i++)
6171
6316
   {
6172
6317
     if (i%36 == 0)
6173
6318
       *dp++='\n';
6185
6330
   png_free(ping,text[0].text);
6186
6331
   png_free(ping,text[0].key);
6187
6332
   png_free(ping,text);
6188
 
#endif
6189
6333
}
6190
6334
 
6191
6335
static MagickBooleanType png_write_chunk_from_profile(Image *image,
6254
6398
    num_passes,
6255
6399
    pass;
6256
6400
 
 
6401
  png_byte
 
6402
     ping_trans_alpha[256];
 
6403
 
6257
6404
  png_colorp
6258
6405
     palette;
6259
6406
 
 
6407
  png_color_16
 
6408
    ping_background,
 
6409
    ping_trans_color;
 
6410
 
6260
6411
  png_info
6261
6412
    *ping_info;
6262
6413
 
6263
6414
  png_struct
6264
6415
    *ping;
6265
6416
 
6266
 
  long
 
6417
  png_uint_32
 
6418
    ping_height,
 
6419
    ping_width;
 
6420
 
 
6421
  ssize_t
6267
6422
    y;
6268
6423
 
6269
6424
  MagickBooleanType
6273
6428
    *quantum_info;
6274
6429
 
6275
6430
  register IndexPacket
6276
 
    *indices;
 
6431
    *indexes;
6277
6432
 
6278
 
  register long
 
6433
  register ssize_t
6279
6434
    i,
6280
6435
    x;
6281
6436
 
6286
6441
    logging,
6287
6442
    matte;
6288
6443
 
6289
 
  volatile unsigned long
 
6444
  volatile int
 
6445
    ping_bit_depth, 
 
6446
    ping_color_type,
 
6447
    ping_interlace_method,
 
6448
    ping_compression_method,
 
6449
    ping_filter_method,
 
6450
    ping_num_trans;
 
6451
 
 
6452
  volatile size_t
6290
6453
    image_colors,
6291
 
    image_depth;
 
6454
    image_depth,
 
6455
    old_bit_depth;
6292
6456
 
6293
 
  unsigned long
6294
 
    old_bit_depth,
 
6457
  size_t
6295
6458
    quality,
6296
6459
    rowbytes,
6297
6460
    save_image_depth;
6300
6463
    "  enter WriteOnePNGImage()");
6301
6464
 
6302
6465
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
6303
 
  (void) LockSemaphoreInfo(png_semaphore);
 
6466
  LockSemaphoreInfo(png_semaphore);
6304
6467
#endif
6305
6468
 
 
6469
  /* Initialize some stuff */
 
6470
  ping_bit_depth=0, 
 
6471
  ping_color_type=0,
 
6472
  ping_interlace_method=0,
 
6473
  ping_compression_method=0,
 
6474
  ping_filter_method=0,
 
6475
  ping_num_trans = 0;
 
6476
 
 
6477
  ping_background.red = 0;
 
6478
  ping_background.green = 0;
 
6479
  ping_background.blue = 0;
 
6480
  ping_background.gray = 0;
 
6481
  ping_background.index = 0;
 
6482
 
 
6483
  ping_trans_color.red=0;
 
6484
  ping_trans_color.green=0;
 
6485
  ping_trans_color.blue=0;
 
6486
  ping_trans_color.gray=0;
 
6487
 
6306
6488
  quantum_info = (QuantumInfo *) NULL;
6307
6489
  image_colors=image->colors;
6308
6490
  image_depth=image->depth;
6311
6493
  if (image->colorspace != RGBColorspace)
6312
6494
    (void) TransformImageColorspace(image,RGBColorspace);
6313
6495
  mng_info->IsPalette=image->storage_class == PseudoClass && 
6314
 
            image_colors <= 256;
 
6496
            image_colors <= 256 && !IsOpaqueImage(image,&image->exception);
6315
6497
  mng_info->optimize=image_info->type == OptimizeType;
6316
6498
 
6317
6499
  /*
6336
6518
  png_set_write_fn(ping,image,png_put_data,png_flush_data);
6337
6519
  png_pixels=(unsigned char *) NULL;
6338
6520
 
6339
 
  if (setjmp(ping->jmpbuf))
 
6521
  if (setjmp(png_jmpbuf(ping)))
6340
6522
    {
6341
6523
      /*
6342
6524
        PNG write failed.
6347
6529
#endif
6348
6530
      png_destroy_write_struct(&ping,&ping_info);
6349
6531
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
6350
 
      (void) UnlockSemaphoreInfo(png_semaphore);
 
6532
      UnlockSemaphoreInfo(png_semaphore);
6351
6533
#endif
6352
6534
      return(MagickFalse);
6353
6535
    }
6364
6546
# endif
6365
6547
#endif
6366
6548
  x=0;
6367
 
  ping_info->width=image->columns;
6368
 
  ping_info->height=image->rows;
 
6549
  ping_width=(png_uint_32) image->columns;
 
6550
  ping_height=(png_uint_32) image->rows;
6369
6551
  if (mng_info->write_png8 || mng_info->write_png24 || mng_info->write_png32)
6370
6552
     image_depth=8;
6371
6553
  if (mng_info->write_png_depth != 0)
6380
6562
  if (logging != MagickFalse)
6381
6563
    {
6382
6564
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6383
 
        "    width=%lu",ping_info->width);
6384
 
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6385
 
        "    height=%lu",ping_info->height);
6386
 
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6387
 
        "    image_matte=%u",image->matte);
6388
 
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6389
 
        "    image_depth=%lu",image->depth);
6390
 
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6391
 
        "    requested PNG image_depth=%lu",image->depth);
 
6565
        "    width=%.20g",(double) ping_width);
 
6566
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
6567
        "    height=%.20g",(double) ping_height);
 
6568
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
6569
        "    image_matte=%.20g",(double) image->matte);
 
6570
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
6571
        "    image_depth=%.20g",(double) image->depth);
 
6572
     (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
6573
        "    requested PNG image_depth=%.20g",(double) image->depth);
6392
6574
    }
6393
6575
  save_image_depth=image_depth;
6394
 
  ping_info->bit_depth=(png_byte) save_image_depth;
 
6576
  ping_bit_depth=(png_byte) save_image_depth;
6395
6577
#if defined(PNG_pHYs_SUPPORTED)
6396
6578
  if ((image->x_resolution != 0) && (image->y_resolution != 0) &&
6397
6579
      (!mng_info->write_mng || !mng_info->equal_physs))
6444
6626
 
6445
6627
      if (image_depth < MAGICKCORE_QUANTUM_DEPTH)
6446
6628
        {
6447
 
          unsigned long
 
6629
          size_t
6448
6630
             maxval;
6449
6631
 
6450
6632
          maxval=(1UL << image_depth)-1;
6478
6660
  old_bit_depth=0;
6479
6661
  if (mng_info->write_png8)
6480
6662
    {
6481
 
      ping_info->color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
6482
 
      ping_info->bit_depth=8;
6483
 
      image_depth=ping_info->bit_depth;
 
6663
      ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
 
6664
      ping_bit_depth=8;
 
6665
      image_depth=ping_bit_depth;
6484
6666
        {
6485
6667
          /* TO DO: make this a function cause it's used twice, except
6486
6668
             for reducing the sample depth from 8. */
6488
6670
          QuantizeInfo
6489
6671
            quantize_info;
6490
6672
 
6491
 
          unsigned long
 
6673
          size_t
6492
6674
             number_colors,
6493
6675
             save_number_colors;
6494
6676
 
6505
6687
              (void) SyncImage(image);
6506
6688
              if (logging != MagickFalse)
6507
6689
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6508
 
                  "    Colors quantized to %ld",number_colors);
 
6690
                  "    Colors quantized to %.20g",(double) number_colors);
6509
6691
            }
6510
6692
          if (matte)
6511
 
            ping_info->valid|=PNG_INFO_tRNS;
 
6693
            png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
6512
6694
          /*
6513
6695
            Set image palette.
6514
6696
          */
6515
 
          ping_info->color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
6516
 
          ping_info->valid|=PNG_INFO_PLTE;
 
6697
          ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
6517
6698
#if defined(PNG_SORT_PALETTE)
6518
6699
          save_number_colors=image_colors;
6519
6700
          if (CompressColormapTransFirst(image) == MagickFalse)
6520
6701
            ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
6521
 
          number_colors=image_colors;
 
6702
          number_colors=image->colors;
6522
6703
          image_colors=save_number_colors;
6523
6704
#endif
6524
6705
          palette=(png_color *) AcquireQuantumMemory(257,
6527
6708
            ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
6528
6709
          if (logging != MagickFalse)
6529
6710
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6530
 
              "  Setting up PLTE chunk");
6531
 
          for (i=0; i < (long) number_colors; i++)
 
6711
                "  Setting up PLTE chunk with %d colors",
 
6712
                (int) number_colors);
 
6713
          for (i=0; i < (ssize_t) number_colors; i++)
6532
6714
          {
6533
6715
            palette[i].red=ScaleQuantumToChar(image->colormap[i].red);
6534
6716
            palette[i].green=ScaleQuantumToChar(image->colormap[i].green);
6540
6722
#else
6541
6723
                "    %5ld (%5d,%5d,%5d)",
6542
6724
#endif
6543
 
                i,palette[i].red,palette[i].green,palette[i].blue);
 
6725
                (long) i,palette[i].red,palette[i].green,palette[i].blue);
6544
6726
 
6545
6727
          }
6546
6728
          if (matte)
6551
6733
              palette[i].blue=ScaleQuantumToChar((Quantum) QuantumRange);
6552
6734
            }
6553
6735
          png_set_PLTE(ping,ping_info,palette,(int) number_colors);
6554
 
#if (PNG_LIBPNG_VER > 10008)
6555
6736
          palette=(png_colorp) RelinquishMagickMemory(palette);
6556
 
#endif
6557
 
            image_depth=ping_info->bit_depth;
6558
 
            ping_info->num_trans=0;
 
6737
            image_depth=ping_bit_depth;
 
6738
            ping_num_trans=0;
6559
6739
            if (matte)
6560
6740
            {
6561
6741
              ExceptionInfo
6562
6742
                *exception;
6563
6743
 
 
6744
              int
 
6745
                trans_alpha[256];
 
6746
 
6564
6747
              /*
6565
6748
                Identify which colormap entry is transparent.
6566
6749
              */
6567
 
              ping_info->trans_alpha=(unsigned char *) AcquireQuantumMemory(
6568
 
                number_colors,sizeof(*ping_info->trans_alpha));
6569
 
              if (ping_info->trans == (unsigned char *) NULL)
6570
 
                ThrowWriterException(ResourceLimitError,
6571
 
                  "MemoryAllocationFailed");
6572
6750
              assert(number_colors <= 256);
6573
 
              for (i=0; i < (long) number_colors; i++)
6574
 
                 ping_info->trans_alpha[i]=255;
 
6751
              for (i=0; i < (ssize_t) number_colors; i++)
 
6752
                 trans_alpha[i]=255;
6575
6753
              exception=(&image->exception);
6576
 
              for (y=0; y < (long) image->rows; y++)
 
6754
              for (y=0; y < (ssize_t) image->rows; y++)
6577
6755
              {
6578
6756
                register const PixelPacket
6579
6757
                  *p;
6581
6759
                p=GetAuthenticPixels(image,0,y,image->columns,1,exception);
6582
6760
                if (p == (PixelPacket *) NULL)
6583
6761
                  break;
6584
 
                indices=GetAuthenticIndexQueue(image);
6585
 
                for (x=0; x < (long) image->columns; x++)
 
6762
                indexes=GetAuthenticIndexQueue(image);
 
6763
                for (x=0; x < (ssize_t) image->columns; x++)
6586
6764
                {
6587
6765
                  if (p->opacity != OpaqueOpacity)
6588
6766
                    {
6589
 
                      indices[x]=(IndexPacket) (number_colors-1);
6590
 
                      ping_info->trans_alpha[(long) indices[x]]=(png_byte) (255-
6591
 
                        ScaleQuantumToChar(p->opacity));
 
6767
                      indexes[x]=(IndexPacket) (number_colors-1);
 
6768
                      trans_alpha[(ssize_t) indexes[x]]=(png_byte) (255-
 
6769
                        ScaleQuantumToChar(GetOpacityPixelComponent(p)));
6592
6770
                    }
6593
6771
                  p++;
6594
6772
                }
6595
6773
                if (SyncAuthenticPixels(image,exception) == MagickFalse)
6596
6774
                  break;
6597
6775
              }
6598
 
              for (i=0; i < (long) number_colors; i++)
6599
 
                if (ping_info->trans_alpha[i] != 255)
6600
 
                  ping_info->num_trans=(unsigned short) (i+1);
6601
 
              if (ping_info->num_trans == 0)
6602
 
                ping_info->valid&=(~PNG_INFO_tRNS);
6603
 
              if (!(ping_info->valid & PNG_INFO_tRNS))
6604
 
                ping_info->num_trans=0;
6605
 
              if (ping_info->num_trans == 0)
6606
 
                ping_info->trans_alpha=(unsigned char *)
6607
 
                  RelinquishMagickMemory(ping_info->trans_alpha);
 
6776
              for (i=0; i < (ssize_t) number_colors; i++)
 
6777
                if (trans_alpha[i] != 255)
 
6778
                  ping_num_trans=(unsigned short) (i+1);
 
6779
 
 
6780
              if (ping_num_trans == 0)
 
6781
                 png_set_invalid(ping, ping_info, PNG_INFO_tRNS);
 
6782
              if (!png_get_valid(ping, ping_info, PNG_INFO_tRNS))
 
6783
                ping_num_trans=0;
 
6784
              if (ping_num_trans != 0)
 
6785
                {
 
6786
                  for (i=0; i<256; i++)
 
6787
                     ping_trans_alpha[i]=(png_byte) trans_alpha[i];
 
6788
                }
 
6789
 
 
6790
              (void) png_set_tRNS(ping, ping_info,
 
6791
                                  ping_trans_alpha,
 
6792
                                  ping_num_trans,
 
6793
                                  &ping_trans_color);
6608
6794
            }
6609
6795
          /*
6610
6796
            Identify which colormap entry is the background color.
6611
6797
          */
6612
 
          for (i=0; i < (long) MagickMax(1L*number_colors-1L,1L); i++)
6613
 
            if (IsPNGColorEqual(ping_info->background,image->colormap[i]))
 
6798
          for (i=0; i < (ssize_t) MagickMax(1L*number_colors-1L,1L); i++)
 
6799
            if (IsPNGColorEqual(ping_background,image->colormap[i]))
6614
6800
              break;
6615
 
          ping_info->background.index=(png_byte) i;
 
6801
          ping_background.index=(png_byte) i;
6616
6802
        }
6617
6803
      if (image_matte != MagickFalse)
6618
6804
        {
6622
6808
  else if (mng_info->write_png24)
6623
6809
    {
6624
6810
      image_matte=MagickFalse;
6625
 
      ping_info->color_type=(png_byte) PNG_COLOR_TYPE_RGB;
 
6811
      ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
6626
6812
    }
6627
6813
  else if (mng_info->write_png32)
6628
6814
    {
6629
6815
      image_matte=MagickTrue;
6630
 
      ping_info->color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
 
6816
      ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
6631
6817
    }
6632
6818
  else
6633
6819
    {
6634
 
      image_depth=ping_info->bit_depth;
 
6820
      image_depth=ping_bit_depth;
6635
6821
      if (mng_info->write_png_colortype)
6636
6822
        {
6637
 
          ping_info->color_type=(png_byte) mng_info->write_png_colortype-1;
6638
 
          if (ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
6639
 
              ping_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 
6823
          ping_color_type=(png_byte) mng_info->write_png_colortype-1;
 
6824
          if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
 
6825
              ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
6640
6826
            image_matte=MagickTrue;
6641
6827
        }
6642
6828
      else
6644
6830
          if (logging != MagickFalse)
6645
6831
             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6646
6832
             "Selecting PNG colortype");
6647
 
          ping_info->color_type=(png_byte) ((matte == MagickTrue)?
 
6833
          ping_color_type=(png_byte) ((matte == MagickTrue)?
6648
6834
          PNG_COLOR_TYPE_RGB_ALPHA:PNG_COLOR_TYPE_RGB);
6649
6835
          if(image_info->type == TrueColorType)
6650
6836
            {
6651
 
              ping_info->color_type=(png_byte) PNG_COLOR_TYPE_RGB;
 
6837
              ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB;
6652
6838
              image_matte=MagickFalse;
6653
6839
            }
6654
6840
          if(image_info->type == TrueColorMatteType)
6655
6841
            {
6656
 
              ping_info->color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
 
6842
              ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
6657
6843
              image_matte=MagickTrue;
6658
6844
            }
6659
6845
          if ((image_info->type == UndefinedType || 
6661
6847
             image_info->type == GrayscaleType) &&
6662
6848
             image_matte == MagickFalse && ImageIsGray(image))
6663
6849
            {
6664
 
              ping_info->color_type=(png_byte) PNG_COLOR_TYPE_GRAY;
 
6850
              ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY;
6665
6851
              image_matte=MagickFalse;
6666
6852
            }
6667
6853
          if ((image_info->type == UndefinedType ||
6669
6855
              image_info->type == GrayscaleMatteType) &&
6670
6856
              image_matte == MagickTrue && ImageIsGray(image))
6671
6857
            {
6672
 
              ping_info->color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA;
 
6858
              ping_color_type=(png_byte) PNG_COLOR_TYPE_GRAY_ALPHA;
6673
6859
              image_matte=MagickTrue;
6674
6860
            } 
6675
6861
        }
6676
6862
      if (logging != MagickFalse)
6677
6863
         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6678
 
         "Selected PNG colortype=%d",ping_info->color_type);
 
6864
         "Selected PNG colortype=%d",ping_color_type);
6679
6865
 
6680
 
      if (ping_info->bit_depth < 8)
 
6866
      if (ping_bit_depth < 8)
6681
6867
       {
6682
 
         if (ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
6683
 
             ping_info->color_type == PNG_COLOR_TYPE_RGB ||
6684
 
             ping_info->color_type == PNG_COLOR_TYPE_RGBA)
6685
 
           ping_info->bit_depth=8;
 
6868
         if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
 
6869
             ping_color_type == PNG_COLOR_TYPE_RGB ||
 
6870
             ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 
6871
           ping_bit_depth=8;
6686
6872
       }
6687
6873
 
6688
 
      if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
 
6874
      if (ping_color_type == PNG_COLOR_TYPE_GRAY)
6689
6875
        {
6690
6876
          if (image->matte == MagickFalse && image->colors < 256)
6691
6877
            {
6692
6878
              if (ImageIsMonochrome(image))
6693
6879
                {
6694
 
                  ping_info->bit_depth=1;
6695
 
                  if (ping_info->bit_depth < mng_info->write_png_depth)
6696
 
                    ping_info->bit_depth = mng_info->write_png_depth;
 
6880
                  ping_bit_depth=1;
 
6881
                  if (ping_bit_depth < (int)mng_info->write_png_depth)
 
6882
                    ping_bit_depth = mng_info->write_png_depth;
6697
6883
                }
6698
6884
            }
6699
6885
        }
6700
 
      if (ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
 
6886
      if (ping_color_type == PNG_COLOR_TYPE_PALETTE)
6701
6887
        {
6702
 
           ping_info->bit_depth=1;
6703
 
           while ((int) (1 << ping_info->bit_depth) < (long) image_colors)
6704
 
             ping_info->bit_depth <<= 1;
 
6888
           size_t one = 1;
 
6889
           ping_bit_depth=1;
 
6890
           while ((int) (one << ping_bit_depth) < (ssize_t) image_colors)
 
6891
             ping_bit_depth <<= 1;
6705
6892
 
6706
6893
           if (logging != MagickFalse)
6707
6894
             {
6708
6895
               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6709
 
                "    Number of colors: %lu",image_colors);
 
6896
                "    Number of colors: %.20g",(double) image_colors);
6710
6897
               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6711
 
                "    Tentative PNG bit depth: %d",ping_info->bit_depth);
 
6898
                "    Tentative PNG bit depth: %d",ping_bit_depth);
6712
6899
             }
6713
6900
           if (mng_info->write_png_depth)
6714
6901
             {
6715
 
               old_bit_depth=ping_info->bit_depth;
6716
 
               if (ping_info->bit_depth < mng_info->write_png_depth)
 
6902
               old_bit_depth=ping_bit_depth;
 
6903
               if (ping_bit_depth < (int)mng_info->write_png_depth)
6717
6904
                 {
6718
 
                   ping_info->bit_depth = mng_info->write_png_depth;
6719
 
                   if (ping_info->bit_depth > 8)
6720
 
                      ping_info->bit_depth = 8;
6721
 
                   if (ping_info->bit_depth != old_bit_depth)
 
6905
                   ping_bit_depth = mng_info->write_png_depth;
 
6906
                   if (ping_bit_depth > 8)
 
6907
                      ping_bit_depth = 8;
 
6908
                   if (ping_bit_depth != (int) old_bit_depth)
6722
6909
                     {
6723
6910
                       if (logging != MagickFalse)
6724
6911
                         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6725
 
                           "    Colors increased to %ld",image_colors);
 
6912
                           "    Colors increased to %.20g",(double)
 
6913
                           image_colors);
6726
6914
                     }
6727
6915
                 }
6728
6916
             }
6729
6917
        }
6730
6918
    }
6731
 
  image_depth=ping_info->bit_depth;
 
6919
  image_depth=ping_bit_depth;
6732
6920
  if (logging != MagickFalse)
6733
6921
    {
6734
6922
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6735
 
        "    Tentative PNG color type: %d",ping_info->color_type);
6736
 
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6737
 
        "    image_info->type: %d",image_info->type);
6738
 
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6739
 
        "    image_depth: %lu",image_depth);
6740
 
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6741
 
        "    ping_info->bit_depth: %d",ping_info->bit_depth);
 
6923
        "    Tentative PNG color type: %.20g",(double) ping_color_type);
 
6924
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
6925
        "    image_info->type: %.20g",(double) image_info->type);
 
6926
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
6927
        "    image_depth: %.20g",(double) image_depth);
 
6928
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
6929
        "    ping_bit_depth: %.20g",(double) ping_bit_depth);
6742
6930
    }
6743
6931
 
6744
6932
  if (matte && (mng_info->optimize || mng_info->IsPalette))
6747
6935
        *p;
6748
6936
 
6749
6937
      p=GetVirtualPixels(image,0,0,image->columns,1,&image->exception);
6750
 
      ping_info->color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
6751
 
      for (y=0; y < (long) image->rows; y++)
 
6938
      ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
 
6939
      for (y=0; y < (ssize_t) image->rows; y++)
6752
6940
      {
6753
6941
        p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
6754
6942
        if (p == (const PixelPacket *) NULL)
6755
6943
          break;
6756
 
        for (x=(long) image->columns-1; x >= 0; x--)
 
6944
        for (x=(ssize_t) image->columns-1; x >= 0; x--)
6757
6945
        {
6758
6946
          if (IsGray(p) == MagickFalse)
6759
6947
            {
6760
 
              ping_info->color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
 
6948
              ping_color_type=(png_byte) PNG_COLOR_TYPE_RGB_ALPHA;
6761
6949
              break;
6762
6950
            }
6763
6951
          p++;
6766
6954
      /*
6767
6955
        Determine if there is any transparent color.
6768
6956
      */
6769
 
      for (y=0; y < (long) image->rows; y++)
 
6957
      for (y=0; y < (ssize_t) image->rows; y++)
6770
6958
      {
6771
6959
        p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
6772
6960
        if (p == (const PixelPacket *) NULL)
6773
6961
          break;
6774
 
        for (x=(long) image->columns-1; x >= 0; x--)
 
6962
        for (x=(ssize_t) image->columns-1; x >= 0; x--)
6775
6963
        {
6776
6964
          if (p->opacity != OpaqueOpacity)
6777
6965
            break;
6780
6968
        if (x != 0)
6781
6969
          break;
6782
6970
      }
6783
 
      if ((y == (long) image->rows) && (x == (long) image->columns))
 
6971
      if ((y == (ssize_t) image->rows) && (x == (ssize_t) image->columns))
6784
6972
        {
6785
6973
          /*
6786
 
            No transparent pixels are present.  Change 4 or 6 to 0 or 2,
6787
 
            and do not set the PNG_INFO_tRNS flag in ping_info->valid.
 
6974
            No transparent pixels are present.  Change 4 or 6 to 0 or 2.
6788
6975
          */
6789
6976
          image_matte=MagickFalse;
6790
 
          ping_info->color_type&=0x03;
 
6977
          ping_color_type&=0x03;
6791
6978
        }
6792
6979
      else
6793
6980
        {
6795
6982
            mask;
6796
6983
 
6797
6984
          mask=0xffff;
6798
 
          if (ping_info->bit_depth == 8)
 
6985
          if (ping_bit_depth == 8)
6799
6986
             mask=0x00ff;
6800
 
          if (ping_info->bit_depth == 4)
 
6987
          if (ping_bit_depth == 4)
6801
6988
             mask=0x000f;
6802
 
          if (ping_info->bit_depth == 2)
 
6989
          if (ping_bit_depth == 2)
6803
6990
             mask=0x0003;
6804
 
          if (ping_info->bit_depth == 1)
 
6991
          if (ping_bit_depth == 1)
6805
6992
             mask=0x0001;
6806
 
          ping_info->valid|=PNG_INFO_tRNS;
6807
 
          ping_info->trans_color.red=(png_uint_16)
6808
 
            (ScaleQuantumToShort(p->red) & mask);
6809
 
          ping_info->trans_color.green=(png_uint_16)
6810
 
            (ScaleQuantumToShort(p->green) & mask);
6811
 
          ping_info->trans_color.blue=(png_uint_16)
6812
 
            (ScaleQuantumToShort(p->blue) & mask);
6813
 
          ping_info->trans_color.gray=(png_uint_16)
 
6993
          ping_trans_color.red=(png_uint_16)
 
6994
            (ScaleQuantumToShort(GetRedPixelComponent(p)) & mask);
 
6995
          ping_trans_color.green=(png_uint_16)
 
6996
            (ScaleQuantumToShort(GetGreenPixelComponent(p)) & mask);
 
6997
          ping_trans_color.blue=(png_uint_16)
 
6998
            (ScaleQuantumToShort(GetBluePixelComponent(p)) & mask);
 
6999
          ping_trans_color.gray=(png_uint_16)
6814
7000
            (ScaleQuantumToShort(PixelIntensityToQuantum(p)) & mask);
6815
 
          ping_info->trans_color.index=(png_byte)
6816
 
            (ScaleQuantumToChar((Quantum) (QuantumRange-p->opacity)));
 
7001
          ping_trans_color.index=(png_byte)
 
7002
            (ScaleQuantumToChar((Quantum) (GetAlphaPixelComponent(p))));
 
7003
          (void) png_set_tRNS(ping, ping_info, NULL, 0,
 
7004
             &ping_trans_color);
6817
7005
        }
6818
 
      if (ping_info->valid & PNG_INFO_tRNS)
 
7006
      if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
6819
7007
        {
6820
7008
          /*
6821
7009
            Determine if there is one and only one transparent color
6822
7010
            and if so if it is fully transparent.
6823
7011
          */
6824
 
          for (y=0; y < (long) image->rows; y++)
 
7012
          for (y=0; y < (ssize_t) image->rows; y++)
6825
7013
          {
6826
7014
            p=GetVirtualPixels(image,0,y,image->columns,1,
6827
7015
               &image->exception);
6828
7016
            x=0;
6829
7017
            if (p == (const PixelPacket *) NULL)
6830
7018
              break;
6831
 
            for (x=(long) image->columns-1; x >= 0; x--)
 
7019
            for (x=(ssize_t) image->columns-1; x >= 0; x--)
6832
7020
            {
6833
7021
              if (p->opacity != OpaqueOpacity)
6834
7022
                {
6835
 
                  if (IsPNGColorEqual(ping_info->trans_color,*p) == 0)
 
7023
                  if (IsPNGColorEqual(ping_trans_color,*p) == 0)
6836
7024
                  {
6837
7025
                     break;  /* Can't use RGB + tRNS for multiple
6838
7026
                                transparent colors.  */
6845
7033
                }
6846
7034
               else
6847
7035
                {
6848
 
                  if (IsPNGColorEqual(ping_info->trans_color,*p))
 
7036
                  if (IsPNGColorEqual(ping_trans_color,*p))
6849
7037
                      break; /* Can't use RGB + tRNS when another pixel
6850
7038
                                having the same RGB samples is
6851
7039
                                transparent. */
6856
7044
               break;
6857
7045
          }
6858
7046
          if (x != 0)
6859
 
            ping_info->valid&=(~PNG_INFO_tRNS);
 
7047
            png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
6860
7048
        }
6861
 
      if (ping_info->valid & PNG_INFO_tRNS)
 
7049
      if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
6862
7050
        {
6863
 
          ping_info->color_type &= 0x03;  /* changes 4 or 6 to 0 or 2 */
 
7051
          ping_color_type &= 0x03;  /* changes 4 or 6 to 0 or 2 */
6864
7052
          if (image_depth == 8)
6865
7053
            {
6866
 
              ping_info->trans_color.red&=0xff;
6867
 
              ping_info->trans_color.green&=0xff;
6868
 
              ping_info->trans_color.blue&=0xff;
6869
 
              ping_info->trans_color.gray&=0xff;
 
7054
              ping_trans_color.red&=0xff;
 
7055
              ping_trans_color.green&=0xff;
 
7056
              ping_trans_color.blue&=0xff;
 
7057
              ping_trans_color.gray&=0xff;
6870
7058
            }
6871
7059
        }
6872
7060
    }
6873
7061
    matte=image_matte;
6874
 
    if (ping_info->valid & PNG_INFO_tRNS)
 
7062
    if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
6875
7063
      image_matte=MagickFalse;
6876
7064
    if ((mng_info->optimize || mng_info->IsPalette) &&
6877
7065
        mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE &&
6878
7066
        ImageIsGray(image) && (!image_matte || image_depth >= 8))
6879
7067
      {
 
7068
        size_t one=1;
6880
7069
        if (image_matte != MagickFalse)
6881
 
            ping_info->color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
 
7070
          ping_color_type=PNG_COLOR_TYPE_GRAY_ALPHA;
6882
7071
        else
6883
7072
          {
6884
 
            ping_info->color_type=PNG_COLOR_TYPE_GRAY;
 
7073
            ping_color_type=PNG_COLOR_TYPE_GRAY;
6885
7074
            if (save_image_depth == 16 && image_depth == 8)
6886
 
              ping_info->trans_color.gray*=0x0101;
 
7075
              ping_trans_color.gray*=0x0101;
6887
7076
          }
6888
7077
        if (image_depth > MAGICKCORE_QUANTUM_DEPTH)
6889
7078
          image_depth=MAGICKCORE_QUANTUM_DEPTH;
6890
7079
        if (image_colors == 0 || image_colors-1 > MaxColormapSize)
6891
 
          image_colors=1 << image_depth;
 
7080
          image_colors=one << image_depth;
6892
7081
        if (image_depth > 8)
6893
 
          ping_info->bit_depth=16;
 
7082
          ping_bit_depth=16;
6894
7083
        else
6895
7084
          {
6896
 
            ping_info->bit_depth=8;
6897
 
            if ((int) ping_info->color_type == PNG_COLOR_TYPE_PALETTE)
 
7085
            ping_bit_depth=8;
 
7086
            if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
6898
7087
              {
6899
7088
                if(!mng_info->write_png_depth)
6900
7089
                  {
6901
 
                    ping_info->bit_depth=1;
6902
 
                    while ((int) (1 << ping_info->bit_depth)
6903
 
                        < (long) image_colors)
6904
 
                      ping_info->bit_depth <<= 1;
 
7090
                    ping_bit_depth=1;
 
7091
                    while ((int) (one << ping_bit_depth)
 
7092
                        < (ssize_t) image_colors)
 
7093
                      ping_bit_depth <<= 1;
6905
7094
                  }
6906
7095
              }
6907
 
            else if (mng_info->optimize && ping_info->color_type ==
 
7096
            else if (mng_info->optimize && ping_color_type ==
6908
7097
                PNG_COLOR_TYPE_GRAY && image_colors < 17 && 
6909
7098
                mng_info->IsPalette)
6910
7099
              {
6915
7104
                  depth_2_ok=MagickTrue,
6916
7105
                  depth_1_ok=MagickTrue;
6917
7106
 
6918
 
                for (i=0; i < (long) image_colors; i++)
 
7107
                for (i=0; i < (ssize_t) image_colors; i++)
6919
7108
                {
6920
7109
                   unsigned char
6921
7110
                     intensity;
6930
7119
                     depth_1_ok=MagickFalse;
6931
7120
                }
6932
7121
                if (depth_1_ok && mng_info->write_png_depth <= 1)
6933
 
                   ping_info->bit_depth=1;
 
7122
                  ping_bit_depth=1;
6934
7123
                else if (depth_2_ok && mng_info->write_png_depth <= 2)
6935
 
                   ping_info->bit_depth=2;
 
7124
                  ping_bit_depth=2;
6936
7125
                else if (depth_4_ok && mng_info->write_png_depth <= 4)
6937
 
                   ping_info->bit_depth=4;
 
7126
                  ping_bit_depth=4;
6938
7127
              }
6939
7128
          }
6940
 
          image_depth=ping_info->bit_depth;
 
7129
          image_depth=ping_bit_depth;
6941
7130
      }
6942
7131
    else
6943
7132
      if (mng_info->IsPalette)
6944
7133
      {
 
7134
        size_t
 
7135
           number_colors;
 
7136
 
 
7137
        number_colors=image_colors;
 
7138
 
6945
7139
        if (image_depth <= 8)
6946
7140
          {
6947
 
            unsigned long
6948
 
               number_colors;
6949
 
 
6950
 
            number_colors=image_colors;
6951
 
            if (matte)
6952
 
               ping_info->valid|=PNG_INFO_tRNS;
6953
7141
            /*
6954
7142
              Set image palette.
6955
7143
            */
6956
 
            ping_info->color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
6957
 
            ping_info->valid|=PNG_INFO_PLTE;
 
7144
            ping_color_type=(png_byte) PNG_COLOR_TYPE_PALETTE;
6958
7145
            if (mng_info->have_write_global_plte && !matte)
6959
7146
              {
6960
 
                 png_set_PLTE(ping,ping_info,NULL,0);
6961
 
                 if (logging)
6962
 
                   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6963
 
                     "  Setting up empty PLTE chunk");
 
7147
                png_set_PLTE(ping,ping_info,NULL,0);
 
7148
                if (logging)
 
7149
                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7150
                    "  Setting up empty PLTE chunk");
6964
7151
              }
6965
7152
            else
6966
7153
              {
6967
7154
#if defined(PNG_SORT_PALETTE)
6968
 
                unsigned long
 
7155
                size_t
6969
7156
                   save_number_colors;
6970
7157
 
6971
7158
                if (mng_info->optimize)
6974
7161
                    if (CompressColormapTransFirst(image) == MagickFalse)
6975
7162
                       ThrowWriterException(ResourceLimitError,
6976
7163
                                            "MemoryAllocationFailed");
6977
 
                    number_colors=image_colors;
 
7164
                    number_colors=image->colors;
6978
7165
                    image_colors=save_number_colors;
6979
7166
                  }
6980
7167
#endif
6983
7170
                if (palette == (png_color *) NULL)
6984
7171
                  ThrowWriterException(ResourceLimitError,
6985
7172
                     "MemoryAllocationFailed");
6986
 
                for (i=0; i < (long) number_colors; i++)
 
7173
                for (i=0; i < (ssize_t) number_colors; i++)
6987
7174
                {
6988
7175
                  palette[i].red=ScaleQuantumToChar(image->colormap[i].red);
6989
7176
                  palette[i].green=ScaleQuantumToChar(image->colormap[i].green);
6991
7178
                }
6992
7179
                if (logging)
6993
7180
                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
6994
 
                    "  Setting up PLTE chunk");
 
7181
                    "  Setting up PLTE chunk with %d colors",
 
7182
                    (int) number_colors);
6995
7183
                png_set_PLTE(ping,ping_info,palette,(int) number_colors);
6996
 
#if (PNG_LIBPNG_VER > 10008)
6997
7184
                palette=(png_colorp) RelinquishMagickMemory(palette);
6998
 
#endif
6999
7185
              }
7000
7186
            /* color_type is PNG_COLOR_TYPE_PALETTE */
7001
7187
            if (!mng_info->write_png_depth)
7002
7188
              {
7003
 
                ping_info->bit_depth=1;
7004
 
                while ((1UL << ping_info->bit_depth) < number_colors)
7005
 
                  ping_info->bit_depth <<= 1;
 
7189
                size_t
 
7190
                  one;
 
7191
 
 
7192
                ping_bit_depth=1;
 
7193
                one=1;
 
7194
                while ((one << ping_bit_depth) < number_colors)
 
7195
                  ping_bit_depth <<= 1;
7006
7196
              }
7007
 
            ping_info->num_trans=0;
 
7197
            ping_num_trans=0;
7008
7198
            if (matte)
7009
7199
            {
7010
7200
              ExceptionInfo
7017
7207
                trans[256];
7018
7208
 
7019
7209
              register const IndexPacket
7020
 
                *packet_indices;
 
7210
                *packet_indexes;
7021
7211
 
7022
7212
              /*
7023
7213
                Identify which colormap entry is transparent.
7024
7214
              */
7025
7215
              assert(number_colors <= 256);
7026
 
              for (i=0; i < (long) number_colors; i++)
 
7216
              for (i=0; i < (ssize_t) number_colors; i++)
7027
7217
                trans[i]=256;
7028
7218
              exception=(&image->exception);
7029
 
              for (y=0; y < (long) image->rows; y++)
 
7219
              for (y=0; y < (ssize_t) image->rows; y++)
7030
7220
              {
7031
7221
                p=GetVirtualPixels(image,0,y,image->columns,1,exception);
7032
7222
                if (p == (const PixelPacket *) NULL)
7033
7223
                  break;
7034
 
                packet_indices=GetVirtualIndexQueue(image);
7035
 
                for (x=0; x < (long) image->columns; x++)
 
7224
                packet_indexes=GetVirtualIndexQueue(image);
 
7225
                for (x=0; x < (ssize_t) image->columns; x++)
7036
7226
                {
7037
7227
                  if (p->opacity != OpaqueOpacity)
7038
7228
                    {
7039
7229
                      IndexPacket
7040
7230
                        packet_index;
7041
7231
 
7042
 
                      packet_index=packet_indices[x];
7043
 
                      assert((unsigned long) packet_index < number_colors);
7044
 
                      if (trans[(long) packet_index] != 256)
 
7232
                      packet_index=packet_indexes[x];
 
7233
                      assert((size_t) packet_index < number_colors);
 
7234
                      if (trans[(ssize_t) packet_index] != 256)
7045
7235
                        {
7046
 
                          if (trans[(long) packet_index] != (png_byte) (255-
7047
 
                             ScaleQuantumToChar(p->opacity)))
 
7236
                          if (trans[(ssize_t) packet_index] != (png_byte) (255-
 
7237
                             ScaleQuantumToChar(GetOpacityPixelComponent(p))))
7048
7238
                            {
7049
 
                              ping_info->color_type=(png_byte)
 
7239
                              ping_color_type=(png_byte)
7050
7240
                                PNG_COLOR_TYPE_RGB_ALPHA;
7051
7241
                              break;
7052
7242
                            }
7053
7243
                        }
7054
 
                      trans[(long) packet_index]=(png_byte) (255-
7055
 
                        ScaleQuantumToChar(p->opacity));
 
7244
                      trans[(ssize_t) packet_index]=(png_byte) (255-
 
7245
                        ScaleQuantumToChar(GetOpacityPixelComponent(p)));
7056
7246
                    }
7057
7247
                  p++;
7058
7248
                }
7059
 
                if ((int) ping_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
 
7249
                if ((int) ping_color_type == PNG_COLOR_TYPE_RGB_ALPHA)
7060
7250
                {
7061
 
                  ping_info->num_trans=0;
7062
 
                  ping_info->valid&=(~PNG_INFO_tRNS);
7063
 
                  ping_info->valid&=(~PNG_INFO_PLTE);
 
7251
                  ping_num_trans=0;
 
7252
                  png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
 
7253
                  png_set_invalid(ping,ping_info,PNG_INFO_PLTE);
7064
7254
                  mng_info->IsPalette=MagickFalse;
7065
7255
                  (void) SyncImage(image);
7066
7256
                  if (logging)
7069
7259
                  break;
7070
7260
                }
7071
7261
              }
7072
 
              if ((ping_info->valid & PNG_INFO_tRNS))
 
7262
              if (png_get_valid(ping,ping_info,PNG_INFO_tRNS))
7073
7263
              {
7074
 
                for (i=0; i < (long) number_colors; i++)
 
7264
                for (i=0; i < (ssize_t) number_colors; i++)
7075
7265
                {
7076
7266
                  if (trans[i] == 256)
7077
7267
                    trans[i]=255;
7078
7268
                  if (trans[i] != 255)
7079
 
                    ping_info->num_trans=(unsigned short) (i+1);
7080
 
                }
7081
 
              }
7082
 
              if (ping_info->num_trans == 0)
7083
 
                ping_info->valid&=(~PNG_INFO_tRNS);
7084
 
              if (!(ping_info->valid & PNG_INFO_tRNS))
7085
 
                ping_info->num_trans=0;
7086
 
              if (ping_info->num_trans != 0)
7087
 
              {
7088
 
                ping_info->trans_alpha=(unsigned char *) AcquireQuantumMemory(
7089
 
                  number_colors,sizeof(*ping_info->trans_alpha));
7090
 
                if (ping_info->trans_alpha == (unsigned char *) NULL)
7091
 
                  ThrowWriterException(ResourceLimitError,
7092
 
                     "MemoryAllocationFailed");
7093
 
                for (i=0; i < (long) number_colors; i++)
7094
 
                    ping_info->trans_alpha[i]=(png_byte) trans[i];
7095
 
              }
 
7269
                    ping_num_trans=(unsigned short) (i+1);
 
7270
                }
 
7271
              }
 
7272
              if (ping_num_trans == 0)
 
7273
                png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
 
7274
              if (!png_get_valid(ping,ping_info,PNG_INFO_tRNS))
 
7275
                ping_num_trans=0;
 
7276
              if (ping_num_trans != 0)
 
7277
                {
 
7278
                  for (i=0; i < (ssize_t) number_colors; i++)
 
7279
                      ping_trans_alpha[i]=(png_byte) trans[i];
 
7280
                }
7096
7281
            }
7097
7282
 
7098
 
            /*
7099
 
              Identify which colormap entry is the background color.
7100
 
            */
7101
 
            for (i=0; i < (long) MagickMax(1L*number_colors-1L,1L); i++)
7102
 
              if (IsPNGColorEqual(ping_info->background,image->colormap[i]))
7103
 
                break;
7104
 
            ping_info->background.index=(png_byte) i;
7105
7283
          }
7106
7284
      }
7107
7285
    else
7110
7288
          image_depth=8;
7111
7289
        if ((save_image_depth == 16) && (image_depth == 8))
7112
7290
          {
7113
 
            ping_info->trans_color.red*=0x0101;
7114
 
            ping_info->trans_color.green*=0x0101;
7115
 
            ping_info->trans_color.blue*=0x0101;
7116
 
            ping_info->trans_color.gray*=0x0101;
 
7291
            ping_trans_color.red*=0x0101;
 
7292
            ping_trans_color.green*=0x0101;
 
7293
            ping_trans_color.blue*=0x0101;
 
7294
            ping_trans_color.gray*=0x0101;
7117
7295
          }
7118
7296
      }
7119
7297
 
7120
7298
    /*
7121
7299
      Adjust background and transparency samples in sub-8-bit grayscale files.
7122
7300
    */
7123
 
    if (ping_info->bit_depth < 8 && ping_info->color_type ==
 
7301
    if (ping_bit_depth < 8 && ping_color_type ==
7124
7302
        PNG_COLOR_TYPE_GRAY)
7125
7303
      {
7126
7304
         png_uint_16
7129
7307
         png_color_16
7130
7308
           background;
7131
7309
 
7132
 
         maxval=(png_uint_16) ((1 << ping_info->bit_depth)-1);
 
7310
         size_t
 
7311
           one=1;
 
7312
 
 
7313
         maxval=(png_uint_16) ((one << ping_bit_depth)-1);
7133
7314
 
7134
7315
 
7135
7316
         background.gray=(png_uint_16)
7140
7321
             "  Setting up bKGD chunk");
7141
7322
         png_set_bKGD(ping,ping_info,&background);
7142
7323
 
7143
 
         ping_info->trans_color.gray=(png_uint_16) (QuantumScale*(maxval*
7144
 
           ping_info->trans_color.gray));
7145
 
      }
 
7324
         ping_trans_color.gray=(png_uint_16) (QuantumScale*(maxval*
 
7325
           ping_trans_color.gray));
 
7326
      }
 
7327
 
 
7328
    if ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE)
 
7329
      {
 
7330
        /*
 
7331
           Identify which colormap entry is the background color.
 
7332
        */
 
7333
 
 
7334
        size_t
 
7335
           number_colors;
 
7336
 
 
7337
        number_colors=image_colors;
 
7338
 
 
7339
        for (i=0; i < (ssize_t) MagickMax(1L*number_colors-1L,1L); i++)
 
7340
          if (IsPNGColorEqual(ping_background,image->colormap[i]))
 
7341
            break;
 
7342
 
 
7343
        ping_background.index=(png_byte) i;
 
7344
 
 
7345
        if (logging)
 
7346
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7347
            "  Setting up bKGD chunk with index=%d",(int) i);
 
7348
 
 
7349
        png_set_bKGD(ping,ping_info,&ping_background);
 
7350
      }
 
7351
 
7146
7352
  if (logging != MagickFalse)
7147
7353
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7148
 
      "    PNG color type: %d",ping_info->color_type);
 
7354
      "    PNG color type: %d",ping_color_type);
7149
7355
  /*
7150
7356
    Initialize compression level and filtering.
7151
7357
  */
7152
7358
  if (logging != MagickFalse)
7153
7359
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7154
7360
      "  Setting up deflate compression");
7155
 
#if (PNG_LIBPNG_VER > 99)
7156
7361
  if (logging != MagickFalse)
7157
7362
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7158
7363
      "    Compression buffer size: 32768");
7159
7364
  png_set_compression_buffer_size(ping,32768L);
7160
 
#endif
7161
7365
  if (logging != MagickFalse)
7162
7366
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7163
7367
      "    Compression mem level: 9");
7169
7373
      int
7170
7374
        level;
7171
7375
 
7172
 
      level=(int) MagickMin((long) quality/10,9);
 
7376
      level=(int) MagickMin((ssize_t) quality/10,9);
7173
7377
      if (logging != MagickFalse)
7174
7378
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7175
7379
          "    Compression level: %d",level);
7193
7397
      if (logging != MagickFalse)
7194
7398
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7195
7399
          "    Filter_type: PNG_INTRAPIXEL_DIFFERENCING");
7196
 
      ping_info->filter_type=PNG_INTRAPIXEL_DIFFERENCING;
 
7400
      ping_filter_method=PNG_INTRAPIXEL_DIFFERENCING;
7197
7401
    }
7198
7402
  else
7199
7403
    if (logging != MagickFalse)
7210
7414
      if ((quality % 10) != 5)
7211
7415
        base_filter=(int) quality % 10;
7212
7416
      else
7213
 
        if (((int) ping_info->color_type == PNG_COLOR_TYPE_GRAY) ||
7214
 
            ((int) ping_info->color_type == PNG_COLOR_TYPE_PALETTE) ||
 
7417
        if (((int) ping_color_type == PNG_COLOR_TYPE_GRAY) ||
 
7418
            ((int) ping_color_type == PNG_COLOR_TYPE_PALETTE) ||
7215
7419
            (quality < 50))
7216
7420
          base_filter=PNG_NO_FILTERS;
7217
7421
        else
7234
7438
    profile=GetImageProfile(image,name);
7235
7439
    if (profile != (StringInfo *) NULL)
7236
7440
      {
7237
 
#if (PNG_LIBPNG_VER > 10008) && defined(PNG_WRITE_iCCP_SUPPORTED)
 
7441
#ifdef PNG_WRITE_iCCP_SUPPORTED
7238
7442
        if ((LocaleCompare(name,"ICC") == 0) ||
7239
7443
            (LocaleCompare(name,"ICM") == 0))
7240
7444
          png_set_iCCP(ping,ping_info,(const png_charp) name,0,(png_charp)
7266
7470
      (void) png_set_sRGB(ping,ping_info,(int) (image->rendering_intent-1));
7267
7471
      png_set_gAMA(ping,ping_info,0.45455);
7268
7472
    }
7269
 
  if ((!mng_info->write_mng) || !(ping_info->valid & PNG_INFO_sRGB))
 
7473
  if ((!mng_info->write_mng) || (!png_get_valid(ping,ping_info,PNG_INFO_sRGB)))
7270
7474
#endif
7271
7475
    {
7272
7476
      if ((mng_info->have_write_global_gama == 0) && (image->gamma != 0.0))
7305
7509
               bp.x,bp.y);
7306
7510
       }
7307
7511
    }
7308
 
  ping_info->interlace_type=image_info->interlace != NoInterlace;
 
7512
  ping_interlace_method=image_info->interlace != NoInterlace;
7309
7513
 
7310
7514
  if (mng_info->write_mng)
7311
7515
    png_set_sig_bytes(ping,8);
7317
7521
     if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY)
7318
7522
       if (ImageIsGray(image) == MagickFalse)
7319
7523
         {
7320
 
           ping_info->color_type = PNG_COLOR_TYPE_RGB;
7321
 
           if (ping_info->bit_depth < 8)
7322
 
             ping_info->bit_depth=8;
 
7524
           ping_color_type = PNG_COLOR_TYPE_RGB;
 
7525
           if (ping_bit_depth < 8)
 
7526
             ping_bit_depth=8;
7323
7527
         }
7324
7528
         
7325
7529
     if (mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY_ALPHA)
7326
7530
       if (ImageIsGray(image) == MagickFalse)
7327
 
         ping_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
 
7531
         ping_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
7328
7532
    }
7329
7533
 
7330
7534
  if ((mng_info->write_png_depth &&
7331
 
      mng_info->write_png_depth != ping_info->bit_depth) ||
 
7535
     (int) mng_info->write_png_depth != ping_bit_depth) ||
7332
7536
     (mng_info->write_png_colortype &&
7333
 
      mng_info->write_png_colortype-1 != ping_info->color_type))
 
7537
     ((int) mng_info->write_png_colortype-1 != ping_color_type &&
 
7538
      mng_info->write_png_colortype != 7 &&
 
7539
      !(mng_info->write_png_colortype == 5 && ping_color_type == 0))))
7334
7540
    {
7335
7541
      if (logging != MagickFalse)
7336
7542
        {
7339
7545
              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7340
7546
                  "  Defined PNG:bit-depth=%u, Computed depth=%u",
7341
7547
                  mng_info->write_png_depth,
7342
 
                  ping_info->bit_depth);
 
7548
                  ping_bit_depth);
7343
7549
            }
7344
7550
          if (mng_info->write_png_colortype)
7345
7551
            {
7346
7552
              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7347
7553
                  "  Defined PNG:color-type=%u, Computed color type=%u",
7348
7554
                  mng_info->write_png_colortype-1,
7349
 
                  ping_info->color_type);
 
7555
                  ping_color_type);
7350
7556
            }
7351
7557
        }
7352
7558
      png_error(ping,
7358
7564
      /* Add an opaque matte channel */
7359
7565
      image->matte = MagickTrue;
7360
7566
      (void) SetImageOpacity(image,0);
 
7567
      if (logging != MagickFalse)
 
7568
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7569
          "  Added an opaque matte channel");
 
7570
    }
 
7571
 
 
7572
  if (image->matte == MagickTrue && ping_color_type < 4)
 
7573
    {
 
7574
      if (ping_color_type == 3 && ping_num_trans == 0)
 
7575
        {
 
7576
          png_set_invalid(ping,ping_info,PNG_INFO_tRNS);
 
7577
          if (logging != MagickFalse)
 
7578
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7579
              "  Ignoring request to write tRNS chunk with num_trans==0");
 
7580
        }
 
7581
      else
 
7582
        (void) png_set_tRNS(ping, ping_info,
 
7583
                            ping_trans_alpha,
 
7584
                            ping_num_trans,
 
7585
                            &ping_trans_color);
7361
7586
    }
7362
7587
 
7363
7588
  if (logging != MagickFalse)
7364
7589
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7365
7590
      "  Writing PNG header chunks");
7366
7591
 
 
7592
  png_set_IHDR(ping,ping_info,ping_width,ping_height,
 
7593
               ping_bit_depth,ping_color_type,
 
7594
               ping_interlace_method,ping_compression_method,
 
7595
               ping_filter_method);
 
7596
 
7367
7597
  png_write_info_before_PLTE(ping, ping_info);
7368
7598
  /* write any png-chunk-b profiles */
7369
7599
  (void) png_write_chunk_from_profile(image,"PNG-chunk-b",(int) logging);
7373
7603
 
7374
7604
  if (image->page.width || image->page.height)
7375
7605
    {
7376
 
       unsigned char
7377
 
         chunk[14];
 
7606
      unsigned char
 
7607
        chunk[14];
7378
7608
 
7379
 
       (void) WriteBlobMSBULong(image,9L);  /* data length=8 */
7380
 
       PNGType(chunk,mng_vpAg);
7381
 
       LogPNGChunk((int) logging,mng_vpAg,9L);
7382
 
       PNGLong(chunk+4,(png_uint_32) image->page.width);
7383
 
       PNGLong(chunk+8,(png_uint_32) image->page.height);
7384
 
       chunk[12]=0;   /* unit = pixels */
7385
 
       (void) WriteBlob(image,13,chunk);
7386
 
       (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
 
7609
      (void) WriteBlobMSBULong(image,9L);  /* data length=8 */
 
7610
      PNGType(chunk,mng_vpAg);
 
7611
      LogPNGChunk((int) logging,mng_vpAg,9L);
 
7612
      PNGLong(chunk+4,(png_uint_32) image->page.width);
 
7613
      PNGLong(chunk+8,(png_uint_32) image->page.height);
 
7614
      chunk[12]=0;   /* unit = pixels */
 
7615
      (void) WriteBlob(image,13,chunk);
 
7616
      (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
7387
7617
    }
7388
7618
 
7389
7619
#if (PNG_LIBPNG_VER == 10206)
7390
 
      /* avoid libpng-1.2.6 bug by setting PNG_HAVE_IDAT flag */
 
7620
    /* avoid libpng-1.2.6 bug by setting PNG_HAVE_IDAT flag */
7391
7621
#define PNG_HAVE_IDAT               0x04
7392
 
      ping->mode |= PNG_HAVE_IDAT;
 
7622
    ping->mode |= PNG_HAVE_IDAT;
7393
7623
#undef PNG_HAVE_IDAT
7394
7624
#endif
7395
7625
 
7398
7628
    Allocate memory.
7399
7629
  */
7400
7630
  rowbytes=image->columns;
7401
 
  if (image_depth <= 8)
 
7631
  if (image_depth > 8)
 
7632
    rowbytes*=2;
 
7633
  switch (ping_color_type)
7402
7634
    {
7403
 
      if (mng_info->write_png24 || (mng_info->write_png_depth == 8 &&
7404
 
          mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_RGB))
 
7635
      case PNG_COLOR_TYPE_RGB:
7405
7636
        rowbytes*=3;
7406
 
      else if (mng_info->write_png32 || (mng_info->write_png_depth == 8 &&
7407
 
           mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_RGB_ALPHA))
 
7637
        break;
 
7638
      case PNG_COLOR_TYPE_GRAY_ALPHA:
 
7639
        rowbytes*=2;
 
7640
        break;
 
7641
      case PNG_COLOR_TYPE_RGBA:
7408
7642
        rowbytes*=4;
7409
 
      else if ((!mng_info->write_png8 ||
7410
 
           mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY ||
7411
 
           mng_info->write_png_colortype-1 == PNG_COLOR_TYPE_GRAY_ALPHA )&&
7412
 
           ((mng_info->optimize || mng_info->IsPalette) && ImageIsGray(image)))
7413
 
        rowbytes*=(image_matte ? 2 : 1);
7414
 
      else
7415
 
        {
7416
 
          if (!mng_info->IsPalette)
7417
 
            rowbytes*=(image_matte ? 4 : 3);
7418
 
        }
7419
 
    }
7420
 
  else
7421
 
    {
7422
 
      if ((mng_info->optimize || mng_info->IsPalette) &&
7423
 
          ImageIsGray(image))
7424
 
        rowbytes*=(image_matte ? 4 : 2);
7425
 
      else
7426
 
        rowbytes*=(image_matte ? 8 : 6);
 
7643
        break;
 
7644
      default:
 
7645
        break;
7427
7646
    }
7428
7647
  if (logging)
7429
 
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7430
 
      "  Allocating %lu bytes of memory for pixels",rowbytes);
 
7648
    {
 
7649
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7650
        "  Writing PNG image data");
 
7651
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7652
        "    Allocating %.20g bytes of memory for pixels",(double) rowbytes);
 
7653
    }
7431
7654
  png_pixels=(unsigned char *) AcquireQuantumMemory(rowbytes,
7432
7655
    sizeof(*png_pixels));
7433
7656
  if (png_pixels == (unsigned char *) NULL)
7435
7658
  /*
7436
7659
    Initialize image scanlines.
7437
7660
  */
7438
 
  if (setjmp(ping->jmpbuf))
 
7661
  if (setjmp(png_jmpbuf(ping)))
7439
7662
    {
7440
7663
      /*
7441
7664
        PNG write failed.
7450
7673
      if (png_pixels != (unsigned char *) NULL)
7451
7674
        png_pixels=(unsigned char *) RelinquishMagickMemory(png_pixels);
7452
7675
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
7453
 
      (void) UnlockSemaphoreInfo(png_semaphore);
 
7676
      UnlockSemaphoreInfo(png_semaphore);
7454
7677
#endif
7455
7678
      return(MagickFalse);
7456
7679
    }
7475
7698
        /*
7476
7699
          Convert PseudoClass image to a PNG monochrome image.
7477
7700
        */
7478
 
        for (y=0; y < (long) image->rows; y++)
 
7701
        for (y=0; y < (ssize_t) image->rows; y++)
7479
7702
        {
7480
7703
          p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
7481
7704
          if (p == (const PixelPacket *) NULL)
7489
7712
                  mng_info->write_png_depth != old_bit_depth)
7490
7713
                {
7491
7714
                  /* Undo pixel scaling */
7492
 
                  for (i=0; i < (long) image->columns; i++)
 
7715
                  for (i=0; i < (ssize_t) image->columns; i++)
7493
7716
                     *(png_pixels+i)=(unsigned char) (*(png_pixels+i)
7494
7717
                     >> (8-old_bit_depth));
7495
7718
                }
7500
7723
                quantum_info,RedQuantum,png_pixels,&image->exception);
7501
7724
            }
7502
7725
          if (mng_info->write_png_colortype-1 != PNG_COLOR_TYPE_PALETTE)
7503
 
            for (i=0; i < (long) image->columns; i++)
 
7726
            for (i=0; i < (ssize_t) image->columns; i++)
7504
7727
               *(png_pixels+i)=(unsigned char) ((*(png_pixels+i) > 127) ?
7505
7728
                      255 : 0);
 
7729
          if (logging && y == 0)
 
7730
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7731
                "    Writing row of pixels (1)");
7506
7732
          png_write_row(ping,png_pixels);
7507
7733
        }
7508
7734
        if (image->previous == (Image *) NULL)
7522
7748
      if ((!mng_info->write_png8 && !mng_info->write_png24 && 
7523
7749
         !mng_info->write_png32) &&
7524
7750
         (image_matte ||
7525
 
         (ping_info->bit_depth >= MAGICKCORE_QUANTUM_DEPTH)) &&
 
7751
         (ping_bit_depth >= MAGICKCORE_QUANTUM_DEPTH)) &&
7526
7752
         (mng_info->optimize || mng_info->IsPalette) && ImageIsGray(image))
7527
7753
      {
7528
 
        for (y=0; y < (long) image->rows; y++)
 
7754
        for (y=0; y < (ssize_t) image->rows; y++)
7529
7755
        {
7530
7756
          p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
7531
7757
          if (p == (const PixelPacket *) NULL)
7532
7758
            break;
7533
 
          if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
 
7759
          if (ping_color_type == PNG_COLOR_TYPE_GRAY)
7534
7760
            {
7535
7761
              if (mng_info->IsPalette)
7536
7762
                (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7538
7764
              else
7539
7765
                (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7540
7766
                  quantum_info,RedQuantum,png_pixels,&image->exception);
 
7767
              if (logging && y == 0)
 
7768
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7769
                     "    Writing GRAY PNG pixels (2)");
7541
7770
            }
7542
7771
          else /* PNG_COLOR_TYPE_GRAY_ALPHA */
7543
7772
            {
 
7773
              if (logging && y == 0)
 
7774
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7775
                       "    Writing GRAY_ALPHA PNG pixels (2)");
7544
7776
              (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7545
7777
                quantum_info,GrayAlphaQuantum,png_pixels,&image->exception);
7546
7778
            }
 
7779
          if (logging && y == 0)
 
7780
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7781
                "    Writing row of pixels (2)");
7547
7782
          png_write_row(ping,png_pixels);
7548
7783
        }
7549
7784
        if (image->previous == (Image *) NULL)
7559
7794
        if ((image_depth > 8) || (mng_info->write_png24 ||
7560
7795
            mng_info->write_png32 ||
7561
7796
            (!mng_info->write_png8 && !mng_info->IsPalette)))
7562
 
          for (y=0; y < (long) image->rows; y++)
 
7797
          for (y=0; y < (ssize_t) image->rows; y++)
7563
7798
          {
7564
7799
            p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
7565
7800
            if (p == (const PixelPacket *) NULL)
7566
7801
              break;
7567
 
            if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
 
7802
            if (ping_color_type == PNG_COLOR_TYPE_GRAY)
7568
7803
              {
7569
7804
                if (image->storage_class == DirectClass)
7570
7805
                  (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7573
7808
                  (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7574
7809
                    quantum_info,GrayQuantum,png_pixels,&image->exception);
7575
7810
              }
7576
 
            else if (ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
7577
 
              (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7578
 
                quantum_info,GrayAlphaQuantum,png_pixels,&image->exception);
 
7811
            else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 
7812
              {
 
7813
                (void) ExportQuantumPixels(image,(const CacheView *) NULL,
 
7814
                  quantum_info,GrayAlphaQuantum,png_pixels,&image->exception);
 
7815
                if (logging && y == 0)
 
7816
                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7817
                       "    Writing GRAY_ALPHA PNG pixels (3)");
 
7818
              }
7579
7819
            else if (image_matte != MagickFalse)
7580
7820
              (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7581
7821
                quantum_info,RGBAQuantum,png_pixels,&image->exception);
7582
7822
            else
7583
7823
              (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7584
7824
                quantum_info,RGBQuantum,png_pixels,&image->exception);
 
7825
            if (logging && y == 0)
 
7826
              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7827
                  "    Writing row of pixels (3)");
7585
7828
            png_write_row(ping,png_pixels);
7586
7829
          }
7587
7830
      else
7589
7832
            mng_info->write_png32 ||
7590
7833
            (!mng_info->write_png8 && !mng_info->IsPalette))) */
7591
7834
        {
7592
 
          if ((ping_info->color_type != PNG_COLOR_TYPE_GRAY) &&
7593
 
              (ping_info->color_type != PNG_COLOR_TYPE_GRAY_ALPHA))
 
7835
          if ((ping_color_type != PNG_COLOR_TYPE_GRAY) &&
 
7836
              (ping_color_type != PNG_COLOR_TYPE_GRAY_ALPHA))
7594
7837
            {
7595
7838
              if (logging)
7596
7839
                (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7598
7841
              quantum_info->depth=8;
7599
7842
              image_depth=8;
7600
7843
            }
7601
 
          for (y=0; y < (long) image->rows; y++)
 
7844
          for (y=0; y < (ssize_t) image->rows; y++)
7602
7845
          {
7603
 
            if (logging)
 
7846
            if (logging && y == 0)
7604
7847
              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7605
7848
                "  pass %d, Image Is RGB, 16-bit GRAY, or GRAY_ALPHA",pass);
7606
7849
            p=GetVirtualPixels(image,0,y,image->columns,1,&image->exception);
7607
7850
            if (p == (const PixelPacket *) NULL)
7608
7851
              break;
7609
 
            if (ping_info->color_type == PNG_COLOR_TYPE_GRAY)
 
7852
            if (ping_color_type == PNG_COLOR_TYPE_GRAY)
7610
7853
              (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7611
7854
                quantum_info,GrayQuantum,png_pixels,&image->exception);
7612
 
            else if (ping_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
7613
 
              (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7614
 
                quantum_info,GrayAlphaQuantum,png_pixels,&image->exception);
 
7855
            else if (ping_color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 
7856
              {
 
7857
                if (logging && y == 0)
 
7858
                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7859
                       "  Writing GRAY_ALPHA PNG pixels (4)");
 
7860
                (void) ExportQuantumPixels(image,(const CacheView *) NULL,
 
7861
                  quantum_info,GrayAlphaQuantum,png_pixels,&image->exception);
 
7862
              }
7615
7863
            else
7616
7864
              (void) ExportQuantumPixels(image,(const CacheView *) NULL,
7617
7865
                quantum_info,IndexQuantum,png_pixels,&image->exception);
 
7866
            if (logging && y == 0)
 
7867
              (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7868
                  "  Writing row of pixels (4)");
7618
7869
            png_write_row(ping,png_pixels);
7619
7870
          }
7620
7871
        }
7632
7883
  if (logging != MagickFalse)
7633
7884
    {
7634
7885
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7635
 
        "  Writing PNG image data");
7636
 
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7637
 
        "    Width: %lu",ping_info->width);
7638
 
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7639
 
        "    Height: %lu",ping_info->height);
 
7886
        "  Wrote PNG image data");
 
7887
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7888
        "    Width: %.20g",(double) ping_width);
 
7889
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
7890
        "    Height: %.20g",(double) ping_height);
7640
7891
      if (mng_info->write_png_depth)
7641
7892
        {
7642
7893
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7643
7894
            "    Defined PNG:bit-depth: %d",mng_info->write_png_depth);
7644
7895
        }
7645
7896
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7646
 
        "    PNG bit-depth written: %d",ping_info->bit_depth);
 
7897
        "    PNG bit-depth written: %d",ping_bit_depth);
7647
7898
      if (mng_info->write_png_colortype)
7648
7899
        {
7649
7900
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7650
7901
            "    Defined PNG:color-type: %d",mng_info->write_png_colortype-1);
7651
7902
        }
7652
7903
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7653
 
        "    PNG color-type written: %d",ping_info->color_type);
 
7904
        "    PNG color-type written: %d",ping_color_type);
7654
7905
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7655
 
        "    PNG Interlace method: %d",ping_info->interlace_type);
 
7906
        "    PNG Interlace method: %d",ping_interlace_method);
7656
7907
    }
7657
7908
  /*
7658
7909
    Generate text chunks.
7659
7910
  */
7660
 
#if (PNG_LIBPNG_VER <= 10005)
7661
 
  ping_info->num_text=0;
7662
 
#endif
7663
7911
  ResetImagePropertyIterator(image);
7664
7912
  property=GetNextImageProperty(image);
7665
7913
  while (property != (const char *) NULL)
7666
7914
  {
7667
 
#if (PNG_LIBPNG_VER > 10005)
7668
7915
    png_textp
7669
7916
      text;
7670
 
#endif
7671
7917
 
7672
7918
    value=GetImageProperty(image,property);
7673
7919
    if (value != (const char *) NULL)
7674
7920
      {
7675
 
#if (PNG_LIBPNG_VER > 10005)
7676
7921
        text=(png_textp) png_malloc(ping,(png_uint_32) sizeof(png_text));
7677
7922
        text[0].key=(char *) property;
7678
7923
        text[0].text=(char *) value;
7689
7934
          }
7690
7935
        png_set_text(ping,ping_info,text,1);
7691
7936
        png_free(ping,text);
7692
 
#else
7693
 
/* Work directly with ping_info struct; png_set_text before libpng version
7694
 
 * 1.0.5a is leaky */
7695
 
        if (ping_info->num_text == 0)
7696
 
          {
7697
 
            ping_info->text=(png_text *) AcquireQuantumMemory(256,
7698
 
              sizeof(*ping_info->text));
7699
 
            if (ping_info->text == (png_text *) NULL)
7700
 
              (void) ThrowMagickException(&image->exception,GetMagickModule(),
7701
 
                ResourceLimitError,"MemoryAllocationFailed","`%s'",
7702
 
                image->filename);
7703
 
          }
7704
 
        i=ping_info->num_text++;
7705
 
        if (i > 255)
7706
 
          (void) ThrowMagickException(&image->exception,GetMagickModule(),
7707
 
            ResourceLimitError,"Cannot write more than 256 PNG text chunks",
7708
 
            "`%s'",image->filename);
7709
 
        ping_info->text[i].key=(char *) property;
7710
 
        ping_info->text[i].text=(char *) value;
7711
 
        ping_info->text[i].text_length=strlen(value);
7712
 
        ping_info->text[i].compression=
7713
 
          image_info->compression == NoCompression ||
7714
 
          (image_info->compression == UndefinedCompression &&
7715
 
          ping_info->text[i].text_length < 128) ? -1 : 0;
7716
 
        if (logging != MagickFalse)
7717
 
          {
7718
 
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7719
 
              "  Setting up text chunk");
7720
 
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
7721
 
              "    keyword: %s",ping_info->text[i].key);
7722
 
          }
7723
 
#endif
7724
7937
      }
7725
7938
    property=GetNextImageProperty(image);
7726
7939
  }
7735
7948
  if (mng_info->need_fram && (int) image->dispose == BackgroundDispose)
7736
7949
    {
7737
7950
      if (mng_info->page.x || mng_info->page.y ||
7738
 
          (ping_info->width != mng_info->page.width) ||
7739
 
          (ping_info->height != mng_info->page.height))
 
7951
          (ping_width != mng_info->page.width) ||
 
7952
          (ping_height != mng_info->page.height))
7740
7953
        {
7741
7954
          unsigned char
7742
7955
            chunk[32];
7757
7970
          chunk[14]=0; /* clipping boundaries delta type */
7758
7971
          PNGLong(chunk+15,(png_uint_32) (mng_info->page.x)); /* left cb */
7759
7972
          PNGLong(chunk+19,
7760
 
             (png_uint_32) (mng_info->page.x + ping_info->width));
 
7973
             (png_uint_32) (mng_info->page.x + ping_width));
7761
7974
          PNGLong(chunk+23,(png_uint_32) (mng_info->page.y)); /* top cb */
7762
7975
          PNGLong(chunk+27,
7763
 
             (png_uint_32) (mng_info->page.y + ping_info->height));
 
7976
             (png_uint_32) (mng_info->page.y + ping_height));
7764
7977
          (void) WriteBlob(image,31,chunk);
7765
7978
          (void) WriteBlobMSBULong(image,crc32(0,chunk,31));
7766
7979
          mng_info->old_framing_mode=4;
7778
7991
 
7779
7992
  /* Save depth actually written */
7780
7993
 
7781
 
  s[0]=(char) ping_info->bit_depth;
 
7994
  s[0]=(char) ping_bit_depth;
7782
7995
  s[1]='\0';
7783
7996
 
7784
7997
  (void) SetImageProperty(image,"png:bit-depth-written",s);
7786
7999
  /*
7787
8000
    Free PNG resources.
7788
8001
  */
7789
 
#if (PNG_LIBPNG_VER < 10007)
7790
 
  if (ping_info->valid & PNG_INFO_PLTE)
7791
 
    {
7792
 
      ping_info->palette=(png_colorp)
7793
 
        RelinquishMagickMemory(ping_info->palette);
7794
 
      ping_info->valid&=(~PNG_INFO_PLTE);
7795
 
    }
7796
 
  if (ping_info->valid & PNG_INFO_tRNS)
7797
 
    {
7798
 
      ping_info->trans_alpha=(unsigned char *) RelinquishMagickMemory(
7799
 
        ping_info->trans_alpha);
7800
 
      ping_info->valid&=(~PNG_INFO_tRNS);
7801
 
    }
7802
 
#endif
7803
8002
 
7804
8003
  png_destroy_write_struct(&ping,&ping_info);
7805
8004
 
7806
8005
  png_pixels=(unsigned char *) RelinquishMagickMemory(png_pixels);
7807
8006
 
7808
8007
#if defined(PNG_SETJMP_NOT_THREAD_SAFE)
7809
 
  (void) UnlockSemaphoreInfo(png_semaphore);
 
8008
  UnlockSemaphoreInfo(png_semaphore);
7810
8009
#endif
7811
8010
 
7812
8011
  if (logging != MagickFalse)
7959
8158
    Allocate a MngInfo structure.
7960
8159
  */
7961
8160
  have_mng_structure=MagickFalse;
7962
 
  mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
 
8161
  mng_info=(MngInfo *) AcquireAlignedMemory(1,sizeof(MngInfo));
7963
8162
  if (mng_info == (MngInfo *) NULL)
7964
8163
    ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
7965
8164
  /*
7977
8176
 
7978
8177
  if (mng_info->write_png8)
7979
8178
    {
7980
 
         mng_info->write_png_colortype = /* 3 */ 4;
7981
 
         mng_info->write_png_depth = 8;
7982
 
         image->depth = 8;
 
8179
      mng_info->write_png_colortype = /* 3 */ 4;
 
8180
      mng_info->write_png_depth = 8;
 
8181
      image->depth = 8;
7983
8182
#if 0 /* this does not work */
7984
 
         if (image->matte == MagickTrue)
7985
 
            (void) SetImageType(image,PaletteMatteType);
7986
 
         else
7987
 
            (void) SetImageType(image,PaletteType);
7988
 
         (void) SyncImage(image);
 
8183
      if (image->matte == MagickTrue)
 
8184
        (void) SetImageType(image,PaletteMatteType);
 
8185
      else
 
8186
        (void) SetImageType(image,PaletteType);
 
8187
      (void) SyncImage(image);
7989
8188
#endif
7990
8189
    }
7991
8190
 
7992
8191
  if (mng_info->write_png24)
7993
8192
    {
7994
 
         mng_info->write_png_colortype = /* 2 */ 3;
7995
 
         mng_info->write_png_depth = 8;
7996
 
         image->depth = 8;
7997
 
         if (image->matte == MagickTrue)
7998
 
            (void) SetImageType(image,TrueColorMatteType);
7999
 
         else
8000
 
            (void) SetImageType(image,TrueColorType);
8001
 
         (void) SyncImage(image);
 
8193
      mng_info->write_png_colortype = /* 2 */ 3;
 
8194
      mng_info->write_png_depth = 8;
 
8195
      image->depth = 8;
 
8196
      if (image->matte == MagickTrue)
 
8197
        (void) SetImageType(image,TrueColorMatteType);
 
8198
      else
 
8199
        (void) SetImageType(image,TrueColorType);
 
8200
      (void) SyncImage(image);
8002
8201
    }
8003
8202
 
8004
8203
  if (mng_info->write_png32)
8005
8204
    {
8006
 
         mng_info->write_png_colortype = /* 6 */  7;
8007
 
         mng_info->write_png_depth = 8;
8008
 
         image->depth = 8;
8009
 
         if (image->matte == MagickTrue)
8010
 
            (void) SetImageType(image,TrueColorMatteType);
8011
 
         else
8012
 
            (void) SetImageType(image,TrueColorType);
8013
 
         (void) SyncImage(image);
 
8205
      mng_info->write_png_colortype = /* 6 */  7;
 
8206
      mng_info->write_png_depth = 8;
 
8207
      image->depth = 8;
 
8208
      if (image->matte == MagickTrue)
 
8209
        (void) SetImageType(image,TrueColorMatteType);
 
8210
      else
 
8211
        (void) SetImageType(image,TrueColorType);
 
8212
      (void) SyncImage(image);
8014
8213
    }
8015
8214
 
8016
8215
  value=GetImageOption(image_info,"png:bit-depth");
8017
8216
  if (value != (char *) NULL)
8018
8217
    {
8019
8218
      if (LocaleCompare(value,"1") == 0)
8020
 
         mng_info->write_png_depth = 1;
 
8219
        mng_info->write_png_depth = 1;
8021
8220
      else if (LocaleCompare(value,"2") == 0)
8022
 
         mng_info->write_png_depth = 2;
 
8221
        mng_info->write_png_depth = 2;
8023
8222
      else if (LocaleCompare(value,"4") == 0)
8024
 
         mng_info->write_png_depth = 4;
 
8223
        mng_info->write_png_depth = 4;
8025
8224
      else if (LocaleCompare(value,"8") == 0)
8026
 
         mng_info->write_png_depth = 8;
 
8225
        mng_info->write_png_depth = 8;
8027
8226
      else if (LocaleCompare(value,"16") == 0)
8028
 
         mng_info->write_png_depth = 16;
 
8227
        mng_info->write_png_depth = 16;
8029
8228
      if (logging != MagickFalse)
8030
 
         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8031
 
         "png:bit-depth=%d was defined.\n",mng_info->write_png_depth);
 
8229
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
8230
          "png:bit-depth=%d was defined.\n",mng_info->write_png_depth);
8032
8231
    }
8033
8232
  value=GetImageOption(image_info,"png:color-type");
8034
8233
  if (value != (char *) NULL)
8035
8234
    {
8036
8235
      /* We must store colortype+1 because 0 is a valid colortype */
8037
8236
      if (LocaleCompare(value,"0") == 0)
8038
 
         mng_info->write_png_colortype = 1;
 
8237
        mng_info->write_png_colortype = 1;
8039
8238
      else if (LocaleCompare(value,"2") == 0)
8040
 
         mng_info->write_png_colortype = 3;
 
8239
        mng_info->write_png_colortype = 3;
8041
8240
      else if (LocaleCompare(value,"3") == 0)
8042
 
         mng_info->write_png_colortype = 4;
 
8241
        mng_info->write_png_colortype = 4;
8043
8242
      else if (LocaleCompare(value,"4") == 0)
8044
 
         mng_info->write_png_colortype = 5;
 
8243
        mng_info->write_png_colortype = 5;
8045
8244
      else if (LocaleCompare(value,"6") == 0)
8046
 
         mng_info->write_png_colortype = 7;
 
8245
        mng_info->write_png_colortype = 7;
8047
8246
      if (logging != MagickFalse)
8048
 
         (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8049
 
         "png:color-type=%d was defined.\n",mng_info->write_png_colortype-1);
 
8247
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
 
8248
          "png:color-type=%d was defined.\n",mng_info->write_png_colortype-1);
8050
8249
    }
8051
8250
 
8052
8251
  status=WriteOnePNGImage(mng_info,image_info,image);
8089
8288
    logging,
8090
8289
    transparent;
8091
8290
 
8092
 
  unsigned long
 
8291
  size_t
8093
8292
    jng_quality;
8094
8293
 
8095
8294
  logging=LogMagickEvent(CoderEvent,GetMagickModule(),
8194
8393
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8195
8394
              "  Creating blob.");
8196
8395
          blob=ImageToBlob(jpeg_image_info,jpeg_image,&length,
8197
 
             &image->exception);
 
8396
           &image->exception);
8198
8397
          jng_alpha_sample_depth=8;
8199
8398
          if (logging != MagickFalse)
8200
8399
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8201
 
              "  Successfully read jpeg_image into a blob, length=%lu.",
8202
 
              (unsigned long) length);
 
8400
              "  Successfully read jpeg_image into a blob, length=%.20g.",
 
8401
              (double) length);
8203
8402
 
8204
8403
        }
8205
8404
      /* Destroy JPEG image and image_info */
8212
8411
  (void) WriteBlobMSBULong(image,16L);  /* chunk data length=16 */
8213
8412
  PNGType(chunk,mng_JHDR);
8214
8413
  LogPNGChunk((int) logging,mng_JHDR,16L);
8215
 
  PNGLong(chunk+4,image->columns);
8216
 
  PNGLong(chunk+8,image->rows);
 
8414
  PNGLong(chunk+4,(png_uint_32) image->columns);
 
8415
  PNGLong(chunk+8,(png_uint_32) image->rows);
8217
8416
  chunk[12]=jng_color_type;
8218
8417
  chunk[13]=8;  /* sample depth */
8219
8418
  chunk[14]=8; /*jng_image_compression_method */
8227
8426
  if (logging != MagickFalse)
8228
8427
    {
8229
8428
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8230
 
        "    JNG width:%15lu",image->columns);
 
8429
        "    JNG width:%15lu",(unsigned long) image->columns);
8231
8430
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8232
 
        "    JNG height:%14lu",image->rows);
 
8431
        "    JNG height:%14lu",(unsigned long) image->rows);
8233
8432
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8234
8433
        "    JNG color type:%10d",jng_color_type);
8235
8434
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8266
8465
      green,
8267
8466
      red;
8268
8467
 
8269
 
    long
 
8468
    ssize_t
8270
8469
      num_bytes;
8271
8470
 
8272
8471
    if (jng_color_type == 8 || jng_color_type == 12)
8273
8472
      num_bytes=6L;
8274
8473
    else
8275
8474
      num_bytes=10L;
8276
 
    (void) WriteBlobMSBULong(image,(unsigned long) (num_bytes-4L));
 
8475
    (void) WriteBlobMSBULong(image,(size_t) (num_bytes-4L));
8277
8476
    PNGType(chunk,mng_bKGD);
8278
 
    LogPNGChunk((int) logging,mng_bKGD,(unsigned long) (num_bytes-4L));
 
8477
    LogPNGChunk((int) logging,mng_bKGD,(size_t) (num_bytes-4L));
8279
8478
    red=ScaleQuantumToChar(image->background_color.red);
8280
8479
    green=ScaleQuantumToChar(image->background_color.green);
8281
8480
    blue=ScaleQuantumToChar(image->background_color.blue);
8314
8513
          (void) WriteBlobMSBULong(image,4L);
8315
8514
          PNGType(chunk,mng_gAMA);
8316
8515
          LogPNGChunk((int) logging,mng_gAMA,4L);
8317
 
          PNGLong(chunk+4,(unsigned long) (100000*image->gamma+0.5));
 
8516
          PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5));
8318
8517
          (void) WriteBlob(image,8,chunk);
8319
8518
          (void) WriteBlobMSBULong(image,crc32(0,chunk,8));
8320
8519
        }
8331
8530
          PNGType(chunk,mng_cHRM);
8332
8531
          LogPNGChunk((int) logging,mng_cHRM,32L);
8333
8532
          primary=image->chromaticity.white_point;
8334
 
          PNGLong(chunk+4,(unsigned long) (100000*primary.x+0.5));
8335
 
          PNGLong(chunk+8,(unsigned long) (100000*primary.y+0.5));
 
8533
          PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5));
 
8534
          PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5));
8336
8535
          primary=image->chromaticity.red_primary;
8337
 
          PNGLong(chunk+12,(unsigned long) (100000*primary.x+0.5));
8338
 
          PNGLong(chunk+16,(unsigned long) (100000*primary.y+0.5));
 
8536
          PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5));
 
8537
          PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5));
8339
8538
          primary=image->chromaticity.green_primary;
8340
 
          PNGLong(chunk+20,(unsigned long) (100000*primary.x+0.5));
8341
 
          PNGLong(chunk+24,(unsigned long) (100000*primary.y+0.5));
 
8539
          PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5));
 
8540
          PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5));
8342
8541
          primary=image->chromaticity.blue_primary;
8343
 
          PNGLong(chunk+28,(unsigned long) (100000*primary.x+0.5));
8344
 
          PNGLong(chunk+32,(unsigned long) (100000*primary.y+0.5));
 
8542
          PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5));
 
8543
          PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5));
8345
8544
          (void) WriteBlob(image,36,chunk);
8346
8545
          (void) WriteBlobMSBULong(image,crc32(0,chunk,36));
8347
8546
        }
8356
8555
      LogPNGChunk((int) logging,mng_pHYs,9L);
8357
8556
      if (image->units == PixelsPerInchResolution)
8358
8557
        {
8359
 
          PNGLong(chunk+4,(unsigned long)
 
8558
          PNGLong(chunk+4,(png_uint_32)
8360
8559
            (image->x_resolution*100.0/2.54+0.5));
8361
 
          PNGLong(chunk+8,(unsigned long)
 
8560
          PNGLong(chunk+8,(png_uint_32)
8362
8561
            (image->y_resolution*100.0/2.54+0.5));
8363
8562
          chunk[12]=1;
8364
8563
        }
8366
8565
        {
8367
8566
          if (image->units == PixelsPerCentimeterResolution)
8368
8567
            {
8369
 
              PNGLong(chunk+4,(unsigned long)
 
8568
              PNGLong(chunk+4,(png_uint_32)
8370
8569
                (image->x_resolution*100.0+0.5));
8371
 
              PNGLong(chunk+8,(unsigned long)
 
8570
              PNGLong(chunk+8,(png_uint_32)
8372
8571
                (image->y_resolution*100.0+0.5));
8373
8572
              chunk[12]=1;
8374
8573
            }
8375
8574
          else
8376
8575
            {
8377
 
              PNGLong(chunk+4,(unsigned long) (image->x_resolution+0.5));
8378
 
              PNGLong(chunk+8,(unsigned long) (image->y_resolution+0.5));
 
8576
              PNGLong(chunk+4,(png_uint_32) (image->x_resolution+0.5));
 
8577
              PNGLong(chunk+8,(png_uint_32) (image->y_resolution+0.5));
8379
8578
              chunk[12]=0;
8380
8579
            }
8381
8580
        }
8391
8590
      (void) WriteBlobMSBULong(image,9L);
8392
8591
      PNGType(chunk,mng_oFFs);
8393
8592
      LogPNGChunk((int) logging,mng_oFFs,9L);
8394
 
      PNGsLong(chunk+4,(long) (image->page.x));
8395
 
      PNGsLong(chunk+8,(long) (image->page.y));
 
8593
      PNGsLong(chunk+4,(ssize_t) (image->page.x));
 
8594
      PNGsLong(chunk+8,(ssize_t) (image->page.y));
8396
8595
      chunk[12]=0;
8397
8596
      (void) WriteBlob(image,13,chunk);
8398
8597
      (void) WriteBlobMSBULong(image,crc32(0,chunk,13));
8414
8613
    {
8415
8614
      if (jng_alpha_compression_method==0)
8416
8615
        {
8417
 
          register long
 
8616
          register ssize_t
8418
8617
            i;
8419
8618
 
8420
 
          long
 
8619
          ssize_t
8421
8620
            len;
8422
8621
 
8423
8622
          /* Write IDAT chunk header */
8424
8623
          if (logging != MagickFalse)
8425
8624
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8426
 
              "  Write IDAT chunks from blob, length=%lu.",
8427
 
              (unsigned long) length);
 
8625
              "  Write IDAT chunks from blob, length=%.20g.",(double)
 
8626
              length);
8428
8627
 
8429
8628
          /* Copy IDAT chunks */
8430
8629
          len=0;
8431
8630
          p=blob+8;
8432
 
          for (i=8; i<(long) length; i+=len+12)
 
8631
          for (i=8; i<(ssize_t) length; i+=len+12)
8433
8632
          {
8434
8633
            len=(*p<<24)|((*(p+1))<<16)|((*(p+2))<<8)|(*(p+3));
8435
8634
            p+=4;
8436
8635
            if (*(p)==73 && *(p+1)==68 && *(p+2)==65 && *(p+3)==84) /* IDAT */
8437
8636
              {
8438
8637
                /* Found an IDAT chunk. */
8439
 
                (void) WriteBlobMSBULong(image,(unsigned long) len);
8440
 
                LogPNGChunk((int) logging,mng_IDAT,(unsigned long) len);
 
8638
                (void) WriteBlobMSBULong(image,(size_t) len);
 
8639
                LogPNGChunk((int) logging,mng_IDAT,(size_t) len);
8441
8640
                (void) WriteBlob(image,(size_t) len+4,p);
8442
8641
                (void) WriteBlobMSBULong(image,
8443
8642
                    crc32(0,p,(uInt) len+4));
8446
8645
              {
8447
8646
                if (logging != MagickFalse)
8448
8647
                  (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8449
 
                    "    Skipping %c%c%c%c chunk, length=%lu.",
8450
 
                    *(p),*(p+1),*(p+2),*(p+3),len);
 
8648
                    "    Skipping %c%c%c%c chunk, length=%.20g.",
 
8649
                    *(p),*(p+1),*(p+2),*(p+3),(double) len);
8451
8650
              }
8452
8651
            p+=(8+len);
8453
8652
          }
8457
8656
          /* Write JDAA chunk header */
8458
8657
          if (logging != MagickFalse)
8459
8658
            (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8460
 
              "  Write JDAA chunk, length=%lu.",
8461
 
              (unsigned long) length);
8462
 
          (void) WriteBlobMSBULong(image,(unsigned long) length);
 
8659
              "  Write JDAA chunk, length=%.20g.",(double) length);
 
8660
          (void) WriteBlobMSBULong(image,(size_t) length);
8463
8661
          PNGType(chunk,mng_JDAA);
8464
8662
          LogPNGChunk((int) logging,mng_JDAA,length);
8465
8663
          /* Write JDAT chunk(s) data */
8497
8695
 
8498
8696
  if (logging != MagickFalse)
8499
8697
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8500
 
      "  Created jpeg_image, %lu x %lu.",jpeg_image->columns,
8501
 
      jpeg_image->rows);
 
8698
      "  Created jpeg_image, %.20g x %.20g.",(double) jpeg_image->columns,
 
8699
      (double) jpeg_image->rows);
8502
8700
 
8503
8701
  if (jng_color_type == 8 || jng_color_type == 12)
8504
8702
    jpeg_image_info->type=GrayscaleType;
8512
8710
  if (logging != MagickFalse)
8513
8711
    {
8514
8712
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8515
 
        "  Successfully read jpeg_image into a blob, length=%lu.",
8516
 
        (unsigned long) length);
 
8713
        "  Successfully read jpeg_image into a blob, length=%.20g.",
 
8714
        (double) length);
8517
8715
 
8518
8716
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8519
 
        "  Write JDAT chunk, length=%lu.",
8520
 
        (unsigned long) length);
 
8717
        "  Write JDAT chunk, length=%.20g.",(double) length);
8521
8718
    }
8522
8719
  /* Write JDAT chunk(s) */
8523
 
  (void) WriteBlobMSBULong(image,(unsigned long) length);
 
8720
  (void) WriteBlobMSBULong(image,(size_t) length);
8524
8721
  PNGType(chunk,mng_JDAT);
8525
8722
  LogPNGChunk((int) logging,mng_JDAT,length);
8526
8723
  (void) WriteBlob(image,4,chunk);
8607
8804
    Allocate a MngInfo structure.
8608
8805
  */
8609
8806
  have_mng_structure=MagickFalse;
8610
 
  mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
 
8807
  mng_info=(MngInfo *) AcquireAlignedMemory(1,sizeof(MngInfo));
8611
8808
  if (mng_info == (MngInfo *) NULL)
8612
8809
    ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
8613
8810
  /*
8663
8860
    optimize,
8664
8861
    use_global_plte;
8665
8862
 
8666
 
  register long
 
8863
  register ssize_t
8667
8864
    i;
8668
8865
 
8669
8866
  unsigned char
8673
8870
    write_jng,
8674
8871
    write_mng;
8675
8872
 
8676
 
  volatile unsigned long
 
8873
  volatile size_t
8677
8874
    scene;
8678
8875
 
8679
 
  unsigned long
 
8876
  size_t
8680
8877
    final_delay=0,
8681
8878
    initial_delay;
8682
8879
 
8683
 
#if (PNG_LIBPNG_VER < 10007)
 
8880
#if (PNG_LIBPNG_VER < 10200)
8684
8881
    if (image_info->verbose)
8685
8882
      printf("Your PNG library (libpng-%s) is rather old.\n",
8686
8883
         PNG_LIBPNG_VER_STRING);
8703
8900
    Allocate a MngInfo structure.
8704
8901
  */
8705
8902
  have_mng_structure=MagickFalse;
8706
 
  mng_info=(MngInfo *) AcquireMagickMemory(sizeof(MngInfo));
 
8903
  mng_info=(MngInfo *) AcquireAlignedMemory(1,sizeof(MngInfo));
8707
8904
  if (mng_info == (MngInfo *) NULL)
8708
8905
    ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
8709
8906
  /*
8757
8954
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8758
8955
          "    Optimize: FALSE");
8759
8956
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8760
 
        "    Image_info depth: %ld",image_info->depth);
 
8957
        "    Image_info depth: %.20g",(double) image_info->depth);
8761
8958
      (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8762
8959
        "    Type: %d",image_info->type);
8763
8960
 
8765
8962
      for (p=image; p != (Image *) NULL; p=GetNextImageInList(p))
8766
8963
      {
8767
8964
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8768
 
          "    Scene: %ld",scene++);
 
8965
          "    Scene: %.20g",(double) scene++);
8769
8966
        (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8770
 
          "      Image depth: %lu",p->depth);
 
8967
          "      Image depth: %.20g",(double) p->depth);
8771
8968
        if (p->matte)
8772
8969
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8773
8970
            "      Matte: True");
8782
8979
            "      Storage class: DirectClass");
8783
8980
        if (p->colors)
8784
8981
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8785
 
            "      Number of colors: %lu",p->colors);
 
8982
            "      Number of colors: %.20g",(double) p->colors);
8786
8983
        else
8787
8984
          (void) LogMagickEvent(CoderEvent,GetMagickModule(),
8788
8985
            "      Number of colors: unspecified");
8827
9024
              {
8828
9025
                p->colors=0;
8829
9026
                if (p->matte != MagickFalse)
8830
 
                    (void) SetImageType(p,PaletteMatteType);
 
9027
                  (void) SetImageType(p,PaletteMatteType);
8831
9028
                else
8832
 
                    (void) SetImageType(p,PaletteType);
 
9029
                  (void) SetImageType(p,PaletteType);
8833
9030
              }
8834
9031
          }
8835
9032
        if (mng_info->adjoin == MagickFalse)
9008
9205
               mng_info->ticks_per_second=0;
9009
9206
           }
9010
9207
         if (final_delay != 0)
9011
 
           mng_info->ticks_per_second=1UL*image->ticks_per_second/final_delay;
 
9208
           mng_info->ticks_per_second=(png_uint_32) (image->ticks_per_second/final_delay);
9012
9209
         if (final_delay > 50)
9013
9210
           mng_info->ticks_per_second=2;
9014
9211
         if (final_delay > 75)
9035
9232
     (void) WriteBlobMSBULong(image,28L);  /* chunk data length=28 */
9036
9233
     PNGType(chunk,mng_MHDR);
9037
9234
     LogPNGChunk((int) logging,mng_MHDR,28L);
9038
 
     PNGLong(chunk+4,mng_info->page.width);
9039
 
     PNGLong(chunk+8,mng_info->page.height);
 
9235
     PNGLong(chunk+4,(png_uint_32) mng_info->page.width);
 
9236
     PNGLong(chunk+8,(png_uint_32) mng_info->page.height);
9040
9237
     PNGLong(chunk+12,mng_info->ticks_per_second);
9041
9238
     PNGLong(chunk+16,0L);  /* layer count=unknown */
9042
9239
     PNGLong(chunk+20,0L);  /* frame count=unknown */
9088
9285
         */
9089
9286
         PNGType(chunk,mng_nEED);
9090
9287
         length=CopyMagickString((char *) chunk+4,"CACHEOFF",20);
9091
 
         (void) WriteBlobMSBULong(image,(unsigned long) length);
9092
 
         LogPNGChunk((int) logging,mng_nEED,(unsigned long) length);
 
9288
         (void) WriteBlobMSBULong(image,(size_t) length);
 
9289
         LogPNGChunk((int) logging,mng_nEED,(size_t) length);
9093
9290
         length+=4;
9094
9291
         (void) WriteBlob(image,length,chunk);
9095
9292
         (void) WriteBlobMSBULong(image,crc32(0,chunk,(uInt) length));
9115
9312
         if (logging != MagickFalse)
9116
9313
           {
9117
9314
             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9118
 
               "     TERM delay: %lu",
9119
 
               (png_uint_32) (mng_info->ticks_per_second*
9120
 
                  final_delay/MagickMax(image->ticks_per_second,1)));
 
9315
               "     TERM delay: %.20g",(double) (mng_info->ticks_per_second*
 
9316
              final_delay/MagickMax(image->ticks_per_second,1)));
9121
9317
             if (image->iterations == 0)
9122
9318
               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9123
 
                 "     TERM iterations: %lu",PNG_UINT_31_MAX);
 
9319
                 "     TERM iterations: %.20g",(double) PNG_UINT_31_MAX);
9124
9320
             else
9125
9321
               (void) LogMagickEvent(CoderEvent,GetMagickModule(),
9126
 
                 "     Image iterations: %lu",image->iterations);
 
9322
                 "     Image iterations: %.20g",(double) image->iterations);
9127
9323
           }
9128
9324
         (void) WriteBlob(image,14,chunk);
9129
9325
         (void) WriteBlobMSBULong(image,crc32(0,chunk,14));
9158
9354
             (void) WriteBlobMSBULong(image,4L);
9159
9355
             PNGType(chunk,mng_gAMA);
9160
9356
             LogPNGChunk((int) logging,mng_gAMA,4L);
9161
 
             PNGLong(chunk+4,(unsigned long) (100000*image->gamma+0.5));
 
9357
             PNGLong(chunk+4,(png_uint_32) (100000*image->gamma+0.5));
9162
9358
             (void) WriteBlob(image,8,chunk);
9163
9359
             (void) WriteBlobMSBULong(image,crc32(0,chunk,8));
9164
9360
             mng_info->have_write_global_gama=MagickTrue;
9175
9371
             PNGType(chunk,mng_cHRM);
9176
9372
             LogPNGChunk((int) logging,mng_cHRM,32L);
9177
9373
             primary=image->chromaticity.white_point;
9178
 
             PNGLong(chunk+4,(unsigned long) (100000*primary.x+0.5));
9179
 
             PNGLong(chunk+8,(unsigned long) (100000*primary.y+0.5));
 
9374
             PNGLong(chunk+4,(png_uint_32) (100000*primary.x+0.5));
 
9375
             PNGLong(chunk+8,(png_uint_32) (100000*primary.y+0.5));
9180
9376
             primary=image->chromaticity.red_primary;
9181
 
             PNGLong(chunk+12,(unsigned long) (100000*primary.x+0.5));
9182
 
             PNGLong(chunk+16,(unsigned long) (100000*primary.y+0.5));
 
9377
             PNGLong(chunk+12,(png_uint_32) (100000*primary.x+0.5));
 
9378
             PNGLong(chunk+16,(png_uint_32) (100000*primary.y+0.5));
9183
9379
             primary=image->chromaticity.green_primary;
9184
 
             PNGLong(chunk+20,(unsigned long) (100000*primary.x+0.5));
9185
 
             PNGLong(chunk+24,(unsigned long) (100000*primary.y+0.5));
 
9380
             PNGLong(chunk+20,(png_uint_32) (100000*primary.x+0.5));
 
9381
             PNGLong(chunk+24,(png_uint_32) (100000*primary.y+0.5));
9186
9382
             primary=image->chromaticity.blue_primary;
9187
 
             PNGLong(chunk+28,(unsigned long) (100000*primary.x+0.5));
9188
 
             PNGLong(chunk+32,(unsigned long) (100000*primary.y+0.5));
 
9383
             PNGLong(chunk+28,(png_uint_32) (100000*primary.x+0.5));
 
9384
             PNGLong(chunk+32,(png_uint_32) (100000*primary.y+0.5));
9189
9385
             (void) WriteBlob(image,36,chunk);
9190
9386
             (void) WriteBlobMSBULong(image,crc32(0,chunk,36));
9191
9387
             mng_info->have_write_global_chrm=MagickTrue;
9201
9397
         LogPNGChunk((int) logging,mng_pHYs,9L);
9202
9398
         if (image->units == PixelsPerInchResolution)
9203
9399
           {
9204
 
             PNGLong(chunk+4,(unsigned long)
 
9400
             PNGLong(chunk+4,(png_uint_32)
9205
9401
               (image->x_resolution*100.0/2.54+0.5));
9206
 
             PNGLong(chunk+8,(unsigned long)
 
9402
             PNGLong(chunk+8,(png_uint_32)
9207
9403
               (image->y_resolution*100.0/2.54+0.5));
9208
9404
             chunk[12]=1;
9209
9405
           }
9211
9407
           {
9212
9408
             if (image->units == PixelsPerCentimeterResolution)
9213
9409
               {
9214
 
                 PNGLong(chunk+4,(unsigned long)
 
9410
                 PNGLong(chunk+4,(png_uint_32)
9215
9411
                   (image->x_resolution*100.0+0.5));
9216
 
                 PNGLong(chunk+8,(unsigned long)
 
9412
                 PNGLong(chunk+8,(png_uint_32)
9217
9413
                   (image->y_resolution*100.0+0.5));
9218
9414
                 chunk[12]=1;
9219
9415
               }
9220
9416
             else
9221
9417
               {
9222
 
                 PNGLong(chunk+4,(unsigned long) (image->x_resolution+0.5));
9223
 
                 PNGLong(chunk+8,(unsigned long) (image->y_resolution+0.5));
 
9418
                 PNGLong(chunk+4,(png_uint_32) (image->x_resolution+0.5));
 
9419
                 PNGLong(chunk+8,(png_uint_32) (image->y_resolution+0.5));
9224
9420
                 chunk[12]=0;
9225
9421
               }
9226
9422
           }
9263
9459
         (image->storage_class == PseudoClass) &&
9264
9460
         (all_images_are_gray == MagickFalse))
9265
9461
       {
9266
 
         unsigned long
 
9462
         size_t
9267
9463
           data_length;
9268
9464
 
9269
9465
         /*
9273
9469
         (void) WriteBlobMSBULong(image,data_length);
9274
9470
         PNGType(chunk,mng_PLTE);
9275
9471
         LogPNGChunk((int) logging,mng_PLTE,data_length);
9276
 
         for (i=0; i < (long) image->colors; i++)
 
9472
         for (i=0; i < (ssize_t) image->colors; i++)
9277
9473
         {
9278
9474
           chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red) & 0xff;
9279
9475
           chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green) & 0xff;
9316
9512
                /*
9317
9513
                  Write MNG PLTE chunk
9318
9514
                */
9319
 
                unsigned long
 
9515
                size_t
9320
9516
                  data_length;
9321
9517
 
9322
9518
                data_length=3*image->colors;
9323
9519
                (void) WriteBlobMSBULong(image,data_length);
9324
9520
                PNGType(chunk,mng_PLTE);
9325
9521
                LogPNGChunk((int) logging,mng_PLTE,data_length);
9326
 
                for (i=0; i < (long) image->colors; i++)
 
9522
                for (i=0; i < (ssize_t) image->colors; i++)
9327
9523
                {
9328
9524
                  chunk[4+i*3]=ScaleQuantumToChar(image->colormap[i].red);
9329
9525
                  chunk[5+i*3]=ScaleQuantumToChar(image->colormap[i].green);
9341
9537
#endif
9342
9538
    if (need_defi)
9343
9539
      {
9344
 
        long
 
9540
        ssize_t
9345
9541
          previous_x,
9346
9542
          previous_y;
9347
9543
 
9414
9610
             image->delay)/MagickMax(image->ticks_per_second,1)));
9415
9611
           (void) WriteBlob(image,14,chunk);
9416
9612
           (void) WriteBlobMSBULong(image,crc32(0,chunk,14));
9417
 
           mng_info->delay=image->delay;
 
9613
           mng_info->delay=(png_uint_32) image->delay;
9418
9614
         }
9419
9615
       mng_info->old_framing_mode=mng_info->framing_mode;
9420
9616
     }
9480
9676
    (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit WriteMNGImage()");
9481
9677
  return(MagickTrue);
9482
9678
}
9483
 
#else /* PNG_LIBPNG_VER > 95 */
 
9679
#else /* PNG_LIBPNG_VER > 10011 */
9484
9680
static MagickBooleanType WritePNGImage(const ImageInfo *image_info,Image *image)
9485
9681
{
9486
9682
  image=image;
9493
9689
{
9494
9690
  return(WritePNGImage(image_info,image));
9495
9691
}
9496
 
#endif /* PNG_LIBPNG_VER > 95 */
 
9692
#endif /* PNG_LIBPNG_VER > 10011 */
9497
9693
#endif