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