cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | % % |
cristy | 7e41fe8 | 2010-12-04 23:12:08 +0000 | [diff] [blame] | 20 | % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 21 | % 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 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 177 | #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" |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 184 | #include "MagickCore/colorspace-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 185 | #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" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | Define declarations. |
| 205 | */ |
cristy | e128751 | 2010-06-19 17:38:25 +0000 | [diff] [blame] | 206 | #if !defined(__APPLE__) && !defined(TARGET_OS_IPHONE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 207 | #define CacheShift 2 |
cristy | e128751 | 2010-06-19 17:38:25 +0000 | [diff] [blame] | 208 | #else |
| 209 | #define CacheShift 3 |
| 210 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 211 | #define ErrorQueueLength 16 |
| 212 | #define MaxNodes 266817 |
| 213 | #define MaxTreeDepth 8 |
| 214 | #define NodesInAList 1920 |
| 215 | |
| 216 | /* |
| 217 | Typdef declarations. |
| 218 | */ |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 219 | typedef struct _RealPixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 220 | { |
| 221 | MagickRealType |
| 222 | red, |
| 223 | green, |
| 224 | blue, |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 225 | alpha; |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 226 | } RealPixelInfo; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 227 | |
| 228 | typedef struct _NodeInfo |
| 229 | { |
| 230 | struct _NodeInfo |
| 231 | *parent, |
| 232 | *child[16]; |
| 233 | |
| 234 | MagickSizeType |
| 235 | number_unique; |
| 236 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 237 | RealPixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 238 | total_color; |
| 239 | |
| 240 | MagickRealType |
| 241 | quantize_error; |
| 242 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 243 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 244 | color_number, |
| 245 | id, |
| 246 | level; |
| 247 | } NodeInfo; |
| 248 | |
| 249 | typedef struct _Nodes |
| 250 | { |
| 251 | NodeInfo |
| 252 | *nodes; |
| 253 | |
| 254 | struct _Nodes |
| 255 | *next; |
| 256 | } Nodes; |
| 257 | |
| 258 | typedef struct _CubeInfo |
| 259 | { |
| 260 | NodeInfo |
| 261 | *root; |
| 262 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 263 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 264 | colors, |
| 265 | maximum_colors; |
| 266 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 267 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 268 | transparent_index; |
| 269 | |
| 270 | MagickSizeType |
| 271 | transparent_pixels; |
| 272 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 273 | RealPixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 274 | target; |
| 275 | |
| 276 | MagickRealType |
| 277 | distance, |
| 278 | pruning_threshold, |
| 279 | next_threshold; |
| 280 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 281 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 282 | nodes, |
| 283 | free_nodes, |
| 284 | color_number; |
| 285 | |
| 286 | NodeInfo |
| 287 | *next_node; |
| 288 | |
| 289 | Nodes |
| 290 | *node_queue; |
| 291 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 292 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 293 | *cache; |
| 294 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 295 | RealPixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 296 | error[ErrorQueueLength]; |
| 297 | |
| 298 | MagickRealType |
| 299 | weights[ErrorQueueLength]; |
| 300 | |
| 301 | QuantizeInfo |
| 302 | *quantize_info; |
| 303 | |
| 304 | MagickBooleanType |
| 305 | associate_alpha; |
| 306 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 307 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 308 | x, |
| 309 | y; |
| 310 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 311 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 312 | depth; |
| 313 | |
| 314 | MagickOffsetType |
| 315 | offset; |
| 316 | |
| 317 | MagickSizeType |
| 318 | span; |
| 319 | } CubeInfo; |
| 320 | |
| 321 | /* |
| 322 | Method prototypes. |
| 323 | */ |
| 324 | static CubeInfo |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 325 | *GetCubeInfo(const QuantizeInfo *,const size_t,const size_t); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 326 | |
| 327 | static NodeInfo |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 328 | *GetNodeInfo(CubeInfo *,const size_t,const size_t,NodeInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 329 | |
| 330 | static MagickBooleanType |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 331 | AssignImageColors(Image *,CubeInfo *,ExceptionInfo *), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 332 | ClassifyImageColors(CubeInfo *,const Image *,ExceptionInfo *), |
| 333 | DitherImage(Image *,CubeInfo *), |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 334 | SetGrayscaleImage(Image *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 335 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 336 | static size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 337 | DefineImageColormap(Image *,CubeInfo *,NodeInfo *); |
| 338 | |
| 339 | static 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 | */ |
| 368 | MagickExport QuantizeInfo *AcquireQuantizeInfo(const ImageInfo *image_info) |
| 369 | { |
| 370 | QuantizeInfo |
| 371 | *quantize_info; |
| 372 | |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 373 | quantize_info=(QuantizeInfo *) AcquireMagickMemory(sizeof(*quantize_info)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 374 | 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) |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 385 | quantize_info->dither_method=(DitherMethod) ParseCommandOption( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 386 | 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 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 432 | static inline void AssociateAlphaPixel(const Image *image, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 433 | const CubeInfo *cube_info,const Quantum *pixel,RealPixelInfo *alpha_pixel) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 434 | { |
| 435 | MagickRealType |
| 436 | alpha; |
| 437 | |
| 438 | if ((cube_info->associate_alpha == MagickFalse) || |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 439 | (GetPixelAlpha(image,pixel)== OpaqueAlpha)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 440 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 441 | 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); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 445 | return; |
| 446 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 447 | 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 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 454 | static inline void AssociateAlphaPixelInfo(const Image *image, |
| 455 | const CubeInfo *cube_info,const PixelInfo *pixel, |
| 456 | RealPixelInfo *alpha_pixel) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 457 | { |
| 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; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 475 | } |
| 476 | |
cristy | 75ffdb7 | 2010-01-07 17:40:12 +0000 | [diff] [blame] | 477 | static inline Quantum ClampToUnsignedQuantum(const MagickRealType value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 478 | { |
| 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 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 486 | static inline size_t ColorToNodeId(const CubeInfo *cube_info, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 487 | const RealPixelInfo *pixel,size_t index) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 488 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 489 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 490 | id; |
| 491 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 492 | 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); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 495 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 496 | id|=((ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->alpha)) >> index) & 0x1) << 3; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 497 | return(id); |
| 498 | } |
| 499 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 500 | static MagickBooleanType AssignImageColors(Image *image,CubeInfo *cube_info, |
| 501 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 502 | { |
| 503 | #define AssignImageTag "Assign/Image" |
| 504 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 505 | ssize_t |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 506 | y; |
| 507 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 508 | /* |
| 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, |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 514 | cube_info->quantize_info->colorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 515 | else |
| 516 | if ((image->colorspace != GRAYColorspace) && |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 517 | (IsRGBColorspace(image->colorspace) == MagickFalse) && |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 518 | (image->colorspace != CMYColorspace)) |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 519 | (void) TransformImageColorspace((Image *) image,RGBColorspace,exception); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 520 | if (AcquireImageColormap(image,cube_info->colors,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 521 | 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) && |
cristy | d5acfd1 | 2010-06-15 00:11:38 +0000 | [diff] [blame] | 531 | (cube_info->quantize_info->dither_method != NoDitherMethod)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 532 | (void) DitherImage(image,cube_info); |
| 533 | else |
| 534 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 535 | CacheView |
| 536 | *image_view; |
| 537 | |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 538 | ExceptionInfo |
| 539 | *exception; |
| 540 | |
| 541 | MagickBooleanType |
| 542 | status; |
| 543 | |
| 544 | status=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 545 | exception=(&image->exception); |
| 546 | image_view=AcquireCacheView(image); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 547 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 548 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 549 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 550 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 551 | { |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 552 | CubeInfo |
| 553 | cube; |
| 554 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 555 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 556 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 557 | |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 558 | register ssize_t |
| 559 | x; |
| 560 | |
| 561 | ssize_t |
| 562 | count; |
| 563 | |
| 564 | if (status == MagickFalse) |
| 565 | continue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 566 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1, |
| 567 | exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 568 | if (q == (Quantum *) NULL) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 569 | { |
| 570 | status=MagickFalse; |
| 571 | continue; |
| 572 | } |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 573 | cube=(*cube_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 574 | for (x=0; x < (ssize_t) image->columns; x+=count) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 575 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 576 | RealPixelInfo |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 577 | pixel; |
| 578 | |
| 579 | register const NodeInfo |
| 580 | *node_info; |
| 581 | |
| 582 | register ssize_t |
| 583 | i; |
| 584 | |
| 585 | size_t |
| 586 | id, |
| 587 | index; |
| 588 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 589 | /* |
| 590 | Identify the deepest node containing the pixel's color. |
| 591 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 592 | for (count=1; (x+count) < (ssize_t) image->columns; count++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 593 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 594 | PixelInfo |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 595 | packet; |
| 596 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 597 | GetPixelInfoPixel(image,q+count*GetPixelChannels(image),&packet); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 598 | if (IsPixelEquivalent(image,q,&packet) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 599 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 600 | } |
| 601 | AssociateAlphaPixel(image,&cube,q,&pixel); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 602 | node_info=cube.root; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 603 | for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 604 | { |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 605 | id=ColorToNodeId(&cube,&pixel,index); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 606 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 607 | break; |
| 608 | node_info=node_info->child[id]; |
| 609 | } |
| 610 | /* |
| 611 | Find closest color among siblings and their children. |
| 612 | */ |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 613 | cube.target=pixel; |
| 614 | cube.distance=(MagickRealType) (4.0*(QuantumRange+1.0)* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 615 | (QuantumRange+1.0)+1.0); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 616 | ClosestColor(image,&cube,node_info->parent); |
| 617 | index=cube.color_number; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 618 | for (i=0; i < (ssize_t) count; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 619 | { |
| 620 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 621 | SetPixelIndex(image,(Quantum) index,q); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 622 | if (cube.quantize_info->measure_error == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 623 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 624 | SetPixelRed(image,image->colormap[index].red,q); |
| 625 | SetPixelGreen(image,image->colormap[index].green,q); |
| 626 | SetPixelBlue(image,image->colormap[index].blue,q); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 627 | if (cube.associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 628 | SetPixelAlpha(image,image->colormap[index].alpha,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 629 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 630 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 631 | } |
| 632 | } |
| 633 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 634 | status=MagickFalse; |
| 635 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 636 | { |
| 637 | MagickBooleanType |
| 638 | proceed; |
| 639 | |
| 640 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 641 | #pragma omp critical (MagickCore_AssignImageColors) |
| 642 | #endif |
| 643 | proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y, |
| 644 | image->rows); |
| 645 | if (proceed == MagickFalse) |
| 646 | status=MagickFalse; |
| 647 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 648 | } |
| 649 | image_view=DestroyCacheView(image_view); |
| 650 | } |
| 651 | if (cube_info->quantize_info->measure_error != MagickFalse) |
| 652 | (void) GetImageQuantizeError(image); |
| 653 | if ((cube_info->quantize_info->number_colors == 2) && |
| 654 | (cube_info->quantize_info->colorspace == GRAYColorspace)) |
| 655 | { |
| 656 | Quantum |
| 657 | intensity; |
| 658 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 659 | register PixelInfo |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 660 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 661 | |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 662 | register ssize_t |
| 663 | i; |
| 664 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 665 | /* |
| 666 | Monochrome image. |
| 667 | */ |
| 668 | q=image->colormap; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 669 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 670 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 671 | intensity=(Quantum) ((MagickRealType) GetPixelInfoIntensity(q) < |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 672 | ((MagickRealType) QuantumRange/2.0) ? 0 : QuantumRange); |
| 673 | q->red=intensity; |
| 674 | q->green=intensity; |
| 675 | q->blue=intensity; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 676 | q++; |
| 677 | } |
| 678 | } |
| 679 | (void) SyncImage(image); |
| 680 | if ((cube_info->quantize_info->colorspace != UndefinedColorspace) && |
| 681 | (cube_info->quantize_info->colorspace != CMYKColorspace)) |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 682 | (void) TransformImageColorspace((Image *) image,RGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 683 | return(MagickTrue); |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 688 | % % |
| 689 | % % |
| 690 | % % |
| 691 | + C l a s s i f y I m a g e C o l o r s % |
| 692 | % % |
| 693 | % % |
| 694 | % % |
| 695 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 696 | % |
| 697 | % ClassifyImageColors() begins by initializing a color description tree |
| 698 | % of sufficient depth to represent each possible input color in a leaf. |
| 699 | % However, it is impractical to generate a fully-formed color |
| 700 | % description tree in the storage_class phase for realistic values of |
| 701 | % Cmax. If colors components in the input image are quantized to k-bit |
| 702 | % precision, so that Cmax= 2k-1, the tree would need k levels below the |
| 703 | % root node to allow representing each possible input color in a leaf. |
| 704 | % This becomes prohibitive because the tree's total number of nodes is |
| 705 | % 1 + sum(i=1,k,8k). |
| 706 | % |
| 707 | % A complete tree would require 19,173,961 nodes for k = 8, Cmax = 255. |
| 708 | % Therefore, to avoid building a fully populated tree, QUANTIZE: (1) |
| 709 | % Initializes data structures for nodes only as they are needed; (2) |
| 710 | % Chooses a maximum depth for the tree as a function of the desired |
| 711 | % number of colors in the output image (currently log2(colormap size)). |
| 712 | % |
| 713 | % For each pixel in the input image, storage_class scans downward from |
| 714 | % the root of the color description tree. At each level of the tree it |
| 715 | % identifies the single node which represents a cube in RGB space |
| 716 | % containing It updates the following data for each such node: |
| 717 | % |
| 718 | % n1 : Number of pixels whose color is contained in the RGB cube |
| 719 | % which this node represents; |
| 720 | % |
| 721 | % n2 : Number of pixels whose color is not represented in a node at |
| 722 | % lower depth in the tree; initially, n2 = 0 for all nodes except |
| 723 | % leaves of the tree. |
| 724 | % |
| 725 | % Sr, Sg, Sb : Sums of the red, green, and blue component values for |
| 726 | % all pixels not classified at a lower depth. The combination of |
| 727 | % these sums and n2 will ultimately characterize the mean color of a |
| 728 | % set of pixels represented by this node. |
| 729 | % |
| 730 | % E: the distance squared in RGB space between each pixel contained |
| 731 | % within a node and the nodes' center. This represents the quantization |
| 732 | % error for a node. |
| 733 | % |
| 734 | % The format of the ClassifyImageColors() method is: |
| 735 | % |
| 736 | % MagickBooleanType ClassifyImageColors(CubeInfo *cube_info, |
| 737 | % const Image *image,ExceptionInfo *exception) |
| 738 | % |
| 739 | % A description of each parameter follows. |
| 740 | % |
| 741 | % o cube_info: A pointer to the Cube structure. |
| 742 | % |
| 743 | % o image: the image. |
| 744 | % |
| 745 | */ |
| 746 | |
| 747 | static inline void SetAssociatedAlpha(const Image *image,CubeInfo *cube_info) |
| 748 | { |
| 749 | MagickBooleanType |
| 750 | associate_alpha; |
| 751 | |
| 752 | associate_alpha=image->matte; |
| 753 | if (cube_info->quantize_info->colorspace == TransparentColorspace) |
| 754 | associate_alpha=MagickFalse; |
| 755 | if ((cube_info->quantize_info->number_colors == 2) && |
| 756 | (cube_info->quantize_info->colorspace == GRAYColorspace)) |
| 757 | associate_alpha=MagickFalse; |
| 758 | cube_info->associate_alpha=associate_alpha; |
| 759 | } |
| 760 | |
| 761 | static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info, |
| 762 | const Image *image,ExceptionInfo *exception) |
| 763 | { |
| 764 | #define ClassifyImageTag "Classify/Image" |
| 765 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 766 | CacheView |
| 767 | *image_view; |
| 768 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 769 | MagickBooleanType |
| 770 | proceed; |
| 771 | |
| 772 | MagickRealType |
| 773 | bisect; |
| 774 | |
| 775 | NodeInfo |
| 776 | *node_info; |
| 777 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 778 | RealPixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 779 | error, |
| 780 | mid, |
| 781 | midpoint, |
| 782 | pixel; |
| 783 | |
| 784 | size_t |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 785 | count, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 786 | id, |
| 787 | index, |
| 788 | level; |
| 789 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 790 | ssize_t |
| 791 | y; |
| 792 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 793 | /* |
| 794 | Classify the first cube_info->maximum_colors colors to a tree depth of 8. |
| 795 | */ |
| 796 | SetAssociatedAlpha(image,cube_info); |
| 797 | if ((cube_info->quantize_info->colorspace != UndefinedColorspace) && |
| 798 | (cube_info->quantize_info->colorspace != CMYKColorspace)) |
| 799 | (void) TransformImageColorspace((Image *) image, |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 800 | cube_info->quantize_info->colorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 801 | else |
| 802 | if ((image->colorspace != GRAYColorspace) && |
| 803 | (image->colorspace != CMYColorspace) && |
cristy | 510d06a | 2011-07-06 23:43:54 +0000 | [diff] [blame] | 804 | (IsRGBColorspace(image->colorspace) == MagickFalse)) |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 805 | (void) TransformImageColorspace((Image *) image,RGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 806 | midpoint.red=(MagickRealType) QuantumRange/2.0; |
| 807 | midpoint.green=(MagickRealType) QuantumRange/2.0; |
| 808 | midpoint.blue=(MagickRealType) QuantumRange/2.0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 809 | midpoint.alpha=(MagickRealType) QuantumRange/2.0; |
| 810 | error.alpha=0.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 811 | image_view=AcquireCacheView(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 812 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 813 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 814 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 815 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 816 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 817 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 818 | x; |
| 819 | |
| 820 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 821 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 822 | break; |
| 823 | if (cube_info->nodes > MaxNodes) |
| 824 | { |
| 825 | /* |
| 826 | Prune one level if the color tree is too large. |
| 827 | */ |
| 828 | PruneLevel(image,cube_info,cube_info->root); |
| 829 | cube_info->depth--; |
| 830 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 831 | for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 832 | { |
| 833 | /* |
| 834 | Start at the root and descend the color cube tree. |
| 835 | */ |
cristy | bb66d9c | 2010-10-09 01:40:31 +0000 | [diff] [blame] | 836 | for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 837 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 838 | PixelInfo |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 839 | packet; |
| 840 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 841 | GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 842 | if (IsPixelEquivalent(image,p,&packet) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 843 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 844 | } |
| 845 | AssociateAlphaPixel(image,cube_info,p,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 846 | index=MaxTreeDepth-1; |
| 847 | bisect=((MagickRealType) QuantumRange+1.0)/2.0; |
| 848 | mid=midpoint; |
| 849 | node_info=cube_info->root; |
| 850 | for (level=1; level <= MaxTreeDepth; level++) |
| 851 | { |
| 852 | bisect*=0.5; |
| 853 | id=ColorToNodeId(cube_info,&pixel,index); |
| 854 | mid.red+=(id & 1) != 0 ? bisect : -bisect; |
| 855 | mid.green+=(id & 2) != 0 ? bisect : -bisect; |
| 856 | mid.blue+=(id & 4) != 0 ? bisect : -bisect; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 857 | mid.alpha+=(id & 8) != 0 ? bisect : -bisect; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 858 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 859 | { |
| 860 | /* |
| 861 | Set colors of new node to contain pixel. |
| 862 | */ |
| 863 | node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info); |
| 864 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 865 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 866 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 867 | image->filename); |
| 868 | if (level == MaxTreeDepth) |
| 869 | cube_info->colors++; |
| 870 | } |
| 871 | /* |
| 872 | Approximate the quantization error represented by this node. |
| 873 | */ |
| 874 | node_info=node_info->child[id]; |
| 875 | error.red=QuantumScale*(pixel.red-mid.red); |
| 876 | error.green=QuantumScale*(pixel.green-mid.green); |
| 877 | error.blue=QuantumScale*(pixel.blue-mid.blue); |
| 878 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 879 | error.alpha=QuantumScale*(pixel.alpha-mid.alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 880 | node_info->quantize_error+=sqrt((double) (count*error.red*error.red+ |
| 881 | count*error.green*error.green+count*error.blue*error.blue+ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 882 | count*error.alpha*error.alpha)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 883 | cube_info->root->quantize_error+=node_info->quantize_error; |
| 884 | index--; |
| 885 | } |
| 886 | /* |
| 887 | Sum RGB for this leaf for later derivation of the mean cube color. |
| 888 | */ |
| 889 | node_info->number_unique+=count; |
| 890 | node_info->total_color.red+=count*QuantumScale*pixel.red; |
| 891 | node_info->total_color.green+=count*QuantumScale*pixel.green; |
| 892 | node_info->total_color.blue+=count*QuantumScale*pixel.blue; |
| 893 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 894 | node_info->total_color.alpha+=count*QuantumScale*pixel.alpha; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 895 | p+=count*GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 896 | } |
| 897 | if (cube_info->colors > cube_info->maximum_colors) |
| 898 | { |
| 899 | PruneToCubeDepth(image,cube_info,cube_info->root); |
| 900 | break; |
| 901 | } |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 902 | proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y, |
| 903 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 904 | if (proceed == MagickFalse) |
| 905 | break; |
| 906 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 907 | for (y++; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 908 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 909 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 910 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 911 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 912 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 913 | x; |
| 914 | |
| 915 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 916 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 917 | break; |
| 918 | if (cube_info->nodes > MaxNodes) |
| 919 | { |
| 920 | /* |
| 921 | Prune one level if the color tree is too large. |
| 922 | */ |
| 923 | PruneLevel(image,cube_info,cube_info->root); |
| 924 | cube_info->depth--; |
| 925 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 926 | for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 927 | { |
| 928 | /* |
| 929 | Start at the root and descend the color cube tree. |
| 930 | */ |
cristy | bb66d9c | 2010-10-09 01:40:31 +0000 | [diff] [blame] | 931 | for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 932 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 933 | PixelInfo |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 934 | packet; |
| 935 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 936 | GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 937 | if (IsPixelEquivalent(image,p,&packet) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 938 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 939 | } |
| 940 | AssociateAlphaPixel(image,cube_info,p,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 941 | index=MaxTreeDepth-1; |
| 942 | bisect=((MagickRealType) QuantumRange+1.0)/2.0; |
| 943 | mid=midpoint; |
| 944 | node_info=cube_info->root; |
| 945 | for (level=1; level <= cube_info->depth; level++) |
| 946 | { |
| 947 | bisect*=0.5; |
| 948 | id=ColorToNodeId(cube_info,&pixel,index); |
| 949 | mid.red+=(id & 1) != 0 ? bisect : -bisect; |
| 950 | mid.green+=(id & 2) != 0 ? bisect : -bisect; |
| 951 | mid.blue+=(id & 4) != 0 ? bisect : -bisect; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 952 | mid.alpha+=(id & 8) != 0 ? bisect : -bisect; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 953 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 954 | { |
| 955 | /* |
| 956 | Set colors of new node to contain pixel. |
| 957 | */ |
| 958 | node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info); |
| 959 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 960 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 961 | ResourceLimitError,"MemoryAllocationFailed","%s", |
| 962 | image->filename); |
| 963 | if (level == cube_info->depth) |
| 964 | cube_info->colors++; |
| 965 | } |
| 966 | /* |
| 967 | Approximate the quantization error represented by this node. |
| 968 | */ |
| 969 | node_info=node_info->child[id]; |
| 970 | error.red=QuantumScale*(pixel.red-mid.red); |
| 971 | error.green=QuantumScale*(pixel.green-mid.green); |
| 972 | error.blue=QuantumScale*(pixel.blue-mid.blue); |
| 973 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 974 | error.alpha=QuantumScale*(pixel.alpha-mid.alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 975 | node_info->quantize_error+=sqrt((double) (count*error.red*error.red+ |
cristy | 83b6e79 | 2011-01-26 15:46:06 +0000 | [diff] [blame] | 976 | count*error.green*error.green+count*error.blue*error.blue+ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 977 | count*error.alpha*error.alpha)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 978 | cube_info->root->quantize_error+=node_info->quantize_error; |
| 979 | index--; |
| 980 | } |
| 981 | /* |
| 982 | Sum RGB for this leaf for later derivation of the mean cube color. |
| 983 | */ |
| 984 | node_info->number_unique+=count; |
| 985 | node_info->total_color.red+=count*QuantumScale*pixel.red; |
| 986 | node_info->total_color.green+=count*QuantumScale*pixel.green; |
| 987 | node_info->total_color.blue+=count*QuantumScale*pixel.blue; |
| 988 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 989 | node_info->total_color.alpha+=count*QuantumScale*pixel.alpha; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 990 | p+=count*GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 991 | } |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 992 | proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y, |
| 993 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 994 | if (proceed == MagickFalse) |
| 995 | break; |
| 996 | } |
| 997 | image_view=DestroyCacheView(image_view); |
| 998 | if ((cube_info->quantize_info->colorspace != UndefinedColorspace) && |
| 999 | (cube_info->quantize_info->colorspace != CMYKColorspace)) |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 1000 | (void) TransformImageColorspace((Image *) image,RGBColorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1001 | return(MagickTrue); |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1006 | % % |
| 1007 | % % |
| 1008 | % % |
| 1009 | % C l o n e Q u a n t i z e I n f o % |
| 1010 | % % |
| 1011 | % % |
| 1012 | % % |
| 1013 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1014 | % |
| 1015 | % CloneQuantizeInfo() makes a duplicate of the given quantize info structure, |
| 1016 | % or if quantize info is NULL, a new one. |
| 1017 | % |
| 1018 | % The format of the CloneQuantizeInfo method is: |
| 1019 | % |
| 1020 | % QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info) |
| 1021 | % |
| 1022 | % A description of each parameter follows: |
| 1023 | % |
| 1024 | % o clone_info: Method CloneQuantizeInfo returns a duplicate of the given |
| 1025 | % quantize info, or if image info is NULL a new one. |
| 1026 | % |
| 1027 | % o quantize_info: a structure of type info. |
| 1028 | % |
| 1029 | */ |
| 1030 | MagickExport QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info) |
| 1031 | { |
| 1032 | QuantizeInfo |
| 1033 | *clone_info; |
| 1034 | |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 1035 | clone_info=(QuantizeInfo *) AcquireMagickMemory(sizeof(*clone_info)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1036 | if (clone_info == (QuantizeInfo *) NULL) |
| 1037 | ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed"); |
| 1038 | GetQuantizeInfo(clone_info); |
| 1039 | if (quantize_info == (QuantizeInfo *) NULL) |
| 1040 | return(clone_info); |
| 1041 | clone_info->number_colors=quantize_info->number_colors; |
| 1042 | clone_info->tree_depth=quantize_info->tree_depth; |
| 1043 | clone_info->dither=quantize_info->dither; |
| 1044 | clone_info->dither_method=quantize_info->dither_method; |
| 1045 | clone_info->colorspace=quantize_info->colorspace; |
| 1046 | clone_info->measure_error=quantize_info->measure_error; |
| 1047 | return(clone_info); |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1052 | % % |
| 1053 | % % |
| 1054 | % % |
| 1055 | + C l o s e s t C o l o r % |
| 1056 | % % |
| 1057 | % % |
| 1058 | % % |
| 1059 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1060 | % |
| 1061 | % ClosestColor() traverses the color cube tree at a particular node and |
| 1062 | % determines which colormap entry best represents the input color. |
| 1063 | % |
| 1064 | % The format of the ClosestColor method is: |
| 1065 | % |
| 1066 | % void ClosestColor(const Image *image,CubeInfo *cube_info, |
| 1067 | % const NodeInfo *node_info) |
| 1068 | % |
| 1069 | % A description of each parameter follows. |
| 1070 | % |
| 1071 | % o image: the image. |
| 1072 | % |
| 1073 | % o cube_info: A pointer to the Cube structure. |
| 1074 | % |
| 1075 | % o node_info: the address of a structure of type NodeInfo which points to a |
| 1076 | % node in the color cube tree that is to be pruned. |
| 1077 | % |
| 1078 | */ |
| 1079 | static void ClosestColor(const Image *image,CubeInfo *cube_info, |
| 1080 | const NodeInfo *node_info) |
| 1081 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1082 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1083 | i; |
| 1084 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1085 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1086 | number_children; |
| 1087 | |
| 1088 | /* |
| 1089 | Traverse any children. |
| 1090 | */ |
| 1091 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1092 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1093 | if (node_info->child[i] != (NodeInfo *) NULL) |
| 1094 | ClosestColor(image,cube_info,node_info->child[i]); |
| 1095 | if (node_info->number_unique != 0) |
| 1096 | { |
| 1097 | MagickRealType |
| 1098 | pixel; |
| 1099 | |
| 1100 | register MagickRealType |
| 1101 | alpha, |
| 1102 | beta, |
| 1103 | distance; |
| 1104 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1105 | register PixelInfo |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1106 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1107 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1108 | register RealPixelInfo |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1109 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1110 | |
| 1111 | /* |
| 1112 | Determine if this color is "closest". |
| 1113 | */ |
| 1114 | p=image->colormap+node_info->color_number; |
| 1115 | q=(&cube_info->target); |
| 1116 | alpha=1.0; |
| 1117 | beta=1.0; |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 1118 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1119 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1120 | alpha=(MagickRealType) (QuantumScale*p->alpha); |
| 1121 | beta=(MagickRealType) (QuantumScale*q->alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1122 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1123 | pixel=alpha*p->red-beta*q->red; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1124 | distance=pixel*pixel; |
cristy | 36fbc3b | 2011-02-09 02:30:04 +0000 | [diff] [blame] | 1125 | if (distance <= cube_info->distance) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1126 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1127 | pixel=alpha*p->green-beta*q->green; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1128 | distance+=pixel*pixel; |
cristy | 36fbc3b | 2011-02-09 02:30:04 +0000 | [diff] [blame] | 1129 | if (distance <= cube_info->distance) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1130 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1131 | pixel=alpha*p->blue-beta*q->blue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1132 | distance+=pixel*pixel; |
cristy | 36fbc3b | 2011-02-09 02:30:04 +0000 | [diff] [blame] | 1133 | if (distance <= cube_info->distance) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1134 | { |
| 1135 | pixel=alpha-beta; |
| 1136 | distance+=pixel*pixel; |
cristy | c408040 | 2011-02-09 02:55:58 +0000 | [diff] [blame] | 1137 | if (distance <= cube_info->distance) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1138 | { |
| 1139 | cube_info->distance=distance; |
| 1140 | cube_info->color_number=node_info->color_number; |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | /* |
| 1149 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1150 | % % |
| 1151 | % % |
| 1152 | % % |
| 1153 | % C o m p r e s s I m a g e C o l o r m a p % |
| 1154 | % % |
| 1155 | % % |
| 1156 | % % |
| 1157 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1158 | % |
| 1159 | % CompressImageColormap() compresses an image colormap by removing any |
| 1160 | % duplicate or unused color entries. |
| 1161 | % |
| 1162 | % The format of the CompressImageColormap method is: |
| 1163 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1164 | % MagickBooleanType CompressImageColormap(Image *image, |
| 1165 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1166 | % |
| 1167 | % A description of each parameter follows: |
| 1168 | % |
| 1169 | % o image: the image. |
| 1170 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1171 | % o exception: return any errors or warnings in this structure. |
| 1172 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1173 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1174 | MagickExport MagickBooleanType CompressImageColormap(Image *image, |
| 1175 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1176 | { |
| 1177 | QuantizeInfo |
| 1178 | quantize_info; |
| 1179 | |
| 1180 | assert(image != (Image *) NULL); |
| 1181 | assert(image->signature == MagickSignature); |
| 1182 | if (image->debug != MagickFalse) |
| 1183 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1184 | if (IsPaletteImage(image,&image->exception) == MagickFalse) |
| 1185 | return(MagickFalse); |
| 1186 | GetQuantizeInfo(&quantize_info); |
| 1187 | quantize_info.number_colors=image->colors; |
| 1188 | quantize_info.tree_depth=MaxTreeDepth; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1189 | return(QuantizeImage(&quantize_info,image,exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | /* |
| 1193 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1194 | % % |
| 1195 | % % |
| 1196 | % % |
| 1197 | + D e f i n e I m a g e C o l o r m a p % |
| 1198 | % % |
| 1199 | % % |
| 1200 | % % |
| 1201 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1202 | % |
| 1203 | % DefineImageColormap() traverses the color cube tree and notes each colormap |
| 1204 | % entry. A colormap entry is any node in the color cube tree where the |
| 1205 | % of unique colors is not zero. DefineImageColormap() returns the number of |
| 1206 | % colors in the image colormap. |
| 1207 | % |
| 1208 | % The format of the DefineImageColormap method is: |
| 1209 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1210 | % size_t DefineImageColormap(Image *image,CubeInfo *cube_info, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1211 | % NodeInfo *node_info) |
| 1212 | % |
| 1213 | % A description of each parameter follows. |
| 1214 | % |
| 1215 | % o image: the image. |
| 1216 | % |
| 1217 | % o cube_info: A pointer to the Cube structure. |
| 1218 | % |
| 1219 | % o node_info: the address of a structure of type NodeInfo which points to a |
| 1220 | % node in the color cube tree that is to be pruned. |
| 1221 | % |
| 1222 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1223 | static size_t DefineImageColormap(Image *image,CubeInfo *cube_info, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1224 | NodeInfo *node_info) |
| 1225 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1226 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1227 | i; |
| 1228 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1229 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1230 | number_children; |
| 1231 | |
| 1232 | /* |
| 1233 | Traverse any children. |
| 1234 | */ |
| 1235 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1236 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1237 | if (node_info->child[i] != (NodeInfo *) NULL) |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1238 | (void) DefineImageColormap(image,cube_info,node_info->child[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1239 | if (node_info->number_unique != 0) |
| 1240 | { |
| 1241 | register MagickRealType |
| 1242 | alpha; |
| 1243 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1244 | register PixelInfo |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1245 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1246 | |
| 1247 | /* |
| 1248 | Colormap entry is defined by the mean color in this cube. |
| 1249 | */ |
| 1250 | q=image->colormap+image->colors; |
| 1251 | alpha=(MagickRealType) ((MagickOffsetType) node_info->number_unique); |
| 1252 | alpha=1.0/(fabs(alpha) <= MagickEpsilon ? 1.0 : alpha); |
| 1253 | if (cube_info->associate_alpha == MagickFalse) |
| 1254 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1255 | q->red=ClampToQuantum((MagickRealType) |
| 1256 | (alpha*QuantumRange*node_info->total_color.red)); |
| 1257 | q->green=ClampToQuantum((MagickRealType) |
| 1258 | (alpha*QuantumRange*node_info->total_color.green)); |
| 1259 | q->blue=ClampToQuantum((MagickRealType) |
| 1260 | (alpha*QuantumRange*node_info->total_color.blue)); |
| 1261 | q->alpha=OpaqueAlpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1262 | } |
| 1263 | else |
| 1264 | { |
| 1265 | MagickRealType |
| 1266 | opacity; |
| 1267 | |
| 1268 | opacity=(MagickRealType) (alpha*QuantumRange* |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1269 | node_info->total_color.alpha); |
| 1270 | q->alpha=ClampToQuantum(opacity); |
| 1271 | if (q->alpha == OpaqueAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1272 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1273 | q->red=ClampToQuantum((MagickRealType) |
| 1274 | (alpha*QuantumRange*node_info->total_color.red)); |
| 1275 | q->green=ClampToQuantum((MagickRealType) |
| 1276 | (alpha*QuantumRange*node_info->total_color.green)); |
| 1277 | q->blue=ClampToQuantum((MagickRealType) |
| 1278 | (alpha*QuantumRange*node_info->total_color.blue)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | MagickRealType |
| 1283 | gamma; |
| 1284 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1285 | gamma=(MagickRealType) (QuantumScale*q->alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1286 | gamma=1.0/(fabs(gamma) <= MagickEpsilon ? 1.0 : gamma); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1287 | q->red=ClampToQuantum((MagickRealType) |
| 1288 | (alpha*gamma*QuantumRange*node_info->total_color.red)); |
| 1289 | q->green=ClampToQuantum((MagickRealType) |
| 1290 | (alpha*gamma*QuantumRange*node_info->total_color.green)); |
| 1291 | q->blue=ClampToQuantum((MagickRealType) |
| 1292 | (alpha*gamma*QuantumRange*node_info->total_color.blue)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1293 | if (node_info->number_unique > cube_info->transparent_pixels) |
| 1294 | { |
| 1295 | cube_info->transparent_pixels=node_info->number_unique; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1296 | cube_info->transparent_index=(ssize_t) image->colors; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | node_info->color_number=image->colors++; |
| 1301 | } |
| 1302 | return(image->colors); |
| 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1307 | % % |
| 1308 | % % |
| 1309 | % % |
| 1310 | + D e s t r o y C u b e I n f o % |
| 1311 | % % |
| 1312 | % % |
| 1313 | % % |
| 1314 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1315 | % |
| 1316 | % DestroyCubeInfo() deallocates memory associated with an image. |
| 1317 | % |
| 1318 | % The format of the DestroyCubeInfo method is: |
| 1319 | % |
| 1320 | % DestroyCubeInfo(CubeInfo *cube_info) |
| 1321 | % |
| 1322 | % A description of each parameter follows: |
| 1323 | % |
| 1324 | % o cube_info: the address of a structure of type CubeInfo. |
| 1325 | % |
| 1326 | */ |
| 1327 | static void DestroyCubeInfo(CubeInfo *cube_info) |
| 1328 | { |
| 1329 | register Nodes |
| 1330 | *nodes; |
| 1331 | |
| 1332 | /* |
| 1333 | Release color cube tree storage. |
| 1334 | */ |
| 1335 | do |
| 1336 | { |
| 1337 | nodes=cube_info->node_queue->next; |
| 1338 | cube_info->node_queue->nodes=(NodeInfo *) RelinquishMagickMemory( |
| 1339 | cube_info->node_queue->nodes); |
| 1340 | cube_info->node_queue=(Nodes *) RelinquishMagickMemory( |
| 1341 | cube_info->node_queue); |
| 1342 | cube_info->node_queue=nodes; |
| 1343 | } while (cube_info->node_queue != (Nodes *) NULL); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1344 | if (cube_info->cache != (ssize_t *) NULL) |
| 1345 | cube_info->cache=(ssize_t *) RelinquishMagickMemory(cube_info->cache); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1346 | cube_info->quantize_info=DestroyQuantizeInfo(cube_info->quantize_info); |
| 1347 | cube_info=(CubeInfo *) RelinquishMagickMemory(cube_info); |
| 1348 | } |
| 1349 | |
| 1350 | /* |
| 1351 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1352 | % % |
| 1353 | % % |
| 1354 | % % |
| 1355 | % D e s t r o y Q u a n t i z e I n f o % |
| 1356 | % % |
| 1357 | % % |
| 1358 | % % |
| 1359 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1360 | % |
| 1361 | % DestroyQuantizeInfo() deallocates memory associated with an QuantizeInfo |
| 1362 | % structure. |
| 1363 | % |
| 1364 | % The format of the DestroyQuantizeInfo method is: |
| 1365 | % |
| 1366 | % QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info) |
| 1367 | % |
| 1368 | % A description of each parameter follows: |
| 1369 | % |
| 1370 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 1371 | % |
| 1372 | */ |
| 1373 | MagickExport QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info) |
| 1374 | { |
| 1375 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| 1376 | assert(quantize_info != (QuantizeInfo *) NULL); |
| 1377 | assert(quantize_info->signature == MagickSignature); |
| 1378 | quantize_info->signature=(~MagickSignature); |
| 1379 | quantize_info=(QuantizeInfo *) RelinquishMagickMemory(quantize_info); |
| 1380 | return(quantize_info); |
| 1381 | } |
| 1382 | |
| 1383 | /* |
| 1384 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1385 | % % |
| 1386 | % % |
| 1387 | % % |
| 1388 | + D i t h e r I m a g e % |
| 1389 | % % |
| 1390 | % % |
| 1391 | % % |
| 1392 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1393 | % |
| 1394 | % DitherImage() distributes the difference between an original image and |
| 1395 | % the corresponding color reduced algorithm to neighboring pixels using |
| 1396 | % serpentine-scan Floyd-Steinberg error diffusion. DitherImage returns |
| 1397 | % MagickTrue if the image is dithered otherwise MagickFalse. |
| 1398 | % |
| 1399 | % The format of the DitherImage method is: |
| 1400 | % |
| 1401 | % MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info) |
| 1402 | % |
| 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 | % |
| 1409 | */ |
| 1410 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1411 | static RealPixelInfo **DestroyPixelThreadSet(RealPixelInfo **pixels) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1412 | { |
| 1413 | register ssize_t |
| 1414 | i; |
| 1415 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1416 | assert(pixels != (RealPixelInfo **) NULL); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1417 | for (i=0; i < (ssize_t) GetOpenMPMaximumThreads(); i++) |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1418 | if (pixels[i] != (RealPixelInfo *) NULL) |
| 1419 | pixels[i]=(RealPixelInfo *) RelinquishMagickMemory(pixels[i]); |
| 1420 | pixels=(RealPixelInfo **) RelinquishMagickMemory(pixels); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1421 | return(pixels); |
| 1422 | } |
| 1423 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1424 | static RealPixelInfo **AcquirePixelThreadSet(const size_t count) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1425 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1426 | RealPixelInfo |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1427 | **pixels; |
| 1428 | |
| 1429 | register ssize_t |
| 1430 | i; |
| 1431 | |
| 1432 | size_t |
| 1433 | number_threads; |
| 1434 | |
| 1435 | number_threads=GetOpenMPMaximumThreads(); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1436 | pixels=(RealPixelInfo **) AcquireQuantumMemory(number_threads, |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1437 | sizeof(*pixels)); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1438 | if (pixels == (RealPixelInfo **) NULL) |
| 1439 | return((RealPixelInfo **) NULL); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1440 | (void) ResetMagickMemory(pixels,0,number_threads*sizeof(*pixels)); |
| 1441 | for (i=0; i < (ssize_t) number_threads; i++) |
| 1442 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1443 | pixels[i]=(RealPixelInfo *) AcquireQuantumMemory(count, |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1444 | 2*sizeof(**pixels)); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1445 | if (pixels[i] == (RealPixelInfo *) NULL) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1446 | return(DestroyPixelThreadSet(pixels)); |
| 1447 | } |
| 1448 | return(pixels); |
| 1449 | } |
| 1450 | |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1451 | static inline ssize_t CacheOffset(CubeInfo *cube_info, |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1452 | const RealPixelInfo *pixel) |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1453 | { |
| 1454 | #define RedShift(pixel) (((pixel) >> CacheShift) << (0*(8-CacheShift))) |
| 1455 | #define GreenShift(pixel) (((pixel) >> CacheShift) << (1*(8-CacheShift))) |
| 1456 | #define BlueShift(pixel) (((pixel) >> CacheShift) << (2*(8-CacheShift))) |
| 1457 | #define AlphaShift(pixel) (((pixel) >> CacheShift) << (3*(8-CacheShift))) |
| 1458 | |
| 1459 | ssize_t |
| 1460 | offset; |
| 1461 | |
| 1462 | offset=(ssize_t) |
cristy | 15893a4 | 2010-11-20 18:57:15 +0000 | [diff] [blame] | 1463 | (RedShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->red))) | |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1464 | GreenShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->green))) | |
cristy | 15893a4 | 2010-11-20 18:57:15 +0000 | [diff] [blame] | 1465 | BlueShift(ScaleQuantumToChar(ClampToUnsignedQuantum(pixel->blue)))); |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1466 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 15893a4 | 2010-11-20 18:57:15 +0000 | [diff] [blame] | 1467 | offset|=AlphaShift(ScaleQuantumToChar(ClampToUnsignedQuantum( |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1468 | pixel->alpha))); |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1469 | return(offset); |
| 1470 | } |
| 1471 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1472 | static MagickBooleanType FloydSteinbergDither(Image *image,CubeInfo *cube_info) |
| 1473 | { |
| 1474 | #define DitherImageTag "Dither/Image" |
| 1475 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1476 | CacheView |
| 1477 | *image_view; |
| 1478 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1479 | ExceptionInfo |
| 1480 | *exception; |
| 1481 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1482 | MagickBooleanType |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1483 | status; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1484 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1485 | RealPixelInfo |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1486 | **pixels; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1487 | |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 1488 | ssize_t |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 1489 | y; |
| 1490 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1491 | /* |
| 1492 | Distribute quantization error using Floyd-Steinberg. |
| 1493 | */ |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1494 | pixels=AcquirePixelThreadSet(image->columns); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1495 | if (pixels == (RealPixelInfo **) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1496 | return(MagickFalse); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1497 | exception=(&image->exception); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1498 | status=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1499 | image_view=AcquireCacheView(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1500 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1501 | { |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1502 | const int |
| 1503 | id = GetOpenMPThreadId(); |
| 1504 | |
| 1505 | CubeInfo |
| 1506 | cube; |
| 1507 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1508 | RealPixelInfo |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1509 | *current, |
| 1510 | *previous; |
| 1511 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1512 | register Quantum |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 1513 | *restrict q; |
| 1514 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1515 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1516 | x; |
| 1517 | |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1518 | size_t |
| 1519 | index; |
| 1520 | |
| 1521 | ssize_t |
| 1522 | v; |
| 1523 | |
| 1524 | if (status == MagickFalse) |
| 1525 | continue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1526 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1527 | if (q == (Quantum *) NULL) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1528 | { |
| 1529 | status=MagickFalse; |
cristy | 00cbdd6 | 2011-02-20 17:29:26 +0000 | [diff] [blame] | 1530 | continue; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1531 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1532 | q+=(y & 0x01)*image->columns*GetPixelChannels(image); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1533 | cube=(*cube_info); |
| 1534 | current=pixels[id]+(y & 0x01)*image->columns; |
| 1535 | previous=pixels[id]+((y+1) & 0x01)*image->columns; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1536 | v=(ssize_t) ((y & 0x01) != 0 ? -1 : 1); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1537 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1538 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1539 | RealPixelInfo |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1540 | color, |
| 1541 | pixel; |
| 1542 | |
| 1543 | register ssize_t |
| 1544 | i; |
| 1545 | |
| 1546 | ssize_t |
| 1547 | u; |
| 1548 | |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1549 | q-=(y & 0x01)*GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1550 | u=(y & 0x01) != 0 ? (ssize_t) image->columns-1-x : x; |
| 1551 | AssociateAlphaPixel(image,&cube,q,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1552 | if (x > 0) |
| 1553 | { |
| 1554 | pixel.red+=7*current[u-v].red/16; |
| 1555 | pixel.green+=7*current[u-v].green/16; |
| 1556 | pixel.blue+=7*current[u-v].blue/16; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1557 | if (cube.associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1558 | pixel.alpha+=7*current[u-v].alpha/16; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1559 | } |
| 1560 | if (y > 0) |
| 1561 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1562 | if (x < (ssize_t) (image->columns-1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1563 | { |
| 1564 | pixel.red+=previous[u+v].red/16; |
| 1565 | pixel.green+=previous[u+v].green/16; |
| 1566 | pixel.blue+=previous[u+v].blue/16; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1567 | if (cube.associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1568 | pixel.alpha+=previous[u+v].alpha/16; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1569 | } |
| 1570 | pixel.red+=5*previous[u].red/16; |
| 1571 | pixel.green+=5*previous[u].green/16; |
| 1572 | pixel.blue+=5*previous[u].blue/16; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1573 | if (cube.associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1574 | pixel.alpha+=5*previous[u].alpha/16; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1575 | if (x > 0) |
| 1576 | { |
| 1577 | pixel.red+=3*previous[u-v].red/16; |
| 1578 | pixel.green+=3*previous[u-v].green/16; |
| 1579 | pixel.blue+=3*previous[u-v].blue/16; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1580 | if (cube.associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1581 | pixel.alpha+=3*previous[u-v].alpha/16; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1582 | } |
| 1583 | } |
cristy | 75ffdb7 | 2010-01-07 17:40:12 +0000 | [diff] [blame] | 1584 | pixel.red=(MagickRealType) ClampToUnsignedQuantum(pixel.red); |
| 1585 | pixel.green=(MagickRealType) ClampToUnsignedQuantum(pixel.green); |
| 1586 | pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1587 | if (cube.associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1588 | pixel.alpha=(MagickRealType) ClampToUnsignedQuantum(pixel.alpha); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1589 | i=CacheOffset(&cube,&pixel); |
| 1590 | if (cube.cache[i] < 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1591 | { |
| 1592 | register NodeInfo |
| 1593 | *node_info; |
| 1594 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1595 | register size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1596 | id; |
| 1597 | |
| 1598 | /* |
| 1599 | Identify the deepest node containing the pixel's color. |
| 1600 | */ |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1601 | node_info=cube.root; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1602 | for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1603 | { |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1604 | id=ColorToNodeId(&cube,&pixel,index); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1605 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 1606 | break; |
| 1607 | node_info=node_info->child[id]; |
| 1608 | } |
| 1609 | /* |
| 1610 | Find closest color among siblings and their children. |
| 1611 | */ |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1612 | cube.target=pixel; |
| 1613 | cube.distance=(MagickRealType) (4.0*(QuantumRange+1.0)*(QuantumRange+ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1614 | 1.0)+1.0); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1615 | ClosestColor(image,&cube,node_info->parent); |
| 1616 | cube.cache[i]=(ssize_t) cube.color_number; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1617 | } |
| 1618 | /* |
| 1619 | Assign pixel to closest colormap entry. |
| 1620 | */ |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1621 | index=(size_t) cube.cache[i]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1622 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1623 | SetPixelIndex(image,(Quantum) index,q); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1624 | if (cube.quantize_info->measure_error == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1625 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1626 | SetPixelRed(image,image->colormap[index].red,q); |
| 1627 | SetPixelGreen(image,image->colormap[index].green,q); |
| 1628 | SetPixelBlue(image,image->colormap[index].blue,q); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1629 | if (cube.associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1630 | SetPixelAlpha(image,image->colormap[index].alpha,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1631 | } |
| 1632 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1633 | status=MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1634 | /* |
| 1635 | Store the error. |
| 1636 | */ |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1637 | AssociateAlphaPixelInfo(image,&cube,image->colormap+index,&color); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1638 | current[u].red=pixel.red-color.red; |
| 1639 | current[u].green=pixel.green-color.green; |
| 1640 | current[u].blue=pixel.blue-color.blue; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1641 | if (cube.associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1642 | current[u].alpha=pixel.alpha-color.alpha; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1643 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1644 | { |
| 1645 | MagickBooleanType |
| 1646 | proceed; |
| 1647 | |
| 1648 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1649 | #pragma omp critical (MagickCore_FloydSteinbergDither) |
| 1650 | #endif |
| 1651 | proceed=SetImageProgress(image,DitherImageTag,(MagickOffsetType) y, |
| 1652 | image->rows); |
| 1653 | if (proceed == MagickFalse) |
| 1654 | status=MagickFalse; |
| 1655 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1656 | q+=((y+1) & 0x01)*GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1657 | } |
| 1658 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1659 | image_view=DestroyCacheView(image_view); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1660 | pixels=DestroyPixelThreadSet(pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1661 | return(MagickTrue); |
| 1662 | } |
| 1663 | |
| 1664 | static MagickBooleanType |
| 1665 | RiemersmaDither(Image *,CacheView *,CubeInfo *,const unsigned int); |
| 1666 | |
| 1667 | static void Riemersma(Image *image,CacheView *image_view,CubeInfo *cube_info, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1668 | const size_t level,const unsigned int direction) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1669 | { |
| 1670 | if (level == 1) |
| 1671 | switch (direction) |
| 1672 | { |
| 1673 | case WestGravity: |
| 1674 | { |
| 1675 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity); |
| 1676 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity); |
| 1677 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity); |
| 1678 | break; |
| 1679 | } |
| 1680 | case EastGravity: |
| 1681 | { |
| 1682 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity); |
| 1683 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity); |
| 1684 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity); |
| 1685 | break; |
| 1686 | } |
| 1687 | case NorthGravity: |
| 1688 | { |
| 1689 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity); |
| 1690 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity); |
| 1691 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity); |
| 1692 | break; |
| 1693 | } |
| 1694 | case SouthGravity: |
| 1695 | { |
| 1696 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity); |
| 1697 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity); |
| 1698 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity); |
| 1699 | break; |
| 1700 | } |
| 1701 | default: |
| 1702 | break; |
| 1703 | } |
| 1704 | else |
| 1705 | switch (direction) |
| 1706 | { |
| 1707 | case WestGravity: |
| 1708 | { |
| 1709 | Riemersma(image,image_view,cube_info,level-1,NorthGravity); |
| 1710 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity); |
| 1711 | Riemersma(image,image_view,cube_info,level-1,WestGravity); |
| 1712 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity); |
| 1713 | Riemersma(image,image_view,cube_info,level-1,WestGravity); |
| 1714 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity); |
| 1715 | Riemersma(image,image_view,cube_info,level-1,SouthGravity); |
| 1716 | break; |
| 1717 | } |
| 1718 | case EastGravity: |
| 1719 | { |
| 1720 | Riemersma(image,image_view,cube_info,level-1,SouthGravity); |
| 1721 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity); |
| 1722 | Riemersma(image,image_view,cube_info,level-1,EastGravity); |
| 1723 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity); |
| 1724 | Riemersma(image,image_view,cube_info,level-1,EastGravity); |
| 1725 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity); |
| 1726 | Riemersma(image,image_view,cube_info,level-1,NorthGravity); |
| 1727 | break; |
| 1728 | } |
| 1729 | case NorthGravity: |
| 1730 | { |
| 1731 | Riemersma(image,image_view,cube_info,level-1,WestGravity); |
| 1732 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity); |
| 1733 | Riemersma(image,image_view,cube_info,level-1,NorthGravity); |
| 1734 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity); |
| 1735 | Riemersma(image,image_view,cube_info,level-1,NorthGravity); |
| 1736 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity); |
| 1737 | Riemersma(image,image_view,cube_info,level-1,EastGravity); |
| 1738 | break; |
| 1739 | } |
| 1740 | case SouthGravity: |
| 1741 | { |
| 1742 | Riemersma(image,image_view,cube_info,level-1,EastGravity); |
| 1743 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity); |
| 1744 | Riemersma(image,image_view,cube_info,level-1,SouthGravity); |
| 1745 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity); |
| 1746 | Riemersma(image,image_view,cube_info,level-1,SouthGravity); |
| 1747 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity); |
| 1748 | Riemersma(image,image_view,cube_info,level-1,WestGravity); |
| 1749 | break; |
| 1750 | } |
| 1751 | default: |
| 1752 | break; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | static MagickBooleanType RiemersmaDither(Image *image,CacheView *image_view, |
| 1757 | CubeInfo *cube_info,const unsigned int direction) |
| 1758 | { |
| 1759 | #define DitherImageTag "Dither/Image" |
| 1760 | |
| 1761 | MagickBooleanType |
| 1762 | proceed; |
| 1763 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1764 | RealPixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1765 | color, |
| 1766 | pixel; |
| 1767 | |
| 1768 | register CubeInfo |
| 1769 | *p; |
| 1770 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1771 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1772 | index; |
| 1773 | |
| 1774 | p=cube_info; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1775 | if ((p->x >= 0) && (p->x < (ssize_t) image->columns) && |
| 1776 | (p->y >= 0) && (p->y < (ssize_t) image->rows)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1777 | { |
| 1778 | ExceptionInfo |
| 1779 | *exception; |
| 1780 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1781 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 1782 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1783 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 1784 | register ssize_t |
| 1785 | i; |
| 1786 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1787 | /* |
| 1788 | Distribute error. |
| 1789 | */ |
| 1790 | exception=(&image->exception); |
| 1791 | q=GetCacheViewAuthenticPixels(image_view,p->x,p->y,1,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1792 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1793 | return(MagickFalse); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1794 | AssociateAlphaPixel(image,cube_info,q,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1795 | for (i=0; i < ErrorQueueLength; i++) |
| 1796 | { |
| 1797 | pixel.red+=p->weights[i]*p->error[i].red; |
| 1798 | pixel.green+=p->weights[i]*p->error[i].green; |
| 1799 | pixel.blue+=p->weights[i]*p->error[i].blue; |
| 1800 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1801 | pixel.alpha+=p->weights[i]*p->error[i].alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1802 | } |
cristy | 75ffdb7 | 2010-01-07 17:40:12 +0000 | [diff] [blame] | 1803 | pixel.red=(MagickRealType) ClampToUnsignedQuantum(pixel.red); |
| 1804 | pixel.green=(MagickRealType) ClampToUnsignedQuantum(pixel.green); |
| 1805 | pixel.blue=(MagickRealType) ClampToUnsignedQuantum(pixel.blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1806 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1807 | pixel.alpha=(MagickRealType) ClampToUnsignedQuantum(pixel.alpha); |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1808 | i=CacheOffset(cube_info,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1809 | if (p->cache[i] < 0) |
| 1810 | { |
| 1811 | register NodeInfo |
| 1812 | *node_info; |
| 1813 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1814 | register size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1815 | id; |
| 1816 | |
| 1817 | /* |
| 1818 | Identify the deepest node containing the pixel's color. |
| 1819 | */ |
| 1820 | node_info=p->root; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1821 | for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1822 | { |
| 1823 | id=ColorToNodeId(cube_info,&pixel,index); |
| 1824 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 1825 | break; |
| 1826 | node_info=node_info->child[id]; |
| 1827 | } |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 1828 | node_info=node_info->parent; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1829 | /* |
| 1830 | Find closest color among siblings and their children. |
| 1831 | */ |
| 1832 | p->target=pixel; |
| 1833 | p->distance=(MagickRealType) (4.0*(QuantumRange+1.0)*((MagickRealType) |
| 1834 | QuantumRange+1.0)+1.0); |
| 1835 | ClosestColor(image,p,node_info->parent); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1836 | p->cache[i]=(ssize_t) p->color_number; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1837 | } |
| 1838 | /* |
| 1839 | Assign pixel to closest colormap entry. |
| 1840 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1841 | index=(size_t) p->cache[i]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1842 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1843 | SetPixelIndex(image,(Quantum) index,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1844 | if (cube_info->quantize_info->measure_error == MagickFalse) |
| 1845 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1846 | SetPixelRed(image,image->colormap[index].red,q); |
| 1847 | SetPixelGreen(image,image->colormap[index].green,q); |
| 1848 | SetPixelBlue(image,image->colormap[index].blue,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1849 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1850 | SetPixelAlpha(image,image->colormap[index].alpha,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1851 | } |
| 1852 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1853 | return(MagickFalse); |
| 1854 | /* |
| 1855 | Propagate the error as the last entry of the error queue. |
| 1856 | */ |
| 1857 | (void) CopyMagickMemory(p->error,p->error+1,(ErrorQueueLength-1)* |
| 1858 | sizeof(p->error[0])); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 1859 | AssociateAlphaPixelInfo(image,cube_info,image->colormap+index,&color); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1860 | p->error[ErrorQueueLength-1].red=pixel.red-color.red; |
| 1861 | p->error[ErrorQueueLength-1].green=pixel.green-color.green; |
| 1862 | p->error[ErrorQueueLength-1].blue=pixel.blue-color.blue; |
| 1863 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1864 | p->error[ErrorQueueLength-1].alpha=pixel.alpha-color.alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1865 | proceed=SetImageProgress(image,DitherImageTag,p->offset,p->span); |
| 1866 | if (proceed == MagickFalse) |
| 1867 | return(MagickFalse); |
| 1868 | p->offset++; |
| 1869 | } |
| 1870 | switch (direction) |
| 1871 | { |
| 1872 | case WestGravity: p->x--; break; |
| 1873 | case EastGravity: p->x++; break; |
| 1874 | case NorthGravity: p->y--; break; |
| 1875 | case SouthGravity: p->y++; break; |
| 1876 | } |
| 1877 | return(MagickTrue); |
| 1878 | } |
| 1879 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1880 | static inline ssize_t MagickMax(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1881 | { |
| 1882 | if (x > y) |
| 1883 | return(x); |
| 1884 | return(y); |
| 1885 | } |
| 1886 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1887 | static inline ssize_t MagickMin(const ssize_t x,const ssize_t y) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1888 | { |
| 1889 | if (x < y) |
| 1890 | return(x); |
| 1891 | return(y); |
| 1892 | } |
| 1893 | |
| 1894 | static MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info) |
| 1895 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1896 | CacheView |
| 1897 | *image_view; |
| 1898 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1899 | MagickBooleanType |
| 1900 | status; |
| 1901 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1902 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1903 | i; |
| 1904 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1905 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1906 | depth; |
| 1907 | |
cristy | fb7e9cd | 2011-02-20 16:26:15 +0000 | [diff] [blame] | 1908 | if (cube_info->quantize_info->dither_method != RiemersmaDitherMethod) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1909 | return(FloydSteinbergDither(image,cube_info)); |
| 1910 | /* |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1911 | Distribute quantization error along a Hilbert curve. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1912 | */ |
| 1913 | (void) ResetMagickMemory(cube_info->error,0,ErrorQueueLength* |
| 1914 | sizeof(*cube_info->error)); |
| 1915 | cube_info->x=0; |
| 1916 | cube_info->y=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1917 | i=MagickMax((ssize_t) image->columns,(ssize_t) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1918 | for (depth=1; i != 0; depth++) |
| 1919 | i>>=1; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1920 | if ((ssize_t) (1L << depth) < MagickMax((ssize_t) image->columns,(ssize_t) image->rows)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1921 | depth++; |
| 1922 | cube_info->offset=0; |
| 1923 | cube_info->span=(MagickSizeType) image->columns*image->rows; |
| 1924 | image_view=AcquireCacheView(image); |
| 1925 | if (depth > 1) |
| 1926 | Riemersma(image,image_view,cube_info,depth-1,NorthGravity); |
| 1927 | status=RiemersmaDither(image,image_view,cube_info,ForgetGravity); |
| 1928 | image_view=DestroyCacheView(image_view); |
| 1929 | return(status); |
| 1930 | } |
| 1931 | |
| 1932 | /* |
| 1933 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1934 | % % |
| 1935 | % % |
| 1936 | % % |
| 1937 | + G e t C u b e I n f o % |
| 1938 | % % |
| 1939 | % % |
| 1940 | % % |
| 1941 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1942 | % |
| 1943 | % GetCubeInfo() initialize the Cube data structure. |
| 1944 | % |
| 1945 | % The format of the GetCubeInfo method is: |
| 1946 | % |
| 1947 | % CubeInfo GetCubeInfo(const QuantizeInfo *quantize_info, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1948 | % const size_t depth,const size_t maximum_colors) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1949 | % |
| 1950 | % A description of each parameter follows. |
| 1951 | % |
| 1952 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 1953 | % |
| 1954 | % o depth: Normally, this integer value is zero or one. A zero or |
| 1955 | % one tells Quantize to choose a optimal tree depth of Log4(number_colors). |
| 1956 | % A tree of this depth generally allows the best representation of the |
| 1957 | % reference image with the least amount of memory and the fastest |
| 1958 | % computational speed. In some cases, such as an image with low color |
| 1959 | % dispersion (a few number of colors), a value other than |
| 1960 | % Log4(number_colors) is required. To expand the color tree completely, |
| 1961 | % use a value of 8. |
| 1962 | % |
| 1963 | % o maximum_colors: maximum colors. |
| 1964 | % |
| 1965 | */ |
| 1966 | static CubeInfo *GetCubeInfo(const QuantizeInfo *quantize_info, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1967 | const size_t depth,const size_t maximum_colors) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1968 | { |
| 1969 | CubeInfo |
| 1970 | *cube_info; |
| 1971 | |
| 1972 | MagickRealType |
| 1973 | sum, |
| 1974 | weight; |
| 1975 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1976 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1977 | i; |
| 1978 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 1979 | size_t |
| 1980 | length; |
| 1981 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1982 | /* |
| 1983 | Initialize tree to describe color cube_info. |
| 1984 | */ |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 1985 | cube_info=(CubeInfo *) AcquireMagickMemory(sizeof(*cube_info)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1986 | if (cube_info == (CubeInfo *) NULL) |
| 1987 | return((CubeInfo *) NULL); |
| 1988 | (void) ResetMagickMemory(cube_info,0,sizeof(*cube_info)); |
| 1989 | cube_info->depth=depth; |
| 1990 | if (cube_info->depth > MaxTreeDepth) |
| 1991 | cube_info->depth=MaxTreeDepth; |
| 1992 | if (cube_info->depth < 2) |
| 1993 | cube_info->depth=2; |
| 1994 | cube_info->maximum_colors=maximum_colors; |
| 1995 | /* |
| 1996 | Initialize root node. |
| 1997 | */ |
| 1998 | cube_info->root=GetNodeInfo(cube_info,0,0,(NodeInfo *) NULL); |
| 1999 | if (cube_info->root == (NodeInfo *) NULL) |
| 2000 | return((CubeInfo *) NULL); |
| 2001 | cube_info->root->parent=cube_info->root; |
| 2002 | cube_info->quantize_info=CloneQuantizeInfo(quantize_info); |
| 2003 | if (cube_info->quantize_info->dither == MagickFalse) |
| 2004 | return(cube_info); |
| 2005 | /* |
| 2006 | Initialize dither resources. |
| 2007 | */ |
| 2008 | length=(size_t) (1UL << (4*(8-CacheShift))); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2009 | cube_info->cache=(ssize_t *) AcquireQuantumMemory(length, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2010 | sizeof(*cube_info->cache)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2011 | if (cube_info->cache == (ssize_t *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2012 | return((CubeInfo *) NULL); |
| 2013 | /* |
| 2014 | Initialize color cache. |
| 2015 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2016 | for (i=0; i < (ssize_t) length; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2017 | cube_info->cache[i]=(-1); |
| 2018 | /* |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 2019 | Distribute weights along a curve of exponential decay. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2020 | */ |
| 2021 | weight=1.0; |
| 2022 | for (i=0; i < ErrorQueueLength; i++) |
| 2023 | { |
| 2024 | cube_info->weights[ErrorQueueLength-i-1]=1.0/weight; |
| 2025 | weight*=exp(log(((double) QuantumRange+1.0))/(ErrorQueueLength-1.0)); |
| 2026 | } |
| 2027 | /* |
| 2028 | Normalize the weighting factors. |
| 2029 | */ |
| 2030 | weight=0.0; |
| 2031 | for (i=0; i < ErrorQueueLength; i++) |
| 2032 | weight+=cube_info->weights[i]; |
| 2033 | sum=0.0; |
| 2034 | for (i=0; i < ErrorQueueLength; i++) |
| 2035 | { |
| 2036 | cube_info->weights[i]/=weight; |
| 2037 | sum+=cube_info->weights[i]; |
| 2038 | } |
| 2039 | cube_info->weights[0]+=1.0-sum; |
| 2040 | return(cube_info); |
| 2041 | } |
| 2042 | |
| 2043 | /* |
| 2044 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2045 | % % |
| 2046 | % % |
| 2047 | % % |
| 2048 | + G e t N o d e I n f o % |
| 2049 | % % |
| 2050 | % % |
| 2051 | % % |
| 2052 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2053 | % |
| 2054 | % GetNodeInfo() allocates memory for a new node in the color cube tree and |
| 2055 | % presets all fields to zero. |
| 2056 | % |
| 2057 | % The format of the GetNodeInfo method is: |
| 2058 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2059 | % NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id, |
| 2060 | % const size_t level,NodeInfo *parent) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2061 | % |
| 2062 | % A description of each parameter follows. |
| 2063 | % |
| 2064 | % o node: The GetNodeInfo method returns a pointer to a queue of nodes. |
| 2065 | % |
| 2066 | % o id: Specifies the child number of the node. |
| 2067 | % |
| 2068 | % o level: Specifies the level in the storage_class the node resides. |
| 2069 | % |
| 2070 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2071 | static NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id, |
| 2072 | const size_t level,NodeInfo *parent) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2073 | { |
| 2074 | NodeInfo |
| 2075 | *node_info; |
| 2076 | |
| 2077 | if (cube_info->free_nodes == 0) |
| 2078 | { |
| 2079 | Nodes |
| 2080 | *nodes; |
| 2081 | |
| 2082 | /* |
| 2083 | Allocate a new queue of nodes. |
| 2084 | */ |
cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 2085 | nodes=(Nodes *) AcquireMagickMemory(sizeof(*nodes)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2086 | if (nodes == (Nodes *) NULL) |
| 2087 | return((NodeInfo *) NULL); |
| 2088 | nodes->nodes=(NodeInfo *) AcquireQuantumMemory(NodesInAList, |
| 2089 | sizeof(*nodes->nodes)); |
| 2090 | if (nodes->nodes == (NodeInfo *) NULL) |
| 2091 | return((NodeInfo *) NULL); |
| 2092 | nodes->next=cube_info->node_queue; |
| 2093 | cube_info->node_queue=nodes; |
| 2094 | cube_info->next_node=nodes->nodes; |
| 2095 | cube_info->free_nodes=NodesInAList; |
| 2096 | } |
| 2097 | cube_info->nodes++; |
| 2098 | cube_info->free_nodes--; |
| 2099 | node_info=cube_info->next_node++; |
| 2100 | (void) ResetMagickMemory(node_info,0,sizeof(*node_info)); |
| 2101 | node_info->parent=parent; |
| 2102 | node_info->id=id; |
| 2103 | node_info->level=level; |
| 2104 | return(node_info); |
| 2105 | } |
| 2106 | |
| 2107 | /* |
| 2108 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2109 | % % |
| 2110 | % % |
| 2111 | % % |
| 2112 | % G e t I m a g e Q u a n t i z e E r r o r % |
| 2113 | % % |
| 2114 | % % |
| 2115 | % % |
| 2116 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2117 | % |
| 2118 | % GetImageQuantizeError() measures the difference between the original |
| 2119 | % and quantized images. This difference is the total quantization error. |
| 2120 | % The error is computed by summing over all pixels in an image the distance |
| 2121 | % squared in RGB space between each reference pixel value and its quantized |
| 2122 | % value. These values are computed: |
| 2123 | % |
| 2124 | % o mean_error_per_pixel: This value is the mean error for any single |
| 2125 | % pixel in the image. |
| 2126 | % |
| 2127 | % o normalized_mean_square_error: This value is the normalized mean |
| 2128 | % quantization error for any single pixel in the image. This distance |
| 2129 | % measure is normalized to a range between 0 and 1. It is independent |
| 2130 | % of the range of red, green, and blue values in the image. |
| 2131 | % |
| 2132 | % o normalized_maximum_square_error: Thsi value is the normalized |
| 2133 | % maximum quantization error for any single pixel in the image. This |
| 2134 | % distance measure is normalized to a range between 0 and 1. It is |
| 2135 | % independent of the range of red, green, and blue values in your image. |
| 2136 | % |
| 2137 | % The format of the GetImageQuantizeError method is: |
| 2138 | % |
| 2139 | % MagickBooleanType GetImageQuantizeError(Image *image) |
| 2140 | % |
| 2141 | % A description of each parameter follows. |
| 2142 | % |
| 2143 | % o image: the image. |
| 2144 | % |
| 2145 | */ |
| 2146 | MagickExport MagickBooleanType GetImageQuantizeError(Image *image) |
| 2147 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2148 | CacheView |
| 2149 | *image_view; |
| 2150 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2151 | ExceptionInfo |
| 2152 | *exception; |
| 2153 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2154 | MagickRealType |
| 2155 | alpha, |
| 2156 | area, |
| 2157 | beta, |
| 2158 | distance, |
| 2159 | maximum_error, |
| 2160 | mean_error, |
| 2161 | mean_error_per_pixel; |
| 2162 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2163 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2164 | index; |
| 2165 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 2166 | ssize_t |
| 2167 | y; |
| 2168 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2169 | assert(image != (Image *) NULL); |
| 2170 | assert(image->signature == MagickSignature); |
| 2171 | if (image->debug != MagickFalse) |
| 2172 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2173 | image->total_colors=GetNumberColors(image,(FILE *) NULL,&image->exception); |
| 2174 | (void) ResetMagickMemory(&image->error,0,sizeof(image->error)); |
| 2175 | if (image->storage_class == DirectClass) |
| 2176 | return(MagickTrue); |
| 2177 | alpha=1.0; |
| 2178 | beta=1.0; |
| 2179 | area=3.0*image->columns*image->rows; |
| 2180 | maximum_error=0.0; |
| 2181 | mean_error_per_pixel=0.0; |
| 2182 | mean_error=0.0; |
| 2183 | exception=(&image->exception); |
| 2184 | image_view=AcquireCacheView(image); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2185 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2186 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2187 | register const Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 2188 | *restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2189 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2190 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2191 | x; |
| 2192 | |
| 2193 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2194 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2195 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2196 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2197 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2198 | index=1UL*GetPixelIndex(image,p); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2199 | if (image->matte != MagickFalse) |
| 2200 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2201 | alpha=(MagickRealType) (QuantumScale*GetPixelAlpha(image,p)); |
| 2202 | beta=(MagickRealType) (QuantumScale*image->colormap[index].alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2203 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2204 | distance=fabs(alpha*GetPixelRed(image,p)-beta* |
cristy | 01e4e7d | 2011-05-01 23:00:41 +0000 | [diff] [blame] | 2205 | image->colormap[index].red); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2206 | mean_error_per_pixel+=distance; |
| 2207 | mean_error+=distance*distance; |
| 2208 | if (distance > maximum_error) |
| 2209 | maximum_error=distance; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2210 | distance=fabs(alpha*GetPixelGreen(image,p)-beta* |
cristy | 01e4e7d | 2011-05-01 23:00:41 +0000 | [diff] [blame] | 2211 | image->colormap[index].green); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2212 | mean_error_per_pixel+=distance; |
| 2213 | mean_error+=distance*distance; |
| 2214 | if (distance > maximum_error) |
| 2215 | maximum_error=distance; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2216 | distance=fabs(alpha*GetPixelBlue(image,p)-beta* |
cristy | 01e4e7d | 2011-05-01 23:00:41 +0000 | [diff] [blame] | 2217 | image->colormap[index].blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2218 | mean_error_per_pixel+=distance; |
| 2219 | mean_error+=distance*distance; |
| 2220 | if (distance > maximum_error) |
| 2221 | maximum_error=distance; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2222 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | image_view=DestroyCacheView(image_view); |
| 2226 | image->error.mean_error_per_pixel=(double) mean_error_per_pixel/area; |
| 2227 | image->error.normalized_mean_error=(double) QuantumScale*QuantumScale* |
| 2228 | mean_error/area; |
| 2229 | image->error.normalized_maximum_error=(double) QuantumScale*maximum_error; |
| 2230 | return(MagickTrue); |
| 2231 | } |
| 2232 | |
| 2233 | /* |
| 2234 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2235 | % % |
| 2236 | % % |
| 2237 | % % |
| 2238 | % G e t Q u a n t i z e I n f o % |
| 2239 | % % |
| 2240 | % % |
| 2241 | % % |
| 2242 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2243 | % |
| 2244 | % GetQuantizeInfo() initializes the QuantizeInfo structure. |
| 2245 | % |
| 2246 | % The format of the GetQuantizeInfo method is: |
| 2247 | % |
| 2248 | % GetQuantizeInfo(QuantizeInfo *quantize_info) |
| 2249 | % |
| 2250 | % A description of each parameter follows: |
| 2251 | % |
| 2252 | % o quantize_info: Specifies a pointer to a QuantizeInfo structure. |
| 2253 | % |
| 2254 | */ |
| 2255 | MagickExport void GetQuantizeInfo(QuantizeInfo *quantize_info) |
| 2256 | { |
| 2257 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| 2258 | assert(quantize_info != (QuantizeInfo *) NULL); |
| 2259 | (void) ResetMagickMemory(quantize_info,0,sizeof(*quantize_info)); |
| 2260 | quantize_info->number_colors=256; |
| 2261 | quantize_info->dither=MagickTrue; |
| 2262 | quantize_info->dither_method=RiemersmaDitherMethod; |
| 2263 | quantize_info->colorspace=UndefinedColorspace; |
| 2264 | quantize_info->measure_error=MagickFalse; |
| 2265 | quantize_info->signature=MagickSignature; |
| 2266 | } |
| 2267 | |
| 2268 | /* |
| 2269 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2270 | % % |
| 2271 | % % |
| 2272 | % % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2273 | % P o s t e r i z e I m a g e % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2274 | % % |
| 2275 | % % |
| 2276 | % % |
| 2277 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2278 | % |
| 2279 | % PosterizeImage() reduces the image to a limited number of colors for a |
| 2280 | % "poster" effect. |
| 2281 | % |
| 2282 | % The format of the PosterizeImage method is: |
| 2283 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2284 | % MagickBooleanType PosterizeImage(Image *image,const size_t levels, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2285 | % const MagickBooleanType dither,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2286 | % |
| 2287 | % A description of each parameter follows: |
| 2288 | % |
| 2289 | % o image: Specifies a pointer to an Image structure. |
| 2290 | % |
| 2291 | % o levels: Number of color levels allowed in each channel. Very low values |
| 2292 | % (2, 3, or 4) have the most visible effect. |
| 2293 | % |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 2294 | % o dither: Set this integer value to something other than zero to dither |
| 2295 | % the mapped image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2296 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2297 | % o exception: return any errors or warnings in this structure. |
| 2298 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2299 | */ |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2300 | |
cristy | 4d72715 | 2011-02-10 19:57:21 +0000 | [diff] [blame] | 2301 | static inline ssize_t MagickRound(MagickRealType x) |
| 2302 | { |
| 2303 | /* |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 2304 | Round the fraction to nearest integer. |
cristy | 4d72715 | 2011-02-10 19:57:21 +0000 | [diff] [blame] | 2305 | */ |
| 2306 | if (x >= 0.0) |
| 2307 | return((ssize_t) (x+0.5)); |
| 2308 | return((ssize_t) (x-0.5)); |
| 2309 | } |
| 2310 | |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2311 | MagickExport MagickBooleanType PosterizeImage(Image *image,const size_t levels, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2312 | const MagickBooleanType dither,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2313 | { |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2314 | #define PosterizeImageTag "Posterize/Image" |
cristy | 4d72715 | 2011-02-10 19:57:21 +0000 | [diff] [blame] | 2315 | #define PosterizePixel(pixel) (Quantum) (QuantumRange*(MagickRound( \ |
cristy | 3e9cad0 | 2011-02-20 01:42:00 +0000 | [diff] [blame] | 2316 | QuantumScale*pixel*(levels-1)))/MagickMax((ssize_t) levels-1,1)) |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2317 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2318 | CacheView |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2319 | *image_view; |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2320 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2321 | MagickBooleanType |
| 2322 | status; |
| 2323 | |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2324 | MagickOffsetType |
| 2325 | progress; |
| 2326 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2327 | QuantizeInfo |
| 2328 | *quantize_info; |
| 2329 | |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 2330 | register ssize_t |
| 2331 | i; |
| 2332 | |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 2333 | ssize_t |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2334 | y; |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 2335 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2336 | assert(image != (Image *) NULL); |
| 2337 | assert(image->signature == MagickSignature); |
| 2338 | if (image->debug != MagickFalse) |
| 2339 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2340 | if (image->storage_class == PseudoClass) |
| 2341 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 00cbdd6 | 2011-02-20 17:29:26 +0000 | [diff] [blame] | 2342 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2343 | #endif |
| 2344 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2345 | { |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2346 | /* |
| 2347 | Posterize colormap. |
| 2348 | */ |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2349 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2350 | image->colormap[i].red=PosterizePixel(image->colormap[i].red); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2351 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2352 | image->colormap[i].green=PosterizePixel(image->colormap[i].green); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2353 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2354 | image->colormap[i].blue=PosterizePixel(image->colormap[i].blue); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2355 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2356 | image->colormap[i].alpha=PosterizePixel(image->colormap[i].alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2357 | } |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2358 | /* |
| 2359 | Posterize image. |
| 2360 | */ |
| 2361 | status=MagickTrue; |
| 2362 | progress=0; |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2363 | image_view=AcquireCacheView(image); |
| 2364 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2365 | #pragma omp parallel for schedule(dynamic,4) shared(progress,status) |
| 2366 | #endif |
| 2367 | for (y=0; y < (ssize_t) image->rows; y++) |
| 2368 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2369 | register Quantum |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2370 | *restrict q; |
| 2371 | |
| 2372 | register ssize_t |
| 2373 | x; |
| 2374 | |
| 2375 | if (status == MagickFalse) |
| 2376 | continue; |
| 2377 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 2378 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2379 | { |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2380 | status=MagickFalse; |
| 2381 | continue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2382 | } |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2383 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2384 | { |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2385 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2386 | SetPixelRed(image,PosterizePixel(GetPixelRed(image,q)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2387 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2388 | SetPixelGreen(image,PosterizePixel(GetPixelGreen(image,q)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2389 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2390 | SetPixelBlue(image,PosterizePixel(GetPixelBlue(image,q)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2391 | if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) && |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2392 | (image->colorspace == CMYKColorspace)) |
| 2393 | SetPixelBlack(image,PosterizePixel(GetPixelBlack(image,q)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2394 | if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) && |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2395 | (image->matte == MagickTrue)) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2396 | SetPixelAlpha(image,PosterizePixel(GetPixelAlpha(image,q)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2397 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2398 | } |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2399 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 2400 | status=MagickFalse; |
| 2401 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2402 | { |
| 2403 | MagickBooleanType |
| 2404 | proceed; |
| 2405 | |
| 2406 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 1302067 | 2011-07-08 02:33:26 +0000 | [diff] [blame] | 2407 | #pragma omp critical (MagickCore_PosterizeImage) |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2408 | #endif |
| 2409 | proceed=SetImageProgress(image,PosterizeImageTag,progress++, |
| 2410 | image->rows); |
| 2411 | if (proceed == MagickFalse) |
| 2412 | status=MagickFalse; |
| 2413 | } |
| 2414 | } |
| 2415 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2416 | quantize_info=AcquireQuantizeInfo((ImageInfo *) NULL); |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2417 | quantize_info->number_colors=(size_t) MagickMin((ssize_t) levels*levels* |
| 2418 | levels,MaxColormapSize+1); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2419 | quantize_info->dither=dither; |
cristy | 3e9cad0 | 2011-02-20 01:42:00 +0000 | [diff] [blame] | 2420 | quantize_info->tree_depth=MaxTreeDepth; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2421 | status=QuantizeImage(quantize_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2422 | quantize_info=DestroyQuantizeInfo(quantize_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2423 | return(status); |
| 2424 | } |
| 2425 | |
| 2426 | /* |
| 2427 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2428 | % % |
| 2429 | % % |
| 2430 | % % |
| 2431 | + P r u n e C h i l d % |
| 2432 | % % |
| 2433 | % % |
| 2434 | % % |
| 2435 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2436 | % |
| 2437 | % PruneChild() deletes the given node and merges its statistics into its |
| 2438 | % parent. |
| 2439 | % |
| 2440 | % The format of the PruneSubtree method is: |
| 2441 | % |
| 2442 | % PruneChild(const Image *image,CubeInfo *cube_info, |
| 2443 | % const NodeInfo *node_info) |
| 2444 | % |
| 2445 | % A description of each parameter follows. |
| 2446 | % |
| 2447 | % o image: the image. |
| 2448 | % |
| 2449 | % o cube_info: A pointer to the Cube structure. |
| 2450 | % |
| 2451 | % o node_info: pointer to node in color cube tree that is to be pruned. |
| 2452 | % |
| 2453 | */ |
| 2454 | static void PruneChild(const Image *image,CubeInfo *cube_info, |
| 2455 | const NodeInfo *node_info) |
| 2456 | { |
| 2457 | NodeInfo |
| 2458 | *parent; |
| 2459 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2460 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2461 | i; |
| 2462 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2463 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2464 | number_children; |
| 2465 | |
| 2466 | /* |
| 2467 | Traverse any children. |
| 2468 | */ |
| 2469 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2470 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2471 | if (node_info->child[i] != (NodeInfo *) NULL) |
| 2472 | PruneChild(image,cube_info,node_info->child[i]); |
| 2473 | /* |
| 2474 | Merge color statistics into parent. |
| 2475 | */ |
| 2476 | parent=node_info->parent; |
| 2477 | parent->number_unique+=node_info->number_unique; |
| 2478 | parent->total_color.red+=node_info->total_color.red; |
| 2479 | parent->total_color.green+=node_info->total_color.green; |
| 2480 | parent->total_color.blue+=node_info->total_color.blue; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2481 | parent->total_color.alpha+=node_info->total_color.alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2482 | parent->child[node_info->id]=(NodeInfo *) NULL; |
| 2483 | cube_info->nodes--; |
| 2484 | } |
| 2485 | |
| 2486 | /* |
| 2487 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2488 | % % |
| 2489 | % % |
| 2490 | % % |
| 2491 | + P r u n e L e v e l % |
| 2492 | % % |
| 2493 | % % |
| 2494 | % % |
| 2495 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2496 | % |
| 2497 | % PruneLevel() deletes all nodes at the bottom level of the color tree merging |
| 2498 | % their color statistics into their parent node. |
| 2499 | % |
| 2500 | % The format of the PruneLevel method is: |
| 2501 | % |
| 2502 | % PruneLevel(const Image *image,CubeInfo *cube_info, |
| 2503 | % const NodeInfo *node_info) |
| 2504 | % |
| 2505 | % A description of each parameter follows. |
| 2506 | % |
| 2507 | % o image: the image. |
| 2508 | % |
| 2509 | % o cube_info: A pointer to the Cube structure. |
| 2510 | % |
| 2511 | % o node_info: pointer to node in color cube tree that is to be pruned. |
| 2512 | % |
| 2513 | */ |
| 2514 | static void PruneLevel(const Image *image,CubeInfo *cube_info, |
| 2515 | const NodeInfo *node_info) |
| 2516 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2517 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2518 | i; |
| 2519 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2520 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2521 | number_children; |
| 2522 | |
| 2523 | /* |
| 2524 | Traverse any children. |
| 2525 | */ |
| 2526 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2527 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2528 | if (node_info->child[i] != (NodeInfo *) NULL) |
| 2529 | PruneLevel(image,cube_info,node_info->child[i]); |
| 2530 | if (node_info->level == cube_info->depth) |
| 2531 | PruneChild(image,cube_info,node_info); |
| 2532 | } |
| 2533 | |
| 2534 | /* |
| 2535 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2536 | % % |
| 2537 | % % |
| 2538 | % % |
| 2539 | + P r u n e T o C u b e D e p t h % |
| 2540 | % % |
| 2541 | % % |
| 2542 | % % |
| 2543 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2544 | % |
| 2545 | % PruneToCubeDepth() deletes any nodes at a depth greater than |
| 2546 | % cube_info->depth while merging their color statistics into their parent |
| 2547 | % node. |
| 2548 | % |
| 2549 | % The format of the PruneToCubeDepth method is: |
| 2550 | % |
| 2551 | % PruneToCubeDepth(const Image *image,CubeInfo *cube_info, |
| 2552 | % const NodeInfo *node_info) |
| 2553 | % |
| 2554 | % A description of each parameter follows. |
| 2555 | % |
| 2556 | % o cube_info: A pointer to the Cube structure. |
| 2557 | % |
| 2558 | % o node_info: pointer to node in color cube tree that is to be pruned. |
| 2559 | % |
| 2560 | */ |
| 2561 | static void PruneToCubeDepth(const Image *image,CubeInfo *cube_info, |
| 2562 | const NodeInfo *node_info) |
| 2563 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2564 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2565 | i; |
| 2566 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2567 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2568 | number_children; |
| 2569 | |
| 2570 | /* |
| 2571 | Traverse any children. |
| 2572 | */ |
| 2573 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2574 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2575 | if (node_info->child[i] != (NodeInfo *) NULL) |
| 2576 | PruneToCubeDepth(image,cube_info,node_info->child[i]); |
| 2577 | if (node_info->level > cube_info->depth) |
| 2578 | PruneChild(image,cube_info,node_info); |
| 2579 | } |
| 2580 | |
| 2581 | /* |
| 2582 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2583 | % % |
| 2584 | % % |
| 2585 | % % |
| 2586 | % Q u a n t i z e I m a g e % |
| 2587 | % % |
| 2588 | % % |
| 2589 | % % |
| 2590 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2591 | % |
| 2592 | % QuantizeImage() analyzes the colors within a reference image and chooses a |
| 2593 | % fixed number of colors to represent the image. The goal of the algorithm |
| 2594 | % is to minimize the color difference between the input and output image while |
| 2595 | % minimizing the processing time. |
| 2596 | % |
| 2597 | % The format of the QuantizeImage method is: |
| 2598 | % |
| 2599 | % MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2600 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2601 | % |
| 2602 | % A description of each parameter follows: |
| 2603 | % |
| 2604 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 2605 | % |
| 2606 | % o image: the image. |
| 2607 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2608 | % o exception: return any errors or warnings in this structure. |
| 2609 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2610 | */ |
cristy | 5f7dca6 | 2011-08-12 12:38:05 +0000 | [diff] [blame] | 2611 | |
| 2612 | static MagickBooleanType DirectToColormapImage(Image *image, |
| 2613 | ExceptionInfo *exception) |
| 2614 | { |
| 2615 | CacheView |
| 2616 | *image_view; |
| 2617 | |
| 2618 | MagickBooleanType |
| 2619 | status; |
| 2620 | |
| 2621 | register ssize_t |
| 2622 | i; |
| 2623 | |
| 2624 | size_t |
| 2625 | number_colors; |
| 2626 | |
| 2627 | ssize_t |
| 2628 | y; |
| 2629 | |
| 2630 | status=MagickTrue; |
| 2631 | number_colors=(size_t) (image->columns*image->rows); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2632 | if (AcquireImageColormap(image,number_colors,exception) == MagickFalse) |
cristy | 5f7dca6 | 2011-08-12 12:38:05 +0000 | [diff] [blame] | 2633 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 2634 | image->filename); |
| 2635 | if (image->colors != number_colors) |
| 2636 | return(MagickFalse); |
| 2637 | i=0; |
| 2638 | image_view=AcquireCacheView(image); |
| 2639 | for (y=0; y < (ssize_t) image->rows; y++) |
| 2640 | { |
| 2641 | MagickBooleanType |
| 2642 | proceed; |
| 2643 | |
| 2644 | register Quantum |
| 2645 | *restrict q; |
| 2646 | |
| 2647 | register ssize_t |
| 2648 | x; |
| 2649 | |
| 2650 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 2651 | if (q == (Quantum *) NULL) |
| 2652 | break; |
| 2653 | for (x=0; x < (ssize_t) image->columns; x++) |
| 2654 | { |
| 2655 | image->colormap[i].red=GetPixelRed(image,q); |
| 2656 | image->colormap[i].green=GetPixelGreen(image,q); |
| 2657 | image->colormap[i].blue=GetPixelBlue(image,q); |
| 2658 | image->colormap[i].alpha=GetPixelAlpha(image,q); |
| 2659 | SetPixelIndex(image,(Quantum) i,q); |
| 2660 | i++; |
| 2661 | q+=GetPixelChannels(image); |
| 2662 | } |
| 2663 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 2664 | break; |
| 2665 | proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y, |
| 2666 | image->rows); |
| 2667 | if (proceed == MagickFalse) |
| 2668 | status=MagickFalse; |
| 2669 | } |
| 2670 | image_view=DestroyCacheView(image_view); |
| 2671 | return(status); |
| 2672 | } |
| 2673 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2674 | MagickExport MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2675 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2676 | { |
| 2677 | CubeInfo |
| 2678 | *cube_info; |
| 2679 | |
| 2680 | MagickBooleanType |
| 2681 | status; |
| 2682 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2683 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2684 | depth, |
| 2685 | maximum_colors; |
| 2686 | |
| 2687 | assert(quantize_info != (const QuantizeInfo *) NULL); |
| 2688 | assert(quantize_info->signature == MagickSignature); |
| 2689 | assert(image != (Image *) NULL); |
| 2690 | assert(image->signature == MagickSignature); |
| 2691 | if (image->debug != MagickFalse) |
| 2692 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2693 | maximum_colors=quantize_info->number_colors; |
| 2694 | if (maximum_colors == 0) |
| 2695 | maximum_colors=MaxColormapSize; |
| 2696 | if (maximum_colors > MaxColormapSize) |
| 2697 | maximum_colors=MaxColormapSize; |
cristy | 5f7dca6 | 2011-08-12 12:38:05 +0000 | [diff] [blame] | 2698 | if ((image->columns*image->rows) <= maximum_colors) |
| 2699 | (void) DirectToColormapImage(image,&image->exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2700 | if ((IsImageGray(image,&image->exception) != MagickFalse) && |
cristy | 8e75275 | 2011-04-16 13:48:22 +0000 | [diff] [blame] | 2701 | (image->matte == MagickFalse)) |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2702 | (void) SetGrayscaleImage(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2703 | if ((image->storage_class == PseudoClass) && |
| 2704 | (image->colors <= maximum_colors)) |
| 2705 | return(MagickTrue); |
| 2706 | depth=quantize_info->tree_depth; |
| 2707 | if (depth == 0) |
| 2708 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2709 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2710 | colors; |
| 2711 | |
| 2712 | /* |
| 2713 | Depth of color tree is: Log4(colormap size)+2. |
| 2714 | */ |
| 2715 | colors=maximum_colors; |
| 2716 | for (depth=1; colors != 0; depth++) |
| 2717 | colors>>=2; |
| 2718 | if ((quantize_info->dither != MagickFalse) && (depth > 2)) |
| 2719 | depth--; |
| 2720 | if ((image->matte != MagickFalse) && (depth > 5)) |
| 2721 | depth--; |
| 2722 | } |
| 2723 | /* |
| 2724 | Initialize color cube. |
| 2725 | */ |
| 2726 | cube_info=GetCubeInfo(quantize_info,depth,maximum_colors); |
| 2727 | if (cube_info == (CubeInfo *) NULL) |
| 2728 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 2729 | image->filename); |
| 2730 | status=ClassifyImageColors(cube_info,image,&image->exception); |
| 2731 | if (status != MagickFalse) |
| 2732 | { |
| 2733 | /* |
| 2734 | Reduce the number of colors in the image. |
| 2735 | */ |
| 2736 | ReduceImageColors(image,cube_info); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2737 | status=AssignImageColors(image,cube_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2738 | } |
| 2739 | DestroyCubeInfo(cube_info); |
| 2740 | return(status); |
| 2741 | } |
| 2742 | |
| 2743 | /* |
| 2744 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2745 | % % |
| 2746 | % % |
| 2747 | % % |
| 2748 | % Q u a n t i z e I m a g e s % |
| 2749 | % % |
| 2750 | % % |
| 2751 | % % |
| 2752 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2753 | % |
| 2754 | % QuantizeImages() analyzes the colors within a set of reference images and |
| 2755 | % chooses a fixed number of colors to represent the set. The goal of the |
| 2756 | % algorithm is to minimize the color difference between the input and output |
| 2757 | % images while minimizing the processing time. |
| 2758 | % |
| 2759 | % The format of the QuantizeImages method is: |
| 2760 | % |
| 2761 | % MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2762 | % Image *images,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2763 | % |
| 2764 | % A description of each parameter follows: |
| 2765 | % |
| 2766 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 2767 | % |
| 2768 | % o images: Specifies a pointer to a list of Image structures. |
| 2769 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2770 | % o exception: return any errors or warnings in this structure. |
| 2771 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2772 | */ |
| 2773 | MagickExport MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2774 | Image *images,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2775 | { |
| 2776 | CubeInfo |
| 2777 | *cube_info; |
| 2778 | |
| 2779 | Image |
| 2780 | *image; |
| 2781 | |
| 2782 | MagickBooleanType |
| 2783 | proceed, |
| 2784 | status; |
| 2785 | |
| 2786 | MagickProgressMonitor |
| 2787 | progress_monitor; |
| 2788 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2789 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2790 | i; |
| 2791 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2792 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2793 | depth, |
| 2794 | maximum_colors, |
| 2795 | number_images; |
| 2796 | |
| 2797 | assert(quantize_info != (const QuantizeInfo *) NULL); |
| 2798 | assert(quantize_info->signature == MagickSignature); |
| 2799 | assert(images != (Image *) NULL); |
| 2800 | assert(images->signature == MagickSignature); |
| 2801 | if (images->debug != MagickFalse) |
| 2802 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename); |
| 2803 | if (GetNextImageInList(images) == (Image *) NULL) |
| 2804 | { |
| 2805 | /* |
| 2806 | Handle a single image with QuantizeImage. |
| 2807 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2808 | status=QuantizeImage(quantize_info,images,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2809 | return(status); |
| 2810 | } |
| 2811 | status=MagickFalse; |
| 2812 | maximum_colors=quantize_info->number_colors; |
| 2813 | if (maximum_colors == 0) |
| 2814 | maximum_colors=MaxColormapSize; |
| 2815 | if (maximum_colors > MaxColormapSize) |
| 2816 | maximum_colors=MaxColormapSize; |
| 2817 | depth=quantize_info->tree_depth; |
| 2818 | if (depth == 0) |
| 2819 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2820 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2821 | colors; |
| 2822 | |
| 2823 | /* |
| 2824 | Depth of color tree is: Log4(colormap size)+2. |
| 2825 | */ |
| 2826 | colors=maximum_colors; |
| 2827 | for (depth=1; colors != 0; depth++) |
| 2828 | colors>>=2; |
| 2829 | if (quantize_info->dither != MagickFalse) |
| 2830 | depth--; |
| 2831 | } |
| 2832 | /* |
| 2833 | Initialize color cube. |
| 2834 | */ |
| 2835 | cube_info=GetCubeInfo(quantize_info,depth,maximum_colors); |
| 2836 | if (cube_info == (CubeInfo *) NULL) |
| 2837 | { |
| 2838 | (void) ThrowMagickException(&images->exception,GetMagickModule(), |
| 2839 | ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename); |
| 2840 | return(MagickFalse); |
| 2841 | } |
| 2842 | number_images=GetImageListLength(images); |
| 2843 | image=images; |
| 2844 | for (i=0; image != (Image *) NULL; i++) |
| 2845 | { |
| 2846 | progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor) NULL, |
| 2847 | image->client_data); |
| 2848 | status=ClassifyImageColors(cube_info,image,&image->exception); |
| 2849 | if (status == MagickFalse) |
| 2850 | break; |
| 2851 | (void) SetImageProgressMonitor(image,progress_monitor,image->client_data); |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 2852 | proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i, |
| 2853 | number_images); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2854 | if (proceed == MagickFalse) |
| 2855 | break; |
| 2856 | image=GetNextImageInList(image); |
| 2857 | } |
| 2858 | if (status != MagickFalse) |
| 2859 | { |
| 2860 | /* |
| 2861 | Reduce the number of colors in an image sequence. |
| 2862 | */ |
| 2863 | ReduceImageColors(images,cube_info); |
| 2864 | image=images; |
| 2865 | for (i=0; image != (Image *) NULL; i++) |
| 2866 | { |
| 2867 | progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor) |
| 2868 | NULL,image->client_data); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2869 | status=AssignImageColors(image,cube_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2870 | if (status == MagickFalse) |
| 2871 | break; |
| 2872 | (void) SetImageProgressMonitor(image,progress_monitor, |
| 2873 | image->client_data); |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 2874 | proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i, |
| 2875 | number_images); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2876 | if (proceed == MagickFalse) |
| 2877 | break; |
| 2878 | image=GetNextImageInList(image); |
| 2879 | } |
| 2880 | } |
| 2881 | DestroyCubeInfo(cube_info); |
| 2882 | return(status); |
| 2883 | } |
| 2884 | |
| 2885 | /* |
| 2886 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2887 | % % |
| 2888 | % % |
| 2889 | % % |
| 2890 | + R e d u c e % |
| 2891 | % % |
| 2892 | % % |
| 2893 | % % |
| 2894 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2895 | % |
| 2896 | % Reduce() traverses the color cube tree and prunes any node whose |
| 2897 | % quantization error falls below a particular threshold. |
| 2898 | % |
| 2899 | % The format of the Reduce method is: |
| 2900 | % |
| 2901 | % Reduce(const Image *image,CubeInfo *cube_info,const NodeInfo *node_info) |
| 2902 | % |
| 2903 | % A description of each parameter follows. |
| 2904 | % |
| 2905 | % o image: the image. |
| 2906 | % |
| 2907 | % o cube_info: A pointer to the Cube structure. |
| 2908 | % |
| 2909 | % o node_info: pointer to node in color cube tree that is to be pruned. |
| 2910 | % |
| 2911 | */ |
| 2912 | static void Reduce(const Image *image,CubeInfo *cube_info, |
| 2913 | const NodeInfo *node_info) |
| 2914 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2915 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2916 | i; |
| 2917 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2918 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2919 | number_children; |
| 2920 | |
| 2921 | /* |
| 2922 | Traverse any children. |
| 2923 | */ |
| 2924 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2925 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2926 | if (node_info->child[i] != (NodeInfo *) NULL) |
| 2927 | Reduce(image,cube_info,node_info->child[i]); |
| 2928 | if (node_info->quantize_error <= cube_info->pruning_threshold) |
| 2929 | PruneChild(image,cube_info,node_info); |
| 2930 | else |
| 2931 | { |
| 2932 | /* |
| 2933 | Find minimum pruning threshold. |
| 2934 | */ |
| 2935 | if (node_info->number_unique > 0) |
| 2936 | cube_info->colors++; |
| 2937 | if (node_info->quantize_error < cube_info->next_threshold) |
| 2938 | cube_info->next_threshold=node_info->quantize_error; |
| 2939 | } |
| 2940 | } |
| 2941 | |
| 2942 | /* |
| 2943 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2944 | % % |
| 2945 | % % |
| 2946 | % % |
| 2947 | + R e d u c e I m a g e C o l o r s % |
| 2948 | % % |
| 2949 | % % |
| 2950 | % % |
| 2951 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2952 | % |
| 2953 | % ReduceImageColors() repeatedly prunes the tree until the number of nodes |
| 2954 | % with n2 > 0 is less than or equal to the maximum number of colors allowed |
| 2955 | % in the output image. On any given iteration over the tree, it selects |
| 2956 | % those nodes whose E value is minimal for pruning and merges their |
| 2957 | % color statistics upward. It uses a pruning threshold, Ep, to govern |
| 2958 | % node selection as follows: |
| 2959 | % |
| 2960 | % Ep = 0 |
| 2961 | % while number of nodes with (n2 > 0) > required maximum number of colors |
| 2962 | % prune all nodes such that E <= Ep |
| 2963 | % Set Ep to minimum E in remaining nodes |
| 2964 | % |
| 2965 | % This has the effect of minimizing any quantization error when merging |
| 2966 | % two nodes together. |
| 2967 | % |
| 2968 | % When a node to be pruned has offspring, the pruning procedure invokes |
| 2969 | % itself recursively in order to prune the tree from the leaves upward. |
| 2970 | % n2, Sr, Sg, and Sb in a node being pruned are always added to the |
| 2971 | % corresponding data in that node's parent. This retains the pruned |
| 2972 | % node's color characteristics for later averaging. |
| 2973 | % |
| 2974 | % For each node, n2 pixels exist for which that node represents the |
| 2975 | % smallest volume in RGB space containing those pixel's colors. When n2 |
| 2976 | % > 0 the node will uniquely define a color in the output image. At the |
| 2977 | % beginning of reduction, n2 = 0 for all nodes except a the leaves of |
| 2978 | % the tree which represent colors present in the input image. |
| 2979 | % |
| 2980 | % The other pixel count, n1, indicates the total number of colors |
| 2981 | % within the cubic volume which the node represents. This includes n1 - |
| 2982 | % n2 pixels whose colors should be defined by nodes at a lower level in |
| 2983 | % the tree. |
| 2984 | % |
| 2985 | % The format of the ReduceImageColors method is: |
| 2986 | % |
| 2987 | % ReduceImageColors(const Image *image,CubeInfo *cube_info) |
| 2988 | % |
| 2989 | % A description of each parameter follows. |
| 2990 | % |
| 2991 | % o image: the image. |
| 2992 | % |
| 2993 | % o cube_info: A pointer to the Cube structure. |
| 2994 | % |
| 2995 | */ |
| 2996 | static void ReduceImageColors(const Image *image,CubeInfo *cube_info) |
| 2997 | { |
| 2998 | #define ReduceImageTag "Reduce/Image" |
| 2999 | |
| 3000 | MagickBooleanType |
| 3001 | proceed; |
| 3002 | |
| 3003 | MagickOffsetType |
| 3004 | offset; |
| 3005 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3006 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3007 | span; |
| 3008 | |
| 3009 | cube_info->next_threshold=0.0; |
| 3010 | for (span=cube_info->colors; cube_info->colors > cube_info->maximum_colors; ) |
| 3011 | { |
| 3012 | cube_info->pruning_threshold=cube_info->next_threshold; |
| 3013 | cube_info->next_threshold=cube_info->root->quantize_error-1; |
| 3014 | cube_info->colors=0; |
| 3015 | Reduce(image,cube_info,cube_info->root); |
| 3016 | offset=(MagickOffsetType) span-cube_info->colors; |
| 3017 | proceed=SetImageProgress(image,ReduceImageTag,offset,span- |
| 3018 | cube_info->maximum_colors+1); |
| 3019 | if (proceed == MagickFalse) |
| 3020 | break; |
| 3021 | } |
| 3022 | } |
| 3023 | |
| 3024 | /* |
| 3025 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3026 | % % |
| 3027 | % % |
| 3028 | % % |
| 3029 | % R e m a p I m a g e % |
| 3030 | % % |
| 3031 | % % |
| 3032 | % % |
| 3033 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3034 | % |
| 3035 | % RemapImage() replaces the colors of an image with the closest color from |
| 3036 | % a reference image. |
| 3037 | % |
| 3038 | % The format of the RemapImage method is: |
| 3039 | % |
| 3040 | % MagickBooleanType RemapImage(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3041 | % Image *image,const Image *remap_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3042 | % |
| 3043 | % A description of each parameter follows: |
| 3044 | % |
| 3045 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 3046 | % |
| 3047 | % o image: the image. |
| 3048 | % |
| 3049 | % o remap_image: the reference image. |
| 3050 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3051 | % o exception: return any errors or warnings in this structure. |
| 3052 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3053 | */ |
| 3054 | MagickExport MagickBooleanType RemapImage(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3055 | Image *image,const Image *remap_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3056 | { |
| 3057 | CubeInfo |
| 3058 | *cube_info; |
| 3059 | |
| 3060 | MagickBooleanType |
| 3061 | status; |
| 3062 | |
| 3063 | /* |
| 3064 | Initialize color cube. |
| 3065 | */ |
| 3066 | assert(image != (Image *) NULL); |
| 3067 | assert(image->signature == MagickSignature); |
| 3068 | if (image->debug != MagickFalse) |
| 3069 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 3070 | assert(remap_image != (Image *) NULL); |
| 3071 | assert(remap_image->signature == MagickSignature); |
| 3072 | cube_info=GetCubeInfo(quantize_info,MaxTreeDepth, |
| 3073 | quantize_info->number_colors); |
| 3074 | if (cube_info == (CubeInfo *) NULL) |
| 3075 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3076 | image->filename); |
| 3077 | status=ClassifyImageColors(cube_info,remap_image,&image->exception); |
| 3078 | if (status != MagickFalse) |
| 3079 | { |
| 3080 | /* |
| 3081 | Classify image colors from the reference image. |
| 3082 | */ |
| 3083 | cube_info->quantize_info->number_colors=cube_info->colors; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3084 | status=AssignImageColors(image,cube_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3085 | } |
| 3086 | DestroyCubeInfo(cube_info); |
| 3087 | return(status); |
| 3088 | } |
| 3089 | |
| 3090 | /* |
| 3091 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3092 | % % |
| 3093 | % % |
| 3094 | % % |
| 3095 | % R e m a p I m a g e s % |
| 3096 | % % |
| 3097 | % % |
| 3098 | % % |
| 3099 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3100 | % |
| 3101 | % RemapImages() replaces the colors of a sequence of images with the |
| 3102 | % closest color from a reference image. |
| 3103 | % |
| 3104 | % The format of the RemapImage method is: |
| 3105 | % |
| 3106 | % MagickBooleanType RemapImages(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3107 | % Image *images,Image *remap_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3108 | % |
| 3109 | % A description of each parameter follows: |
| 3110 | % |
| 3111 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 3112 | % |
| 3113 | % o images: the image sequence. |
| 3114 | % |
| 3115 | % o remap_image: the reference image. |
| 3116 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3117 | % o exception: return any errors or warnings in this structure. |
| 3118 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3119 | */ |
| 3120 | MagickExport MagickBooleanType RemapImages(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3121 | Image *images,const Image *remap_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3122 | { |
| 3123 | CubeInfo |
| 3124 | *cube_info; |
| 3125 | |
| 3126 | Image |
| 3127 | *image; |
| 3128 | |
| 3129 | MagickBooleanType |
| 3130 | status; |
| 3131 | |
| 3132 | assert(images != (Image *) NULL); |
| 3133 | assert(images->signature == MagickSignature); |
| 3134 | if (images->debug != MagickFalse) |
| 3135 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename); |
| 3136 | image=images; |
| 3137 | if (remap_image == (Image *) NULL) |
| 3138 | { |
| 3139 | /* |
| 3140 | Create a global colormap for an image sequence. |
| 3141 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3142 | status=QuantizeImages(quantize_info,images,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3143 | return(status); |
| 3144 | } |
| 3145 | /* |
| 3146 | Classify image colors from the reference image. |
| 3147 | */ |
| 3148 | cube_info=GetCubeInfo(quantize_info,MaxTreeDepth, |
| 3149 | quantize_info->number_colors); |
| 3150 | if (cube_info == (CubeInfo *) NULL) |
| 3151 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3152 | image->filename); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3153 | status=ClassifyImageColors(cube_info,remap_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3154 | if (status != MagickFalse) |
| 3155 | { |
| 3156 | /* |
| 3157 | Classify image colors from the reference image. |
| 3158 | */ |
| 3159 | cube_info->quantize_info->number_colors=cube_info->colors; |
| 3160 | image=images; |
| 3161 | for ( ; image != (Image *) NULL; image=GetNextImageInList(image)) |
| 3162 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3163 | status=AssignImageColors(image,cube_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3164 | if (status == MagickFalse) |
| 3165 | break; |
| 3166 | } |
| 3167 | } |
| 3168 | DestroyCubeInfo(cube_info); |
| 3169 | return(status); |
| 3170 | } |
| 3171 | |
| 3172 | /* |
| 3173 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3174 | % % |
| 3175 | % % |
| 3176 | % % |
| 3177 | % S e t G r a y s c a l e I m a g e % |
| 3178 | % % |
| 3179 | % % |
| 3180 | % % |
| 3181 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3182 | % |
| 3183 | % SetGrayscaleImage() converts an image to a PseudoClass grayscale image. |
| 3184 | % |
| 3185 | % The format of the SetGrayscaleImage method is: |
| 3186 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3187 | % MagickBooleanType SetGrayscaleImage(Image *image,ExceptionInfo *exeption) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3188 | % |
| 3189 | % A description of each parameter follows: |
| 3190 | % |
| 3191 | % o image: The image. |
| 3192 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3193 | % o exception: return any errors or warnings in this structure. |
| 3194 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3195 | */ |
| 3196 | |
| 3197 | #if defined(__cplusplus) || defined(c_plusplus) |
| 3198 | extern "C" { |
| 3199 | #endif |
| 3200 | |
| 3201 | static int IntensityCompare(const void *x,const void *y) |
| 3202 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3203 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3204 | *color_1, |
| 3205 | *color_2; |
| 3206 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 3207 | ssize_t |
| 3208 | intensity; |
| 3209 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3210 | color_1=(PixelInfo *) x; |
| 3211 | color_2=(PixelInfo *) y; |
| 3212 | intensity=GetPixelInfoIntensity(color_1)-(ssize_t) |
| 3213 | GetPixelInfoIntensity(color_2); |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 3214 | return((int) intensity); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | #if defined(__cplusplus) || defined(c_plusplus) |
| 3218 | } |
| 3219 | #endif |
| 3220 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3221 | static MagickBooleanType SetGrayscaleImage(Image *image, |
| 3222 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3223 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 3224 | CacheView |
| 3225 | *image_view; |
| 3226 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 3227 | MagickBooleanType |
| 3228 | status; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3229 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3230 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3231 | *colormap; |
| 3232 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3233 | register ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3234 | i; |
| 3235 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 3236 | ssize_t |
| 3237 | *colormap_index, |
| 3238 | j, |
| 3239 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3240 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3241 | assert(image != (Image *) NULL); |
| 3242 | assert(image->signature == MagickSignature); |
| 3243 | if (image->type != GrayscaleType) |
cristy | e941a75 | 2011-10-15 01:52:48 +0000 | [diff] [blame] | 3244 | (void) TransformImageColorspace(image,GRAYColorspace,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3245 | colormap_index=(ssize_t *) AcquireQuantumMemory(MaxMap+1, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3246 | sizeof(*colormap_index)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3247 | if (colormap_index == (ssize_t *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3248 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3249 | image->filename); |
| 3250 | if (image->storage_class != PseudoClass) |
| 3251 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3252 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3253 | colormap_index[i]=(-1); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3254 | if (AcquireImageColormap(image,MaxMap+1,exception) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3255 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3256 | image->filename); |
| 3257 | image->colors=0; |
| 3258 | status=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3259 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3260 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 00cbdd6 | 2011-02-20 17:29:26 +0000 | [diff] [blame] | 3261 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3262 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3263 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3264 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3265 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3266 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3267 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 3268 | register ssize_t |
| 3269 | x; |
| 3270 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3271 | if (status == MagickFalse) |
| 3272 | continue; |
| 3273 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1, |
| 3274 | exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 3275 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3276 | { |
| 3277 | status=MagickFalse; |
| 3278 | continue; |
| 3279 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3280 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3281 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3282 | register size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3283 | intensity; |
| 3284 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3285 | intensity=ScaleQuantumToMap(GetPixelRed(image,q)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3286 | if (colormap_index[intensity] < 0) |
| 3287 | { |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3288 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3289 | #pragma omp critical (MagickCore_SetGrayscaleImage) |
| 3290 | #endif |
| 3291 | if (colormap_index[intensity] < 0) |
| 3292 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3293 | colormap_index[intensity]=(ssize_t) image->colors; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3294 | image->colormap[image->colors].red=GetPixelRed(image,q); |
| 3295 | image->colormap[image->colors].green=GetPixelGreen(image,q); |
| 3296 | image->colormap[image->colors].blue=GetPixelBlue(image,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3297 | image->colors++; |
| 3298 | } |
| 3299 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3300 | SetPixelIndex(image,(Quantum) |
| 3301 | colormap_index[intensity],q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 3302 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3303 | } |
| 3304 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 3305 | status=MagickFalse; |
| 3306 | } |
| 3307 | image_view=DestroyCacheView(image_view); |
| 3308 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3309 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3310 | image->colormap[i].alpha=(unsigned short) i; |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3311 | qsort((void *) image->colormap,image->colors,sizeof(PixelInfo), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3312 | IntensityCompare); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3313 | colormap=(PixelInfo *) AcquireQuantumMemory(image->colors, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3314 | sizeof(*colormap)); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3315 | if (colormap == (PixelInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3316 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3317 | image->filename); |
| 3318 | j=0; |
| 3319 | colormap[j]=image->colormap[0]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3320 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3321 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3322 | if (IsPixelInfoEquivalent(&colormap[j],&image->colormap[i]) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3323 | { |
| 3324 | j++; |
| 3325 | colormap[j]=image->colormap[i]; |
| 3326 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3327 | colormap_index[(ssize_t) image->colormap[i].alpha]=j; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3328 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3329 | image->colors=(size_t) (j+1); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3330 | image->colormap=(PixelInfo *) RelinquishMagickMemory(image->colormap); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3331 | image->colormap=colormap; |
| 3332 | status=MagickTrue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3333 | image_view=AcquireCacheView(image); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3334 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 3335 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3336 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3337 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3338 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3339 | register Quantum |
cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 3340 | *restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3341 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 3342 | register ssize_t |
| 3343 | x; |
| 3344 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3345 | if (status == MagickFalse) |
| 3346 | continue; |
| 3347 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 3348 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3349 | { |
| 3350 | status=MagickFalse; |
| 3351 | continue; |
| 3352 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3353 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3354 | { |
| 3355 | SetPixelIndex(image,(Quantum) colormap_index[ScaleQuantumToMap( |
| 3356 | GetPixelIndex(image,q))],q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 3357 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3358 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3359 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 3360 | status=MagickFalse; |
| 3361 | } |
| 3362 | image_view=DestroyCacheView(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3363 | colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3364 | image->type=GrayscaleType; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3365 | if (IsImageMonochrome(image,&image->exception) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3366 | image->type=BilevelType; |
| 3367 | return(status); |
| 3368 | } |