blob: 02574e1e0ace734658703ba59629307836baee27 [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) &&
cristy510d06a2011-07-06 23:43:54 +0000517 (IsRGBColorspace(image->colorspace) == MagickFalse) &&
cristy3ed852e2009-09-05 21:47:34 +0000518 (image->colorspace != CMYColorspace))
cristye941a752011-10-15 01:52:48 +0000519 (void) TransformImageColorspace((Image *) image,RGBColorspace,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 {
cristy4c08aed2011-07-01 19:47:50 +0000623 SetPixelRed(image,image->colormap[index].red,q);
624 SetPixelGreen(image,image->colormap[index].green,q);
625 SetPixelBlue(image,image->colormap[index].blue,q);
cristye9717ac2011-02-20 16:17:17 +0000626 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000627 SetPixelAlpha(image,image->colormap[index].alpha,q);
cristy3ed852e2009-09-05 21:47:34 +0000628 }
cristyed231572011-07-14 02:18:59 +0000629 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000630 }
631 }
632 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristye9717ac2011-02-20 16:17:17 +0000633 status=MagickFalse;
634 if (image->progress_monitor != (MagickProgressMonitor) NULL)
635 {
636 MagickBooleanType
637 proceed;
638
639#if defined(MAGICKCORE_OPENMP_SUPPORT)
640 #pragma omp critical (MagickCore_AssignImageColors)
641#endif
642 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y,
643 image->rows);
644 if (proceed == MagickFalse)
645 status=MagickFalse;
646 }
cristy3ed852e2009-09-05 21:47:34 +0000647 }
648 image_view=DestroyCacheView(image_view);
649 }
650 if (cube_info->quantize_info->measure_error != MagickFalse)
cristy8a11cb12011-10-19 23:53:34 +0000651 (void) GetImageQuantizeError(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000652 if ((cube_info->quantize_info->number_colors == 2) &&
653 (cube_info->quantize_info->colorspace == GRAYColorspace))
654 {
655 Quantum
656 intensity;
657
cristy101ab702011-10-13 13:06:32 +0000658 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +0000659 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +0000660
cristye9717ac2011-02-20 16:17:17 +0000661 register ssize_t
662 i;
663
cristy3ed852e2009-09-05 21:47:34 +0000664 /*
665 Monochrome image.
666 */
667 q=image->colormap;
cristybb503372010-05-27 20:51:26 +0000668 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +0000669 {
cristy101ab702011-10-13 13:06:32 +0000670 intensity=(Quantum) ((MagickRealType) GetPixelInfoIntensity(q) <
cristy4c08aed2011-07-01 19:47:50 +0000671 ((MagickRealType) QuantumRange/2.0) ? 0 : QuantumRange);
672 q->red=intensity;
673 q->green=intensity;
674 q->blue=intensity;
cristy3ed852e2009-09-05 21:47:34 +0000675 q++;
676 }
677 }
cristyea1a8aa2011-10-20 13:24:06 +0000678 (void) SyncImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +0000679 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
680 (cube_info->quantize_info->colorspace != CMYKColorspace))
cristye941a752011-10-15 01:52:48 +0000681 (void) TransformImageColorspace((Image *) image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000682 return(MagickTrue);
683}
684
685/*
686%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
687% %
688% %
689% %
690+ C l a s s i f y I m a g e C o l o r s %
691% %
692% %
693% %
694%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
695%
696% ClassifyImageColors() begins by initializing a color description tree
697% of sufficient depth to represent each possible input color in a leaf.
698% However, it is impractical to generate a fully-formed color
699% description tree in the storage_class phase for realistic values of
700% Cmax. If colors components in the input image are quantized to k-bit
701% precision, so that Cmax= 2k-1, the tree would need k levels below the
702% root node to allow representing each possible input color in a leaf.
703% This becomes prohibitive because the tree's total number of nodes is
704% 1 + sum(i=1,k,8k).
705%
706% A complete tree would require 19,173,961 nodes for k = 8, Cmax = 255.
707% Therefore, to avoid building a fully populated tree, QUANTIZE: (1)
708% Initializes data structures for nodes only as they are needed; (2)
709% Chooses a maximum depth for the tree as a function of the desired
710% number of colors in the output image (currently log2(colormap size)).
711%
712% For each pixel in the input image, storage_class scans downward from
713% the root of the color description tree. At each level of the tree it
714% identifies the single node which represents a cube in RGB space
715% containing It updates the following data for each such node:
716%
717% n1 : Number of pixels whose color is contained in the RGB cube
718% which this node represents;
719%
720% n2 : Number of pixels whose color is not represented in a node at
721% lower depth in the tree; initially, n2 = 0 for all nodes except
722% leaves of the tree.
723%
724% Sr, Sg, Sb : Sums of the red, green, and blue component values for
725% all pixels not classified at a lower depth. The combination of
726% these sums and n2 will ultimately characterize the mean color of a
727% set of pixels represented by this node.
728%
729% E: the distance squared in RGB space between each pixel contained
730% within a node and the nodes' center. This represents the quantization
731% error for a node.
732%
733% The format of the ClassifyImageColors() method is:
734%
735% MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
736% const Image *image,ExceptionInfo *exception)
737%
738% A description of each parameter follows.
739%
740% o cube_info: A pointer to the Cube structure.
741%
742% o image: the image.
743%
744*/
745
746static inline void SetAssociatedAlpha(const Image *image,CubeInfo *cube_info)
747{
748 MagickBooleanType
749 associate_alpha;
750
751 associate_alpha=image->matte;
752 if (cube_info->quantize_info->colorspace == TransparentColorspace)
753 associate_alpha=MagickFalse;
754 if ((cube_info->quantize_info->number_colors == 2) &&
755 (cube_info->quantize_info->colorspace == GRAYColorspace))
756 associate_alpha=MagickFalse;
757 cube_info->associate_alpha=associate_alpha;
758}
759
760static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info,
761 const Image *image,ExceptionInfo *exception)
762{
763#define ClassifyImageTag "Classify/Image"
764
cristyc4c8d132010-01-07 01:58:38 +0000765 CacheView
766 *image_view;
767
cristy3ed852e2009-09-05 21:47:34 +0000768 MagickBooleanType
769 proceed;
770
771 MagickRealType
772 bisect;
773
774 NodeInfo
775 *node_info;
776
cristy101ab702011-10-13 13:06:32 +0000777 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +0000778 error,
779 mid,
780 midpoint,
781 pixel;
782
783 size_t
cristyecc31b12011-02-13 00:32:29 +0000784 count,
cristy3ed852e2009-09-05 21:47:34 +0000785 id,
786 index,
787 level;
788
cristyecc31b12011-02-13 00:32:29 +0000789 ssize_t
790 y;
791
cristy3ed852e2009-09-05 21:47:34 +0000792 /*
793 Classify the first cube_info->maximum_colors colors to a tree depth of 8.
794 */
795 SetAssociatedAlpha(image,cube_info);
796 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
797 (cube_info->quantize_info->colorspace != CMYKColorspace))
798 (void) TransformImageColorspace((Image *) image,
cristye941a752011-10-15 01:52:48 +0000799 cube_info->quantize_info->colorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000800 else
801 if ((image->colorspace != GRAYColorspace) &&
802 (image->colorspace != CMYColorspace) &&
cristy510d06a2011-07-06 23:43:54 +0000803 (IsRGBColorspace(image->colorspace) == MagickFalse))
cristye941a752011-10-15 01:52:48 +0000804 (void) TransformImageColorspace((Image *) image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +0000805 midpoint.red=(MagickRealType) QuantumRange/2.0;
806 midpoint.green=(MagickRealType) QuantumRange/2.0;
807 midpoint.blue=(MagickRealType) QuantumRange/2.0;
cristy4c08aed2011-07-01 19:47:50 +0000808 midpoint.alpha=(MagickRealType) QuantumRange/2.0;
809 error.alpha=0.0;
cristy3ed852e2009-09-05 21:47:34 +0000810 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000811 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000812 {
cristy4c08aed2011-07-01 19:47:50 +0000813 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000814 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000815
cristybb503372010-05-27 20:51:26 +0000816 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000817 x;
818
819 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000820 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000821 break;
822 if (cube_info->nodes > MaxNodes)
823 {
824 /*
825 Prune one level if the color tree is too large.
826 */
827 PruneLevel(image,cube_info,cube_info->root);
828 cube_info->depth--;
829 }
cristybb503372010-05-27 20:51:26 +0000830 for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count)
cristy3ed852e2009-09-05 21:47:34 +0000831 {
832 /*
833 Start at the root and descend the color cube tree.
834 */
cristybb66d9c2010-10-09 01:40:31 +0000835 for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000836 {
cristy101ab702011-10-13 13:06:32 +0000837 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000838 packet;
839
cristy101ab702011-10-13 13:06:32 +0000840 GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000841 if (IsPixelEquivalent(image,p,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000842 break;
cristy4c08aed2011-07-01 19:47:50 +0000843 }
844 AssociateAlphaPixel(image,cube_info,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +0000845 index=MaxTreeDepth-1;
846 bisect=((MagickRealType) QuantumRange+1.0)/2.0;
847 mid=midpoint;
848 node_info=cube_info->root;
849 for (level=1; level <= MaxTreeDepth; level++)
850 {
851 bisect*=0.5;
852 id=ColorToNodeId(cube_info,&pixel,index);
853 mid.red+=(id & 1) != 0 ? bisect : -bisect;
854 mid.green+=(id & 2) != 0 ? bisect : -bisect;
855 mid.blue+=(id & 4) != 0 ? bisect : -bisect;
cristy4c08aed2011-07-01 19:47:50 +0000856 mid.alpha+=(id & 8) != 0 ? bisect : -bisect;
cristy3ed852e2009-09-05 21:47:34 +0000857 if (node_info->child[id] == (NodeInfo *) NULL)
858 {
859 /*
860 Set colors of new node to contain pixel.
861 */
862 node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info);
863 if (node_info->child[id] == (NodeInfo *) NULL)
864 (void) ThrowMagickException(exception,GetMagickModule(),
865 ResourceLimitError,"MemoryAllocationFailed","`%s'",
866 image->filename);
867 if (level == MaxTreeDepth)
868 cube_info->colors++;
869 }
870 /*
871 Approximate the quantization error represented by this node.
872 */
873 node_info=node_info->child[id];
874 error.red=QuantumScale*(pixel.red-mid.red);
875 error.green=QuantumScale*(pixel.green-mid.green);
876 error.blue=QuantumScale*(pixel.blue-mid.blue);
877 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000878 error.alpha=QuantumScale*(pixel.alpha-mid.alpha);
cristy3ed852e2009-09-05 21:47:34 +0000879 node_info->quantize_error+=sqrt((double) (count*error.red*error.red+
880 count*error.green*error.green+count*error.blue*error.blue+
cristy4c08aed2011-07-01 19:47:50 +0000881 count*error.alpha*error.alpha));
cristy3ed852e2009-09-05 21:47:34 +0000882 cube_info->root->quantize_error+=node_info->quantize_error;
883 index--;
884 }
885 /*
886 Sum RGB for this leaf for later derivation of the mean cube color.
887 */
888 node_info->number_unique+=count;
889 node_info->total_color.red+=count*QuantumScale*pixel.red;
890 node_info->total_color.green+=count*QuantumScale*pixel.green;
891 node_info->total_color.blue+=count*QuantumScale*pixel.blue;
892 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000893 node_info->total_color.alpha+=count*QuantumScale*pixel.alpha;
cristyed231572011-07-14 02:18:59 +0000894 p+=count*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000895 }
896 if (cube_info->colors > cube_info->maximum_colors)
897 {
898 PruneToCubeDepth(image,cube_info,cube_info->root);
899 break;
900 }
cristycee97112010-05-28 00:44:52 +0000901 proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y,
902 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000903 if (proceed == MagickFalse)
904 break;
905 }
cristybb503372010-05-27 20:51:26 +0000906 for (y++; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +0000907 {
cristy4c08aed2011-07-01 19:47:50 +0000908 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +0000909 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +0000910
cristybb503372010-05-27 20:51:26 +0000911 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000912 x;
913
914 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000915 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +0000916 break;
917 if (cube_info->nodes > MaxNodes)
918 {
919 /*
920 Prune one level if the color tree is too large.
921 */
922 PruneLevel(image,cube_info,cube_info->root);
923 cube_info->depth--;
924 }
cristybb503372010-05-27 20:51:26 +0000925 for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count)
cristy3ed852e2009-09-05 21:47:34 +0000926 {
927 /*
928 Start at the root and descend the color cube tree.
929 */
cristybb66d9c2010-10-09 01:40:31 +0000930 for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++)
cristy4c08aed2011-07-01 19:47:50 +0000931 {
cristy101ab702011-10-13 13:06:32 +0000932 PixelInfo
cristy4c08aed2011-07-01 19:47:50 +0000933 packet;
934
cristy101ab702011-10-13 13:06:32 +0000935 GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet);
cristy4c08aed2011-07-01 19:47:50 +0000936 if (IsPixelEquivalent(image,p,&packet) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +0000937 break;
cristy4c08aed2011-07-01 19:47:50 +0000938 }
939 AssociateAlphaPixel(image,cube_info,p,&pixel);
cristy3ed852e2009-09-05 21:47:34 +0000940 index=MaxTreeDepth-1;
941 bisect=((MagickRealType) QuantumRange+1.0)/2.0;
942 mid=midpoint;
943 node_info=cube_info->root;
944 for (level=1; level <= cube_info->depth; level++)
945 {
946 bisect*=0.5;
947 id=ColorToNodeId(cube_info,&pixel,index);
948 mid.red+=(id & 1) != 0 ? bisect : -bisect;
949 mid.green+=(id & 2) != 0 ? bisect : -bisect;
950 mid.blue+=(id & 4) != 0 ? bisect : -bisect;
cristy4c08aed2011-07-01 19:47:50 +0000951 mid.alpha+=(id & 8) != 0 ? bisect : -bisect;
cristy3ed852e2009-09-05 21:47:34 +0000952 if (node_info->child[id] == (NodeInfo *) NULL)
953 {
954 /*
955 Set colors of new node to contain pixel.
956 */
957 node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info);
958 if (node_info->child[id] == (NodeInfo *) NULL)
959 (void) ThrowMagickException(exception,GetMagickModule(),
960 ResourceLimitError,"MemoryAllocationFailed","%s",
961 image->filename);
962 if (level == cube_info->depth)
963 cube_info->colors++;
964 }
965 /*
966 Approximate the quantization error represented by this node.
967 */
968 node_info=node_info->child[id];
969 error.red=QuantumScale*(pixel.red-mid.red);
970 error.green=QuantumScale*(pixel.green-mid.green);
971 error.blue=QuantumScale*(pixel.blue-mid.blue);
972 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000973 error.alpha=QuantumScale*(pixel.alpha-mid.alpha);
cristy3ed852e2009-09-05 21:47:34 +0000974 node_info->quantize_error+=sqrt((double) (count*error.red*error.red+
cristy83b6e792011-01-26 15:46:06 +0000975 count*error.green*error.green+count*error.blue*error.blue+
cristy4c08aed2011-07-01 19:47:50 +0000976 count*error.alpha*error.alpha));
cristy3ed852e2009-09-05 21:47:34 +0000977 cube_info->root->quantize_error+=node_info->quantize_error;
978 index--;
979 }
980 /*
981 Sum RGB for this leaf for later derivation of the mean cube color.
982 */
983 node_info->number_unique+=count;
984 node_info->total_color.red+=count*QuantumScale*pixel.red;
985 node_info->total_color.green+=count*QuantumScale*pixel.green;
986 node_info->total_color.blue+=count*QuantumScale*pixel.blue;
987 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000988 node_info->total_color.alpha+=count*QuantumScale*pixel.alpha;
cristyed231572011-07-14 02:18:59 +0000989 p+=count*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +0000990 }
cristycee97112010-05-28 00:44:52 +0000991 proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y,
992 image->rows);
cristy3ed852e2009-09-05 21:47:34 +0000993 if (proceed == MagickFalse)
994 break;
995 }
996 image_view=DestroyCacheView(image_view);
997 if ((cube_info->quantize_info->colorspace != UndefinedColorspace) &&
998 (cube_info->quantize_info->colorspace != CMYKColorspace))
cristye941a752011-10-15 01:52:48 +0000999 (void) TransformImageColorspace((Image *) image,RGBColorspace,exception);
cristy3ed852e2009-09-05 21:47:34 +00001000 return(MagickTrue);
1001}
1002
1003/*
1004%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1005% %
1006% %
1007% %
1008% C l o n e Q u a n t i z e I n f o %
1009% %
1010% %
1011% %
1012%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1013%
1014% CloneQuantizeInfo() makes a duplicate of the given quantize info structure,
1015% or if quantize info is NULL, a new one.
1016%
1017% The format of the CloneQuantizeInfo method is:
1018%
1019% QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info)
1020%
1021% A description of each parameter follows:
1022%
1023% o clone_info: Method CloneQuantizeInfo returns a duplicate of the given
1024% quantize info, or if image info is NULL a new one.
1025%
1026% o quantize_info: a structure of type info.
1027%
1028*/
1029MagickExport QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info)
1030{
1031 QuantizeInfo
1032 *clone_info;
1033
cristy73bd4a52010-10-05 11:24:23 +00001034 clone_info=(QuantizeInfo *) AcquireMagickMemory(sizeof(*clone_info));
cristy3ed852e2009-09-05 21:47:34 +00001035 if (clone_info == (QuantizeInfo *) NULL)
1036 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1037 GetQuantizeInfo(clone_info);
1038 if (quantize_info == (QuantizeInfo *) NULL)
1039 return(clone_info);
1040 clone_info->number_colors=quantize_info->number_colors;
1041 clone_info->tree_depth=quantize_info->tree_depth;
1042 clone_info->dither=quantize_info->dither;
1043 clone_info->dither_method=quantize_info->dither_method;
1044 clone_info->colorspace=quantize_info->colorspace;
1045 clone_info->measure_error=quantize_info->measure_error;
1046 return(clone_info);
1047}
1048
1049/*
1050%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1051% %
1052% %
1053% %
1054+ C l o s e s t C o l o r %
1055% %
1056% %
1057% %
1058%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1059%
1060% ClosestColor() traverses the color cube tree at a particular node and
1061% determines which colormap entry best represents the input color.
1062%
1063% The format of the ClosestColor method is:
1064%
1065% void ClosestColor(const Image *image,CubeInfo *cube_info,
1066% const NodeInfo *node_info)
1067%
1068% A description of each parameter follows.
1069%
1070% o image: the image.
1071%
1072% o cube_info: A pointer to the Cube structure.
1073%
1074% o node_info: the address of a structure of type NodeInfo which points to a
1075% node in the color cube tree that is to be pruned.
1076%
1077*/
1078static void ClosestColor(const Image *image,CubeInfo *cube_info,
1079 const NodeInfo *node_info)
1080{
cristybb503372010-05-27 20:51:26 +00001081 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001082 i;
1083
cristybb503372010-05-27 20:51:26 +00001084 size_t
cristy3ed852e2009-09-05 21:47:34 +00001085 number_children;
1086
1087 /*
1088 Traverse any children.
1089 */
1090 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00001091 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00001092 if (node_info->child[i] != (NodeInfo *) NULL)
1093 ClosestColor(image,cube_info,node_info->child[i]);
1094 if (node_info->number_unique != 0)
1095 {
1096 MagickRealType
1097 pixel;
1098
1099 register MagickRealType
1100 alpha,
1101 beta,
1102 distance;
1103
cristy101ab702011-10-13 13:06:32 +00001104 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001105 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00001106
cristy101ab702011-10-13 13:06:32 +00001107 register RealPixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001108 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001109
1110 /*
1111 Determine if this color is "closest".
1112 */
1113 p=image->colormap+node_info->color_number;
1114 q=(&cube_info->target);
1115 alpha=1.0;
1116 beta=1.0;
cristy847620f2011-02-09 02:24:21 +00001117 if (cube_info->associate_alpha != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001118 {
cristy4c08aed2011-07-01 19:47:50 +00001119 alpha=(MagickRealType) (QuantumScale*p->alpha);
1120 beta=(MagickRealType) (QuantumScale*q->alpha);
cristy3ed852e2009-09-05 21:47:34 +00001121 }
cristy4c08aed2011-07-01 19:47:50 +00001122 pixel=alpha*p->red-beta*q->red;
cristy3ed852e2009-09-05 21:47:34 +00001123 distance=pixel*pixel;
cristy36fbc3b2011-02-09 02:30:04 +00001124 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001125 {
cristy4c08aed2011-07-01 19:47:50 +00001126 pixel=alpha*p->green-beta*q->green;
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->blue-beta*q->blue;
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 {
1134 pixel=alpha-beta;
1135 distance+=pixel*pixel;
cristyc4080402011-02-09 02:55:58 +00001136 if (distance <= cube_info->distance)
cristy3ed852e2009-09-05 21:47:34 +00001137 {
1138 cube_info->distance=distance;
1139 cube_info->color_number=node_info->color_number;
1140 }
1141 }
1142 }
1143 }
1144 }
1145}
1146
1147/*
1148%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1149% %
1150% %
1151% %
1152% C o m p r e s s I m a g e C o l o r m a p %
1153% %
1154% %
1155% %
1156%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1157%
1158% CompressImageColormap() compresses an image colormap by removing any
1159% duplicate or unused color entries.
1160%
1161% The format of the CompressImageColormap method is:
1162%
cristy018f07f2011-09-04 21:15:19 +00001163% MagickBooleanType CompressImageColormap(Image *image,
1164% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001165%
1166% A description of each parameter follows:
1167%
1168% o image: the image.
1169%
cristy018f07f2011-09-04 21:15:19 +00001170% o exception: return any errors or warnings in this structure.
1171%
cristy3ed852e2009-09-05 21:47:34 +00001172*/
cristy018f07f2011-09-04 21:15:19 +00001173MagickExport MagickBooleanType CompressImageColormap(Image *image,
1174 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001175{
1176 QuantizeInfo
1177 quantize_info;
1178
1179 assert(image != (Image *) NULL);
1180 assert(image->signature == MagickSignature);
1181 if (image->debug != MagickFalse)
1182 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy8a11cb12011-10-19 23:53:34 +00001183 if (IsPaletteImage(image,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001184 return(MagickFalse);
1185 GetQuantizeInfo(&quantize_info);
1186 quantize_info.number_colors=image->colors;
1187 quantize_info.tree_depth=MaxTreeDepth;
cristy018f07f2011-09-04 21:15:19 +00001188 return(QuantizeImage(&quantize_info,image,exception));
cristy3ed852e2009-09-05 21:47:34 +00001189}
1190
1191/*
1192%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1193% %
1194% %
1195% %
1196+ D e f i n e I m a g e C o l o r m a p %
1197% %
1198% %
1199% %
1200%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1201%
1202% DefineImageColormap() traverses the color cube tree and notes each colormap
1203% entry. A colormap entry is any node in the color cube tree where the
1204% of unique colors is not zero. DefineImageColormap() returns the number of
1205% colors in the image colormap.
1206%
1207% The format of the DefineImageColormap method is:
1208%
cristybb503372010-05-27 20:51:26 +00001209% size_t DefineImageColormap(Image *image,CubeInfo *cube_info,
cristy3ed852e2009-09-05 21:47:34 +00001210% NodeInfo *node_info)
1211%
1212% A description of each parameter follows.
1213%
1214% o image: the image.
1215%
1216% o cube_info: A pointer to the Cube structure.
1217%
1218% o node_info: the address of a structure of type NodeInfo which points to a
1219% node in the color cube tree that is to be pruned.
1220%
1221*/
cristybb503372010-05-27 20:51:26 +00001222static size_t DefineImageColormap(Image *image,CubeInfo *cube_info,
cristy3ed852e2009-09-05 21:47:34 +00001223 NodeInfo *node_info)
1224{
cristybb503372010-05-27 20:51:26 +00001225 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001226 i;
1227
cristybb503372010-05-27 20:51:26 +00001228 size_t
cristy3ed852e2009-09-05 21:47:34 +00001229 number_children;
1230
1231 /*
1232 Traverse any children.
1233 */
1234 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00001235 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00001236 if (node_info->child[i] != (NodeInfo *) NULL)
cristycee97112010-05-28 00:44:52 +00001237 (void) DefineImageColormap(image,cube_info,node_info->child[i]);
cristy3ed852e2009-09-05 21:47:34 +00001238 if (node_info->number_unique != 0)
1239 {
1240 register MagickRealType
1241 alpha;
1242
cristy101ab702011-10-13 13:06:32 +00001243 register PixelInfo
cristyc47d1f82009-11-26 01:44:43 +00001244 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001245
1246 /*
1247 Colormap entry is defined by the mean color in this cube.
1248 */
1249 q=image->colormap+image->colors;
1250 alpha=(MagickRealType) ((MagickOffsetType) node_info->number_unique);
1251 alpha=1.0/(fabs(alpha) <= MagickEpsilon ? 1.0 : alpha);
1252 if (cube_info->associate_alpha == MagickFalse)
1253 {
cristy4c08aed2011-07-01 19:47:50 +00001254 q->red=ClampToQuantum((MagickRealType)
1255 (alpha*QuantumRange*node_info->total_color.red));
1256 q->green=ClampToQuantum((MagickRealType)
1257 (alpha*QuantumRange*node_info->total_color.green));
1258 q->blue=ClampToQuantum((MagickRealType)
1259 (alpha*QuantumRange*node_info->total_color.blue));
1260 q->alpha=OpaqueAlpha;
cristy3ed852e2009-09-05 21:47:34 +00001261 }
1262 else
1263 {
1264 MagickRealType
1265 opacity;
1266
1267 opacity=(MagickRealType) (alpha*QuantumRange*
cristy4c08aed2011-07-01 19:47:50 +00001268 node_info->total_color.alpha);
1269 q->alpha=ClampToQuantum(opacity);
1270 if (q->alpha == OpaqueAlpha)
cristy3ed852e2009-09-05 21:47:34 +00001271 {
cristy4c08aed2011-07-01 19:47:50 +00001272 q->red=ClampToQuantum((MagickRealType)
1273 (alpha*QuantumRange*node_info->total_color.red));
1274 q->green=ClampToQuantum((MagickRealType)
1275 (alpha*QuantumRange*node_info->total_color.green));
1276 q->blue=ClampToQuantum((MagickRealType)
1277 (alpha*QuantumRange*node_info->total_color.blue));
cristy3ed852e2009-09-05 21:47:34 +00001278 }
1279 else
1280 {
1281 MagickRealType
1282 gamma;
1283
cristy4c08aed2011-07-01 19:47:50 +00001284 gamma=(MagickRealType) (QuantumScale*q->alpha);
cristy3ed852e2009-09-05 21:47:34 +00001285 gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma);
cristy4c08aed2011-07-01 19:47:50 +00001286 q->red=ClampToQuantum((MagickRealType)
1287 (alpha*gamma*QuantumRange*node_info->total_color.red));
1288 q->green=ClampToQuantum((MagickRealType)
1289 (alpha*gamma*QuantumRange*node_info->total_color.green));
1290 q->blue=ClampToQuantum((MagickRealType)
1291 (alpha*gamma*QuantumRange*node_info->total_color.blue));
cristy3ed852e2009-09-05 21:47:34 +00001292 if (node_info->number_unique > cube_info->transparent_pixels)
1293 {
1294 cube_info->transparent_pixels=node_info->number_unique;
cristybb503372010-05-27 20:51:26 +00001295 cube_info->transparent_index=(ssize_t) image->colors;
cristy3ed852e2009-09-05 21:47:34 +00001296 }
1297 }
1298 }
1299 node_info->color_number=image->colors++;
1300 }
1301 return(image->colors);
1302}
1303
1304/*
1305%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1306% %
1307% %
1308% %
1309+ D e s t r o y C u b e I n f o %
1310% %
1311% %
1312% %
1313%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1314%
1315% DestroyCubeInfo() deallocates memory associated with an image.
1316%
1317% The format of the DestroyCubeInfo method is:
1318%
1319% DestroyCubeInfo(CubeInfo *cube_info)
1320%
1321% A description of each parameter follows:
1322%
1323% o cube_info: the address of a structure of type CubeInfo.
1324%
1325*/
1326static void DestroyCubeInfo(CubeInfo *cube_info)
1327{
1328 register Nodes
1329 *nodes;
1330
1331 /*
1332 Release color cube tree storage.
1333 */
1334 do
1335 {
1336 nodes=cube_info->node_queue->next;
1337 cube_info->node_queue->nodes=(NodeInfo *) RelinquishMagickMemory(
1338 cube_info->node_queue->nodes);
1339 cube_info->node_queue=(Nodes *) RelinquishMagickMemory(
1340 cube_info->node_queue);
1341 cube_info->node_queue=nodes;
1342 } while (cube_info->node_queue != (Nodes *) NULL);
cristybb503372010-05-27 20:51:26 +00001343 if (cube_info->cache != (ssize_t *) NULL)
1344 cube_info->cache=(ssize_t *) RelinquishMagickMemory(cube_info->cache);
cristy3ed852e2009-09-05 21:47:34 +00001345 cube_info->quantize_info=DestroyQuantizeInfo(cube_info->quantize_info);
1346 cube_info=(CubeInfo *) RelinquishMagickMemory(cube_info);
1347}
1348
1349/*
1350%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1351% %
1352% %
1353% %
1354% D e s t r o y Q u a n t i z e I n f o %
1355% %
1356% %
1357% %
1358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1359%
1360% DestroyQuantizeInfo() deallocates memory associated with an QuantizeInfo
1361% structure.
1362%
1363% The format of the DestroyQuantizeInfo method is:
1364%
1365% QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info)
1366%
1367% A description of each parameter follows:
1368%
1369% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
1370%
1371*/
1372MagickExport QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info)
1373{
1374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1375 assert(quantize_info != (QuantizeInfo *) NULL);
1376 assert(quantize_info->signature == MagickSignature);
1377 quantize_info->signature=(~MagickSignature);
1378 quantize_info=(QuantizeInfo *) RelinquishMagickMemory(quantize_info);
1379 return(quantize_info);
1380}
1381
1382/*
1383%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384% %
1385% %
1386% %
1387+ D i t h e r I m a g e %
1388% %
1389% %
1390% %
1391%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1392%
1393% DitherImage() distributes the difference between an original image and
1394% the corresponding color reduced algorithm to neighboring pixels using
1395% serpentine-scan Floyd-Steinberg error diffusion. DitherImage returns
1396% MagickTrue if the image is dithered otherwise MagickFalse.
1397%
1398% The format of the DitherImage method is:
1399%
cristy8a11cb12011-10-19 23:53:34 +00001400% MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info,
1401% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001402%
1403% A description of each parameter follows.
1404%
1405% o image: the image.
1406%
1407% o cube_info: A pointer to the Cube structure.
1408%
cristy8a11cb12011-10-19 23:53:34 +00001409% o exception: return any errors or warnings in this structure.
1410%
cristy3ed852e2009-09-05 21:47:34 +00001411*/
1412
cristy101ab702011-10-13 13:06:32 +00001413static RealPixelInfo **DestroyPixelThreadSet(RealPixelInfo **pixels)
cristye9717ac2011-02-20 16:17:17 +00001414{
1415 register ssize_t
1416 i;
1417
cristy101ab702011-10-13 13:06:32 +00001418 assert(pixels != (RealPixelInfo **) NULL);
cristye9717ac2011-02-20 16:17:17 +00001419 for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++)
cristy101ab702011-10-13 13:06:32 +00001420 if (pixels[i] != (RealPixelInfo *) NULL)
1421 pixels[i]=(RealPixelInfo *) RelinquishMagickMemory(pixels[i]);
1422 pixels=(RealPixelInfo **) RelinquishMagickMemory(pixels);
cristye9717ac2011-02-20 16:17:17 +00001423 return(pixels);
1424}
1425
cristy101ab702011-10-13 13:06:32 +00001426static RealPixelInfo **AcquirePixelThreadSet(const size_t count)
cristye9717ac2011-02-20 16:17:17 +00001427{
cristy101ab702011-10-13 13:06:32 +00001428 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001429 **pixels;
1430
1431 register ssize_t
1432 i;
1433
1434 size_t
1435 number_threads;
1436
1437 number_threads=GetOpenMPMaximumThreads();
cristy101ab702011-10-13 13:06:32 +00001438 pixels=(RealPixelInfo **) AcquireQuantumMemory(number_threads,
cristye9717ac2011-02-20 16:17:17 +00001439 sizeof(*pixels));
cristy101ab702011-10-13 13:06:32 +00001440 if (pixels == (RealPixelInfo **) NULL)
1441 return((RealPixelInfo **) NULL);
cristye9717ac2011-02-20 16:17:17 +00001442 (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels));
1443 for (i=0; i < (ssize_t) number_threads; i++)
1444 {
cristy101ab702011-10-13 13:06:32 +00001445 pixels[i]=(RealPixelInfo *) AcquireQuantumMemory(count,
cristye9717ac2011-02-20 16:17:17 +00001446 2*sizeof(**pixels));
cristy101ab702011-10-13 13:06:32 +00001447 if (pixels[i] == (RealPixelInfo *) NULL)
cristye9717ac2011-02-20 16:17:17 +00001448 return(DestroyPixelThreadSet(pixels));
1449 }
1450 return(pixels);
1451}
1452
cristyca972de2010-06-20 23:37:02 +00001453static inline ssize_t CacheOffset(CubeInfo *cube_info,
cristy101ab702011-10-13 13:06:32 +00001454 const RealPixelInfo *pixel)
cristyca972de2010-06-20 23:37:02 +00001455{
1456#define RedShift(pixel) (((pixel) >> CacheShift) << (0*(8-CacheShift)))
1457#define GreenShift(pixel) (((pixel) >> CacheShift) << (1*(8-CacheShift)))
1458#define BlueShift(pixel) (((pixel) >> CacheShift) << (2*(8-CacheShift)))
1459#define AlphaShift(pixel) (((pixel) >> CacheShift) << (3*(8-CacheShift)))
1460
1461 ssize_t
1462 offset;
1463
1464 offset=(ssize_t)
cristy15893a42010-11-20 18:57:15 +00001465 (RedShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->red))) |
cristyca972de2010-06-20 23:37:02 +00001466 GreenShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->green))) |
cristy15893a42010-11-20 18:57:15 +00001467 BlueShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->blue))));
cristyca972de2010-06-20 23:37:02 +00001468 if (cube_info->associate_alpha != MagickFalse)
cristy15893a42010-11-20 18:57:15 +00001469 offset|=AlphaShift(ScaleQuantumToChar(ClampToUnsignedQuantum(
cristy4c08aed2011-07-01 19:47:50 +00001470 pixel->alpha)));
cristyca972de2010-06-20 23:37:02 +00001471 return(offset);
1472}
1473
cristy8a11cb12011-10-19 23:53:34 +00001474static MagickBooleanType FloydSteinbergDither(Image *image,CubeInfo *cube_info,
1475 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001476{
1477#define DitherImageTag "Dither/Image"
1478
cristyc4c8d132010-01-07 01:58:38 +00001479 CacheView
1480 *image_view;
1481
cristy3ed852e2009-09-05 21:47:34 +00001482 MagickBooleanType
cristye9717ac2011-02-20 16:17:17 +00001483 status;
cristy3ed852e2009-09-05 21:47:34 +00001484
cristy101ab702011-10-13 13:06:32 +00001485 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001486 **pixels;
cristy3ed852e2009-09-05 21:47:34 +00001487
cristy847620f2011-02-09 02:24:21 +00001488 ssize_t
cristy847620f2011-02-09 02:24:21 +00001489 y;
1490
cristy3ed852e2009-09-05 21:47:34 +00001491 /*
1492 Distribute quantization error using Floyd-Steinberg.
1493 */
cristye9717ac2011-02-20 16:17:17 +00001494 pixels=AcquirePixelThreadSet(image->columns);
cristy101ab702011-10-13 13:06:32 +00001495 if (pixels == (RealPixelInfo **) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001496 return(MagickFalse);
cristye9717ac2011-02-20 16:17:17 +00001497 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00001498 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +00001499 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00001500 {
cristye9717ac2011-02-20 16:17:17 +00001501 const int
1502 id = GetOpenMPThreadId();
1503
1504 CubeInfo
1505 cube;
1506
cristy101ab702011-10-13 13:06:32 +00001507 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001508 *current,
1509 *previous;
1510
cristy4c08aed2011-07-01 19:47:50 +00001511 register Quantum
cristyecc31b12011-02-13 00:32:29 +00001512 *restrict q;
1513
cristybb503372010-05-27 20:51:26 +00001514 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001515 x;
1516
cristye9717ac2011-02-20 16:17:17 +00001517 size_t
1518 index;
1519
1520 ssize_t
1521 v;
1522
1523 if (status == MagickFalse)
1524 continue;
cristy3ed852e2009-09-05 21:47:34 +00001525 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001526 if (q == (Quantum *) NULL)
cristye9717ac2011-02-20 16:17:17 +00001527 {
1528 status=MagickFalse;
cristy00cbdd62011-02-20 17:29:26 +00001529 continue;
cristye9717ac2011-02-20 16:17:17 +00001530 }
cristyed231572011-07-14 02:18:59 +00001531 q+=(y & 0x01)*image->columns*GetPixelChannels(image);
cristye9717ac2011-02-20 16:17:17 +00001532 cube=(*cube_info);
1533 current=pixels[id]+(y & 0x01)*image->columns;
1534 previous=pixels[id]+((y+1) & 0x01)*image->columns;
cristy4c08aed2011-07-01 19:47:50 +00001535 v=(ssize_t) ((y & 0x01) != 0 ? -1 : 1);
cristybb503372010-05-27 20:51:26 +00001536 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00001537 {
cristy101ab702011-10-13 13:06:32 +00001538 RealPixelInfo
cristye9717ac2011-02-20 16:17:17 +00001539 color,
1540 pixel;
1541
1542 register ssize_t
1543 i;
1544
1545 ssize_t
1546 u;
1547
cristyed231572011-07-14 02:18:59 +00001548 q-=(y & 0x01)*GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00001549 u=(y & 0x01) != 0 ? (ssize_t) image->columns-1-x : x;
1550 AssociateAlphaPixel(image,&cube,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001551 if (x > 0)
1552 {
1553 pixel.red+=7*current[u-v].red/16;
1554 pixel.green+=7*current[u-v].green/16;
1555 pixel.blue+=7*current[u-v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001556 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001557 pixel.alpha+=7*current[u-v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001558 }
1559 if (y > 0)
1560 {
cristybb503372010-05-27 20:51:26 +00001561 if (x < (ssize_t) (image->columns-1))
cristy3ed852e2009-09-05 21:47:34 +00001562 {
1563 pixel.red+=previous[u+v].red/16;
1564 pixel.green+=previous[u+v].green/16;
1565 pixel.blue+=previous[u+v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001566 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001567 pixel.alpha+=previous[u+v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001568 }
1569 pixel.red+=5*previous[u].red/16;
1570 pixel.green+=5*previous[u].green/16;
1571 pixel.blue+=5*previous[u].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001572 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001573 pixel.alpha+=5*previous[u].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001574 if (x > 0)
1575 {
1576 pixel.red+=3*previous[u-v].red/16;
1577 pixel.green+=3*previous[u-v].green/16;
1578 pixel.blue+=3*previous[u-v].blue/16;
cristye9717ac2011-02-20 16:17:17 +00001579 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001580 pixel.alpha+=3*previous[u-v].alpha/16;
cristy3ed852e2009-09-05 21:47:34 +00001581 }
1582 }
cristy75ffdb72010-01-07 17:40:12 +00001583 pixel.red=(MagickRealType) ClampToUnsignedQuantum(pixel.red);
1584 pixel.green=(MagickRealType) ClampToUnsignedQuantum(pixel.green);
1585 pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue);
cristye9717ac2011-02-20 16:17:17 +00001586 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001587 pixel.alpha=(MagickRealType) ClampToUnsignedQuantum(pixel.alpha);
cristye9717ac2011-02-20 16:17:17 +00001588 i=CacheOffset(&cube,&pixel);
1589 if (cube.cache[i] < 0)
cristy3ed852e2009-09-05 21:47:34 +00001590 {
1591 register NodeInfo
1592 *node_info;
1593
cristybb503372010-05-27 20:51:26 +00001594 register size_t
cristy3ed852e2009-09-05 21:47:34 +00001595 id;
1596
1597 /*
1598 Identify the deepest node containing the pixel's color.
1599 */
cristye9717ac2011-02-20 16:17:17 +00001600 node_info=cube.root;
cristybb503372010-05-27 20:51:26 +00001601 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +00001602 {
cristye9717ac2011-02-20 16:17:17 +00001603 id=ColorToNodeId(&cube,&pixel,index);
cristy3ed852e2009-09-05 21:47:34 +00001604 if (node_info->child[id] == (NodeInfo *) NULL)
1605 break;
1606 node_info=node_info->child[id];
1607 }
1608 /*
1609 Find closest color among siblings and their children.
1610 */
cristye9717ac2011-02-20 16:17:17 +00001611 cube.target=pixel;
1612 cube.distance=(MagickRealType) (4.0*(QuantumRange+1.0)*(QuantumRange+
cristy3ed852e2009-09-05 21:47:34 +00001613 1.0)+1.0);
cristye9717ac2011-02-20 16:17:17 +00001614 ClosestColor(image,&cube,node_info->parent);
1615 cube.cache[i]=(ssize_t) cube.color_number;
cristy3ed852e2009-09-05 21:47:34 +00001616 }
1617 /*
1618 Assign pixel to closest colormap entry.
1619 */
cristye9717ac2011-02-20 16:17:17 +00001620 index=(size_t) cube.cache[i];
cristy3ed852e2009-09-05 21:47:34 +00001621 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +00001622 SetPixelIndex(image,(Quantum) index,q);
cristye9717ac2011-02-20 16:17:17 +00001623 if (cube.quantize_info->measure_error == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00001624 {
cristy4c08aed2011-07-01 19:47:50 +00001625 SetPixelRed(image,image->colormap[index].red,q);
1626 SetPixelGreen(image,image->colormap[index].green,q);
1627 SetPixelBlue(image,image->colormap[index].blue,q);
cristye9717ac2011-02-20 16:17:17 +00001628 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001629 SetPixelAlpha(image,image->colormap[index].alpha,q);
cristy3ed852e2009-09-05 21:47:34 +00001630 }
1631 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
cristye9717ac2011-02-20 16:17:17 +00001632 status=MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +00001633 /*
1634 Store the error.
1635 */
cristy101ab702011-10-13 13:06:32 +00001636 AssociateAlphaPixelInfo(image,&cube,image->colormap+index,&color);
cristy3ed852e2009-09-05 21:47:34 +00001637 current[u].red=pixel.red-color.red;
1638 current[u].green=pixel.green-color.green;
1639 current[u].blue=pixel.blue-color.blue;
cristye9717ac2011-02-20 16:17:17 +00001640 if (cube.associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001641 current[u].alpha=pixel.alpha-color.alpha;
cristye9717ac2011-02-20 16:17:17 +00001642 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1643 {
1644 MagickBooleanType
1645 proceed;
1646
1647#if defined(MAGICKCORE_OPENMP_SUPPORT)
1648 #pragma omp critical (MagickCore_FloydSteinbergDither)
1649#endif
1650 proceed=SetImageProgress(image,DitherImageTag,(MagickOffsetType) y,
1651 image->rows);
1652 if (proceed == MagickFalse)
1653 status=MagickFalse;
1654 }
cristyed231572011-07-14 02:18:59 +00001655 q+=((y+1) & 0x01)*GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00001656 }
1657 }
cristy3ed852e2009-09-05 21:47:34 +00001658 image_view=DestroyCacheView(image_view);
cristye9717ac2011-02-20 16:17:17 +00001659 pixels=DestroyPixelThreadSet(pixels);
cristy3ed852e2009-09-05 21:47:34 +00001660 return(MagickTrue);
1661}
1662
1663static MagickBooleanType
cristy8a11cb12011-10-19 23:53:34 +00001664 RiemersmaDither(Image *,CacheView *,CubeInfo *,const unsigned int,
1665 ExceptionInfo *exception);
cristy3ed852e2009-09-05 21:47:34 +00001666
1667static void Riemersma(Image *image,CacheView *image_view,CubeInfo *cube_info,
cristy8a11cb12011-10-19 23:53:34 +00001668 const size_t level,const unsigned int direction,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001669{
1670 if (level == 1)
1671 switch (direction)
1672 {
1673 case WestGravity:
1674 {
cristy8a11cb12011-10-19 23:53:34 +00001675 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1676 exception);
1677 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1678 exception);
1679 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1680 exception);
cristy3ed852e2009-09-05 21:47:34 +00001681 break;
1682 }
1683 case EastGravity:
1684 {
cristy8a11cb12011-10-19 23:53:34 +00001685 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1686 exception);
1687 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1688 exception);
1689 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1690 exception);
cristy3ed852e2009-09-05 21:47:34 +00001691 break;
1692 }
1693 case NorthGravity:
1694 {
cristy8a11cb12011-10-19 23:53:34 +00001695 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1696 exception);
1697 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1698 exception);
1699 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1700 exception);
cristy3ed852e2009-09-05 21:47:34 +00001701 break;
1702 }
1703 case SouthGravity:
1704 {
cristy8a11cb12011-10-19 23:53:34 +00001705 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1706 exception);
1707 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1708 exception);
1709 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1710 exception);
cristy3ed852e2009-09-05 21:47:34 +00001711 break;
1712 }
1713 default:
1714 break;
1715 }
1716 else
1717 switch (direction)
1718 {
1719 case WestGravity:
1720 {
cristy8a11cb12011-10-19 23:53:34 +00001721 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1722 exception);
1723 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1724 exception);
1725 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1726 exception);
1727 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1728 exception);
1729 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1730 exception);
1731 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1732 exception);
1733 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1734 exception);
cristy3ed852e2009-09-05 21:47:34 +00001735 break;
1736 }
1737 case EastGravity:
1738 {
cristy8a11cb12011-10-19 23:53:34 +00001739 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1740 exception);
1741 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1742 exception);
1743 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1744 exception);
1745 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1746 exception);
1747 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1748 exception);
1749 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1750 exception);
1751 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1752 exception);
cristy3ed852e2009-09-05 21:47:34 +00001753 break;
1754 }
1755 case NorthGravity:
1756 {
cristy8a11cb12011-10-19 23:53:34 +00001757 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1758 exception);
1759 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1760 exception);
1761 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1762 exception);
1763 (void) RiemersmaDither(image,image_view,cube_info,EastGravity,
1764 exception);
1765 Riemersma(image,image_view,cube_info,level-1,NorthGravity,
1766 exception);
1767 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1768 exception);
1769 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1770 exception);
cristy3ed852e2009-09-05 21:47:34 +00001771 break;
1772 }
1773 case SouthGravity:
1774 {
cristy8a11cb12011-10-19 23:53:34 +00001775 Riemersma(image,image_view,cube_info,level-1,EastGravity,
1776 exception);
1777 (void) RiemersmaDither(image,image_view,cube_info,NorthGravity,
1778 exception);
1779 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1780 exception);
1781 (void) RiemersmaDither(image,image_view,cube_info,WestGravity,
1782 exception);
1783 Riemersma(image,image_view,cube_info,level-1,SouthGravity,
1784 exception);
1785 (void) RiemersmaDither(image,image_view,cube_info,SouthGravity,
1786 exception);
1787 Riemersma(image,image_view,cube_info,level-1,WestGravity,
1788 exception);
cristy3ed852e2009-09-05 21:47:34 +00001789 break;
1790 }
1791 default:
1792 break;
1793 }
1794}
1795
1796static MagickBooleanType RiemersmaDither(Image *image,CacheView *image_view,
cristy8a11cb12011-10-19 23:53:34 +00001797 CubeInfo *cube_info,const unsigned int direction,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001798{
1799#define DitherImageTag "Dither/Image"
1800
1801 MagickBooleanType
1802 proceed;
1803
cristy101ab702011-10-13 13:06:32 +00001804 RealPixelInfo
cristy3ed852e2009-09-05 21:47:34 +00001805 color,
1806 pixel;
1807
1808 register CubeInfo
1809 *p;
1810
cristybb503372010-05-27 20:51:26 +00001811 size_t
cristy3ed852e2009-09-05 21:47:34 +00001812 index;
1813
1814 p=cube_info;
cristybb503372010-05-27 20:51:26 +00001815 if ((p->x >= 0) && (p->x < (ssize_t) image->columns) &&
1816 (p->y >= 0) && (p->y < (ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001817 {
cristy4c08aed2011-07-01 19:47:50 +00001818 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00001819 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00001820
cristyecc31b12011-02-13 00:32:29 +00001821 register ssize_t
1822 i;
1823
cristy3ed852e2009-09-05 21:47:34 +00001824 /*
1825 Distribute error.
1826 */
cristy3ed852e2009-09-05 21:47:34 +00001827 q=GetCacheViewAuthenticPixels(image_view,p->x,p->y,1,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00001828 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00001829 return(MagickFalse);
cristy4c08aed2011-07-01 19:47:50 +00001830 AssociateAlphaPixel(image,cube_info,q,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001831 for (i=0; i < ErrorQueueLength; i++)
1832 {
1833 pixel.red+=p->weights[i]*p->error[i].red;
1834 pixel.green+=p->weights[i]*p->error[i].green;
1835 pixel.blue+=p->weights[i]*p->error[i].blue;
1836 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001837 pixel.alpha+=p->weights[i]*p->error[i].alpha;
cristy3ed852e2009-09-05 21:47:34 +00001838 }
cristy75ffdb72010-01-07 17:40:12 +00001839 pixel.red=(MagickRealType) ClampToUnsignedQuantum(pixel.red);
1840 pixel.green=(MagickRealType) ClampToUnsignedQuantum(pixel.green);
1841 pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue);
cristy3ed852e2009-09-05 21:47:34 +00001842 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001843 pixel.alpha=(MagickRealType) ClampToUnsignedQuantum(pixel.alpha);
cristyca972de2010-06-20 23:37:02 +00001844 i=CacheOffset(cube_info,&pixel);
cristy3ed852e2009-09-05 21:47:34 +00001845 if (p->cache[i] < 0)
1846 {
1847 register NodeInfo
1848 *node_info;
1849
cristybb503372010-05-27 20:51:26 +00001850 register size_t
cristy3ed852e2009-09-05 21:47:34 +00001851 id;
1852
1853 /*
1854 Identify the deepest node containing the pixel's color.
1855 */
1856 node_info=p->root;
cristybb503372010-05-27 20:51:26 +00001857 for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--)
cristy3ed852e2009-09-05 21:47:34 +00001858 {
1859 id=ColorToNodeId(cube_info,&pixel,index);
1860 if (node_info->child[id] == (NodeInfo *) NULL)
1861 break;
1862 node_info=node_info->child[id];
1863 }
cristyecc31b12011-02-13 00:32:29 +00001864 node_info=node_info->parent;
cristy3ed852e2009-09-05 21:47:34 +00001865 /*
1866 Find closest color among siblings and their children.
1867 */
1868 p->target=pixel;
1869 p->distance=(MagickRealType) (4.0*(QuantumRange+1.0)*((MagickRealType)
1870 QuantumRange+1.0)+1.0);
1871 ClosestColor(image,p,node_info->parent);
cristybb503372010-05-27 20:51:26 +00001872 p->cache[i]=(ssize_t) p->color_number;
cristy3ed852e2009-09-05 21:47:34 +00001873 }
1874 /*
1875 Assign pixel to closest colormap entry.
1876 */
cristy4c08aed2011-07-01 19:47:50 +00001877 index=(size_t) p->cache[i];
cristy3ed852e2009-09-05 21:47:34 +00001878 if (image->storage_class == PseudoClass)
cristy4c08aed2011-07-01 19:47:50 +00001879 SetPixelIndex(image,(Quantum) index,q);
cristy3ed852e2009-09-05 21:47:34 +00001880 if (cube_info->quantize_info->measure_error == MagickFalse)
1881 {
cristy4c08aed2011-07-01 19:47:50 +00001882 SetPixelRed(image,image->colormap[index].red,q);
1883 SetPixelGreen(image,image->colormap[index].green,q);
1884 SetPixelBlue(image,image->colormap[index].blue,q);
cristy3ed852e2009-09-05 21:47:34 +00001885 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001886 SetPixelAlpha(image,image->colormap[index].alpha,q);
cristy3ed852e2009-09-05 21:47:34 +00001887 }
1888 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
1889 return(MagickFalse);
1890 /*
1891 Propagate the error as the last entry of the error queue.
1892 */
1893 (void) CopyMagickMemory(p->error,p->error+1,(ErrorQueueLength-1)*
1894 sizeof(p->error[0]));
cristy101ab702011-10-13 13:06:32 +00001895 AssociateAlphaPixelInfo(image,cube_info,image->colormap+index,&color);
cristy3ed852e2009-09-05 21:47:34 +00001896 p->error[ErrorQueueLength-1].red=pixel.red-color.red;
1897 p->error[ErrorQueueLength-1].green=pixel.green-color.green;
1898 p->error[ErrorQueueLength-1].blue=pixel.blue-color.blue;
1899 if (cube_info->associate_alpha != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001900 p->error[ErrorQueueLength-1].alpha=pixel.alpha-color.alpha;
cristy3ed852e2009-09-05 21:47:34 +00001901 proceed=SetImageProgress(image,DitherImageTag,p->offset,p->span);
1902 if (proceed == MagickFalse)
1903 return(MagickFalse);
1904 p->offset++;
1905 }
1906 switch (direction)
1907 {
1908 case WestGravity: p->x--; break;
1909 case EastGravity: p->x++; break;
1910 case NorthGravity: p->y--; break;
1911 case SouthGravity: p->y++; break;
1912 }
1913 return(MagickTrue);
1914}
1915
cristybb503372010-05-27 20:51:26 +00001916static inline ssize_t MagickMax(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00001917{
1918 if (x > y)
1919 return(x);
1920 return(y);
1921}
1922
cristybb503372010-05-27 20:51:26 +00001923static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
cristy3ed852e2009-09-05 21:47:34 +00001924{
1925 if (x < y)
1926 return(x);
1927 return(y);
1928}
1929
cristy8a11cb12011-10-19 23:53:34 +00001930static MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info,
1931 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001932{
cristyc4c8d132010-01-07 01:58:38 +00001933 CacheView
1934 *image_view;
1935
cristy3ed852e2009-09-05 21:47:34 +00001936 MagickBooleanType
1937 status;
1938
cristybb503372010-05-27 20:51:26 +00001939 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001940 i;
1941
cristybb503372010-05-27 20:51:26 +00001942 size_t
cristy3ed852e2009-09-05 21:47:34 +00001943 depth;
1944
cristyfb7e9cd2011-02-20 16:26:15 +00001945 if (cube_info->quantize_info->dither_method != RiemersmaDitherMethod)
cristy8a11cb12011-10-19 23:53:34 +00001946 return(FloydSteinbergDither(image,cube_info,exception));
cristy3ed852e2009-09-05 21:47:34 +00001947 /*
cristycee97112010-05-28 00:44:52 +00001948 Distribute quantization error along a Hilbert curve.
cristy3ed852e2009-09-05 21:47:34 +00001949 */
1950 (void) ResetMagickMemory(cube_info->error,0,ErrorQueueLength*
1951 sizeof(*cube_info->error));
1952 cube_info->x=0;
1953 cube_info->y=0;
cristybb503372010-05-27 20:51:26 +00001954 i=MagickMax((ssize_t) image->columns,(ssize_t) image->rows);
cristy3ed852e2009-09-05 21:47:34 +00001955 for (depth=1; i != 0; depth++)
1956 i>>=1;
cristybb503372010-05-27 20:51:26 +00001957 if ((ssize_t) (1L << depth) < MagickMax((ssize_t) image->columns,(ssize_t) image->rows))
cristy3ed852e2009-09-05 21:47:34 +00001958 depth++;
1959 cube_info->offset=0;
1960 cube_info->span=(MagickSizeType) image->columns*image->rows;
1961 image_view=AcquireCacheView(image);
1962 if (depth > 1)
cristy8a11cb12011-10-19 23:53:34 +00001963 Riemersma(image,image_view,cube_info,depth-1,NorthGravity,exception);
1964 status=RiemersmaDither(image,image_view,cube_info,ForgetGravity,exception);
cristy3ed852e2009-09-05 21:47:34 +00001965 image_view=DestroyCacheView(image_view);
1966 return(status);
1967}
1968
1969/*
1970%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1971% %
1972% %
1973% %
1974+ G e t C u b e I n f o %
1975% %
1976% %
1977% %
1978%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1979%
1980% GetCubeInfo() initialize the Cube data structure.
1981%
1982% The format of the GetCubeInfo method is:
1983%
1984% CubeInfo GetCubeInfo(const QuantizeInfo *quantize_info,
cristybb503372010-05-27 20:51:26 +00001985% const size_t depth,const size_t maximum_colors)
cristy3ed852e2009-09-05 21:47:34 +00001986%
1987% A description of each parameter follows.
1988%
1989% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
1990%
1991% o depth: Normally, this integer value is zero or one. A zero or
1992% one tells Quantize to choose a optimal tree depth of Log4(number_colors).
1993% A tree of this depth generally allows the best representation of the
1994% reference image with the least amount of memory and the fastest
1995% computational speed. In some cases, such as an image with low color
1996% dispersion (a few number of colors), a value other than
1997% Log4(number_colors) is required. To expand the color tree completely,
1998% use a value of 8.
1999%
2000% o maximum_colors: maximum colors.
2001%
2002*/
2003static CubeInfo *GetCubeInfo(const QuantizeInfo *quantize_info,
cristybb503372010-05-27 20:51:26 +00002004 const size_t depth,const size_t maximum_colors)
cristy3ed852e2009-09-05 21:47:34 +00002005{
2006 CubeInfo
2007 *cube_info;
2008
2009 MagickRealType
2010 sum,
2011 weight;
2012
cristybb503372010-05-27 20:51:26 +00002013 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002014 i;
2015
cristyecc31b12011-02-13 00:32:29 +00002016 size_t
2017 length;
2018
cristy3ed852e2009-09-05 21:47:34 +00002019 /*
2020 Initialize tree to describe color cube_info.
2021 */
cristy73bd4a52010-10-05 11:24:23 +00002022 cube_info=(CubeInfo *) AcquireMagickMemory(sizeof(*cube_info));
cristy3ed852e2009-09-05 21:47:34 +00002023 if (cube_info == (CubeInfo *) NULL)
2024 return((CubeInfo *) NULL);
2025 (void) ResetMagickMemory(cube_info,0,sizeof(*cube_info));
2026 cube_info->depth=depth;
2027 if (cube_info->depth > MaxTreeDepth)
2028 cube_info->depth=MaxTreeDepth;
2029 if (cube_info->depth < 2)
2030 cube_info->depth=2;
2031 cube_info->maximum_colors=maximum_colors;
2032 /*
2033 Initialize root node.
2034 */
2035 cube_info->root=GetNodeInfo(cube_info,0,0,(NodeInfo *) NULL);
2036 if (cube_info->root == (NodeInfo *) NULL)
2037 return((CubeInfo *) NULL);
2038 cube_info->root->parent=cube_info->root;
2039 cube_info->quantize_info=CloneQuantizeInfo(quantize_info);
2040 if (cube_info->quantize_info->dither == MagickFalse)
2041 return(cube_info);
2042 /*
2043 Initialize dither resources.
2044 */
2045 length=(size_t) (1UL << (4*(8-CacheShift)));
cristybb503372010-05-27 20:51:26 +00002046 cube_info->cache=(ssize_t *) AcquireQuantumMemory(length,
cristy3ed852e2009-09-05 21:47:34 +00002047 sizeof(*cube_info->cache));
cristybb503372010-05-27 20:51:26 +00002048 if (cube_info->cache == (ssize_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002049 return((CubeInfo *) NULL);
2050 /*
2051 Initialize color cache.
2052 */
cristybb503372010-05-27 20:51:26 +00002053 for (i=0; i < (ssize_t) length; i++)
cristy3ed852e2009-09-05 21:47:34 +00002054 cube_info->cache[i]=(-1);
2055 /*
cristycee97112010-05-28 00:44:52 +00002056 Distribute weights along a curve of exponential decay.
cristy3ed852e2009-09-05 21:47:34 +00002057 */
2058 weight=1.0;
2059 for (i=0; i < ErrorQueueLength; i++)
2060 {
2061 cube_info->weights[ErrorQueueLength-i-1]=1.0/weight;
2062 weight*=exp(log(((double) QuantumRange+1.0))/(ErrorQueueLength-1.0));
2063 }
2064 /*
2065 Normalize the weighting factors.
2066 */
2067 weight=0.0;
2068 for (i=0; i < ErrorQueueLength; i++)
2069 weight+=cube_info->weights[i];
2070 sum=0.0;
2071 for (i=0; i < ErrorQueueLength; i++)
2072 {
2073 cube_info->weights[i]/=weight;
2074 sum+=cube_info->weights[i];
2075 }
2076 cube_info->weights[0]+=1.0-sum;
2077 return(cube_info);
2078}
2079
2080/*
2081%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2082% %
2083% %
2084% %
2085+ G e t N o d e I n f o %
2086% %
2087% %
2088% %
2089%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2090%
2091% GetNodeInfo() allocates memory for a new node in the color cube tree and
2092% presets all fields to zero.
2093%
2094% The format of the GetNodeInfo method is:
2095%
cristybb503372010-05-27 20:51:26 +00002096% NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id,
2097% const size_t level,NodeInfo *parent)
cristy3ed852e2009-09-05 21:47:34 +00002098%
2099% A description of each parameter follows.
2100%
2101% o node: The GetNodeInfo method returns a pointer to a queue of nodes.
2102%
2103% o id: Specifies the child number of the node.
2104%
2105% o level: Specifies the level in the storage_class the node resides.
2106%
2107*/
cristybb503372010-05-27 20:51:26 +00002108static NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id,
2109 const size_t level,NodeInfo *parent)
cristy3ed852e2009-09-05 21:47:34 +00002110{
2111 NodeInfo
2112 *node_info;
2113
2114 if (cube_info->free_nodes == 0)
2115 {
2116 Nodes
2117 *nodes;
2118
2119 /*
2120 Allocate a new queue of nodes.
2121 */
cristy73bd4a52010-10-05 11:24:23 +00002122 nodes=(Nodes *) AcquireMagickMemory(sizeof(*nodes));
cristy3ed852e2009-09-05 21:47:34 +00002123 if (nodes == (Nodes *) NULL)
2124 return((NodeInfo *) NULL);
2125 nodes->nodes=(NodeInfo *) AcquireQuantumMemory(NodesInAList,
2126 sizeof(*nodes->nodes));
2127 if (nodes->nodes == (NodeInfo *) NULL)
2128 return((NodeInfo *) NULL);
2129 nodes->next=cube_info->node_queue;
2130 cube_info->node_queue=nodes;
2131 cube_info->next_node=nodes->nodes;
2132 cube_info->free_nodes=NodesInAList;
2133 }
2134 cube_info->nodes++;
2135 cube_info->free_nodes--;
2136 node_info=cube_info->next_node++;
2137 (void) ResetMagickMemory(node_info,0,sizeof(*node_info));
2138 node_info->parent=parent;
2139 node_info->id=id;
2140 node_info->level=level;
2141 return(node_info);
2142}
2143
2144/*
2145%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2146% %
2147% %
2148% %
2149% G e t I m a g e Q u a n t i z e E r r o r %
2150% %
2151% %
2152% %
2153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2154%
2155% GetImageQuantizeError() measures the difference between the original
2156% and quantized images. This difference is the total quantization error.
2157% The error is computed by summing over all pixels in an image the distance
2158% squared in RGB space between each reference pixel value and its quantized
2159% value. These values are computed:
2160%
2161% o mean_error_per_pixel: This value is the mean error for any single
2162% pixel in the image.
2163%
2164% o normalized_mean_square_error: This value is the normalized mean
2165% quantization error for any single pixel in the image. This distance
2166% measure is normalized to a range between 0 and 1. It is independent
2167% of the range of red, green, and blue values in the image.
2168%
2169% o normalized_maximum_square_error: Thsi value is the normalized
2170% maximum quantization error for any single pixel in the image. This
2171% distance measure is normalized to a range between 0 and 1. It is
2172% independent of the range of red, green, and blue values in your image.
2173%
2174% The format of the GetImageQuantizeError method is:
2175%
cristy8a11cb12011-10-19 23:53:34 +00002176% MagickBooleanType GetImageQuantizeError(Image *image,
2177% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002178%
2179% A description of each parameter follows.
2180%
2181% o image: the image.
2182%
cristy8a11cb12011-10-19 23:53:34 +00002183% o exception: return any errors or warnings in this structure.
2184%
cristy3ed852e2009-09-05 21:47:34 +00002185*/
cristy8a11cb12011-10-19 23:53:34 +00002186MagickExport MagickBooleanType GetImageQuantizeError(Image *image,
2187 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002188{
cristyc4c8d132010-01-07 01:58:38 +00002189 CacheView
2190 *image_view;
2191
cristy3ed852e2009-09-05 21:47:34 +00002192 MagickRealType
2193 alpha,
2194 area,
2195 beta,
2196 distance,
2197 maximum_error,
2198 mean_error,
2199 mean_error_per_pixel;
2200
cristybb503372010-05-27 20:51:26 +00002201 size_t
cristy3ed852e2009-09-05 21:47:34 +00002202 index;
2203
cristyecc31b12011-02-13 00:32:29 +00002204 ssize_t
2205 y;
2206
cristy3ed852e2009-09-05 21:47:34 +00002207 assert(image != (Image *) NULL);
2208 assert(image->signature == MagickSignature);
2209 if (image->debug != MagickFalse)
2210 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy8a11cb12011-10-19 23:53:34 +00002211 image->total_colors=GetNumberColors(image,(FILE *) NULL,exception);
cristy3ed852e2009-09-05 21:47:34 +00002212 (void) ResetMagickMemory(&image->error,0,sizeof(image->error));
2213 if (image->storage_class == DirectClass)
2214 return(MagickTrue);
2215 alpha=1.0;
2216 beta=1.0;
2217 area=3.0*image->columns*image->rows;
2218 maximum_error=0.0;
2219 mean_error_per_pixel=0.0;
2220 mean_error=0.0;
cristy3ed852e2009-09-05 21:47:34 +00002221 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +00002222 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00002223 {
cristy4c08aed2011-07-01 19:47:50 +00002224 register const Quantum
cristyc47d1f82009-11-26 01:44:43 +00002225 *restrict p;
cristy3ed852e2009-09-05 21:47:34 +00002226
cristybb503372010-05-27 20:51:26 +00002227 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002228 x;
2229
2230 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +00002231 if (p == (const Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002232 break;
cristybb503372010-05-27 20:51:26 +00002233 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002234 {
cristy4c08aed2011-07-01 19:47:50 +00002235 index=1UL*GetPixelIndex(image,p);
cristy3ed852e2009-09-05 21:47:34 +00002236 if (image->matte != MagickFalse)
2237 {
cristy4c08aed2011-07-01 19:47:50 +00002238 alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p));
2239 beta=(MagickRealType) (QuantumScale*image->colormap[index].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002240 }
cristy4c08aed2011-07-01 19:47:50 +00002241 distance=fabs(alpha*GetPixelRed(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002242 image->colormap[index].red);
cristy3ed852e2009-09-05 21:47:34 +00002243 mean_error_per_pixel+=distance;
2244 mean_error+=distance*distance;
2245 if (distance > maximum_error)
2246 maximum_error=distance;
cristy4c08aed2011-07-01 19:47:50 +00002247 distance=fabs(alpha*GetPixelGreen(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002248 image->colormap[index].green);
cristy3ed852e2009-09-05 21:47:34 +00002249 mean_error_per_pixel+=distance;
2250 mean_error+=distance*distance;
2251 if (distance > maximum_error)
2252 maximum_error=distance;
cristy4c08aed2011-07-01 19:47:50 +00002253 distance=fabs(alpha*GetPixelBlue(image,p)-beta*
cristy01e4e7d2011-05-01 23:00:41 +00002254 image->colormap[index].blue);
cristy3ed852e2009-09-05 21:47:34 +00002255 mean_error_per_pixel+=distance;
2256 mean_error+=distance*distance;
2257 if (distance > maximum_error)
2258 maximum_error=distance;
cristyed231572011-07-14 02:18:59 +00002259 p+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002260 }
2261 }
2262 image_view=DestroyCacheView(image_view);
2263 image->error.mean_error_per_pixel=(double) mean_error_per_pixel/area;
2264 image->error.normalized_mean_error=(double) QuantumScale*QuantumScale*
2265 mean_error/area;
2266 image->error.normalized_maximum_error=(double) QuantumScale*maximum_error;
2267 return(MagickTrue);
2268}
2269
2270/*
2271%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2272% %
2273% %
2274% %
2275% G e t Q u a n t i z e I n f o %
2276% %
2277% %
2278% %
2279%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2280%
2281% GetQuantizeInfo() initializes the QuantizeInfo structure.
2282%
2283% The format of the GetQuantizeInfo method is:
2284%
2285% GetQuantizeInfo(QuantizeInfo *quantize_info)
2286%
2287% A description of each parameter follows:
2288%
2289% o quantize_info: Specifies a pointer to a QuantizeInfo structure.
2290%
2291*/
2292MagickExport void GetQuantizeInfo(QuantizeInfo *quantize_info)
2293{
2294 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2295 assert(quantize_info != (QuantizeInfo *) NULL);
2296 (void) ResetMagickMemory(quantize_info,0,sizeof(*quantize_info));
2297 quantize_info->number_colors=256;
2298 quantize_info->dither=MagickTrue;
2299 quantize_info->dither_method=RiemersmaDitherMethod;
2300 quantize_info->colorspace=UndefinedColorspace;
2301 quantize_info->measure_error=MagickFalse;
2302 quantize_info->signature=MagickSignature;
2303}
2304
2305/*
2306%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2307% %
2308% %
2309% %
cristy018f07f2011-09-04 21:15:19 +00002310% P o s t e r i z e I m a g e %
cristy3ed852e2009-09-05 21:47:34 +00002311% %
2312% %
2313% %
2314%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2315%
2316% PosterizeImage() reduces the image to a limited number of colors for a
2317% "poster" effect.
2318%
2319% The format of the PosterizeImage method is:
2320%
cristybb503372010-05-27 20:51:26 +00002321% MagickBooleanType PosterizeImage(Image *image,const size_t levels,
cristy018f07f2011-09-04 21:15:19 +00002322% const MagickBooleanType dither,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002323%
2324% A description of each parameter follows:
2325%
2326% o image: Specifies a pointer to an Image structure.
2327%
2328% o levels: Number of color levels allowed in each channel. Very low values
2329% (2, 3, or 4) have the most visible effect.
2330%
cristy847620f2011-02-09 02:24:21 +00002331% o dither: Set this integer value to something other than zero to dither
2332% the mapped image.
cristy3ed852e2009-09-05 21:47:34 +00002333%
cristy018f07f2011-09-04 21:15:19 +00002334% o exception: return any errors or warnings in this structure.
2335%
cristy3ed852e2009-09-05 21:47:34 +00002336*/
cristyd1a2c0f2011-02-09 14:14:50 +00002337
cristy4d727152011-02-10 19:57:21 +00002338static inline ssize_t MagickRound(MagickRealType x)
2339{
2340 /*
cristyecc31b12011-02-13 00:32:29 +00002341 Round the fraction to nearest integer.
cristy4d727152011-02-10 19:57:21 +00002342 */
2343 if (x >= 0.0)
2344 return((ssize_t) (x+0.5));
2345 return((ssize_t) (x-0.5));
2346}
2347
cristyd1a2c0f2011-02-09 14:14:50 +00002348MagickExport MagickBooleanType PosterizeImage(Image *image,const size_t levels,
cristy018f07f2011-09-04 21:15:19 +00002349 const MagickBooleanType dither,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002350{
cristyd1a2c0f2011-02-09 14:14:50 +00002351#define PosterizeImageTag "Posterize/Image"
cristy4d727152011-02-10 19:57:21 +00002352#define PosterizePixel(pixel) (Quantum) (QuantumRange*(MagickRound( \
cristy3e9cad02011-02-20 01:42:00 +00002353 QuantumScale*pixel*(levels-1)))/MagickMax((ssize_t) levels-1,1))
cristyd1a2c0f2011-02-09 14:14:50 +00002354
cristyc4c8d132010-01-07 01:58:38 +00002355 CacheView
cristyd1a2c0f2011-02-09 14:14:50 +00002356 *image_view;
cristyc4c8d132010-01-07 01:58:38 +00002357
cristy3ed852e2009-09-05 21:47:34 +00002358 MagickBooleanType
2359 status;
2360
cristyd1a2c0f2011-02-09 14:14:50 +00002361 MagickOffsetType
2362 progress;
2363
cristy3ed852e2009-09-05 21:47:34 +00002364 QuantizeInfo
2365 *quantize_info;
2366
cristy847620f2011-02-09 02:24:21 +00002367 register ssize_t
2368 i;
2369
cristy847620f2011-02-09 02:24:21 +00002370 ssize_t
cristyd1a2c0f2011-02-09 14:14:50 +00002371 y;
cristy847620f2011-02-09 02:24:21 +00002372
cristy3ed852e2009-09-05 21:47:34 +00002373 assert(image != (Image *) NULL);
2374 assert(image->signature == MagickSignature);
2375 if (image->debug != MagickFalse)
2376 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristyd1a2c0f2011-02-09 14:14:50 +00002377 if (image->storage_class == PseudoClass)
2378#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002379 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyd1a2c0f2011-02-09 14:14:50 +00002380#endif
2381 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00002382 {
cristyd1a2c0f2011-02-09 14:14:50 +00002383 /*
2384 Posterize colormap.
2385 */
cristyed231572011-07-14 02:18:59 +00002386 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristyd1a2c0f2011-02-09 14:14:50 +00002387 image->colormap[i].red=PosterizePixel(image->colormap[i].red);
cristyed231572011-07-14 02:18:59 +00002388 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristyd1a2c0f2011-02-09 14:14:50 +00002389 image->colormap[i].green=PosterizePixel(image->colormap[i].green);
cristyed231572011-07-14 02:18:59 +00002390 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristyd1a2c0f2011-02-09 14:14:50 +00002391 image->colormap[i].blue=PosterizePixel(image->colormap[i].blue);
cristyed231572011-07-14 02:18:59 +00002392 if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002393 image->colormap[i].alpha=PosterizePixel(image->colormap[i].alpha);
cristy3ed852e2009-09-05 21:47:34 +00002394 }
cristyd1a2c0f2011-02-09 14:14:50 +00002395 /*
2396 Posterize image.
2397 */
2398 status=MagickTrue;
2399 progress=0;
cristyd1a2c0f2011-02-09 14:14:50 +00002400 image_view=AcquireCacheView(image);
2401#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00002402 #pragma omp parallel for schedule(static,4) shared(progress,status)
cristyd1a2c0f2011-02-09 14:14:50 +00002403#endif
2404 for (y=0; y < (ssize_t) image->rows; y++)
2405 {
cristy4c08aed2011-07-01 19:47:50 +00002406 register Quantum
cristyd1a2c0f2011-02-09 14:14:50 +00002407 *restrict q;
2408
2409 register ssize_t
2410 x;
2411
2412 if (status == MagickFalse)
2413 continue;
2414 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00002415 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002416 {
cristyd1a2c0f2011-02-09 14:14:50 +00002417 status=MagickFalse;
2418 continue;
cristy3ed852e2009-09-05 21:47:34 +00002419 }
cristyd1a2c0f2011-02-09 14:14:50 +00002420 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00002421 {
cristyed231572011-07-14 02:18:59 +00002422 if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002423 SetPixelRed(image,PosterizePixel(GetPixelRed(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002424 if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002425 SetPixelGreen(image,PosterizePixel(GetPixelGreen(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002426 if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0)
cristy4c08aed2011-07-01 19:47:50 +00002427 SetPixelBlue(image,PosterizePixel(GetPixelBlue(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002428 if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) &&
cristy4c08aed2011-07-01 19:47:50 +00002429 (image->colorspace == CMYKColorspace))
2430 SetPixelBlack(image,PosterizePixel(GetPixelBlack(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002431 if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) &&
cristyd1a2c0f2011-02-09 14:14:50 +00002432 (image->matte == MagickTrue))
cristy4c08aed2011-07-01 19:47:50 +00002433 SetPixelAlpha(image,PosterizePixel(GetPixelAlpha(image,q)),q);
cristyed231572011-07-14 02:18:59 +00002434 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00002435 }
cristyd1a2c0f2011-02-09 14:14:50 +00002436 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2437 status=MagickFalse;
2438 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2439 {
2440 MagickBooleanType
2441 proceed;
2442
2443#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy13020672011-07-08 02:33:26 +00002444 #pragma omp critical (MagickCore_PosterizeImage)
cristyd1a2c0f2011-02-09 14:14:50 +00002445#endif
2446 proceed=SetImageProgress(image,PosterizeImageTag,progress++,
2447 image->rows);
2448 if (proceed == MagickFalse)
2449 status=MagickFalse;
2450 }
2451 }
2452 image_view=DestroyCacheView(image_view);
cristy3ed852e2009-09-05 21:47:34 +00002453 quantize_info=AcquireQuantizeInfo((ImageInfo *) NULL);
cristyd1a2c0f2011-02-09 14:14:50 +00002454 quantize_info->number_colors=(size_t) MagickMin((ssize_t) levels*levels*
2455 levels,MaxColormapSize+1);
cristy3ed852e2009-09-05 21:47:34 +00002456 quantize_info->dither=dither;
cristy3e9cad02011-02-20 01:42:00 +00002457 quantize_info->tree_depth=MaxTreeDepth;
cristy018f07f2011-09-04 21:15:19 +00002458 status=QuantizeImage(quantize_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002459 quantize_info=DestroyQuantizeInfo(quantize_info);
cristy3ed852e2009-09-05 21:47:34 +00002460 return(status);
2461}
2462
2463/*
2464%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2465% %
2466% %
2467% %
2468+ P r u n e C h i l d %
2469% %
2470% %
2471% %
2472%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2473%
2474% PruneChild() deletes the given node and merges its statistics into its
2475% parent.
2476%
2477% The format of the PruneSubtree method is:
2478%
2479% PruneChild(const Image *image,CubeInfo *cube_info,
2480% const NodeInfo *node_info)
2481%
2482% A description of each parameter follows.
2483%
2484% o image: the image.
2485%
2486% o cube_info: A pointer to the Cube structure.
2487%
2488% o node_info: pointer to node in color cube tree that is to be pruned.
2489%
2490*/
2491static void PruneChild(const Image *image,CubeInfo *cube_info,
2492 const NodeInfo *node_info)
2493{
2494 NodeInfo
2495 *parent;
2496
cristybb503372010-05-27 20:51:26 +00002497 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002498 i;
2499
cristybb503372010-05-27 20:51:26 +00002500 size_t
cristy3ed852e2009-09-05 21:47:34 +00002501 number_children;
2502
2503 /*
2504 Traverse any children.
2505 */
2506 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002507 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002508 if (node_info->child[i] != (NodeInfo *) NULL)
2509 PruneChild(image,cube_info,node_info->child[i]);
2510 /*
2511 Merge color statistics into parent.
2512 */
2513 parent=node_info->parent;
2514 parent->number_unique+=node_info->number_unique;
2515 parent->total_color.red+=node_info->total_color.red;
2516 parent->total_color.green+=node_info->total_color.green;
2517 parent->total_color.blue+=node_info->total_color.blue;
cristy4c08aed2011-07-01 19:47:50 +00002518 parent->total_color.alpha+=node_info->total_color.alpha;
cristy3ed852e2009-09-05 21:47:34 +00002519 parent->child[node_info->id]=(NodeInfo *) NULL;
2520 cube_info->nodes--;
2521}
2522
2523/*
2524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2525% %
2526% %
2527% %
2528+ P r u n e L e v e l %
2529% %
2530% %
2531% %
2532%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2533%
2534% PruneLevel() deletes all nodes at the bottom level of the color tree merging
2535% their color statistics into their parent node.
2536%
2537% The format of the PruneLevel method is:
2538%
2539% PruneLevel(const Image *image,CubeInfo *cube_info,
2540% const NodeInfo *node_info)
2541%
2542% A description of each parameter follows.
2543%
2544% o image: the image.
2545%
2546% o cube_info: A pointer to the Cube structure.
2547%
2548% o node_info: pointer to node in color cube tree that is to be pruned.
2549%
2550*/
2551static void PruneLevel(const Image *image,CubeInfo *cube_info,
2552 const NodeInfo *node_info)
2553{
cristybb503372010-05-27 20:51:26 +00002554 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002555 i;
2556
cristybb503372010-05-27 20:51:26 +00002557 size_t
cristy3ed852e2009-09-05 21:47:34 +00002558 number_children;
2559
2560 /*
2561 Traverse any children.
2562 */
2563 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002564 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002565 if (node_info->child[i] != (NodeInfo *) NULL)
2566 PruneLevel(image,cube_info,node_info->child[i]);
2567 if (node_info->level == cube_info->depth)
2568 PruneChild(image,cube_info,node_info);
2569}
2570
2571/*
2572%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2573% %
2574% %
2575% %
2576+ P r u n e T o C u b e D e p t h %
2577% %
2578% %
2579% %
2580%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2581%
2582% PruneToCubeDepth() deletes any nodes at a depth greater than
2583% cube_info->depth while merging their color statistics into their parent
2584% node.
2585%
2586% The format of the PruneToCubeDepth method is:
2587%
2588% PruneToCubeDepth(const Image *image,CubeInfo *cube_info,
2589% const NodeInfo *node_info)
2590%
2591% A description of each parameter follows.
2592%
2593% o cube_info: A pointer to the Cube structure.
2594%
2595% o node_info: pointer to node in color cube tree that is to be pruned.
2596%
2597*/
2598static void PruneToCubeDepth(const Image *image,CubeInfo *cube_info,
2599 const NodeInfo *node_info)
2600{
cristybb503372010-05-27 20:51:26 +00002601 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002602 i;
2603
cristybb503372010-05-27 20:51:26 +00002604 size_t
cristy3ed852e2009-09-05 21:47:34 +00002605 number_children;
2606
2607 /*
2608 Traverse any children.
2609 */
2610 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002611 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002612 if (node_info->child[i] != (NodeInfo *) NULL)
2613 PruneToCubeDepth(image,cube_info,node_info->child[i]);
2614 if (node_info->level > cube_info->depth)
2615 PruneChild(image,cube_info,node_info);
2616}
2617
2618/*
2619%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2620% %
2621% %
2622% %
2623% Q u a n t i z e I m a g e %
2624% %
2625% %
2626% %
2627%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2628%
2629% QuantizeImage() analyzes the colors within a reference image and chooses a
2630% fixed number of colors to represent the image. The goal of the algorithm
2631% is to minimize the color difference between the input and output image while
2632% minimizing the processing time.
2633%
2634% The format of the QuantizeImage method is:
2635%
2636% MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002637% Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002638%
2639% A description of each parameter follows:
2640%
2641% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
2642%
2643% o image: the image.
2644%
cristy018f07f2011-09-04 21:15:19 +00002645% o exception: return any errors or warnings in this structure.
2646%
cristy3ed852e2009-09-05 21:47:34 +00002647*/
cristy5f7dca62011-08-12 12:38:05 +00002648
2649static MagickBooleanType DirectToColormapImage(Image *image,
2650 ExceptionInfo *exception)
2651{
2652 CacheView
2653 *image_view;
2654
2655 MagickBooleanType
2656 status;
2657
2658 register ssize_t
2659 i;
2660
2661 size_t
2662 number_colors;
2663
2664 ssize_t
2665 y;
2666
2667 status=MagickTrue;
2668 number_colors=(size_t) (image->columns*image->rows);
cristy018f07f2011-09-04 21:15:19 +00002669 if (AcquireImageColormap(image,number_colors,exception) == MagickFalse)
cristy5f7dca62011-08-12 12:38:05 +00002670 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2671 image->filename);
2672 if (image->colors != number_colors)
2673 return(MagickFalse);
2674 i=0;
2675 image_view=AcquireCacheView(image);
2676 for (y=0; y < (ssize_t) image->rows; y++)
2677 {
2678 MagickBooleanType
2679 proceed;
2680
2681 register Quantum
2682 *restrict q;
2683
2684 register ssize_t
2685 x;
2686
2687 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
2688 if (q == (Quantum *) NULL)
2689 break;
2690 for (x=0; x < (ssize_t) image->columns; x++)
2691 {
2692 image->colormap[i].red=GetPixelRed(image,q);
2693 image->colormap[i].green=GetPixelGreen(image,q);
2694 image->colormap[i].blue=GetPixelBlue(image,q);
2695 image->colormap[i].alpha=GetPixelAlpha(image,q);
2696 SetPixelIndex(image,(Quantum) i,q);
2697 i++;
2698 q+=GetPixelChannels(image);
2699 }
2700 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
2701 break;
2702 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y,
2703 image->rows);
2704 if (proceed == MagickFalse)
2705 status=MagickFalse;
2706 }
2707 image_view=DestroyCacheView(image_view);
2708 return(status);
2709}
2710
cristy3ed852e2009-09-05 21:47:34 +00002711MagickExport MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002712 Image *image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002713{
2714 CubeInfo
2715 *cube_info;
2716
2717 MagickBooleanType
2718 status;
2719
cristybb503372010-05-27 20:51:26 +00002720 size_t
cristy3ed852e2009-09-05 21:47:34 +00002721 depth,
2722 maximum_colors;
2723
2724 assert(quantize_info != (const QuantizeInfo *) NULL);
2725 assert(quantize_info->signature == MagickSignature);
2726 assert(image != (Image *) NULL);
2727 assert(image->signature == MagickSignature);
2728 if (image->debug != MagickFalse)
2729 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2730 maximum_colors=quantize_info->number_colors;
2731 if (maximum_colors == 0)
2732 maximum_colors=MaxColormapSize;
2733 if (maximum_colors > MaxColormapSize)
2734 maximum_colors=MaxColormapSize;
cristy5f7dca62011-08-12 12:38:05 +00002735 if ((image->columns*image->rows) <= maximum_colors)
cristy8a11cb12011-10-19 23:53:34 +00002736 (void) DirectToColormapImage(image,exception);
2737 if ((IsImageGray(image,exception) != MagickFalse) &&
cristy8e752752011-04-16 13:48:22 +00002738 (image->matte == MagickFalse))
cristy018f07f2011-09-04 21:15:19 +00002739 (void) SetGrayscaleImage(image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002740 if ((image->storage_class == PseudoClass) &&
2741 (image->colors <= maximum_colors))
2742 return(MagickTrue);
2743 depth=quantize_info->tree_depth;
2744 if (depth == 0)
2745 {
cristybb503372010-05-27 20:51:26 +00002746 size_t
cristy3ed852e2009-09-05 21:47:34 +00002747 colors;
2748
2749 /*
2750 Depth of color tree is: Log4(colormap size)+2.
2751 */
2752 colors=maximum_colors;
2753 for (depth=1; colors != 0; depth++)
2754 colors>>=2;
2755 if ((quantize_info->dither != MagickFalse) && (depth > 2))
2756 depth--;
2757 if ((image->matte != MagickFalse) && (depth > 5))
2758 depth--;
2759 }
2760 /*
2761 Initialize color cube.
2762 */
2763 cube_info=GetCubeInfo(quantize_info,depth,maximum_colors);
2764 if (cube_info == (CubeInfo *) NULL)
2765 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
2766 image->filename);
cristy8a11cb12011-10-19 23:53:34 +00002767 status=ClassifyImageColors(cube_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002768 if (status != MagickFalse)
2769 {
2770 /*
2771 Reduce the number of colors in the image.
2772 */
2773 ReduceImageColors(image,cube_info);
cristy018f07f2011-09-04 21:15:19 +00002774 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00002775 }
2776 DestroyCubeInfo(cube_info);
2777 return(status);
2778}
2779
2780/*
2781%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2782% %
2783% %
2784% %
2785% Q u a n t i z e I m a g e s %
2786% %
2787% %
2788% %
2789%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2790%
2791% QuantizeImages() analyzes the colors within a set of reference images and
2792% chooses a fixed number of colors to represent the set. The goal of the
2793% algorithm is to minimize the color difference between the input and output
2794% images while minimizing the processing time.
2795%
2796% The format of the QuantizeImages method is:
2797%
2798% MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002799% Image *images,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002800%
2801% A description of each parameter follows:
2802%
2803% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
2804%
2805% o images: Specifies a pointer to a list of Image structures.
2806%
cristy018f07f2011-09-04 21:15:19 +00002807% o exception: return any errors or warnings in this structure.
2808%
cristy3ed852e2009-09-05 21:47:34 +00002809*/
2810MagickExport MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00002811 Image *images,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002812{
2813 CubeInfo
2814 *cube_info;
2815
2816 Image
2817 *image;
2818
2819 MagickBooleanType
2820 proceed,
2821 status;
2822
2823 MagickProgressMonitor
2824 progress_monitor;
2825
cristybb503372010-05-27 20:51:26 +00002826 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002827 i;
2828
cristybb503372010-05-27 20:51:26 +00002829 size_t
cristy3ed852e2009-09-05 21:47:34 +00002830 depth,
2831 maximum_colors,
2832 number_images;
2833
2834 assert(quantize_info != (const QuantizeInfo *) NULL);
2835 assert(quantize_info->signature == MagickSignature);
2836 assert(images != (Image *) NULL);
2837 assert(images->signature == MagickSignature);
2838 if (images->debug != MagickFalse)
2839 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
2840 if (GetNextImageInList(images) == (Image *) NULL)
2841 {
2842 /*
2843 Handle a single image with QuantizeImage.
2844 */
cristy018f07f2011-09-04 21:15:19 +00002845 status=QuantizeImage(quantize_info,images,exception);
cristy3ed852e2009-09-05 21:47:34 +00002846 return(status);
2847 }
2848 status=MagickFalse;
2849 maximum_colors=quantize_info->number_colors;
2850 if (maximum_colors == 0)
2851 maximum_colors=MaxColormapSize;
2852 if (maximum_colors > MaxColormapSize)
2853 maximum_colors=MaxColormapSize;
2854 depth=quantize_info->tree_depth;
2855 if (depth == 0)
2856 {
cristybb503372010-05-27 20:51:26 +00002857 size_t
cristy3ed852e2009-09-05 21:47:34 +00002858 colors;
2859
2860 /*
2861 Depth of color tree is: Log4(colormap size)+2.
2862 */
2863 colors=maximum_colors;
2864 for (depth=1; colors != 0; depth++)
2865 colors>>=2;
2866 if (quantize_info->dither != MagickFalse)
2867 depth--;
2868 }
2869 /*
2870 Initialize color cube.
2871 */
2872 cube_info=GetCubeInfo(quantize_info,depth,maximum_colors);
2873 if (cube_info == (CubeInfo *) NULL)
2874 {
cristy8a11cb12011-10-19 23:53:34 +00002875 (void) ThrowMagickException(exception,GetMagickModule(),
cristy3ed852e2009-09-05 21:47:34 +00002876 ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename);
2877 return(MagickFalse);
2878 }
2879 number_images=GetImageListLength(images);
2880 image=images;
2881 for (i=0; image != (Image *) NULL; i++)
2882 {
2883 progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor) NULL,
2884 image->client_data);
cristy8a11cb12011-10-19 23:53:34 +00002885 status=ClassifyImageColors(cube_info,image,exception);
cristy3ed852e2009-09-05 21:47:34 +00002886 if (status == MagickFalse)
2887 break;
2888 (void) SetImageProgressMonitor(image,progress_monitor,image->client_data);
cristycee97112010-05-28 00:44:52 +00002889 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i,
2890 number_images);
cristy3ed852e2009-09-05 21:47:34 +00002891 if (proceed == MagickFalse)
2892 break;
2893 image=GetNextImageInList(image);
2894 }
2895 if (status != MagickFalse)
2896 {
2897 /*
2898 Reduce the number of colors in an image sequence.
2899 */
2900 ReduceImageColors(images,cube_info);
2901 image=images;
2902 for (i=0; image != (Image *) NULL; i++)
2903 {
2904 progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor)
2905 NULL,image->client_data);
cristy018f07f2011-09-04 21:15:19 +00002906 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00002907 if (status == MagickFalse)
2908 break;
2909 (void) SetImageProgressMonitor(image,progress_monitor,
2910 image->client_data);
cristycee97112010-05-28 00:44:52 +00002911 proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i,
2912 number_images);
cristy3ed852e2009-09-05 21:47:34 +00002913 if (proceed == MagickFalse)
2914 break;
2915 image=GetNextImageInList(image);
2916 }
2917 }
2918 DestroyCubeInfo(cube_info);
2919 return(status);
2920}
2921
2922/*
2923%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2924% %
2925% %
2926% %
2927+ R e d u c e %
2928% %
2929% %
2930% %
2931%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2932%
2933% Reduce() traverses the color cube tree and prunes any node whose
2934% quantization error falls below a particular threshold.
2935%
2936% The format of the Reduce method is:
2937%
2938% Reduce(const Image *image,CubeInfo *cube_info,const NodeInfo *node_info)
2939%
2940% A description of each parameter follows.
2941%
2942% o image: the image.
2943%
2944% o cube_info: A pointer to the Cube structure.
2945%
2946% o node_info: pointer to node in color cube tree that is to be pruned.
2947%
2948*/
2949static void Reduce(const Image *image,CubeInfo *cube_info,
2950 const NodeInfo *node_info)
2951{
cristybb503372010-05-27 20:51:26 +00002952 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002953 i;
2954
cristybb503372010-05-27 20:51:26 +00002955 size_t
cristy3ed852e2009-09-05 21:47:34 +00002956 number_children;
2957
2958 /*
2959 Traverse any children.
2960 */
2961 number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL;
cristybb503372010-05-27 20:51:26 +00002962 for (i=0; i < (ssize_t) number_children; i++)
cristy3ed852e2009-09-05 21:47:34 +00002963 if (node_info->child[i] != (NodeInfo *) NULL)
2964 Reduce(image,cube_info,node_info->child[i]);
2965 if (node_info->quantize_error <= cube_info->pruning_threshold)
2966 PruneChild(image,cube_info,node_info);
2967 else
2968 {
2969 /*
2970 Find minimum pruning threshold.
2971 */
2972 if (node_info->number_unique > 0)
2973 cube_info->colors++;
2974 if (node_info->quantize_error < cube_info->next_threshold)
2975 cube_info->next_threshold=node_info->quantize_error;
2976 }
2977}
2978
2979/*
2980%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2981% %
2982% %
2983% %
2984+ R e d u c e I m a g e C o l o r s %
2985% %
2986% %
2987% %
2988%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2989%
2990% ReduceImageColors() repeatedly prunes the tree until the number of nodes
2991% with n2 > 0 is less than or equal to the maximum number of colors allowed
2992% in the output image. On any given iteration over the tree, it selects
2993% those nodes whose E value is minimal for pruning and merges their
2994% color statistics upward. It uses a pruning threshold, Ep, to govern
2995% node selection as follows:
2996%
2997% Ep = 0
2998% while number of nodes with (n2 > 0) > required maximum number of colors
2999% prune all nodes such that E <= Ep
3000% Set Ep to minimum E in remaining nodes
3001%
3002% This has the effect of minimizing any quantization error when merging
3003% two nodes together.
3004%
3005% When a node to be pruned has offspring, the pruning procedure invokes
3006% itself recursively in order to prune the tree from the leaves upward.
3007% n2, Sr, Sg, and Sb in a node being pruned are always added to the
3008% corresponding data in that node's parent. This retains the pruned
3009% node's color characteristics for later averaging.
3010%
3011% For each node, n2 pixels exist for which that node represents the
3012% smallest volume in RGB space containing those pixel's colors. When n2
3013% > 0 the node will uniquely define a color in the output image. At the
3014% beginning of reduction, n2 = 0 for all nodes except a the leaves of
3015% the tree which represent colors present in the input image.
3016%
3017% The other pixel count, n1, indicates the total number of colors
3018% within the cubic volume which the node represents. This includes n1 -
3019% n2 pixels whose colors should be defined by nodes at a lower level in
3020% the tree.
3021%
3022% The format of the ReduceImageColors method is:
3023%
3024% ReduceImageColors(const Image *image,CubeInfo *cube_info)
3025%
3026% A description of each parameter follows.
3027%
3028% o image: the image.
3029%
3030% o cube_info: A pointer to the Cube structure.
3031%
3032*/
3033static void ReduceImageColors(const Image *image,CubeInfo *cube_info)
3034{
3035#define ReduceImageTag "Reduce/Image"
3036
3037 MagickBooleanType
3038 proceed;
3039
3040 MagickOffsetType
3041 offset;
3042
cristybb503372010-05-27 20:51:26 +00003043 size_t
cristy3ed852e2009-09-05 21:47:34 +00003044 span;
3045
3046 cube_info->next_threshold=0.0;
3047 for (span=cube_info->colors; cube_info->colors > cube_info->maximum_colors; )
3048 {
3049 cube_info->pruning_threshold=cube_info->next_threshold;
3050 cube_info->next_threshold=cube_info->root->quantize_error-1;
3051 cube_info->colors=0;
3052 Reduce(image,cube_info,cube_info->root);
3053 offset=(MagickOffsetType) span-cube_info->colors;
3054 proceed=SetImageProgress(image,ReduceImageTag,offset,span-
3055 cube_info->maximum_colors+1);
3056 if (proceed == MagickFalse)
3057 break;
3058 }
3059}
3060
3061/*
3062%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3063% %
3064% %
3065% %
3066% R e m a p I m a g e %
3067% %
3068% %
3069% %
3070%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3071%
anthony31f1bf72012-01-30 12:37:22 +00003072% RemapImage() replaces the colors of an image with a dither of the colors
3073% provided.
cristy3ed852e2009-09-05 21:47:34 +00003074%
3075% The format of the RemapImage method is:
3076%
3077% MagickBooleanType RemapImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003078% Image *image,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003079%
3080% A description of each parameter follows:
3081%
3082% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
3083%
3084% o image: the image.
3085%
3086% o remap_image: the reference image.
3087%
cristy018f07f2011-09-04 21:15:19 +00003088% o exception: return any errors or warnings in this structure.
3089%
cristy3ed852e2009-09-05 21:47:34 +00003090*/
3091MagickExport MagickBooleanType RemapImage(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003092 Image *image,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003093{
3094 CubeInfo
3095 *cube_info;
3096
3097 MagickBooleanType
3098 status;
3099
3100 /*
3101 Initialize color cube.
3102 */
3103 assert(image != (Image *) NULL);
3104 assert(image->signature == MagickSignature);
3105 if (image->debug != MagickFalse)
3106 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
3107 assert(remap_image != (Image *) NULL);
3108 assert(remap_image->signature == MagickSignature);
3109 cube_info=GetCubeInfo(quantize_info,MaxTreeDepth,
3110 quantize_info->number_colors);
3111 if (cube_info == (CubeInfo *) NULL)
3112 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3113 image->filename);
cristy8a11cb12011-10-19 23:53:34 +00003114 status=ClassifyImageColors(cube_info,remap_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003115 if (status != MagickFalse)
3116 {
3117 /*
3118 Classify image colors from the reference image.
3119 */
3120 cube_info->quantize_info->number_colors=cube_info->colors;
cristy018f07f2011-09-04 21:15:19 +00003121 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003122 }
3123 DestroyCubeInfo(cube_info);
3124 return(status);
3125}
3126
3127/*
3128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3129% %
3130% %
3131% %
3132% R e m a p I m a g e s %
3133% %
3134% %
3135% %
3136%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3137%
3138% RemapImages() replaces the colors of a sequence of images with the
3139% closest color from a reference image.
3140%
3141% The format of the RemapImage method is:
3142%
3143% MagickBooleanType RemapImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003144% Image *images,Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003145%
3146% A description of each parameter follows:
3147%
3148% o quantize_info: Specifies a pointer to an QuantizeInfo structure.
3149%
3150% o images: the image sequence.
3151%
3152% o remap_image: the reference image.
3153%
cristy018f07f2011-09-04 21:15:19 +00003154% o exception: return any errors or warnings in this structure.
3155%
cristy3ed852e2009-09-05 21:47:34 +00003156*/
3157MagickExport MagickBooleanType RemapImages(const QuantizeInfo *quantize_info,
cristy018f07f2011-09-04 21:15:19 +00003158 Image *images,const Image *remap_image,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003159{
3160 CubeInfo
3161 *cube_info;
3162
3163 Image
3164 *image;
3165
3166 MagickBooleanType
3167 status;
3168
3169 assert(images != (Image *) NULL);
3170 assert(images->signature == MagickSignature);
3171 if (images->debug != MagickFalse)
3172 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename);
3173 image=images;
3174 if (remap_image == (Image *) NULL)
3175 {
3176 /*
3177 Create a global colormap for an image sequence.
3178 */
cristy018f07f2011-09-04 21:15:19 +00003179 status=QuantizeImages(quantize_info,images,exception);
cristy3ed852e2009-09-05 21:47:34 +00003180 return(status);
3181 }
3182 /*
3183 Classify image colors from the reference image.
3184 */
3185 cube_info=GetCubeInfo(quantize_info,MaxTreeDepth,
3186 quantize_info->number_colors);
3187 if (cube_info == (CubeInfo *) NULL)
3188 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3189 image->filename);
cristy018f07f2011-09-04 21:15:19 +00003190 status=ClassifyImageColors(cube_info,remap_image,exception);
cristy3ed852e2009-09-05 21:47:34 +00003191 if (status != MagickFalse)
3192 {
3193 /*
3194 Classify image colors from the reference image.
3195 */
3196 cube_info->quantize_info->number_colors=cube_info->colors;
3197 image=images;
3198 for ( ; image != (Image *) NULL; image=GetNextImageInList(image))
3199 {
cristy018f07f2011-09-04 21:15:19 +00003200 status=AssignImageColors(image,cube_info,exception);
cristy3ed852e2009-09-05 21:47:34 +00003201 if (status == MagickFalse)
3202 break;
3203 }
3204 }
3205 DestroyCubeInfo(cube_info);
3206 return(status);
3207}
3208
3209/*
3210%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3211% %
3212% %
3213% %
3214% S e t G r a y s c a l e I m a g e %
3215% %
3216% %
3217% %
3218%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3219%
3220% SetGrayscaleImage() converts an image to a PseudoClass grayscale image.
3221%
3222% The format of the SetGrayscaleImage method is:
3223%
cristy018f07f2011-09-04 21:15:19 +00003224% MagickBooleanType SetGrayscaleImage(Image *image,ExceptionInfo *exeption)
cristy3ed852e2009-09-05 21:47:34 +00003225%
3226% A description of each parameter follows:
3227%
3228% o image: The image.
3229%
cristy018f07f2011-09-04 21:15:19 +00003230% o exception: return any errors or warnings in this structure.
3231%
cristy3ed852e2009-09-05 21:47:34 +00003232*/
3233
3234#if defined(__cplusplus) || defined(c_plusplus)
3235extern "C" {
3236#endif
3237
3238static int IntensityCompare(const void *x,const void *y)
3239{
cristy101ab702011-10-13 13:06:32 +00003240 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003241 *color_1,
3242 *color_2;
3243
cristyecc31b12011-02-13 00:32:29 +00003244 ssize_t
3245 intensity;
3246
cristy101ab702011-10-13 13:06:32 +00003247 color_1=(PixelInfo *) x;
3248 color_2=(PixelInfo *) y;
3249 intensity=GetPixelInfoIntensity(color_1)-(ssize_t)
3250 GetPixelInfoIntensity(color_2);
cristycee97112010-05-28 00:44:52 +00003251 return((int) intensity);
cristy3ed852e2009-09-05 21:47:34 +00003252}
3253
3254#if defined(__cplusplus) || defined(c_plusplus)
3255}
3256#endif
3257
cristy018f07f2011-09-04 21:15:19 +00003258static MagickBooleanType SetGrayscaleImage(Image *image,
3259 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00003260{
cristyc4c8d132010-01-07 01:58:38 +00003261 CacheView
3262 *image_view;
3263
cristyecc31b12011-02-13 00:32:29 +00003264 MagickBooleanType
3265 status;
cristy3ed852e2009-09-05 21:47:34 +00003266
cristy101ab702011-10-13 13:06:32 +00003267 PixelInfo
cristy3ed852e2009-09-05 21:47:34 +00003268 *colormap;
3269
cristybb503372010-05-27 20:51:26 +00003270 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00003271 i;
3272
cristyecc31b12011-02-13 00:32:29 +00003273 ssize_t
3274 *colormap_index,
3275 j,
3276 y;
cristy3ed852e2009-09-05 21:47:34 +00003277
cristy3ed852e2009-09-05 21:47:34 +00003278 assert(image != (Image *) NULL);
3279 assert(image->signature == MagickSignature);
3280 if (image->type != GrayscaleType)
cristye941a752011-10-15 01:52:48 +00003281 (void) TransformImageColorspace(image,GRAYColorspace,exception);
cristybb503372010-05-27 20:51:26 +00003282 colormap_index=(ssize_t *) AcquireQuantumMemory(MaxMap+1,
cristy3ed852e2009-09-05 21:47:34 +00003283 sizeof(*colormap_index));
cristybb503372010-05-27 20:51:26 +00003284 if (colormap_index == (ssize_t *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003285 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3286 image->filename);
3287 if (image->storage_class != PseudoClass)
3288 {
cristybb503372010-05-27 20:51:26 +00003289 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy3ed852e2009-09-05 21:47:34 +00003290 colormap_index[i]=(-1);
cristy018f07f2011-09-04 21:15:19 +00003291 if (AcquireImageColormap(image,MaxMap+1,exception) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003292 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3293 image->filename);
3294 image->colors=0;
3295 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00003296 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00003297#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003298 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003299#endif
cristybb503372010-05-27 20:51:26 +00003300 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003301 {
cristy4c08aed2011-07-01 19:47:50 +00003302 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003303 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003304
cristyecc31b12011-02-13 00:32:29 +00003305 register ssize_t
3306 x;
3307
cristy3ed852e2009-09-05 21:47:34 +00003308 if (status == MagickFalse)
3309 continue;
3310 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,
3311 exception);
cristyacd2ed22011-08-30 01:44:23 +00003312 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003313 {
3314 status=MagickFalse;
3315 continue;
3316 }
cristybb503372010-05-27 20:51:26 +00003317 for (x=0; x < (ssize_t) image->columns; x++)
cristy3ed852e2009-09-05 21:47:34 +00003318 {
cristybb503372010-05-27 20:51:26 +00003319 register size_t
cristy3ed852e2009-09-05 21:47:34 +00003320 intensity;
3321
cristy4c08aed2011-07-01 19:47:50 +00003322 intensity=ScaleQuantumToMap(GetPixelRed(image,q));
cristy3ed852e2009-09-05 21:47:34 +00003323 if (colormap_index[intensity] < 0)
3324 {
cristyb5d5f722009-11-04 03:03:49 +00003325#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00003326 #pragma omp critical (MagickCore_SetGrayscaleImage)
3327#endif
3328 if (colormap_index[intensity] < 0)
3329 {
cristybb503372010-05-27 20:51:26 +00003330 colormap_index[intensity]=(ssize_t) image->colors;
cristy4c08aed2011-07-01 19:47:50 +00003331 image->colormap[image->colors].red=GetPixelRed(image,q);
3332 image->colormap[image->colors].green=GetPixelGreen(image,q);
3333 image->colormap[image->colors].blue=GetPixelBlue(image,q);
cristy3ed852e2009-09-05 21:47:34 +00003334 image->colors++;
3335 }
3336 }
cristy4c08aed2011-07-01 19:47:50 +00003337 SetPixelIndex(image,(Quantum)
3338 colormap_index[intensity],q);
cristyed231572011-07-14 02:18:59 +00003339 q+=GetPixelChannels(image);
cristy3ed852e2009-09-05 21:47:34 +00003340 }
3341 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3342 status=MagickFalse;
3343 }
3344 image_view=DestroyCacheView(image_view);
3345 }
cristybb503372010-05-27 20:51:26 +00003346 for (i=0; i < (ssize_t) image->colors; i++)
cristy4c08aed2011-07-01 19:47:50 +00003347 image->colormap[i].alpha=(unsigned short) i;
cristy101ab702011-10-13 13:06:32 +00003348 qsort((void *) image->colormap,image->colors,sizeof(PixelInfo),
cristy3ed852e2009-09-05 21:47:34 +00003349 IntensityCompare);
cristy101ab702011-10-13 13:06:32 +00003350 colormap=(PixelInfo *) AcquireQuantumMemory(image->colors,
cristy3ed852e2009-09-05 21:47:34 +00003351 sizeof(*colormap));
cristy101ab702011-10-13 13:06:32 +00003352 if (colormap == (PixelInfo *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003353 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
3354 image->filename);
3355 j=0;
3356 colormap[j]=image->colormap[0];
cristybb503372010-05-27 20:51:26 +00003357 for (i=0; i < (ssize_t) image->colors; i++)
cristy3ed852e2009-09-05 21:47:34 +00003358 {
cristy101ab702011-10-13 13:06:32 +00003359 if (IsPixelInfoEquivalent(&colormap[j],&image->colormap[i]) == MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003360 {
3361 j++;
3362 colormap[j]=image->colormap[i];
3363 }
cristy4c08aed2011-07-01 19:47:50 +00003364 colormap_index[(ssize_t) image->colormap[i].alpha]=j;
cristy3ed852e2009-09-05 21:47:34 +00003365 }
cristybb503372010-05-27 20:51:26 +00003366 image->colors=(size_t) (j+1);
cristy101ab702011-10-13 13:06:32 +00003367 image->colormap=(PixelInfo *) RelinquishMagickMemory(image->colormap);
cristy3ed852e2009-09-05 21:47:34 +00003368 image->colormap=colormap;
3369 status=MagickTrue;
cristy3ed852e2009-09-05 21:47:34 +00003370 image_view=AcquireCacheView(image);
cristyb5d5f722009-11-04 03:03:49 +00003371#if defined(MAGICKCORE_OPENMP_SUPPORT)
cristye6178502011-12-23 17:02:29 +00003372 #pragma omp parallel for schedule(static,4) shared(status)
cristy3ed852e2009-09-05 21:47:34 +00003373#endif
cristybb503372010-05-27 20:51:26 +00003374 for (y=0; y < (ssize_t) image->rows; y++)
cristy3ed852e2009-09-05 21:47:34 +00003375 {
cristy4c08aed2011-07-01 19:47:50 +00003376 register Quantum
cristyc47d1f82009-11-26 01:44:43 +00003377 *restrict q;
cristy3ed852e2009-09-05 21:47:34 +00003378
cristyecc31b12011-02-13 00:32:29 +00003379 register ssize_t
3380 x;
3381
cristy3ed852e2009-09-05 21:47:34 +00003382 if (status == MagickFalse)
3383 continue;
3384 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
cristyacd2ed22011-08-30 01:44:23 +00003385 if (q == (Quantum *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00003386 {
3387 status=MagickFalse;
3388 continue;
3389 }
cristybb503372010-05-27 20:51:26 +00003390 for (x=0; x < (ssize_t) image->columns; x++)
cristy4c08aed2011-07-01 19:47:50 +00003391 {
3392 SetPixelIndex(image,(Quantum) colormap_index[ScaleQuantumToMap(
3393 GetPixelIndex(image,q))],q);
cristyed231572011-07-14 02:18:59 +00003394 q+=GetPixelChannels(image);
cristy4c08aed2011-07-01 19:47:50 +00003395 }
cristy3ed852e2009-09-05 21:47:34 +00003396 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
3397 status=MagickFalse;
3398 }
3399 image_view=DestroyCacheView(image_view);
cristybb503372010-05-27 20:51:26 +00003400 colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index);
cristy3ed852e2009-09-05 21:47:34 +00003401 image->type=GrayscaleType;
cristy8a11cb12011-10-19 23:53:34 +00003402 if (IsImageMonochrome(image,exception) != MagickFalse)
cristy3ed852e2009-09-05 21:47:34 +00003403 image->type=BilevelType;
3404 return(status);
3405}