blob: b52dbec179fa4f44e839603f4dc3108661bb94ee [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"
200#include "MagickCore/string_.h"
201#include "MagickCore/thread-private.h"
cristy3ed852e2009-09-05 21:47:34 +0000202
203/*
204 Define declarations.
205*/
cristye1287512010-06-19 17:38:25 +0000206#if !defined(__APPLE__) && !defined(TARGET_OS_IPHONE)
cristy3ed852e2009-09-05 21:47:34 +0000207#define CacheShift 2
cristye1287512010-06-19 17:38:25 +0000208#else
209#define CacheShift 3
210#endif
cristy3ed852e2009-09-05 21:47:34 +0000211#define ErrorQueueLength 16
212#define MaxNodes 266817
213#define MaxTreeDepth 8
214#define NodesInAList 1920
215
216/*
217 Typdef declarations.
218*/
cristy101ab702011-10-13 13:06:32 +0000219typedef struct _RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000220{
221 MagickRealType
222 red,
223 green,
224 blue,
cristy4c08aed2011-07-01 19:47:50 +0000225 alpha;
cristy101ab702011-10-13 13:06:32 +0000226} RealPixelInfo;
cristy3ed852e2009-09-05 21:47:34 +0000227
228typedef struct _NodeInfo
229{
230 struct _NodeInfo
231 *parent,
232 *child[16];
233
234 MagickSizeType
235 number_unique;
236
cristy101ab702011-10-13 13:06:32 +0000237 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000238 total_color;
239
240 MagickRealType
241 quantize_error;
242
cristybb503372010-05-27 20:51:26 +0000243 size_t
cristy3ed852e2009-09-05 21:47:34 +0000244 color_number,
245 id,
246 level;
247} NodeInfo;
248
249typedef struct _Nodes
250{
251 NodeInfo
252 *nodes;
253
254 struct _Nodes
255 *next;
256} Nodes;
257
258typedef struct _CubeInfo
259{
260 NodeInfo
261 *root;
262
cristybb503372010-05-27 20:51:26 +0000263 size_t
cristy3ed852e2009-09-05 21:47:34 +0000264 colors,
265 maximum_colors;
266
cristybb503372010-05-27 20:51:26 +0000267 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000268 transparent_index;
269
270 MagickSizeType
271 transparent_pixels;
272
cristy101ab702011-10-13 13:06:32 +0000273 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000274 target;
275
276 MagickRealType
277 distance,
278 pruning_threshold,
279 next_threshold;
280
cristybb503372010-05-27 20:51:26 +0000281 size_t
cristy3ed852e2009-09-05 21:47:34 +0000282 nodes,
283 free_nodes,
284 color_number;
285
286 NodeInfo
287 *next_node;
288
289 Nodes
290 *node_queue;
291
cristybb503372010-05-27 20:51:26 +0000292 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000293 *cache;
294
cristy101ab702011-10-13 13:06:32 +0000295 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000296 error[ErrorQueueLength];
297
298 MagickRealType
299 weights[ErrorQueueLength];
300
301 QuantizeInfo
302 *quantize_info;
303
304 MagickBooleanType
305 associate_alpha;
306
cristybb503372010-05-27 20:51:26 +0000307 ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000308 x,
309 y;
310
cristybb503372010-05-27 20:51:26 +0000311 size_t
cristy3ed852e2009-09-05 21:47:34 +0000312 depth;
313
314 MagickOffsetType
315 offset;
316
317 MagickSizeType
318 span;
319} CubeInfo;
320
321/*
322 Method prototypes.
323*/
324static CubeInfo
cristybb503372010-05-27 20:51:26 +0000325 *GetCubeInfo(const QuantizeInfo *,const size_t,const size_t);
cristy3ed852e2009-09-05 21:47:34 +0000326
327static NodeInfo
cristybb503372010-05-27 20:51:26 +0000328 *GetNodeInfo(CubeInfo *,const size_t,const size_t,NodeInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000329
330static MagickBooleanType
cristy018f07f2011-09-04 21:15:19 +0000331 AssignImageColors(Image *,CubeInfo *,ExceptionInfo *),
cristy3ed852e2009-09-05 21:47:34 +0000332 ClassifyImageColors(CubeInfo *,const Image *,ExceptionInfo *),
cristy8a11cb12011-10-19 23:53:34 +0000333 DitherImage(Image *,CubeInfo *,ExceptionInfo *),
cristy018f07f2011-09-04 21:15:19 +0000334 SetGrayscaleImage(Image *,ExceptionInfo *);
cristy3ed852e2009-09-05 21:47:34 +0000335
cristybb503372010-05-27 20:51:26 +0000336static size_t
cristy3ed852e2009-09-05 21:47:34 +0000337 DefineImageColormap(Image *,CubeInfo *,NodeInfo *);
338
339static void
340 ClosestColor(const Image *,CubeInfo *,const NodeInfo *),
341 DestroyCubeInfo(CubeInfo *),
342 PruneLevel(const Image *,CubeInfo *,const NodeInfo *),
343 PruneToCubeDepth(const Image *,CubeInfo *,const NodeInfo *),
344 ReduceImageColors(const Image *,CubeInfo *);
345
346/*
347%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348% %
349% %
350% %
351% A c q u i r e Q u a n t i z e I n f o %
352% %
353% %
354% %
355%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
356%
357% AcquireQuantizeInfo() allocates the QuantizeInfo structure.
358%
359% The format of the AcquireQuantizeInfo method is:
360%
361% QuantizeInfo *AcquireQuantizeInfo(const ImageInfo *image_info)
362%
363% A description of each parameter follows:
364%
365% o image_info: the image info.
366%
367*/
368MagickExport QuantizeInfo *AcquireQuantizeInfo(const ImageInfo *image_info)
369{
370 QuantizeInfo
371 *quantize_info;
372
cristy73bd4a52010-10-05 11:24:23 +0000373 quantize_info=(QuantizeInfo *) AcquireMagickMemory(sizeof(*quantize_info));
cristy3ed852e2009-09-05 21:47:34 +0000374 if (quantize_info == (QuantizeInfo *) NULL)
375 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
376 GetQuantizeInfo(quantize_info);
377 if (image_info != (ImageInfo *) NULL)
378 {
379 const char
380 *option;
381
382 quantize_info->dither=image_info->dither;
383 option=GetImageOption(image_info,"dither");
384 if (option != (const char *) NULL)
cristy042ee782011-04-22 18:48:30 +0000385 quantize_info->dither_method=(DitherMethod) ParseCommandOption(
cristy3ed852e2009-09-05 21:47:34 +0000386 MagickDitherOptions,MagickFalse,option);
387 quantize_info->measure_error=image_info->verbose;
388 }
389 return(quantize_info);
390}
391
392/*
393%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394% %
395% %
396% %
397+ A s s i g n I m a g e C o l o r s %
398% %
399% %
400% %
401%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402%
403% AssignImageColors() generates the output image from the pruned tree. The
404% output image consists of two parts: (1) A color map, which is an array
405% of color descriptions (RGB triples) for each color present in the
406% output image; (2) A pixel array, which represents each pixel as an
407% index into the color map array.
408%
409% First, the assignment phase makes one pass over the pruned color
410% description tree to establish the image's color map. For each node
411% with n2 > 0, it divides Sr, Sg, and Sb by n2 . This produces the mean
412% color of all pixels that classify no lower than this node. Each of
413% these colors becomes an entry in the color map.
414%
415% Finally, the assignment phase reclassifies each pixel in the pruned
416% tree to identify the deepest node containing the pixel's color. The
417% pixel's value in the pixel array becomes the index of this node's mean
418% color in the color map.
419%
420% The format of the AssignImageColors() method is:
421%
422% MagickBooleanType AssignImageColors(Image *image,CubeInfo *cube_info)
423%
424% A description of each parameter follows.
425%
426% o image: the image.
427%
428% o cube_info: A pointer to the Cube structure.
429%
430*/
431
cristy4c08aed2011-07-01 19:47:50 +0000432static inline void AssociateAlphaPixel(const Image *image,
cristy101ab702011-10-13 13:06:32 +0000433 const CubeInfo *cube_info,const Quantum *pixel,RealPixelInfo *alpha_pixel)
cristy3ed852e2009-09-05 21:47:34 +0000434{
435 MagickRealType
436 alpha;
437
438 if ((cube_info->associate_alpha == MagickFalse) ||
cristy4c08aed2011-07-01 19:47:50 +0000439 (GetPixelAlpha(image,pixel)== OpaqueAlpha))
cristy3ed852e2009-09-05 21:47:34 +0000440 {
cristy4c08aed2011-07-01 19:47:50 +0000441 alpha_pixel->red=(MagickRealType) GetPixelRed(image,pixel);
442 alpha_pixel->green=(MagickRealType) GetPixelGreen(image,pixel);
443 alpha_pixel->blue=(MagickRealType) GetPixelBlue(image,pixel);
444 alpha_pixel->alpha=(MagickRealType) GetPixelAlpha(image,pixel);
cristy3ed852e2009-09-05 21:47:34 +0000445 return;
446 }
cristy4c08aed2011-07-01 19:47:50 +0000447 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,pixel));
448 alpha_pixel->red=alpha*GetPixelRed(image,pixel);
449 alpha_pixel->green=alpha*GetPixelGreen(image,pixel);
450 alpha_pixel->blue=alpha*GetPixelBlue(image,pixel);
451 alpha_pixel->alpha=(MagickRealType) GetPixelAlpha(image,pixel);
452}
453
cristy101ab702011-10-13 13:06:32 +0000454static inline void AssociateAlphaPixelInfo(const Image *image,
455 const CubeInfo *cube_info,const PixelInfo *pixel,
456 RealPixelInfo *alpha_pixel)
cristy4c08aed2011-07-01 19:47:50 +0000457{
458 MagickRealType
459 alpha;
460
461 if ((cube_info->associate_alpha == MagickFalse) ||
462 (pixel->alpha == OpaqueAlpha))
463 {
464 alpha_pixel->red=(MagickRealType) pixel->red;
465 alpha_pixel->green=(MagickRealType) pixel->green;
466 alpha_pixel->blue=(MagickRealType) pixel->blue;
467 alpha_pixel->alpha=(MagickRealType) pixel->alpha;
468 return;
469 }
470 alpha=(MagickRealType) (QuantumScale*pixel->alpha);
471 alpha_pixel->red=alpha*pixel->red;
472 alpha_pixel->green=alpha*pixel->green;
473 alpha_pixel->blue=alpha*pixel->blue;
474 alpha_pixel->alpha=(MagickRealType) pixel->alpha;
cristy3ed852e2009-09-05 21:47:34 +0000475}
476
cristy75ffdb72010-01-07 17:40:12 +0000477static inline Quantum ClampToUnsignedQuantum(const MagickRealType value)
cristy3ed852e2009-09-05 21:47:34 +0000478{
479 if (value <= 0.0)
480 return((Quantum) 0);
481 if (value >= QuantumRange)
482 return((Quantum) QuantumRange);
483 return((Quantum) (value+0.5));
484}
485
cristybb503372010-05-27 20:51:26 +0000486static inline size_t ColorToNodeId(const CubeInfo *cube_info,
cristy101ab702011-10-13 13:06:32 +0000487 const RealPixelInfo *pixel,size_t index)
cristy3ed852e2009-09-05 21:47:34 +0000488{
cristybb503372010-05-27 20:51:26 +0000489 size_t
cristy3ed852e2009-09-05 21:47:34 +0000490 id;
491
cristy4c08aed2011-07-01 19:47:50 +0000492 id=(size_t) (((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->red)) >> index) & 0x01) |
493 ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->green)) >> index) & 0x01) << 1 |
494 ((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->blue)) >> index) & 0x01) << 2);
cristy3ed852e2009-09-05 21:47:34 +0000495 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000496 id|=((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->alpha)) >> index) & 0x1) << 3;
cristy3ed852e2009-09-05 21:47:34 +0000497 return(id);
498}
499
cristy018f07f2011-09-04 21:15:19 +0000500static MagickBooleanType AssignImageColors(Image *image,CubeInfo *cube_info,
501 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000502{
503#define AssignImageTag "Assign/Image"
504
cristyecc31b12011-02-13 00:32:29 +0000505 ssize_t
cristyecc31b12011-02-13 00:32:29 +0000506 y;
507
cristy3ed852e2009-09-05 21:47:34 +0000508 /*
509 Allocate image colormap.
510 */
511 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
512 (cube_info->quantize_info->colorspace != CMYKColorspace))
513 (void) TransformImageColorspace((Image *) image,
cristye941a752011-10-15 01:52:48 +0000514 cube_info->quantize_info->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000515 else
516 if ((image->colorspace != GRAYColorspace) &&
cristy501c5592012-04-18 12:45:09 +0000517 (IssRGBColorspace(image->colorspace) == MagickFalse) &&
cristy3ed852e2009-09-05 21:47:34 +0000518 (image->colorspace != CMYColorspace))
cristyc511e882012-04-16 21:11:14 +0000519 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy018f07f2011-09-04 21:15:19 +0000520 if (AcquireImageColormap(image,cube_info->colors,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000521 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
522 image->filename);
523 image->colors=0;
524 cube_info->transparent_pixels=0;
525 cube_info->transparent_index=(-1);
526 (void) DefineImageColormap(image,cube_info,cube_info->root);
527 /*
528 Create a reduced color image.
529 */
530 if ((cube_info->quantize_info->dither != MagickFalse) &&
cristyd5acfd12010-06-15 00:11:38 +0000531 (cube_info->quantize_info->dither_method != NoDitherMethod))
cristy8a11cb12011-10-19 23:53:34 +0000532 (void) DitherImage(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +0000533 else
534 {
cristy3ed852e2009-09-05 21:47:34 +0000535 CacheView
536 *image_view;
537
cristye9717ac2011-02-20 16:17:17 +0000538 MagickBooleanType
539 status;
540
541 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +0000542 image_view=AcquireAuthenticCacheView(image,exception);
cristye9717ac2011-02-20 16:17:17 +0000543#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000544 #pragma omp parallel for schedule(static,4) shared(status)
cristye9717ac2011-02-20 16:17:17 +0000545#endif
cristybb503372010-05-27 20:51:26 +0000546 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000547 {
cristye9717ac2011-02-20 16:17:17 +0000548 CubeInfo
549 cube;
550
cristy4c08aed2011-07-01 19:47:50 +0000551 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000552 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000553
cristye9717ac2011-02-20 16:17:17 +0000554 register ssize_t
555 x;
556
557 ssize_t
558 count;
559
560 if (status == MagickFalse)
561 continue;
cristy3ed852e2009-09-05 21:47:34 +0000562 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
563 exception);
cristyacd2ed22011-08-30 01:44:23 +0000564 if (q == (Quantum *) NULL)
cristye9717ac2011-02-20 16:17:17 +0000565 {
566 status=MagickFalse;
567 continue;
568 }
cristye9717ac2011-02-20 16:17:17 +0000569 cube=(*cube_info);
cristybb503372010-05-27 20:51:26 +0000570 for (x=0; x < (ssize_t) image->columns; x+=count)
cristy3ed852e2009-09-05 21:47:34 +0000571 {
cristy101ab702011-10-13 13:06:32 +0000572 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +0000573 pixel;
574
575 register const NodeInfo
576 *node_info;
577
578 register ssize_t
579 i;
580
581 size_t
582 id,
583 index;
584
cristy3ed852e2009-09-05 21:47:34 +0000585 /*
586 Identify the deepest node containing the pixel's color.
587 */
cristybb503372010-05-27 20:51:26 +0000588 for (count=1; (x+count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000589 {
cristy101ab702011-10-13 13:06:32 +0000590 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000591 packet;
592
cristy101ab702011-10-13 13:06:32 +0000593 GetPixelInfoPixel(image,q+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000594 if (IsPixelEquivalent(image,q,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000595 break;
cristy4c08aed2011-07-01 19:47:50 +0000596 }
597 AssociateAlphaPixel(image,&cube,q,&pixel);
cristye9717ac2011-02-20 16:17:17 +0000598 node_info=cube.root;
cristybb503372010-05-27 20:51:26 +0000599 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +0000600 {
cristye9717ac2011-02-20 16:17:17 +0000601 id=ColorToNodeId(&cube,&pixel,index);
cristy3ed852e2009-09-05 21:47:34 +0000602 if (node_info->child[id] == (NodeInfo *) NULL)
603 break;
604 node_info=node_info->child[id];
605 }
606 /*
607 Find closest color among siblings and their children.
608 */
cristye9717ac2011-02-20 16:17:17 +0000609 cube.target=pixel;
610 cube.distance=(MagickRealType) (4.0*(QuantumRange+1.0)*
cristy3ed852e2009-09-05 21:47:34 +0000611 (QuantumRange+1.0)+1.0);
cristye9717ac2011-02-20 16:17:17 +0000612 ClosestColor(image,&cube,node_info->parent);
613 index=cube.color_number;
cristybb503372010-05-27 20:51:26 +0000614 for (i=0; i < (ssize_t) count; i++)
cristy3ed852e2009-09-05 21:47:34 +0000615 {
616 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000617 SetPixelIndex(image,(Quantum) index,q);
cristye9717ac2011-02-20 16:17:17 +0000618 if (cube.quantize_info->measure_error == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000619 {
cristye42f6582012-02-11 17:59:50 +0000620 SetPixelRed(image,ClampToQuantum(
621 image->colormap[index].red),q);
622 SetPixelGreen(image,ClampToQuantum(
623 image->colormap[index].green),q);
624 SetPixelBlue(image,ClampToQuantum(
625 image->colormap[index].blue),q);
cristye9717ac2011-02-20 16:17:17 +0000626 if (cube.associate_alpha != MagickFalse)
cristye42f6582012-02-11 17:59:50 +0000627 SetPixelAlpha(image,ClampToQuantum(
628 image->colormap[index].alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000629 }
cristyed231572011-07-14 02:18:59 +0000630 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000631 }
632 }
633 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristye9717ac2011-02-20 16:17:17 +0000634 status=MagickFalse;
635 if (image->progress_monitor != (MagickProgressMonitor) NULL)
636 {
637 MagickBooleanType
638 proceed;
639
640#if defined(MAGICKCORE_OPENMP_SUPPORT)
641 #pragma omp critical (MagickCore_AssignImageColors)
642#endif
643 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y,
644 image->rows);
645 if (proceed == MagickFalse)
646 status=MagickFalse;
647 }
cristy3ed852e2009-09-05 21:47:34 +0000648 }
649 image_view=DestroyCacheView(image_view);
650 }
651 if (cube_info->quantize_info->measure_error != MagickFalse)
cristy8a11cb12011-10-19 23:53:34 +0000652 (void) GetImageQuantizeError(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000653 if ((cube_info->quantize_info->number_colors == 2) &&
654 (cube_info->quantize_info->colorspace == GRAYColorspace))
655 {
cristye42f6582012-02-11 17:59:50 +0000656 double
cristy3ed852e2009-09-05 21:47:34 +0000657 intensity;
658
cristy101ab702011-10-13 13:06:32 +0000659 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +0000660 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000661
cristye9717ac2011-02-20 16:17:17 +0000662 register ssize_t
663 i;
664
cristy3ed852e2009-09-05 21:47:34 +0000665 /*
666 Monochrome image.
667 */
668 q=image->colormap;
cristybb503372010-05-27 20:51:26 +0000669 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000670 {
cristye42f6582012-02-11 17:59:50 +0000671 intensity=(double) ((MagickRealType) GetPixelInfoIntensity(q) <
cristy4c08aed2011-07-01 19:47:50 +0000672 ((MagickRealType) QuantumRange/2.0) ? 0 : QuantumRange);
673 q->red=intensity;
674 q->green=intensity;
675 q->blue=intensity;
cristy3ed852e2009-09-05 21:47:34 +0000676 q++;
677 }
678 }
cristyea1a8aa2011-10-20 13:24:06 +0000679 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000680 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
681 (cube_info->quantize_info->colorspace != CMYKColorspace))
cristyc511e882012-04-16 21:11:14 +0000682 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000683 return(MagickTrue);
684}
685
686/*
687%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
688% %
689% %
690% %
691+ C l a s s i f y I m a g e C o l o r s %
692% %
693% %
694% %
695%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
696%
697% ClassifyImageColors() begins by initializing a color description tree
698% of sufficient depth to represent each possible input color in a leaf.
699% However, it is impractical to generate a fully-formed color
700% description tree in the storage_class phase for realistic values of
701% Cmax. If colors components in the input image are quantized to k-bit
702% precision, so that Cmax= 2k-1, the tree would need k levels below the
703% root node to allow representing each possible input color in a leaf.
704% This becomes prohibitive because the tree's total number of nodes is
705% 1 + sum(i=1,k,8k).
706%
707% A complete tree would require 19,173,961 nodes for k = 8, Cmax = 255.
708% Therefore, to avoid building a fully populated tree, QUANTIZE: (1)
709% Initializes data structures for nodes only as they are needed; (2)
710% Chooses a maximum depth for the tree as a function of the desired
711% number of colors in the output image (currently log2(colormap size)).
712%
713% For each pixel in the input image, storage_class scans downward from
714% the root of the color description tree. At each level of the tree it
715% identifies the single node which represents a cube in RGB space
716% containing It updates the following data for each such node:
717%
718% n1 : Number of pixels whose color is contained in the RGB cube
719% which this node represents;
720%
721% n2 : Number of pixels whose color is not represented in a node at
722% lower depth in the tree; initially, n2 = 0 for all nodes except
723% leaves of the tree.
724%
725% Sr, Sg, Sb : Sums of the red, green, and blue component values for
726% all pixels not classified at a lower depth. The combination of
727% these sums and n2 will ultimately characterize the mean color of a
728% set of pixels represented by this node.
729%
730% E: the distance squared in RGB space between each pixel contained
731% within a node and the nodes' center. This represents the quantization
732% error for a node.
733%
734% The format of the ClassifyImageColors() method is:
735%
736% MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
737% const Image *image,ExceptionInfo *exception)
738%
739% A description of each parameter follows.
740%
741% o cube_info: A pointer to the Cube structure.
742%
743% o image: the image.
744%
745*/
746
747static inline void SetAssociatedAlpha(const Image *image,CubeInfo *cube_info)
748{
749 MagickBooleanType
750 associate_alpha;
751
752 associate_alpha=image->matte;
753 if (cube_info->quantize_info->colorspace == TransparentColorspace)
754 associate_alpha=MagickFalse;
755 if ((cube_info->quantize_info->number_colors == 2) &&
756 (cube_info->quantize_info->colorspace == GRAYColorspace))
757 associate_alpha=MagickFalse;
758 cube_info->associate_alpha=associate_alpha;
759}
760
761static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
762 const Image *image,ExceptionInfo *exception)
763{
764#define ClassifyImageTag "Classify/Image"
765
cristyc4c8d132010-01-07 01:58:38 +0000766 CacheView
767 *image_view;
768
cristy3ed852e2009-09-05 21:47:34 +0000769 MagickBooleanType
770 proceed;
771
772 MagickRealType
773 bisect;
774
775 NodeInfo
776 *node_info;
777
cristy101ab702011-10-13 13:06:32 +0000778 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000779 error,
780 mid,
781 midpoint,
782 pixel;
783
784 size_t
cristyecc31b12011-02-13 00:32:29 +0000785 count,
cristy3ed852e2009-09-05 21:47:34 +0000786 id,
787 index,
788 level;
789
cristyecc31b12011-02-13 00:32:29 +0000790 ssize_t
791 y;
792
cristy3ed852e2009-09-05 21:47:34 +0000793 /*
794 Classify the first cube_info->maximum_colors colors to a tree depth of 8.
795 */
796 SetAssociatedAlpha(image,cube_info);
797 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
798 (cube_info->quantize_info->colorspace != CMYKColorspace))
799 (void) TransformImageColorspace((Image *) image,
cristye941a752011-10-15 01:52:48 +0000800 cube_info->quantize_info->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000801 else
802 if ((image->colorspace != GRAYColorspace) &&
803 (image->colorspace != CMYColorspace) &&
cristy501c5592012-04-18 12:45:09 +0000804 (IssRGBColorspace(image->colorspace) == MagickFalse))
cristyc511e882012-04-16 21:11:14 +0000805 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000806 midpoint.red=(MagickRealType) QuantumRange/2.0;
807 midpoint.green=(MagickRealType) QuantumRange/2.0;
808 midpoint.blue=(MagickRealType) QuantumRange/2.0;
cristy4c08aed2011-07-01 19:47:50 +0000809 midpoint.alpha=(MagickRealType) QuantumRange/2.0;
810 error.alpha=0.0;
cristydb070952012-04-20 14:33:00 +0000811 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +0000812 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000813 {
cristy4c08aed2011-07-01 19:47:50 +0000814 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000815 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000816
cristybb503372010-05-27 20:51:26 +0000817 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000818 x;
819
820 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000821 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000822 break;
823 if (cube_info->nodes > MaxNodes)
824 {
825 /*
826 Prune one level if the color tree is too large.
827 */
828 PruneLevel(image,cube_info,cube_info->root);
829 cube_info->depth--;
830 }
cristybb503372010-05-27 20:51:26 +0000831 for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count)
cristy3ed852e2009-09-05 21:47:34 +0000832 {
833 /*
834 Start at the root and descend the color cube tree.
835 */
cristybb66d9c2010-10-09 01:40:31 +0000836 for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000837 {
cristy101ab702011-10-13 13:06:32 +0000838 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000839 packet;
840
cristy101ab702011-10-13 13:06:32 +0000841 GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000842 if (IsPixelEquivalent(image,p,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000843 break;
cristy4c08aed2011-07-01 19:47:50 +0000844 }
845 AssociateAlphaPixel(image,cube_info,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +0000846 index=MaxTreeDepth-1;
847 bisect=((MagickRealType) QuantumRange+1.0)/2.0;
848 mid=midpoint;
849 node_info=cube_info->root;
850 for (level=1; level <= MaxTreeDepth; level++)
851 {
852 bisect*=0.5;
853 id=ColorToNodeId(cube_info,&pixel,index);
854 mid.red+=(id & 1) != 0 ? bisect : -bisect;
855 mid.green+=(id & 2) != 0 ? bisect : -bisect;
856 mid.blue+=(id & 4) != 0 ? bisect : -bisect;
cristy4c08aed2011-07-01 19:47:50 +0000857 mid.alpha+=(id & 8) != 0 ? bisect : -bisect;
cristy3ed852e2009-09-05 21:47:34 +0000858 if (node_info->child[id] == (NodeInfo *) NULL)
859 {
860 /*
861 Set colors of new node to contain pixel.
862 */
863 node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info);
864 if (node_info->child[id] == (NodeInfo *) NULL)
865 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +0000866 ResourceLimitError,"MemoryAllocationFailed","'%s'",
cristy3ed852e2009-09-05 21:47:34 +0000867 image->filename);
868 if (level == MaxTreeDepth)
869 cube_info->colors++;
870 }
871 /*
872 Approximate the quantization error represented by this node.
873 */
874 node_info=node_info->child[id];
875 error.red=QuantumScale*(pixel.red-mid.red);
876 error.green=QuantumScale*(pixel.green-mid.green);
877 error.blue=QuantumScale*(pixel.blue-mid.blue);
878 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000879 error.alpha=QuantumScale*(pixel.alpha-mid.alpha);
cristy3ed852e2009-09-05 21:47:34 +0000880 node_info->quantize_error+=sqrt((double) (count*error.red*error.red+
881 count*error.green*error.green+count*error.blue*error.blue+
cristy4c08aed2011-07-01 19:47:50 +0000882 count*error.alpha*error.alpha));
cristy3ed852e2009-09-05 21:47:34 +0000883 cube_info->root->quantize_error+=node_info->quantize_error;
884 index--;
885 }
886 /*
887 Sum RGB for this leaf for later derivation of the mean cube color.
888 */
889 node_info->number_unique+=count;
890 node_info->total_color.red+=count*QuantumScale*pixel.red;
891 node_info->total_color.green+=count*QuantumScale*pixel.green;
892 node_info->total_color.blue+=count*QuantumScale*pixel.blue;
893 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000894 node_info->total_color.alpha+=count*QuantumScale*pixel.alpha;
cristyed231572011-07-14 02:18:59 +0000895 p+=count*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000896 }
897 if (cube_info->colors > cube_info->maximum_colors)
898 {
899 PruneToCubeDepth(image,cube_info,cube_info->root);
900 break;
901 }
cristycee97112010-05-28 00:44:52 +0000902 proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y,
903 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000904 if (proceed == MagickFalse)
905 break;
906 }
cristybb503372010-05-27 20:51:26 +0000907 for (y++; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000908 {
cristy4c08aed2011-07-01 19:47:50 +0000909 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000910 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000911
cristybb503372010-05-27 20:51:26 +0000912 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000913 x;
914
915 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000916 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000917 break;
918 if (cube_info->nodes > MaxNodes)
919 {
920 /*
921 Prune one level if the color tree is too large.
922 */
923 PruneLevel(image,cube_info,cube_info->root);
924 cube_info->depth--;
925 }
cristybb503372010-05-27 20:51:26 +0000926 for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count)
cristy3ed852e2009-09-05 21:47:34 +0000927 {
928 /*
929 Start at the root and descend the color cube tree.
930 */
cristybb66d9c2010-10-09 01:40:31 +0000931 for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000932 {
cristy101ab702011-10-13 13:06:32 +0000933 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000934 packet;
935
cristy101ab702011-10-13 13:06:32 +0000936 GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000937 if (IsPixelEquivalent(image,p,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000938 break;
cristy4c08aed2011-07-01 19:47:50 +0000939 }
940 AssociateAlphaPixel(image,cube_info,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +0000941 index=MaxTreeDepth-1;
942 bisect=((MagickRealType) QuantumRange+1.0)/2.0;
943 mid=midpoint;
944 node_info=cube_info->root;
945 for (level=1; level <= cube_info->depth; level++)
946 {
947 bisect*=0.5;
948 id=ColorToNodeId(cube_info,&pixel,index);
949 mid.red+=(id & 1) != 0 ? bisect : -bisect;
950 mid.green+=(id & 2) != 0 ? bisect : -bisect;
951 mid.blue+=(id & 4) != 0 ? bisect : -bisect;
cristy4c08aed2011-07-01 19:47:50 +0000952 mid.alpha+=(id & 8) != 0 ? bisect : -bisect;
cristy3ed852e2009-09-05 21:47:34 +0000953 if (node_info->child[id] == (NodeInfo *) NULL)
954 {
955 /*
956 Set colors of new node to contain pixel.
957 */
958 node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info);
959 if (node_info->child[id] == (NodeInfo *) NULL)
960 (void) ThrowMagickException(exception,GetMagickModule(),
961 ResourceLimitError,"MemoryAllocationFailed","%s",
962 image->filename);
963 if (level == cube_info->depth)
964 cube_info->colors++;
965 }
966 /*
967 Approximate the quantization error represented by this node.
968 */
969 node_info=node_info->child[id];
970 error.red=QuantumScale*(pixel.red-mid.red);
971 error.green=QuantumScale*(pixel.green-mid.green);
972 error.blue=QuantumScale*(pixel.blue-mid.blue);
973 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000974 error.alpha=QuantumScale*(pixel.alpha-mid.alpha);
cristy3ed852e2009-09-05 21:47:34 +0000975 node_info->quantize_error+=sqrt((double) (count*error.red*error.red+
cristy83b6e792011-01-26 15:46:06 +0000976 count*error.green*error.green+count*error.blue*error.blue+
cristy4c08aed2011-07-01 19:47:50 +0000977 count*error.alpha*error.alpha));
cristy3ed852e2009-09-05 21:47:34 +0000978 cube_info->root->quantize_error+=node_info->quantize_error;
979 index--;
980 }
981 /*
982 Sum RGB for this leaf for later derivation of the mean cube color.
983 */
984 node_info->number_unique+=count;
985 node_info->total_color.red+=count*QuantumScale*pixel.red;
986 node_info->total_color.green+=count*QuantumScale*pixel.green;
987 node_info->total_color.blue+=count*QuantumScale*pixel.blue;
988 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000989 node_info->total_color.alpha+=count*QuantumScale*pixel.alpha;
cristyed231572011-07-14 02:18:59 +0000990 p+=count*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000991 }
cristycee97112010-05-28 00:44:52 +0000992 proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y,
993 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000994 if (proceed == MagickFalse)
995 break;
996 }
997 image_view=DestroyCacheView(image_view);
998 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
999 (cube_info->quantize_info->colorspace != CMYKColorspace))
cristyc511e882012-04-16 21:11:14 +00001000 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001001 return(MagickTrue);
1002}
1003
1004/*
1005%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006% %
1007% %
1008% %
1009% C l o n e Q u a n t i z e I n f o %
1010% %
1011% %
1012% %
1013%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1014%
1015% CloneQuantizeInfo() makes a duplicate of the given quantize info structure,
1016% or if quantize info is NULL, a new one.
1017%
1018% The format of the CloneQuantizeInfo method is:
1019%
1020% QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info)
1021%
1022% A description of each parameter follows:
1023%
1024% o clone_info: Method CloneQuantizeInfo returns a duplicate of the given
1025% quantize info, or if image info is NULL a new one.
1026%
1027% o quantize_info: a structure of type info.
1028%
1029*/
1030MagickExport QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info)
1031{
1032 QuantizeInfo
1033 *clone_info;
1034
cristy73bd4a52010-10-05 11:24:23 +00001035 clone_info=(QuantizeInfo *) AcquireMagickMemory(sizeof(*clone_info));
cristy3ed852e2009-09-05 21:47:34 +00001036 if (clone_info == (QuantizeInfo *) NULL)
1037 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1038 GetQuantizeInfo(clone_info);
1039 if (quantize_info == (QuantizeInfo *) NULL)
1040 return(clone_info);
1041 clone_info->number_colors=quantize_info->number_colors;
1042 clone_info->tree_depth=quantize_info->tree_depth;
1043 clone_info->dither=quantize_info->dither;
1044 clone_info->dither_method=quantize_info->dither_method;
1045 clone_info->colorspace=quantize_info->colorspace;
1046 clone_info->measure_error=quantize_info->measure_error;
1047 return(clone_info);
1048}
1049
1050/*
1051%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1052% %
1053% %
1054% %
1055+ C l o s e s t C o l o r %
1056% %
1057% %
1058% %
1059%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1060%
1061% ClosestColor() traverses the color cube tree at a particular node and
1062% determines which colormap entry best represents the input color.
1063%
1064% The format of the ClosestColor method is:
1065%
1066% void ClosestColor(const Image *image,CubeInfo *cube_info,
1067% const NodeInfo *node_info)
1068%
1069% A description of each parameter follows.
1070%
1071% o image: the image.
1072%
1073% o cube_info: A pointer to the Cube structure.
1074%
1075% o node_info: the address of a structure of type NodeInfo which points to a
1076% node in the color cube tree that is to be pruned.
1077%
1078*/
1079static void ClosestColor(const Image *image,CubeInfo *cube_info,
1080 const NodeInfo *node_info)
1081{
cristybb503372010-05-27 20:51:26 +00001082 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001083 i;
1084
cristybb503372010-05-27 20:51:26 +00001085 size_t
cristy3ed852e2009-09-05 21:47:34 +00001086 number_children;
1087
1088 /*
1089 Traverse any children.
1090 */
1091 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00001092 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00001093 if (node_info->child[i] != (NodeInfo *) NULL)
1094 ClosestColor(image,cube_info,node_info->child[i]);
1095 if (node_info->number_unique != 0)
1096 {
1097 MagickRealType
1098 pixel;
1099
1100 register MagickRealType
1101 alpha,
1102 beta,
1103 distance;
1104
cristy101ab702011-10-13 13:06:32 +00001105 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001106 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001107
cristy101ab702011-10-13 13:06:32 +00001108 register RealPixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001109 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001110
1111 /*
1112 Determine if this color is "closest".
1113 */
1114 p=image->colormap+node_info->color_number;
1115 q=(&cube_info->target);
1116 alpha=1.0;
1117 beta=1.0;
cristy847620f2011-02-09 02:24:21 +00001118 if (cube_info->associate_alpha != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001119 {
cristy4c08aed2011-07-01 19:47:50 +00001120 alpha=(MagickRealType) (QuantumScale*p->alpha);
1121 beta=(MagickRealType) (QuantumScale*q->alpha);
cristy3ed852e2009-09-05 21:47:34 +00001122 }
cristy4c08aed2011-07-01 19:47:50 +00001123 pixel=alpha*p->red-beta*q->red;
cristy3ed852e2009-09-05 21:47:34 +00001124 distance=pixel*pixel;
cristy36fbc3b2011-02-09 02:30:04 +00001125 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001126 {
cristy4c08aed2011-07-01 19:47:50 +00001127 pixel=alpha*p->green-beta*q->green;
cristy3ed852e2009-09-05 21:47:34 +00001128 distance+=pixel*pixel;
cristy36fbc3b2011-02-09 02:30:04 +00001129 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001130 {
cristy4c08aed2011-07-01 19:47:50 +00001131 pixel=alpha*p->blue-beta*q->blue;
cristy3ed852e2009-09-05 21:47:34 +00001132 distance+=pixel*pixel;
cristy36fbc3b2011-02-09 02:30:04 +00001133 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001134 {
1135 pixel=alpha-beta;
1136 distance+=pixel*pixel;
cristyc4080402011-02-09 02:55:58 +00001137 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001138 {
1139 cube_info->distance=distance;
1140 cube_info->color_number=node_info->color_number;
1141 }
1142 }
1143 }
1144 }
1145 }
1146}
1147
1148/*
1149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1150% %
1151% %
1152% %
1153% C o m p r e s s I m a g e C o l o r m a p %
1154% %
1155% %
1156% %
1157%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1158%
1159% CompressImageColormap() compresses an image colormap by removing any
1160% duplicate or unused color entries.
1161%
1162% The format of the CompressImageColormap method is:
1163%
cristy018f07f2011-09-04 21:15:19 +00001164% MagickBooleanType CompressImageColormap(Image *image,
1165% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001166%
1167% A description of each parameter follows:
1168%
1169% o image: the image.
1170%
cristy018f07f2011-09-04 21:15:19 +00001171% o exception: return any errors or warnings in this structure.
1172%
cristy3ed852e2009-09-05 21:47:34 +00001173*/
cristy018f07f2011-09-04 21:15:19 +00001174MagickExport MagickBooleanType CompressImageColormap(Image *image,
1175 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001176{
1177 QuantizeInfo
1178 quantize_info;
1179
1180 assert(image != (Image *) NULL);
1181 assert(image->signature == MagickSignature);
1182 if (image->debug != MagickFalse)
1183 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy8a11cb12011-10-19 23:53:34 +00001184 if (IsPaletteImage(image,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001185 return(MagickFalse);
1186 GetQuantizeInfo(&quantize_info);
1187 quantize_info.number_colors=image->colors;
1188 quantize_info.tree_depth=MaxTreeDepth;
cristy018f07f2011-09-04 21:15:19 +00001189 return(QuantizeImage(&quantize_info,image,exception));
cristy3ed852e2009-09-05 21:47:34 +00001190}
1191
1192/*
1193%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1194% %
1195% %
1196% %
1197+ D e f i n e I m a g e C o l o r m a p %
1198% %
1199% %
1200% %
1201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1202%
1203% DefineImageColormap() traverses the color cube tree and notes each colormap
1204% entry. A colormap entry is any node in the color cube tree where the
1205% of unique colors is not zero. DefineImageColormap() returns the number of
1206% colors in the image colormap.
1207%
1208% The format of the DefineImageColormap method is:
1209%
cristybb503372010-05-27 20:51:26 +00001210% size_t DefineImageColormap(Image *image,CubeInfo *cube_info,
cristy3ed852e2009-09-05 21:47:34 +00001211% NodeInfo *node_info)
1212%
1213% A description of each parameter follows.
1214%
1215% o image: the image.
1216%
1217% o cube_info: A pointer to the Cube structure.
1218%
1219% o node_info: the address of a structure of type NodeInfo which points to a
1220% node in the color cube tree that is to be pruned.
1221%
1222*/
cristybb503372010-05-27 20:51:26 +00001223static size_t DefineImageColormap(Image *image,CubeInfo *cube_info,
cristy3ed852e2009-09-05 21:47:34 +00001224 NodeInfo *node_info)
1225{
cristybb503372010-05-27 20:51:26 +00001226 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001227 i;
1228
cristybb503372010-05-27 20:51:26 +00001229 size_t
cristy3ed852e2009-09-05 21:47:34 +00001230 number_children;
1231
1232 /*
1233 Traverse any children.
1234 */
1235 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00001236 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00001237 if (node_info->child[i] != (NodeInfo *) NULL)
cristycee97112010-05-28 00:44:52 +00001238 (void) DefineImageColormap(image,cube_info,node_info->child[i]);
cristy3ed852e2009-09-05 21:47:34 +00001239 if (node_info->number_unique != 0)
1240 {
1241 register MagickRealType
1242 alpha;
1243
cristy101ab702011-10-13 13:06:32 +00001244 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001245 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001246
1247 /*
1248 Colormap entry is defined by the mean color in this cube.
1249 */
1250 q=image->colormap+image->colors;
1251 alpha=(MagickRealType) ((MagickOffsetType) node_info->number_unique);
1252 alpha=1.0/(fabs(alpha) <= MagickEpsilon ? 1.0 : alpha);
1253 if (cube_info->associate_alpha == MagickFalse)
1254 {
cristye42f6582012-02-11 17:59:50 +00001255 q->red=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001256 (alpha*QuantumRange*node_info->total_color.red));
cristye42f6582012-02-11 17:59:50 +00001257 q->green=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001258 (alpha*QuantumRange*node_info->total_color.green));
cristye42f6582012-02-11 17:59:50 +00001259 q->blue=(double) ClampToQuantum((MagickRealType)
1260 (alpha*(double) QuantumRange*node_info->total_color.blue));
cristy4c08aed2011-07-01 19:47:50 +00001261 q->alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00001262 }
1263 else
1264 {
1265 MagickRealType
1266 opacity;
1267
1268 opacity=(MagickRealType) (alpha*QuantumRange*
cristy4c08aed2011-07-01 19:47:50 +00001269 node_info->total_color.alpha);
cristye42f6582012-02-11 17:59:50 +00001270 q->alpha=(double) ClampToQuantum(opacity);
cristy4c08aed2011-07-01 19:47:50 +00001271 if (q->alpha == OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00001272 {
cristye42f6582012-02-11 17:59:50 +00001273 q->red=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001274 (alpha*QuantumRange*node_info->total_color.red));
cristye42f6582012-02-11 17:59:50 +00001275 q->green=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001276 (alpha*QuantumRange*node_info->total_color.green));
cristye42f6582012-02-11 17:59:50 +00001277 q->blue=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001278 (alpha*QuantumRange*node_info->total_color.blue));
cristy3ed852e2009-09-05 21:47:34 +00001279 }
1280 else
1281 {
1282 MagickRealType
1283 gamma;
1284
cristy4c08aed2011-07-01 19:47:50 +00001285 gamma=(MagickRealType) (QuantumScale*q->alpha);
cristy3ed852e2009-09-05 21:47:34 +00001286 gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma);
cristye42f6582012-02-11 17:59:50 +00001287 q->red=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001288 (alpha*gamma*QuantumRange*node_info->total_color.red));
cristye42f6582012-02-11 17:59:50 +00001289 q->green=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001290 (alpha*gamma*QuantumRange*node_info->total_color.green));
cristye42f6582012-02-11 17:59:50 +00001291 q->blue=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001292 (alpha*gamma*QuantumRange*node_info->total_color.blue));
cristy3ed852e2009-09-05 21:47:34 +00001293 if (node_info->number_unique > cube_info->transparent_pixels)
1294 {
1295 cube_info->transparent_pixels=node_info->number_unique;
cristybb503372010-05-27 20:51:26 +00001296 cube_info->transparent_index=(ssize_t) image->colors;
cristy3ed852e2009-09-05 21:47:34 +00001297 }
1298 }
1299 }
1300 node_info->color_number=image->colors++;
1301 }
1302 return(image->colors);
1303}
1304
1305/*
1306%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1307% %
1308% %
1309% %
1310+ D e s t r o y C u b e I n f o %
1311% %
1312% %
1313% %
1314%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1315%
1316% DestroyCubeInfo() deallocates memory associated with an image.
1317%
1318% The format of the DestroyCubeInfo method is:
1319%
1320% DestroyCubeInfo(CubeInfo *cube_info)
1321%
1322% A description of each parameter follows:
1323%
1324% o cube_info: the address of a structure of type CubeInfo.
1325%
1326*/
1327static void DestroyCubeInfo(CubeInfo *cube_info)
1328{
1329 register Nodes
1330 *nodes;
1331
1332 /*
1333 Release color cube tree storage.
1334 */
1335 do
1336 {
1337 nodes=cube_info->node_queue->next;
1338 cube_info->node_queue->nodes=(NodeInfo *) RelinquishMagickMemory(
1339 cube_info->node_queue->nodes);
1340 cube_info->node_queue=(Nodes *) RelinquishMagickMemory(
1341 cube_info->node_queue);
1342 cube_info->node_queue=nodes;
1343 } while (cube_info->node_queue != (Nodes *) NULL);
cristybb503372010-05-27 20:51:26 +00001344 if (cube_info->cache != (ssize_t *) NULL)
1345 cube_info->cache=(ssize_t *) RelinquishMagickMemory(cube_info->cache);
cristy3ed852e2009-09-05 21:47:34 +00001346 cube_info->quantize_info=DestroyQuantizeInfo(cube_info->quantize_info);
1347 cube_info=(CubeInfo *) RelinquishMagickMemory(cube_info);
1348}
1349
1350/*
1351%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1352% %
1353% %
1354% %
1355% D e s t r o y Q u a n t i z e I n f o %
1356% %
1357% %
1358% %
1359%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1360%
1361% DestroyQuantizeInfo() deallocates memory associated with an QuantizeInfo
1362% structure.
1363%
1364% The format of the DestroyQuantizeInfo method is:
1365%
1366% QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info)
1367%
1368% A description of each parameter follows:
1369%
1370% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
1371%
1372*/
1373MagickExport QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info)
1374{
1375 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1376 assert(quantize_info != (QuantizeInfo *) NULL);
1377 assert(quantize_info->signature == MagickSignature);
1378 quantize_info->signature=(~MagickSignature);
1379 quantize_info=(QuantizeInfo *) RelinquishMagickMemory(quantize_info);
1380 return(quantize_info);
1381}
1382
1383/*
1384%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1385% %
1386% %
1387% %
1388+ D i t h e r I m a g e %
1389% %
1390% %
1391% %
1392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393%
1394% DitherImage() distributes the difference between an original image and
1395% the corresponding color reduced algorithm to neighboring pixels using
1396% serpentine-scan Floyd-Steinberg error diffusion. DitherImage returns
1397% MagickTrue if the image is dithered otherwise MagickFalse.
1398%
1399% The format of the DitherImage method is:
1400%
cristy8a11cb12011-10-19 23:53:34 +00001401% MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info,
1402% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001403%
1404% A description of each parameter follows.
1405%
1406% o image: the image.
1407%
1408% o cube_info: A pointer to the Cube structure.
1409%
cristy8a11cb12011-10-19 23:53:34 +00001410% o exception: return any errors or warnings in this structure.
1411%
cristy3ed852e2009-09-05 21:47:34 +00001412*/
1413
cristy101ab702011-10-13 13:06:32 +00001414static RealPixelInfo **DestroyPixelThreadSet(RealPixelInfo **pixels)
cristye9717ac2011-02-20 16:17:17 +00001415{
1416 register ssize_t
1417 i;
1418
cristy101ab702011-10-13 13:06:32 +00001419 assert(pixels != (RealPixelInfo **) NULL);
cristye9717ac2011-02-20 16:17:17 +00001420 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy101ab702011-10-13 13:06:32 +00001421 if (pixels[i] != (RealPixelInfo *) NULL)
1422 pixels[i]=(RealPixelInfo *) RelinquishMagickMemory(pixels[i]);
1423 pixels=(RealPixelInfo **) RelinquishMagickMemory(pixels);
cristye9717ac2011-02-20 16:17:17 +00001424 return(pixels);
1425}
1426
cristy101ab702011-10-13 13:06:32 +00001427static RealPixelInfo **AcquirePixelThreadSet(const size_t count)
cristye9717ac2011-02-20 16:17:17 +00001428{
cristy101ab702011-10-13 13:06:32 +00001429 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001430 **pixels;
1431
1432 register ssize_t
1433 i;
1434
1435 size_t
1436 number_threads;
1437
1438 number_threads=GetOpenMPMaximumThreads();
cristy101ab702011-10-13 13:06:32 +00001439 pixels=(RealPixelInfo **) AcquireQuantumMemory(number_threads,
cristye9717ac2011-02-20 16:17:17 +00001440 sizeof(*pixels));
cristy101ab702011-10-13 13:06:32 +00001441 if (pixels == (RealPixelInfo **) NULL)
1442 return((RealPixelInfo **) NULL);
cristye9717ac2011-02-20 16:17:17 +00001443 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
1444 for (i=0; i < (ssize_t) number_threads; i++)
1445 {
cristy101ab702011-10-13 13:06:32 +00001446 pixels[i]=(RealPixelInfo *) AcquireQuantumMemory(count,
cristye9717ac2011-02-20 16:17:17 +00001447 2*sizeof(**pixels));
cristy101ab702011-10-13 13:06:32 +00001448 if (pixels[i] == (RealPixelInfo *) NULL)
cristye9717ac2011-02-20 16:17:17 +00001449 return(DestroyPixelThreadSet(pixels));
1450 }
1451 return(pixels);
1452}
1453
cristyca972de2010-06-20 23:37:02 +00001454static inline ssize_t CacheOffset(CubeInfo *cube_info,
cristy101ab702011-10-13 13:06:32 +00001455 const RealPixelInfo *pixel)
cristyca972de2010-06-20 23:37:02 +00001456{
1457#define RedShift(pixel) (((pixel) >> CacheShift) << (0*(8-CacheShift)))
1458#define GreenShift(pixel) (((pixel) >> CacheShift) << (1*(8-CacheShift)))
1459#define BlueShift(pixel) (((pixel) >> CacheShift) << (2*(8-CacheShift)))
1460#define AlphaShift(pixel) (((pixel) >> CacheShift) << (3*(8-CacheShift)))
1461
1462 ssize_t
1463 offset;
1464
1465 offset=(ssize_t)
cristy15893a42010-11-20 18:57:15 +00001466 (RedShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->red))) |
cristyca972de2010-06-20 23:37:02 +00001467 GreenShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->green))) |
cristy15893a42010-11-20 18:57:15 +00001468 BlueShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->blue))));
cristyca972de2010-06-20 23:37:02 +00001469 if (cube_info->associate_alpha != MagickFalse)
cristy15893a42010-11-20 18:57:15 +00001470 offset|=AlphaShift(ScaleQuantumToChar(ClampToUnsignedQuantum(
cristy4c08aed2011-07-01 19:47:50 +00001471 pixel->alpha)));
cristyca972de2010-06-20 23:37:02 +00001472 return(offset);
1473}
1474
cristy8a11cb12011-10-19 23:53:34 +00001475static MagickBooleanType FloydSteinbergDither(Image *image,CubeInfo *cube_info,
1476 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001477{
1478#define DitherImageTag "Dither/Image"
1479
cristyc4c8d132010-01-07 01:58:38 +00001480 CacheView
1481 *image_view;
1482
cristy3ed852e2009-09-05 21:47:34 +00001483 MagickBooleanType
cristye9717ac2011-02-20 16:17:17 +00001484 status;
cristy3ed852e2009-09-05 21:47:34 +00001485
cristy101ab702011-10-13 13:06:32 +00001486 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001487 **pixels;
cristy3ed852e2009-09-05 21:47:34 +00001488
cristy847620f2011-02-09 02:24:21 +00001489 ssize_t
cristy847620f2011-02-09 02:24:21 +00001490 y;
1491
cristy3ed852e2009-09-05 21:47:34 +00001492 /*
1493 Distribute quantization error using Floyd-Steinberg.
1494 */
cristye9717ac2011-02-20 16:17:17 +00001495 pixels=AcquirePixelThreadSet(image->columns);
cristy101ab702011-10-13 13:06:32 +00001496 if (pixels == (RealPixelInfo **) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001497 return(MagickFalse);
cristye9717ac2011-02-20 16:17:17 +00001498 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00001499 image_view=AcquireAuthenticCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00001500 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001501 {
cristye9717ac2011-02-20 16:17:17 +00001502 const int
1503 id = GetOpenMPThreadId();
1504
1505 CubeInfo
1506 cube;
1507
cristy101ab702011-10-13 13:06:32 +00001508 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001509 *current,
1510 *previous;
1511
cristy4c08aed2011-07-01 19:47:50 +00001512 register Quantum
cristyecc31b12011-02-13 00:32:29 +00001513 *restrict q;
1514
cristybb503372010-05-27 20:51:26 +00001515 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001516 x;
1517
cristye9717ac2011-02-20 16:17:17 +00001518 size_t
1519 index;
1520
1521 ssize_t
1522 v;
1523
1524 if (status == MagickFalse)
1525 continue;
cristy3ed852e2009-09-05 21:47:34 +00001526 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001527 if (q == (Quantum *) NULL)
cristye9717ac2011-02-20 16:17:17 +00001528 {
1529 status=MagickFalse;
cristy00cbdd62011-02-20 17:29:26 +00001530 continue;
cristye9717ac2011-02-20 16:17:17 +00001531 }
cristyed231572011-07-14 02:18:59 +00001532 q+=(y & 0x01)*image->columns*GetPixelChannels(image);
cristye9717ac2011-02-20 16:17:17 +00001533 cube=(*cube_info);
1534 current=pixels[id]+(y & 0x01)*image->columns;
1535 previous=pixels[id]+((y+1) & 0x01)*image->columns;
cristy4c08aed2011-07-01 19:47:50 +00001536 v=(ssize_t) ((y & 0x01) != 0 ? -1 : 1);
cristybb503372010-05-27 20:51:26 +00001537 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001538 {
cristy101ab702011-10-13 13:06:32 +00001539 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001540 color,
1541 pixel;
1542
1543 register ssize_t
1544 i;
1545
1546 ssize_t
1547 u;
1548
cristyed231572011-07-14 02:18:59 +00001549 q-=(y & 0x01)*GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001550 u=(y & 0x01) != 0 ? (ssize_t) image->columns-1-x : x;
1551 AssociateAlphaPixel(image,&cube,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001552 if (x > 0)
1553 {
1554 pixel.red+=7*current[u-v].red/16;
1555 pixel.green+=7*current[u-v].green/16;
1556 pixel.blue+=7*current[u-v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001557 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001558 pixel.alpha+=7*current[u-v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001559 }
1560 if (y > 0)
1561 {
cristybb503372010-05-27 20:51:26 +00001562 if (x < (ssize_t) (image->columns-1))
cristy3ed852e2009-09-05 21:47:34 +00001563 {
1564 pixel.red+=previous[u+v].red/16;
1565 pixel.green+=previous[u+v].green/16;
1566 pixel.blue+=previous[u+v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001567 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001568 pixel.alpha+=previous[u+v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001569 }
1570 pixel.red+=5*previous[u].red/16;
1571 pixel.green+=5*previous[u].green/16;
1572 pixel.blue+=5*previous[u].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001573 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001574 pixel.alpha+=5*previous[u].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001575 if (x > 0)
1576 {
1577 pixel.red+=3*previous[u-v].red/16;
1578 pixel.green+=3*previous[u-v].green/16;
1579 pixel.blue+=3*previous[u-v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001580 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001581 pixel.alpha+=3*previous[u-v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001582 }
1583 }
cristy75ffdb72010-01-07 17:40:12 +00001584 pixel.red=(MagickRealType) ClampToUnsignedQuantum(pixel.red);
1585 pixel.green=(MagickRealType) ClampToUnsignedQuantum(pixel.green);
1586 pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue);
cristye9717ac2011-02-20 16:17:17 +00001587 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001588 pixel.alpha=(MagickRealType) ClampToUnsignedQuantum(pixel.alpha);
cristye9717ac2011-02-20 16:17:17 +00001589 i=CacheOffset(&cube,&pixel);
1590 if (cube.cache[i] < 0)
cristy3ed852e2009-09-05 21:47:34 +00001591 {
1592 register NodeInfo
1593 *node_info;
1594
cristybb503372010-05-27 20:51:26 +00001595 register size_t
cristy3ed852e2009-09-05 21:47:34 +00001596 id;
1597
1598 /*
1599 Identify the deepest node containing the pixel's color.
1600 */
cristye9717ac2011-02-20 16:17:17 +00001601 node_info=cube.root;
cristybb503372010-05-27 20:51:26 +00001602 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +00001603 {
cristye9717ac2011-02-20 16:17:17 +00001604 id=ColorToNodeId(&cube,&pixel,index);
cristy3ed852e2009-09-05 21:47:34 +00001605 if (node_info->child[id] == (NodeInfo *) NULL)
1606 break;
1607 node_info=node_info->child[id];
1608 }
1609 /*
1610 Find closest color among siblings and their children.
1611 */
cristye9717ac2011-02-20 16:17:17 +00001612 cube.target=pixel;
1613 cube.distance=(MagickRealType) (4.0*(QuantumRange+1.0)*(QuantumRange+
cristy3ed852e2009-09-05 21:47:34 +00001614 1.0)+1.0);
cristye9717ac2011-02-20 16:17:17 +00001615 ClosestColor(image,&cube,node_info->parent);
1616 cube.cache[i]=(ssize_t) cube.color_number;
cristy3ed852e2009-09-05 21:47:34 +00001617 }
1618 /*
1619 Assign pixel to closest colormap entry.
1620 */
cristye9717ac2011-02-20 16:17:17 +00001621 index=(size_t) cube.cache[i];
cristy3ed852e2009-09-05 21:47:34 +00001622 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +00001623 SetPixelIndex(image,(Quantum) index,q);
cristye9717ac2011-02-20 16:17:17 +00001624 if (cube.quantize_info->measure_error == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001625 {
cristye42f6582012-02-11 17:59:50 +00001626 SetPixelRed(image,ClampToQuantum(image->colormap[index].red),q);
1627 SetPixelGreen(image,ClampToQuantum(image->colormap[index].green),q);
1628 SetPixelBlue(image,ClampToQuantum(image->colormap[index].blue),q);
cristye9717ac2011-02-20 16:17:17 +00001629 if (cube.associate_alpha != MagickFalse)
cristye42f6582012-02-11 17:59:50 +00001630 SetPixelAlpha(image,ClampToQuantum(image->colormap[index].alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001631 }
1632 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristye9717ac2011-02-20 16:17:17 +00001633 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001634 /*
1635 Store the error.
1636 */
cristy101ab702011-10-13 13:06:32 +00001637 AssociateAlphaPixelInfo(image,&cube,image->colormap+index,&color);
cristy3ed852e2009-09-05 21:47:34 +00001638 current[u].red=pixel.red-color.red;
1639 current[u].green=pixel.green-color.green;
1640 current[u].blue=pixel.blue-color.blue;
cristye9717ac2011-02-20 16:17:17 +00001641 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001642 current[u].alpha=pixel.alpha-color.alpha;
cristye9717ac2011-02-20 16:17:17 +00001643 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1644 {
1645 MagickBooleanType
1646 proceed;
1647
1648#if defined(MAGICKCORE_OPENMP_SUPPORT)
1649 #pragma omp critical (MagickCore_FloydSteinbergDither)
1650#endif
1651 proceed=SetImageProgress(image,DitherImageTag,(MagickOffsetType) y,
1652 image->rows);
1653 if (proceed == MagickFalse)
1654 status=MagickFalse;
1655 }
cristyed231572011-07-14 02:18:59 +00001656 q+=((y+1) & 0x01)*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001657 }
1658 }
cristy3ed852e2009-09-05 21:47:34 +00001659 image_view=DestroyCacheView(image_view);
cristye9717ac2011-02-20 16:17:17 +00001660 pixels=DestroyPixelThreadSet(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001661 return(MagickTrue);
1662}
1663
1664static MagickBooleanType
cristy8a11cb12011-10-19 23:53:34 +00001665 RiemersmaDither(Image *,CacheView *,CubeInfo *,const unsigned int,
1666 ExceptionInfo *exception);
cristy3ed852e2009-09-05 21:47:34 +00001667
1668static void Riemersma(Image *image,CacheView *image_view,CubeInfo *cube_info,
cristy8a11cb12011-10-19 23:53:34 +00001669 const size_t level,const unsigned int direction,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001670{
1671 if (level == 1)
1672 switch (direction)
1673 {
1674 case WestGravity:
1675 {
cristy8a11cb12011-10-19 23:53:34 +00001676 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1677 exception);
1678 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1679 exception);
1680 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1681 exception);
cristy3ed852e2009-09-05 21:47:34 +00001682 break;
1683 }
1684 case EastGravity:
1685 {
cristy8a11cb12011-10-19 23:53:34 +00001686 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1687 exception);
1688 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1689 exception);
1690 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1691 exception);
cristy3ed852e2009-09-05 21:47:34 +00001692 break;
1693 }
1694 case NorthGravity:
1695 {
cristy8a11cb12011-10-19 23:53:34 +00001696 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1697 exception);
1698 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1699 exception);
1700 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1701 exception);
cristy3ed852e2009-09-05 21:47:34 +00001702 break;
1703 }
1704 case SouthGravity:
1705 {
cristy8a11cb12011-10-19 23:53:34 +00001706 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1707 exception);
1708 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1709 exception);
1710 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1711 exception);
cristy3ed852e2009-09-05 21:47:34 +00001712 break;
1713 }
1714 default:
1715 break;
1716 }
1717 else
1718 switch (direction)
1719 {
1720 case WestGravity:
1721 {
cristy8a11cb12011-10-19 23:53:34 +00001722 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1723 exception);
1724 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1725 exception);
1726 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1727 exception);
1728 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1729 exception);
1730 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1731 exception);
1732 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1733 exception);
1734 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1735 exception);
cristy3ed852e2009-09-05 21:47:34 +00001736 break;
1737 }
1738 case EastGravity:
1739 {
cristy8a11cb12011-10-19 23:53:34 +00001740 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1741 exception);
1742 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1743 exception);
1744 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1745 exception);
1746 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1747 exception);
1748 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1749 exception);
1750 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1751 exception);
1752 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1753 exception);
cristy3ed852e2009-09-05 21:47:34 +00001754 break;
1755 }
1756 case NorthGravity:
1757 {
cristy8a11cb12011-10-19 23:53:34 +00001758 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1759 exception);
1760 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1761 exception);
1762 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1763 exception);
1764 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1765 exception);
1766 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1767 exception);
1768 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1769 exception);
1770 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1771 exception);
cristy3ed852e2009-09-05 21:47:34 +00001772 break;
1773 }
1774 case SouthGravity:
1775 {
cristy8a11cb12011-10-19 23:53:34 +00001776 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1777 exception);
1778 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1779 exception);
1780 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1781 exception);
1782 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1783 exception);
1784 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1785 exception);
1786 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1787 exception);
1788 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1789 exception);
cristy3ed852e2009-09-05 21:47:34 +00001790 break;
1791 }
1792 default:
1793 break;
1794 }
1795}
1796
1797static MagickBooleanType RiemersmaDither(Image *image,CacheView *image_view,
cristy8a11cb12011-10-19 23:53:34 +00001798 CubeInfo *cube_info,const unsigned int direction,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001799{
1800#define DitherImageTag "Dither/Image"
1801
1802 MagickBooleanType
1803 proceed;
1804
cristy101ab702011-10-13 13:06:32 +00001805 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001806 color,
1807 pixel;
1808
1809 register CubeInfo
1810 *p;
1811
cristybb503372010-05-27 20:51:26 +00001812 size_t
cristy3ed852e2009-09-05 21:47:34 +00001813 index;
1814
1815 p=cube_info;
cristybb503372010-05-27 20:51:26 +00001816 if ((p->x >= 0) && (p->x < (ssize_t) image->columns) &&
1817 (p->y >= 0) && (p->y < (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001818 {
cristy4c08aed2011-07-01 19:47:50 +00001819 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001820 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001821
cristyecc31b12011-02-13 00:32:29 +00001822 register ssize_t
1823 i;
1824
cristy3ed852e2009-09-05 21:47:34 +00001825 /*
1826 Distribute error.
1827 */
cristy3ed852e2009-09-05 21:47:34 +00001828 q=GetCacheViewAuthenticPixels(image_view,p->x,p->y,1,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001829 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001830 return(MagickFalse);
cristy4c08aed2011-07-01 19:47:50 +00001831 AssociateAlphaPixel(image,cube_info,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001832 for (i=0; i < ErrorQueueLength; i++)
1833 {
1834 pixel.red+=p->weights[i]*p->error[i].red;
1835 pixel.green+=p->weights[i]*p->error[i].green;
1836 pixel.blue+=p->weights[i]*p->error[i].blue;
1837 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001838 pixel.alpha+=p->weights[i]*p->error[i].alpha;
cristy3ed852e2009-09-05 21:47:34 +00001839 }
cristy75ffdb72010-01-07 17:40:12 +00001840 pixel.red=(MagickRealType) ClampToUnsignedQuantum(pixel.red);
1841 pixel.green=(MagickRealType) ClampToUnsignedQuantum(pixel.green);
1842 pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00001843 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001844 pixel.alpha=(MagickRealType) ClampToUnsignedQuantum(pixel.alpha);
cristyca972de2010-06-20 23:37:02 +00001845 i=CacheOffset(cube_info,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001846 if (p->cache[i] < 0)
1847 {
1848 register NodeInfo
1849 *node_info;
1850
cristybb503372010-05-27 20:51:26 +00001851 register size_t
cristy3ed852e2009-09-05 21:47:34 +00001852 id;
1853
1854 /*
1855 Identify the deepest node containing the pixel's color.
1856 */
1857 node_info=p->root;
cristybb503372010-05-27 20:51:26 +00001858 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +00001859 {
1860 id=ColorToNodeId(cube_info,&pixel,index);
1861 if (node_info->child[id] == (NodeInfo *) NULL)
1862 break;
1863 node_info=node_info->child[id];
1864 }
cristyecc31b12011-02-13 00:32:29 +00001865 node_info=node_info->parent;
cristy3ed852e2009-09-05 21:47:34 +00001866 /*
1867 Find closest color among siblings and their children.
1868 */
1869 p->target=pixel;
1870 p->distance=(MagickRealType) (4.0*(QuantumRange+1.0)*((MagickRealType)
1871 QuantumRange+1.0)+1.0);
1872 ClosestColor(image,p,node_info->parent);
cristybb503372010-05-27 20:51:26 +00001873 p->cache[i]=(ssize_t) p->color_number;
cristy3ed852e2009-09-05 21:47:34 +00001874 }
1875 /*
1876 Assign pixel to closest colormap entry.
1877 */
cristy4c08aed2011-07-01 19:47:50 +00001878 index=(size_t) p->cache[i];
cristy3ed852e2009-09-05 21:47:34 +00001879 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +00001880 SetPixelIndex(image,(Quantum) index,q);
cristy3ed852e2009-09-05 21:47:34 +00001881 if (cube_info->quantize_info->measure_error == MagickFalse)
1882 {
cristye42f6582012-02-11 17:59:50 +00001883 SetPixelRed(image,ClampToQuantum(image->colormap[index].red),q);
1884 SetPixelGreen(image,ClampToQuantum(image->colormap[index].green),q);
1885 SetPixelBlue(image,ClampToQuantum(image->colormap[index].blue),q);
cristy3ed852e2009-09-05 21:47:34 +00001886 if (cube_info->associate_alpha != MagickFalse)
cristye42f6582012-02-11 17:59:50 +00001887 SetPixelAlpha(image,ClampToQuantum(image->colormap[index].alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001888 }
1889 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1890 return(MagickFalse);
1891 /*
1892 Propagate the error as the last entry of the error queue.
1893 */
1894 (void) CopyMagickMemory(p->error,p->error+1,(ErrorQueueLength-1)*
1895 sizeof(p->error[0]));
cristy101ab702011-10-13 13:06:32 +00001896 AssociateAlphaPixelInfo(image,cube_info,image->colormap+index,&color);
cristy3ed852e2009-09-05 21:47:34 +00001897 p->error[ErrorQueueLength-1].red=pixel.red-color.red;
1898 p->error[ErrorQueueLength-1].green=pixel.green-color.green;
1899 p->error[ErrorQueueLength-1].blue=pixel.blue-color.blue;
1900 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001901 p->error[ErrorQueueLength-1].alpha=pixel.alpha-color.alpha;
cristy3ed852e2009-09-05 21:47:34 +00001902 proceed=SetImageProgress(image,DitherImageTag,p->offset,p->span);
1903 if (proceed == MagickFalse)
1904 return(MagickFalse);
1905 p->offset++;
1906 }
1907 switch (direction)
1908 {
1909 case WestGravity: p->x--; break;
1910 case EastGravity: p->x++; break;
1911 case NorthGravity: p->y--; break;
1912 case SouthGravity: p->y++; break;
1913 }
1914 return(MagickTrue);
1915}
1916
cristybb503372010-05-27 20:51:26 +00001917static inline ssize_t MagickMax(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00001918{
1919 if (x > y)
1920 return(x);
1921 return(y);
1922}
1923
cristybb503372010-05-27 20:51:26 +00001924static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00001925{
1926 if (x < y)
1927 return(x);
1928 return(y);
1929}
1930
cristy8a11cb12011-10-19 23:53:34 +00001931static MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info,
1932 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001933{
cristyc4c8d132010-01-07 01:58:38 +00001934 CacheView
1935 *image_view;
1936
cristy3ed852e2009-09-05 21:47:34 +00001937 MagickBooleanType
1938 status;
1939
cristybb503372010-05-27 20:51:26 +00001940 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001941 i;
1942
cristybb503372010-05-27 20:51:26 +00001943 size_t
cristy3ed852e2009-09-05 21:47:34 +00001944 depth;
1945
cristyfb7e9cd2011-02-20 16:26:15 +00001946 if (cube_info->quantize_info->dither_method != RiemersmaDitherMethod)
cristy8a11cb12011-10-19 23:53:34 +00001947 return(FloydSteinbergDither(image,cube_info,exception));
cristy3ed852e2009-09-05 21:47:34 +00001948 /*
cristycee97112010-05-28 00:44:52 +00001949 Distribute quantization error along a Hilbert curve.
cristy3ed852e2009-09-05 21:47:34 +00001950 */
1951 (void) ResetMagickMemory(cube_info->error,0,ErrorQueueLength*
1952 sizeof(*cube_info->error));
1953 cube_info->x=0;
1954 cube_info->y=0;
cristybb503372010-05-27 20:51:26 +00001955 i=MagickMax((ssize_t) image->columns,(ssize_t) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001956 for (depth=1; i != 0; depth++)
1957 i>>=1;
cristybb503372010-05-27 20:51:26 +00001958 if ((ssize_t) (1L << depth) < MagickMax((ssize_t) image->columns,(ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001959 depth++;
1960 cube_info->offset=0;
1961 cube_info->span=(MagickSizeType) image->columns*image->rows;
cristydb070952012-04-20 14:33:00 +00001962 image_view=AcquireAuthenticCacheView(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00001963 if (depth > 1)
cristy8a11cb12011-10-19 23:53:34 +00001964 Riemersma(image,image_view,cube_info,depth-1,NorthGravity,exception);
1965 status=RiemersmaDither(image,image_view,cube_info,ForgetGravity,exception);
cristy3ed852e2009-09-05 21:47:34 +00001966 image_view=DestroyCacheView(image_view);
1967 return(status);
1968}
1969
1970/*
1971%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1972% %
1973% %
1974% %
1975+ G e t C u b e I n f o %
1976% %
1977% %
1978% %
1979%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1980%
1981% GetCubeInfo() initialize the Cube data structure.
1982%
1983% The format of the GetCubeInfo method is:
1984%
1985% CubeInfo GetCubeInfo(const QuantizeInfo *quantize_info,
cristybb503372010-05-27 20:51:26 +00001986% const size_t depth,const size_t maximum_colors)
cristy3ed852e2009-09-05 21:47:34 +00001987%
1988% A description of each parameter follows.
1989%
1990% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
1991%
1992% o depth: Normally, this integer value is zero or one. A zero or
1993% one tells Quantize to choose a optimal tree depth of Log4(number_colors).
1994% A tree of this depth generally allows the best representation of the
1995% reference image with the least amount of memory and the fastest
1996% computational speed. In some cases, such as an image with low color
1997% dispersion (a few number of colors), a value other than
1998% Log4(number_colors) is required. To expand the color tree completely,
1999% use a value of 8.
2000%
2001% o maximum_colors: maximum colors.
2002%
2003*/
2004static CubeInfo *GetCubeInfo(const QuantizeInfo *quantize_info,
cristybb503372010-05-27 20:51:26 +00002005 const size_t depth,const size_t maximum_colors)
cristy3ed852e2009-09-05 21:47:34 +00002006{
2007 CubeInfo
2008 *cube_info;
2009
2010 MagickRealType
2011 sum,
2012 weight;
2013
cristybb503372010-05-27 20:51:26 +00002014 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002015 i;
2016
cristyecc31b12011-02-13 00:32:29 +00002017 size_t
2018 length;
2019
cristy3ed852e2009-09-05 21:47:34 +00002020 /*
2021 Initialize tree to describe color cube_info.
2022 */
cristy73bd4a52010-10-05 11:24:23 +00002023 cube_info=(CubeInfo *) AcquireMagickMemory(sizeof(*cube_info));
cristy3ed852e2009-09-05 21:47:34 +00002024 if (cube_info == (CubeInfo *) NULL)
2025 return((CubeInfo *) NULL);
2026 (void) ResetMagickMemory(cube_info,0,sizeof(*cube_info));
2027 cube_info->depth=depth;
2028 if (cube_info->depth > MaxTreeDepth)
2029 cube_info->depth=MaxTreeDepth;
2030 if (cube_info->depth < 2)
2031 cube_info->depth=2;
2032 cube_info->maximum_colors=maximum_colors;
2033 /*
2034 Initialize root node.
2035 */
2036 cube_info->root=GetNodeInfo(cube_info,0,0,(NodeInfo *) NULL);
2037 if (cube_info->root == (NodeInfo *) NULL)
2038 return((CubeInfo *) NULL);
2039 cube_info->root->parent=cube_info->root;
2040 cube_info->quantize_info=CloneQuantizeInfo(quantize_info);
2041 if (cube_info->quantize_info->dither == MagickFalse)
2042 return(cube_info);
2043 /*
2044 Initialize dither resources.
2045 */
2046 length=(size_t) (1UL << (4*(8-CacheShift)));
cristybb503372010-05-27 20:51:26 +00002047 cube_info->cache=(ssize_t *) AcquireQuantumMemory(length,
cristy3ed852e2009-09-05 21:47:34 +00002048 sizeof(*cube_info->cache));
cristybb503372010-05-27 20:51:26 +00002049 if (cube_info->cache == (ssize_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002050 return((CubeInfo *) NULL);
2051 /*
2052 Initialize color cache.
2053 */
cristybb503372010-05-27 20:51:26 +00002054 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00002055 cube_info->cache[i]=(-1);
2056 /*
cristycee97112010-05-28 00:44:52 +00002057 Distribute weights along a curve of exponential decay.
cristy3ed852e2009-09-05 21:47:34 +00002058 */
2059 weight=1.0;
2060 for (i=0; i < ErrorQueueLength; i++)
2061 {
2062 cube_info->weights[ErrorQueueLength-i-1]=1.0/weight;
2063 weight*=exp(log(((double) QuantumRange+1.0))/(ErrorQueueLength-1.0));
2064 }
2065 /*
2066 Normalize the weighting factors.
2067 */
2068 weight=0.0;
2069 for (i=0; i < ErrorQueueLength; i++)
2070 weight+=cube_info->weights[i];
2071 sum=0.0;
2072 for (i=0; i < ErrorQueueLength; i++)
2073 {
2074 cube_info->weights[i]/=weight;
2075 sum+=cube_info->weights[i];
2076 }
2077 cube_info->weights[0]+=1.0-sum;
2078 return(cube_info);
2079}
2080
2081/*
2082%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2083% %
2084% %
2085% %
2086+ G e t N o d e I n f o %
2087% %
2088% %
2089% %
2090%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2091%
2092% GetNodeInfo() allocates memory for a new node in the color cube tree and
2093% presets all fields to zero.
2094%
2095% The format of the GetNodeInfo method is:
2096%
cristybb503372010-05-27 20:51:26 +00002097% NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id,
2098% const size_t level,NodeInfo *parent)
cristy3ed852e2009-09-05 21:47:34 +00002099%
2100% A description of each parameter follows.
2101%
2102% o node: The GetNodeInfo method returns a pointer to a queue of nodes.
2103%
2104% o id: Specifies the child number of the node.
2105%
2106% o level: Specifies the level in the storage_class the node resides.
2107%
2108*/
cristybb503372010-05-27 20:51:26 +00002109static NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id,
2110 const size_t level,NodeInfo *parent)
cristy3ed852e2009-09-05 21:47:34 +00002111{
2112 NodeInfo
2113 *node_info;
2114
2115 if (cube_info->free_nodes == 0)
2116 {
2117 Nodes
2118 *nodes;
2119
2120 /*
2121 Allocate a new queue of nodes.
2122 */
cristy73bd4a52010-10-05 11:24:23 +00002123 nodes=(Nodes *) AcquireMagickMemory(sizeof(*nodes));
cristy3ed852e2009-09-05 21:47:34 +00002124 if (nodes == (Nodes *) NULL)
2125 return((NodeInfo *) NULL);
2126 nodes->nodes=(NodeInfo *) AcquireQuantumMemory(NodesInAList,
2127 sizeof(*nodes->nodes));
2128 if (nodes->nodes == (NodeInfo *) NULL)
2129 return((NodeInfo *) NULL);
2130 nodes->next=cube_info->node_queue;
2131 cube_info->node_queue=nodes;
2132 cube_info->next_node=nodes->nodes;
2133 cube_info->free_nodes=NodesInAList;
2134 }
2135 cube_info->nodes++;
2136 cube_info->free_nodes--;
2137 node_info=cube_info->next_node++;
2138 (void) ResetMagickMemory(node_info,0,sizeof(*node_info));
2139 node_info->parent=parent;
2140 node_info->id=id;
2141 node_info->level=level;
2142 return(node_info);
2143}
2144
2145/*
2146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2147% %
2148% %
2149% %
2150% G e t I m a g e Q u a n t i z e E r r o r %
2151% %
2152% %
2153% %
2154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2155%
2156% GetImageQuantizeError() measures the difference between the original
2157% and quantized images. This difference is the total quantization error.
2158% The error is computed by summing over all pixels in an image the distance
2159% squared in RGB space between each reference pixel value and its quantized
2160% value. These values are computed:
2161%
2162% o mean_error_per_pixel: This value is the mean error for any single
2163% pixel in the image.
2164%
2165% o normalized_mean_square_error: This value is the normalized mean
2166% quantization error for any single pixel in the image. This distance
2167% measure is normalized to a range between 0 and 1. It is independent
2168% of the range of red, green, and blue values in the image.
2169%
2170% o normalized_maximum_square_error: Thsi value is the normalized
2171% maximum quantization error for any single pixel in the image. This
2172% distance measure is normalized to a range between 0 and 1. It is
2173% independent of the range of red, green, and blue values in your image.
2174%
2175% The format of the GetImageQuantizeError method is:
2176%
cristy8a11cb12011-10-19 23:53:34 +00002177% MagickBooleanType GetImageQuantizeError(Image *image,
2178% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002179%
2180% A description of each parameter follows.
2181%
2182% o image: the image.
2183%
cristy8a11cb12011-10-19 23:53:34 +00002184% o exception: return any errors or warnings in this structure.
2185%
cristy3ed852e2009-09-05 21:47:34 +00002186*/
cristy8a11cb12011-10-19 23:53:34 +00002187MagickExport MagickBooleanType GetImageQuantizeError(Image *image,
2188 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002189{
cristyc4c8d132010-01-07 01:58:38 +00002190 CacheView
2191 *image_view;
2192
cristy3ed852e2009-09-05 21:47:34 +00002193 MagickRealType
2194 alpha,
2195 area,
2196 beta,
2197 distance,
2198 maximum_error,
2199 mean_error,
2200 mean_error_per_pixel;
2201
cristybb503372010-05-27 20:51:26 +00002202 size_t
cristy3ed852e2009-09-05 21:47:34 +00002203 index;
2204
cristyecc31b12011-02-13 00:32:29 +00002205 ssize_t
2206 y;
2207
cristy3ed852e2009-09-05 21:47:34 +00002208 assert(image != (Image *) NULL);
2209 assert(image->signature == MagickSignature);
2210 if (image->debug != MagickFalse)
2211 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy8a11cb12011-10-19 23:53:34 +00002212 image->total_colors=GetNumberColors(image,(FILE *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00002213 (void) ResetMagickMemory(&image->error,0,sizeof(image->error));
2214 if (image->storage_class == DirectClass)
2215 return(MagickTrue);
2216 alpha=1.0;
2217 beta=1.0;
2218 area=3.0*image->columns*image->rows;
2219 maximum_error=0.0;
2220 mean_error_per_pixel=0.0;
2221 mean_error=0.0;
cristydb070952012-04-20 14:33:00 +00002222 image_view=AcquireVirtualCacheView(image,exception);
cristybb503372010-05-27 20:51:26 +00002223 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002224 {
cristy4c08aed2011-07-01 19:47:50 +00002225 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002226 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002227
cristybb503372010-05-27 20:51:26 +00002228 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002229 x;
2230
2231 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002232 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002233 break;
cristybb503372010-05-27 20:51:26 +00002234 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002235 {
cristy4c08aed2011-07-01 19:47:50 +00002236 index=1UL*GetPixelIndex(image,p);
cristy3ed852e2009-09-05 21:47:34 +00002237 if (image->matte != MagickFalse)
2238 {
cristy4c08aed2011-07-01 19:47:50 +00002239 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
2240 beta=(MagickRealType) (QuantumScale*image->colormap[index].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002241 }
cristy4c08aed2011-07-01 19:47:50 +00002242 distance=fabs(alpha*GetPixelRed(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002243 image->colormap[index].red);
cristy3ed852e2009-09-05 21:47:34 +00002244 mean_error_per_pixel+=distance;
2245 mean_error+=distance*distance;
2246 if (distance > maximum_error)
2247 maximum_error=distance;
cristy4c08aed2011-07-01 19:47:50 +00002248 distance=fabs(alpha*GetPixelGreen(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002249 image->colormap[index].green);
cristy3ed852e2009-09-05 21:47:34 +00002250 mean_error_per_pixel+=distance;
2251 mean_error+=distance*distance;
2252 if (distance > maximum_error)
2253 maximum_error=distance;
cristy4c08aed2011-07-01 19:47:50 +00002254 distance=fabs(alpha*GetPixelBlue(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002255 image->colormap[index].blue);
cristy3ed852e2009-09-05 21:47:34 +00002256 mean_error_per_pixel+=distance;
2257 mean_error+=distance*distance;
2258 if (distance > maximum_error)
2259 maximum_error=distance;
cristyed231572011-07-14 02:18:59 +00002260 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002261 }
2262 }
2263 image_view=DestroyCacheView(image_view);
2264 image->error.mean_error_per_pixel=(double) mean_error_per_pixel/area;
2265 image->error.normalized_mean_error=(double) QuantumScale*QuantumScale*
2266 mean_error/area;
2267 image->error.normalized_maximum_error=(double) QuantumScale*maximum_error;
2268 return(MagickTrue);
2269}
2270
2271/*
2272%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2273% %
2274% %
2275% %
2276% G e t Q u a n t i z e I n f o %
2277% %
2278% %
2279% %
2280%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2281%
2282% GetQuantizeInfo() initializes the QuantizeInfo structure.
2283%
2284% The format of the GetQuantizeInfo method is:
2285%
2286% GetQuantizeInfo(QuantizeInfo *quantize_info)
2287%
2288% A description of each parameter follows:
2289%
2290% o quantize_info: Specifies a pointer to a QuantizeInfo structure.
2291%
2292*/
2293MagickExport void GetQuantizeInfo(QuantizeInfo *quantize_info)
2294{
2295 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2296 assert(quantize_info != (QuantizeInfo *) NULL);
2297 (void) ResetMagickMemory(quantize_info,0,sizeof(*quantize_info));
2298 quantize_info->number_colors=256;
2299 quantize_info->dither=MagickTrue;
2300 quantize_info->dither_method=RiemersmaDitherMethod;
2301 quantize_info->colorspace=UndefinedColorspace;
2302 quantize_info->measure_error=MagickFalse;
2303 quantize_info->signature=MagickSignature;
2304}
2305
2306/*
2307%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2308% %
2309% %
2310% %
cristy018f07f2011-09-04 21:15:19 +00002311% P o s t e r i z e I m a g e %
cristy3ed852e2009-09-05 21:47:34 +00002312% %
2313% %
2314% %
2315%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2316%
2317% PosterizeImage() reduces the image to a limited number of colors for a
2318% "poster" effect.
2319%
2320% The format of the PosterizeImage method is:
2321%
cristybb503372010-05-27 20:51:26 +00002322% MagickBooleanType PosterizeImage(Image *image,const size_t levels,
cristy018f07f2011-09-04 21:15:19 +00002323% const MagickBooleanType dither,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002324%
2325% A description of each parameter follows:
2326%
2327% o image: Specifies a pointer to an Image structure.
2328%
2329% o levels: Number of color levels allowed in each channel. Very low values
2330% (2, 3, or 4) have the most visible effect.
2331%
cristy847620f2011-02-09 02:24:21 +00002332% o dither: Set this integer value to something other than zero to dither
2333% the mapped image.
cristy3ed852e2009-09-05 21:47:34 +00002334%
cristy018f07f2011-09-04 21:15:19 +00002335% o exception: return any errors or warnings in this structure.
2336%
cristy3ed852e2009-09-05 21:47:34 +00002337*/
cristyd1a2c0f2011-02-09 14:14:50 +00002338
cristy4d727152011-02-10 19:57:21 +00002339static inline ssize_t MagickRound(MagickRealType x)
2340{
2341 /*
cristyecc31b12011-02-13 00:32:29 +00002342 Round the fraction to nearest integer.
cristy4d727152011-02-10 19:57:21 +00002343 */
2344 if (x >= 0.0)
2345 return((ssize_t) (x+0.5));
2346 return((ssize_t) (x-0.5));
2347}
2348
cristyd1a2c0f2011-02-09 14:14:50 +00002349MagickExport MagickBooleanType PosterizeImage(Image *image,const size_t levels,
cristy018f07f2011-09-04 21:15:19 +00002350 const MagickBooleanType dither,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002351{
cristyd1a2c0f2011-02-09 14:14:50 +00002352#define PosterizeImageTag "Posterize/Image"
cristy4d727152011-02-10 19:57:21 +00002353#define PosterizePixel(pixel) (Quantum) (QuantumRange*(MagickRound( \
cristy3e9cad02011-02-20 01:42:00 +00002354 QuantumScale*pixel*(levels-1)))/MagickMax((ssize_t) levels-1,1))
cristyd1a2c0f2011-02-09 14:14:50 +00002355
cristyc4c8d132010-01-07 01:58:38 +00002356 CacheView
cristyd1a2c0f2011-02-09 14:14:50 +00002357 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00002358
cristy3ed852e2009-09-05 21:47:34 +00002359 MagickBooleanType
2360 status;
2361
cristyd1a2c0f2011-02-09 14:14:50 +00002362 MagickOffsetType
2363 progress;
2364
cristy3ed852e2009-09-05 21:47:34 +00002365 QuantizeInfo
2366 *quantize_info;
2367
cristy847620f2011-02-09 02:24:21 +00002368 register ssize_t
2369 i;
2370
cristy847620f2011-02-09 02:24:21 +00002371 ssize_t
cristyd1a2c0f2011-02-09 14:14:50 +00002372 y;
cristy847620f2011-02-09 02:24:21 +00002373
cristy3ed852e2009-09-05 21:47:34 +00002374 assert(image != (Image *) NULL);
2375 assert(image->signature == MagickSignature);
2376 if (image->debug != MagickFalse)
2377 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyd1a2c0f2011-02-09 14:14:50 +00002378 if (image->storage_class == PseudoClass)
2379#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002380 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyd1a2c0f2011-02-09 14:14:50 +00002381#endif
2382 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002383 {
cristyd1a2c0f2011-02-09 14:14:50 +00002384 /*
2385 Posterize colormap.
2386 */
cristyed231572011-07-14 02:18:59 +00002387 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002388 image->colormap[i].red=(double)
2389 PosterizePixel(image->colormap[i].red);
cristyed231572011-07-14 02:18:59 +00002390 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002391 image->colormap[i].green=(double)
2392 PosterizePixel(image->colormap[i].green);
cristyed231572011-07-14 02:18:59 +00002393 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002394 image->colormap[i].blue=(double)
2395 PosterizePixel(image->colormap[i].blue);
cristyed231572011-07-14 02:18:59 +00002396 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002397 image->colormap[i].alpha=(double)
2398 PosterizePixel(image->colormap[i].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002399 }
cristyd1a2c0f2011-02-09 14:14:50 +00002400 /*
2401 Posterize image.
2402 */
2403 status=MagickTrue;
2404 progress=0;
cristydb070952012-04-20 14:33:00 +00002405 image_view=AcquireAuthenticCacheView(image,exception);
cristyd1a2c0f2011-02-09 14:14:50 +00002406#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002407 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyd1a2c0f2011-02-09 14:14:50 +00002408#endif
2409 for (y=0; y < (ssize_t) image->rows; y++)
2410 {
cristy4c08aed2011-07-01 19:47:50 +00002411 register Quantum
cristyd1a2c0f2011-02-09 14:14:50 +00002412 *restrict q;
2413
2414 register ssize_t
2415 x;
2416
2417 if (status == MagickFalse)
2418 continue;
2419 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002420 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002421 {
cristyd1a2c0f2011-02-09 14:14:50 +00002422 status=MagickFalse;
2423 continue;
cristy3ed852e2009-09-05 21:47:34 +00002424 }
cristyd1a2c0f2011-02-09 14:14:50 +00002425 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002426 {
cristyed231572011-07-14 02:18:59 +00002427 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002428 SetPixelRed(image,PosterizePixel(GetPixelRed(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002429 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002430 SetPixelGreen(image,PosterizePixel(GetPixelGreen(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002431 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002432 SetPixelBlue(image,PosterizePixel(GetPixelBlue(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002433 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002434 (image->colorspace == CMYKColorspace))
2435 SetPixelBlack(image,PosterizePixel(GetPixelBlack(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002436 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristyd1a2c0f2011-02-09 14:14:50 +00002437 (image->matte == MagickTrue))
cristy4c08aed2011-07-01 19:47:50 +00002438 SetPixelAlpha(image,PosterizePixel(GetPixelAlpha(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002439 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002440 }
cristyd1a2c0f2011-02-09 14:14:50 +00002441 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2442 status=MagickFalse;
2443 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2444 {
2445 MagickBooleanType
2446 proceed;
2447
2448#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13020672011-07-08 02:33:26 +00002449 #pragma omp critical (MagickCore_PosterizeImage)
cristyd1a2c0f2011-02-09 14:14:50 +00002450#endif
2451 proceed=SetImageProgress(image,PosterizeImageTag,progress++,
2452 image->rows);
2453 if (proceed == MagickFalse)
2454 status=MagickFalse;
2455 }
2456 }
2457 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002458 quantize_info=AcquireQuantizeInfo((ImageInfo *) NULL);
cristyd1a2c0f2011-02-09 14:14:50 +00002459 quantize_info->number_colors=(size_t) MagickMin((ssize_t) levels*levels*
2460 levels,MaxColormapSize+1);
cristy3ed852e2009-09-05 21:47:34 +00002461 quantize_info->dither=dither;
cristy3e9cad02011-02-20 01:42:00 +00002462 quantize_info->tree_depth=MaxTreeDepth;
cristy018f07f2011-09-04 21:15:19 +00002463 status=QuantizeImage(quantize_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002464 quantize_info=DestroyQuantizeInfo(quantize_info);
cristy3ed852e2009-09-05 21:47:34 +00002465 return(status);
2466}
2467
2468/*
2469%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2470% %
2471% %
2472% %
2473+ P r u n e C h i l d %
2474% %
2475% %
2476% %
2477%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2478%
2479% PruneChild() deletes the given node and merges its statistics into its
2480% parent.
2481%
2482% The format of the PruneSubtree method is:
2483%
2484% PruneChild(const Image *image,CubeInfo *cube_info,
2485% const NodeInfo *node_info)
2486%
2487% A description of each parameter follows.
2488%
2489% o image: the image.
2490%
2491% o cube_info: A pointer to the Cube structure.
2492%
2493% o node_info: pointer to node in color cube tree that is to be pruned.
2494%
2495*/
2496static void PruneChild(const Image *image,CubeInfo *cube_info,
2497 const NodeInfo *node_info)
2498{
2499 NodeInfo
2500 *parent;
2501
cristybb503372010-05-27 20:51:26 +00002502 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002503 i;
2504
cristybb503372010-05-27 20:51:26 +00002505 size_t
cristy3ed852e2009-09-05 21:47:34 +00002506 number_children;
2507
2508 /*
2509 Traverse any children.
2510 */
2511 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002512 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002513 if (node_info->child[i] != (NodeInfo *) NULL)
2514 PruneChild(image,cube_info,node_info->child[i]);
2515 /*
2516 Merge color statistics into parent.
2517 */
2518 parent=node_info->parent;
2519 parent->number_unique+=node_info->number_unique;
2520 parent->total_color.red+=node_info->total_color.red;
2521 parent->total_color.green+=node_info->total_color.green;
2522 parent->total_color.blue+=node_info->total_color.blue;
cristy4c08aed2011-07-01 19:47:50 +00002523 parent->total_color.alpha+=node_info->total_color.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002524 parent->child[node_info->id]=(NodeInfo *) NULL;
2525 cube_info->nodes--;
2526}
2527
2528/*
2529%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2530% %
2531% %
2532% %
2533+ P r u n e L e v e l %
2534% %
2535% %
2536% %
2537%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2538%
2539% PruneLevel() deletes all nodes at the bottom level of the color tree merging
2540% their color statistics into their parent node.
2541%
2542% The format of the PruneLevel method is:
2543%
2544% PruneLevel(const Image *image,CubeInfo *cube_info,
2545% const NodeInfo *node_info)
2546%
2547% A description of each parameter follows.
2548%
2549% o image: the image.
2550%
2551% o cube_info: A pointer to the Cube structure.
2552%
2553% o node_info: pointer to node in color cube tree that is to be pruned.
2554%
2555*/
2556static void PruneLevel(const Image *image,CubeInfo *cube_info,
2557 const NodeInfo *node_info)
2558{
cristybb503372010-05-27 20:51:26 +00002559 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002560 i;
2561
cristybb503372010-05-27 20:51:26 +00002562 size_t
cristy3ed852e2009-09-05 21:47:34 +00002563 number_children;
2564
2565 /*
2566 Traverse any children.
2567 */
2568 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002569 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002570 if (node_info->child[i] != (NodeInfo *) NULL)
2571 PruneLevel(image,cube_info,node_info->child[i]);
2572 if (node_info->level == cube_info->depth)
2573 PruneChild(image,cube_info,node_info);
2574}
2575
2576/*
2577%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2578% %
2579% %
2580% %
2581+ P r u n e T o C u b e D e p t h %
2582% %
2583% %
2584% %
2585%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2586%
2587% PruneToCubeDepth() deletes any nodes at a depth greater than
2588% cube_info->depth while merging their color statistics into their parent
2589% node.
2590%
2591% The format of the PruneToCubeDepth method is:
2592%
2593% PruneToCubeDepth(const Image *image,CubeInfo *cube_info,
2594% const NodeInfo *node_info)
2595%
2596% A description of each parameter follows.
2597%
2598% o cube_info: A pointer to the Cube structure.
2599%
2600% o node_info: pointer to node in color cube tree that is to be pruned.
2601%
2602*/
2603static void PruneToCubeDepth(const Image *image,CubeInfo *cube_info,
2604 const NodeInfo *node_info)
2605{
cristybb503372010-05-27 20:51:26 +00002606 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002607 i;
2608
cristybb503372010-05-27 20:51:26 +00002609 size_t
cristy3ed852e2009-09-05 21:47:34 +00002610 number_children;
2611
2612 /*
2613 Traverse any children.
2614 */
2615 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002616 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002617 if (node_info->child[i] != (NodeInfo *) NULL)
2618 PruneToCubeDepth(image,cube_info,node_info->child[i]);
2619 if (node_info->level > cube_info->depth)
2620 PruneChild(image,cube_info,node_info);
2621}
2622
2623/*
2624%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2625% %
2626% %
2627% %
2628% Q u a n t i z e I m a g e %
2629% %
2630% %
2631% %
2632%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2633%
2634% QuantizeImage() analyzes the colors within a reference image and chooses a
2635% fixed number of colors to represent the image. The goal of the algorithm
2636% is to minimize the color difference between the input and output image while
2637% minimizing the processing time.
2638%
2639% The format of the QuantizeImage method is:
2640%
2641% MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002642% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002643%
2644% A description of each parameter follows:
2645%
2646% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
2647%
2648% o image: the image.
2649%
cristy018f07f2011-09-04 21:15:19 +00002650% o exception: return any errors or warnings in this structure.
2651%
cristy3ed852e2009-09-05 21:47:34 +00002652*/
cristy5f7dca62011-08-12 12:38:05 +00002653
2654static MagickBooleanType DirectToColormapImage(Image *image,
2655 ExceptionInfo *exception)
2656{
2657 CacheView
2658 *image_view;
2659
2660 MagickBooleanType
2661 status;
2662
2663 register ssize_t
2664 i;
2665
2666 size_t
2667 number_colors;
2668
2669 ssize_t
2670 y;
2671
2672 status=MagickTrue;
2673 number_colors=(size_t) (image->columns*image->rows);
cristy018f07f2011-09-04 21:15:19 +00002674 if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
cristy5f7dca62011-08-12 12:38:05 +00002675 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2676 image->filename);
2677 if (image->colors != number_colors)
2678 return(MagickFalse);
2679 i=0;
cristydb070952012-04-20 14:33:00 +00002680 image_view=AcquireAuthenticCacheView(image,exception);
cristy5f7dca62011-08-12 12:38:05 +00002681 for (y=0; y < (ssize_t) image->rows; y++)
2682 {
2683 MagickBooleanType
2684 proceed;
2685
2686 register Quantum
2687 *restrict q;
2688
2689 register ssize_t
2690 x;
2691
2692 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2693 if (q == (Quantum *) NULL)
2694 break;
2695 for (x=0; x < (ssize_t) image->columns; x++)
2696 {
cristye42f6582012-02-11 17:59:50 +00002697 image->colormap[i].red=(double) GetPixelRed(image,q);
2698 image->colormap[i].green=(double) GetPixelGreen(image,q);
2699 image->colormap[i].blue=(double) GetPixelBlue(image,q);
2700 image->colormap[i].alpha=(double) GetPixelAlpha(image,q);
cristy5f7dca62011-08-12 12:38:05 +00002701 SetPixelIndex(image,(Quantum) i,q);
2702 i++;
2703 q+=GetPixelChannels(image);
2704 }
2705 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2706 break;
2707 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y,
2708 image->rows);
2709 if (proceed == MagickFalse)
2710 status=MagickFalse;
2711 }
2712 image_view=DestroyCacheView(image_view);
2713 return(status);
2714}
2715
cristy3ed852e2009-09-05 21:47:34 +00002716MagickExport MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002717 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002718{
2719 CubeInfo
2720 *cube_info;
2721
2722 MagickBooleanType
2723 status;
2724
cristybb503372010-05-27 20:51:26 +00002725 size_t
cristy3ed852e2009-09-05 21:47:34 +00002726 depth,
2727 maximum_colors;
2728
2729 assert(quantize_info != (const QuantizeInfo *) NULL);
2730 assert(quantize_info->signature == MagickSignature);
2731 assert(image != (Image *) NULL);
2732 assert(image->signature == MagickSignature);
2733 if (image->debug != MagickFalse)
2734 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2735 maximum_colors=quantize_info->number_colors;
2736 if (maximum_colors == 0)
2737 maximum_colors=MaxColormapSize;
2738 if (maximum_colors > MaxColormapSize)
2739 maximum_colors=MaxColormapSize;
cristy5f7dca62011-08-12 12:38:05 +00002740 if ((image->columns*image->rows) <= maximum_colors)
cristy8a11cb12011-10-19 23:53:34 +00002741 (void) DirectToColormapImage(image,exception);
2742 if ((IsImageGray(image,exception) != MagickFalse) &&
cristy8e752752011-04-16 13:48:22 +00002743 (image->matte == MagickFalse))
cristy018f07f2011-09-04 21:15:19 +00002744 (void) SetGrayscaleImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002745 if ((image->storage_class == PseudoClass) &&
2746 (image->colors <= maximum_colors))
2747 return(MagickTrue);
2748 depth=quantize_info->tree_depth;
2749 if (depth == 0)
2750 {
cristybb503372010-05-27 20:51:26 +00002751 size_t
cristy3ed852e2009-09-05 21:47:34 +00002752 colors;
2753
2754 /*
2755 Depth of color tree is: Log4(colormap size)+2.
2756 */
2757 colors=maximum_colors;
2758 for (depth=1; colors != 0; depth++)
2759 colors>>=2;
2760 if ((quantize_info->dither != MagickFalse) && (depth > 2))
2761 depth--;
2762 if ((image->matte != MagickFalse) && (depth > 5))
2763 depth--;
2764 }
2765 /*
2766 Initialize color cube.
2767 */
2768 cube_info=GetCubeInfo(quantize_info,depth,maximum_colors);
2769 if (cube_info == (CubeInfo *) NULL)
2770 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2771 image->filename);
cristy8a11cb12011-10-19 23:53:34 +00002772 status=ClassifyImageColors(cube_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002773 if (status != MagickFalse)
2774 {
2775 /*
2776 Reduce the number of colors in the image.
2777 */
2778 ReduceImageColors(image,cube_info);
cristy018f07f2011-09-04 21:15:19 +00002779 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00002780 }
2781 DestroyCubeInfo(cube_info);
2782 return(status);
2783}
2784
2785/*
2786%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2787% %
2788% %
2789% %
2790% Q u a n t i z e I m a g e s %
2791% %
2792% %
2793% %
2794%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2795%
2796% QuantizeImages() analyzes the colors within a set of reference images and
2797% chooses a fixed number of colors to represent the set. The goal of the
2798% algorithm is to minimize the color difference between the input and output
2799% images while minimizing the processing time.
2800%
2801% The format of the QuantizeImages method is:
2802%
2803% MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002804% Image *images,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002805%
2806% A description of each parameter follows:
2807%
2808% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
2809%
2810% o images: Specifies a pointer to a list of Image structures.
2811%
cristy018f07f2011-09-04 21:15:19 +00002812% o exception: return any errors or warnings in this structure.
2813%
cristy3ed852e2009-09-05 21:47:34 +00002814*/
2815MagickExport MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002816 Image *images,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002817{
2818 CubeInfo
2819 *cube_info;
2820
2821 Image
2822 *image;
2823
2824 MagickBooleanType
2825 proceed,
2826 status;
2827
2828 MagickProgressMonitor
2829 progress_monitor;
2830
cristybb503372010-05-27 20:51:26 +00002831 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002832 i;
2833
cristybb503372010-05-27 20:51:26 +00002834 size_t
cristy3ed852e2009-09-05 21:47:34 +00002835 depth,
2836 maximum_colors,
2837 number_images;
2838
2839 assert(quantize_info != (const QuantizeInfo *) NULL);
2840 assert(quantize_info->signature == MagickSignature);
2841 assert(images != (Image *) NULL);
2842 assert(images->signature == MagickSignature);
2843 if (images->debug != MagickFalse)
2844 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
2845 if (GetNextImageInList(images) == (Image *) NULL)
2846 {
2847 /*
2848 Handle a single image with QuantizeImage.
2849 */
cristy018f07f2011-09-04 21:15:19 +00002850 status=QuantizeImage(quantize_info,images,exception);
cristy3ed852e2009-09-05 21:47:34 +00002851 return(status);
2852 }
2853 status=MagickFalse;
2854 maximum_colors=quantize_info->number_colors;
2855 if (maximum_colors == 0)
2856 maximum_colors=MaxColormapSize;
2857 if (maximum_colors > MaxColormapSize)
2858 maximum_colors=MaxColormapSize;
2859 depth=quantize_info->tree_depth;
2860 if (depth == 0)
2861 {
cristybb503372010-05-27 20:51:26 +00002862 size_t
cristy3ed852e2009-09-05 21:47:34 +00002863 colors;
2864
2865 /*
2866 Depth of color tree is: Log4(colormap size)+2.
2867 */
2868 colors=maximum_colors;
2869 for (depth=1; colors != 0; depth++)
2870 colors>>=2;
2871 if (quantize_info->dither != MagickFalse)
2872 depth--;
2873 }
2874 /*
2875 Initialize color cube.
2876 */
2877 cube_info=GetCubeInfo(quantize_info,depth,maximum_colors);
2878 if (cube_info == (CubeInfo *) NULL)
2879 {
cristy8a11cb12011-10-19 23:53:34 +00002880 (void) ThrowMagickException(exception,GetMagickModule(),
anthonye5b39652012-04-21 05:37:29 +00002881 ResourceLimitError,"MemoryAllocationFailed","'%s'",images->filename);
cristy3ed852e2009-09-05 21:47:34 +00002882 return(MagickFalse);
2883 }
2884 number_images=GetImageListLength(images);
2885 image=images;
2886 for (i=0; image != (Image *) NULL; i++)
2887 {
2888 progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor) NULL,
2889 image->client_data);
cristy8a11cb12011-10-19 23:53:34 +00002890 status=ClassifyImageColors(cube_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002891 if (status == MagickFalse)
2892 break;
2893 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
cristycee97112010-05-28 00:44:52 +00002894 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i,
2895 number_images);
cristy3ed852e2009-09-05 21:47:34 +00002896 if (proceed == MagickFalse)
2897 break;
2898 image=GetNextImageInList(image);
2899 }
2900 if (status != MagickFalse)
2901 {
2902 /*
2903 Reduce the number of colors in an image sequence.
2904 */
2905 ReduceImageColors(images,cube_info);
2906 image=images;
2907 for (i=0; image != (Image *) NULL; i++)
2908 {
2909 progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor)
2910 NULL,image->client_data);
cristy018f07f2011-09-04 21:15:19 +00002911 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00002912 if (status == MagickFalse)
2913 break;
2914 (void) SetImageProgressMonitor(image,progress_monitor,
2915 image->client_data);
cristycee97112010-05-28 00:44:52 +00002916 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i,
2917 number_images);
cristy3ed852e2009-09-05 21:47:34 +00002918 if (proceed == MagickFalse)
2919 break;
2920 image=GetNextImageInList(image);
2921 }
2922 }
2923 DestroyCubeInfo(cube_info);
2924 return(status);
2925}
2926
2927/*
2928%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2929% %
2930% %
2931% %
2932+ R e d u c e %
2933% %
2934% %
2935% %
2936%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2937%
2938% Reduce() traverses the color cube tree and prunes any node whose
2939% quantization error falls below a particular threshold.
2940%
2941% The format of the Reduce method is:
2942%
2943% Reduce(const Image *image,CubeInfo *cube_info,const NodeInfo *node_info)
2944%
2945% A description of each parameter follows.
2946%
2947% o image: the image.
2948%
2949% o cube_info: A pointer to the Cube structure.
2950%
2951% o node_info: pointer to node in color cube tree that is to be pruned.
2952%
2953*/
2954static void Reduce(const Image *image,CubeInfo *cube_info,
2955 const NodeInfo *node_info)
2956{
cristybb503372010-05-27 20:51:26 +00002957 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002958 i;
2959
cristybb503372010-05-27 20:51:26 +00002960 size_t
cristy3ed852e2009-09-05 21:47:34 +00002961 number_children;
2962
2963 /*
2964 Traverse any children.
2965 */
2966 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002967 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002968 if (node_info->child[i] != (NodeInfo *) NULL)
2969 Reduce(image,cube_info,node_info->child[i]);
2970 if (node_info->quantize_error <= cube_info->pruning_threshold)
2971 PruneChild(image,cube_info,node_info);
2972 else
2973 {
2974 /*
2975 Find minimum pruning threshold.
2976 */
2977 if (node_info->number_unique > 0)
2978 cube_info->colors++;
2979 if (node_info->quantize_error < cube_info->next_threshold)
2980 cube_info->next_threshold=node_info->quantize_error;
2981 }
2982}
2983
2984/*
2985%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2986% %
2987% %
2988% %
2989+ R e d u c e I m a g e C o l o r s %
2990% %
2991% %
2992% %
2993%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2994%
2995% ReduceImageColors() repeatedly prunes the tree until the number of nodes
2996% with n2 > 0 is less than or equal to the maximum number of colors allowed
2997% in the output image. On any given iteration over the tree, it selects
2998% those nodes whose E value is minimal for pruning and merges their
2999% color statistics upward. It uses a pruning threshold, Ep, to govern
3000% node selection as follows:
3001%
3002% Ep = 0
3003% while number of nodes with (n2 > 0) > required maximum number of colors
3004% prune all nodes such that E <= Ep
3005% Set Ep to minimum E in remaining nodes
3006%
3007% This has the effect of minimizing any quantization error when merging
3008% two nodes together.
3009%
3010% When a node to be pruned has offspring, the pruning procedure invokes
3011% itself recursively in order to prune the tree from the leaves upward.
3012% n2, Sr, Sg, and Sb in a node being pruned are always added to the
3013% corresponding data in that node's parent. This retains the pruned
3014% node's color characteristics for later averaging.
3015%
3016% For each node, n2 pixels exist for which that node represents the
3017% smallest volume in RGB space containing those pixel's colors. When n2
3018% > 0 the node will uniquely define a color in the output image. At the
3019% beginning of reduction, n2 = 0 for all nodes except a the leaves of
3020% the tree which represent colors present in the input image.
3021%
3022% The other pixel count, n1, indicates the total number of colors
3023% within the cubic volume which the node represents. This includes n1 -
3024% n2 pixels whose colors should be defined by nodes at a lower level in
3025% the tree.
3026%
3027% The format of the ReduceImageColors method is:
3028%
3029% ReduceImageColors(const Image *image,CubeInfo *cube_info)
3030%
3031% A description of each parameter follows.
3032%
3033% o image: the image.
3034%
3035% o cube_info: A pointer to the Cube structure.
3036%
3037*/
3038static void ReduceImageColors(const Image *image,CubeInfo *cube_info)
3039{
3040#define ReduceImageTag "Reduce/Image"
3041
3042 MagickBooleanType
3043 proceed;
3044
3045 MagickOffsetType
3046 offset;
3047
cristybb503372010-05-27 20:51:26 +00003048 size_t
cristy3ed852e2009-09-05 21:47:34 +00003049 span;
3050
3051 cube_info->next_threshold=0.0;
3052 for (span=cube_info->colors; cube_info->colors > cube_info->maximum_colors; )
3053 {
3054 cube_info->pruning_threshold=cube_info->next_threshold;
3055 cube_info->next_threshold=cube_info->root->quantize_error-1;
3056 cube_info->colors=0;
3057 Reduce(image,cube_info,cube_info->root);
3058 offset=(MagickOffsetType) span-cube_info->colors;
3059 proceed=SetImageProgress(image,ReduceImageTag,offset,span-
3060 cube_info->maximum_colors+1);
3061 if (proceed == MagickFalse)
3062 break;
3063 }
3064}
3065
3066/*
3067%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3068% %
3069% %
3070% %
3071% R e m a p I m a g e %
3072% %
3073% %
3074% %
3075%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3076%
anthony31f1bf72012-01-30 12:37:22 +00003077% RemapImage() replaces the colors of an image with a dither of the colors
3078% provided.
cristy3ed852e2009-09-05 21:47:34 +00003079%
3080% The format of the RemapImage method is:
3081%
3082% MagickBooleanType RemapImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003083% Image *image,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003084%
3085% A description of each parameter follows:
3086%
3087% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
3088%
3089% o image: the image.
3090%
3091% o remap_image: the reference image.
3092%
cristy018f07f2011-09-04 21:15:19 +00003093% o exception: return any errors or warnings in this structure.
3094%
cristy3ed852e2009-09-05 21:47:34 +00003095*/
3096MagickExport MagickBooleanType RemapImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003097 Image *image,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003098{
3099 CubeInfo
3100 *cube_info;
3101
3102 MagickBooleanType
3103 status;
3104
3105 /*
3106 Initialize color cube.
3107 */
3108 assert(image != (Image *) NULL);
3109 assert(image->signature == MagickSignature);
3110 if (image->debug != MagickFalse)
3111 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3112 assert(remap_image != (Image *) NULL);
3113 assert(remap_image->signature == MagickSignature);
3114 cube_info=GetCubeInfo(quantize_info,MaxTreeDepth,
3115 quantize_info->number_colors);
3116 if (cube_info == (CubeInfo *) NULL)
3117 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3118 image->filename);
cristy8a11cb12011-10-19 23:53:34 +00003119 status=ClassifyImageColors(cube_info,remap_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003120 if (status != MagickFalse)
3121 {
3122 /*
3123 Classify image colors from the reference image.
3124 */
3125 cube_info->quantize_info->number_colors=cube_info->colors;
cristy018f07f2011-09-04 21:15:19 +00003126 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003127 }
3128 DestroyCubeInfo(cube_info);
3129 return(status);
3130}
3131
3132/*
3133%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3134% %
3135% %
3136% %
3137% R e m a p I m a g e s %
3138% %
3139% %
3140% %
3141%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3142%
3143% RemapImages() replaces the colors of a sequence of images with the
3144% closest color from a reference image.
3145%
3146% The format of the RemapImage method is:
3147%
3148% MagickBooleanType RemapImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003149% Image *images,Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003150%
3151% A description of each parameter follows:
3152%
3153% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
3154%
3155% o images: the image sequence.
3156%
3157% o remap_image: the reference image.
3158%
cristy018f07f2011-09-04 21:15:19 +00003159% o exception: return any errors or warnings in this structure.
3160%
cristy3ed852e2009-09-05 21:47:34 +00003161*/
3162MagickExport MagickBooleanType RemapImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003163 Image *images,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003164{
3165 CubeInfo
3166 *cube_info;
3167
3168 Image
3169 *image;
3170
3171 MagickBooleanType
3172 status;
3173
3174 assert(images != (Image *) NULL);
3175 assert(images->signature == MagickSignature);
3176 if (images->debug != MagickFalse)
3177 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
3178 image=images;
3179 if (remap_image == (Image *) NULL)
3180 {
3181 /*
3182 Create a global colormap for an image sequence.
3183 */
cristy018f07f2011-09-04 21:15:19 +00003184 status=QuantizeImages(quantize_info,images,exception);
cristy3ed852e2009-09-05 21:47:34 +00003185 return(status);
3186 }
3187 /*
3188 Classify image colors from the reference image.
3189 */
3190 cube_info=GetCubeInfo(quantize_info,MaxTreeDepth,
3191 quantize_info->number_colors);
3192 if (cube_info == (CubeInfo *) NULL)
3193 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3194 image->filename);
cristy018f07f2011-09-04 21:15:19 +00003195 status=ClassifyImageColors(cube_info,remap_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003196 if (status != MagickFalse)
3197 {
3198 /*
3199 Classify image colors from the reference image.
3200 */
3201 cube_info->quantize_info->number_colors=cube_info->colors;
3202 image=images;
3203 for ( ; image != (Image *) NULL; image=GetNextImageInList(image))
3204 {
cristy018f07f2011-09-04 21:15:19 +00003205 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003206 if (status == MagickFalse)
3207 break;
3208 }
3209 }
3210 DestroyCubeInfo(cube_info);
3211 return(status);
3212}
3213
3214/*
3215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3216% %
3217% %
3218% %
3219% S e t G r a y s c a l e I m a g e %
3220% %
3221% %
3222% %
3223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3224%
3225% SetGrayscaleImage() converts an image to a PseudoClass grayscale image.
3226%
3227% The format of the SetGrayscaleImage method is:
3228%
cristy018f07f2011-09-04 21:15:19 +00003229% MagickBooleanType SetGrayscaleImage(Image *image,ExceptionInfo *exeption)
cristy3ed852e2009-09-05 21:47:34 +00003230%
3231% A description of each parameter follows:
3232%
3233% o image: The image.
3234%
cristy018f07f2011-09-04 21:15:19 +00003235% o exception: return any errors or warnings in this structure.
3236%
cristy3ed852e2009-09-05 21:47:34 +00003237*/
3238
3239#if defined(__cplusplus) || defined(c_plusplus)
3240extern "C" {
3241#endif
3242
3243static int IntensityCompare(const void *x,const void *y)
3244{
cristy101ab702011-10-13 13:06:32 +00003245 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003246 *color_1,
3247 *color_2;
3248
cristyecc31b12011-02-13 00:32:29 +00003249 ssize_t
3250 intensity;
3251
cristy101ab702011-10-13 13:06:32 +00003252 color_1=(PixelInfo *) x;
3253 color_2=(PixelInfo *) y;
3254 intensity=GetPixelInfoIntensity(color_1)-(ssize_t)
3255 GetPixelInfoIntensity(color_2);
cristycee97112010-05-28 00:44:52 +00003256 return((int) intensity);
cristy3ed852e2009-09-05 21:47:34 +00003257}
3258
3259#if defined(__cplusplus) || defined(c_plusplus)
3260}
3261#endif
3262
cristy018f07f2011-09-04 21:15:19 +00003263static MagickBooleanType SetGrayscaleImage(Image *image,
3264 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003265{
cristyc4c8d132010-01-07 01:58:38 +00003266 CacheView
3267 *image_view;
3268
cristyecc31b12011-02-13 00:32:29 +00003269 MagickBooleanType
3270 status;
cristy3ed852e2009-09-05 21:47:34 +00003271
cristy101ab702011-10-13 13:06:32 +00003272 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003273 *colormap;
3274
cristybb503372010-05-27 20:51:26 +00003275 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003276 i;
3277
cristyecc31b12011-02-13 00:32:29 +00003278 ssize_t
3279 *colormap_index,
3280 j,
3281 y;
cristy3ed852e2009-09-05 21:47:34 +00003282
cristy3ed852e2009-09-05 21:47:34 +00003283 assert(image != (Image *) NULL);
3284 assert(image->signature == MagickSignature);
3285 if (image->type != GrayscaleType)
cristye941a752011-10-15 01:52:48 +00003286 (void) TransformImageColorspace(image,GRAYColorspace,exception);
cristybb503372010-05-27 20:51:26 +00003287 colormap_index=(ssize_t *) AcquireQuantumMemory(MaxMap+1,
cristy3ed852e2009-09-05 21:47:34 +00003288 sizeof(*colormap_index));
cristybb503372010-05-27 20:51:26 +00003289 if (colormap_index == (ssize_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003290 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3291 image->filename);
3292 if (image->storage_class != PseudoClass)
3293 {
cristybb503372010-05-27 20:51:26 +00003294 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy3ed852e2009-09-05 21:47:34 +00003295 colormap_index[i]=(-1);
cristy018f07f2011-09-04 21:15:19 +00003296 if (AcquireImageColormap(image,MaxMap+1,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003297 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3298 image->filename);
3299 image->colors=0;
3300 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00003301 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003302#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003303 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003304#endif
cristybb503372010-05-27 20:51:26 +00003305 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003306 {
cristy4c08aed2011-07-01 19:47:50 +00003307 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003308 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003309
cristyecc31b12011-02-13 00:32:29 +00003310 register ssize_t
3311 x;
3312
cristy3ed852e2009-09-05 21:47:34 +00003313 if (status == MagickFalse)
3314 continue;
3315 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3316 exception);
cristyacd2ed22011-08-30 01:44:23 +00003317 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003318 {
3319 status=MagickFalse;
3320 continue;
3321 }
cristybb503372010-05-27 20:51:26 +00003322 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003323 {
cristybb503372010-05-27 20:51:26 +00003324 register size_t
cristy3ed852e2009-09-05 21:47:34 +00003325 intensity;
3326
cristy4c08aed2011-07-01 19:47:50 +00003327 intensity=ScaleQuantumToMap(GetPixelRed(image,q));
cristy3ed852e2009-09-05 21:47:34 +00003328 if (colormap_index[intensity] < 0)
3329 {
cristyb5d5f722009-11-04 03:03:49 +00003330#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003331 #pragma omp critical (MagickCore_SetGrayscaleImage)
3332#endif
3333 if (colormap_index[intensity] < 0)
3334 {
cristybb503372010-05-27 20:51:26 +00003335 colormap_index[intensity]=(ssize_t) image->colors;
cristye42f6582012-02-11 17:59:50 +00003336 image->colormap[image->colors].red=(double)
3337 GetPixelRed(image,q);
3338 image->colormap[image->colors].green=(double)
3339 GetPixelGreen(image,q);
3340 image->colormap[image->colors].blue=(double)
3341 GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00003342 image->colors++;
3343 }
3344 }
cristy4c08aed2011-07-01 19:47:50 +00003345 SetPixelIndex(image,(Quantum)
3346 colormap_index[intensity],q);
cristyed231572011-07-14 02:18:59 +00003347 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003348 }
3349 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3350 status=MagickFalse;
3351 }
3352 image_view=DestroyCacheView(image_view);
3353 }
cristybb503372010-05-27 20:51:26 +00003354 for (i=0; i < (ssize_t) image->colors; i++)
cristye42f6582012-02-11 17:59:50 +00003355 image->colormap[i].alpha=(double) i;
cristy101ab702011-10-13 13:06:32 +00003356 qsort((void *) image->colormap,image->colors,sizeof(PixelInfo),
cristy3ed852e2009-09-05 21:47:34 +00003357 IntensityCompare);
cristy101ab702011-10-13 13:06:32 +00003358 colormap=(PixelInfo *) AcquireQuantumMemory(image->colors,
cristy3ed852e2009-09-05 21:47:34 +00003359 sizeof(*colormap));
cristy101ab702011-10-13 13:06:32 +00003360 if (colormap == (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003361 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3362 image->filename);
3363 j=0;
3364 colormap[j]=image->colormap[0];
cristybb503372010-05-27 20:51:26 +00003365 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003366 {
cristy101ab702011-10-13 13:06:32 +00003367 if (IsPixelInfoEquivalent(&colormap[j],&image->colormap[i]) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003368 {
3369 j++;
3370 colormap[j]=image->colormap[i];
3371 }
cristy4c08aed2011-07-01 19:47:50 +00003372 colormap_index[(ssize_t) image->colormap[i].alpha]=j;
cristy3ed852e2009-09-05 21:47:34 +00003373 }
cristybb503372010-05-27 20:51:26 +00003374 image->colors=(size_t) (j+1);
cristy101ab702011-10-13 13:06:32 +00003375 image->colormap=(PixelInfo *) RelinquishMagickMemory(image->colormap);
cristy3ed852e2009-09-05 21:47:34 +00003376 image->colormap=colormap;
3377 status=MagickTrue;
cristydb070952012-04-20 14:33:00 +00003378 image_view=AcquireAuthenticCacheView(image,exception);
cristyb5d5f722009-11-04 03:03:49 +00003379#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003380 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003381#endif
cristybb503372010-05-27 20:51:26 +00003382 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003383 {
cristy4c08aed2011-07-01 19:47:50 +00003384 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003385 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003386
cristyecc31b12011-02-13 00:32:29 +00003387 register ssize_t
3388 x;
3389
cristy3ed852e2009-09-05 21:47:34 +00003390 if (status == MagickFalse)
3391 continue;
3392 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003393 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003394 {
3395 status=MagickFalse;
3396 continue;
3397 }
cristybb503372010-05-27 20:51:26 +00003398 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00003399 {
3400 SetPixelIndex(image,(Quantum) colormap_index[ScaleQuantumToMap(
3401 GetPixelIndex(image,q))],q);
cristyed231572011-07-14 02:18:59 +00003402 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00003403 }
cristy3ed852e2009-09-05 21:47:34 +00003404 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3405 status=MagickFalse;
3406 }
3407 image_view=DestroyCacheView(image_view);
cristybb503372010-05-27 20:51:26 +00003408 colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index);
cristy3ed852e2009-09-05 21:47:34 +00003409 image->type=GrayscaleType;
cristy8a11cb12011-10-19 23:53:34 +00003410 if (IsImageMonochrome(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003411 image->type=BilevelType;
3412 return(status);
3413}