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

« back to all changes in this revision

Viewing changes to magick/quantize.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2011-06-15 11:05:28 UTC
  • mfrom: (6.2.11 sid)
  • Revision ID: james.westby@ubuntu.com-20110615110528-08jgo07a4846xh8d
Tags: 8:6.6.0.4-3ubuntu1
* Resynchronise with Debian (LP: #797595).  Remaining changes:
  - Make ufraw-batch (universe) a suggestion instead of a recommendation.
  - Make debian/rules install target depend on check; they cannot reliably
    be run in parallel.
  - Don't set MAKEFLAGS in debian/rules; just pass it to the build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
178
178
#include "magick/cache-view.h"
179
179
#include "magick/color.h"
180
180
#include "magick/color-private.h"
181
 
#include "magick/colormap.h"
182
181
#include "magick/colorspace.h"
183
182
#include "magick/enhance.h"
184
183
#include "magick/exception.h"
232
231
  MagickRealType
233
232
    quantize_error;
234
233
 
235
 
  size_t
 
234
  unsigned long
236
235
    color_number,
237
236
    id,
238
237
    level;
252
251
  NodeInfo
253
252
    *root;
254
253
 
255
 
  size_t
 
254
  unsigned long
256
255
    colors,
257
256
    maximum_colors;
258
257
 
259
 
  ssize_t
 
258
  long
260
259
    transparent_index;
261
260
 
262
261
  MagickSizeType
270
269
    pruning_threshold,
271
270
    next_threshold;
272
271
 
273
 
  size_t
 
272
  unsigned long
274
273
    nodes,
275
274
    free_nodes,
276
275
    color_number;
281
280
  Nodes
282
281
    *node_queue;
283
282
 
284
 
  ssize_t
 
283
  long
285
284
    *cache;
286
285
 
287
286
  RealPixelPacket
296
295
  MagickBooleanType
297
296
    associate_alpha;
298
297
 
299
 
  ssize_t
 
298
  long
300
299
    x,
301
300
    y;
302
301
 
303
 
  size_t
 
302
  unsigned long
304
303
    depth;
305
304
 
306
305
  MagickOffsetType
314
313
  Method prototypes.
315
314
*/
316
315
static CubeInfo
317
 
  *GetCubeInfo(const QuantizeInfo *,const size_t,const size_t);
 
316
  *GetCubeInfo(const QuantizeInfo *,const unsigned long,const unsigned long);
318
317
 
319
318
static NodeInfo
320
 
  *GetNodeInfo(CubeInfo *,const size_t,const size_t,NodeInfo *);
 
319
  *GetNodeInfo(CubeInfo *,const unsigned long,const unsigned long,NodeInfo *);
321
320
 
322
321
static MagickBooleanType
323
322
  AssignImageColors(Image *,CubeInfo *),
325
324
  DitherImage(Image *,CubeInfo *),
326
325
  SetGrayscaleImage(Image *);
327
326
 
328
 
static size_t
 
327
static unsigned long
329
328
  DefineImageColormap(Image *,CubeInfo *,NodeInfo *);
330
329
 
331
330
static void
452
451
  return((Quantum) (value+0.5));
453
452
}
454
453
 
455
 
static inline size_t ColorToNodeId(const CubeInfo *cube_info,
456
 
  const RealPixelPacket *pixel,size_t index)
 
454
static inline unsigned long ColorToNodeId(const CubeInfo *cube_info,
 
455
  const RealPixelPacket *pixel,unsigned long index)
457
456
{
458
 
  size_t
 
457
  unsigned long
459
458
    id;
460
459
 
461
 
  id=(size_t) (
 
460
  id=(unsigned long) (
462
461
    ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->red)) >> index) & 0x1) |
463
462
    ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->green)) >> index) & 0x1) << 1 |
464
463
    ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->blue)) >> index) & 0x1) << 2);
482
481
{
483
482
#define AssignImageTag  "Assign/Image"
484
483
 
485
 
  ssize_t
 
484
  long
486
485
    y;
487
486
 
488
487
  MagickBooleanType
491
490
  RealPixelPacket
492
491
    pixel;
493
492
 
494
 
  register ssize_t
 
493
  register long
495
494
    i,
496
495
    x;
497
496
 
501
500
  ssize_t
502
501
    count;
503
502
 
504
 
  size_t
 
503
  unsigned long
505
504
    id,
506
505
    index;
507
506
 
540
539
 
541
540
      exception=(&image->exception);
542
541
      image_view=AcquireCacheView(image);
543
 
      for (y=0; y < (ssize_t) image->rows; y++)
 
542
      for (y=0; y < (long) image->rows; y++)
544
543
      {
545
544
        register IndexPacket
546
545
          *restrict indexes;
553
552
        if (q == (PixelPacket *) NULL)
554
553
          break;
555
554
        indexes=GetCacheViewAuthenticIndexQueue(image_view);
556
 
        for (x=0; x < (ssize_t) image->columns; x+=count)
 
555
        for (x=0; x < (long) image->columns; x+=count)
557
556
        {
558
557
          /*
559
558
            Identify the deepest node containing the pixel's color.
560
559
          */
561
 
          for (count=1; (x+count) < (ssize_t) image->columns; count++)
 
560
          for (count=1; (x+count) < (long) image->columns; count++)
562
561
            if (IsSameColor(image,q,q+count) == MagickFalse)
563
562
              break;
564
563
          AssociateAlphaPixel(cube_info,q,&pixel);
565
564
          node_info=cube_info->root;
566
 
          for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
 
565
          for (index=MaxTreeDepth-1; (long) index > 0; index--)
567
566
          {
568
567
            id=ColorToNodeId(cube_info,&pixel,index);
569
568
            if (node_info->child[id] == (NodeInfo *) NULL)
578
577
            (QuantumRange+1.0)+1.0);
579
578
          ClosestColor(image,cube_info,node_info->parent);
580
579
          index=cube_info->color_number;
581
 
          for (i=0; i < (ssize_t) count; i++)
 
580
          for (i=0; i < (long) count; i++)
582
581
          {
583
582
            if (image->storage_class == PseudoClass)
584
583
              indexes[x+i]=(IndexPacket) index;
595
594
        }
596
595
        if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
597
596
          break;
598
 
        proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y,
599
 
          image->rows);
 
597
        proceed=SetImageProgress(image,AssignImageTag,y,image->rows);
600
598
        if (proceed == MagickFalse)
601
599
          break;
602
600
      }
617
615
        Monochrome image.
618
616
      */
619
617
      q=image->colormap;
620
 
      for (i=0; i < (ssize_t) image->colors; i++)
 
618
      for (i=0; i < (long) image->colors; i++)
621
619
      {
622
620
        intensity=(Quantum) (PixelIntensity(q) < ((MagickRealType)
623
621
          QuantumRange/2.0) ? 0 : QuantumRange);
717
715
  CacheView
718
716
    *image_view;
719
717
 
720
 
  ssize_t
 
718
  long
721
719
    y;
722
720
 
723
721
  MagickBooleanType
738
736
  size_t
739
737
    count;
740
738
 
741
 
  size_t
 
739
  unsigned long
742
740
    id,
743
741
    index,
744
742
    level;
762
760
  midpoint.opacity=(MagickRealType) QuantumRange/2.0;
763
761
  error.opacity=0.0;
764
762
  image_view=AcquireCacheView(image);
765
 
  for (y=0; y < (ssize_t) image->rows; y++)
 
763
  for (y=0; y < (long) image->rows; y++)
766
764
  {
767
765
    register const PixelPacket
768
766
      *restrict p;
769
767
 
770
 
    register ssize_t
 
768
    register long
771
769
      x;
772
770
 
773
771
    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
781
779
        PruneLevel(image,cube_info,cube_info->root);
782
780
        cube_info->depth--;
783
781
      }
784
 
    for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count)
 
782
    for (x=0; x < (long) image->columns; x+=(long) count)
785
783
    {
786
784
      /*
787
785
        Start at the root and descend the color cube tree.
788
786
      */
789
 
      for (count=1; (x+count) < (ssize_t) image->columns; count++)
 
787
      for (count=1; (x+count) < image->columns; count++)
790
788
        if (IsSameColor(image,p,p+count) == MagickFalse)
791
789
          break;
792
790
      AssociateAlphaPixel(cube_info,p,&pixel);
846
844
        PruneToCubeDepth(image,cube_info,cube_info->root);
847
845
        break;
848
846
      }
849
 
    proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y,
850
 
      image->rows);
 
847
    proceed=SetImageProgress(image,ClassifyImageTag,y,image->rows);
851
848
    if (proceed == MagickFalse)
852
849
      break;
853
850
  }
854
 
  for (y++; y < (ssize_t) image->rows; y++)
 
851
  for (y++; y < (long) image->rows; y++)
855
852
  {
856
853
    register const PixelPacket
857
854
      *restrict p;
858
855
 
859
 
    register ssize_t
 
856
    register long
860
857
      x;
861
858
 
862
859
    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
870
867
        PruneLevel(image,cube_info,cube_info->root);
871
868
        cube_info->depth--;
872
869
      }
873
 
    for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count)
 
870
    for (x=0; x < (long) image->columns; x+=(long) count)
874
871
    {
875
872
      /*
876
873
        Start at the root and descend the color cube tree.
877
874
      */
878
 
      for (count=1; (x+count) < (ssize_t) image->columns; count++)
 
875
      for (count=1; (x+count) < image->columns; count++)
879
876
        if (IsSameColor(image,p,p+count) == MagickFalse)
880
877
          break;
881
878
      AssociateAlphaPixel(cube_info,p,&pixel);
930
927
        node_info->total_color.opacity+=count*QuantumScale*pixel.opacity;
931
928
      p+=count;
932
929
    }
933
 
    proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y,
934
 
      image->rows);
 
930
    proceed=SetImageProgress(image,ClassifyImageTag,y,image->rows);
935
931
    if (proceed == MagickFalse)
936
932
      break;
937
933
  }
1020
1016
static void ClosestColor(const Image *image,CubeInfo *cube_info,
1021
1017
  const NodeInfo *node_info)
1022
1018
{
1023
 
  register ssize_t
 
1019
  register long
1024
1020
    i;
1025
1021
 
1026
 
  size_t
 
1022
  unsigned long
1027
1023
    number_children;
1028
1024
 
1029
1025
  /*
1030
1026
    Traverse any children.
1031
1027
  */
1032
1028
  number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
1033
 
  for (i=0; i < (ssize_t) number_children; i++)
 
1029
  for (i=0; i < (long) number_children; i++)
1034
1030
    if (node_info->child[i] != (NodeInfo *) NULL)
1035
1031
      ClosestColor(image,cube_info,node_info->child[i]);
1036
1032
  if (node_info->number_unique != 0)
1144
1140
%
1145
1141
%  The format of the DefineImageColormap method is:
1146
1142
%
1147
 
%      size_t DefineImageColormap(Image *image,CubeInfo *cube_info,
 
1143
%      unsigned long DefineImageColormap(Image *image,CubeInfo *cube_info,
1148
1144
%        NodeInfo *node_info)
1149
1145
%
1150
1146
%  A description of each parameter follows.
1157
1153
%      node in the color cube tree that is to be pruned.
1158
1154
%
1159
1155
*/
1160
 
static size_t DefineImageColormap(Image *image,CubeInfo *cube_info,
 
1156
static unsigned long DefineImageColormap(Image *image,CubeInfo *cube_info,
1161
1157
  NodeInfo *node_info)
1162
1158
{
1163
 
  register ssize_t
 
1159
  register long
1164
1160
    i;
1165
1161
 
1166
 
  size_t
 
1162
  unsigned long
1167
1163
    number_children;
1168
1164
 
1169
1165
  /*
1170
1166
    Traverse any children.
1171
1167
  */
1172
1168
  number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
1173
 
  for (i=0; i < (ssize_t) number_children; i++)
 
1169
  for (i=0; i < (long) number_children; i++)
1174
1170
    if (node_info->child[i] != (NodeInfo *) NULL)
1175
 
      (void) DefineImageColormap(image,cube_info,node_info->child[i]);
 
1171
      DefineImageColormap(image,cube_info,node_info->child[i]);
1176
1172
  if (node_info->number_unique != 0)
1177
1173
    {
1178
1174
      register MagickRealType
1231
1227
              if (node_info->number_unique > cube_info->transparent_pixels)
1232
1228
                {
1233
1229
                  cube_info->transparent_pixels=node_info->number_unique;
1234
 
                  cube_info->transparent_index=(ssize_t) image->colors;
 
1230
                  cube_info->transparent_index=(long) image->colors;
1235
1231
                }
1236
1232
            }
1237
1233
        }
1279
1275
      cube_info->node_queue);
1280
1276
    cube_info->node_queue=nodes;
1281
1277
  } while (cube_info->node_queue != (Nodes *) NULL);
1282
 
  if (cube_info->cache != (ssize_t *) NULL)
1283
 
    cube_info->cache=(ssize_t *) RelinquishMagickMemory(cube_info->cache);
 
1278
  if (cube_info->cache != (long *) NULL)
 
1279
    cube_info->cache=(long *) RelinquishMagickMemory(cube_info->cache);
1284
1280
  cube_info->quantize_info=DestroyQuantizeInfo(cube_info->quantize_info);
1285
1281
  cube_info=(CubeInfo *) RelinquishMagickMemory(cube_info);
1286
1282
}
1356
1352
  ExceptionInfo
1357
1353
    *exception;
1358
1354
 
1359
 
  ssize_t
 
1355
  long
1360
1356
    u,
1361
1357
    v,
1362
1358
    y;
1374
1370
  register CubeInfo
1375
1371
    *p;
1376
1372
 
1377
 
  size_t
 
1373
  unsigned long
1378
1374
    index;
1379
1375
 
1380
1376
  /*
1387
1383
  p=cube_info;
1388
1384
  exception=(&image->exception);
1389
1385
  image_view=AcquireCacheView(image);
1390
 
  for (y=0; y < (ssize_t) image->rows; y++)
 
1386
  for (y=0; y < (long) image->rows; y++)
1391
1387
  {
1392
1388
    register IndexPacket
1393
1389
      *restrict indexes;
1394
1390
 
1395
 
    register ssize_t
 
1391
    register long
1396
1392
      i,
1397
1393
      x;
1398
1394
 
1405
1401
    indexes=GetCacheViewAuthenticIndexQueue(image_view);
1406
1402
    current=scanlines+(y & 0x01)*image->columns;
1407
1403
    previous=scanlines+((y+1) & 0x01)*image->columns;
1408
 
    v=(ssize_t) ((y & 0x01) ? -1 : 1);
1409
 
    for (x=0; x < (ssize_t) image->columns; x++)
 
1404
    v=(y & 0x01) ? -1 : 1;
 
1405
    for (x=0; x < (long) image->columns; x++)
1410
1406
    {
1411
 
      u=(y & 0x01) ? (ssize_t) image->columns-1-x : x;
 
1407
      u=(y & 0x01) ? (long) image->columns-1-x : x;
1412
1408
      AssociateAlphaPixel(cube_info,q+u,&pixel);
1413
1409
      if (x > 0)
1414
1410
        {
1420
1416
        }
1421
1417
      if (y > 0)
1422
1418
        {
1423
 
          if (x < (ssize_t) (image->columns-1))
 
1419
          if (x < (long) (image->columns-1))
1424
1420
            {
1425
1421
              pixel.red+=previous[u+v].red/16;
1426
1422
              pixel.green+=previous[u+v].green/16;
1447
1443
      pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue);
1448
1444
      if (cube_info->associate_alpha != MagickFalse)
1449
1445
        pixel.opacity=(MagickRealType) ClampToUnsignedQuantum(pixel.opacity);
1450
 
      i=(ssize_t) ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel.red)) >> CacheShift) |
 
1446
      i=(long) ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel.red)) >> CacheShift) |
1451
1447
        (ScaleQuantumToChar(ClampToUnsignedQuantum(pixel.green)) >> CacheShift) << 6 |
1452
1448
        (ScaleQuantumToChar(ClampToUnsignedQuantum(pixel.blue)) >> CacheShift) << 12);
1453
1449
      if (cube_info->associate_alpha != MagickFalse)
1458
1454
          register NodeInfo
1459
1455
            *node_info;
1460
1456
 
1461
 
          register size_t
 
1457
          register unsigned long
1462
1458
            id;
1463
1459
 
1464
1460
          /*
1465
1461
            Identify the deepest node containing the pixel's color.
1466
1462
          */
1467
1463
          node_info=p->root;
1468
 
          for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
 
1464
          for (index=MaxTreeDepth-1; (long) index > 0; index--)
1469
1465
          {
1470
1466
            id=ColorToNodeId(cube_info,&pixel,index);
1471
1467
            if (node_info->child[id] == (NodeInfo *) NULL)
1479
1475
          p->distance=(MagickRealType) (4.0*(QuantumRange+1.0)*(QuantumRange+
1480
1476
            1.0)+1.0);
1481
1477
          ClosestColor(image,p,node_info->parent);
1482
 
          p->cache[i]=(ssize_t) p->color_number;
 
1478
          p->cache[i]=(long) p->color_number;
1483
1479
        }
1484
1480
      /*
1485
1481
        Assign pixel to closest colormap entry.
1486
1482
      */
1487
 
      index=(size_t) p->cache[i];
 
1483
      index=(unsigned long) p->cache[i];
1488
1484
      if (image->storage_class == PseudoClass)
1489
1485
        indexes[u]=(IndexPacket) index;
1490
1486
      if (cube_info->quantize_info->measure_error == MagickFalse)
1521
1517
  RiemersmaDither(Image *,CacheView *,CubeInfo *,const unsigned int);
1522
1518
 
1523
1519
static void Riemersma(Image *image,CacheView *image_view,CubeInfo *cube_info,
1524
 
  const size_t level,const unsigned int direction)
 
1520
  const unsigned long level,const unsigned int direction)
1525
1521
{
1526
1522
  if (level == 1)
1527
1523
    switch (direction)
1624
1620
  register CubeInfo
1625
1621
    *p;
1626
1622
 
1627
 
  size_t
 
1623
  unsigned long
1628
1624
    index;
1629
1625
 
1630
1626
  p=cube_info;
1631
 
  if ((p->x >= 0) && (p->x < (ssize_t) image->columns) &&
1632
 
      (p->y >= 0) && (p->y < (ssize_t) image->rows))
 
1627
  if ((p->x >= 0) && (p->x < (long) image->columns) &&
 
1628
      (p->y >= 0) && (p->y < (long) image->rows))
1633
1629
    {
1634
1630
      ExceptionInfo
1635
1631
        *exception;
1637
1633
      register IndexPacket
1638
1634
        *restrict indexes;
1639
1635
 
1640
 
      register ssize_t
 
1636
      register long
1641
1637
        i;
1642
1638
 
1643
1639
      register PixelPacket
1665
1661
      pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue);
1666
1662
      if (cube_info->associate_alpha != MagickFalse)
1667
1663
        pixel.opacity=(MagickRealType) ClampToUnsignedQuantum(pixel.opacity);
1668
 
      i=(ssize_t) ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel.red)) >> CacheShift) |
 
1664
      i=(long) ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel.red)) >> CacheShift) |
1669
1665
        (ScaleQuantumToChar(ClampToUnsignedQuantum(pixel.green)) >> CacheShift) << 6 |
1670
1666
        (ScaleQuantumToChar(ClampToUnsignedQuantum(pixel.blue)) >> CacheShift) << 12);
1671
1667
      if (cube_info->associate_alpha != MagickFalse)
1676
1672
          register NodeInfo
1677
1673
            *node_info;
1678
1674
 
1679
 
          register size_t
 
1675
          register unsigned long
1680
1676
            id;
1681
1677
 
1682
1678
          /*
1683
1679
            Identify the deepest node containing the pixel's color.
1684
1680
          */
1685
1681
          node_info=p->root;
1686
 
          for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
 
1682
          for (index=MaxTreeDepth-1; (long) index > 0; index--)
1687
1683
          {
1688
1684
            id=ColorToNodeId(cube_info,&pixel,index);
1689
1685
            if (node_info->child[id] == (NodeInfo *) NULL)
1697
1693
          p->distance=(MagickRealType) (4.0*(QuantumRange+1.0)*((MagickRealType)
1698
1694
            QuantumRange+1.0)+1.0);
1699
1695
          ClosestColor(image,p,node_info->parent);
1700
 
          p->cache[i]=(ssize_t) p->color_number;
 
1696
          p->cache[i]=(long) p->color_number;
1701
1697
        }
1702
1698
      /*
1703
1699
        Assign pixel to closest colormap entry.
1704
1700
      */
1705
 
      index=(size_t) (1*p->cache[i]);
 
1701
      index=(unsigned long) (1*p->cache[i]);
1706
1702
      if (image->storage_class == PseudoClass)
1707
1703
        *indexes=(IndexPacket) index;
1708
1704
      if (cube_info->quantize_info->measure_error == MagickFalse)
1741
1737
  return(MagickTrue);
1742
1738
}
1743
1739
 
1744
 
static inline ssize_t MagickMax(const ssize_t x,const ssize_t y)
 
1740
static inline long MagickMax(const long x,const long y)
1745
1741
{
1746
1742
  if (x > y)
1747
1743
    return(x);
1748
1744
  return(y);
1749
1745
}
1750
1746
 
1751
 
static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
 
1747
static inline long MagickMin(const long x,const long y)
1752
1748
{
1753
1749
  if (x < y)
1754
1750
    return(x);
1763
1759
  MagickBooleanType
1764
1760
    status;
1765
1761
 
1766
 
  register ssize_t
 
1762
  register long
1767
1763
    i;
1768
1764
 
1769
 
  size_t
 
1765
  unsigned long
1770
1766
    depth;
1771
1767
 
1772
1768
  if (cube_info->quantize_info->dither_method == FloydSteinbergDitherMethod)
1778
1774
    sizeof(*cube_info->error));
1779
1775
  cube_info->x=0;
1780
1776
  cube_info->y=0;
1781
 
  i=MagickMax((ssize_t) image->columns,(ssize_t) image->rows);
 
1777
  i=MagickMax((long) image->columns,(long) image->rows);
1782
1778
  for (depth=1; i != 0; depth++)
1783
1779
    i>>=1;
1784
 
  if ((ssize_t) (1L << depth) < MagickMax((ssize_t) image->columns,(ssize_t) image->rows))
 
1780
  if ((long) (1L << depth) < MagickMax((long) image->columns,(long) image->rows))
1785
1781
    depth++;
1786
1782
  cube_info->offset=0;
1787
1783
  cube_info->span=(MagickSizeType) image->columns*image->rows;
1809
1805
%  The format of the GetCubeInfo method is:
1810
1806
%
1811
1807
%      CubeInfo GetCubeInfo(const QuantizeInfo *quantize_info,
1812
 
%        const size_t depth,const size_t maximum_colors)
 
1808
%        const unsigned long depth,const unsigned long maximum_colors)
1813
1809
%
1814
1810
%  A description of each parameter follows.
1815
1811
%
1828
1824
%
1829
1825
*/
1830
1826
static CubeInfo *GetCubeInfo(const QuantizeInfo *quantize_info,
1831
 
  const size_t depth,const size_t maximum_colors)
 
1827
  const unsigned long depth,const unsigned long maximum_colors)
1832
1828
{
1833
1829
  CubeInfo
1834
1830
    *cube_info;
1840
1836
  size_t
1841
1837
    length;
1842
1838
 
1843
 
  register ssize_t
 
1839
  register long
1844
1840
    i;
1845
1841
 
1846
1842
  /*
1870
1866
    Initialize dither resources.
1871
1867
  */
1872
1868
  length=(size_t) (1UL << (4*(8-CacheShift)));
1873
 
  cube_info->cache=(ssize_t *) AcquireQuantumMemory(length,
 
1869
  cube_info->cache=(long *) AcquireQuantumMemory(length,
1874
1870
    sizeof(*cube_info->cache));
1875
 
  if (cube_info->cache == (ssize_t *) NULL)
 
1871
  if (cube_info->cache == (long *) NULL)
1876
1872
    return((CubeInfo *) NULL);
1877
1873
  /*
1878
1874
    Initialize color cache.
1879
1875
  */
1880
 
  for (i=0; i < (ssize_t) length; i++)
 
1876
  for (i=0; i < (long) length; i++)
1881
1877
    cube_info->cache[i]=(-1);
1882
1878
  /*
1883
1879
    Distribute weights along a curve of exponential decay.
1920
1916
%
1921
1917
%  The format of the GetNodeInfo method is:
1922
1918
%
1923
 
%      NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id,
1924
 
%        const size_t level,NodeInfo *parent)
 
1919
%      NodeInfo *GetNodeInfo(CubeInfo *cube_info,const unsigned long id,
 
1920
%        const unsigned long level,NodeInfo *parent)
1925
1921
%
1926
1922
%  A description of each parameter follows.
1927
1923
%
1932
1928
%    o level: Specifies the level in the storage_class the node resides.
1933
1929
%
1934
1930
*/
1935
 
static NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id,
1936
 
  const size_t level,NodeInfo *parent)
 
1931
static NodeInfo *GetNodeInfo(CubeInfo *cube_info,const unsigned long id,
 
1932
  const unsigned long level,NodeInfo *parent)
1937
1933
{
1938
1934
  NodeInfo
1939
1935
    *node_info;
2018
2014
  IndexPacket
2019
2015
    *indexes;
2020
2016
 
2021
 
  ssize_t
 
2017
  long
2022
2018
    y;
2023
2019
 
2024
2020
  MagickRealType
2030
2026
    mean_error,
2031
2027
    mean_error_per_pixel;
2032
2028
 
2033
 
  size_t
 
2029
  unsigned long
2034
2030
    index;
2035
2031
 
2036
2032
  assert(image != (Image *) NULL);
2049
2045
  mean_error=0.0;
2050
2046
  exception=(&image->exception);
2051
2047
  image_view=AcquireCacheView(image);
2052
 
  for (y=0; y < (ssize_t) image->rows; y++)
 
2048
  for (y=0; y < (long) image->rows; y++)
2053
2049
  {
2054
2050
    register const PixelPacket
2055
2051
      *restrict p;
2056
2052
 
2057
 
    register ssize_t
 
2053
    register long
2058
2054
      x;
2059
2055
 
2060
2056
    p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2061
2057
    if (p == (const PixelPacket *) NULL)
2062
2058
      break;
2063
2059
    indexes=GetCacheViewAuthenticIndexQueue(image_view);
2064
 
    for (x=0; x < (ssize_t) image->columns; x++)
 
2060
    for (x=0; x < (long) image->columns; x++)
2065
2061
    {
2066
2062
      index=1UL*indexes[x];
2067
2063
      if (image->matte != MagickFalse)
2147
2143
%
2148
2144
%  The format of the PosterizeImage method is:
2149
2145
%
2150
 
%      MagickBooleanType PosterizeImage(Image *image,const size_t levels,
 
2146
%      MagickBooleanType PosterizeImage(Image *image,const unsigned long levels,
2151
2147
%        const MagickBooleanType dither)
2152
2148
%
2153
2149
%  A description of each parameter follows:
2162
2158
%
2163
2159
*/
2164
2160
MagickExport MagickBooleanType PosterizeImage(Image *image,
2165
 
  const size_t levels,const MagickBooleanType dither)
 
2161
  const unsigned long levels,const MagickBooleanType dither)
2166
2162
{
2167
2163
  CacheView
2168
2164
    *posterize_view;
2176
2172
  IndexPacket
2177
2173
    *indexes;
2178
2174
 
2179
 
  ssize_t
 
2175
  long
2180
2176
    j,
2181
2177
    k,
2182
2178
    l,
2188
2184
  QuantizeInfo
2189
2185
    *quantize_info;
2190
2186
 
2191
 
  register ssize_t
 
2187
  register long
2192
2188
    i;
2193
2189
 
2194
2190
  register PixelPacket
2195
2191
    *restrict q;
2196
2192
 
2197
 
  size_t
2198
 
    length;
2199
 
 
2200
2193
  /*
2201
2194
    Posterize image.
2202
2195
  */
2208
2201
  if (posterize_image == (Image *) NULL)
2209
2202
    return(MagickFalse);
2210
2203
  l=1;
2211
 
  length=(size_t) (levels*levels*levels);
2212
 
  while ((l*l*l) < (ssize_t) MagickMin((ssize_t) length,MaxColormapSize+1))
 
2204
  while ((l*l*l) < (long) MagickMin((long) levels*levels*levels,MaxColormapSize+1))
2213
2205
    l++;
2214
 
  status=SetImageExtent(posterize_image,(size_t) (l*l*l),1);
 
2206
  status=SetImageExtent(posterize_image,(unsigned long) (l*l*l),1);
2215
2207
  if (status == MagickFalse)
2216
2208
    {
2217
2209
      posterize_image=DestroyImage(posterize_image);
2299
2291
  NodeInfo
2300
2292
    *parent;
2301
2293
 
2302
 
  register ssize_t
 
2294
  register long
2303
2295
    i;
2304
2296
 
2305
 
  size_t
 
2297
  unsigned long
2306
2298
    number_children;
2307
2299
 
2308
2300
  /*
2309
2301
    Traverse any children.
2310
2302
  */
2311
2303
  number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
2312
 
  for (i=0; i < (ssize_t) number_children; i++)
 
2304
  for (i=0; i < (long) number_children; i++)
2313
2305
    if (node_info->child[i] != (NodeInfo *) NULL)
2314
2306
      PruneChild(image,cube_info,node_info->child[i]);
2315
2307
  /*
2356
2348
static void PruneLevel(const Image *image,CubeInfo *cube_info,
2357
2349
  const NodeInfo *node_info)
2358
2350
{
2359
 
  register ssize_t
 
2351
  register long
2360
2352
    i;
2361
2353
 
2362
 
  size_t
 
2354
  unsigned long
2363
2355
    number_children;
2364
2356
 
2365
2357
  /*
2366
2358
    Traverse any children.
2367
2359
  */
2368
2360
  number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
2369
 
  for (i=0; i < (ssize_t) number_children; i++)
 
2361
  for (i=0; i < (long) number_children; i++)
2370
2362
    if (node_info->child[i] != (NodeInfo *) NULL)
2371
2363
      PruneLevel(image,cube_info,node_info->child[i]);
2372
2364
  if (node_info->level == cube_info->depth)
2403
2395
static void PruneToCubeDepth(const Image *image,CubeInfo *cube_info,
2404
2396
  const NodeInfo *node_info)
2405
2397
{
2406
 
  register ssize_t
 
2398
  register long
2407
2399
    i;
2408
2400
 
2409
 
  size_t
 
2401
  unsigned long
2410
2402
    number_children;
2411
2403
 
2412
2404
  /*
2413
2405
    Traverse any children.
2414
2406
  */
2415
2407
  number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
2416
 
  for (i=0; i < (ssize_t) number_children; i++)
 
2408
  for (i=0; i < (long) number_children; i++)
2417
2409
    if (node_info->child[i] != (NodeInfo *) NULL)
2418
2410
      PruneToCubeDepth(image,cube_info,node_info->child[i]);
2419
2411
  if (node_info->level > cube_info->depth)
2448
2440
%    o image: the image.
2449
2441
%
2450
2442
*/
2451
 
static MagickBooleanType DirectToColormapImage(Image *image,
2452
 
  ExceptionInfo *exception)
2453
 
{
2454
 
  CacheView
2455
 
    *image_view;
2456
 
 
2457
 
  ssize_t
2458
 
    y;
2459
 
 
2460
 
  MagickBooleanType
2461
 
    status;
2462
 
 
2463
 
  register ssize_t
2464
 
    i;
2465
 
 
2466
 
  size_t
2467
 
    number_colors;
2468
 
 
2469
 
  status=MagickTrue;
2470
 
  number_colors=(size_t) (image->columns*image->rows);
2471
 
  if (AcquireImageColormap(image,number_colors) == MagickFalse)
2472
 
    ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2473
 
      image->filename);
2474
 
  i=0;
2475
 
  image_view=AcquireCacheView(image);
2476
 
  for (y=0; y < (ssize_t) image->rows; y++)
2477
 
  {
2478
 
    MagickBooleanType
2479
 
      proceed;
2480
 
 
2481
 
    register IndexPacket
2482
 
      *restrict indexes;
2483
 
 
2484
 
    register PixelPacket
2485
 
      *restrict q;
2486
 
 
2487
 
    register ssize_t
2488
 
      x;
2489
 
 
2490
 
    q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2491
 
    if (q == (const PixelPacket *) NULL)
2492
 
      break;
2493
 
    indexes=GetCacheViewAuthenticIndexQueue(image_view);
2494
 
    for (x=0; x < (ssize_t) image->columns; x++)
2495
 
    {
2496
 
      indexes[x]=(IndexPacket) i;
2497
 
      image->colormap[i++]=(*q++);
2498
 
    }
2499
 
    if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2500
 
      break;
2501
 
    proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y,
2502
 
      image->rows);
2503
 
    if (proceed == MagickFalse)
2504
 
      status=MagickFalse;
2505
 
  }
2506
 
  image_view=DestroyCacheView(image_view);
2507
 
  return(status);
2508
 
}
2509
 
 
2510
2443
MagickExport MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
2511
2444
  Image *image)
2512
2445
{
2516
2449
  MagickBooleanType
2517
2450
    status;
2518
2451
 
2519
 
  size_t
 
2452
  unsigned long
2520
2453
    depth,
2521
2454
    maximum_colors;
2522
2455
 
2531
2464
    maximum_colors=MaxColormapSize;
2532
2465
  if (maximum_colors > MaxColormapSize)
2533
2466
    maximum_colors=MaxColormapSize;
2534
 
  if ((image->columns*image->rows) <= maximum_colors)
2535
 
    return(DirectToColormapImage(image,&image->exception));
2536
2467
  if ((IsGrayImage(image,&image->exception) != MagickFalse) &&
2537
2468
      (image->matte == MagickFalse))
2538
2469
    (void) SetGrayscaleImage(image);
2542
2473
  depth=quantize_info->tree_depth;
2543
2474
  if (depth == 0)
2544
2475
    {
2545
 
      size_t
 
2476
      unsigned long
2546
2477
        colors;
2547
2478
 
2548
2479
      /*
2620
2551
  MagickProgressMonitor
2621
2552
    progress_monitor;
2622
2553
 
2623
 
  register ssize_t
 
2554
  register long
2624
2555
    i;
2625
2556
 
2626
 
  size_t
 
2557
  unsigned long
2627
2558
    depth,
2628
2559
    maximum_colors,
2629
2560
    number_images;
2651
2582
  depth=quantize_info->tree_depth;
2652
2583
  if (depth == 0)
2653
2584
    {
2654
 
      size_t
 
2585
      unsigned long
2655
2586
        colors;
2656
2587
 
2657
2588
      /*
2683
2614
    if (status == MagickFalse)
2684
2615
      break;
2685
2616
    (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
2686
 
    proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i,
2687
 
      number_images);
 
2617
    proceed=SetImageProgress(image,AssignImageTag,i,number_images);
2688
2618
    if (proceed == MagickFalse)
2689
2619
      break;
2690
2620
    image=GetNextImageInList(image);
2705
2635
          break;
2706
2636
        (void) SetImageProgressMonitor(image,progress_monitor,
2707
2637
          image->client_data);
2708
 
        proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i,
2709
 
          number_images);
 
2638
        proceed=SetImageProgress(image,AssignImageTag,i,number_images);
2710
2639
        if (proceed == MagickFalse)
2711
2640
          break;
2712
2641
        image=GetNextImageInList(image);
2746
2675
static void Reduce(const Image *image,CubeInfo *cube_info,
2747
2676
  const NodeInfo *node_info)
2748
2677
{
2749
 
  register ssize_t
 
2678
  register long
2750
2679
    i;
2751
2680
 
2752
 
  size_t
 
2681
  unsigned long
2753
2682
    number_children;
2754
2683
 
2755
2684
  /*
2756
2685
    Traverse any children.
2757
2686
  */
2758
2687
  number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
2759
 
  for (i=0; i < (ssize_t) number_children; i++)
 
2688
  for (i=0; i < (long) number_children; i++)
2760
2689
    if (node_info->child[i] != (NodeInfo *) NULL)
2761
2690
      Reduce(image,cube_info,node_info->child[i]);
2762
2691
  if (node_info->quantize_error <= cube_info->pruning_threshold)
2837
2766
  MagickOffsetType
2838
2767
    offset;
2839
2768
 
2840
 
  size_t
 
2769
  unsigned long
2841
2770
    span;
2842
2771
 
2843
2772
  cube_info->next_threshold=0.0;
3028
2957
 
3029
2958
static int IntensityCompare(const void *x,const void *y)
3030
2959
{
3031
 
  ssize_t
 
2960
  long
3032
2961
    intensity;
3033
2962
 
3034
2963
  PixelPacket
3037
2966
 
3038
2967
  color_1=(PixelPacket *) x;
3039
2968
  color_2=(PixelPacket *) y;
3040
 
  intensity=PixelIntensityToQuantum(color_1)-(ssize_t)
 
2969
  intensity=PixelIntensityToQuantum(color_1)-(long)
3041
2970
    PixelIntensityToQuantum(color_2);
3042
 
  return((int) intensity);
 
2971
  return(intensity);
3043
2972
}
3044
2973
 
3045
2974
#if defined(__cplusplus) || defined(c_plusplus)
3054
2983
  ExceptionInfo
3055
2984
    *exception;
3056
2985
 
3057
 
  ssize_t
 
2986
  long
3058
2987
    j,
3059
2988
    y;
3060
2989
 
3061
2990
  PixelPacket
3062
2991
    *colormap;
3063
2992
 
3064
 
  ssize_t
 
2993
  long
3065
2994
    *colormap_index;
3066
2995
 
3067
 
  register ssize_t
 
2996
  register long
3068
2997
    i;
3069
2998
 
3070
2999
  MagickBooleanType
3074
3003
  assert(image->signature == MagickSignature);
3075
3004
  if (image->type != GrayscaleType)
3076
3005
    (void) TransformImageColorspace(image,GRAYColorspace);
3077
 
  colormap_index=(ssize_t *) AcquireQuantumMemory(MaxMap+1,
 
3006
  colormap_index=(long *) AcquireQuantumMemory(MaxMap+1,
3078
3007
    sizeof(*colormap_index));
3079
 
  if (colormap_index == (ssize_t *) NULL)
 
3008
  if (colormap_index == (long *) NULL)
3080
3009
    ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3081
3010
      image->filename);
3082
3011
  if (image->storage_class != PseudoClass)
3084
3013
      ExceptionInfo
3085
3014
        *exception;
3086
3015
 
3087
 
      for (i=0; i <= (ssize_t) MaxMap; i++)
 
3016
      for (i=0; i <= (long) MaxMap; i++)
3088
3017
        colormap_index[i]=(-1);
3089
3018
      if (AcquireImageColormap(image,MaxMap+1) == MagickFalse)
3090
3019
        ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3096
3025
#if defined(MAGICKCORE_OPENMP_SUPPORT)
3097
3026
  #pragma omp parallel for schedule(dynamic,4) shared(status)
3098
3027
#endif
3099
 
      for (y=0; y < (ssize_t) image->rows; y++)
 
3028
      for (y=0; y < (long) image->rows; y++)
3100
3029
      {
3101
3030
        register IndexPacket
3102
3031
          *restrict indexes;
3103
3032
 
3104
 
        register ssize_t
 
3033
        register long
3105
3034
          x;
3106
3035
 
3107
3036
        register const PixelPacket
3117
3046
            continue;
3118
3047
          }
3119
3048
        indexes=GetCacheViewAuthenticIndexQueue(image_view);
3120
 
        for (x=0; x < (ssize_t) image->columns; x++)
 
3049
        for (x=0; x < (long) image->columns; x++)
3121
3050
        {
3122
 
          register size_t
 
3051
          register unsigned long
3123
3052
            intensity;
3124
3053
 
3125
3054
          intensity=ScaleQuantumToMap(q->red);
3130
3059
#endif
3131
3060
              if (colormap_index[intensity] < 0)
3132
3061
                {
3133
 
                  colormap_index[intensity]=(ssize_t) image->colors;
 
3062
                  colormap_index[intensity]=(long) image->colors;
3134
3063
                  image->colormap[image->colors]=(*q);
3135
3064
                  image->colors++;
3136
3065
               }
3143
3072
      }
3144
3073
      image_view=DestroyCacheView(image_view);
3145
3074
    }
3146
 
  for (i=0; i < (ssize_t) image->colors; i++)
 
3075
  for (i=0; i < (long) image->colors; i++)
3147
3076
    image->colormap[i].opacity=(unsigned short) i;
3148
3077
  qsort((void *) image->colormap,image->colors,sizeof(PixelPacket),
3149
3078
    IntensityCompare);
3154
3083
      image->filename);
3155
3084
  j=0;
3156
3085
  colormap[j]=image->colormap[0];
3157
 
  for (i=0; i < (ssize_t) image->colors; i++)
 
3086
  for (i=0; i < (long) image->colors; i++)
3158
3087
  {
3159
3088
    if (IsSameColor(image,&colormap[j],&image->colormap[i]) == MagickFalse)
3160
3089
      {
3161
3090
        j++;
3162
3091
        colormap[j]=image->colormap[i];
3163
3092
      }
3164
 
    colormap_index[(ssize_t) image->colormap[i].opacity]=j;
 
3093
    colormap_index[(long) image->colormap[i].opacity]=j;
3165
3094
  }
3166
 
  image->colors=(size_t) (j+1);
 
3095
  image->colors=(unsigned long) (j+1);
3167
3096
  image->colormap=(PixelPacket *) RelinquishMagickMemory(image->colormap);
3168
3097
  image->colormap=colormap;
3169
3098
  status=MagickTrue;
3172
3101
#if defined(MAGICKCORE_OPENMP_SUPPORT)
3173
3102
  #pragma omp parallel for schedule(dynamic,4) shared(status)
3174
3103
#endif
3175
 
  for (y=0; y < (ssize_t) image->rows; y++)
 
3104
  for (y=0; y < (long) image->rows; y++)
3176
3105
  {
3177
3106
    register IndexPacket
3178
3107
      *restrict indexes;
3179
3108
 
3180
 
    register ssize_t
 
3109
    register long
3181
3110
      x;
3182
3111
 
3183
3112
    register const PixelPacket
3192
3121
        continue;
3193
3122
      }
3194
3123
    indexes=GetCacheViewAuthenticIndexQueue(image_view);
3195
 
    for (x=0; x < (ssize_t) image->columns; x++)
 
3124
    for (x=0; x < (long) image->columns; x++)
3196
3125
      indexes[x]=(IndexPacket) colormap_index[ScaleQuantumToMap(indexes[x])];
3197
3126
    if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3198
3127
      status=MagickFalse;
3199
3128
  }
3200
3129
  image_view=DestroyCacheView(image_view);
3201
 
  colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index);
 
3130
  colormap_index=(long *) RelinquishMagickMemory(colormap_index);
3202
3131
  image->type=GrayscaleType;
3203
3132
  if (IsMonochromeImage(image,&image->exception) != MagickFalse)
3204
3133
    image->type=BilevelType;