blob: 1c657456fad73c797f3799188bbe26d59f23b373 [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 ExceptionInfo
539 *exception;
540
541 MagickBooleanType
542 status;
543
544 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +0000545 image_view=AcquireCacheView(image);
cristye9717ac2011-02-20 16:17:17 +0000546#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +0000547 #pragma omp parallel for schedule(static,4) shared(status)
cristye9717ac2011-02-20 16:17:17 +0000548#endif
cristybb503372010-05-27 20:51:26 +0000549 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000550 {
cristye9717ac2011-02-20 16:17:17 +0000551 CubeInfo
552 cube;
553
cristy4c08aed2011-07-01 19:47:50 +0000554 register Quantum
cristyc47d1f82009-11-26 01:44:43 +0000555 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000556
cristye9717ac2011-02-20 16:17:17 +0000557 register ssize_t
558 x;
559
560 ssize_t
561 count;
562
563 if (status == MagickFalse)
564 continue;
cristy3ed852e2009-09-05 21:47:34 +0000565 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
566 exception);
cristyacd2ed22011-08-30 01:44:23 +0000567 if (q == (Quantum *) NULL)
cristye9717ac2011-02-20 16:17:17 +0000568 {
569 status=MagickFalse;
570 continue;
571 }
cristye9717ac2011-02-20 16:17:17 +0000572 cube=(*cube_info);
cristybb503372010-05-27 20:51:26 +0000573 for (x=0; x < (ssize_t) image->columns; x+=count)
cristy3ed852e2009-09-05 21:47:34 +0000574 {
cristy101ab702011-10-13 13:06:32 +0000575 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +0000576 pixel;
577
578 register const NodeInfo
579 *node_info;
580
581 register ssize_t
582 i;
583
584 size_t
585 id,
586 index;
587
cristy3ed852e2009-09-05 21:47:34 +0000588 /*
589 Identify the deepest node containing the pixel's color.
590 */
cristybb503372010-05-27 20:51:26 +0000591 for (count=1; (x+count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000592 {
cristy101ab702011-10-13 13:06:32 +0000593 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000594 packet;
595
cristy101ab702011-10-13 13:06:32 +0000596 GetPixelInfoPixel(image,q+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000597 if (IsPixelEquivalent(image,q,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000598 break;
cristy4c08aed2011-07-01 19:47:50 +0000599 }
600 AssociateAlphaPixel(image,&cube,q,&pixel);
cristye9717ac2011-02-20 16:17:17 +0000601 node_info=cube.root;
cristybb503372010-05-27 20:51:26 +0000602 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +0000603 {
cristye9717ac2011-02-20 16:17:17 +0000604 id=ColorToNodeId(&cube,&pixel,index);
cristy3ed852e2009-09-05 21:47:34 +0000605 if (node_info->child[id] == (NodeInfo *) NULL)
606 break;
607 node_info=node_info->child[id];
608 }
609 /*
610 Find closest color among siblings and their children.
611 */
cristye9717ac2011-02-20 16:17:17 +0000612 cube.target=pixel;
613 cube.distance=(MagickRealType) (4.0*(QuantumRange+1.0)*
cristy3ed852e2009-09-05 21:47:34 +0000614 (QuantumRange+1.0)+1.0);
cristye9717ac2011-02-20 16:17:17 +0000615 ClosestColor(image,&cube,node_info->parent);
616 index=cube.color_number;
cristybb503372010-05-27 20:51:26 +0000617 for (i=0; i < (ssize_t) count; i++)
cristy3ed852e2009-09-05 21:47:34 +0000618 {
619 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +0000620 SetPixelIndex(image,(Quantum) index,q);
cristye9717ac2011-02-20 16:17:17 +0000621 if (cube.quantize_info->measure_error == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000622 {
cristye42f6582012-02-11 17:59:50 +0000623 SetPixelRed(image,ClampToQuantum(
624 image->colormap[index].red),q);
625 SetPixelGreen(image,ClampToQuantum(
626 image->colormap[index].green),q);
627 SetPixelBlue(image,ClampToQuantum(
628 image->colormap[index].blue),q);
cristye9717ac2011-02-20 16:17:17 +0000629 if (cube.associate_alpha != MagickFalse)
cristye42f6582012-02-11 17:59:50 +0000630 SetPixelAlpha(image,ClampToQuantum(
631 image->colormap[index].alpha),q);
cristy3ed852e2009-09-05 21:47:34 +0000632 }
cristyed231572011-07-14 02:18:59 +0000633 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000634 }
635 }
636 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristye9717ac2011-02-20 16:17:17 +0000637 status=MagickFalse;
638 if (image->progress_monitor != (MagickProgressMonitor) NULL)
639 {
640 MagickBooleanType
641 proceed;
642
643#if defined(MAGICKCORE_OPENMP_SUPPORT)
644 #pragma omp critical (MagickCore_AssignImageColors)
645#endif
646 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y,
647 image->rows);
648 if (proceed == MagickFalse)
649 status=MagickFalse;
650 }
cristy3ed852e2009-09-05 21:47:34 +0000651 }
652 image_view=DestroyCacheView(image_view);
653 }
654 if (cube_info->quantize_info->measure_error != MagickFalse)
cristy8a11cb12011-10-19 23:53:34 +0000655 (void) GetImageQuantizeError(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000656 if ((cube_info->quantize_info->number_colors == 2) &&
657 (cube_info->quantize_info->colorspace == GRAYColorspace))
658 {
cristye42f6582012-02-11 17:59:50 +0000659 double
cristy3ed852e2009-09-05 21:47:34 +0000660 intensity;
661
cristy101ab702011-10-13 13:06:32 +0000662 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +0000663 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000664
cristye9717ac2011-02-20 16:17:17 +0000665 register ssize_t
666 i;
667
cristy3ed852e2009-09-05 21:47:34 +0000668 /*
669 Monochrome image.
670 */
671 q=image->colormap;
cristybb503372010-05-27 20:51:26 +0000672 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000673 {
cristye42f6582012-02-11 17:59:50 +0000674 intensity=(double) ((MagickRealType) GetPixelInfoIntensity(q) <
cristy4c08aed2011-07-01 19:47:50 +0000675 ((MagickRealType) QuantumRange/2.0) ? 0 : QuantumRange);
676 q->red=intensity;
677 q->green=intensity;
678 q->blue=intensity;
cristy3ed852e2009-09-05 21:47:34 +0000679 q++;
680 }
681 }
cristyea1a8aa2011-10-20 13:24:06 +0000682 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000683 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
684 (cube_info->quantize_info->colorspace != CMYKColorspace))
cristyc511e882012-04-16 21:11:14 +0000685 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000686 return(MagickTrue);
687}
688
689/*
690%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
691% %
692% %
693% %
694+ C l a s s i f y I m a g e C o l o r s %
695% %
696% %
697% %
698%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
699%
700% ClassifyImageColors() begins by initializing a color description tree
701% of sufficient depth to represent each possible input color in a leaf.
702% However, it is impractical to generate a fully-formed color
703% description tree in the storage_class phase for realistic values of
704% Cmax. If colors components in the input image are quantized to k-bit
705% precision, so that Cmax= 2k-1, the tree would need k levels below the
706% root node to allow representing each possible input color in a leaf.
707% This becomes prohibitive because the tree's total number of nodes is
708% 1 + sum(i=1,k,8k).
709%
710% A complete tree would require 19,173,961 nodes for k = 8, Cmax = 255.
711% Therefore, to avoid building a fully populated tree, QUANTIZE: (1)
712% Initializes data structures for nodes only as they are needed; (2)
713% Chooses a maximum depth for the tree as a function of the desired
714% number of colors in the output image (currently log2(colormap size)).
715%
716% For each pixel in the input image, storage_class scans downward from
717% the root of the color description tree. At each level of the tree it
718% identifies the single node which represents a cube in RGB space
719% containing It updates the following data for each such node:
720%
721% n1 : Number of pixels whose color is contained in the RGB cube
722% which this node represents;
723%
724% n2 : Number of pixels whose color is not represented in a node at
725% lower depth in the tree; initially, n2 = 0 for all nodes except
726% leaves of the tree.
727%
728% Sr, Sg, Sb : Sums of the red, green, and blue component values for
729% all pixels not classified at a lower depth. The combination of
730% these sums and n2 will ultimately characterize the mean color of a
731% set of pixels represented by this node.
732%
733% E: the distance squared in RGB space between each pixel contained
734% within a node and the nodes' center. This represents the quantization
735% error for a node.
736%
737% The format of the ClassifyImageColors() method is:
738%
739% MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
740% const Image *image,ExceptionInfo *exception)
741%
742% A description of each parameter follows.
743%
744% o cube_info: A pointer to the Cube structure.
745%
746% o image: the image.
747%
748*/
749
750static inline void SetAssociatedAlpha(const Image *image,CubeInfo *cube_info)
751{
752 MagickBooleanType
753 associate_alpha;
754
755 associate_alpha=image->matte;
756 if (cube_info->quantize_info->colorspace == TransparentColorspace)
757 associate_alpha=MagickFalse;
758 if ((cube_info->quantize_info->number_colors == 2) &&
759 (cube_info->quantize_info->colorspace == GRAYColorspace))
760 associate_alpha=MagickFalse;
761 cube_info->associate_alpha=associate_alpha;
762}
763
764static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
765 const Image *image,ExceptionInfo *exception)
766{
767#define ClassifyImageTag "Classify/Image"
768
cristyc4c8d132010-01-07 01:58:38 +0000769 CacheView
770 *image_view;
771
cristy3ed852e2009-09-05 21:47:34 +0000772 MagickBooleanType
773 proceed;
774
775 MagickRealType
776 bisect;
777
778 NodeInfo
779 *node_info;
780
cristy101ab702011-10-13 13:06:32 +0000781 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000782 error,
783 mid,
784 midpoint,
785 pixel;
786
787 size_t
cristyecc31b12011-02-13 00:32:29 +0000788 count,
cristy3ed852e2009-09-05 21:47:34 +0000789 id,
790 index,
791 level;
792
cristyecc31b12011-02-13 00:32:29 +0000793 ssize_t
794 y;
795
cristy3ed852e2009-09-05 21:47:34 +0000796 /*
797 Classify the first cube_info->maximum_colors colors to a tree depth of 8.
798 */
799 SetAssociatedAlpha(image,cube_info);
800 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
801 (cube_info->quantize_info->colorspace != CMYKColorspace))
802 (void) TransformImageColorspace((Image *) image,
cristye941a752011-10-15 01:52:48 +0000803 cube_info->quantize_info->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000804 else
805 if ((image->colorspace != GRAYColorspace) &&
806 (image->colorspace != CMYColorspace) &&
cristy501c5592012-04-18 12:45:09 +0000807 (IssRGBColorspace(image->colorspace) == MagickFalse))
cristyc511e882012-04-16 21:11:14 +0000808 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000809 midpoint.red=(MagickRealType) QuantumRange/2.0;
810 midpoint.green=(MagickRealType) QuantumRange/2.0;
811 midpoint.blue=(MagickRealType) QuantumRange/2.0;
cristy4c08aed2011-07-01 19:47:50 +0000812 midpoint.alpha=(MagickRealType) QuantumRange/2.0;
813 error.alpha=0.0;
cristy3ed852e2009-09-05 21:47:34 +0000814 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000815 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000816 {
cristy4c08aed2011-07-01 19:47:50 +0000817 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000818 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000819
cristybb503372010-05-27 20:51:26 +0000820 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000821 x;
822
823 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000824 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000825 break;
826 if (cube_info->nodes > MaxNodes)
827 {
828 /*
829 Prune one level if the color tree is too large.
830 */
831 PruneLevel(image,cube_info,cube_info->root);
832 cube_info->depth--;
833 }
cristybb503372010-05-27 20:51:26 +0000834 for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count)
cristy3ed852e2009-09-05 21:47:34 +0000835 {
836 /*
837 Start at the root and descend the color cube tree.
838 */
cristybb66d9c2010-10-09 01:40:31 +0000839 for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000840 {
cristy101ab702011-10-13 13:06:32 +0000841 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000842 packet;
843
cristy101ab702011-10-13 13:06:32 +0000844 GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000845 if (IsPixelEquivalent(image,p,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000846 break;
cristy4c08aed2011-07-01 19:47:50 +0000847 }
848 AssociateAlphaPixel(image,cube_info,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +0000849 index=MaxTreeDepth-1;
850 bisect=((MagickRealType) QuantumRange+1.0)/2.0;
851 mid=midpoint;
852 node_info=cube_info->root;
853 for (level=1; level <= MaxTreeDepth; level++)
854 {
855 bisect*=0.5;
856 id=ColorToNodeId(cube_info,&pixel,index);
857 mid.red+=(id & 1) != 0 ? bisect : -bisect;
858 mid.green+=(id & 2) != 0 ? bisect : -bisect;
859 mid.blue+=(id & 4) != 0 ? bisect : -bisect;
cristy4c08aed2011-07-01 19:47:50 +0000860 mid.alpha+=(id & 8) != 0 ? bisect : -bisect;
cristy3ed852e2009-09-05 21:47:34 +0000861 if (node_info->child[id] == (NodeInfo *) NULL)
862 {
863 /*
864 Set colors of new node to contain pixel.
865 */
866 node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info);
867 if (node_info->child[id] == (NodeInfo *) NULL)
868 (void) ThrowMagickException(exception,GetMagickModule(),
869 ResourceLimitError,"MemoryAllocationFailed","`%s'",
870 image->filename);
871 if (level == MaxTreeDepth)
872 cube_info->colors++;
873 }
874 /*
875 Approximate the quantization error represented by this node.
876 */
877 node_info=node_info->child[id];
878 error.red=QuantumScale*(pixel.red-mid.red);
879 error.green=QuantumScale*(pixel.green-mid.green);
880 error.blue=QuantumScale*(pixel.blue-mid.blue);
881 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000882 error.alpha=QuantumScale*(pixel.alpha-mid.alpha);
cristy3ed852e2009-09-05 21:47:34 +0000883 node_info->quantize_error+=sqrt((double) (count*error.red*error.red+
884 count*error.green*error.green+count*error.blue*error.blue+
cristy4c08aed2011-07-01 19:47:50 +0000885 count*error.alpha*error.alpha));
cristy3ed852e2009-09-05 21:47:34 +0000886 cube_info->root->quantize_error+=node_info->quantize_error;
887 index--;
888 }
889 /*
890 Sum RGB for this leaf for later derivation of the mean cube color.
891 */
892 node_info->number_unique+=count;
893 node_info->total_color.red+=count*QuantumScale*pixel.red;
894 node_info->total_color.green+=count*QuantumScale*pixel.green;
895 node_info->total_color.blue+=count*QuantumScale*pixel.blue;
896 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000897 node_info->total_color.alpha+=count*QuantumScale*pixel.alpha;
cristyed231572011-07-14 02:18:59 +0000898 p+=count*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000899 }
900 if (cube_info->colors > cube_info->maximum_colors)
901 {
902 PruneToCubeDepth(image,cube_info,cube_info->root);
903 break;
904 }
cristycee97112010-05-28 00:44:52 +0000905 proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y,
906 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000907 if (proceed == MagickFalse)
908 break;
909 }
cristybb503372010-05-27 20:51:26 +0000910 for (y++; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000911 {
cristy4c08aed2011-07-01 19:47:50 +0000912 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000913 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000914
cristybb503372010-05-27 20:51:26 +0000915 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000916 x;
917
918 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000919 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000920 break;
921 if (cube_info->nodes > MaxNodes)
922 {
923 /*
924 Prune one level if the color tree is too large.
925 */
926 PruneLevel(image,cube_info,cube_info->root);
927 cube_info->depth--;
928 }
cristybb503372010-05-27 20:51:26 +0000929 for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count)
cristy3ed852e2009-09-05 21:47:34 +0000930 {
931 /*
932 Start at the root and descend the color cube tree.
933 */
cristybb66d9c2010-10-09 01:40:31 +0000934 for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000935 {
cristy101ab702011-10-13 13:06:32 +0000936 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000937 packet;
938
cristy101ab702011-10-13 13:06:32 +0000939 GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000940 if (IsPixelEquivalent(image,p,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000941 break;
cristy4c08aed2011-07-01 19:47:50 +0000942 }
943 AssociateAlphaPixel(image,cube_info,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +0000944 index=MaxTreeDepth-1;
945 bisect=((MagickRealType) QuantumRange+1.0)/2.0;
946 mid=midpoint;
947 node_info=cube_info->root;
948 for (level=1; level <= cube_info->depth; level++)
949 {
950 bisect*=0.5;
951 id=ColorToNodeId(cube_info,&pixel,index);
952 mid.red+=(id & 1) != 0 ? bisect : -bisect;
953 mid.green+=(id & 2) != 0 ? bisect : -bisect;
954 mid.blue+=(id & 4) != 0 ? bisect : -bisect;
cristy4c08aed2011-07-01 19:47:50 +0000955 mid.alpha+=(id & 8) != 0 ? bisect : -bisect;
cristy3ed852e2009-09-05 21:47:34 +0000956 if (node_info->child[id] == (NodeInfo *) NULL)
957 {
958 /*
959 Set colors of new node to contain pixel.
960 */
961 node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info);
962 if (node_info->child[id] == (NodeInfo *) NULL)
963 (void) ThrowMagickException(exception,GetMagickModule(),
964 ResourceLimitError,"MemoryAllocationFailed","%s",
965 image->filename);
966 if (level == cube_info->depth)
967 cube_info->colors++;
968 }
969 /*
970 Approximate the quantization error represented by this node.
971 */
972 node_info=node_info->child[id];
973 error.red=QuantumScale*(pixel.red-mid.red);
974 error.green=QuantumScale*(pixel.green-mid.green);
975 error.blue=QuantumScale*(pixel.blue-mid.blue);
976 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000977 error.alpha=QuantumScale*(pixel.alpha-mid.alpha);
cristy3ed852e2009-09-05 21:47:34 +0000978 node_info->quantize_error+=sqrt((double) (count*error.red*error.red+
cristy83b6e792011-01-26 15:46:06 +0000979 count*error.green*error.green+count*error.blue*error.blue+
cristy4c08aed2011-07-01 19:47:50 +0000980 count*error.alpha*error.alpha));
cristy3ed852e2009-09-05 21:47:34 +0000981 cube_info->root->quantize_error+=node_info->quantize_error;
982 index--;
983 }
984 /*
985 Sum RGB for this leaf for later derivation of the mean cube color.
986 */
987 node_info->number_unique+=count;
988 node_info->total_color.red+=count*QuantumScale*pixel.red;
989 node_info->total_color.green+=count*QuantumScale*pixel.green;
990 node_info->total_color.blue+=count*QuantumScale*pixel.blue;
991 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000992 node_info->total_color.alpha+=count*QuantumScale*pixel.alpha;
cristyed231572011-07-14 02:18:59 +0000993 p+=count*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000994 }
cristycee97112010-05-28 00:44:52 +0000995 proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y,
996 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000997 if (proceed == MagickFalse)
998 break;
999 }
1000 image_view=DestroyCacheView(image_view);
1001 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
1002 (cube_info->quantize_info->colorspace != CMYKColorspace))
cristyc511e882012-04-16 21:11:14 +00001003 (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001004 return(MagickTrue);
1005}
1006
1007/*
1008%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1009% %
1010% %
1011% %
1012% C l o n e Q u a n t i z e I n f o %
1013% %
1014% %
1015% %
1016%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1017%
1018% CloneQuantizeInfo() makes a duplicate of the given quantize info structure,
1019% or if quantize info is NULL, a new one.
1020%
1021% The format of the CloneQuantizeInfo method is:
1022%
1023% QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info)
1024%
1025% A description of each parameter follows:
1026%
1027% o clone_info: Method CloneQuantizeInfo returns a duplicate of the given
1028% quantize info, or if image info is NULL a new one.
1029%
1030% o quantize_info: a structure of type info.
1031%
1032*/
1033MagickExport QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info)
1034{
1035 QuantizeInfo
1036 *clone_info;
1037
cristy73bd4a52010-10-05 11:24:23 +00001038 clone_info=(QuantizeInfo *) AcquireMagickMemory(sizeof(*clone_info));
cristy3ed852e2009-09-05 21:47:34 +00001039 if (clone_info == (QuantizeInfo *) NULL)
1040 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1041 GetQuantizeInfo(clone_info);
1042 if (quantize_info == (QuantizeInfo *) NULL)
1043 return(clone_info);
1044 clone_info->number_colors=quantize_info->number_colors;
1045 clone_info->tree_depth=quantize_info->tree_depth;
1046 clone_info->dither=quantize_info->dither;
1047 clone_info->dither_method=quantize_info->dither_method;
1048 clone_info->colorspace=quantize_info->colorspace;
1049 clone_info->measure_error=quantize_info->measure_error;
1050 return(clone_info);
1051}
1052
1053/*
1054%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1055% %
1056% %
1057% %
1058+ C l o s e s t C o l o r %
1059% %
1060% %
1061% %
1062%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1063%
1064% ClosestColor() traverses the color cube tree at a particular node and
1065% determines which colormap entry best represents the input color.
1066%
1067% The format of the ClosestColor method is:
1068%
1069% void ClosestColor(const Image *image,CubeInfo *cube_info,
1070% const NodeInfo *node_info)
1071%
1072% A description of each parameter follows.
1073%
1074% o image: the image.
1075%
1076% o cube_info: A pointer to the Cube structure.
1077%
1078% o node_info: the address of a structure of type NodeInfo which points to a
1079% node in the color cube tree that is to be pruned.
1080%
1081*/
1082static void ClosestColor(const Image *image,CubeInfo *cube_info,
1083 const NodeInfo *node_info)
1084{
cristybb503372010-05-27 20:51:26 +00001085 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001086 i;
1087
cristybb503372010-05-27 20:51:26 +00001088 size_t
cristy3ed852e2009-09-05 21:47:34 +00001089 number_children;
1090
1091 /*
1092 Traverse any children.
1093 */
1094 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00001095 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00001096 if (node_info->child[i] != (NodeInfo *) NULL)
1097 ClosestColor(image,cube_info,node_info->child[i]);
1098 if (node_info->number_unique != 0)
1099 {
1100 MagickRealType
1101 pixel;
1102
1103 register MagickRealType
1104 alpha,
1105 beta,
1106 distance;
1107
cristy101ab702011-10-13 13:06:32 +00001108 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001109 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001110
cristy101ab702011-10-13 13:06:32 +00001111 register RealPixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001112 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001113
1114 /*
1115 Determine if this color is "closest".
1116 */
1117 p=image->colormap+node_info->color_number;
1118 q=(&cube_info->target);
1119 alpha=1.0;
1120 beta=1.0;
cristy847620f2011-02-09 02:24:21 +00001121 if (cube_info->associate_alpha != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001122 {
cristy4c08aed2011-07-01 19:47:50 +00001123 alpha=(MagickRealType) (QuantumScale*p->alpha);
1124 beta=(MagickRealType) (QuantumScale*q->alpha);
cristy3ed852e2009-09-05 21:47:34 +00001125 }
cristy4c08aed2011-07-01 19:47:50 +00001126 pixel=alpha*p->red-beta*q->red;
cristy3ed852e2009-09-05 21:47:34 +00001127 distance=pixel*pixel;
cristy36fbc3b2011-02-09 02:30:04 +00001128 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001129 {
cristy4c08aed2011-07-01 19:47:50 +00001130 pixel=alpha*p->green-beta*q->green;
cristy3ed852e2009-09-05 21:47:34 +00001131 distance+=pixel*pixel;
cristy36fbc3b2011-02-09 02:30:04 +00001132 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001133 {
cristy4c08aed2011-07-01 19:47:50 +00001134 pixel=alpha*p->blue-beta*q->blue;
cristy3ed852e2009-09-05 21:47:34 +00001135 distance+=pixel*pixel;
cristy36fbc3b2011-02-09 02:30:04 +00001136 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001137 {
1138 pixel=alpha-beta;
1139 distance+=pixel*pixel;
cristyc4080402011-02-09 02:55:58 +00001140 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001141 {
1142 cube_info->distance=distance;
1143 cube_info->color_number=node_info->color_number;
1144 }
1145 }
1146 }
1147 }
1148 }
1149}
1150
1151/*
1152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1153% %
1154% %
1155% %
1156% C o m p r e s s I m a g e C o l o r m a p %
1157% %
1158% %
1159% %
1160%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1161%
1162% CompressImageColormap() compresses an image colormap by removing any
1163% duplicate or unused color entries.
1164%
1165% The format of the CompressImageColormap method is:
1166%
cristy018f07f2011-09-04 21:15:19 +00001167% MagickBooleanType CompressImageColormap(Image *image,
1168% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001169%
1170% A description of each parameter follows:
1171%
1172% o image: the image.
1173%
cristy018f07f2011-09-04 21:15:19 +00001174% o exception: return any errors or warnings in this structure.
1175%
cristy3ed852e2009-09-05 21:47:34 +00001176*/
cristy018f07f2011-09-04 21:15:19 +00001177MagickExport MagickBooleanType CompressImageColormap(Image *image,
1178 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001179{
1180 QuantizeInfo
1181 quantize_info;
1182
1183 assert(image != (Image *) NULL);
1184 assert(image->signature == MagickSignature);
1185 if (image->debug != MagickFalse)
1186 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy8a11cb12011-10-19 23:53:34 +00001187 if (IsPaletteImage(image,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001188 return(MagickFalse);
1189 GetQuantizeInfo(&quantize_info);
1190 quantize_info.number_colors=image->colors;
1191 quantize_info.tree_depth=MaxTreeDepth;
cristy018f07f2011-09-04 21:15:19 +00001192 return(QuantizeImage(&quantize_info,image,exception));
cristy3ed852e2009-09-05 21:47:34 +00001193}
1194
1195/*
1196%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1197% %
1198% %
1199% %
1200+ D e f i n e I m a g e C o l o r m a p %
1201% %
1202% %
1203% %
1204%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1205%
1206% DefineImageColormap() traverses the color cube tree and notes each colormap
1207% entry. A colormap entry is any node in the color cube tree where the
1208% of unique colors is not zero. DefineImageColormap() returns the number of
1209% colors in the image colormap.
1210%
1211% The format of the DefineImageColormap method is:
1212%
cristybb503372010-05-27 20:51:26 +00001213% size_t DefineImageColormap(Image *image,CubeInfo *cube_info,
cristy3ed852e2009-09-05 21:47:34 +00001214% NodeInfo *node_info)
1215%
1216% A description of each parameter follows.
1217%
1218% o image: the image.
1219%
1220% o cube_info: A pointer to the Cube structure.
1221%
1222% o node_info: the address of a structure of type NodeInfo which points to a
1223% node in the color cube tree that is to be pruned.
1224%
1225*/
cristybb503372010-05-27 20:51:26 +00001226static size_t DefineImageColormap(Image *image,CubeInfo *cube_info,
cristy3ed852e2009-09-05 21:47:34 +00001227 NodeInfo *node_info)
1228{
cristybb503372010-05-27 20:51:26 +00001229 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001230 i;
1231
cristybb503372010-05-27 20:51:26 +00001232 size_t
cristy3ed852e2009-09-05 21:47:34 +00001233 number_children;
1234
1235 /*
1236 Traverse any children.
1237 */
1238 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00001239 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00001240 if (node_info->child[i] != (NodeInfo *) NULL)
cristycee97112010-05-28 00:44:52 +00001241 (void) DefineImageColormap(image,cube_info,node_info->child[i]);
cristy3ed852e2009-09-05 21:47:34 +00001242 if (node_info->number_unique != 0)
1243 {
1244 register MagickRealType
1245 alpha;
1246
cristy101ab702011-10-13 13:06:32 +00001247 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001248 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001249
1250 /*
1251 Colormap entry is defined by the mean color in this cube.
1252 */
1253 q=image->colormap+image->colors;
1254 alpha=(MagickRealType) ((MagickOffsetType) node_info->number_unique);
1255 alpha=1.0/(fabs(alpha) <= MagickEpsilon ? 1.0 : alpha);
1256 if (cube_info->associate_alpha == MagickFalse)
1257 {
cristye42f6582012-02-11 17:59:50 +00001258 q->red=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001259 (alpha*QuantumRange*node_info->total_color.red));
cristye42f6582012-02-11 17:59:50 +00001260 q->green=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001261 (alpha*QuantumRange*node_info->total_color.green));
cristye42f6582012-02-11 17:59:50 +00001262 q->blue=(double) ClampToQuantum((MagickRealType)
1263 (alpha*(double) QuantumRange*node_info->total_color.blue));
cristy4c08aed2011-07-01 19:47:50 +00001264 q->alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00001265 }
1266 else
1267 {
1268 MagickRealType
1269 opacity;
1270
1271 opacity=(MagickRealType) (alpha*QuantumRange*
cristy4c08aed2011-07-01 19:47:50 +00001272 node_info->total_color.alpha);
cristye42f6582012-02-11 17:59:50 +00001273 q->alpha=(double) ClampToQuantum(opacity);
cristy4c08aed2011-07-01 19:47:50 +00001274 if (q->alpha == OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00001275 {
cristye42f6582012-02-11 17:59:50 +00001276 q->red=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001277 (alpha*QuantumRange*node_info->total_color.red));
cristye42f6582012-02-11 17:59:50 +00001278 q->green=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001279 (alpha*QuantumRange*node_info->total_color.green));
cristye42f6582012-02-11 17:59:50 +00001280 q->blue=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001281 (alpha*QuantumRange*node_info->total_color.blue));
cristy3ed852e2009-09-05 21:47:34 +00001282 }
1283 else
1284 {
1285 MagickRealType
1286 gamma;
1287
cristy4c08aed2011-07-01 19:47:50 +00001288 gamma=(MagickRealType) (QuantumScale*q->alpha);
cristy3ed852e2009-09-05 21:47:34 +00001289 gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma);
cristye42f6582012-02-11 17:59:50 +00001290 q->red=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001291 (alpha*gamma*QuantumRange*node_info->total_color.red));
cristye42f6582012-02-11 17:59:50 +00001292 q->green=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001293 (alpha*gamma*QuantumRange*node_info->total_color.green));
cristye42f6582012-02-11 17:59:50 +00001294 q->blue=(double) ClampToQuantum((MagickRealType)
cristy4c08aed2011-07-01 19:47:50 +00001295 (alpha*gamma*QuantumRange*node_info->total_color.blue));
cristy3ed852e2009-09-05 21:47:34 +00001296 if (node_info->number_unique > cube_info->transparent_pixels)
1297 {
1298 cube_info->transparent_pixels=node_info->number_unique;
cristybb503372010-05-27 20:51:26 +00001299 cube_info->transparent_index=(ssize_t) image->colors;
cristy3ed852e2009-09-05 21:47:34 +00001300 }
1301 }
1302 }
1303 node_info->color_number=image->colors++;
1304 }
1305 return(image->colors);
1306}
1307
1308/*
1309%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1310% %
1311% %
1312% %
1313+ D e s t r o y C u b e I n f o %
1314% %
1315% %
1316% %
1317%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1318%
1319% DestroyCubeInfo() deallocates memory associated with an image.
1320%
1321% The format of the DestroyCubeInfo method is:
1322%
1323% DestroyCubeInfo(CubeInfo *cube_info)
1324%
1325% A description of each parameter follows:
1326%
1327% o cube_info: the address of a structure of type CubeInfo.
1328%
1329*/
1330static void DestroyCubeInfo(CubeInfo *cube_info)
1331{
1332 register Nodes
1333 *nodes;
1334
1335 /*
1336 Release color cube tree storage.
1337 */
1338 do
1339 {
1340 nodes=cube_info->node_queue->next;
1341 cube_info->node_queue->nodes=(NodeInfo *) RelinquishMagickMemory(
1342 cube_info->node_queue->nodes);
1343 cube_info->node_queue=(Nodes *) RelinquishMagickMemory(
1344 cube_info->node_queue);
1345 cube_info->node_queue=nodes;
1346 } while (cube_info->node_queue != (Nodes *) NULL);
cristybb503372010-05-27 20:51:26 +00001347 if (cube_info->cache != (ssize_t *) NULL)
1348 cube_info->cache=(ssize_t *) RelinquishMagickMemory(cube_info->cache);
cristy3ed852e2009-09-05 21:47:34 +00001349 cube_info->quantize_info=DestroyQuantizeInfo(cube_info->quantize_info);
1350 cube_info=(CubeInfo *) RelinquishMagickMemory(cube_info);
1351}
1352
1353/*
1354%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1355% %
1356% %
1357% %
1358% D e s t r o y Q u a n t i z e I n f o %
1359% %
1360% %
1361% %
1362%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1363%
1364% DestroyQuantizeInfo() deallocates memory associated with an QuantizeInfo
1365% structure.
1366%
1367% The format of the DestroyQuantizeInfo method is:
1368%
1369% QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info)
1370%
1371% A description of each parameter follows:
1372%
1373% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
1374%
1375*/
1376MagickExport QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info)
1377{
1378 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1379 assert(quantize_info != (QuantizeInfo *) NULL);
1380 assert(quantize_info->signature == MagickSignature);
1381 quantize_info->signature=(~MagickSignature);
1382 quantize_info=(QuantizeInfo *) RelinquishMagickMemory(quantize_info);
1383 return(quantize_info);
1384}
1385
1386/*
1387%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388% %
1389% %
1390% %
1391+ D i t h e r I m a g e %
1392% %
1393% %
1394% %
1395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396%
1397% DitherImage() distributes the difference between an original image and
1398% the corresponding color reduced algorithm to neighboring pixels using
1399% serpentine-scan Floyd-Steinberg error diffusion. DitherImage returns
1400% MagickTrue if the image is dithered otherwise MagickFalse.
1401%
1402% The format of the DitherImage method is:
1403%
cristy8a11cb12011-10-19 23:53:34 +00001404% MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info,
1405% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001406%
1407% A description of each parameter follows.
1408%
1409% o image: the image.
1410%
1411% o cube_info: A pointer to the Cube structure.
1412%
cristy8a11cb12011-10-19 23:53:34 +00001413% o exception: return any errors or warnings in this structure.
1414%
cristy3ed852e2009-09-05 21:47:34 +00001415*/
1416
cristy101ab702011-10-13 13:06:32 +00001417static RealPixelInfo **DestroyPixelThreadSet(RealPixelInfo **pixels)
cristye9717ac2011-02-20 16:17:17 +00001418{
1419 register ssize_t
1420 i;
1421
cristy101ab702011-10-13 13:06:32 +00001422 assert(pixels != (RealPixelInfo **) NULL);
cristye9717ac2011-02-20 16:17:17 +00001423 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy101ab702011-10-13 13:06:32 +00001424 if (pixels[i] != (RealPixelInfo *) NULL)
1425 pixels[i]=(RealPixelInfo *) RelinquishMagickMemory(pixels[i]);
1426 pixels=(RealPixelInfo **) RelinquishMagickMemory(pixels);
cristye9717ac2011-02-20 16:17:17 +00001427 return(pixels);
1428}
1429
cristy101ab702011-10-13 13:06:32 +00001430static RealPixelInfo **AcquirePixelThreadSet(const size_t count)
cristye9717ac2011-02-20 16:17:17 +00001431{
cristy101ab702011-10-13 13:06:32 +00001432 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001433 **pixels;
1434
1435 register ssize_t
1436 i;
1437
1438 size_t
1439 number_threads;
1440
1441 number_threads=GetOpenMPMaximumThreads();
cristy101ab702011-10-13 13:06:32 +00001442 pixels=(RealPixelInfo **) AcquireQuantumMemory(number_threads,
cristye9717ac2011-02-20 16:17:17 +00001443 sizeof(*pixels));
cristy101ab702011-10-13 13:06:32 +00001444 if (pixels == (RealPixelInfo **) NULL)
1445 return((RealPixelInfo **) NULL);
cristye9717ac2011-02-20 16:17:17 +00001446 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
1447 for (i=0; i < (ssize_t) number_threads; i++)
1448 {
cristy101ab702011-10-13 13:06:32 +00001449 pixels[i]=(RealPixelInfo *) AcquireQuantumMemory(count,
cristye9717ac2011-02-20 16:17:17 +00001450 2*sizeof(**pixels));
cristy101ab702011-10-13 13:06:32 +00001451 if (pixels[i] == (RealPixelInfo *) NULL)
cristye9717ac2011-02-20 16:17:17 +00001452 return(DestroyPixelThreadSet(pixels));
1453 }
1454 return(pixels);
1455}
1456
cristyca972de2010-06-20 23:37:02 +00001457static inline ssize_t CacheOffset(CubeInfo *cube_info,
cristy101ab702011-10-13 13:06:32 +00001458 const RealPixelInfo *pixel)
cristyca972de2010-06-20 23:37:02 +00001459{
1460#define RedShift(pixel) (((pixel) >> CacheShift) << (0*(8-CacheShift)))
1461#define GreenShift(pixel) (((pixel) >> CacheShift) << (1*(8-CacheShift)))
1462#define BlueShift(pixel) (((pixel) >> CacheShift) << (2*(8-CacheShift)))
1463#define AlphaShift(pixel) (((pixel) >> CacheShift) << (3*(8-CacheShift)))
1464
1465 ssize_t
1466 offset;
1467
1468 offset=(ssize_t)
cristy15893a42010-11-20 18:57:15 +00001469 (RedShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->red))) |
cristyca972de2010-06-20 23:37:02 +00001470 GreenShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->green))) |
cristy15893a42010-11-20 18:57:15 +00001471 BlueShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->blue))));
cristyca972de2010-06-20 23:37:02 +00001472 if (cube_info->associate_alpha != MagickFalse)
cristy15893a42010-11-20 18:57:15 +00001473 offset|=AlphaShift(ScaleQuantumToChar(ClampToUnsignedQuantum(
cristy4c08aed2011-07-01 19:47:50 +00001474 pixel->alpha)));
cristyca972de2010-06-20 23:37:02 +00001475 return(offset);
1476}
1477
cristy8a11cb12011-10-19 23:53:34 +00001478static MagickBooleanType FloydSteinbergDither(Image *image,CubeInfo *cube_info,
1479 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001480{
1481#define DitherImageTag "Dither/Image"
1482
cristyc4c8d132010-01-07 01:58:38 +00001483 CacheView
1484 *image_view;
1485
cristy3ed852e2009-09-05 21:47:34 +00001486 MagickBooleanType
cristye9717ac2011-02-20 16:17:17 +00001487 status;
cristy3ed852e2009-09-05 21:47:34 +00001488
cristy101ab702011-10-13 13:06:32 +00001489 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001490 **pixels;
cristy3ed852e2009-09-05 21:47:34 +00001491
cristy847620f2011-02-09 02:24:21 +00001492 ssize_t
cristy847620f2011-02-09 02:24:21 +00001493 y;
1494
cristy3ed852e2009-09-05 21:47:34 +00001495 /*
1496 Distribute quantization error using Floyd-Steinberg.
1497 */
cristye9717ac2011-02-20 16:17:17 +00001498 pixels=AcquirePixelThreadSet(image->columns);
cristy101ab702011-10-13 13:06:32 +00001499 if (pixels == (RealPixelInfo **) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001500 return(MagickFalse);
cristye9717ac2011-02-20 16:17:17 +00001501 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001502 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +00001503 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001504 {
cristye9717ac2011-02-20 16:17:17 +00001505 const int
1506 id = GetOpenMPThreadId();
1507
1508 CubeInfo
1509 cube;
1510
cristy101ab702011-10-13 13:06:32 +00001511 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001512 *current,
1513 *previous;
1514
cristy4c08aed2011-07-01 19:47:50 +00001515 register Quantum
cristyecc31b12011-02-13 00:32:29 +00001516 *restrict q;
1517
cristybb503372010-05-27 20:51:26 +00001518 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001519 x;
1520
cristye9717ac2011-02-20 16:17:17 +00001521 size_t
1522 index;
1523
1524 ssize_t
1525 v;
1526
1527 if (status == MagickFalse)
1528 continue;
cristy3ed852e2009-09-05 21:47:34 +00001529 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001530 if (q == (Quantum *) NULL)
cristye9717ac2011-02-20 16:17:17 +00001531 {
1532 status=MagickFalse;
cristy00cbdd62011-02-20 17:29:26 +00001533 continue;
cristye9717ac2011-02-20 16:17:17 +00001534 }
cristyed231572011-07-14 02:18:59 +00001535 q+=(y & 0x01)*image->columns*GetPixelChannels(image);
cristye9717ac2011-02-20 16:17:17 +00001536 cube=(*cube_info);
1537 current=pixels[id]+(y & 0x01)*image->columns;
1538 previous=pixels[id]+((y+1) & 0x01)*image->columns;
cristy4c08aed2011-07-01 19:47:50 +00001539 v=(ssize_t) ((y & 0x01) != 0 ? -1 : 1);
cristybb503372010-05-27 20:51:26 +00001540 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001541 {
cristy101ab702011-10-13 13:06:32 +00001542 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001543 color,
1544 pixel;
1545
1546 register ssize_t
1547 i;
1548
1549 ssize_t
1550 u;
1551
cristyed231572011-07-14 02:18:59 +00001552 q-=(y & 0x01)*GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001553 u=(y & 0x01) != 0 ? (ssize_t) image->columns-1-x : x;
1554 AssociateAlphaPixel(image,&cube,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001555 if (x > 0)
1556 {
1557 pixel.red+=7*current[u-v].red/16;
1558 pixel.green+=7*current[u-v].green/16;
1559 pixel.blue+=7*current[u-v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001560 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001561 pixel.alpha+=7*current[u-v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001562 }
1563 if (y > 0)
1564 {
cristybb503372010-05-27 20:51:26 +00001565 if (x < (ssize_t) (image->columns-1))
cristy3ed852e2009-09-05 21:47:34 +00001566 {
1567 pixel.red+=previous[u+v].red/16;
1568 pixel.green+=previous[u+v].green/16;
1569 pixel.blue+=previous[u+v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001570 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001571 pixel.alpha+=previous[u+v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001572 }
1573 pixel.red+=5*previous[u].red/16;
1574 pixel.green+=5*previous[u].green/16;
1575 pixel.blue+=5*previous[u].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001576 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001577 pixel.alpha+=5*previous[u].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001578 if (x > 0)
1579 {
1580 pixel.red+=3*previous[u-v].red/16;
1581 pixel.green+=3*previous[u-v].green/16;
1582 pixel.blue+=3*previous[u-v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001583 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001584 pixel.alpha+=3*previous[u-v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001585 }
1586 }
cristy75ffdb72010-01-07 17:40:12 +00001587 pixel.red=(MagickRealType) ClampToUnsignedQuantum(pixel.red);
1588 pixel.green=(MagickRealType) ClampToUnsignedQuantum(pixel.green);
1589 pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue);
cristye9717ac2011-02-20 16:17:17 +00001590 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001591 pixel.alpha=(MagickRealType) ClampToUnsignedQuantum(pixel.alpha);
cristye9717ac2011-02-20 16:17:17 +00001592 i=CacheOffset(&cube,&pixel);
1593 if (cube.cache[i] < 0)
cristy3ed852e2009-09-05 21:47:34 +00001594 {
1595 register NodeInfo
1596 *node_info;
1597
cristybb503372010-05-27 20:51:26 +00001598 register size_t
cristy3ed852e2009-09-05 21:47:34 +00001599 id;
1600
1601 /*
1602 Identify the deepest node containing the pixel's color.
1603 */
cristye9717ac2011-02-20 16:17:17 +00001604 node_info=cube.root;
cristybb503372010-05-27 20:51:26 +00001605 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +00001606 {
cristye9717ac2011-02-20 16:17:17 +00001607 id=ColorToNodeId(&cube,&pixel,index);
cristy3ed852e2009-09-05 21:47:34 +00001608 if (node_info->child[id] == (NodeInfo *) NULL)
1609 break;
1610 node_info=node_info->child[id];
1611 }
1612 /*
1613 Find closest color among siblings and their children.
1614 */
cristye9717ac2011-02-20 16:17:17 +00001615 cube.target=pixel;
1616 cube.distance=(MagickRealType) (4.0*(QuantumRange+1.0)*(QuantumRange+
cristy3ed852e2009-09-05 21:47:34 +00001617 1.0)+1.0);
cristye9717ac2011-02-20 16:17:17 +00001618 ClosestColor(image,&cube,node_info->parent);
1619 cube.cache[i]=(ssize_t) cube.color_number;
cristy3ed852e2009-09-05 21:47:34 +00001620 }
1621 /*
1622 Assign pixel to closest colormap entry.
1623 */
cristye9717ac2011-02-20 16:17:17 +00001624 index=(size_t) cube.cache[i];
cristy3ed852e2009-09-05 21:47:34 +00001625 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +00001626 SetPixelIndex(image,(Quantum) index,q);
cristye9717ac2011-02-20 16:17:17 +00001627 if (cube.quantize_info->measure_error == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001628 {
cristye42f6582012-02-11 17:59:50 +00001629 SetPixelRed(image,ClampToQuantum(image->colormap[index].red),q);
1630 SetPixelGreen(image,ClampToQuantum(image->colormap[index].green),q);
1631 SetPixelBlue(image,ClampToQuantum(image->colormap[index].blue),q);
cristye9717ac2011-02-20 16:17:17 +00001632 if (cube.associate_alpha != MagickFalse)
cristye42f6582012-02-11 17:59:50 +00001633 SetPixelAlpha(image,ClampToQuantum(image->colormap[index].alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001634 }
1635 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristye9717ac2011-02-20 16:17:17 +00001636 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001637 /*
1638 Store the error.
1639 */
cristy101ab702011-10-13 13:06:32 +00001640 AssociateAlphaPixelInfo(image,&cube,image->colormap+index,&color);
cristy3ed852e2009-09-05 21:47:34 +00001641 current[u].red=pixel.red-color.red;
1642 current[u].green=pixel.green-color.green;
1643 current[u].blue=pixel.blue-color.blue;
cristye9717ac2011-02-20 16:17:17 +00001644 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001645 current[u].alpha=pixel.alpha-color.alpha;
cristye9717ac2011-02-20 16:17:17 +00001646 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1647 {
1648 MagickBooleanType
1649 proceed;
1650
1651#if defined(MAGICKCORE_OPENMP_SUPPORT)
1652 #pragma omp critical (MagickCore_FloydSteinbergDither)
1653#endif
1654 proceed=SetImageProgress(image,DitherImageTag,(MagickOffsetType) y,
1655 image->rows);
1656 if (proceed == MagickFalse)
1657 status=MagickFalse;
1658 }
cristyed231572011-07-14 02:18:59 +00001659 q+=((y+1) & 0x01)*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001660 }
1661 }
cristy3ed852e2009-09-05 21:47:34 +00001662 image_view=DestroyCacheView(image_view);
cristye9717ac2011-02-20 16:17:17 +00001663 pixels=DestroyPixelThreadSet(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001664 return(MagickTrue);
1665}
1666
1667static MagickBooleanType
cristy8a11cb12011-10-19 23:53:34 +00001668 RiemersmaDither(Image *,CacheView *,CubeInfo *,const unsigned int,
1669 ExceptionInfo *exception);
cristy3ed852e2009-09-05 21:47:34 +00001670
1671static void Riemersma(Image *image,CacheView *image_view,CubeInfo *cube_info,
cristy8a11cb12011-10-19 23:53:34 +00001672 const size_t level,const unsigned int direction,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001673{
1674 if (level == 1)
1675 switch (direction)
1676 {
1677 case WestGravity:
1678 {
cristy8a11cb12011-10-19 23:53:34 +00001679 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1680 exception);
1681 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1682 exception);
1683 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1684 exception);
cristy3ed852e2009-09-05 21:47:34 +00001685 break;
1686 }
1687 case EastGravity:
1688 {
cristy8a11cb12011-10-19 23:53:34 +00001689 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1690 exception);
1691 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1692 exception);
1693 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1694 exception);
cristy3ed852e2009-09-05 21:47:34 +00001695 break;
1696 }
1697 case NorthGravity:
1698 {
cristy8a11cb12011-10-19 23:53:34 +00001699 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1700 exception);
1701 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1702 exception);
1703 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1704 exception);
cristy3ed852e2009-09-05 21:47:34 +00001705 break;
1706 }
1707 case SouthGravity:
1708 {
cristy8a11cb12011-10-19 23:53:34 +00001709 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1710 exception);
1711 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1712 exception);
1713 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1714 exception);
cristy3ed852e2009-09-05 21:47:34 +00001715 break;
1716 }
1717 default:
1718 break;
1719 }
1720 else
1721 switch (direction)
1722 {
1723 case WestGravity:
1724 {
cristy8a11cb12011-10-19 23:53:34 +00001725 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1726 exception);
1727 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1728 exception);
1729 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1730 exception);
1731 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1732 exception);
1733 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1734 exception);
1735 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1736 exception);
1737 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1738 exception);
cristy3ed852e2009-09-05 21:47:34 +00001739 break;
1740 }
1741 case EastGravity:
1742 {
cristy8a11cb12011-10-19 23:53:34 +00001743 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1744 exception);
1745 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1746 exception);
1747 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1748 exception);
1749 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1750 exception);
1751 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1752 exception);
1753 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1754 exception);
1755 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1756 exception);
cristy3ed852e2009-09-05 21:47:34 +00001757 break;
1758 }
1759 case NorthGravity:
1760 {
cristy8a11cb12011-10-19 23:53:34 +00001761 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1762 exception);
1763 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1764 exception);
1765 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1766 exception);
1767 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1768 exception);
1769 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1770 exception);
1771 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1772 exception);
1773 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1774 exception);
cristy3ed852e2009-09-05 21:47:34 +00001775 break;
1776 }
1777 case SouthGravity:
1778 {
cristy8a11cb12011-10-19 23:53:34 +00001779 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1780 exception);
1781 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1782 exception);
1783 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1784 exception);
1785 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1786 exception);
1787 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1788 exception);
1789 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1790 exception);
1791 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1792 exception);
cristy3ed852e2009-09-05 21:47:34 +00001793 break;
1794 }
1795 default:
1796 break;
1797 }
1798}
1799
1800static MagickBooleanType RiemersmaDither(Image *image,CacheView *image_view,
cristy8a11cb12011-10-19 23:53:34 +00001801 CubeInfo *cube_info,const unsigned int direction,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001802{
1803#define DitherImageTag "Dither/Image"
1804
1805 MagickBooleanType
1806 proceed;
1807
cristy101ab702011-10-13 13:06:32 +00001808 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001809 color,
1810 pixel;
1811
1812 register CubeInfo
1813 *p;
1814
cristybb503372010-05-27 20:51:26 +00001815 size_t
cristy3ed852e2009-09-05 21:47:34 +00001816 index;
1817
1818 p=cube_info;
cristybb503372010-05-27 20:51:26 +00001819 if ((p->x >= 0) && (p->x < (ssize_t) image->columns) &&
1820 (p->y >= 0) && (p->y < (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001821 {
cristy4c08aed2011-07-01 19:47:50 +00001822 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001823 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001824
cristyecc31b12011-02-13 00:32:29 +00001825 register ssize_t
1826 i;
1827
cristy3ed852e2009-09-05 21:47:34 +00001828 /*
1829 Distribute error.
1830 */
cristy3ed852e2009-09-05 21:47:34 +00001831 q=GetCacheViewAuthenticPixels(image_view,p->x,p->y,1,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001832 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001833 return(MagickFalse);
cristy4c08aed2011-07-01 19:47:50 +00001834 AssociateAlphaPixel(image,cube_info,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001835 for (i=0; i < ErrorQueueLength; i++)
1836 {
1837 pixel.red+=p->weights[i]*p->error[i].red;
1838 pixel.green+=p->weights[i]*p->error[i].green;
1839 pixel.blue+=p->weights[i]*p->error[i].blue;
1840 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001841 pixel.alpha+=p->weights[i]*p->error[i].alpha;
cristy3ed852e2009-09-05 21:47:34 +00001842 }
cristy75ffdb72010-01-07 17:40:12 +00001843 pixel.red=(MagickRealType) ClampToUnsignedQuantum(pixel.red);
1844 pixel.green=(MagickRealType) ClampToUnsignedQuantum(pixel.green);
1845 pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00001846 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001847 pixel.alpha=(MagickRealType) ClampToUnsignedQuantum(pixel.alpha);
cristyca972de2010-06-20 23:37:02 +00001848 i=CacheOffset(cube_info,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001849 if (p->cache[i] < 0)
1850 {
1851 register NodeInfo
1852 *node_info;
1853
cristybb503372010-05-27 20:51:26 +00001854 register size_t
cristy3ed852e2009-09-05 21:47:34 +00001855 id;
1856
1857 /*
1858 Identify the deepest node containing the pixel's color.
1859 */
1860 node_info=p->root;
cristybb503372010-05-27 20:51:26 +00001861 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +00001862 {
1863 id=ColorToNodeId(cube_info,&pixel,index);
1864 if (node_info->child[id] == (NodeInfo *) NULL)
1865 break;
1866 node_info=node_info->child[id];
1867 }
cristyecc31b12011-02-13 00:32:29 +00001868 node_info=node_info->parent;
cristy3ed852e2009-09-05 21:47:34 +00001869 /*
1870 Find closest color among siblings and their children.
1871 */
1872 p->target=pixel;
1873 p->distance=(MagickRealType) (4.0*(QuantumRange+1.0)*((MagickRealType)
1874 QuantumRange+1.0)+1.0);
1875 ClosestColor(image,p,node_info->parent);
cristybb503372010-05-27 20:51:26 +00001876 p->cache[i]=(ssize_t) p->color_number;
cristy3ed852e2009-09-05 21:47:34 +00001877 }
1878 /*
1879 Assign pixel to closest colormap entry.
1880 */
cristy4c08aed2011-07-01 19:47:50 +00001881 index=(size_t) p->cache[i];
cristy3ed852e2009-09-05 21:47:34 +00001882 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +00001883 SetPixelIndex(image,(Quantum) index,q);
cristy3ed852e2009-09-05 21:47:34 +00001884 if (cube_info->quantize_info->measure_error == MagickFalse)
1885 {
cristye42f6582012-02-11 17:59:50 +00001886 SetPixelRed(image,ClampToQuantum(image->colormap[index].red),q);
1887 SetPixelGreen(image,ClampToQuantum(image->colormap[index].green),q);
1888 SetPixelBlue(image,ClampToQuantum(image->colormap[index].blue),q);
cristy3ed852e2009-09-05 21:47:34 +00001889 if (cube_info->associate_alpha != MagickFalse)
cristye42f6582012-02-11 17:59:50 +00001890 SetPixelAlpha(image,ClampToQuantum(image->colormap[index].alpha),q);
cristy3ed852e2009-09-05 21:47:34 +00001891 }
1892 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1893 return(MagickFalse);
1894 /*
1895 Propagate the error as the last entry of the error queue.
1896 */
1897 (void) CopyMagickMemory(p->error,p->error+1,(ErrorQueueLength-1)*
1898 sizeof(p->error[0]));
cristy101ab702011-10-13 13:06:32 +00001899 AssociateAlphaPixelInfo(image,cube_info,image->colormap+index,&color);
cristy3ed852e2009-09-05 21:47:34 +00001900 p->error[ErrorQueueLength-1].red=pixel.red-color.red;
1901 p->error[ErrorQueueLength-1].green=pixel.green-color.green;
1902 p->error[ErrorQueueLength-1].blue=pixel.blue-color.blue;
1903 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001904 p->error[ErrorQueueLength-1].alpha=pixel.alpha-color.alpha;
cristy3ed852e2009-09-05 21:47:34 +00001905 proceed=SetImageProgress(image,DitherImageTag,p->offset,p->span);
1906 if (proceed == MagickFalse)
1907 return(MagickFalse);
1908 p->offset++;
1909 }
1910 switch (direction)
1911 {
1912 case WestGravity: p->x--; break;
1913 case EastGravity: p->x++; break;
1914 case NorthGravity: p->y--; break;
1915 case SouthGravity: p->y++; break;
1916 }
1917 return(MagickTrue);
1918}
1919
cristybb503372010-05-27 20:51:26 +00001920static inline ssize_t MagickMax(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00001921{
1922 if (x > y)
1923 return(x);
1924 return(y);
1925}
1926
cristybb503372010-05-27 20:51:26 +00001927static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00001928{
1929 if (x < y)
1930 return(x);
1931 return(y);
1932}
1933
cristy8a11cb12011-10-19 23:53:34 +00001934static MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info,
1935 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001936{
cristyc4c8d132010-01-07 01:58:38 +00001937 CacheView
1938 *image_view;
1939
cristy3ed852e2009-09-05 21:47:34 +00001940 MagickBooleanType
1941 status;
1942
cristybb503372010-05-27 20:51:26 +00001943 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001944 i;
1945
cristybb503372010-05-27 20:51:26 +00001946 size_t
cristy3ed852e2009-09-05 21:47:34 +00001947 depth;
1948
cristyfb7e9cd2011-02-20 16:26:15 +00001949 if (cube_info->quantize_info->dither_method != RiemersmaDitherMethod)
cristy8a11cb12011-10-19 23:53:34 +00001950 return(FloydSteinbergDither(image,cube_info,exception));
cristy3ed852e2009-09-05 21:47:34 +00001951 /*
cristycee97112010-05-28 00:44:52 +00001952 Distribute quantization error along a Hilbert curve.
cristy3ed852e2009-09-05 21:47:34 +00001953 */
1954 (void) ResetMagickMemory(cube_info->error,0,ErrorQueueLength*
1955 sizeof(*cube_info->error));
1956 cube_info->x=0;
1957 cube_info->y=0;
cristybb503372010-05-27 20:51:26 +00001958 i=MagickMax((ssize_t) image->columns,(ssize_t) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001959 for (depth=1; i != 0; depth++)
1960 i>>=1;
cristybb503372010-05-27 20:51:26 +00001961 if ((ssize_t) (1L << depth) < MagickMax((ssize_t) image->columns,(ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001962 depth++;
1963 cube_info->offset=0;
1964 cube_info->span=(MagickSizeType) image->columns*image->rows;
1965 image_view=AcquireCacheView(image);
1966 if (depth > 1)
cristy8a11cb12011-10-19 23:53:34 +00001967 Riemersma(image,image_view,cube_info,depth-1,NorthGravity,exception);
1968 status=RiemersmaDither(image,image_view,cube_info,ForgetGravity,exception);
cristy3ed852e2009-09-05 21:47:34 +00001969 image_view=DestroyCacheView(image_view);
1970 return(status);
1971}
1972
1973/*
1974%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1975% %
1976% %
1977% %
1978+ G e t C u b e I n f o %
1979% %
1980% %
1981% %
1982%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1983%
1984% GetCubeInfo() initialize the Cube data structure.
1985%
1986% The format of the GetCubeInfo method is:
1987%
1988% CubeInfo GetCubeInfo(const QuantizeInfo *quantize_info,
cristybb503372010-05-27 20:51:26 +00001989% const size_t depth,const size_t maximum_colors)
cristy3ed852e2009-09-05 21:47:34 +00001990%
1991% A description of each parameter follows.
1992%
1993% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
1994%
1995% o depth: Normally, this integer value is zero or one. A zero or
1996% one tells Quantize to choose a optimal tree depth of Log4(number_colors).
1997% A tree of this depth generally allows the best representation of the
1998% reference image with the least amount of memory and the fastest
1999% computational speed. In some cases, such as an image with low color
2000% dispersion (a few number of colors), a value other than
2001% Log4(number_colors) is required. To expand the color tree completely,
2002% use a value of 8.
2003%
2004% o maximum_colors: maximum colors.
2005%
2006*/
2007static CubeInfo *GetCubeInfo(const QuantizeInfo *quantize_info,
cristybb503372010-05-27 20:51:26 +00002008 const size_t depth,const size_t maximum_colors)
cristy3ed852e2009-09-05 21:47:34 +00002009{
2010 CubeInfo
2011 *cube_info;
2012
2013 MagickRealType
2014 sum,
2015 weight;
2016
cristybb503372010-05-27 20:51:26 +00002017 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002018 i;
2019
cristyecc31b12011-02-13 00:32:29 +00002020 size_t
2021 length;
2022
cristy3ed852e2009-09-05 21:47:34 +00002023 /*
2024 Initialize tree to describe color cube_info.
2025 */
cristy73bd4a52010-10-05 11:24:23 +00002026 cube_info=(CubeInfo *) AcquireMagickMemory(sizeof(*cube_info));
cristy3ed852e2009-09-05 21:47:34 +00002027 if (cube_info == (CubeInfo *) NULL)
2028 return((CubeInfo *) NULL);
2029 (void) ResetMagickMemory(cube_info,0,sizeof(*cube_info));
2030 cube_info->depth=depth;
2031 if (cube_info->depth > MaxTreeDepth)
2032 cube_info->depth=MaxTreeDepth;
2033 if (cube_info->depth < 2)
2034 cube_info->depth=2;
2035 cube_info->maximum_colors=maximum_colors;
2036 /*
2037 Initialize root node.
2038 */
2039 cube_info->root=GetNodeInfo(cube_info,0,0,(NodeInfo *) NULL);
2040 if (cube_info->root == (NodeInfo *) NULL)
2041 return((CubeInfo *) NULL);
2042 cube_info->root->parent=cube_info->root;
2043 cube_info->quantize_info=CloneQuantizeInfo(quantize_info);
2044 if (cube_info->quantize_info->dither == MagickFalse)
2045 return(cube_info);
2046 /*
2047 Initialize dither resources.
2048 */
2049 length=(size_t) (1UL << (4*(8-CacheShift)));
cristybb503372010-05-27 20:51:26 +00002050 cube_info->cache=(ssize_t *) AcquireQuantumMemory(length,
cristy3ed852e2009-09-05 21:47:34 +00002051 sizeof(*cube_info->cache));
cristybb503372010-05-27 20:51:26 +00002052 if (cube_info->cache == (ssize_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002053 return((CubeInfo *) NULL);
2054 /*
2055 Initialize color cache.
2056 */
cristybb503372010-05-27 20:51:26 +00002057 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00002058 cube_info->cache[i]=(-1);
2059 /*
cristycee97112010-05-28 00:44:52 +00002060 Distribute weights along a curve of exponential decay.
cristy3ed852e2009-09-05 21:47:34 +00002061 */
2062 weight=1.0;
2063 for (i=0; i < ErrorQueueLength; i++)
2064 {
2065 cube_info->weights[ErrorQueueLength-i-1]=1.0/weight;
2066 weight*=exp(log(((double) QuantumRange+1.0))/(ErrorQueueLength-1.0));
2067 }
2068 /*
2069 Normalize the weighting factors.
2070 */
2071 weight=0.0;
2072 for (i=0; i < ErrorQueueLength; i++)
2073 weight+=cube_info->weights[i];
2074 sum=0.0;
2075 for (i=0; i < ErrorQueueLength; i++)
2076 {
2077 cube_info->weights[i]/=weight;
2078 sum+=cube_info->weights[i];
2079 }
2080 cube_info->weights[0]+=1.0-sum;
2081 return(cube_info);
2082}
2083
2084/*
2085%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2086% %
2087% %
2088% %
2089+ G e t N o d e I n f o %
2090% %
2091% %
2092% %
2093%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2094%
2095% GetNodeInfo() allocates memory for a new node in the color cube tree and
2096% presets all fields to zero.
2097%
2098% The format of the GetNodeInfo method is:
2099%
cristybb503372010-05-27 20:51:26 +00002100% NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id,
2101% const size_t level,NodeInfo *parent)
cristy3ed852e2009-09-05 21:47:34 +00002102%
2103% A description of each parameter follows.
2104%
2105% o node: The GetNodeInfo method returns a pointer to a queue of nodes.
2106%
2107% o id: Specifies the child number of the node.
2108%
2109% o level: Specifies the level in the storage_class the node resides.
2110%
2111*/
cristybb503372010-05-27 20:51:26 +00002112static NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id,
2113 const size_t level,NodeInfo *parent)
cristy3ed852e2009-09-05 21:47:34 +00002114{
2115 NodeInfo
2116 *node_info;
2117
2118 if (cube_info->free_nodes == 0)
2119 {
2120 Nodes
2121 *nodes;
2122
2123 /*
2124 Allocate a new queue of nodes.
2125 */
cristy73bd4a52010-10-05 11:24:23 +00002126 nodes=(Nodes *) AcquireMagickMemory(sizeof(*nodes));
cristy3ed852e2009-09-05 21:47:34 +00002127 if (nodes == (Nodes *) NULL)
2128 return((NodeInfo *) NULL);
2129 nodes->nodes=(NodeInfo *) AcquireQuantumMemory(NodesInAList,
2130 sizeof(*nodes->nodes));
2131 if (nodes->nodes == (NodeInfo *) NULL)
2132 return((NodeInfo *) NULL);
2133 nodes->next=cube_info->node_queue;
2134 cube_info->node_queue=nodes;
2135 cube_info->next_node=nodes->nodes;
2136 cube_info->free_nodes=NodesInAList;
2137 }
2138 cube_info->nodes++;
2139 cube_info->free_nodes--;
2140 node_info=cube_info->next_node++;
2141 (void) ResetMagickMemory(node_info,0,sizeof(*node_info));
2142 node_info->parent=parent;
2143 node_info->id=id;
2144 node_info->level=level;
2145 return(node_info);
2146}
2147
2148/*
2149%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2150% %
2151% %
2152% %
2153% G e t I m a g e Q u a n t i z e E r r o r %
2154% %
2155% %
2156% %
2157%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2158%
2159% GetImageQuantizeError() measures the difference between the original
2160% and quantized images. This difference is the total quantization error.
2161% The error is computed by summing over all pixels in an image the distance
2162% squared in RGB space between each reference pixel value and its quantized
2163% value. These values are computed:
2164%
2165% o mean_error_per_pixel: This value is the mean error for any single
2166% pixel in the image.
2167%
2168% o normalized_mean_square_error: This value is the normalized mean
2169% quantization error for any single pixel in the image. This distance
2170% measure is normalized to a range between 0 and 1. It is independent
2171% of the range of red, green, and blue values in the image.
2172%
2173% o normalized_maximum_square_error: Thsi value is the normalized
2174% maximum quantization error for any single pixel in the image. This
2175% distance measure is normalized to a range between 0 and 1. It is
2176% independent of the range of red, green, and blue values in your image.
2177%
2178% The format of the GetImageQuantizeError method is:
2179%
cristy8a11cb12011-10-19 23:53:34 +00002180% MagickBooleanType GetImageQuantizeError(Image *image,
2181% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002182%
2183% A description of each parameter follows.
2184%
2185% o image: the image.
2186%
cristy8a11cb12011-10-19 23:53:34 +00002187% o exception: return any errors or warnings in this structure.
2188%
cristy3ed852e2009-09-05 21:47:34 +00002189*/
cristy8a11cb12011-10-19 23:53:34 +00002190MagickExport MagickBooleanType GetImageQuantizeError(Image *image,
2191 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002192{
cristyc4c8d132010-01-07 01:58:38 +00002193 CacheView
2194 *image_view;
2195
cristy3ed852e2009-09-05 21:47:34 +00002196 MagickRealType
2197 alpha,
2198 area,
2199 beta,
2200 distance,
2201 maximum_error,
2202 mean_error,
2203 mean_error_per_pixel;
2204
cristybb503372010-05-27 20:51:26 +00002205 size_t
cristy3ed852e2009-09-05 21:47:34 +00002206 index;
2207
cristyecc31b12011-02-13 00:32:29 +00002208 ssize_t
2209 y;
2210
cristy3ed852e2009-09-05 21:47:34 +00002211 assert(image != (Image *) NULL);
2212 assert(image->signature == MagickSignature);
2213 if (image->debug != MagickFalse)
2214 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy8a11cb12011-10-19 23:53:34 +00002215 image->total_colors=GetNumberColors(image,(FILE *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00002216 (void) ResetMagickMemory(&image->error,0,sizeof(image->error));
2217 if (image->storage_class == DirectClass)
2218 return(MagickTrue);
2219 alpha=1.0;
2220 beta=1.0;
2221 area=3.0*image->columns*image->rows;
2222 maximum_error=0.0;
2223 mean_error_per_pixel=0.0;
2224 mean_error=0.0;
cristy3ed852e2009-09-05 21:47:34 +00002225 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +00002226 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002227 {
cristy4c08aed2011-07-01 19:47:50 +00002228 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002229 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002230
cristybb503372010-05-27 20:51:26 +00002231 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002232 x;
2233
2234 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002235 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002236 break;
cristybb503372010-05-27 20:51:26 +00002237 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002238 {
cristy4c08aed2011-07-01 19:47:50 +00002239 index=1UL*GetPixelIndex(image,p);
cristy3ed852e2009-09-05 21:47:34 +00002240 if (image->matte != MagickFalse)
2241 {
cristy4c08aed2011-07-01 19:47:50 +00002242 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
2243 beta=(MagickRealType) (QuantumScale*image->colormap[index].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002244 }
cristy4c08aed2011-07-01 19:47:50 +00002245 distance=fabs(alpha*GetPixelRed(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002246 image->colormap[index].red);
cristy3ed852e2009-09-05 21:47:34 +00002247 mean_error_per_pixel+=distance;
2248 mean_error+=distance*distance;
2249 if (distance > maximum_error)
2250 maximum_error=distance;
cristy4c08aed2011-07-01 19:47:50 +00002251 distance=fabs(alpha*GetPixelGreen(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002252 image->colormap[index].green);
cristy3ed852e2009-09-05 21:47:34 +00002253 mean_error_per_pixel+=distance;
2254 mean_error+=distance*distance;
2255 if (distance > maximum_error)
2256 maximum_error=distance;
cristy4c08aed2011-07-01 19:47:50 +00002257 distance=fabs(alpha*GetPixelBlue(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002258 image->colormap[index].blue);
cristy3ed852e2009-09-05 21:47:34 +00002259 mean_error_per_pixel+=distance;
2260 mean_error+=distance*distance;
2261 if (distance > maximum_error)
2262 maximum_error=distance;
cristyed231572011-07-14 02:18:59 +00002263 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002264 }
2265 }
2266 image_view=DestroyCacheView(image_view);
2267 image->error.mean_error_per_pixel=(double) mean_error_per_pixel/area;
2268 image->error.normalized_mean_error=(double) QuantumScale*QuantumScale*
2269 mean_error/area;
2270 image->error.normalized_maximum_error=(double) QuantumScale*maximum_error;
2271 return(MagickTrue);
2272}
2273
2274/*
2275%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2276% %
2277% %
2278% %
2279% G e t Q u a n t i z e I n f o %
2280% %
2281% %
2282% %
2283%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2284%
2285% GetQuantizeInfo() initializes the QuantizeInfo structure.
2286%
2287% The format of the GetQuantizeInfo method is:
2288%
2289% GetQuantizeInfo(QuantizeInfo *quantize_info)
2290%
2291% A description of each parameter follows:
2292%
2293% o quantize_info: Specifies a pointer to a QuantizeInfo structure.
2294%
2295*/
2296MagickExport void GetQuantizeInfo(QuantizeInfo *quantize_info)
2297{
2298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2299 assert(quantize_info != (QuantizeInfo *) NULL);
2300 (void) ResetMagickMemory(quantize_info,0,sizeof(*quantize_info));
2301 quantize_info->number_colors=256;
2302 quantize_info->dither=MagickTrue;
2303 quantize_info->dither_method=RiemersmaDitherMethod;
2304 quantize_info->colorspace=UndefinedColorspace;
2305 quantize_info->measure_error=MagickFalse;
2306 quantize_info->signature=MagickSignature;
2307}
2308
2309/*
2310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2311% %
2312% %
2313% %
cristy018f07f2011-09-04 21:15:19 +00002314% P o s t e r i z e I m a g e %
cristy3ed852e2009-09-05 21:47:34 +00002315% %
2316% %
2317% %
2318%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2319%
2320% PosterizeImage() reduces the image to a limited number of colors for a
2321% "poster" effect.
2322%
2323% The format of the PosterizeImage method is:
2324%
cristybb503372010-05-27 20:51:26 +00002325% MagickBooleanType PosterizeImage(Image *image,const size_t levels,
cristy018f07f2011-09-04 21:15:19 +00002326% const MagickBooleanType dither,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002327%
2328% A description of each parameter follows:
2329%
2330% o image: Specifies a pointer to an Image structure.
2331%
2332% o levels: Number of color levels allowed in each channel. Very low values
2333% (2, 3, or 4) have the most visible effect.
2334%
cristy847620f2011-02-09 02:24:21 +00002335% o dither: Set this integer value to something other than zero to dither
2336% the mapped image.
cristy3ed852e2009-09-05 21:47:34 +00002337%
cristy018f07f2011-09-04 21:15:19 +00002338% o exception: return any errors or warnings in this structure.
2339%
cristy3ed852e2009-09-05 21:47:34 +00002340*/
cristyd1a2c0f2011-02-09 14:14:50 +00002341
cristy4d727152011-02-10 19:57:21 +00002342static inline ssize_t MagickRound(MagickRealType x)
2343{
2344 /*
cristyecc31b12011-02-13 00:32:29 +00002345 Round the fraction to nearest integer.
cristy4d727152011-02-10 19:57:21 +00002346 */
2347 if (x >= 0.0)
2348 return((ssize_t) (x+0.5));
2349 return((ssize_t) (x-0.5));
2350}
2351
cristyd1a2c0f2011-02-09 14:14:50 +00002352MagickExport MagickBooleanType PosterizeImage(Image *image,const size_t levels,
cristy018f07f2011-09-04 21:15:19 +00002353 const MagickBooleanType dither,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002354{
cristyd1a2c0f2011-02-09 14:14:50 +00002355#define PosterizeImageTag "Posterize/Image"
cristy4d727152011-02-10 19:57:21 +00002356#define PosterizePixel(pixel) (Quantum) (QuantumRange*(MagickRound( \
cristy3e9cad02011-02-20 01:42:00 +00002357 QuantumScale*pixel*(levels-1)))/MagickMax((ssize_t) levels-1,1))
cristyd1a2c0f2011-02-09 14:14:50 +00002358
cristyc4c8d132010-01-07 01:58:38 +00002359 CacheView
cristyd1a2c0f2011-02-09 14:14:50 +00002360 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00002361
cristy3ed852e2009-09-05 21:47:34 +00002362 MagickBooleanType
2363 status;
2364
cristyd1a2c0f2011-02-09 14:14:50 +00002365 MagickOffsetType
2366 progress;
2367
cristy3ed852e2009-09-05 21:47:34 +00002368 QuantizeInfo
2369 *quantize_info;
2370
cristy847620f2011-02-09 02:24:21 +00002371 register ssize_t
2372 i;
2373
cristy847620f2011-02-09 02:24:21 +00002374 ssize_t
cristyd1a2c0f2011-02-09 14:14:50 +00002375 y;
cristy847620f2011-02-09 02:24:21 +00002376
cristy3ed852e2009-09-05 21:47:34 +00002377 assert(image != (Image *) NULL);
2378 assert(image->signature == MagickSignature);
2379 if (image->debug != MagickFalse)
2380 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyd1a2c0f2011-02-09 14:14:50 +00002381 if (image->storage_class == PseudoClass)
2382#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002383 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyd1a2c0f2011-02-09 14:14:50 +00002384#endif
2385 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002386 {
cristyd1a2c0f2011-02-09 14:14:50 +00002387 /*
2388 Posterize colormap.
2389 */
cristyed231572011-07-14 02:18:59 +00002390 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002391 image->colormap[i].red=(double)
2392 PosterizePixel(image->colormap[i].red);
cristyed231572011-07-14 02:18:59 +00002393 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002394 image->colormap[i].green=(double)
2395 PosterizePixel(image->colormap[i].green);
cristyed231572011-07-14 02:18:59 +00002396 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002397 image->colormap[i].blue=(double)
2398 PosterizePixel(image->colormap[i].blue);
cristyed231572011-07-14 02:18:59 +00002399 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristye42f6582012-02-11 17:59:50 +00002400 image->colormap[i].alpha=(double)
2401 PosterizePixel(image->colormap[i].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002402 }
cristyd1a2c0f2011-02-09 14:14:50 +00002403 /*
2404 Posterize image.
2405 */
2406 status=MagickTrue;
2407 progress=0;
cristyd1a2c0f2011-02-09 14:14:50 +00002408 image_view=AcquireCacheView(image);
2409#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002410 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyd1a2c0f2011-02-09 14:14:50 +00002411#endif
2412 for (y=0; y < (ssize_t) image->rows; y++)
2413 {
cristy4c08aed2011-07-01 19:47:50 +00002414 register Quantum
cristyd1a2c0f2011-02-09 14:14:50 +00002415 *restrict q;
2416
2417 register ssize_t
2418 x;
2419
2420 if (status == MagickFalse)
2421 continue;
2422 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002423 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002424 {
cristyd1a2c0f2011-02-09 14:14:50 +00002425 status=MagickFalse;
2426 continue;
cristy3ed852e2009-09-05 21:47:34 +00002427 }
cristyd1a2c0f2011-02-09 14:14:50 +00002428 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002429 {
cristyed231572011-07-14 02:18:59 +00002430 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002431 SetPixelRed(image,PosterizePixel(GetPixelRed(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002432 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002433 SetPixelGreen(image,PosterizePixel(GetPixelGreen(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002434 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002435 SetPixelBlue(image,PosterizePixel(GetPixelBlue(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002436 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002437 (image->colorspace == CMYKColorspace))
2438 SetPixelBlack(image,PosterizePixel(GetPixelBlack(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002439 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristyd1a2c0f2011-02-09 14:14:50 +00002440 (image->matte == MagickTrue))
cristy4c08aed2011-07-01 19:47:50 +00002441 SetPixelAlpha(image,PosterizePixel(GetPixelAlpha(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002442 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002443 }
cristyd1a2c0f2011-02-09 14:14:50 +00002444 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2445 status=MagickFalse;
2446 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2447 {
2448 MagickBooleanType
2449 proceed;
2450
2451#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13020672011-07-08 02:33:26 +00002452 #pragma omp critical (MagickCore_PosterizeImage)
cristyd1a2c0f2011-02-09 14:14:50 +00002453#endif
2454 proceed=SetImageProgress(image,PosterizeImageTag,progress++,
2455 image->rows);
2456 if (proceed == MagickFalse)
2457 status=MagickFalse;
2458 }
2459 }
2460 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002461 quantize_info=AcquireQuantizeInfo((ImageInfo *) NULL);
cristyd1a2c0f2011-02-09 14:14:50 +00002462 quantize_info->number_colors=(size_t) MagickMin((ssize_t) levels*levels*
2463 levels,MaxColormapSize+1);
cristy3ed852e2009-09-05 21:47:34 +00002464 quantize_info->dither=dither;
cristy3e9cad02011-02-20 01:42:00 +00002465 quantize_info->tree_depth=MaxTreeDepth;
cristy018f07f2011-09-04 21:15:19 +00002466 status=QuantizeImage(quantize_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002467 quantize_info=DestroyQuantizeInfo(quantize_info);
cristy3ed852e2009-09-05 21:47:34 +00002468 return(status);
2469}
2470
2471/*
2472%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2473% %
2474% %
2475% %
2476+ P r u n e C h i l d %
2477% %
2478% %
2479% %
2480%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2481%
2482% PruneChild() deletes the given node and merges its statistics into its
2483% parent.
2484%
2485% The format of the PruneSubtree method is:
2486%
2487% PruneChild(const Image *image,CubeInfo *cube_info,
2488% const NodeInfo *node_info)
2489%
2490% A description of each parameter follows.
2491%
2492% o image: the image.
2493%
2494% o cube_info: A pointer to the Cube structure.
2495%
2496% o node_info: pointer to node in color cube tree that is to be pruned.
2497%
2498*/
2499static void PruneChild(const Image *image,CubeInfo *cube_info,
2500 const NodeInfo *node_info)
2501{
2502 NodeInfo
2503 *parent;
2504
cristybb503372010-05-27 20:51:26 +00002505 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002506 i;
2507
cristybb503372010-05-27 20:51:26 +00002508 size_t
cristy3ed852e2009-09-05 21:47:34 +00002509 number_children;
2510
2511 /*
2512 Traverse any children.
2513 */
2514 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002515 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002516 if (node_info->child[i] != (NodeInfo *) NULL)
2517 PruneChild(image,cube_info,node_info->child[i]);
2518 /*
2519 Merge color statistics into parent.
2520 */
2521 parent=node_info->parent;
2522 parent->number_unique+=node_info->number_unique;
2523 parent->total_color.red+=node_info->total_color.red;
2524 parent->total_color.green+=node_info->total_color.green;
2525 parent->total_color.blue+=node_info->total_color.blue;
cristy4c08aed2011-07-01 19:47:50 +00002526 parent->total_color.alpha+=node_info->total_color.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002527 parent->child[node_info->id]=(NodeInfo *) NULL;
2528 cube_info->nodes--;
2529}
2530
2531/*
2532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2533% %
2534% %
2535% %
2536+ P r u n e L e v e l %
2537% %
2538% %
2539% %
2540%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2541%
2542% PruneLevel() deletes all nodes at the bottom level of the color tree merging
2543% their color statistics into their parent node.
2544%
2545% The format of the PruneLevel method is:
2546%
2547% PruneLevel(const Image *image,CubeInfo *cube_info,
2548% const NodeInfo *node_info)
2549%
2550% A description of each parameter follows.
2551%
2552% o image: the image.
2553%
2554% o cube_info: A pointer to the Cube structure.
2555%
2556% o node_info: pointer to node in color cube tree that is to be pruned.
2557%
2558*/
2559static void PruneLevel(const Image *image,CubeInfo *cube_info,
2560 const NodeInfo *node_info)
2561{
cristybb503372010-05-27 20:51:26 +00002562 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002563 i;
2564
cristybb503372010-05-27 20:51:26 +00002565 size_t
cristy3ed852e2009-09-05 21:47:34 +00002566 number_children;
2567
2568 /*
2569 Traverse any children.
2570 */
2571 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002572 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002573 if (node_info->child[i] != (NodeInfo *) NULL)
2574 PruneLevel(image,cube_info,node_info->child[i]);
2575 if (node_info->level == cube_info->depth)
2576 PruneChild(image,cube_info,node_info);
2577}
2578
2579/*
2580%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2581% %
2582% %
2583% %
2584+ P r u n e T o C u b e D e p t h %
2585% %
2586% %
2587% %
2588%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2589%
2590% PruneToCubeDepth() deletes any nodes at a depth greater than
2591% cube_info->depth while merging their color statistics into their parent
2592% node.
2593%
2594% The format of the PruneToCubeDepth method is:
2595%
2596% PruneToCubeDepth(const Image *image,CubeInfo *cube_info,
2597% const NodeInfo *node_info)
2598%
2599% A description of each parameter follows.
2600%
2601% o cube_info: A pointer to the Cube structure.
2602%
2603% o node_info: pointer to node in color cube tree that is to be pruned.
2604%
2605*/
2606static void PruneToCubeDepth(const Image *image,CubeInfo *cube_info,
2607 const NodeInfo *node_info)
2608{
cristybb503372010-05-27 20:51:26 +00002609 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002610 i;
2611
cristybb503372010-05-27 20:51:26 +00002612 size_t
cristy3ed852e2009-09-05 21:47:34 +00002613 number_children;
2614
2615 /*
2616 Traverse any children.
2617 */
2618 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002619 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002620 if (node_info->child[i] != (NodeInfo *) NULL)
2621 PruneToCubeDepth(image,cube_info,node_info->child[i]);
2622 if (node_info->level > cube_info->depth)
2623 PruneChild(image,cube_info,node_info);
2624}
2625
2626/*
2627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2628% %
2629% %
2630% %
2631% Q u a n t i z e I m a g e %
2632% %
2633% %
2634% %
2635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2636%
2637% QuantizeImage() analyzes the colors within a reference image and chooses a
2638% fixed number of colors to represent the image. The goal of the algorithm
2639% is to minimize the color difference between the input and output image while
2640% minimizing the processing time.
2641%
2642% The format of the QuantizeImage method is:
2643%
2644% MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002645% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002646%
2647% A description of each parameter follows:
2648%
2649% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
2650%
2651% o image: the image.
2652%
cristy018f07f2011-09-04 21:15:19 +00002653% o exception: return any errors or warnings in this structure.
2654%
cristy3ed852e2009-09-05 21:47:34 +00002655*/
cristy5f7dca62011-08-12 12:38:05 +00002656
2657static MagickBooleanType DirectToColormapImage(Image *image,
2658 ExceptionInfo *exception)
2659{
2660 CacheView
2661 *image_view;
2662
2663 MagickBooleanType
2664 status;
2665
2666 register ssize_t
2667 i;
2668
2669 size_t
2670 number_colors;
2671
2672 ssize_t
2673 y;
2674
2675 status=MagickTrue;
2676 number_colors=(size_t) (image->columns*image->rows);
cristy018f07f2011-09-04 21:15:19 +00002677 if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
cristy5f7dca62011-08-12 12:38:05 +00002678 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2679 image->filename);
2680 if (image->colors != number_colors)
2681 return(MagickFalse);
2682 i=0;
2683 image_view=AcquireCacheView(image);
2684 for (y=0; y < (ssize_t) image->rows; y++)
2685 {
2686 MagickBooleanType
2687 proceed;
2688
2689 register Quantum
2690 *restrict q;
2691
2692 register ssize_t
2693 x;
2694
2695 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2696 if (q == (Quantum *) NULL)
2697 break;
2698 for (x=0; x < (ssize_t) image->columns; x++)
2699 {
cristye42f6582012-02-11 17:59:50 +00002700 image->colormap[i].red=(double) GetPixelRed(image,q);
2701 image->colormap[i].green=(double) GetPixelGreen(image,q);
2702 image->colormap[i].blue=(double) GetPixelBlue(image,q);
2703 image->colormap[i].alpha=(double) GetPixelAlpha(image,q);
cristy5f7dca62011-08-12 12:38:05 +00002704 SetPixelIndex(image,(Quantum) i,q);
2705 i++;
2706 q+=GetPixelChannels(image);
2707 }
2708 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2709 break;
2710 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y,
2711 image->rows);
2712 if (proceed == MagickFalse)
2713 status=MagickFalse;
2714 }
2715 image_view=DestroyCacheView(image_view);
2716 return(status);
2717}
2718
cristy3ed852e2009-09-05 21:47:34 +00002719MagickExport MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002720 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002721{
2722 CubeInfo
2723 *cube_info;
2724
2725 MagickBooleanType
2726 status;
2727
cristybb503372010-05-27 20:51:26 +00002728 size_t
cristy3ed852e2009-09-05 21:47:34 +00002729 depth,
2730 maximum_colors;
2731
2732 assert(quantize_info != (const QuantizeInfo *) NULL);
2733 assert(quantize_info->signature == MagickSignature);
2734 assert(image != (Image *) NULL);
2735 assert(image->signature == MagickSignature);
2736 if (image->debug != MagickFalse)
2737 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2738 maximum_colors=quantize_info->number_colors;
2739 if (maximum_colors == 0)
2740 maximum_colors=MaxColormapSize;
2741 if (maximum_colors > MaxColormapSize)
2742 maximum_colors=MaxColormapSize;
cristy5f7dca62011-08-12 12:38:05 +00002743 if ((image->columns*image->rows) <= maximum_colors)
cristy8a11cb12011-10-19 23:53:34 +00002744 (void) DirectToColormapImage(image,exception);
2745 if ((IsImageGray(image,exception) != MagickFalse) &&
cristy8e752752011-04-16 13:48:22 +00002746 (image->matte == MagickFalse))
cristy018f07f2011-09-04 21:15:19 +00002747 (void) SetGrayscaleImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002748 if ((image->storage_class == PseudoClass) &&
2749 (image->colors <= maximum_colors))
2750 return(MagickTrue);
2751 depth=quantize_info->tree_depth;
2752 if (depth == 0)
2753 {
cristybb503372010-05-27 20:51:26 +00002754 size_t
cristy3ed852e2009-09-05 21:47:34 +00002755 colors;
2756
2757 /*
2758 Depth of color tree is: Log4(colormap size)+2.
2759 */
2760 colors=maximum_colors;
2761 for (depth=1; colors != 0; depth++)
2762 colors>>=2;
2763 if ((quantize_info->dither != MagickFalse) && (depth > 2))
2764 depth--;
2765 if ((image->matte != MagickFalse) && (depth > 5))
2766 depth--;
2767 }
2768 /*
2769 Initialize color cube.
2770 */
2771 cube_info=GetCubeInfo(quantize_info,depth,maximum_colors);
2772 if (cube_info == (CubeInfo *) NULL)
2773 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2774 image->filename);
cristy8a11cb12011-10-19 23:53:34 +00002775 status=ClassifyImageColors(cube_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002776 if (status != MagickFalse)
2777 {
2778 /*
2779 Reduce the number of colors in the image.
2780 */
2781 ReduceImageColors(image,cube_info);
cristy018f07f2011-09-04 21:15:19 +00002782 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00002783 }
2784 DestroyCubeInfo(cube_info);
2785 return(status);
2786}
2787
2788/*
2789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2790% %
2791% %
2792% %
2793% Q u a n t i z e I m a g e s %
2794% %
2795% %
2796% %
2797%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2798%
2799% QuantizeImages() analyzes the colors within a set of reference images and
2800% chooses a fixed number of colors to represent the set. The goal of the
2801% algorithm is to minimize the color difference between the input and output
2802% images while minimizing the processing time.
2803%
2804% The format of the QuantizeImages method is:
2805%
2806% MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002807% Image *images,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002808%
2809% A description of each parameter follows:
2810%
2811% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
2812%
2813% o images: Specifies a pointer to a list of Image structures.
2814%
cristy018f07f2011-09-04 21:15:19 +00002815% o exception: return any errors or warnings in this structure.
2816%
cristy3ed852e2009-09-05 21:47:34 +00002817*/
2818MagickExport MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002819 Image *images,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002820{
2821 CubeInfo
2822 *cube_info;
2823
2824 Image
2825 *image;
2826
2827 MagickBooleanType
2828 proceed,
2829 status;
2830
2831 MagickProgressMonitor
2832 progress_monitor;
2833
cristybb503372010-05-27 20:51:26 +00002834 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002835 i;
2836
cristybb503372010-05-27 20:51:26 +00002837 size_t
cristy3ed852e2009-09-05 21:47:34 +00002838 depth,
2839 maximum_colors,
2840 number_images;
2841
2842 assert(quantize_info != (const QuantizeInfo *) NULL);
2843 assert(quantize_info->signature == MagickSignature);
2844 assert(images != (Image *) NULL);
2845 assert(images->signature == MagickSignature);
2846 if (images->debug != MagickFalse)
2847 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
2848 if (GetNextImageInList(images) == (Image *) NULL)
2849 {
2850 /*
2851 Handle a single image with QuantizeImage.
2852 */
cristy018f07f2011-09-04 21:15:19 +00002853 status=QuantizeImage(quantize_info,images,exception);
cristy3ed852e2009-09-05 21:47:34 +00002854 return(status);
2855 }
2856 status=MagickFalse;
2857 maximum_colors=quantize_info->number_colors;
2858 if (maximum_colors == 0)
2859 maximum_colors=MaxColormapSize;
2860 if (maximum_colors > MaxColormapSize)
2861 maximum_colors=MaxColormapSize;
2862 depth=quantize_info->tree_depth;
2863 if (depth == 0)
2864 {
cristybb503372010-05-27 20:51:26 +00002865 size_t
cristy3ed852e2009-09-05 21:47:34 +00002866 colors;
2867
2868 /*
2869 Depth of color tree is: Log4(colormap size)+2.
2870 */
2871 colors=maximum_colors;
2872 for (depth=1; colors != 0; depth++)
2873 colors>>=2;
2874 if (quantize_info->dither != MagickFalse)
2875 depth--;
2876 }
2877 /*
2878 Initialize color cube.
2879 */
2880 cube_info=GetCubeInfo(quantize_info,depth,maximum_colors);
2881 if (cube_info == (CubeInfo *) NULL)
2882 {
cristy8a11cb12011-10-19 23:53:34 +00002883 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00002884 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
2885 return(MagickFalse);
2886 }
2887 number_images=GetImageListLength(images);
2888 image=images;
2889 for (i=0; image != (Image *) NULL; i++)
2890 {
2891 progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor) NULL,
2892 image->client_data);
cristy8a11cb12011-10-19 23:53:34 +00002893 status=ClassifyImageColors(cube_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002894 if (status == MagickFalse)
2895 break;
2896 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
cristycee97112010-05-28 00:44:52 +00002897 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i,
2898 number_images);
cristy3ed852e2009-09-05 21:47:34 +00002899 if (proceed == MagickFalse)
2900 break;
2901 image=GetNextImageInList(image);
2902 }
2903 if (status != MagickFalse)
2904 {
2905 /*
2906 Reduce the number of colors in an image sequence.
2907 */
2908 ReduceImageColors(images,cube_info);
2909 image=images;
2910 for (i=0; image != (Image *) NULL; i++)
2911 {
2912 progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor)
2913 NULL,image->client_data);
cristy018f07f2011-09-04 21:15:19 +00002914 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00002915 if (status == MagickFalse)
2916 break;
2917 (void) SetImageProgressMonitor(image,progress_monitor,
2918 image->client_data);
cristycee97112010-05-28 00:44:52 +00002919 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i,
2920 number_images);
cristy3ed852e2009-09-05 21:47:34 +00002921 if (proceed == MagickFalse)
2922 break;
2923 image=GetNextImageInList(image);
2924 }
2925 }
2926 DestroyCubeInfo(cube_info);
2927 return(status);
2928}
2929
2930/*
2931%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2932% %
2933% %
2934% %
2935+ R e d u c e %
2936% %
2937% %
2938% %
2939%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2940%
2941% Reduce() traverses the color cube tree and prunes any node whose
2942% quantization error falls below a particular threshold.
2943%
2944% The format of the Reduce method is:
2945%
2946% Reduce(const Image *image,CubeInfo *cube_info,const NodeInfo *node_info)
2947%
2948% A description of each parameter follows.
2949%
2950% o image: the image.
2951%
2952% o cube_info: A pointer to the Cube structure.
2953%
2954% o node_info: pointer to node in color cube tree that is to be pruned.
2955%
2956*/
2957static void Reduce(const Image *image,CubeInfo *cube_info,
2958 const NodeInfo *node_info)
2959{
cristybb503372010-05-27 20:51:26 +00002960 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002961 i;
2962
cristybb503372010-05-27 20:51:26 +00002963 size_t
cristy3ed852e2009-09-05 21:47:34 +00002964 number_children;
2965
2966 /*
2967 Traverse any children.
2968 */
2969 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002970 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002971 if (node_info->child[i] != (NodeInfo *) NULL)
2972 Reduce(image,cube_info,node_info->child[i]);
2973 if (node_info->quantize_error <= cube_info->pruning_threshold)
2974 PruneChild(image,cube_info,node_info);
2975 else
2976 {
2977 /*
2978 Find minimum pruning threshold.
2979 */
2980 if (node_info->number_unique > 0)
2981 cube_info->colors++;
2982 if (node_info->quantize_error < cube_info->next_threshold)
2983 cube_info->next_threshold=node_info->quantize_error;
2984 }
2985}
2986
2987/*
2988%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2989% %
2990% %
2991% %
2992+ R e d u c e I m a g e C o l o r s %
2993% %
2994% %
2995% %
2996%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2997%
2998% ReduceImageColors() repeatedly prunes the tree until the number of nodes
2999% with n2 > 0 is less than or equal to the maximum number of colors allowed
3000% in the output image. On any given iteration over the tree, it selects
3001% those nodes whose E value is minimal for pruning and merges their
3002% color statistics upward. It uses a pruning threshold, Ep, to govern
3003% node selection as follows:
3004%
3005% Ep = 0
3006% while number of nodes with (n2 > 0) > required maximum number of colors
3007% prune all nodes such that E <= Ep
3008% Set Ep to minimum E in remaining nodes
3009%
3010% This has the effect of minimizing any quantization error when merging
3011% two nodes together.
3012%
3013% When a node to be pruned has offspring, the pruning procedure invokes
3014% itself recursively in order to prune the tree from the leaves upward.
3015% n2, Sr, Sg, and Sb in a node being pruned are always added to the
3016% corresponding data in that node's parent. This retains the pruned
3017% node's color characteristics for later averaging.
3018%
3019% For each node, n2 pixels exist for which that node represents the
3020% smallest volume in RGB space containing those pixel's colors. When n2
3021% > 0 the node will uniquely define a color in the output image. At the
3022% beginning of reduction, n2 = 0 for all nodes except a the leaves of
3023% the tree which represent colors present in the input image.
3024%
3025% The other pixel count, n1, indicates the total number of colors
3026% within the cubic volume which the node represents. This includes n1 -
3027% n2 pixels whose colors should be defined by nodes at a lower level in
3028% the tree.
3029%
3030% The format of the ReduceImageColors method is:
3031%
3032% ReduceImageColors(const Image *image,CubeInfo *cube_info)
3033%
3034% A description of each parameter follows.
3035%
3036% o image: the image.
3037%
3038% o cube_info: A pointer to the Cube structure.
3039%
3040*/
3041static void ReduceImageColors(const Image *image,CubeInfo *cube_info)
3042{
3043#define ReduceImageTag "Reduce/Image"
3044
3045 MagickBooleanType
3046 proceed;
3047
3048 MagickOffsetType
3049 offset;
3050
cristybb503372010-05-27 20:51:26 +00003051 size_t
cristy3ed852e2009-09-05 21:47:34 +00003052 span;
3053
3054 cube_info->next_threshold=0.0;
3055 for (span=cube_info->colors; cube_info->colors > cube_info->maximum_colors; )
3056 {
3057 cube_info->pruning_threshold=cube_info->next_threshold;
3058 cube_info->next_threshold=cube_info->root->quantize_error-1;
3059 cube_info->colors=0;
3060 Reduce(image,cube_info,cube_info->root);
3061 offset=(MagickOffsetType) span-cube_info->colors;
3062 proceed=SetImageProgress(image,ReduceImageTag,offset,span-
3063 cube_info->maximum_colors+1);
3064 if (proceed == MagickFalse)
3065 break;
3066 }
3067}
3068
3069/*
3070%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3071% %
3072% %
3073% %
3074% R e m a p I m a g e %
3075% %
3076% %
3077% %
3078%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3079%
anthony31f1bf72012-01-30 12:37:22 +00003080% RemapImage() replaces the colors of an image with a dither of the colors
3081% provided.
cristy3ed852e2009-09-05 21:47:34 +00003082%
3083% The format of the RemapImage method is:
3084%
3085% MagickBooleanType RemapImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003086% Image *image,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003087%
3088% A description of each parameter follows:
3089%
3090% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
3091%
3092% o image: the image.
3093%
3094% o remap_image: the reference image.
3095%
cristy018f07f2011-09-04 21:15:19 +00003096% o exception: return any errors or warnings in this structure.
3097%
cristy3ed852e2009-09-05 21:47:34 +00003098*/
3099MagickExport MagickBooleanType RemapImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003100 Image *image,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003101{
3102 CubeInfo
3103 *cube_info;
3104
3105 MagickBooleanType
3106 status;
3107
3108 /*
3109 Initialize color cube.
3110 */
3111 assert(image != (Image *) NULL);
3112 assert(image->signature == MagickSignature);
3113 if (image->debug != MagickFalse)
3114 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3115 assert(remap_image != (Image *) NULL);
3116 assert(remap_image->signature == MagickSignature);
3117 cube_info=GetCubeInfo(quantize_info,MaxTreeDepth,
3118 quantize_info->number_colors);
3119 if (cube_info == (CubeInfo *) NULL)
3120 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3121 image->filename);
cristy8a11cb12011-10-19 23:53:34 +00003122 status=ClassifyImageColors(cube_info,remap_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003123 if (status != MagickFalse)
3124 {
3125 /*
3126 Classify image colors from the reference image.
3127 */
3128 cube_info->quantize_info->number_colors=cube_info->colors;
cristy018f07f2011-09-04 21:15:19 +00003129 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003130 }
3131 DestroyCubeInfo(cube_info);
3132 return(status);
3133}
3134
3135/*
3136%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3137% %
3138% %
3139% %
3140% R e m a p I m a g e s %
3141% %
3142% %
3143% %
3144%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3145%
3146% RemapImages() replaces the colors of a sequence of images with the
3147% closest color from a reference image.
3148%
3149% The format of the RemapImage method is:
3150%
3151% MagickBooleanType RemapImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003152% Image *images,Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003153%
3154% A description of each parameter follows:
3155%
3156% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
3157%
3158% o images: the image sequence.
3159%
3160% o remap_image: the reference image.
3161%
cristy018f07f2011-09-04 21:15:19 +00003162% o exception: return any errors or warnings in this structure.
3163%
cristy3ed852e2009-09-05 21:47:34 +00003164*/
3165MagickExport MagickBooleanType RemapImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003166 Image *images,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003167{
3168 CubeInfo
3169 *cube_info;
3170
3171 Image
3172 *image;
3173
3174 MagickBooleanType
3175 status;
3176
3177 assert(images != (Image *) NULL);
3178 assert(images->signature == MagickSignature);
3179 if (images->debug != MagickFalse)
3180 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
3181 image=images;
3182 if (remap_image == (Image *) NULL)
3183 {
3184 /*
3185 Create a global colormap for an image sequence.
3186 */
cristy018f07f2011-09-04 21:15:19 +00003187 status=QuantizeImages(quantize_info,images,exception);
cristy3ed852e2009-09-05 21:47:34 +00003188 return(status);
3189 }
3190 /*
3191 Classify image colors from the reference image.
3192 */
3193 cube_info=GetCubeInfo(quantize_info,MaxTreeDepth,
3194 quantize_info->number_colors);
3195 if (cube_info == (CubeInfo *) NULL)
3196 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3197 image->filename);
cristy018f07f2011-09-04 21:15:19 +00003198 status=ClassifyImageColors(cube_info,remap_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003199 if (status != MagickFalse)
3200 {
3201 /*
3202 Classify image colors from the reference image.
3203 */
3204 cube_info->quantize_info->number_colors=cube_info->colors;
3205 image=images;
3206 for ( ; image != (Image *) NULL; image=GetNextImageInList(image))
3207 {
cristy018f07f2011-09-04 21:15:19 +00003208 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003209 if (status == MagickFalse)
3210 break;
3211 }
3212 }
3213 DestroyCubeInfo(cube_info);
3214 return(status);
3215}
3216
3217/*
3218%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3219% %
3220% %
3221% %
3222% S e t G r a y s c a l e I m a g e %
3223% %
3224% %
3225% %
3226%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3227%
3228% SetGrayscaleImage() converts an image to a PseudoClass grayscale image.
3229%
3230% The format of the SetGrayscaleImage method is:
3231%
cristy018f07f2011-09-04 21:15:19 +00003232% MagickBooleanType SetGrayscaleImage(Image *image,ExceptionInfo *exeption)
cristy3ed852e2009-09-05 21:47:34 +00003233%
3234% A description of each parameter follows:
3235%
3236% o image: The image.
3237%
cristy018f07f2011-09-04 21:15:19 +00003238% o exception: return any errors or warnings in this structure.
3239%
cristy3ed852e2009-09-05 21:47:34 +00003240*/
3241
3242#if defined(__cplusplus) || defined(c_plusplus)
3243extern "C" {
3244#endif
3245
3246static int IntensityCompare(const void *x,const void *y)
3247{
cristy101ab702011-10-13 13:06:32 +00003248 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003249 *color_1,
3250 *color_2;
3251
cristyecc31b12011-02-13 00:32:29 +00003252 ssize_t
3253 intensity;
3254
cristy101ab702011-10-13 13:06:32 +00003255 color_1=(PixelInfo *) x;
3256 color_2=(PixelInfo *) y;
3257 intensity=GetPixelInfoIntensity(color_1)-(ssize_t)
3258 GetPixelInfoIntensity(color_2);
cristycee97112010-05-28 00:44:52 +00003259 return((int) intensity);
cristy3ed852e2009-09-05 21:47:34 +00003260}
3261
3262#if defined(__cplusplus) || defined(c_plusplus)
3263}
3264#endif
3265
cristy018f07f2011-09-04 21:15:19 +00003266static MagickBooleanType SetGrayscaleImage(Image *image,
3267 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003268{
cristyc4c8d132010-01-07 01:58:38 +00003269 CacheView
3270 *image_view;
3271
cristyecc31b12011-02-13 00:32:29 +00003272 MagickBooleanType
3273 status;
cristy3ed852e2009-09-05 21:47:34 +00003274
cristy101ab702011-10-13 13:06:32 +00003275 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003276 *colormap;
3277
cristybb503372010-05-27 20:51:26 +00003278 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003279 i;
3280
cristyecc31b12011-02-13 00:32:29 +00003281 ssize_t
3282 *colormap_index,
3283 j,
3284 y;
cristy3ed852e2009-09-05 21:47:34 +00003285
cristy3ed852e2009-09-05 21:47:34 +00003286 assert(image != (Image *) NULL);
3287 assert(image->signature == MagickSignature);
3288 if (image->type != GrayscaleType)
cristye941a752011-10-15 01:52:48 +00003289 (void) TransformImageColorspace(image,GRAYColorspace,exception);
cristybb503372010-05-27 20:51:26 +00003290 colormap_index=(ssize_t *) AcquireQuantumMemory(MaxMap+1,
cristy3ed852e2009-09-05 21:47:34 +00003291 sizeof(*colormap_index));
cristybb503372010-05-27 20:51:26 +00003292 if (colormap_index == (ssize_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003293 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3294 image->filename);
3295 if (image->storage_class != PseudoClass)
3296 {
cristybb503372010-05-27 20:51:26 +00003297 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy3ed852e2009-09-05 21:47:34 +00003298 colormap_index[i]=(-1);
cristy018f07f2011-09-04 21:15:19 +00003299 if (AcquireImageColormap(image,MaxMap+1,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003300 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3301 image->filename);
3302 image->colors=0;
3303 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00003304 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00003305#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003306 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003307#endif
cristybb503372010-05-27 20:51:26 +00003308 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003309 {
cristy4c08aed2011-07-01 19:47:50 +00003310 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003311 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003312
cristyecc31b12011-02-13 00:32:29 +00003313 register ssize_t
3314 x;
3315
cristy3ed852e2009-09-05 21:47:34 +00003316 if (status == MagickFalse)
3317 continue;
3318 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3319 exception);
cristyacd2ed22011-08-30 01:44:23 +00003320 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003321 {
3322 status=MagickFalse;
3323 continue;
3324 }
cristybb503372010-05-27 20:51:26 +00003325 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003326 {
cristybb503372010-05-27 20:51:26 +00003327 register size_t
cristy3ed852e2009-09-05 21:47:34 +00003328 intensity;
3329
cristy4c08aed2011-07-01 19:47:50 +00003330 intensity=ScaleQuantumToMap(GetPixelRed(image,q));
cristy3ed852e2009-09-05 21:47:34 +00003331 if (colormap_index[intensity] < 0)
3332 {
cristyb5d5f722009-11-04 03:03:49 +00003333#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003334 #pragma omp critical (MagickCore_SetGrayscaleImage)
3335#endif
3336 if (colormap_index[intensity] < 0)
3337 {
cristybb503372010-05-27 20:51:26 +00003338 colormap_index[intensity]=(ssize_t) image->colors;
cristye42f6582012-02-11 17:59:50 +00003339 image->colormap[image->colors].red=(double)
3340 GetPixelRed(image,q);
3341 image->colormap[image->colors].green=(double)
3342 GetPixelGreen(image,q);
3343 image->colormap[image->colors].blue=(double)
3344 GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00003345 image->colors++;
3346 }
3347 }
cristy4c08aed2011-07-01 19:47:50 +00003348 SetPixelIndex(image,(Quantum)
3349 colormap_index[intensity],q);
cristyed231572011-07-14 02:18:59 +00003350 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003351 }
3352 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3353 status=MagickFalse;
3354 }
3355 image_view=DestroyCacheView(image_view);
3356 }
cristybb503372010-05-27 20:51:26 +00003357 for (i=0; i < (ssize_t) image->colors; i++)
cristye42f6582012-02-11 17:59:50 +00003358 image->colormap[i].alpha=(double) i;
cristy101ab702011-10-13 13:06:32 +00003359 qsort((void *) image->colormap,image->colors,sizeof(PixelInfo),
cristy3ed852e2009-09-05 21:47:34 +00003360 IntensityCompare);
cristy101ab702011-10-13 13:06:32 +00003361 colormap=(PixelInfo *) AcquireQuantumMemory(image->colors,
cristy3ed852e2009-09-05 21:47:34 +00003362 sizeof(*colormap));
cristy101ab702011-10-13 13:06:32 +00003363 if (colormap == (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003364 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3365 image->filename);
3366 j=0;
3367 colormap[j]=image->colormap[0];
cristybb503372010-05-27 20:51:26 +00003368 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003369 {
cristy101ab702011-10-13 13:06:32 +00003370 if (IsPixelInfoEquivalent(&colormap[j],&image->colormap[i]) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003371 {
3372 j++;
3373 colormap[j]=image->colormap[i];
3374 }
cristy4c08aed2011-07-01 19:47:50 +00003375 colormap_index[(ssize_t) image->colormap[i].alpha]=j;
cristy3ed852e2009-09-05 21:47:34 +00003376 }
cristybb503372010-05-27 20:51:26 +00003377 image->colors=(size_t) (j+1);
cristy101ab702011-10-13 13:06:32 +00003378 image->colormap=(PixelInfo *) RelinquishMagickMemory(image->colormap);
cristy3ed852e2009-09-05 21:47:34 +00003379 image->colormap=colormap;
3380 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00003381 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00003382#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003383 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003384#endif
cristybb503372010-05-27 20:51:26 +00003385 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003386 {
cristy4c08aed2011-07-01 19:47:50 +00003387 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003388 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003389
cristyecc31b12011-02-13 00:32:29 +00003390 register ssize_t
3391 x;
3392
cristy3ed852e2009-09-05 21:47:34 +00003393 if (status == MagickFalse)
3394 continue;
3395 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003396 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003397 {
3398 status=MagickFalse;
3399 continue;
3400 }
cristybb503372010-05-27 20:51:26 +00003401 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00003402 {
3403 SetPixelIndex(image,(Quantum) colormap_index[ScaleQuantumToMap(
3404 GetPixelIndex(image,q))],q);
cristyed231572011-07-14 02:18:59 +00003405 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00003406 }
cristy3ed852e2009-09-05 21:47:34 +00003407 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3408 status=MagickFalse;
3409 }
3410 image_view=DestroyCacheView(image_view);
cristybb503372010-05-27 20:51:26 +00003411 colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index);
cristy3ed852e2009-09-05 21:47:34 +00003412 image->type=GrayscaleType;
cristy8a11cb12011-10-19 23:53:34 +00003413 if (IsImageMonochrome(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003414 image->type=BilevelType;
3415 return(status);
3416}