blob: 81f8724393d31d5004d7214bcb7a264b6bfeecec [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% QQQ U U AAA N N TTTTT IIIII ZZZZZ EEEEE %
7% Q Q U U A A NN N T I ZZ E %
8% Q Q U U AAAAA N N N T I ZZZ EEEEE %
9% Q QQ U U A A N NN T I ZZ E %
10% QQQQ UUU A A N N T IIIII ZZZZZ EEEEE %
11% %
12% %
13% MagickCore Methods to Reduce the Number of Unique Colors in an Image %
14% %
15% Software Design %
16% John Cristy %
17% July 1992 %
18% %
19% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36% Realism in computer graphics typically requires using 24 bits/pixel to
37% generate an image. Yet many graphic display devices do not contain the
38% amount of memory necessary to match the spatial and color resolution of
39% the human eye. The Quantize methods takes a 24 bit image and reduces
40% the number of colors so it can be displayed on raster device with less
41% bits per pixel. In most instances, the quantized image closely
42% resembles the original reference image.
43%
44% A reduction of colors in an image is also desirable for image
45% transmission and real-time animation.
46%
47% QuantizeImage() takes a standard RGB or monochrome images and quantizes
48% them down to some fixed number of colors.
49%
50% For purposes of color allocation, an image is a set of n pixels, where
51% each pixel is a point in RGB space. RGB space is a 3-dimensional
52% vector space, and each pixel, Pi, is defined by an ordered triple of
53% red, green, and blue coordinates, (Ri, Gi, Bi).
54%
55% Each primary color component (red, green, or blue) represents an
56% intensity which varies linearly from 0 to a maximum value, Cmax, which
57% corresponds to full saturation of that color. Color allocation is
58% defined over a domain consisting of the cube in RGB space with opposite
59% vertices at (0,0,0) and (Cmax, Cmax, Cmax). QUANTIZE requires Cmax =
60% 255.
61%
62% The algorithm maps this domain onto a tree in which each node
63% represents a cube within that domain. In the following discussion
64% these cubes are defined by the coordinate of two opposite vertices:
65% The vertex nearest the origin in RGB space and the vertex farthest from
66% the origin.
67%
68% The tree's root node represents the entire domain, (0,0,0) through
69% (Cmax,Cmax,Cmax). Each lower level in the tree is generated by
70% subdividing one node's cube into eight smaller cubes of equal size.
71% This corresponds to bisecting the parent cube with planes passing
72% through the midpoints of each edge.
73%
74% The basic algorithm operates in three phases: Classification,
75% Reduction, and Assignment. Classification builds a color description
76% tree for the image. Reduction collapses the tree until the number it
77% represents, at most, the number of colors desired in the output image.
78% Assignment defines the output image's color map and sets each pixel's
79% color by restorage_class in the reduced tree. Our goal is to minimize
80% the numerical discrepancies between the original colors and quantized
81% colors (quantization error).
82%
83% Classification begins by initializing a color description tree of
84% sufficient depth to represent each possible input color in a leaf.
85% However, it is impractical to generate a fully-formed color description
86% tree in the storage_class phase for realistic values of Cmax. If
87% colors components in the input image are quantized to k-bit precision,
88% so that Cmax= 2k-1, the tree would need k levels below the root node to
89% allow representing each possible input color in a leaf. This becomes
90% prohibitive because the tree's total number of nodes is 1 +
91% sum(i=1, k, 8k).
92%
93% A complete tree would require 19,173,961 nodes for k = 8, Cmax = 255.
94% Therefore, to avoid building a fully populated tree, QUANTIZE: (1)
95% Initializes data structures for nodes only as they are needed; (2)
96% Chooses a maximum depth for the tree as a function of the desired
97% number of colors in the output image (currently log2(colormap size)).
98%
99% For each pixel in the input image, storage_class scans downward from
100% the root of the color description tree. At each level of the tree it
101% identifies the single node which represents a cube in RGB space
102% containing the pixel's color. It updates the following data for each
103% such node:
104%
105% n1: Number of pixels whose color is contained in the RGB cube which
106% this node represents;
107%
108% n2: Number of pixels whose color is not represented in a node at
109% lower depth in the tree; initially, n2 = 0 for all nodes except
110% leaves of the tree.
111%
112% Sr, Sg, Sb: Sums of the red, green, and blue component values for all
113% pixels not classified at a lower depth. The combination of these sums
114% and n2 will ultimately characterize the mean color of a set of
115% pixels represented by this node.
116%
117% E: the distance squared in RGB space between each pixel contained
118% within a node and the nodes' center. This represents the
119% quantization error for a node.
120%
121% Reduction repeatedly prunes the tree until the number of nodes with n2
122% > 0 is less than or equal to the maximum number of colors allowed in
123% the output image. On any given iteration over the tree, it selects
124% those nodes whose E count is minimal for pruning and merges their color
125% statistics upward. It uses a pruning threshold, Ep, to govern node
126% selection as follows:
127%
128% Ep = 0
129% while number of nodes with (n2 > 0) > required maximum number of colors
130% prune all nodes such that E <= Ep
131% Set Ep to minimum E in remaining nodes
132%
133% This has the effect of minimizing any quantization error when merging
134% two nodes together.
135%
136% When a node to be pruned has offspring, the pruning procedure invokes
137% itself recursively in order to prune the tree from the leaves upward.
138% n2, Sr, Sg, and Sb in a node being pruned are always added to the
139% corresponding data in that node's parent. This retains the pruned
140% node's color characteristics for later averaging.
141%
142% For each node, n2 pixels exist for which that node represents the
143% smallest volume in RGB space containing those pixel's colors. When n2
144% > 0 the node will uniquely define a color in the output image. At the
145% beginning of reduction, n2 = 0 for all nodes except a the leaves of
146% the tree which represent colors present in the input image.
147%
148% The other pixel count, n1, indicates the total number of colors within
149% the cubic volume which the node represents. This includes n1 - n2
150% pixels whose colors should be defined by nodes at a lower level in the
151% tree.
152%
153% Assignment generates the output image from the pruned tree. The output
154% image consists of two parts: (1) A color map, which is an array of
155% color descriptions (RGB triples) for each color present in the output
156% image; (2) A pixel array, which represents each pixel as an index
157% into the color map array.
158%
159% First, the assignment phase makes one pass over the pruned color
160% description tree to establish the image's color map. For each node
161% with n2 > 0, it divides Sr, Sg, and Sb by n2 . This produces the mean
162% color of all pixels that classify no lower than this node. Each of
163% these colors becomes an entry in the color map.
164%
165% Finally, the assignment phase reclassifies each pixel in the pruned
166% tree to identify the deepest node containing the pixel's color. The
167% pixel's value in the pixel array becomes the index of this node's mean
168% color in the color map.
169%
170% This method is based on a similar algorithm written by Paul Raveling.
171%
172*/
173
174/*
175 Include declarations.
176*/
cristy4c08aed2011-07-01 19:47:50 +0000177#include "MagickCore/studio.h"
178#include "MagickCore/attribute.h"
179#include "MagickCore/cache-view.h"
180#include "MagickCore/color.h"
181#include "MagickCore/color-private.h"
182#include "MagickCore/colormap.h"
183#include "MagickCore/colorspace.h"
cristy510d06a2011-07-06 23:43:54 +0000184#include "MagickCore/colorspace-private.h"
cristy4c08aed2011-07-01 19:47:50 +0000185#include "MagickCore/enhance.h"
186#include "MagickCore/exception.h"
187#include "MagickCore/exception-private.h"
188#include "MagickCore/histogram.h"
189#include "MagickCore/image.h"
190#include "MagickCore/image-private.h"
191#include "MagickCore/list.h"
192#include "MagickCore/memory_.h"
193#include "MagickCore/monitor.h"
194#include "MagickCore/monitor-private.h"
195#include "MagickCore/option.h"
196#include "MagickCore/pixel-accessor.h"
197#include "MagickCore/quantize.h"
198#include "MagickCore/quantum.h"
199#include "MagickCore/quantum-private.h"
cristyac245f82012-05-05 17:13:57 +0000200#include "MagickCore/resource_.h"
cristy4c08aed2011-07-01 19:47:50 +0000201#include "MagickCore/string_.h"
202#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +0000203
204/*
205 Define declarations.
206*/
cristye1287512010-06-19 17:38:25 +0000207#if !defined(__APPLE__) && !defined(TARGET_OS_IPHONE)
cristy3ed852e2009-09-05 21:47:34 +0000208#define CacheShift 2
cristye1287512010-06-19 17:38:25 +0000209#else
210#define CacheShift 3
211#endif
cristy3ed852e2009-09-05 21:47:34 +0000212#define ErrorQueueLength 16
213#define MaxNodes 266817
214#define MaxTreeDepth 8
215#define NodesInAList 1920
216
217/*
218 Typdef declarations.
219*/
cristy101ab702011-10-13 13:06:32 +0000220typedef struct _RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000221{
222 MagickRealType
223 red,
224 green,
225 blue,
cristy4c08aed2011-07-01 19:47:50 +0000226 alpha;
cristy101ab702011-10-13 13:06:32 +0000227} RealPixelInfo;
cristy3ed852e2009-09-05 21:47:34 +0000228
229typedef struct _NodeInfo
230{
231 struct _NodeInfo
232 *parent,
233 *child[16];
234
235 MagickSizeType
236 number_unique;
237
cristy101ab702011-10-13 13:06:32 +0000238 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000239 total_color;
240
241 MagickRealType
242 quantize_error;
243
cristybb503372010-05-27 20:51:26 +0000244 size_t
cristy3ed852e2009-09-05 21:47:34 +0000245 color_number,
246 id,
247 level;
248} NodeInfo;
249
250typedef struct _Nodes
251{
252 NodeInfo
253 *nodes;
254
255 struct _Nodes
256 *next;
257} Nodes;
258
259typedef struct _CubeInfo
260{
261 NodeInfo
262 *root;
263
cristybb503372010-05-27 20:51:26 +0000264 size_t
cristy3ed852e2009-09-05 21:47:34 +0000265 colors,
266 maximum_colors;
267
cristybb503372010-05-27 20:51:26 +0000268 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000269 transparent_index;
270
271 MagickSizeType
272 transparent_pixels;
273
cristy101ab702011-10-13 13:06:32 +0000274 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000275 target;
276
277 MagickRealType
278 distance,
279 pruning_threshold,
280 next_threshold;
281
cristybb503372010-05-27 20:51:26 +0000282 size_t
cristy3ed852e2009-09-05 21:47:34 +0000283 nodes,
284 free_nodes,
285 color_number;
286
287 NodeInfo
288 *next_node;
289
290 Nodes
291 *node_queue;
292
cristybb503372010-05-27 20:51:26 +0000293 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000294 *cache;
295
cristy101ab702011-10-13 13:06:32 +0000296 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000297 error[ErrorQueueLength];
298
299 MagickRealType
300 weights[ErrorQueueLength];
301
302 QuantizeInfo
303 *quantize_info;
304
305 MagickBooleanType
306 associate_alpha;
307
cristybb503372010-05-27 20:51:26 +0000308 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000309 x,
310 y;
311
cristybb503372010-05-27 20:51:26 +0000312 size_t
cristy3ed852e2009-09-05 21:47:34 +0000313 depth;
314
315 MagickOffsetType
316 offset;
317
318 MagickSizeType
319 span;
320} CubeInfo;
321
322/*
323 Method prototypes.
324*/
325static CubeInfo
cristybb503372010-05-27 20:51:26 +0000326 *GetCubeInfo(const QuantizeInfo *,const size_t,const size_t);
cristy3ed852e2009-09-05 21:47:34 +0000327
328static NodeInfo
cristybb503372010-05-27 20:51:26 +0000329 *GetNodeInfo(CubeInfo *,const size_t,const size_t,NodeInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000330
331static MagickBooleanType
cristy018f07f2011-09-04 21:15:19 +0000332 AssignImageColors(Image *,CubeInfo *,ExceptionInfo *),
cristy3ed852e2009-09-05 21:47:34 +0000333 ClassifyImageColors(CubeInfo *,const Image *,ExceptionInfo *),
cristy8a11cb12011-10-19 23:53:34 +0000334 DitherImage(Image *,CubeInfo *,ExceptionInfo *),
cristy018f07f2011-09-04 21:15:19 +0000335 SetGrayscaleImage(Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000336
cristybb503372010-05-27 20:51:26 +0000337static size_t
cristy3ed852e2009-09-05 21:47:34 +0000338 DefineImageColormap(Image *,CubeInfo *,NodeInfo *);
339
340static void
341 ClosestColor(const Image *,CubeInfo *,const NodeInfo *),
342 DestroyCubeInfo(CubeInfo *),
343 PruneLevel(const Image *,CubeInfo *,const NodeInfo *),
344 PruneToCubeDepth(const Image *,CubeInfo *,const NodeInfo *),
345 ReduceImageColors(const Image *,CubeInfo *);
346
347/*
348%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349% %
350% %
351% %
352% A c q u i r e Q u a n t i z e I n f o %
353% %
354% %
355% %
356%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
357%
358% AcquireQuantizeInfo() allocates the QuantizeInfo structure.
359%
360% The format of the AcquireQuantizeInfo method is:
361%
362% QuantizeInfo *AcquireQuantizeInfo(const ImageInfo *image_info)
363%
364% A description of each parameter follows:
365%
366% o image_info: the image info.
367%
368*/
369MagickExport QuantizeInfo *AcquireQuantizeInfo(const ImageInfo *image_info)
370{
371 QuantizeInfo
372 *quantize_info;
373
cristy73bd4a52010-10-05 11:24:23 +0000374 quantize_info=(QuantizeInfo *) AcquireMagickMemory(sizeof(*quantize_info));
cristy3ed852e2009-09-05 21:47:34 +0000375 if (quantize_info == (QuantizeInfo *) NULL)
376 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
377 GetQuantizeInfo(quantize_info);
378 if (image_info != (ImageInfo *) NULL)
379 {
380 const char
381 *option;
382
383 quantize_info->dither=image_info->dither;
384 option=GetImageOption(image_info,"dither");
385 if (option != (const char *) NULL)
cristy042ee782011-04-22 18:48:30 +0000386 quantize_info->dither_method=(DitherMethod) ParseCommandOption(
cristy3ed852e2009-09-05 21:47:34 +0000387 MagickDitherOptions,MagickFalse,option);
388 quantize_info->measure_error=image_info->verbose;
389 }
390 return(quantize_info);
391}
392
393/*
394%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
395% %
396% %
397% %
398+ A s s i g n I m a g e C o l o r s %
399% %
400% %
401% %
402%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403%
404% AssignImageColors() generates the output image from the pruned tree. The
405% output image consists of two parts: (1) A color map, which is an array
406% of color descriptions (RGB triples) for each color present in the
407% output image; (2) A pixel array, which represents each pixel as an
408% index into the color map array.
409%
410% First, the assignment phase makes one pass over the pruned color
411% description tree to establish the image's color map. For each node
412% with n2 > 0, it divides Sr, Sg, and Sb by n2 . This produces the mean
413% color of all pixels that classify no lower than this node. Each of
414% these colors becomes an entry in the color map.
415%
416% Finally, the assignment phase reclassifies each pixel in the pruned
417% tree to identify the deepest node containing the pixel's color. The
418% pixel's value in the pixel array becomes the index of this node's mean
419% color in the color map.
420%
421% The format of the AssignImageColors() method is:
422%
423% MagickBooleanType AssignImageColors(Image *image,CubeInfo *cube_info)
424%
425% A description of each parameter follows.
426%
427% o image: the image.
428%
429% o cube_info: A pointer to the Cube structure.
430%
431*/
432
cristy4c08aed2011-07-01 19:47:50 +0000433static inline void AssociateAlphaPixel(const Image *image,
cristy101ab702011-10-13 13:06:32 +0000434 const CubeInfo *cube_info,const Quantum *pixel,RealPixelInfo *alpha_pixel)
cristy3ed852e2009-09-05 21:47:34 +0000435{
436 MagickRealType
437 alpha;
438
439 if ((cube_info->associate_alpha == MagickFalse) ||
cristy4c08aed2011-07-01 19:47:50 +0000440 (GetPixelAlpha(image,pixel)== OpaqueAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000441 {
cristy4c08aed2011-07-01 19:47:50 +0000442 alpha_pixel->red=(MagickRealType) GetPixelRed(image,pixel);
443 alpha_pixel->green=(MagickRealType) GetPixelGreen(image,pixel);
444 alpha_pixel->blue=(MagickRealType) GetPixelBlue(image,pixel);
445 alpha_pixel->alpha=(MagickRealType) GetPixelAlpha(image,pixel);
cristy3ed852e2009-09-05 21:47:34 +0000446 return;
447 }
cristy4c08aed2011-07-01 19:47:50 +0000448 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixel));
449 alpha_pixel->red=alpha*GetPixelRed(image,pixel);
450 alpha_pixel->green=alpha*GetPixelGreen(image,pixel);
451 alpha_pixel->blue=alpha*GetPixelBlue(image,pixel);
452 alpha_pixel->alpha=(MagickRealType) GetPixelAlpha(image,pixel);
453}
454
cristy101ab702011-10-13 13:06:32 +0000455static inline void AssociateAlphaPixelInfo(const Image *image,
456 const CubeInfo *cube_info,const PixelInfo *pixel,
457 RealPixelInfo *alpha_pixel)
cristy4c08aed2011-07-01 19:47:50 +0000458{
459 MagickRealType
460 alpha;
461
462 if ((cube_info->associate_alpha == MagickFalse) ||
463 (pixel->alpha == OpaqueAlpha))
464 {
465 alpha_pixel->red=(MagickRealType) pixel->red;
466 alpha_pixel->green=(MagickRealType) pixel->green;
467 alpha_pixel->blue=(MagickRealType) pixel->blue;
468 alpha_pixel->alpha=(MagickRealType) pixel->alpha;
469 return;
470 }
471 alpha=(MagickRealType) (QuantumScale*pixel->alpha);
472 alpha_pixel->red=alpha*pixel->red;
473 alpha_pixel->green=alpha*pixel->green;
474 alpha_pixel->blue=alpha*pixel->blue;
475 alpha_pixel->alpha=(MagickRealType) pixel->alpha;
cristy3ed852e2009-09-05 21:47:34 +0000476}
477
cristy75ffdb72010-01-07 17:40:12 +0000478static inline Quantum ClampToUnsignedQuantum(const MagickRealType value)
cristy3ed852e2009-09-05 21:47:34 +0000479{
480 if (value <= 0.0)
481 return((Quantum) 0);
482 if (value >= QuantumRange)
483 return((Quantum) QuantumRange);
484 return((Quantum) (value+0.5));
485}
486
cristybb503372010-05-27 20:51:26 +0000487static inline size_t ColorToNodeId(const CubeInfo *cube_info,
cristy101ab702011-10-13 13:06:32 +0000488 const RealPixelInfo *pixel,size_t index)
cristy3ed852e2009-09-05 21:47:34 +0000489{
cristybb503372010-05-27 20:51:26 +0000490 size_t
cristy3ed852e2009-09-05 21:47:34 +0000491 id;
492
cristy4c08aed2011-07-01 19:47:50 +0000493 id=(size_t) (((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->red)) >> index) & 0x01) |
494 ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->green)) >> index) & 0x01) << 1 |
495 ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->blue)) >> index) & 0x01) << 2);
cristy3ed852e2009-09-05 21:47:34 +0000496 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000497 id|=((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->alpha)) >> index) & 0x1) << 3;
cristy3ed852e2009-09-05 21:47:34 +0000498 return(id);
499}
500
cristy018f07f2011-09-04 21:15:19 +0000501static MagickBooleanType AssignImageColors(Image *image,CubeInfo *cube_info,
502 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000503{
504#define AssignImageTag "Assign/Image"
505
cristyecc31b12011-02-13 00:32:29 +0000506 ssize_t
cristyecc31b12011-02-13 00:32:29 +0000507 y;
508
cristy3ed852e2009-09-05 21:47:34 +0000509 /*
510 Allocate image colormap.
511 */
512 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
513 (cube_info->quantize_info->colorspace != CMYKColorspace))
514 (void) TransformImageColorspace((Image *) image,
cristye941a752011-10-15 01:52:48 +0000515 cube_info->quantize_info->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000516 else
517 if ((image->colorspace != GRAYColorspace) &&
cristy501c5592012-04-18 12:45:09 +0000518 (IssRGBColorspace(image->colorspace) == MagickFalse) &&
cristy3ed852e2009-09-05 21:47:34 +0000519 (image->colorspace != CMYColorspace))
cristyc511e882012-04-16 21:11:14 +0000520 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy018f07f2011-09-04 21:15:19 +0000521 if (AcquireImageColormap(image,cube_info->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000522 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
523 image->filename);
524 image->colors=0;
525 cube_info->transparent_pixels=0;
526 cube_info->transparent_index=(-1);
527 (void) DefineImageColormap(image,cube_info,cube_info->root);
528 /*
529 Create a reduced color image.
530 */
531 if ((cube_info->quantize_info->dither != MagickFalse) &&
cristyd5acfd12010-06-15 00:11:38 +0000532 (cube_info->quantize_info->dither_method != NoDitherMethod))
cristy8a11cb12011-10-19 23:53:34 +0000533 (void) DitherImage(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000534 else
535 {
cristy3ed852e2009-09-05 21:47:34 +0000536 CacheView
537 *image_view;
538
cristye9717ac2011-02-20 16:17:17 +0000539 MagickBooleanType
540 status;
541
542 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +0000543 image_view=AcquireAuthenticCacheView(image,exception);
cristye9717ac2011-02-20 16:17:17 +0000544#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +0000545 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +0000546 dynamic_number_threads(image,image->columns,image->rows,1)
cristye9717ac2011-02-20 16:17:17 +0000547#endif
cristybb503372010-05-27 20:51:26 +0000548 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000549 {
cristye9717ac2011-02-20 16:17:17 +0000550 CubeInfo
551 cube;
552
cristy4c08aed2011-07-01 19:47:50 +0000553 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000554 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000555
cristye9717ac2011-02-20 16:17:17 +0000556 register ssize_t
557 x;
558
559 ssize_t
560 count;
561
562 if (status == MagickFalse)
563 continue;
cristy3ed852e2009-09-05 21:47:34 +0000564 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
565 exception);
cristyacd2ed22011-08-30 01:44:23 +0000566 if (q == (Quantum *) NULL)
cristye9717ac2011-02-20 16:17:17 +0000567 {
568 status=MagickFalse;
569 continue;
570 }
cristye9717ac2011-02-20 16:17:17 +0000571 cube=(*cube_info);
cristybb503372010-05-27 20:51:26 +0000572 for (x=0; x < (ssize_t) image->columns; x+=count)
cristy3ed852e2009-09-05 21:47:34 +0000573 {
cristy101ab702011-10-13 13:06:32 +0000574 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +0000575 pixel;
576
577 register const NodeInfo
578 *node_info;
579
580 register ssize_t
581 i;
582
583 size_t
584 id,
585 index;
586
cristy3ed852e2009-09-05 21:47:34 +0000587 /*
588 Identify the deepest node containing the pixel's color.
589 */
cristybb503372010-05-27 20:51:26 +0000590 for (count=1; (x+count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000591 {
cristy101ab702011-10-13 13:06:32 +0000592 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000593 packet;
594
cristy101ab702011-10-13 13:06:32 +0000595 GetPixelInfoPixel(image,q+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000596 if (IsPixelEquivalent(image,q,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000597 break;
cristy4c08aed2011-07-01 19:47:50 +0000598 }
599 AssociateAlphaPixel(image,&cube,q,&pixel);
cristye9717ac2011-02-20 16:17:17 +0000600 node_info=cube.root;
cristybb503372010-05-27 20:51:26 +0000601 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +0000602 {
cristye9717ac2011-02-20 16:17:17 +0000603 id=ColorToNodeId(&cube,&pixel,index);
cristy3ed852e2009-09-05 21:47:34 +0000604 if (node_info->child[id] == (NodeInfo *) NULL)
605 break;
606 node_info=node_info->child[id];
607 }
608 /*
609 Find closest color among siblings and their children.
610 */
cristye9717ac2011-02-20 16:17:17 +0000611 cube.target=pixel;
612 cube.distance=(MagickRealType) (4.0*(QuantumRange+1.0)*
cristy3ed852e2009-09-05 21:47:34 +0000613 (QuantumRange+1.0)+1.0);
cristye9717ac2011-02-20 16:17:17 +0000614 ClosestColor(image,&cube,node_info->parent);
615 index=cube.color_number;
cristybb503372010-05-27 20:51:26 +0000616 for (i=0; i < (ssize_t) count; i++)
cristy3ed852e2009-09-05 21:47:34 +0000617 {
618 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000619 SetPixelIndex(image,(Quantum) index,q);
cristye9717ac2011-02-20 16:17:17 +0000620 if (cube.quantize_info->measure_error == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000621 {
cristye42f6582012-02-11 17:59:50 +0000622 SetPixelRed(image,ClampToQuantum(
623 image->colormap[index].red),q);
624 SetPixelGreen(image,ClampToQuantum(
625 image->colormap[index].green),q);
626 SetPixelBlue(image,ClampToQuantum(
627 image->colormap[index].blue),q);
cristye9717ac2011-02-20 16:17:17 +0000628 if (cube.associate_alpha != MagickFalse)
cristye42f6582012-02-11 17:59:50 +0000629 SetPixelAlpha(image,ClampToQuantum(
630 image->colormap[index].alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000631 }
cristyed231572011-07-14 02:18:59 +0000632 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000633 }
634 }
635 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristye9717ac2011-02-20 16:17:17 +0000636 status=MagickFalse;
637 if (image->progress_monitor != (MagickProgressMonitor) NULL)
638 {
639 MagickBooleanType
640 proceed;
641
642#if defined(MAGICKCORE_OPENMP_SUPPORT)
643 #pragma omp critical (MagickCore_AssignImageColors)
644#endif
645 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y,
646 image->rows);
647 if (proceed == MagickFalse)
648 status=MagickFalse;
649 }
cristy3ed852e2009-09-05 21:47:34 +0000650 }
651 image_view=DestroyCacheView(image_view);
652 }
653 if (cube_info->quantize_info->measure_error != MagickFalse)
cristy8a11cb12011-10-19 23:53:34 +0000654 (void) GetImageQuantizeError(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000655 if ((cube_info->quantize_info->number_colors == 2) &&
656 (cube_info->quantize_info->colorspace == GRAYColorspace))
657 {
cristye42f6582012-02-11 17:59:50 +0000658 double
cristy3ed852e2009-09-05 21:47:34 +0000659 intensity;
660
cristy101ab702011-10-13 13:06:32 +0000661 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +0000662 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000663
cristye9717ac2011-02-20 16:17:17 +0000664 register ssize_t
665 i;
666
cristy3ed852e2009-09-05 21:47:34 +0000667 /*
668 Monochrome image.
669 */
670 q=image->colormap;
cristybb503372010-05-27 20:51:26 +0000671 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000672 {
cristye42f6582012-02-11 17:59:50 +0000673 intensity=(double) ((MagickRealType) GetPixelInfoIntensity(q) <
cristy4c08aed2011-07-01 19:47:50 +0000674 ((MagickRealType) QuantumRange/2.0) ? 0 : QuantumRange);
675 q->red=intensity;
676 q->green=intensity;
677 q->blue=intensity;
cristy3ed852e2009-09-05 21:47:34 +0000678 q++;
679 }
680 }
cristyea1a8aa2011-10-20 13:24:06 +0000681 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000682 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
683 (cube_info->quantize_info->colorspace != CMYKColorspace))
cristyc511e882012-04-16 21:11:14 +0000684 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000685 return(MagickTrue);
686}
687
688/*
689%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
690% %
691% %
692% %
693+ C l a s s i f y I m a g e C o l o r s %
694% %
695% %
696% %
697%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
698%
699% ClassifyImageColors() begins by initializing a color description tree
700% of sufficient depth to represent each possible input color in a leaf.
701% However, it is impractical to generate a fully-formed color
702% description tree in the storage_class phase for realistic values of
703% Cmax. If colors components in the input image are quantized to k-bit
704% precision, so that Cmax= 2k-1, the tree would need k levels below the
705% root node to allow representing each possible input color in a leaf.
706% This becomes prohibitive because the tree's total number of nodes is
707% 1 + sum(i=1,k,8k).
708%
709% A complete tree would require 19,173,961 nodes for k = 8, Cmax = 255.
710% Therefore, to avoid building a fully populated tree, QUANTIZE: (1)
711% Initializes data structures for nodes only as they are needed; (2)
712% Chooses a maximum depth for the tree as a function of the desired
713% number of colors in the output image (currently log2(colormap size)).
714%
715% For each pixel in the input image, storage_class scans downward from
716% the root of the color description tree. At each level of the tree it
717% identifies the single node which represents a cube in RGB space
718% containing It updates the following data for each such node:
719%
720% n1 : Number of pixels whose color is contained in the RGB cube
721% which this node represents;
722%
723% n2 : Number of pixels whose color is not represented in a node at
724% lower depth in the tree; initially, n2 = 0 for all nodes except
725% leaves of the tree.
726%
727% Sr, Sg, Sb : Sums of the red, green, and blue component values for
728% all pixels not classified at a lower depth. The combination of
729% these sums and n2 will ultimately characterize the mean color of a
730% set of pixels represented by this node.
731%
732% E: the distance squared in RGB space between each pixel contained
733% within a node and the nodes' center. This represents the quantization
734% error for a node.
735%
736% The format of the ClassifyImageColors() method is:
737%
738% MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
739% const Image *image,ExceptionInfo *exception)
740%
741% A description of each parameter follows.
742%
743% o cube_info: A pointer to the Cube structure.
744%
745% o image: the image.
746%
747*/
748
749static inline void SetAssociatedAlpha(const Image *image,CubeInfo *cube_info)
750{
751 MagickBooleanType
752 associate_alpha;
753
754 associate_alpha=image->matte;
755 if (cube_info->quantize_info->colorspace == TransparentColorspace)
756 associate_alpha=MagickFalse;
757 if ((cube_info->quantize_info->number_colors == 2) &&
758 (cube_info->quantize_info->colorspace == GRAYColorspace))
759 associate_alpha=MagickFalse;
760 cube_info->associate_alpha=associate_alpha;
761}
762
763static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
764 const Image *image,ExceptionInfo *exception)
765{
766#define ClassifyImageTag "Classify/Image"
767
cristyc4c8d132010-01-07 01:58:38 +0000768 CacheView
769 *image_view;
770
cristy3ed852e2009-09-05 21:47:34 +0000771 MagickBooleanType
772 proceed;
773
774 MagickRealType
775 bisect;
776
777 NodeInfo
778 *node_info;
779
cristy101ab702011-10-13 13:06:32 +0000780 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000781 error,
782 mid,
783 midpoint,
784 pixel;
785
786 size_t
cristyecc31b12011-02-13 00:32:29 +0000787 count,
cristy3ed852e2009-09-05 21:47:34 +0000788 id,
789 index,
790 level;
791
cristyecc31b12011-02-13 00:32:29 +0000792 ssize_t
793 y;
794
cristy3ed852e2009-09-05 21:47:34 +0000795 /*
796 Classify the first cube_info->maximum_colors colors to a tree depth of 8.
797 */
798 SetAssociatedAlpha(image,cube_info);
799 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
800 (cube_info->quantize_info->colorspace != CMYKColorspace))
801 (void) TransformImageColorspace((Image *) image,
cristye941a752011-10-15 01:52:48 +0000802 cube_info->quantize_info->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000803 else
804 if ((image->colorspace != GRAYColorspace) &&
805 (image->colorspace != CMYColorspace) &&
cristy501c5592012-04-18 12:45:09 +0000806 (IssRGBColorspace(image->colorspace) == MagickFalse))
cristyc511e882012-04-16 21:11:14 +0000807 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000808 midpoint.red=(MagickRealType) QuantumRange/2.0;
809 midpoint.green=(MagickRealType) QuantumRange/2.0;
810 midpoint.blue=(MagickRealType) QuantumRange/2.0;
cristy4c08aed2011-07-01 19:47:50 +0000811 midpoint.alpha=(MagickRealType) QuantumRange/2.0;
812 error.alpha=0.0;
cristydb070952012-04-20 14:33:00 +0000813 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000814 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000815 {
cristy4c08aed2011-07-01 19:47:50 +0000816 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000817 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000818
cristybb503372010-05-27 20:51:26 +0000819 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000820 x;
821
822 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000823 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000824 break;
825 if (cube_info->nodes > MaxNodes)
826 {
827 /*
828 Prune one level if the color tree is too large.
829 */
830 PruneLevel(image,cube_info,cube_info->root);
831 cube_info->depth--;
832 }
cristybb503372010-05-27 20:51:26 +0000833 for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count)
cristy3ed852e2009-09-05 21:47:34 +0000834 {
835 /*
836 Start at the root and descend the color cube tree.
837 */
cristybb66d9c2010-10-09 01:40:31 +0000838 for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000839 {
cristy101ab702011-10-13 13:06:32 +0000840 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000841 packet;
842
cristy101ab702011-10-13 13:06:32 +0000843 GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000844 if (IsPixelEquivalent(image,p,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000845 break;
cristy4c08aed2011-07-01 19:47:50 +0000846 }
847 AssociateAlphaPixel(image,cube_info,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +0000848 index=MaxTreeDepth-1;
849 bisect=((MagickRealType) QuantumRange+1.0)/2.0;
850 mid=midpoint;
851 node_info=cube_info->root;
852 for (level=1; level <= MaxTreeDepth; level++)
853 {
854 bisect*=0.5;
855 id=ColorToNodeId(cube_info,&pixel,index);
856 mid.red+=(id & 1) != 0 ? bisect : -bisect;
857 mid.green+=(id & 2) != 0 ? bisect : -bisect;
858 mid.blue+=(id & 4) != 0 ? bisect : -bisect;
cristy4c08aed2011-07-01 19:47:50 +0000859 mid.alpha+=(id & 8) != 0 ? bisect : -bisect;
cristy3ed852e2009-09-05 21:47:34 +0000860 if (node_info->child[id] == (NodeInfo *) NULL)
861 {
862 /*
863 Set colors of new node to contain pixel.
864 */
865 node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info);
866 if (node_info->child[id] == (NodeInfo *) NULL)
867 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000868 ResourceLimitError,"MemoryAllocationFailed","'%s'",
cristy3ed852e2009-09-05 21:47:34 +0000869 image->filename);
870 if (level == MaxTreeDepth)
871 cube_info->colors++;
872 }
873 /*
874 Approximate the quantization error represented by this node.
875 */
876 node_info=node_info->child[id];
877 error.red=QuantumScale*(pixel.red-mid.red);
878 error.green=QuantumScale*(pixel.green-mid.green);
879 error.blue=QuantumScale*(pixel.blue-mid.blue);
880 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000881 error.alpha=QuantumScale*(pixel.alpha-mid.alpha);
cristy3ed852e2009-09-05 21:47:34 +0000882 node_info->quantize_error+=sqrt((double) (count*error.red*error.red+
883 count*error.green*error.green+count*error.blue*error.blue+
cristy4c08aed2011-07-01 19:47:50 +0000884 count*error.alpha*error.alpha));
cristy3ed852e2009-09-05 21:47:34 +0000885 cube_info->root->quantize_error+=node_info->quantize_error;
886 index--;
887 }
888 /*
889 Sum RGB for this leaf for later derivation of the mean cube color.
890 */
891 node_info->number_unique+=count;
892 node_info->total_color.red+=count*QuantumScale*pixel.red;
893 node_info->total_color.green+=count*QuantumScale*pixel.green;
894 node_info->total_color.blue+=count*QuantumScale*pixel.blue;
895 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000896 node_info->total_color.alpha+=count*QuantumScale*pixel.alpha;
cristyed231572011-07-14 02:18:59 +0000897 p+=count*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000898 }
899 if (cube_info->colors > cube_info->maximum_colors)
900 {
901 PruneToCubeDepth(image,cube_info,cube_info->root);
902 break;
903 }
cristycee97112010-05-28 00:44:52 +0000904 proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y,
905 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000906 if (proceed == MagickFalse)
907 break;
908 }
cristybb503372010-05-27 20:51:26 +0000909 for (y++; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000910 {
cristy4c08aed2011-07-01 19:47:50 +0000911 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000912 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000913
cristybb503372010-05-27 20:51:26 +0000914 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000915 x;
916
917 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000918 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000919 break;
920 if (cube_info->nodes > MaxNodes)
921 {
922 /*
923 Prune one level if the color tree is too large.
924 */
925 PruneLevel(image,cube_info,cube_info->root);
926 cube_info->depth--;
927 }
cristybb503372010-05-27 20:51:26 +0000928 for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count)
cristy3ed852e2009-09-05 21:47:34 +0000929 {
930 /*
931 Start at the root and descend the color cube tree.
932 */
cristybb66d9c2010-10-09 01:40:31 +0000933 for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000934 {
cristy101ab702011-10-13 13:06:32 +0000935 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000936 packet;
937
cristy101ab702011-10-13 13:06:32 +0000938 GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000939 if (IsPixelEquivalent(image,p,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000940 break;
cristy4c08aed2011-07-01 19:47:50 +0000941 }
942 AssociateAlphaPixel(image,cube_info,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +0000943 index=MaxTreeDepth-1;
944 bisect=((MagickRealType) QuantumRange+1.0)/2.0;
945 mid=midpoint;
946 node_info=cube_info->root;
947 for (level=1; level <= cube_info->depth; level++)
948 {
949 bisect*=0.5;
950 id=ColorToNodeId(cube_info,&pixel,index);
951 mid.red+=(id & 1) != 0 ? bisect : -bisect;
952 mid.green+=(id & 2) != 0 ? bisect : -bisect;
953 mid.blue+=(id & 4) != 0 ? bisect : -bisect;
cristy4c08aed2011-07-01 19:47:50 +0000954 mid.alpha+=(id & 8) != 0 ? bisect : -bisect;
cristy3ed852e2009-09-05 21:47:34 +0000955 if (node_info->child[id] == (NodeInfo *) NULL)
956 {
957 /*
958 Set colors of new node to contain pixel.
959 */
960 node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info);
961 if (node_info->child[id] == (NodeInfo *) NULL)
962 (void) ThrowMagickException(exception,GetMagickModule(),
963 ResourceLimitError,"MemoryAllocationFailed","%s",
964 image->filename);
965 if (level == cube_info->depth)
966 cube_info->colors++;
967 }
968 /*
969 Approximate the quantization error represented by this node.
970 */
971 node_info=node_info->child[id];
972 error.red=QuantumScale*(pixel.red-mid.red);
973 error.green=QuantumScale*(pixel.green-mid.green);
974 error.blue=QuantumScale*(pixel.blue-mid.blue);
975 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000976 error.alpha=QuantumScale*(pixel.alpha-mid.alpha);
cristy3ed852e2009-09-05 21:47:34 +0000977 node_info->quantize_error+=sqrt((double) (count*error.red*error.red+
cristy83b6e792011-01-26 15:46:06 +0000978 count*error.green*error.green+count*error.blue*error.blue+
cristy4c08aed2011-07-01 19:47:50 +0000979 count*error.alpha*error.alpha));
cristy3ed852e2009-09-05 21:47:34 +0000980 cube_info->root->quantize_error+=node_info->quantize_error;
981 index--;
982 }
983 /*
984 Sum RGB for this leaf for later derivation of the mean cube color.
985 */
986 node_info->number_unique+=count;
987 node_info->total_color.red+=count*QuantumScale*pixel.red;
988 node_info->total_color.green+=count*QuantumScale*pixel.green;
989 node_info->total_color.blue+=count*QuantumScale*pixel.blue;
990 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000991 node_info->total_color.alpha+=count*QuantumScale*pixel.alpha;
cristyed231572011-07-14 02:18:59 +0000992 p+=count*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000993 }
cristycee97112010-05-28 00:44:52 +0000994 proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y,
995 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000996 if (proceed == MagickFalse)
997 break;
998 }
999 image_view=DestroyCacheView(image_view);
1000 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
1001 (cube_info->quantize_info->colorspace != CMYKColorspace))
cristyc511e882012-04-16 21:11:14 +00001002 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001003 return(MagickTrue);
1004}
1005
1006/*
1007%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1008% %
1009% %
1010% %
1011% C l o n e Q u a n t i z e I n f o %
1012% %
1013% %
1014% %
1015%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1016%
1017% CloneQuantizeInfo() makes a duplicate of the given quantize info structure,
1018% or if quantize info is NULL, a new one.
1019%
1020% The format of the CloneQuantizeInfo method is:
1021%
1022% QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info)
1023%
1024% A description of each parameter follows:
1025%
1026% o clone_info: Method CloneQuantizeInfo returns a duplicate of the given
1027% quantize info, or if image info is NULL a new one.
1028%
1029% o quantize_info: a structure of type info.
1030%
1031*/
1032MagickExport QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info)
1033{
1034 QuantizeInfo
1035 *clone_info;
1036
cristy73bd4a52010-10-05 11:24:23 +00001037 clone_info=(QuantizeInfo *) AcquireMagickMemory(sizeof(*clone_info));
cristy3ed852e2009-09-05 21:47:34 +00001038 if (clone_info == (QuantizeInfo *) NULL)
1039 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1040 GetQuantizeInfo(clone_info);
1041 if (quantize_info == (QuantizeInfo *) NULL)
1042 return(clone_info);
1043 clone_info->number_colors=quantize_info->number_colors;
1044 clone_info->tree_depth=quantize_info->tree_depth;
1045 clone_info->dither=quantize_info->dither;
1046 clone_info->dither_method=quantize_info->dither_method;
1047 clone_info->colorspace=quantize_info->colorspace;
1048 clone_info->measure_error=quantize_info->measure_error;
1049 return(clone_info);
1050}
1051
1052/*
1053%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1054% %
1055% %
1056% %
1057+ C l o s e s t C o l o r %
1058% %
1059% %
1060% %
1061%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1062%
1063% ClosestColor() traverses the color cube tree at a particular node and
1064% determines which colormap entry best represents the input color.
1065%
1066% The format of the ClosestColor method is:
1067%
1068% void ClosestColor(const Image *image,CubeInfo *cube_info,
1069% const NodeInfo *node_info)
1070%
1071% A description of each parameter follows.
1072%
1073% o image: the image.
1074%
1075% o cube_info: A pointer to the Cube structure.
1076%
1077% o node_info: the address of a structure of type NodeInfo which points to a
1078% node in the color cube tree that is to be pruned.
1079%
1080*/
1081static void ClosestColor(const Image *image,CubeInfo *cube_info,
1082 const NodeInfo *node_info)
1083{
cristybb503372010-05-27 20:51:26 +00001084 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001085 i;
1086
cristybb503372010-05-27 20:51:26 +00001087 size_t
cristy3ed852e2009-09-05 21:47:34 +00001088 number_children;
1089
1090 /*
1091 Traverse any children.
1092 */
1093 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00001094 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00001095 if (node_info->child[i] != (NodeInfo *) NULL)
1096 ClosestColor(image,cube_info,node_info->child[i]);
1097 if (node_info->number_unique != 0)
1098 {
1099 MagickRealType
1100 pixel;
1101
1102 register MagickRealType
1103 alpha,
1104 beta,
1105 distance;
1106
cristy101ab702011-10-13 13:06:32 +00001107 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001108 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001109
cristy101ab702011-10-13 13:06:32 +00001110 register RealPixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001111 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001112
1113 /*
1114 Determine if this color is "closest".
1115 */
1116 p=image->colormap+node_info->color_number;
1117 q=(&cube_info->target);
1118 alpha=1.0;
1119 beta=1.0;
cristy847620f2011-02-09 02:24:21 +00001120 if (cube_info->associate_alpha != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001121 {
cristy4c08aed2011-07-01 19:47:50 +00001122 alpha=(MagickRealType) (QuantumScale*p->alpha);
1123 beta=(MagickRealType) (QuantumScale*q->alpha);
cristy3ed852e2009-09-05 21:47:34 +00001124 }
cristy4c08aed2011-07-01 19:47:50 +00001125 pixel=alpha*p->red-beta*q->red;
cristy3ed852e2009-09-05 21:47:34 +00001126 distance=pixel*pixel;
cristy36fbc3b2011-02-09 02:30:04 +00001127 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001128 {
cristy4c08aed2011-07-01 19:47:50 +00001129 pixel=alpha*p->green-beta*q->green;
cristy3ed852e2009-09-05 21:47:34 +00001130 distance+=pixel*pixel;
cristy36fbc3b2011-02-09 02:30:04 +00001131 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001132 {
cristy4c08aed2011-07-01 19:47:50 +00001133 pixel=alpha*p->blue-beta*q->blue;
cristy3ed852e2009-09-05 21:47:34 +00001134 distance+=pixel*pixel;
cristy36fbc3b2011-02-09 02:30:04 +00001135 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001136 {
1137 pixel=alpha-beta;
1138 distance+=pixel*pixel;
cristyc4080402011-02-09 02:55:58 +00001139 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001140 {
1141 cube_info->distance=distance;
1142 cube_info->color_number=node_info->color_number;
1143 }
1144 }
1145 }
1146 }
1147 }
1148}
1149
1150/*
1151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1152% %
1153% %
1154% %
1155% C o m p r e s s I m a g e C o l o r m a p %
1156% %
1157% %
1158% %
1159%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1160%
1161% CompressImageColormap() compresses an image colormap by removing any
1162% duplicate or unused color entries.
1163%
1164% The format of the CompressImageColormap method is:
1165%
cristy018f07f2011-09-04 21:15:19 +00001166% MagickBooleanType CompressImageColormap(Image *image,
1167% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001168%
1169% A description of each parameter follows:
1170%
1171% o image: the image.
1172%
cristy018f07f2011-09-04 21:15:19 +00001173% o exception: return any errors or warnings in this structure.
1174%
cristy3ed852e2009-09-05 21:47:34 +00001175*/
cristy018f07f2011-09-04 21:15:19 +00001176MagickExport MagickBooleanType CompressImageColormap(Image *image,
1177 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001178{
1179 QuantizeInfo
1180 quantize_info;
1181
1182 assert(image != (Image *) NULL);
1183 assert(image->signature == MagickSignature);
1184 if (image->debug != MagickFalse)
1185 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy8a11cb12011-10-19 23:53:34 +00001186 if (IsPaletteImage(image,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001187 return(MagickFalse);
1188 GetQuantizeInfo(&quantize_info);
1189 quantize_info.number_colors=image->colors;
1190 quantize_info.tree_depth=MaxTreeDepth;
cristy018f07f2011-09-04 21:15:19 +00001191 return(QuantizeImage(&quantize_info,image,exception));
cristy3ed852e2009-09-05 21:47:34 +00001192}
1193
1194/*
1195%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1196% %
1197% %
1198% %
1199+ D e f i n e I m a g e C o l o r m a p %
1200% %
1201% %
1202% %
1203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1204%
1205% DefineImageColormap() traverses the color cube tree and notes each colormap
1206% entry. A colormap entry is any node in the color cube tree where the
1207% of unique colors is not zero. DefineImageColormap() returns the number of
1208% colors in the image colormap.
1209%
1210% The format of the DefineImageColormap method is:
1211%
cristybb503372010-05-27 20:51:26 +00001212% size_t DefineImageColormap(Image *image,CubeInfo *cube_info,
cristy3ed852e2009-09-05 21:47:34 +00001213% NodeInfo *node_info)
1214%
1215% A description of each parameter follows.
1216%
1217% o image: the image.
1218%
1219% o cube_info: A pointer to the Cube structure.
1220%
1221% o node_info: the address of a structure of type NodeInfo which points to a
1222% node in the color cube tree that is to be pruned.
1223%
1224*/
cristybb503372010-05-27 20:51:26 +00001225static size_t DefineImageColormap(Image *image,CubeInfo *cube_info,
cristy3ed852e2009-09-05 21:47:34 +00001226 NodeInfo *node_info)
1227{
cristybb503372010-05-27 20:51:26 +00001228 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001229 i;
1230
cristybb503372010-05-27 20:51:26 +00001231 size_t
cristy3ed852e2009-09-05 21:47:34 +00001232 number_children;
1233
1234 /*
1235 Traverse any children.
1236 */
1237 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00001238 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00001239 if (node_info->child[i] != (NodeInfo *) NULL)
cristycee97112010-05-28 00:44:52 +00001240 (void) DefineImageColormap(image,cube_info,node_info->child[i]);
cristy3ed852e2009-09-05 21:47:34 +00001241 if (node_info->number_unique != 0)
1242 {
1243 register MagickRealType
1244 alpha;
1245
cristy101ab702011-10-13 13:06:32 +00001246 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001247 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001248
1249 /*
1250 Colormap entry is defined by the mean color in this cube.
1251 */
1252 q=image->colormap+image->colors;
1253 alpha=(MagickRealType) ((MagickOffsetType) node_info->number_unique);
1254 alpha=1.0/(fabs(alpha) <= MagickEpsilon ? 1.0 : alpha);
1255 if (cube_info->associate_alpha == MagickFalse)
1256 {
cristye42f6582012-02-11 17:59:50 +00001257 q->red=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001258 (alpha*QuantumRange*node_info->total_color.red));
cristye42f6582012-02-11 17:59:50 +00001259 q->green=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001260 (alpha*QuantumRange*node_info->total_color.green));
cristye42f6582012-02-11 17:59:50 +00001261 q->blue=(double) ClampToQuantum((MagickRealType)
1262 (alpha*(double) QuantumRange*node_info->total_color.blue));
cristy4c08aed2011-07-01 19:47:50 +00001263 q->alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00001264 }
1265 else
1266 {
1267 MagickRealType
1268 opacity;
1269
1270 opacity=(MagickRealType) (alpha*QuantumRange*
cristy4c08aed2011-07-01 19:47:50 +00001271 node_info->total_color.alpha);
cristye42f6582012-02-11 17:59:50 +00001272 q->alpha=(double) ClampToQuantum(opacity);
cristy4c08aed2011-07-01 19:47:50 +00001273 if (q->alpha == OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00001274 {
cristye42f6582012-02-11 17:59:50 +00001275 q->red=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001276 (alpha*QuantumRange*node_info->total_color.red));
cristye42f6582012-02-11 17:59:50 +00001277 q->green=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001278 (alpha*QuantumRange*node_info->total_color.green));
cristye42f6582012-02-11 17:59:50 +00001279 q->blue=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001280 (alpha*QuantumRange*node_info->total_color.blue));
cristy3ed852e2009-09-05 21:47:34 +00001281 }
1282 else
1283 {
1284 MagickRealType
1285 gamma;
1286
cristy4c08aed2011-07-01 19:47:50 +00001287 gamma=(MagickRealType) (QuantumScale*q->alpha);
cristy3ed852e2009-09-05 21:47:34 +00001288 gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma);
cristye42f6582012-02-11 17:59:50 +00001289 q->red=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001290 (alpha*gamma*QuantumRange*node_info->total_color.red));
cristye42f6582012-02-11 17:59:50 +00001291 q->green=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001292 (alpha*gamma*QuantumRange*node_info->total_color.green));
cristye42f6582012-02-11 17:59:50 +00001293 q->blue=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001294 (alpha*gamma*QuantumRange*node_info->total_color.blue));
cristy3ed852e2009-09-05 21:47:34 +00001295 if (node_info->number_unique > cube_info->transparent_pixels)
1296 {
1297 cube_info->transparent_pixels=node_info->number_unique;
cristybb503372010-05-27 20:51:26 +00001298 cube_info->transparent_index=(ssize_t) image->colors;
cristy3ed852e2009-09-05 21:47:34 +00001299 }
1300 }
1301 }
1302 node_info->color_number=image->colors++;
1303 }
1304 return(image->colors);
1305}
1306
1307/*
1308%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1309% %
1310% %
1311% %
1312+ D e s t r o y C u b e I n f o %
1313% %
1314% %
1315% %
1316%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1317%
1318% DestroyCubeInfo() deallocates memory associated with an image.
1319%
1320% The format of the DestroyCubeInfo method is:
1321%
1322% DestroyCubeInfo(CubeInfo *cube_info)
1323%
1324% A description of each parameter follows:
1325%
1326% o cube_info: the address of a structure of type CubeInfo.
1327%
1328*/
1329static void DestroyCubeInfo(CubeInfo *cube_info)
1330{
1331 register Nodes
1332 *nodes;
1333
1334 /*
1335 Release color cube tree storage.
1336 */
1337 do
1338 {
1339 nodes=cube_info->node_queue->next;
1340 cube_info->node_queue->nodes=(NodeInfo *) RelinquishMagickMemory(
1341 cube_info->node_queue->nodes);
1342 cube_info->node_queue=(Nodes *) RelinquishMagickMemory(
1343 cube_info->node_queue);
1344 cube_info->node_queue=nodes;
1345 } while (cube_info->node_queue != (Nodes *) NULL);
cristybb503372010-05-27 20:51:26 +00001346 if (cube_info->cache != (ssize_t *) NULL)
1347 cube_info->cache=(ssize_t *) RelinquishMagickMemory(cube_info->cache);
cristy3ed852e2009-09-05 21:47:34 +00001348 cube_info->quantize_info=DestroyQuantizeInfo(cube_info->quantize_info);
1349 cube_info=(CubeInfo *) RelinquishMagickMemory(cube_info);
1350}
1351
1352/*
1353%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1354% %
1355% %
1356% %
1357% D e s t r o y Q u a n t i z e I n f o %
1358% %
1359% %
1360% %
1361%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1362%
1363% DestroyQuantizeInfo() deallocates memory associated with an QuantizeInfo
1364% structure.
1365%
1366% The format of the DestroyQuantizeInfo method is:
1367%
1368% QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info)
1369%
1370% A description of each parameter follows:
1371%
1372% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
1373%
1374*/
1375MagickExport QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info)
1376{
1377 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1378 assert(quantize_info != (QuantizeInfo *) NULL);
1379 assert(quantize_info->signature == MagickSignature);
1380 quantize_info->signature=(~MagickSignature);
1381 quantize_info=(QuantizeInfo *) RelinquishMagickMemory(quantize_info);
1382 return(quantize_info);
1383}
1384
1385/*
1386%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387% %
1388% %
1389% %
1390+ D i t h e r I m a g e %
1391% %
1392% %
1393% %
1394%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1395%
1396% DitherImage() distributes the difference between an original image and
1397% the corresponding color reduced algorithm to neighboring pixels using
1398% serpentine-scan Floyd-Steinberg error diffusion. DitherImage returns
1399% MagickTrue if the image is dithered otherwise MagickFalse.
1400%
1401% The format of the DitherImage method is:
1402%
cristy8a11cb12011-10-19 23:53:34 +00001403% MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info,
1404% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001405%
1406% A description of each parameter follows.
1407%
1408% o image: the image.
1409%
1410% o cube_info: A pointer to the Cube structure.
1411%
cristy8a11cb12011-10-19 23:53:34 +00001412% o exception: return any errors or warnings in this structure.
1413%
cristy3ed852e2009-09-05 21:47:34 +00001414*/
1415
cristy101ab702011-10-13 13:06:32 +00001416static RealPixelInfo **DestroyPixelThreadSet(RealPixelInfo **pixels)
cristye9717ac2011-02-20 16:17:17 +00001417{
1418 register ssize_t
1419 i;
1420
cristy101ab702011-10-13 13:06:32 +00001421 assert(pixels != (RealPixelInfo **) NULL);
cristyac245f82012-05-05 17:13:57 +00001422 for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++)
cristy101ab702011-10-13 13:06:32 +00001423 if (pixels[i] != (RealPixelInfo *) NULL)
1424 pixels[i]=(RealPixelInfo *) RelinquishMagickMemory(pixels[i]);
1425 pixels=(RealPixelInfo **) RelinquishMagickMemory(pixels);
cristye9717ac2011-02-20 16:17:17 +00001426 return(pixels);
1427}
1428
cristy101ab702011-10-13 13:06:32 +00001429static RealPixelInfo **AcquirePixelThreadSet(const size_t count)
cristye9717ac2011-02-20 16:17:17 +00001430{
cristy101ab702011-10-13 13:06:32 +00001431 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001432 **pixels;
1433
1434 register ssize_t
1435 i;
1436
1437 size_t
1438 number_threads;
1439
cristyfeeb98d2012-05-09 16:32:12 +00001440 number_threads=GetOpenMPMaximumThreads();
cristy101ab702011-10-13 13:06:32 +00001441 pixels=(RealPixelInfo **) AcquireQuantumMemory(number_threads,
cristye9717ac2011-02-20 16:17:17 +00001442 sizeof(*pixels));
cristy101ab702011-10-13 13:06:32 +00001443 if (pixels == (RealPixelInfo **) NULL)
1444 return((RealPixelInfo **) NULL);
cristye9717ac2011-02-20 16:17:17 +00001445 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
1446 for (i=0; i < (ssize_t) number_threads; i++)
1447 {
cristy101ab702011-10-13 13:06:32 +00001448 pixels[i]=(RealPixelInfo *) AcquireQuantumMemory(count,
cristye9717ac2011-02-20 16:17:17 +00001449 2*sizeof(**pixels));
cristy101ab702011-10-13 13:06:32 +00001450 if (pixels[i] == (RealPixelInfo *) NULL)
cristye9717ac2011-02-20 16:17:17 +00001451 return(DestroyPixelThreadSet(pixels));
1452 }
1453 return(pixels);
1454}
1455
cristyca972de2010-06-20 23:37:02 +00001456static inline ssize_t CacheOffset(CubeInfo *cube_info,
cristy101ab702011-10-13 13:06:32 +00001457 const RealPixelInfo *pixel)
cristyca972de2010-06-20 23:37:02 +00001458{
1459#define RedShift(pixel) (((pixel) >> CacheShift) << (0*(8-CacheShift)))
1460#define GreenShift(pixel) (((pixel) >> CacheShift) << (1*(8-CacheShift)))
1461#define BlueShift(pixel) (((pixel) >> CacheShift) << (2*(8-CacheShift)))
1462#define AlphaShift(pixel) (((pixel) >> CacheShift) << (3*(8-CacheShift)))
1463
1464 ssize_t
1465 offset;
1466
1467 offset=(ssize_t)
cristy15893a42010-11-20 18:57:15 +00001468 (RedShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->red))) |
cristyca972de2010-06-20 23:37:02 +00001469 GreenShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->green))) |
cristy15893a42010-11-20 18:57:15 +00001470 BlueShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->blue))));
cristyca972de2010-06-20 23:37:02 +00001471 if (cube_info->associate_alpha != MagickFalse)
cristy15893a42010-11-20 18:57:15 +00001472 offset|=AlphaShift(ScaleQuantumToChar(ClampToUnsignedQuantum(
cristy4c08aed2011-07-01 19:47:50 +00001473 pixel->alpha)));
cristyca972de2010-06-20 23:37:02 +00001474 return(offset);
1475}
1476
cristy8a11cb12011-10-19 23:53:34 +00001477static MagickBooleanType FloydSteinbergDither(Image *image,CubeInfo *cube_info,
1478 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001479{
1480#define DitherImageTag "Dither/Image"
1481
cristyc4c8d132010-01-07 01:58:38 +00001482 CacheView
1483 *image_view;
1484
cristy3ed852e2009-09-05 21:47:34 +00001485 MagickBooleanType
cristye9717ac2011-02-20 16:17:17 +00001486 status;
cristy3ed852e2009-09-05 21:47:34 +00001487
cristy101ab702011-10-13 13:06:32 +00001488 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001489 **pixels;
cristy3ed852e2009-09-05 21:47:34 +00001490
cristy847620f2011-02-09 02:24:21 +00001491 ssize_t
cristy847620f2011-02-09 02:24:21 +00001492 y;
1493
cristy3ed852e2009-09-05 21:47:34 +00001494 /*
1495 Distribute quantization error using Floyd-Steinberg.
1496 */
cristye9717ac2011-02-20 16:17:17 +00001497 pixels=AcquirePixelThreadSet(image->columns);
cristy101ab702011-10-13 13:06:32 +00001498 if (pixels == (RealPixelInfo **) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001499 return(MagickFalse);
cristye9717ac2011-02-20 16:17:17 +00001500 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00001501 image_view=AcquireAuthenticCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00001502 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001503 {
cristye9717ac2011-02-20 16:17:17 +00001504 const int
1505 id = GetOpenMPThreadId();
1506
1507 CubeInfo
1508 cube;
1509
cristy101ab702011-10-13 13:06:32 +00001510 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001511 *current,
1512 *previous;
1513
cristy4c08aed2011-07-01 19:47:50 +00001514 register Quantum
cristyecc31b12011-02-13 00:32:29 +00001515 *restrict q;
1516
cristybb503372010-05-27 20:51:26 +00001517 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001518 x;
1519
cristye9717ac2011-02-20 16:17:17 +00001520 size_t
1521 index;
1522
1523 ssize_t
1524 v;
1525
1526 if (status == MagickFalse)
1527 continue;
cristy3ed852e2009-09-05 21:47:34 +00001528 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001529 if (q == (Quantum *) NULL)
cristye9717ac2011-02-20 16:17:17 +00001530 {
1531 status=MagickFalse;
cristy00cbdd62011-02-20 17:29:26 +00001532 continue;
cristye9717ac2011-02-20 16:17:17 +00001533 }
cristyed231572011-07-14 02:18:59 +00001534 q+=(y & 0x01)*image->columns*GetPixelChannels(image);
cristye9717ac2011-02-20 16:17:17 +00001535 cube=(*cube_info);
1536 current=pixels[id]+(y & 0x01)*image->columns;
1537 previous=pixels[id]+((y+1) & 0x01)*image->columns;
cristy4c08aed2011-07-01 19:47:50 +00001538 v=(ssize_t) ((y & 0x01) != 0 ? -1 : 1);
cristybb503372010-05-27 20:51:26 +00001539 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001540 {
cristy101ab702011-10-13 13:06:32 +00001541 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001542 color,
1543 pixel;
1544
1545 register ssize_t
1546 i;
1547
1548 ssize_t
1549 u;
1550
cristyed231572011-07-14 02:18:59 +00001551 q-=(y & 0x01)*GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001552 u=(y & 0x01) != 0 ? (ssize_t) image->columns-1-x : x;
1553 AssociateAlphaPixel(image,&cube,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001554 if (x > 0)
1555 {
1556 pixel.red+=7*current[u-v].red/16;
1557 pixel.green+=7*current[u-v].green/16;
1558 pixel.blue+=7*current[u-v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001559 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001560 pixel.alpha+=7*current[u-v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001561 }
1562 if (y > 0)
1563 {
cristybb503372010-05-27 20:51:26 +00001564 if (x < (ssize_t) (image->columns-1))
cristy3ed852e2009-09-05 21:47:34 +00001565 {
1566 pixel.red+=previous[u+v].red/16;
1567 pixel.green+=previous[u+v].green/16;
1568 pixel.blue+=previous[u+v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001569 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001570 pixel.alpha+=previous[u+v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001571 }
1572 pixel.red+=5*previous[u].red/16;
1573 pixel.green+=5*previous[u].green/16;
1574 pixel.blue+=5*previous[u].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001575 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001576 pixel.alpha+=5*previous[u].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001577 if (x > 0)
1578 {
1579 pixel.red+=3*previous[u-v].red/16;
1580 pixel.green+=3*previous[u-v].green/16;
1581 pixel.blue+=3*previous[u-v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001582 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001583 pixel.alpha+=3*previous[u-v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001584 }
1585 }
cristy75ffdb72010-01-07 17:40:12 +00001586 pixel.red=(MagickRealType) ClampToUnsignedQuantum(pixel.red);
1587 pixel.green=(MagickRealType) ClampToUnsignedQuantum(pixel.green);
1588 pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue);
cristye9717ac2011-02-20 16:17:17 +00001589 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001590 pixel.alpha=(MagickRealType) ClampToUnsignedQuantum(pixel.alpha);
cristye9717ac2011-02-20 16:17:17 +00001591 i=CacheOffset(&cube,&pixel);
1592 if (cube.cache[i] < 0)
cristy3ed852e2009-09-05 21:47:34 +00001593 {
1594 register NodeInfo
1595 *node_info;
1596
cristybb503372010-05-27 20:51:26 +00001597 register size_t
cristy3ed852e2009-09-05 21:47:34 +00001598 id;
1599
1600 /*
1601 Identify the deepest node containing the pixel's color.
1602 */
cristye9717ac2011-02-20 16:17:17 +00001603 node_info=cube.root;
cristybb503372010-05-27 20:51:26 +00001604 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +00001605 {
cristye9717ac2011-02-20 16:17:17 +00001606 id=ColorToNodeId(&cube,&pixel,index);
cristy3ed852e2009-09-05 21:47:34 +00001607 if (node_info->child[id] == (NodeInfo *) NULL)
1608 break;
1609 node_info=node_info->child[id];
1610 }
1611 /*
1612 Find closest color among siblings and their children.
1613 */
cristye9717ac2011-02-20 16:17:17 +00001614 cube.target=pixel;
1615 cube.distance=(MagickRealType) (4.0*(QuantumRange+1.0)*(QuantumRange+
cristy3ed852e2009-09-05 21:47:34 +00001616 1.0)+1.0);
cristye9717ac2011-02-20 16:17:17 +00001617 ClosestColor(image,&cube,node_info->parent);
1618 cube.cache[i]=(ssize_t) cube.color_number;
cristy3ed852e2009-09-05 21:47:34 +00001619 }
1620 /*
1621 Assign pixel to closest colormap entry.
1622 */
cristye9717ac2011-02-20 16:17:17 +00001623 index=(size_t) cube.cache[i];
cristy3ed852e2009-09-05 21:47:34 +00001624 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +00001625 SetPixelIndex(image,(Quantum) index,q);
cristye9717ac2011-02-20 16:17:17 +00001626 if (cube.quantize_info->measure_error == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001627 {
cristye42f6582012-02-11 17:59:50 +00001628 SetPixelRed(image,ClampToQuantum(image->colormap[index].red),q);
1629 SetPixelGreen(image,ClampToQuantum(image->colormap[index].green),q);
1630 SetPixelBlue(image,ClampToQuantum(image->colormap[index].blue),q);
cristye9717ac2011-02-20 16:17:17 +00001631 if (cube.associate_alpha != MagickFalse)
cristye42f6582012-02-11 17:59:50 +00001632 SetPixelAlpha(image,ClampToQuantum(image->colormap[index].alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001633 }
1634 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristye9717ac2011-02-20 16:17:17 +00001635 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001636 /*
1637 Store the error.
1638 */
cristy101ab702011-10-13 13:06:32 +00001639 AssociateAlphaPixelInfo(image,&cube,image->colormap+index,&color);
cristy3ed852e2009-09-05 21:47:34 +00001640 current[u].red=pixel.red-color.red;
1641 current[u].green=pixel.green-color.green;
1642 current[u].blue=pixel.blue-color.blue;
cristye9717ac2011-02-20 16:17:17 +00001643 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001644 current[u].alpha=pixel.alpha-color.alpha;
cristye9717ac2011-02-20 16:17:17 +00001645 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1646 {
1647 MagickBooleanType
1648 proceed;
1649
1650#if defined(MAGICKCORE_OPENMP_SUPPORT)
1651 #pragma omp critical (MagickCore_FloydSteinbergDither)
1652#endif
1653 proceed=SetImageProgress(image,DitherImageTag,(MagickOffsetType) y,
1654 image->rows);
1655 if (proceed == MagickFalse)
1656 status=MagickFalse;
1657 }
cristyed231572011-07-14 02:18:59 +00001658 q+=((y+1) & 0x01)*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001659 }
1660 }
cristy3ed852e2009-09-05 21:47:34 +00001661 image_view=DestroyCacheView(image_view);
cristye9717ac2011-02-20 16:17:17 +00001662 pixels=DestroyPixelThreadSet(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001663 return(MagickTrue);
1664}
1665
1666static MagickBooleanType
cristy8a11cb12011-10-19 23:53:34 +00001667 RiemersmaDither(Image *,CacheView *,CubeInfo *,const unsigned int,
1668 ExceptionInfo *exception);
cristy3ed852e2009-09-05 21:47:34 +00001669
1670static void Riemersma(Image *image,CacheView *image_view,CubeInfo *cube_info,
cristy8a11cb12011-10-19 23:53:34 +00001671 const size_t level,const unsigned int direction,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001672{
1673 if (level == 1)
1674 switch (direction)
1675 {
1676 case WestGravity:
1677 {
cristy8a11cb12011-10-19 23:53:34 +00001678 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1679 exception);
1680 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1681 exception);
1682 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1683 exception);
cristy3ed852e2009-09-05 21:47:34 +00001684 break;
1685 }
1686 case EastGravity:
1687 {
cristy8a11cb12011-10-19 23:53:34 +00001688 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1689 exception);
1690 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1691 exception);
1692 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1693 exception);
cristy3ed852e2009-09-05 21:47:34 +00001694 break;
1695 }
1696 case NorthGravity:
1697 {
cristy8a11cb12011-10-19 23:53:34 +00001698 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1699 exception);
1700 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1701 exception);
1702 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1703 exception);
cristy3ed852e2009-09-05 21:47:34 +00001704 break;
1705 }
1706 case SouthGravity:
1707 {
cristy8a11cb12011-10-19 23:53:34 +00001708 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1709 exception);
1710 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1711 exception);
1712 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1713 exception);
cristy3ed852e2009-09-05 21:47:34 +00001714 break;
1715 }
1716 default:
1717 break;
1718 }
1719 else
1720 switch (direction)
1721 {
1722 case WestGravity:
1723 {
cristy8a11cb12011-10-19 23:53:34 +00001724 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1725 exception);
1726 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1727 exception);
1728 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1729 exception);
1730 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1731 exception);
1732 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1733 exception);
1734 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1735 exception);
1736 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1737 exception);
cristy3ed852e2009-09-05 21:47:34 +00001738 break;
1739 }
1740 case EastGravity:
1741 {
cristy8a11cb12011-10-19 23:53:34 +00001742 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1743 exception);
1744 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1745 exception);
1746 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1747 exception);
1748 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1749 exception);
1750 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1751 exception);
1752 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1753 exception);
1754 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1755 exception);
cristy3ed852e2009-09-05 21:47:34 +00001756 break;
1757 }
1758 case NorthGravity:
1759 {
cristy8a11cb12011-10-19 23:53:34 +00001760 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1761 exception);
1762 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1763 exception);
1764 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1765 exception);
1766 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1767 exception);
1768 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1769 exception);
1770 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1771 exception);
1772 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1773 exception);
cristy3ed852e2009-09-05 21:47:34 +00001774 break;
1775 }
1776 case SouthGravity:
1777 {
cristy8a11cb12011-10-19 23:53:34 +00001778 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1779 exception);
1780 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1781 exception);
1782 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1783 exception);
1784 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1785 exception);
1786 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1787 exception);
1788 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1789 exception);
1790 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1791 exception);
cristy3ed852e2009-09-05 21:47:34 +00001792 break;
1793 }
1794 default:
1795 break;
1796 }
1797}
1798
1799static MagickBooleanType RiemersmaDither(Image *image,CacheView *image_view,
cristy8a11cb12011-10-19 23:53:34 +00001800 CubeInfo *cube_info,const unsigned int direction,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001801{
1802#define DitherImageTag "Dither/Image"
1803
1804 MagickBooleanType
1805 proceed;
1806
cristy101ab702011-10-13 13:06:32 +00001807 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001808 color,
1809 pixel;
1810
1811 register CubeInfo
1812 *p;
1813
cristybb503372010-05-27 20:51:26 +00001814 size_t
cristy3ed852e2009-09-05 21:47:34 +00001815 index;
1816
1817 p=cube_info;
cristybb503372010-05-27 20:51:26 +00001818 if ((p->x >= 0) && (p->x < (ssize_t) image->columns) &&
1819 (p->y >= 0) && (p->y < (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001820 {
cristy4c08aed2011-07-01 19:47:50 +00001821 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001822 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001823
cristyecc31b12011-02-13 00:32:29 +00001824 register ssize_t
1825 i;
1826
cristy3ed852e2009-09-05 21:47:34 +00001827 /*
1828 Distribute error.
1829 */
cristy3ed852e2009-09-05 21:47:34 +00001830 q=GetCacheViewAuthenticPixels(image_view,p->x,p->y,1,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001831 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001832 return(MagickFalse);
cristy4c08aed2011-07-01 19:47:50 +00001833 AssociateAlphaPixel(image,cube_info,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001834 for (i=0; i < ErrorQueueLength; i++)
1835 {
1836 pixel.red+=p->weights[i]*p->error[i].red;
1837 pixel.green+=p->weights[i]*p->error[i].green;
1838 pixel.blue+=p->weights[i]*p->error[i].blue;
1839 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001840 pixel.alpha+=p->weights[i]*p->error[i].alpha;
cristy3ed852e2009-09-05 21:47:34 +00001841 }
cristy75ffdb72010-01-07 17:40:12 +00001842 pixel.red=(MagickRealType) ClampToUnsignedQuantum(pixel.red);
1843 pixel.green=(MagickRealType) ClampToUnsignedQuantum(pixel.green);
1844 pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00001845 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001846 pixel.alpha=(MagickRealType) ClampToUnsignedQuantum(pixel.alpha);
cristyca972de2010-06-20 23:37:02 +00001847 i=CacheOffset(cube_info,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001848 if (p->cache[i] < 0)
1849 {
1850 register NodeInfo
1851 *node_info;
1852
cristybb503372010-05-27 20:51:26 +00001853 register size_t
cristy3ed852e2009-09-05 21:47:34 +00001854 id;
1855
1856 /*
1857 Identify the deepest node containing the pixel's color.
1858 */
1859 node_info=p->root;
cristybb503372010-05-27 20:51:26 +00001860 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +00001861 {
1862 id=ColorToNodeId(cube_info,&pixel,index);
1863 if (node_info->child[id] == (NodeInfo *) NULL)
1864 break;
1865 node_info=node_info->child[id];
1866 }
cristyecc31b12011-02-13 00:32:29 +00001867 node_info=node_info->parent;
cristy3ed852e2009-09-05 21:47:34 +00001868 /*
1869 Find closest color among siblings and their children.
1870 */
1871 p->target=pixel;
1872 p->distance=(MagickRealType) (4.0*(QuantumRange+1.0)*((MagickRealType)
1873 QuantumRange+1.0)+1.0);
1874 ClosestColor(image,p,node_info->parent);
cristybb503372010-05-27 20:51:26 +00001875 p->cache[i]=(ssize_t) p->color_number;
cristy3ed852e2009-09-05 21:47:34 +00001876 }
1877 /*
1878 Assign pixel to closest colormap entry.
1879 */
cristy4c08aed2011-07-01 19:47:50 +00001880 index=(size_t) p->cache[i];
cristy3ed852e2009-09-05 21:47:34 +00001881 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +00001882 SetPixelIndex(image,(Quantum) index,q);
cristy3ed852e2009-09-05 21:47:34 +00001883 if (cube_info->quantize_info->measure_error == MagickFalse)
1884 {
cristye42f6582012-02-11 17:59:50 +00001885 SetPixelRed(image,ClampToQuantum(image->colormap[index].red),q);
1886 SetPixelGreen(image,ClampToQuantum(image->colormap[index].green),q);
1887 SetPixelBlue(image,ClampToQuantum(image->colormap[index].blue),q);
cristy3ed852e2009-09-05 21:47:34 +00001888 if (cube_info->associate_alpha != MagickFalse)
cristye42f6582012-02-11 17:59:50 +00001889 SetPixelAlpha(image,ClampToQuantum(image->colormap[index].alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001890 }
1891 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1892 return(MagickFalse);
1893 /*
1894 Propagate the error as the last entry of the error queue.
1895 */
1896 (void) CopyMagickMemory(p->error,p->error+1,(ErrorQueueLength-1)*
1897 sizeof(p->error[0]));
cristy101ab702011-10-13 13:06:32 +00001898 AssociateAlphaPixelInfo(image,cube_info,image->colormap+index,&color);
cristy3ed852e2009-09-05 21:47:34 +00001899 p->error[ErrorQueueLength-1].red=pixel.red-color.red;
1900 p->error[ErrorQueueLength-1].green=pixel.green-color.green;
1901 p->error[ErrorQueueLength-1].blue=pixel.blue-color.blue;
1902 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001903 p->error[ErrorQueueLength-1].alpha=pixel.alpha-color.alpha;
cristy3ed852e2009-09-05 21:47:34 +00001904 proceed=SetImageProgress(image,DitherImageTag,p->offset,p->span);
1905 if (proceed == MagickFalse)
1906 return(MagickFalse);
1907 p->offset++;
1908 }
1909 switch (direction)
1910 {
1911 case WestGravity: p->x--; break;
1912 case EastGravity: p->x++; break;
1913 case NorthGravity: p->y--; break;
1914 case SouthGravity: p->y++; break;
1915 }
1916 return(MagickTrue);
1917}
1918
cristybb503372010-05-27 20:51:26 +00001919static inline ssize_t MagickMax(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00001920{
1921 if (x > y)
1922 return(x);
1923 return(y);
1924}
1925
cristybb503372010-05-27 20:51:26 +00001926static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00001927{
1928 if (x < y)
1929 return(x);
1930 return(y);
1931}
1932
cristy8a11cb12011-10-19 23:53:34 +00001933static MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info,
1934 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001935{
cristyc4c8d132010-01-07 01:58:38 +00001936 CacheView
1937 *image_view;
1938
cristy3ed852e2009-09-05 21:47:34 +00001939 MagickBooleanType
1940 status;
1941
cristybb503372010-05-27 20:51:26 +00001942 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001943 i;
1944
cristybb503372010-05-27 20:51:26 +00001945 size_t
cristy3ed852e2009-09-05 21:47:34 +00001946 depth;
1947
cristyfb7e9cd2011-02-20 16:26:15 +00001948 if (cube_info->quantize_info->dither_method != RiemersmaDitherMethod)
cristy8a11cb12011-10-19 23:53:34 +00001949 return(FloydSteinbergDither(image,cube_info,exception));
cristy3ed852e2009-09-05 21:47:34 +00001950 /*
cristycee97112010-05-28 00:44:52 +00001951 Distribute quantization error along a Hilbert curve.
cristy3ed852e2009-09-05 21:47:34 +00001952 */
1953 (void) ResetMagickMemory(cube_info->error,0,ErrorQueueLength*
1954 sizeof(*cube_info->error));
1955 cube_info->x=0;
1956 cube_info->y=0;
cristybb503372010-05-27 20:51:26 +00001957 i=MagickMax((ssize_t) image->columns,(ssize_t) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001958 for (depth=1; i != 0; depth++)
1959 i>>=1;
cristybb503372010-05-27 20:51:26 +00001960 if ((ssize_t) (1L << depth) < MagickMax((ssize_t) image->columns,(ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001961 depth++;
1962 cube_info->offset=0;
1963 cube_info->span=(MagickSizeType) image->columns*image->rows;
cristydb070952012-04-20 14:33:00 +00001964 image_view=AcquireAuthenticCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001965 if (depth > 1)
cristy8a11cb12011-10-19 23:53:34 +00001966 Riemersma(image,image_view,cube_info,depth-1,NorthGravity,exception);
1967 status=RiemersmaDither(image,image_view,cube_info,ForgetGravity,exception);
cristy3ed852e2009-09-05 21:47:34 +00001968 image_view=DestroyCacheView(image_view);
1969 return(status);
1970}
1971
1972/*
1973%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1974% %
1975% %
1976% %
1977+ G e t C u b e I n f o %
1978% %
1979% %
1980% %
1981%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1982%
1983% GetCubeInfo() initialize the Cube data structure.
1984%
1985% The format of the GetCubeInfo method is:
1986%
1987% CubeInfo GetCubeInfo(const QuantizeInfo *quantize_info,
cristybb503372010-05-27 20:51:26 +00001988% const size_t depth,const size_t maximum_colors)
cristy3ed852e2009-09-05 21:47:34 +00001989%
1990% A description of each parameter follows.
1991%
1992% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
1993%
1994% o depth: Normally, this integer value is zero or one. A zero or
1995% one tells Quantize to choose a optimal tree depth of Log4(number_colors).
1996% A tree of this depth generally allows the best representation of the
1997% reference image with the least amount of memory and the fastest
1998% computational speed. In some cases, such as an image with low color
1999% dispersion (a few number of colors), a value other than
2000% Log4(number_colors) is required. To expand the color tree completely,
2001% use a value of 8.
2002%
2003% o maximum_colors: maximum colors.
2004%
2005*/
2006static CubeInfo *GetCubeInfo(const QuantizeInfo *quantize_info,
cristybb503372010-05-27 20:51:26 +00002007 const size_t depth,const size_t maximum_colors)
cristy3ed852e2009-09-05 21:47:34 +00002008{
2009 CubeInfo
2010 *cube_info;
2011
2012 MagickRealType
2013 sum,
2014 weight;
2015
cristybb503372010-05-27 20:51:26 +00002016 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002017 i;
2018
cristyecc31b12011-02-13 00:32:29 +00002019 size_t
2020 length;
2021
cristy3ed852e2009-09-05 21:47:34 +00002022 /*
2023 Initialize tree to describe color cube_info.
2024 */
cristy73bd4a52010-10-05 11:24:23 +00002025 cube_info=(CubeInfo *) AcquireMagickMemory(sizeof(*cube_info));
cristy3ed852e2009-09-05 21:47:34 +00002026 if (cube_info == (CubeInfo *) NULL)
2027 return((CubeInfo *) NULL);
2028 (void) ResetMagickMemory(cube_info,0,sizeof(*cube_info));
2029 cube_info->depth=depth;
2030 if (cube_info->depth > MaxTreeDepth)
2031 cube_info->depth=MaxTreeDepth;
2032 if (cube_info->depth < 2)
2033 cube_info->depth=2;
2034 cube_info->maximum_colors=maximum_colors;
2035 /*
2036 Initialize root node.
2037 */
2038 cube_info->root=GetNodeInfo(cube_info,0,0,(NodeInfo *) NULL);
2039 if (cube_info->root == (NodeInfo *) NULL)
2040 return((CubeInfo *) NULL);
2041 cube_info->root->parent=cube_info->root;
2042 cube_info->quantize_info=CloneQuantizeInfo(quantize_info);
2043 if (cube_info->quantize_info->dither == MagickFalse)
2044 return(cube_info);
2045 /*
2046 Initialize dither resources.
2047 */
2048 length=(size_t) (1UL << (4*(8-CacheShift)));
cristybb503372010-05-27 20:51:26 +00002049 cube_info->cache=(ssize_t *) AcquireQuantumMemory(length,
cristy3ed852e2009-09-05 21:47:34 +00002050 sizeof(*cube_info->cache));
cristybb503372010-05-27 20:51:26 +00002051 if (cube_info->cache == (ssize_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002052 return((CubeInfo *) NULL);
2053 /*
2054 Initialize color cache.
2055 */
cristybb503372010-05-27 20:51:26 +00002056 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00002057 cube_info->cache[i]=(-1);
2058 /*
cristycee97112010-05-28 00:44:52 +00002059 Distribute weights along a curve of exponential decay.
cristy3ed852e2009-09-05 21:47:34 +00002060 */
2061 weight=1.0;
2062 for (i=0; i < ErrorQueueLength; i++)
2063 {
2064 cube_info->weights[ErrorQueueLength-i-1]=1.0/weight;
2065 weight*=exp(log(((double) QuantumRange+1.0))/(ErrorQueueLength-1.0));
2066 }
2067 /*
2068 Normalize the weighting factors.
2069 */
2070 weight=0.0;
2071 for (i=0; i < ErrorQueueLength; i++)
2072 weight+=cube_info->weights[i];
2073 sum=0.0;
2074 for (i=0; i < ErrorQueueLength; i++)
2075 {
2076 cube_info->weights[i]/=weight;
2077 sum+=cube_info->weights[i];
2078 }
2079 cube_info->weights[0]+=1.0-sum;
2080 return(cube_info);
2081}
2082
2083/*
2084%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2085% %
2086% %
2087% %
2088+ G e t N o d e I n f o %
2089% %
2090% %
2091% %
2092%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2093%
2094% GetNodeInfo() allocates memory for a new node in the color cube tree and
2095% presets all fields to zero.
2096%
2097% The format of the GetNodeInfo method is:
2098%
cristybb503372010-05-27 20:51:26 +00002099% NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id,
2100% const size_t level,NodeInfo *parent)
cristy3ed852e2009-09-05 21:47:34 +00002101%
2102% A description of each parameter follows.
2103%
2104% o node: The GetNodeInfo method returns a pointer to a queue of nodes.
2105%
2106% o id: Specifies the child number of the node.
2107%
2108% o level: Specifies the level in the storage_class the node resides.
2109%
2110*/
cristybb503372010-05-27 20:51:26 +00002111static NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id,
2112 const size_t level,NodeInfo *parent)
cristy3ed852e2009-09-05 21:47:34 +00002113{
2114 NodeInfo
2115 *node_info;
2116
2117 if (cube_info->free_nodes == 0)
2118 {
2119 Nodes
2120 *nodes;
2121
2122 /*
2123 Allocate a new queue of nodes.
2124 */
cristy73bd4a52010-10-05 11:24:23 +00002125 nodes=(Nodes *) AcquireMagickMemory(sizeof(*nodes));
cristy3ed852e2009-09-05 21:47:34 +00002126 if (nodes == (Nodes *) NULL)
2127 return((NodeInfo *) NULL);
2128 nodes->nodes=(NodeInfo *) AcquireQuantumMemory(NodesInAList,
2129 sizeof(*nodes->nodes));
2130 if (nodes->nodes == (NodeInfo *) NULL)
2131 return((NodeInfo *) NULL);
2132 nodes->next=cube_info->node_queue;
2133 cube_info->node_queue=nodes;
2134 cube_info->next_node=nodes->nodes;
2135 cube_info->free_nodes=NodesInAList;
2136 }
2137 cube_info->nodes++;
2138 cube_info->free_nodes--;
2139 node_info=cube_info->next_node++;
2140 (void) ResetMagickMemory(node_info,0,sizeof(*node_info));
2141 node_info->parent=parent;
2142 node_info->id=id;
2143 node_info->level=level;
2144 return(node_info);
2145}
2146
2147/*
2148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2149% %
2150% %
2151% %
2152% G e t I m a g e Q u a n t i z e E r r o r %
2153% %
2154% %
2155% %
2156%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2157%
2158% GetImageQuantizeError() measures the difference between the original
2159% and quantized images. This difference is the total quantization error.
2160% The error is computed by summing over all pixels in an image the distance
2161% squared in RGB space between each reference pixel value and its quantized
2162% value. These values are computed:
2163%
2164% o mean_error_per_pixel: This value is the mean error for any single
2165% pixel in the image.
2166%
2167% o normalized_mean_square_error: This value is the normalized mean
2168% quantization error for any single pixel in the image. This distance
2169% measure is normalized to a range between 0 and 1. It is independent
2170% of the range of red, green, and blue values in the image.
2171%
2172% o normalized_maximum_square_error: Thsi value is the normalized
2173% maximum quantization error for any single pixel in the image. This
2174% distance measure is normalized to a range between 0 and 1. It is
2175% independent of the range of red, green, and blue values in your image.
2176%
2177% The format of the GetImageQuantizeError method is:
2178%
cristy8a11cb12011-10-19 23:53:34 +00002179% MagickBooleanType GetImageQuantizeError(Image *image,
2180% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002181%
2182% A description of each parameter follows.
2183%
2184% o image: the image.
2185%
cristy8a11cb12011-10-19 23:53:34 +00002186% o exception: return any errors or warnings in this structure.
2187%
cristy3ed852e2009-09-05 21:47:34 +00002188*/
cristy8a11cb12011-10-19 23:53:34 +00002189MagickExport MagickBooleanType GetImageQuantizeError(Image *image,
2190 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002191{
cristyc4c8d132010-01-07 01:58:38 +00002192 CacheView
2193 *image_view;
2194
cristy3ed852e2009-09-05 21:47:34 +00002195 MagickRealType
2196 alpha,
2197 area,
2198 beta,
2199 distance,
2200 maximum_error,
2201 mean_error,
2202 mean_error_per_pixel;
2203
cristybb503372010-05-27 20:51:26 +00002204 size_t
cristy3ed852e2009-09-05 21:47:34 +00002205 index;
2206
cristyecc31b12011-02-13 00:32:29 +00002207 ssize_t
2208 y;
2209
cristy3ed852e2009-09-05 21:47:34 +00002210 assert(image != (Image *) NULL);
2211 assert(image->signature == MagickSignature);
2212 if (image->debug != MagickFalse)
2213 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy8a11cb12011-10-19 23:53:34 +00002214 image->total_colors=GetNumberColors(image,(FILE *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00002215 (void) ResetMagickMemory(&image->error,0,sizeof(image->error));
2216 if (image->storage_class == DirectClass)
2217 return(MagickTrue);
2218 alpha=1.0;
2219 beta=1.0;
2220 area=3.0*image->columns*image->rows;
2221 maximum_error=0.0;
2222 mean_error_per_pixel=0.0;
2223 mean_error=0.0;
cristydb070952012-04-20 14:33:00 +00002224 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00002225 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002226 {
cristy4c08aed2011-07-01 19:47:50 +00002227 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002228 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002229
cristybb503372010-05-27 20:51:26 +00002230 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002231 x;
2232
2233 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002234 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002235 break;
cristybb503372010-05-27 20:51:26 +00002236 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002237 {
cristy4c08aed2011-07-01 19:47:50 +00002238 index=1UL*GetPixelIndex(image,p);
cristy3ed852e2009-09-05 21:47:34 +00002239 if (image->matte != MagickFalse)
2240 {
cristy4c08aed2011-07-01 19:47:50 +00002241 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
2242 beta=(MagickRealType) (QuantumScale*image->colormap[index].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002243 }
cristy4c08aed2011-07-01 19:47:50 +00002244 distance=fabs(alpha*GetPixelRed(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002245 image->colormap[index].red);
cristy3ed852e2009-09-05 21:47:34 +00002246 mean_error_per_pixel+=distance;
2247 mean_error+=distance*distance;
2248 if (distance > maximum_error)
2249 maximum_error=distance;
cristy4c08aed2011-07-01 19:47:50 +00002250 distance=fabs(alpha*GetPixelGreen(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002251 image->colormap[index].green);
cristy3ed852e2009-09-05 21:47:34 +00002252 mean_error_per_pixel+=distance;
2253 mean_error+=distance*distance;
2254 if (distance > maximum_error)
2255 maximum_error=distance;
cristy4c08aed2011-07-01 19:47:50 +00002256 distance=fabs(alpha*GetPixelBlue(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002257 image->colormap[index].blue);
cristy3ed852e2009-09-05 21:47:34 +00002258 mean_error_per_pixel+=distance;
2259 mean_error+=distance*distance;
2260 if (distance > maximum_error)
2261 maximum_error=distance;
cristyed231572011-07-14 02:18:59 +00002262 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002263 }
2264 }
2265 image_view=DestroyCacheView(image_view);
2266 image->error.mean_error_per_pixel=(double) mean_error_per_pixel/area;
2267 image->error.normalized_mean_error=(double) QuantumScale*QuantumScale*
2268 mean_error/area;
2269 image->error.normalized_maximum_error=(double) QuantumScale*maximum_error;
2270 return(MagickTrue);
2271}
2272
2273/*
2274%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2275% %
2276% %
2277% %
2278% G e t Q u a n t i z e I n f o %
2279% %
2280% %
2281% %
2282%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2283%
2284% GetQuantizeInfo() initializes the QuantizeInfo structure.
2285%
2286% The format of the GetQuantizeInfo method is:
2287%
2288% GetQuantizeInfo(QuantizeInfo *quantize_info)
2289%
2290% A description of each parameter follows:
2291%
2292% o quantize_info: Specifies a pointer to a QuantizeInfo structure.
2293%
2294*/
2295MagickExport void GetQuantizeInfo(QuantizeInfo *quantize_info)
2296{
2297 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2298 assert(quantize_info != (QuantizeInfo *) NULL);
2299 (void) ResetMagickMemory(quantize_info,0,sizeof(*quantize_info));
2300 quantize_info->number_colors=256;
2301 quantize_info->dither=MagickTrue;
2302 quantize_info->dither_method=RiemersmaDitherMethod;
2303 quantize_info->colorspace=UndefinedColorspace;
2304 quantize_info->measure_error=MagickFalse;
2305 quantize_info->signature=MagickSignature;
2306}
2307
2308/*
2309%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2310% %
2311% %
2312% %
cristy018f07f2011-09-04 21:15:19 +00002313% P o s t e r i z e I m a g e %
cristy3ed852e2009-09-05 21:47:34 +00002314% %
2315% %
2316% %
2317%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2318%
2319% PosterizeImage() reduces the image to a limited number of colors for a
2320% "poster" effect.
2321%
2322% The format of the PosterizeImage method is:
2323%
cristybb503372010-05-27 20:51:26 +00002324% MagickBooleanType PosterizeImage(Image *image,const size_t levels,
cristy018f07f2011-09-04 21:15:19 +00002325% const MagickBooleanType dither,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002326%
2327% A description of each parameter follows:
2328%
2329% o image: Specifies a pointer to an Image structure.
2330%
2331% o levels: Number of color levels allowed in each channel. Very low values
2332% (2, 3, or 4) have the most visible effect.
2333%
cristy847620f2011-02-09 02:24:21 +00002334% o dither: Set this integer value to something other than zero to dither
2335% the mapped image.
cristy3ed852e2009-09-05 21:47:34 +00002336%
cristy018f07f2011-09-04 21:15:19 +00002337% o exception: return any errors or warnings in this structure.
2338%
cristy3ed852e2009-09-05 21:47:34 +00002339*/
cristyd1a2c0f2011-02-09 14:14:50 +00002340
cristy4d727152011-02-10 19:57:21 +00002341static inline ssize_t MagickRound(MagickRealType x)
2342{
2343 /*
cristyecc31b12011-02-13 00:32:29 +00002344 Round the fraction to nearest integer.
cristy4d727152011-02-10 19:57:21 +00002345 */
2346 if (x >= 0.0)
2347 return((ssize_t) (x+0.5));
2348 return((ssize_t) (x-0.5));
2349}
2350
cristyd1a2c0f2011-02-09 14:14:50 +00002351MagickExport MagickBooleanType PosterizeImage(Image *image,const size_t levels,
cristy018f07f2011-09-04 21:15:19 +00002352 const MagickBooleanType dither,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002353{
cristyd1a2c0f2011-02-09 14:14:50 +00002354#define PosterizeImageTag "Posterize/Image"
cristy4d727152011-02-10 19:57:21 +00002355#define PosterizePixel(pixel) (Quantum) (QuantumRange*(MagickRound( \
cristy3e9cad02011-02-20 01:42:00 +00002356 QuantumScale*pixel*(levels-1)))/MagickMax((ssize_t) levels-1,1))
cristyd1a2c0f2011-02-09 14:14:50 +00002357
cristyc4c8d132010-01-07 01:58:38 +00002358 CacheView
cristyd1a2c0f2011-02-09 14:14:50 +00002359 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00002360
cristy3ed852e2009-09-05 21:47:34 +00002361 MagickBooleanType
2362 status;
2363
cristyd1a2c0f2011-02-09 14:14:50 +00002364 MagickOffsetType
2365 progress;
2366
cristy3ed852e2009-09-05 21:47:34 +00002367 QuantizeInfo
2368 *quantize_info;
2369
cristy847620f2011-02-09 02:24:21 +00002370 register ssize_t
2371 i;
2372
cristy847620f2011-02-09 02:24:21 +00002373 ssize_t
cristyd1a2c0f2011-02-09 14:14:50 +00002374 y;
cristy847620f2011-02-09 02:24:21 +00002375
cristy3ed852e2009-09-05 21:47:34 +00002376 assert(image != (Image *) NULL);
2377 assert(image->signature == MagickSignature);
2378 if (image->debug != MagickFalse)
2379 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyd1a2c0f2011-02-09 14:14:50 +00002380 if (image->storage_class == PseudoClass)
2381#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002382 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00002383 dynamic_number_threads(image,image->columns,1,1)
cristyd1a2c0f2011-02-09 14:14:50 +00002384#endif
2385 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002386 {
cristyd1a2c0f2011-02-09 14:14:50 +00002387 /*
2388 Posterize colormap.
2389 */
cristyed231572011-07-14 02:18:59 +00002390 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002391 image->colormap[i].red=(double)
2392 PosterizePixel(image->colormap[i].red);
cristyed231572011-07-14 02:18:59 +00002393 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002394 image->colormap[i].green=(double)
2395 PosterizePixel(image->colormap[i].green);
cristyed231572011-07-14 02:18:59 +00002396 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002397 image->colormap[i].blue=(double)
2398 PosterizePixel(image->colormap[i].blue);
cristyed231572011-07-14 02:18:59 +00002399 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002400 image->colormap[i].alpha=(double)
2401 PosterizePixel(image->colormap[i].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002402 }
cristyd1a2c0f2011-02-09 14:14:50 +00002403 /*
2404 Posterize image.
2405 */
2406 status=MagickTrue;
2407 progress=0;
cristydb070952012-04-20 14:33:00 +00002408 image_view=AcquireAuthenticCacheView(image,exception);
cristyd1a2c0f2011-02-09 14:14:50 +00002409#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00002410 #pragma omp parallel for schedule(static,4) shared(progress,status) \
cristy4ee2b0c2012-05-15 00:30:35 +00002411 dynamic_number_threads(image,image->columns,image->rows,1)
cristyd1a2c0f2011-02-09 14:14:50 +00002412#endif
2413 for (y=0; y < (ssize_t) image->rows; y++)
2414 {
cristy4c08aed2011-07-01 19:47:50 +00002415 register Quantum
cristyd1a2c0f2011-02-09 14:14:50 +00002416 *restrict q;
2417
2418 register ssize_t
2419 x;
2420
2421 if (status == MagickFalse)
2422 continue;
2423 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002424 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002425 {
cristyd1a2c0f2011-02-09 14:14:50 +00002426 status=MagickFalse;
2427 continue;
cristy3ed852e2009-09-05 21:47:34 +00002428 }
cristyd1a2c0f2011-02-09 14:14:50 +00002429 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002430 {
cristyed231572011-07-14 02:18:59 +00002431 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002432 SetPixelRed(image,PosterizePixel(GetPixelRed(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002433 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002434 SetPixelGreen(image,PosterizePixel(GetPixelGreen(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002435 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002436 SetPixelBlue(image,PosterizePixel(GetPixelBlue(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002437 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002438 (image->colorspace == CMYKColorspace))
2439 SetPixelBlack(image,PosterizePixel(GetPixelBlack(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002440 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristyd1a2c0f2011-02-09 14:14:50 +00002441 (image->matte == MagickTrue))
cristy4c08aed2011-07-01 19:47:50 +00002442 SetPixelAlpha(image,PosterizePixel(GetPixelAlpha(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002443 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002444 }
cristyd1a2c0f2011-02-09 14:14:50 +00002445 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2446 status=MagickFalse;
2447 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2448 {
2449 MagickBooleanType
2450 proceed;
2451
2452#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13020672011-07-08 02:33:26 +00002453 #pragma omp critical (MagickCore_PosterizeImage)
cristyd1a2c0f2011-02-09 14:14:50 +00002454#endif
2455 proceed=SetImageProgress(image,PosterizeImageTag,progress++,
2456 image->rows);
2457 if (proceed == MagickFalse)
2458 status=MagickFalse;
2459 }
2460 }
2461 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002462 quantize_info=AcquireQuantizeInfo((ImageInfo *) NULL);
cristyd1a2c0f2011-02-09 14:14:50 +00002463 quantize_info->number_colors=(size_t) MagickMin((ssize_t) levels*levels*
2464 levels,MaxColormapSize+1);
cristy3ed852e2009-09-05 21:47:34 +00002465 quantize_info->dither=dither;
cristy3e9cad02011-02-20 01:42:00 +00002466 quantize_info->tree_depth=MaxTreeDepth;
cristy018f07f2011-09-04 21:15:19 +00002467 status=QuantizeImage(quantize_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002468 quantize_info=DestroyQuantizeInfo(quantize_info);
cristy3ed852e2009-09-05 21:47:34 +00002469 return(status);
2470}
2471
2472/*
2473%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2474% %
2475% %
2476% %
2477+ P r u n e C h i l d %
2478% %
2479% %
2480% %
2481%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2482%
2483% PruneChild() deletes the given node and merges its statistics into its
2484% parent.
2485%
2486% The format of the PruneSubtree method is:
2487%
2488% PruneChild(const Image *image,CubeInfo *cube_info,
2489% const NodeInfo *node_info)
2490%
2491% A description of each parameter follows.
2492%
2493% o image: the image.
2494%
2495% o cube_info: A pointer to the Cube structure.
2496%
2497% o node_info: pointer to node in color cube tree that is to be pruned.
2498%
2499*/
2500static void PruneChild(const Image *image,CubeInfo *cube_info,
2501 const NodeInfo *node_info)
2502{
2503 NodeInfo
2504 *parent;
2505
cristybb503372010-05-27 20:51:26 +00002506 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002507 i;
2508
cristybb503372010-05-27 20:51:26 +00002509 size_t
cristy3ed852e2009-09-05 21:47:34 +00002510 number_children;
2511
2512 /*
2513 Traverse any children.
2514 */
2515 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002516 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002517 if (node_info->child[i] != (NodeInfo *) NULL)
2518 PruneChild(image,cube_info,node_info->child[i]);
2519 /*
2520 Merge color statistics into parent.
2521 */
2522 parent=node_info->parent;
2523 parent->number_unique+=node_info->number_unique;
2524 parent->total_color.red+=node_info->total_color.red;
2525 parent->total_color.green+=node_info->total_color.green;
2526 parent->total_color.blue+=node_info->total_color.blue;
cristy4c08aed2011-07-01 19:47:50 +00002527 parent->total_color.alpha+=node_info->total_color.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002528 parent->child[node_info->id]=(NodeInfo *) NULL;
2529 cube_info->nodes--;
2530}
2531
2532/*
2533%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2534% %
2535% %
2536% %
2537+ P r u n e L e v e l %
2538% %
2539% %
2540% %
2541%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2542%
2543% PruneLevel() deletes all nodes at the bottom level of the color tree merging
2544% their color statistics into their parent node.
2545%
2546% The format of the PruneLevel method is:
2547%
2548% PruneLevel(const Image *image,CubeInfo *cube_info,
2549% const NodeInfo *node_info)
2550%
2551% A description of each parameter follows.
2552%
2553% o image: the image.
2554%
2555% o cube_info: A pointer to the Cube structure.
2556%
2557% o node_info: pointer to node in color cube tree that is to be pruned.
2558%
2559*/
2560static void PruneLevel(const Image *image,CubeInfo *cube_info,
2561 const NodeInfo *node_info)
2562{
cristybb503372010-05-27 20:51:26 +00002563 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002564 i;
2565
cristybb503372010-05-27 20:51:26 +00002566 size_t
cristy3ed852e2009-09-05 21:47:34 +00002567 number_children;
2568
2569 /*
2570 Traverse any children.
2571 */
2572 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002573 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002574 if (node_info->child[i] != (NodeInfo *) NULL)
2575 PruneLevel(image,cube_info,node_info->child[i]);
2576 if (node_info->level == cube_info->depth)
2577 PruneChild(image,cube_info,node_info);
2578}
2579
2580/*
2581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2582% %
2583% %
2584% %
2585+ P r u n e T o C u b e D e p t h %
2586% %
2587% %
2588% %
2589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2590%
2591% PruneToCubeDepth() deletes any nodes at a depth greater than
2592% cube_info->depth while merging their color statistics into their parent
2593% node.
2594%
2595% The format of the PruneToCubeDepth method is:
2596%
2597% PruneToCubeDepth(const Image *image,CubeInfo *cube_info,
2598% const NodeInfo *node_info)
2599%
2600% A description of each parameter follows.
2601%
2602% o cube_info: A pointer to the Cube structure.
2603%
2604% o node_info: pointer to node in color cube tree that is to be pruned.
2605%
2606*/
2607static void PruneToCubeDepth(const Image *image,CubeInfo *cube_info,
2608 const NodeInfo *node_info)
2609{
cristybb503372010-05-27 20:51:26 +00002610 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002611 i;
2612
cristybb503372010-05-27 20:51:26 +00002613 size_t
cristy3ed852e2009-09-05 21:47:34 +00002614 number_children;
2615
2616 /*
2617 Traverse any children.
2618 */
2619 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002620 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002621 if (node_info->child[i] != (NodeInfo *) NULL)
2622 PruneToCubeDepth(image,cube_info,node_info->child[i]);
2623 if (node_info->level > cube_info->depth)
2624 PruneChild(image,cube_info,node_info);
2625}
2626
2627/*
2628%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2629% %
2630% %
2631% %
2632% Q u a n t i z e I m a g e %
2633% %
2634% %
2635% %
2636%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2637%
2638% QuantizeImage() analyzes the colors within a reference image and chooses a
2639% fixed number of colors to represent the image. The goal of the algorithm
2640% is to minimize the color difference between the input and output image while
2641% minimizing the processing time.
2642%
2643% The format of the QuantizeImage method is:
2644%
2645% MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002646% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002647%
2648% A description of each parameter follows:
2649%
2650% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
2651%
2652% o image: the image.
2653%
cristy018f07f2011-09-04 21:15:19 +00002654% o exception: return any errors or warnings in this structure.
2655%
cristy3ed852e2009-09-05 21:47:34 +00002656*/
cristy5f7dca62011-08-12 12:38:05 +00002657
2658static MagickBooleanType DirectToColormapImage(Image *image,
2659 ExceptionInfo *exception)
2660{
2661 CacheView
2662 *image_view;
2663
2664 MagickBooleanType
2665 status;
2666
2667 register ssize_t
2668 i;
2669
2670 size_t
2671 number_colors;
2672
2673 ssize_t
2674 y;
2675
2676 status=MagickTrue;
2677 number_colors=(size_t) (image->columns*image->rows);
cristy018f07f2011-09-04 21:15:19 +00002678 if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
cristy5f7dca62011-08-12 12:38:05 +00002679 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2680 image->filename);
2681 if (image->colors != number_colors)
2682 return(MagickFalse);
2683 i=0;
cristydb070952012-04-20 14:33:00 +00002684 image_view=AcquireAuthenticCacheView(image,exception);
cristy5f7dca62011-08-12 12:38:05 +00002685 for (y=0; y < (ssize_t) image->rows; y++)
2686 {
2687 MagickBooleanType
2688 proceed;
2689
2690 register Quantum
2691 *restrict q;
2692
2693 register ssize_t
2694 x;
2695
2696 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2697 if (q == (Quantum *) NULL)
2698 break;
2699 for (x=0; x < (ssize_t) image->columns; x++)
2700 {
cristye42f6582012-02-11 17:59:50 +00002701 image->colormap[i].red=(double) GetPixelRed(image,q);
2702 image->colormap[i].green=(double) GetPixelGreen(image,q);
2703 image->colormap[i].blue=(double) GetPixelBlue(image,q);
2704 image->colormap[i].alpha=(double) GetPixelAlpha(image,q);
cristy5f7dca62011-08-12 12:38:05 +00002705 SetPixelIndex(image,(Quantum) i,q);
2706 i++;
2707 q+=GetPixelChannels(image);
2708 }
2709 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2710 break;
2711 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y,
2712 image->rows);
2713 if (proceed == MagickFalse)
2714 status=MagickFalse;
2715 }
2716 image_view=DestroyCacheView(image_view);
2717 return(status);
2718}
2719
cristy3ed852e2009-09-05 21:47:34 +00002720MagickExport MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002721 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002722{
2723 CubeInfo
2724 *cube_info;
2725
2726 MagickBooleanType
2727 status;
2728
cristybb503372010-05-27 20:51:26 +00002729 size_t
cristy3ed852e2009-09-05 21:47:34 +00002730 depth,
2731 maximum_colors;
2732
2733 assert(quantize_info != (const QuantizeInfo *) NULL);
2734 assert(quantize_info->signature == MagickSignature);
2735 assert(image != (Image *) NULL);
2736 assert(image->signature == MagickSignature);
2737 if (image->debug != MagickFalse)
2738 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2739 maximum_colors=quantize_info->number_colors;
2740 if (maximum_colors == 0)
2741 maximum_colors=MaxColormapSize;
2742 if (maximum_colors > MaxColormapSize)
2743 maximum_colors=MaxColormapSize;
cristy5f7dca62011-08-12 12:38:05 +00002744 if ((image->columns*image->rows) <= maximum_colors)
cristy8a11cb12011-10-19 23:53:34 +00002745 (void) DirectToColormapImage(image,exception);
2746 if ((IsImageGray(image,exception) != MagickFalse) &&
cristy8e752752011-04-16 13:48:22 +00002747 (image->matte == MagickFalse))
cristy018f07f2011-09-04 21:15:19 +00002748 (void) SetGrayscaleImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002749 if ((image->storage_class == PseudoClass) &&
2750 (image->colors <= maximum_colors))
2751 return(MagickTrue);
2752 depth=quantize_info->tree_depth;
2753 if (depth == 0)
2754 {
cristybb503372010-05-27 20:51:26 +00002755 size_t
cristy3ed852e2009-09-05 21:47:34 +00002756 colors;
2757
2758 /*
2759 Depth of color tree is: Log4(colormap size)+2.
2760 */
2761 colors=maximum_colors;
2762 for (depth=1; colors != 0; depth++)
2763 colors>>=2;
2764 if ((quantize_info->dither != MagickFalse) && (depth > 2))
2765 depth--;
2766 if ((image->matte != MagickFalse) && (depth > 5))
2767 depth--;
2768 }
2769 /*
2770 Initialize color cube.
2771 */
2772 cube_info=GetCubeInfo(quantize_info,depth,maximum_colors);
2773 if (cube_info == (CubeInfo *) NULL)
2774 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2775 image->filename);
cristy8a11cb12011-10-19 23:53:34 +00002776 status=ClassifyImageColors(cube_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002777 if (status != MagickFalse)
2778 {
2779 /*
2780 Reduce the number of colors in the image.
2781 */
2782 ReduceImageColors(image,cube_info);
cristy018f07f2011-09-04 21:15:19 +00002783 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00002784 }
2785 DestroyCubeInfo(cube_info);
2786 return(status);
2787}
2788
2789/*
2790%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2791% %
2792% %
2793% %
2794% Q u a n t i z e I m a g e s %
2795% %
2796% %
2797% %
2798%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2799%
2800% QuantizeImages() analyzes the colors within a set of reference images and
2801% chooses a fixed number of colors to represent the set. The goal of the
2802% algorithm is to minimize the color difference between the input and output
2803% images while minimizing the processing time.
2804%
2805% The format of the QuantizeImages method is:
2806%
2807% MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002808% Image *images,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002809%
2810% A description of each parameter follows:
2811%
2812% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
2813%
2814% o images: Specifies a pointer to a list of Image structures.
2815%
cristy018f07f2011-09-04 21:15:19 +00002816% o exception: return any errors or warnings in this structure.
2817%
cristy3ed852e2009-09-05 21:47:34 +00002818*/
2819MagickExport MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002820 Image *images,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002821{
2822 CubeInfo
2823 *cube_info;
2824
2825 Image
2826 *image;
2827
2828 MagickBooleanType
2829 proceed,
2830 status;
2831
2832 MagickProgressMonitor
2833 progress_monitor;
2834
cristybb503372010-05-27 20:51:26 +00002835 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002836 i;
2837
cristybb503372010-05-27 20:51:26 +00002838 size_t
cristy3ed852e2009-09-05 21:47:34 +00002839 depth,
2840 maximum_colors,
2841 number_images;
2842
2843 assert(quantize_info != (const QuantizeInfo *) NULL);
2844 assert(quantize_info->signature == MagickSignature);
2845 assert(images != (Image *) NULL);
2846 assert(images->signature == MagickSignature);
2847 if (images->debug != MagickFalse)
2848 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
2849 if (GetNextImageInList(images) == (Image *) NULL)
2850 {
2851 /*
2852 Handle a single image with QuantizeImage.
2853 */
cristy018f07f2011-09-04 21:15:19 +00002854 status=QuantizeImage(quantize_info,images,exception);
cristy3ed852e2009-09-05 21:47:34 +00002855 return(status);
2856 }
2857 status=MagickFalse;
2858 maximum_colors=quantize_info->number_colors;
2859 if (maximum_colors == 0)
2860 maximum_colors=MaxColormapSize;
2861 if (maximum_colors > MaxColormapSize)
2862 maximum_colors=MaxColormapSize;
2863 depth=quantize_info->tree_depth;
2864 if (depth == 0)
2865 {
cristybb503372010-05-27 20:51:26 +00002866 size_t
cristy3ed852e2009-09-05 21:47:34 +00002867 colors;
2868
2869 /*
2870 Depth of color tree is: Log4(colormap size)+2.
2871 */
2872 colors=maximum_colors;
2873 for (depth=1; colors != 0; depth++)
2874 colors>>=2;
2875 if (quantize_info->dither != MagickFalse)
2876 depth--;
2877 }
2878 /*
2879 Initialize color cube.
2880 */
2881 cube_info=GetCubeInfo(quantize_info,depth,maximum_colors);
2882 if (cube_info == (CubeInfo *) NULL)
2883 {
cristy8a11cb12011-10-19 23:53:34 +00002884 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002885 ResourceLimitError,"MemoryAllocationFailed","'%s'",images->filename);
cristy3ed852e2009-09-05 21:47:34 +00002886 return(MagickFalse);
2887 }
2888 number_images=GetImageListLength(images);
2889 image=images;
2890 for (i=0; image != (Image *) NULL; i++)
2891 {
2892 progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor) NULL,
2893 image->client_data);
cristy8a11cb12011-10-19 23:53:34 +00002894 status=ClassifyImageColors(cube_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002895 if (status == MagickFalse)
2896 break;
2897 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
cristycee97112010-05-28 00:44:52 +00002898 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i,
2899 number_images);
cristy3ed852e2009-09-05 21:47:34 +00002900 if (proceed == MagickFalse)
2901 break;
2902 image=GetNextImageInList(image);
2903 }
2904 if (status != MagickFalse)
2905 {
2906 /*
2907 Reduce the number of colors in an image sequence.
2908 */
2909 ReduceImageColors(images,cube_info);
2910 image=images;
2911 for (i=0; image != (Image *) NULL; i++)
2912 {
2913 progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor)
2914 NULL,image->client_data);
cristy018f07f2011-09-04 21:15:19 +00002915 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00002916 if (status == MagickFalse)
2917 break;
2918 (void) SetImageProgressMonitor(image,progress_monitor,
2919 image->client_data);
cristycee97112010-05-28 00:44:52 +00002920 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i,
2921 number_images);
cristy3ed852e2009-09-05 21:47:34 +00002922 if (proceed == MagickFalse)
2923 break;
2924 image=GetNextImageInList(image);
2925 }
2926 }
2927 DestroyCubeInfo(cube_info);
2928 return(status);
2929}
2930
2931/*
2932%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2933% %
2934% %
2935% %
2936+ R e d u c e %
2937% %
2938% %
2939% %
2940%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2941%
2942% Reduce() traverses the color cube tree and prunes any node whose
2943% quantization error falls below a particular threshold.
2944%
2945% The format of the Reduce method is:
2946%
2947% Reduce(const Image *image,CubeInfo *cube_info,const NodeInfo *node_info)
2948%
2949% A description of each parameter follows.
2950%
2951% o image: the image.
2952%
2953% o cube_info: A pointer to the Cube structure.
2954%
2955% o node_info: pointer to node in color cube tree that is to be pruned.
2956%
2957*/
2958static void Reduce(const Image *image,CubeInfo *cube_info,
2959 const NodeInfo *node_info)
2960{
cristybb503372010-05-27 20:51:26 +00002961 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002962 i;
2963
cristybb503372010-05-27 20:51:26 +00002964 size_t
cristy3ed852e2009-09-05 21:47:34 +00002965 number_children;
2966
2967 /*
2968 Traverse any children.
2969 */
2970 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002971 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002972 if (node_info->child[i] != (NodeInfo *) NULL)
2973 Reduce(image,cube_info,node_info->child[i]);
2974 if (node_info->quantize_error <= cube_info->pruning_threshold)
2975 PruneChild(image,cube_info,node_info);
2976 else
2977 {
2978 /*
2979 Find minimum pruning threshold.
2980 */
2981 if (node_info->number_unique > 0)
2982 cube_info->colors++;
2983 if (node_info->quantize_error < cube_info->next_threshold)
2984 cube_info->next_threshold=node_info->quantize_error;
2985 }
2986}
2987
2988/*
2989%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2990% %
2991% %
2992% %
2993+ R e d u c e I m a g e C o l o r s %
2994% %
2995% %
2996% %
2997%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2998%
2999% ReduceImageColors() repeatedly prunes the tree until the number of nodes
3000% with n2 > 0 is less than or equal to the maximum number of colors allowed
3001% in the output image. On any given iteration over the tree, it selects
3002% those nodes whose E value is minimal for pruning and merges their
3003% color statistics upward. It uses a pruning threshold, Ep, to govern
3004% node selection as follows:
3005%
3006% Ep = 0
3007% while number of nodes with (n2 > 0) > required maximum number of colors
3008% prune all nodes such that E <= Ep
3009% Set Ep to minimum E in remaining nodes
3010%
3011% This has the effect of minimizing any quantization error when merging
3012% two nodes together.
3013%
3014% When a node to be pruned has offspring, the pruning procedure invokes
3015% itself recursively in order to prune the tree from the leaves upward.
3016% n2, Sr, Sg, and Sb in a node being pruned are always added to the
3017% corresponding data in that node's parent. This retains the pruned
3018% node's color characteristics for later averaging.
3019%
3020% For each node, n2 pixels exist for which that node represents the
3021% smallest volume in RGB space containing those pixel's colors. When n2
3022% > 0 the node will uniquely define a color in the output image. At the
3023% beginning of reduction, n2 = 0 for all nodes except a the leaves of
3024% the tree which represent colors present in the input image.
3025%
3026% The other pixel count, n1, indicates the total number of colors
3027% within the cubic volume which the node represents. This includes n1 -
3028% n2 pixels whose colors should be defined by nodes at a lower level in
3029% the tree.
3030%
3031% The format of the ReduceImageColors method is:
3032%
3033% ReduceImageColors(const Image *image,CubeInfo *cube_info)
3034%
3035% A description of each parameter follows.
3036%
3037% o image: the image.
3038%
3039% o cube_info: A pointer to the Cube structure.
3040%
3041*/
3042static void ReduceImageColors(const Image *image,CubeInfo *cube_info)
3043{
3044#define ReduceImageTag "Reduce/Image"
3045
3046 MagickBooleanType
3047 proceed;
3048
3049 MagickOffsetType
3050 offset;
3051
cristybb503372010-05-27 20:51:26 +00003052 size_t
cristy3ed852e2009-09-05 21:47:34 +00003053 span;
3054
3055 cube_info->next_threshold=0.0;
3056 for (span=cube_info->colors; cube_info->colors > cube_info->maximum_colors; )
3057 {
3058 cube_info->pruning_threshold=cube_info->next_threshold;
3059 cube_info->next_threshold=cube_info->root->quantize_error-1;
3060 cube_info->colors=0;
3061 Reduce(image,cube_info,cube_info->root);
3062 offset=(MagickOffsetType) span-cube_info->colors;
3063 proceed=SetImageProgress(image,ReduceImageTag,offset,span-
3064 cube_info->maximum_colors+1);
3065 if (proceed == MagickFalse)
3066 break;
3067 }
3068}
3069
3070/*
3071%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3072% %
3073% %
3074% %
3075% R e m a p I m a g e %
3076% %
3077% %
3078% %
3079%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3080%
anthony31f1bf72012-01-30 12:37:22 +00003081% RemapImage() replaces the colors of an image with a dither of the colors
3082% provided.
cristy3ed852e2009-09-05 21:47:34 +00003083%
3084% The format of the RemapImage method is:
3085%
3086% MagickBooleanType RemapImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003087% Image *image,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003088%
3089% A description of each parameter follows:
3090%
3091% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
3092%
3093% o image: the image.
3094%
3095% o remap_image: the reference image.
3096%
cristy018f07f2011-09-04 21:15:19 +00003097% o exception: return any errors or warnings in this structure.
3098%
cristy3ed852e2009-09-05 21:47:34 +00003099*/
3100MagickExport MagickBooleanType RemapImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003101 Image *image,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003102{
3103 CubeInfo
3104 *cube_info;
3105
3106 MagickBooleanType
3107 status;
3108
3109 /*
3110 Initialize color cube.
3111 */
3112 assert(image != (Image *) NULL);
3113 assert(image->signature == MagickSignature);
3114 if (image->debug != MagickFalse)
3115 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3116 assert(remap_image != (Image *) NULL);
3117 assert(remap_image->signature == MagickSignature);
3118 cube_info=GetCubeInfo(quantize_info,MaxTreeDepth,
3119 quantize_info->number_colors);
3120 if (cube_info == (CubeInfo *) NULL)
3121 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3122 image->filename);
cristy8a11cb12011-10-19 23:53:34 +00003123 status=ClassifyImageColors(cube_info,remap_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003124 if (status != MagickFalse)
3125 {
3126 /*
3127 Classify image colors from the reference image.
3128 */
3129 cube_info->quantize_info->number_colors=cube_info->colors;
cristy018f07f2011-09-04 21:15:19 +00003130 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003131 }
3132 DestroyCubeInfo(cube_info);
3133 return(status);
3134}
3135
3136/*
3137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3138% %
3139% %
3140% %
3141% R e m a p I m a g e s %
3142% %
3143% %
3144% %
3145%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3146%
3147% RemapImages() replaces the colors of a sequence of images with the
3148% closest color from a reference image.
3149%
3150% The format of the RemapImage method is:
3151%
3152% MagickBooleanType RemapImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003153% Image *images,Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003154%
3155% A description of each parameter follows:
3156%
3157% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
3158%
3159% o images: the image sequence.
3160%
3161% o remap_image: the reference image.
3162%
cristy018f07f2011-09-04 21:15:19 +00003163% o exception: return any errors or warnings in this structure.
3164%
cristy3ed852e2009-09-05 21:47:34 +00003165*/
3166MagickExport MagickBooleanType RemapImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003167 Image *images,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003168{
3169 CubeInfo
3170 *cube_info;
3171
3172 Image
3173 *image;
3174
3175 MagickBooleanType
3176 status;
3177
3178 assert(images != (Image *) NULL);
3179 assert(images->signature == MagickSignature);
3180 if (images->debug != MagickFalse)
3181 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
3182 image=images;
3183 if (remap_image == (Image *) NULL)
3184 {
3185 /*
3186 Create a global colormap for an image sequence.
3187 */
cristy018f07f2011-09-04 21:15:19 +00003188 status=QuantizeImages(quantize_info,images,exception);
cristy3ed852e2009-09-05 21:47:34 +00003189 return(status);
3190 }
3191 /*
3192 Classify image colors from the reference image.
3193 */
3194 cube_info=GetCubeInfo(quantize_info,MaxTreeDepth,
3195 quantize_info->number_colors);
3196 if (cube_info == (CubeInfo *) NULL)
3197 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3198 image->filename);
cristy018f07f2011-09-04 21:15:19 +00003199 status=ClassifyImageColors(cube_info,remap_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003200 if (status != MagickFalse)
3201 {
3202 /*
3203 Classify image colors from the reference image.
3204 */
3205 cube_info->quantize_info->number_colors=cube_info->colors;
3206 image=images;
3207 for ( ; image != (Image *) NULL; image=GetNextImageInList(image))
3208 {
cristy018f07f2011-09-04 21:15:19 +00003209 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003210 if (status == MagickFalse)
3211 break;
3212 }
3213 }
3214 DestroyCubeInfo(cube_info);
3215 return(status);
3216}
3217
3218/*
3219%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3220% %
3221% %
3222% %
3223% S e t G r a y s c a l e I m a g e %
3224% %
3225% %
3226% %
3227%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3228%
3229% SetGrayscaleImage() converts an image to a PseudoClass grayscale image.
3230%
3231% The format of the SetGrayscaleImage method is:
3232%
cristy018f07f2011-09-04 21:15:19 +00003233% MagickBooleanType SetGrayscaleImage(Image *image,ExceptionInfo *exeption)
cristy3ed852e2009-09-05 21:47:34 +00003234%
3235% A description of each parameter follows:
3236%
3237% o image: The image.
3238%
cristy018f07f2011-09-04 21:15:19 +00003239% o exception: return any errors or warnings in this structure.
3240%
cristy3ed852e2009-09-05 21:47:34 +00003241*/
3242
3243#if defined(__cplusplus) || defined(c_plusplus)
3244extern "C" {
3245#endif
3246
3247static int IntensityCompare(const void *x,const void *y)
3248{
cristy101ab702011-10-13 13:06:32 +00003249 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003250 *color_1,
3251 *color_2;
3252
cristyecc31b12011-02-13 00:32:29 +00003253 ssize_t
3254 intensity;
3255
cristy101ab702011-10-13 13:06:32 +00003256 color_1=(PixelInfo *) x;
3257 color_2=(PixelInfo *) y;
3258 intensity=GetPixelInfoIntensity(color_1)-(ssize_t)
3259 GetPixelInfoIntensity(color_2);
cristycee97112010-05-28 00:44:52 +00003260 return((int) intensity);
cristy3ed852e2009-09-05 21:47:34 +00003261}
3262
3263#if defined(__cplusplus) || defined(c_plusplus)
3264}
3265#endif
3266
cristy018f07f2011-09-04 21:15:19 +00003267static MagickBooleanType SetGrayscaleImage(Image *image,
3268 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003269{
cristyc4c8d132010-01-07 01:58:38 +00003270 CacheView
3271 *image_view;
3272
cristyecc31b12011-02-13 00:32:29 +00003273 MagickBooleanType
3274 status;
cristy3ed852e2009-09-05 21:47:34 +00003275
cristy101ab702011-10-13 13:06:32 +00003276 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003277 *colormap;
3278
cristybb503372010-05-27 20:51:26 +00003279 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003280 i;
3281
cristyecc31b12011-02-13 00:32:29 +00003282 ssize_t
3283 *colormap_index,
3284 j,
3285 y;
cristy3ed852e2009-09-05 21:47:34 +00003286
cristy3ed852e2009-09-05 21:47:34 +00003287 assert(image != (Image *) NULL);
3288 assert(image->signature == MagickSignature);
3289 if (image->type != GrayscaleType)
cristye941a752011-10-15 01:52:48 +00003290 (void) TransformImageColorspace(image,GRAYColorspace,exception);
cristybb503372010-05-27 20:51:26 +00003291 colormap_index=(ssize_t *) AcquireQuantumMemory(MaxMap+1,
cristy3ed852e2009-09-05 21:47:34 +00003292 sizeof(*colormap_index));
cristybb503372010-05-27 20:51:26 +00003293 if (colormap_index == (ssize_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003294 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3295 image->filename);
3296 if (image->storage_class != PseudoClass)
3297 {
cristybb503372010-05-27 20:51:26 +00003298 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy3ed852e2009-09-05 21:47:34 +00003299 colormap_index[i]=(-1);
cristy018f07f2011-09-04 21:15:19 +00003300 if (AcquireImageColormap(image,MaxMap+1,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003301 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3302 image->filename);
3303 image->colors=0;
3304 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00003305 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003306#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003307 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003308 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003309#endif
cristybb503372010-05-27 20:51:26 +00003310 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003311 {
cristy4c08aed2011-07-01 19:47:50 +00003312 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003313 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003314
cristyecc31b12011-02-13 00:32:29 +00003315 register ssize_t
3316 x;
3317
cristy3ed852e2009-09-05 21:47:34 +00003318 if (status == MagickFalse)
3319 continue;
3320 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3321 exception);
cristyacd2ed22011-08-30 01:44:23 +00003322 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003323 {
3324 status=MagickFalse;
3325 continue;
3326 }
cristybb503372010-05-27 20:51:26 +00003327 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003328 {
cristybb503372010-05-27 20:51:26 +00003329 register size_t
cristy3ed852e2009-09-05 21:47:34 +00003330 intensity;
3331
cristy4c08aed2011-07-01 19:47:50 +00003332 intensity=ScaleQuantumToMap(GetPixelRed(image,q));
cristy3ed852e2009-09-05 21:47:34 +00003333 if (colormap_index[intensity] < 0)
3334 {
cristyb5d5f722009-11-04 03:03:49 +00003335#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003336 #pragma omp critical (MagickCore_SetGrayscaleImage)
cristy3ed852e2009-09-05 21:47:34 +00003337#endif
3338 if (colormap_index[intensity] < 0)
3339 {
cristybb503372010-05-27 20:51:26 +00003340 colormap_index[intensity]=(ssize_t) image->colors;
cristye42f6582012-02-11 17:59:50 +00003341 image->colormap[image->colors].red=(double)
3342 GetPixelRed(image,q);
3343 image->colormap[image->colors].green=(double)
3344 GetPixelGreen(image,q);
3345 image->colormap[image->colors].blue=(double)
3346 GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00003347 image->colors++;
3348 }
3349 }
cristy4c08aed2011-07-01 19:47:50 +00003350 SetPixelIndex(image,(Quantum)
3351 colormap_index[intensity],q);
cristyed231572011-07-14 02:18:59 +00003352 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003353 }
3354 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3355 status=MagickFalse;
3356 }
3357 image_view=DestroyCacheView(image_view);
3358 }
cristybb503372010-05-27 20:51:26 +00003359 for (i=0; i < (ssize_t) image->colors; i++)
cristye42f6582012-02-11 17:59:50 +00003360 image->colormap[i].alpha=(double) i;
cristy101ab702011-10-13 13:06:32 +00003361 qsort((void *) image->colormap,image->colors,sizeof(PixelInfo),
cristy3ed852e2009-09-05 21:47:34 +00003362 IntensityCompare);
cristy101ab702011-10-13 13:06:32 +00003363 colormap=(PixelInfo *) AcquireQuantumMemory(image->colors,
cristy3ed852e2009-09-05 21:47:34 +00003364 sizeof(*colormap));
cristy101ab702011-10-13 13:06:32 +00003365 if (colormap == (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003366 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3367 image->filename);
3368 j=0;
3369 colormap[j]=image->colormap[0];
cristybb503372010-05-27 20:51:26 +00003370 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003371 {
cristy101ab702011-10-13 13:06:32 +00003372 if (IsPixelInfoEquivalent(&colormap[j],&image->colormap[i]) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003373 {
3374 j++;
3375 colormap[j]=image->colormap[i];
3376 }
cristy4c08aed2011-07-01 19:47:50 +00003377 colormap_index[(ssize_t) image->colormap[i].alpha]=j;
cristy3ed852e2009-09-05 21:47:34 +00003378 }
cristybb503372010-05-27 20:51:26 +00003379 image->colors=(size_t) (j+1);
cristy101ab702011-10-13 13:06:32 +00003380 image->colormap=(PixelInfo *) RelinquishMagickMemory(image->colormap);
cristy3ed852e2009-09-05 21:47:34 +00003381 image->colormap=colormap;
3382 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00003383 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003384#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristyac245f82012-05-05 17:13:57 +00003385 #pragma omp parallel for schedule(static,4) shared(status) \
cristy4ee2b0c2012-05-15 00:30:35 +00003386 dynamic_number_threads(image,image->columns,image->rows,1)
cristy3ed852e2009-09-05 21:47:34 +00003387#endif
cristybb503372010-05-27 20:51:26 +00003388 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003389 {
cristy4c08aed2011-07-01 19:47:50 +00003390 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003391 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003392
cristyecc31b12011-02-13 00:32:29 +00003393 register ssize_t
3394 x;
3395
cristy3ed852e2009-09-05 21:47:34 +00003396 if (status == MagickFalse)
3397 continue;
3398 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003399 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003400 {
3401 status=MagickFalse;
3402 continue;
3403 }
cristybb503372010-05-27 20:51:26 +00003404 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00003405 {
3406 SetPixelIndex(image,(Quantum) colormap_index[ScaleQuantumToMap(
3407 GetPixelIndex(image,q))],q);
cristyed231572011-07-14 02:18:59 +00003408 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00003409 }
cristy3ed852e2009-09-05 21:47:34 +00003410 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3411 status=MagickFalse;
3412 }
3413 image_view=DestroyCacheView(image_view);
cristybb503372010-05-27 20:51:26 +00003414 colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index);
cristy3ed852e2009-09-05 21:47:34 +00003415 image->type=GrayscaleType;
cristy8a11cb12011-10-19 23:53:34 +00003416 if (IsImageMonochrome(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003417 image->type=BilevelType;
3418 return(status);
3419}