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 % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % July 1992 % |
| 18 | % % |
| 19 | % % |
Cristy | d842011 | 2021-01-01 14:52:00 -0500 | [diff] [blame] | 20 | % Copyright 1999-2021 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 | % % |
Cristy | 9ddfcca | 2018-09-09 19:46:34 -0400 | [diff] [blame] | 26 | % https://imagemagick.org/script/license.php % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 |
cristy | 18856c5 | 2014-03-04 14:04:02 +0000 | [diff] [blame] | 64 | % these cubes are defined by the coordinate of two opposite vertices (vertex |
cristy | 6a91c18 | 2014-03-04 14:05:01 +0000 | [diff] [blame] | 65 | % nearest the origin in RGB space and the vertex farthest from the origin). |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 66 | % |
| 67 | % The tree's root node represents the entire domain, (0,0,0) through |
| 68 | % (Cmax,Cmax,Cmax). Each lower level in the tree is generated by |
| 69 | % subdividing one node's cube into eight smaller cubes of equal size. |
| 70 | % This corresponds to bisecting the parent cube with planes passing |
| 71 | % through the midpoints of each edge. |
| 72 | % |
| 73 | % The basic algorithm operates in three phases: Classification, |
| 74 | % Reduction, and Assignment. Classification builds a color description |
| 75 | % tree for the image. Reduction collapses the tree until the number it |
| 76 | % represents, at most, the number of colors desired in the output image. |
| 77 | % Assignment defines the output image's color map and sets each pixel's |
| 78 | % color by restorage_class in the reduced tree. Our goal is to minimize |
| 79 | % the numerical discrepancies between the original colors and quantized |
| 80 | % colors (quantization error). |
| 81 | % |
| 82 | % Classification begins by initializing a color description tree of |
| 83 | % sufficient depth to represent each possible input color in a leaf. |
| 84 | % However, it is impractical to generate a fully-formed color description |
| 85 | % tree in the storage_class phase for realistic values of Cmax. If |
| 86 | % colors components in the input image are quantized to k-bit precision, |
| 87 | % so that Cmax= 2k-1, the tree would need k levels below the root node to |
| 88 | % allow representing each possible input color in a leaf. This becomes |
| 89 | % prohibitive because the tree's total number of nodes is 1 + |
| 90 | % sum(i=1, k, 8k). |
| 91 | % |
| 92 | % A complete tree would require 19,173,961 nodes for k = 8, Cmax = 255. |
| 93 | % Therefore, to avoid building a fully populated tree, QUANTIZE: (1) |
| 94 | % Initializes data structures for nodes only as they are needed; (2) |
| 95 | % Chooses a maximum depth for the tree as a function of the desired |
| 96 | % number of colors in the output image (currently log2(colormap size)). |
| 97 | % |
| 98 | % For each pixel in the input image, storage_class scans downward from |
| 99 | % the root of the color description tree. At each level of the tree it |
| 100 | % identifies the single node which represents a cube in RGB space |
| 101 | % containing the pixel's color. It updates the following data for each |
| 102 | % such node: |
| 103 | % |
| 104 | % n1: Number of pixels whose color is contained in the RGB cube which |
| 105 | % this node represents; |
| 106 | % |
| 107 | % n2: Number of pixels whose color is not represented in a node at |
| 108 | % lower depth in the tree; initially, n2 = 0 for all nodes except |
| 109 | % leaves of the tree. |
| 110 | % |
| 111 | % Sr, Sg, Sb: Sums of the red, green, and blue component values for all |
| 112 | % pixels not classified at a lower depth. The combination of these sums |
Cristy | 14b1a18 | 2017-07-21 12:29:32 -0400 | [diff] [blame] | 113 | % and n2 will ultimately characterize the mean color of a set of pixels |
| 114 | % represented by this node. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 115 | % |
| 116 | % E: the distance squared in RGB space between each pixel contained |
| 117 | % within a node and the nodes' center. This represents the |
| 118 | % quantization error for a node. |
| 119 | % |
| 120 | % Reduction repeatedly prunes the tree until the number of nodes with n2 |
| 121 | % > 0 is less than or equal to the maximum number of colors allowed in |
| 122 | % the output image. On any given iteration over the tree, it selects |
| 123 | % those nodes whose E count is minimal for pruning and merges their color |
| 124 | % statistics upward. It uses a pruning threshold, Ep, to govern node |
| 125 | % selection as follows: |
| 126 | % |
| 127 | % Ep = 0 |
| 128 | % while number of nodes with (n2 > 0) > required maximum number of colors |
| 129 | % prune all nodes such that E <= Ep |
| 130 | % Set Ep to minimum E in remaining nodes |
| 131 | % |
| 132 | % This has the effect of minimizing any quantization error when merging |
| 133 | % two nodes together. |
| 134 | % |
| 135 | % When a node to be pruned has offspring, the pruning procedure invokes |
| 136 | % itself recursively in order to prune the tree from the leaves upward. |
| 137 | % n2, Sr, Sg, and Sb in a node being pruned are always added to the |
| 138 | % corresponding data in that node's parent. This retains the pruned |
| 139 | % node's color characteristics for later averaging. |
| 140 | % |
| 141 | % For each node, n2 pixels exist for which that node represents the |
| 142 | % smallest volume in RGB space containing those pixel's colors. When n2 |
| 143 | % > 0 the node will uniquely define a color in the output image. At the |
| 144 | % beginning of reduction, n2 = 0 for all nodes except a the leaves of |
| 145 | % the tree which represent colors present in the input image. |
| 146 | % |
| 147 | % The other pixel count, n1, indicates the total number of colors within |
| 148 | % the cubic volume which the node represents. This includes n1 - n2 |
| 149 | % pixels whose colors should be defined by nodes at a lower level in the |
| 150 | % tree. |
| 151 | % |
| 152 | % Assignment generates the output image from the pruned tree. The output |
| 153 | % image consists of two parts: (1) A color map, which is an array of |
| 154 | % color descriptions (RGB triples) for each color present in the output |
| 155 | % image; (2) A pixel array, which represents each pixel as an index |
| 156 | % into the color map array. |
| 157 | % |
| 158 | % First, the assignment phase makes one pass over the pruned color |
| 159 | % description tree to establish the image's color map. For each node |
| 160 | % with n2 > 0, it divides Sr, Sg, and Sb by n2 . This produces the mean |
| 161 | % color of all pixels that classify no lower than this node. Each of |
| 162 | % these colors becomes an entry in the color map. |
| 163 | % |
| 164 | % Finally, the assignment phase reclassifies each pixel in the pruned |
| 165 | % tree to identify the deepest node containing the pixel's color. The |
| 166 | % pixel's value in the pixel array becomes the index of this node's mean |
| 167 | % color in the color map. |
| 168 | % |
| 169 | % This method is based on a similar algorithm written by Paul Raveling. |
| 170 | % |
| 171 | */ |
| 172 | |
| 173 | /* |
| 174 | Include declarations. |
| 175 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 176 | #include "MagickCore/studio.h" |
Cristy | dd1a91a | 2018-06-17 09:21:49 -0400 | [diff] [blame] | 177 | #include "MagickCore/artifact.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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 | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 185 | #include "MagickCore/compare.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 186 | #include "MagickCore/enhance.h" |
| 187 | #include "MagickCore/exception.h" |
| 188 | #include "MagickCore/exception-private.h" |
| 189 | #include "MagickCore/histogram.h" |
| 190 | #include "MagickCore/image.h" |
| 191 | #include "MagickCore/image-private.h" |
| 192 | #include "MagickCore/list.h" |
| 193 | #include "MagickCore/memory_.h" |
Dirk Lemstra | 06344a0 | 2017-10-15 10:10:01 +0200 | [diff] [blame] | 194 | #include "MagickCore/memory-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 195 | #include "MagickCore/monitor.h" |
| 196 | #include "MagickCore/monitor-private.h" |
| 197 | #include "MagickCore/option.h" |
| 198 | #include "MagickCore/pixel-accessor.h" |
cristy | 1d1b10f | 2012-06-02 20:02:55 +0000 | [diff] [blame] | 199 | #include "MagickCore/pixel-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 200 | #include "MagickCore/quantize.h" |
| 201 | #include "MagickCore/quantum.h" |
| 202 | #include "MagickCore/quantum-private.h" |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 203 | #include "MagickCore/random_.h" |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 204 | #include "MagickCore/resource_.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 205 | #include "MagickCore/string_.h" |
Cristy | dd1a91a | 2018-06-17 09:21:49 -0400 | [diff] [blame] | 206 | #include "MagickCore/string-private.h" |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 207 | #include "MagickCore/thread-private.h" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | Define declarations. |
| 211 | */ |
cristy | e128751 | 2010-06-19 17:38:25 +0000 | [diff] [blame] | 212 | #if !defined(__APPLE__) && !defined(TARGET_OS_IPHONE) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 213 | #define CacheShift 2 |
cristy | e128751 | 2010-06-19 17:38:25 +0000 | [diff] [blame] | 214 | #else |
| 215 | #define CacheShift 3 |
| 216 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 217 | #define ErrorQueueLength 16 |
| 218 | #define MaxNodes 266817 |
| 219 | #define MaxTreeDepth 8 |
| 220 | #define NodesInAList 1920 |
| 221 | |
| 222 | /* |
| 223 | Typdef declarations. |
| 224 | */ |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 225 | typedef struct _DoublePixelPacket |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 226 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 227 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 228 | red, |
| 229 | green, |
| 230 | blue, |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 231 | alpha; |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 232 | } DoublePixelPacket; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 233 | |
| 234 | typedef struct _NodeInfo |
| 235 | { |
| 236 | struct _NodeInfo |
| 237 | *parent, |
| 238 | *child[16]; |
| 239 | |
| 240 | MagickSizeType |
| 241 | number_unique; |
| 242 | |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 243 | DoublePixelPacket |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 244 | total_color; |
| 245 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 246 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 247 | quantize_error; |
| 248 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 249 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 250 | color_number, |
| 251 | id, |
| 252 | level; |
| 253 | } NodeInfo; |
| 254 | |
| 255 | typedef struct _Nodes |
| 256 | { |
| 257 | NodeInfo |
| 258 | *nodes; |
| 259 | |
| 260 | struct _Nodes |
| 261 | *next; |
| 262 | } Nodes; |
| 263 | |
| 264 | typedef struct _CubeInfo |
| 265 | { |
| 266 | NodeInfo |
| 267 | *root; |
| 268 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 269 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 270 | colors, |
| 271 | maximum_colors; |
| 272 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 273 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 274 | transparent_index; |
| 275 | |
| 276 | MagickSizeType |
| 277 | transparent_pixels; |
| 278 | |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 279 | DoublePixelPacket |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 280 | target; |
| 281 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 282 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 283 | distance, |
| 284 | pruning_threshold, |
| 285 | next_threshold; |
| 286 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 287 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 288 | nodes, |
| 289 | free_nodes, |
| 290 | color_number; |
| 291 | |
| 292 | NodeInfo |
| 293 | *next_node; |
| 294 | |
| 295 | Nodes |
| 296 | *node_queue; |
| 297 | |
cristy | a321eb7 | 2013-06-23 10:42:37 +0000 | [diff] [blame] | 298 | MemoryInfo |
| 299 | *memory_info; |
| 300 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 301 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 302 | *cache; |
| 303 | |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 304 | DoublePixelPacket |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 305 | error[ErrorQueueLength]; |
| 306 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 307 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 308 | weights[ErrorQueueLength]; |
| 309 | |
| 310 | QuantizeInfo |
| 311 | *quantize_info; |
| 312 | |
| 313 | MagickBooleanType |
| 314 | associate_alpha; |
| 315 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 316 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 317 | x, |
| 318 | y; |
| 319 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 320 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 321 | depth; |
| 322 | |
| 323 | MagickOffsetType |
| 324 | offset; |
| 325 | |
| 326 | MagickSizeType |
| 327 | span; |
| 328 | } CubeInfo; |
| 329 | |
| 330 | /* |
| 331 | Method prototypes. |
| 332 | */ |
| 333 | static CubeInfo |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 334 | *GetCubeInfo(const QuantizeInfo *,const size_t,const size_t); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 335 | |
| 336 | static NodeInfo |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 337 | *GetNodeInfo(CubeInfo *,const size_t,const size_t,NodeInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 338 | |
| 339 | static MagickBooleanType |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 340 | AssignImageColors(Image *,CubeInfo *,ExceptionInfo *), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 341 | ClassifyImageColors(CubeInfo *,const Image *,ExceptionInfo *), |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 342 | DitherImage(Image *,CubeInfo *,ExceptionInfo *), |
Dirk Lemstra | 25229f2 | 2020-04-27 15:01:47 +0200 | [diff] [blame] | 343 | SetGrayscaleImage(Image *,ExceptionInfo *), |
| 344 | SetImageColormap(Image *,CubeInfo *,ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 345 | |
| 346 | static void |
| 347 | ClosestColor(const Image *,CubeInfo *,const NodeInfo *), |
Dirk Lemstra | 25229f2 | 2020-04-27 15:01:47 +0200 | [diff] [blame] | 348 | DefineImageColormap(Image *,CubeInfo *,NodeInfo *), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 349 | DestroyCubeInfo(CubeInfo *), |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 350 | PruneLevel(CubeInfo *,const NodeInfo *), |
| 351 | PruneToCubeDepth(CubeInfo *,const NodeInfo *), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 352 | ReduceImageColors(const Image *,CubeInfo *); |
| 353 | |
| 354 | /* |
| 355 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 356 | % % |
| 357 | % % |
| 358 | % % |
| 359 | % A c q u i r e Q u a n t i z e I n f o % |
| 360 | % % |
| 361 | % % |
| 362 | % % |
| 363 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 364 | % |
| 365 | % AcquireQuantizeInfo() allocates the QuantizeInfo structure. |
| 366 | % |
| 367 | % The format of the AcquireQuantizeInfo method is: |
| 368 | % |
| 369 | % QuantizeInfo *AcquireQuantizeInfo(const ImageInfo *image_info) |
| 370 | % |
| 371 | % A description of each parameter follows: |
| 372 | % |
| 373 | % o image_info: the image info. |
| 374 | % |
| 375 | */ |
| 376 | MagickExport QuantizeInfo *AcquireQuantizeInfo(const ImageInfo *image_info) |
| 377 | { |
| 378 | QuantizeInfo |
| 379 | *quantize_info; |
| 380 | |
Dirk Lemstra | 06344a0 | 2017-10-15 10:10:01 +0200 | [diff] [blame] | 381 | quantize_info=(QuantizeInfo *) AcquireCriticalMemory(sizeof(*quantize_info)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 382 | GetQuantizeInfo(quantize_info); |
| 383 | if (image_info != (ImageInfo *) NULL) |
| 384 | { |
| 385 | const char |
| 386 | *option; |
| 387 | |
cristy | cbda611 | 2012-05-27 20:57:16 +0000 | [diff] [blame] | 388 | quantize_info->dither_method=image_info->dither == MagickFalse ? |
| 389 | NoDitherMethod : RiemersmaDitherMethod; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 390 | option=GetImageOption(image_info,"dither"); |
| 391 | if (option != (const char *) NULL) |
cristy | 042ee78 | 2011-04-22 18:48:30 +0000 | [diff] [blame] | 392 | quantize_info->dither_method=(DitherMethod) ParseCommandOption( |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 393 | MagickDitherOptions,MagickFalse,option); |
| 394 | quantize_info->measure_error=image_info->verbose; |
| 395 | } |
| 396 | return(quantize_info); |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 401 | % % |
| 402 | % % |
| 403 | % % |
| 404 | + A s s i g n I m a g e C o l o r s % |
| 405 | % % |
| 406 | % % |
| 407 | % % |
| 408 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 409 | % |
| 410 | % AssignImageColors() generates the output image from the pruned tree. The |
| 411 | % output image consists of two parts: (1) A color map, which is an array |
| 412 | % of color descriptions (RGB triples) for each color present in the |
| 413 | % output image; (2) A pixel array, which represents each pixel as an |
| 414 | % index into the color map array. |
| 415 | % |
| 416 | % First, the assignment phase makes one pass over the pruned color |
| 417 | % description tree to establish the image's color map. For each node |
| 418 | % with n2 > 0, it divides Sr, Sg, and Sb by n2 . This produces the mean |
| 419 | % color of all pixels that classify no lower than this node. Each of |
| 420 | % these colors becomes an entry in the color map. |
| 421 | % |
| 422 | % Finally, the assignment phase reclassifies each pixel in the pruned |
| 423 | % tree to identify the deepest node containing the pixel's color. The |
| 424 | % pixel's value in the pixel array becomes the index of this node's mean |
| 425 | % color in the color map. |
| 426 | % |
| 427 | % The format of the AssignImageColors() method is: |
| 428 | % |
| 429 | % MagickBooleanType AssignImageColors(Image *image,CubeInfo *cube_info) |
| 430 | % |
| 431 | % A description of each parameter follows. |
| 432 | % |
| 433 | % o image: the image. |
| 434 | % |
| 435 | % o cube_info: A pointer to the Cube structure. |
| 436 | % |
| 437 | */ |
| 438 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 439 | static inline void AssociateAlphaPixel(const Image *image, |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 440 | const CubeInfo *cube_info,const Quantum *pixel,DoublePixelPacket *alpha_pixel) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 441 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 442 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 443 | alpha; |
| 444 | |
| 445 | if ((cube_info->associate_alpha == MagickFalse) || |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 446 | (GetPixelAlpha(image,pixel) == OpaqueAlpha)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 447 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 448 | alpha_pixel->red=(double) GetPixelRed(image,pixel); |
| 449 | alpha_pixel->green=(double) GetPixelGreen(image,pixel); |
| 450 | alpha_pixel->blue=(double) GetPixelBlue(image,pixel); |
| 451 | alpha_pixel->alpha=(double) GetPixelAlpha(image,pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 452 | return; |
| 453 | } |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 454 | alpha=(double) (QuantumScale*GetPixelAlpha(image,pixel)); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 455 | alpha_pixel->red=alpha*GetPixelRed(image,pixel); |
| 456 | alpha_pixel->green=alpha*GetPixelGreen(image,pixel); |
| 457 | alpha_pixel->blue=alpha*GetPixelBlue(image,pixel); |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 458 | alpha_pixel->alpha=(double) GetPixelAlpha(image,pixel); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 459 | } |
| 460 | |
cristy | b0de93f | 2013-05-03 13:39:25 +0000 | [diff] [blame] | 461 | static inline void AssociateAlphaPixelInfo(const CubeInfo *cube_info, |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 462 | const PixelInfo *pixel,DoublePixelPacket *alpha_pixel) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 463 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 464 | double |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 465 | alpha; |
| 466 | |
| 467 | if ((cube_info->associate_alpha == MagickFalse) || |
| 468 | (pixel->alpha == OpaqueAlpha)) |
| 469 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 470 | alpha_pixel->red=(double) pixel->red; |
| 471 | alpha_pixel->green=(double) pixel->green; |
| 472 | alpha_pixel->blue=(double) pixel->blue; |
| 473 | alpha_pixel->alpha=(double) pixel->alpha; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 474 | return; |
| 475 | } |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 476 | alpha=(double) (QuantumScale*pixel->alpha); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 477 | alpha_pixel->red=alpha*pixel->red; |
| 478 | alpha_pixel->green=alpha*pixel->green; |
| 479 | alpha_pixel->blue=alpha*pixel->blue; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 480 | alpha_pixel->alpha=(double) pixel->alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 481 | } |
| 482 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 483 | static inline size_t ColorToNodeId(const CubeInfo *cube_info, |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 484 | const DoublePixelPacket *pixel,size_t index) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 485 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 486 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 487 | id; |
| 488 | |
cristy | 6f7e042 | 2012-12-25 20:04:53 +0000 | [diff] [blame] | 489 | id=(size_t) (((ScaleQuantumToChar(ClampPixel(pixel->red)) >> index) & 0x01) | |
| 490 | ((ScaleQuantumToChar(ClampPixel(pixel->green)) >> index) & 0x01) << 1 | |
| 491 | ((ScaleQuantumToChar(ClampPixel(pixel->blue)) >> index) & 0x01) << 2); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 492 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 6f7e042 | 2012-12-25 20:04:53 +0000 | [diff] [blame] | 493 | id|=((ScaleQuantumToChar(ClampPixel(pixel->alpha)) >> index) & 0x1) << 3; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 494 | return(id); |
| 495 | } |
| 496 | |
dirk | e0e9881 | 2015-07-06 20:52:58 +0000 | [diff] [blame] | 497 | static MagickBooleanType AssignImageColors(Image *image,CubeInfo *cube_info, |
| 498 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 499 | { |
dirk | e0e9881 | 2015-07-06 20:52:58 +0000 | [diff] [blame] | 500 | #define AssignImageTag "Assign/Image" |
| 501 | |
Cristy | 470e71c | 2018-03-02 20:19:10 -0500 | [diff] [blame] | 502 | ColorspaceType |
| 503 | colorspace; |
| 504 | |
dirk | e0e9881 | 2015-07-06 20:52:58 +0000 | [diff] [blame] | 505 | ssize_t |
| 506 | y; |
| 507 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 508 | /* |
| 509 | Allocate image colormap. |
| 510 | */ |
Cristy | 470e71c | 2018-03-02 20:19:10 -0500 | [diff] [blame] | 511 | colorspace=image->colorspace; |
Cristy | a141b12 | 2018-03-02 20:12:55 -0500 | [diff] [blame] | 512 | if (cube_info->quantize_info->colorspace != UndefinedColorspace) |
Cristy | 0b0d735 | 2015-12-06 18:47:24 -0500 | [diff] [blame] | 513 | (void) TransformImageColorspace(image,cube_info->quantize_info->colorspace, |
| 514 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 515 | cube_info->transparent_pixels=0; |
| 516 | cube_info->transparent_index=(-1); |
Dirk Lemstra | 25229f2 | 2020-04-27 15:01:47 +0200 | [diff] [blame] | 517 | if (SetImageColormap(image,cube_info,exception) == MagickFalse) |
| 518 | return(MagickFalse); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 519 | /* |
| 520 | Create a reduced color image. |
| 521 | */ |
cristy | f2a82ee | 2014-05-26 17:49:54 +0000 | [diff] [blame] | 522 | if (cube_info->quantize_info->dither_method != NoDitherMethod) |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 523 | (void) DitherImage(image,cube_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 524 | else |
| 525 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 526 | CacheView |
| 527 | *image_view; |
| 528 | |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 529 | MagickBooleanType |
| 530 | status; |
| 531 | |
| 532 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 533 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 534 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
Cristy | 38b42e1 | 2018-03-18 10:13:01 -0400 | [diff] [blame] | 535 | #pragma omp parallel for schedule(static) shared(status) \ |
Cristy | 8255ca9 | 2017-10-09 08:37:35 -0400 | [diff] [blame] | 536 | magick_number_threads(image,image,image->rows,1) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 537 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 538 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 539 | { |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 540 | CubeInfo |
| 541 | cube; |
| 542 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 543 | Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 544 | *magick_restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 545 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 546 | ssize_t |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 547 | x; |
| 548 | |
| 549 | ssize_t |
| 550 | count; |
| 551 | |
| 552 | if (status == MagickFalse) |
| 553 | continue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 554 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1, |
| 555 | exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 556 | if (q == (Quantum *) NULL) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 557 | { |
| 558 | status=MagickFalse; |
| 559 | continue; |
| 560 | } |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 561 | cube=(*cube_info); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 562 | for (x=0; x < (ssize_t) image->columns; x+=count) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 563 | { |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 564 | DoublePixelPacket |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 565 | pixel; |
| 566 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 567 | const NodeInfo |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 568 | *node_info; |
| 569 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 570 | ssize_t |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 571 | i; |
| 572 | |
| 573 | size_t |
| 574 | id, |
| 575 | index; |
| 576 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 577 | /* |
| 578 | Identify the deepest node containing the pixel's color. |
| 579 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 580 | for (count=1; (x+count) < (ssize_t) image->columns; count++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 581 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 582 | PixelInfo |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 583 | packet; |
| 584 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 585 | GetPixelInfoPixel(image,q+count*GetPixelChannels(image),&packet); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 586 | if (IsPixelEquivalent(image,q,&packet) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 587 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 588 | } |
| 589 | AssociateAlphaPixel(image,&cube,q,&pixel); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 590 | node_info=cube.root; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 591 | for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 592 | { |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 593 | id=ColorToNodeId(&cube,&pixel,index); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 594 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 595 | break; |
| 596 | node_info=node_info->child[id]; |
| 597 | } |
| 598 | /* |
| 599 | Find closest color among siblings and their children. |
| 600 | */ |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 601 | cube.target=pixel; |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 602 | cube.distance=(double) (4.0*(QuantumRange+1.0)*(QuantumRange+1.0)+ |
| 603 | 1.0); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 604 | ClosestColor(image,&cube,node_info->parent); |
| 605 | index=cube.color_number; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 606 | for (i=0; i < (ssize_t) count; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 607 | { |
| 608 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 609 | SetPixelIndex(image,(Quantum) index,q); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 610 | if (cube.quantize_info->measure_error == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 611 | { |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 612 | SetPixelRed(image,ClampToQuantum( |
| 613 | image->colormap[index].red),q); |
| 614 | SetPixelGreen(image,ClampToQuantum( |
| 615 | image->colormap[index].green),q); |
| 616 | SetPixelBlue(image,ClampToQuantum( |
| 617 | image->colormap[index].blue),q); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 618 | if (cube.associate_alpha != MagickFalse) |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 619 | SetPixelAlpha(image,ClampToQuantum( |
| 620 | image->colormap[index].alpha),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 621 | } |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 622 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 626 | status=MagickFalse; |
| 627 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 628 | { |
| 629 | MagickBooleanType |
| 630 | proceed; |
| 631 | |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 632 | proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) y, |
| 633 | image->rows); |
| 634 | if (proceed == MagickFalse) |
| 635 | status=MagickFalse; |
| 636 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 637 | } |
| 638 | image_view=DestroyCacheView(image_view); |
| 639 | } |
dirk | e0e9881 | 2015-07-06 20:52:58 +0000 | [diff] [blame] | 640 | if (cube_info->quantize_info->measure_error != MagickFalse) |
| 641 | (void) GetImageQuantizeError(image,exception); |
| 642 | if ((cube_info->quantize_info->number_colors == 2) && |
Cristy | beb4c2b | 2017-12-26 19:43:17 -0500 | [diff] [blame] | 643 | ((cube_info->quantize_info->colorspace == LinearGRAYColorspace) || |
| 644 | (cube_info->quantize_info->colorspace == GRAYColorspace))) |
dirk | e0e9881 | 2015-07-06 20:52:58 +0000 | [diff] [blame] | 645 | { |
| 646 | double |
| 647 | intensity; |
| 648 | |
dirk | e0e9881 | 2015-07-06 20:52:58 +0000 | [diff] [blame] | 649 | /* |
| 650 | Monochrome image. |
| 651 | */ |
Cristy | 96aa756 | 2020-01-12 10:31:04 -0500 | [diff] [blame] | 652 | intensity=GetPixelInfoLuma(image->colormap+0) < QuantumRange/2.0 ? 0.0 : |
Cristy | b0799c4 | 2019-12-27 12:57:53 -0500 | [diff] [blame] | 653 | QuantumRange; |
Cristy | 971f2e1 | 2020-01-03 17:20:25 -0500 | [diff] [blame] | 654 | if (image->colors > 1) |
| 655 | { |
| 656 | intensity=0.0; |
| 657 | if (GetPixelInfoLuma(image->colormap+0) > |
| 658 | GetPixelInfoLuma(image->colormap+1)) |
| 659 | intensity=(double) QuantumRange; |
| 660 | } |
Cristy | 0b0d735 | 2015-12-06 18:47:24 -0500 | [diff] [blame] | 661 | image->colormap[0].red=intensity; |
| 662 | image->colormap[0].green=intensity; |
| 663 | image->colormap[0].blue=intensity; |
Cristy | d95b619 | 2016-01-24 18:45:59 -0500 | [diff] [blame] | 664 | if (image->colors > 1) |
| 665 | { |
| 666 | image->colormap[1].red=(double) QuantumRange-intensity; |
| 667 | image->colormap[1].green=(double) QuantumRange-intensity; |
| 668 | image->colormap[1].blue=(double) QuantumRange-intensity; |
| 669 | } |
dirk | e0e9881 | 2015-07-06 20:52:58 +0000 | [diff] [blame] | 670 | } |
cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 671 | (void) SyncImage(image,exception); |
Cristy | a59574a | 2018-03-02 20:26:16 -0500 | [diff] [blame] | 672 | if ((cube_info->quantize_info->colorspace != UndefinedColorspace) && |
Cristy | d61de6b | 2018-03-02 20:28:11 -0500 | [diff] [blame] | 673 | (IssRGBCompatibleColorspace(colorspace) == MagickFalse)) |
Cristy | 470e71c | 2018-03-02 20:19:10 -0500 | [diff] [blame] | 674 | (void) TransformImageColorspace(image,colorspace,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 675 | return(MagickTrue); |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 680 | % % |
| 681 | % % |
| 682 | % % |
| 683 | + C l a s s i f y I m a g e C o l o r s % |
| 684 | % % |
| 685 | % % |
| 686 | % % |
| 687 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 688 | % |
| 689 | % ClassifyImageColors() begins by initializing a color description tree |
| 690 | % of sufficient depth to represent each possible input color in a leaf. |
| 691 | % However, it is impractical to generate a fully-formed color |
| 692 | % description tree in the storage_class phase for realistic values of |
| 693 | % Cmax. If colors components in the input image are quantized to k-bit |
| 694 | % precision, so that Cmax= 2k-1, the tree would need k levels below the |
| 695 | % root node to allow representing each possible input color in a leaf. |
| 696 | % This becomes prohibitive because the tree's total number of nodes is |
| 697 | % 1 + sum(i=1,k,8k). |
| 698 | % |
| 699 | % A complete tree would require 19,173,961 nodes for k = 8, Cmax = 255. |
| 700 | % Therefore, to avoid building a fully populated tree, QUANTIZE: (1) |
| 701 | % Initializes data structures for nodes only as they are needed; (2) |
| 702 | % Chooses a maximum depth for the tree as a function of the desired |
| 703 | % number of colors in the output image (currently log2(colormap size)). |
| 704 | % |
| 705 | % For each pixel in the input image, storage_class scans downward from |
| 706 | % the root of the color description tree. At each level of the tree it |
| 707 | % identifies the single node which represents a cube in RGB space |
| 708 | % containing It updates the following data for each such node: |
| 709 | % |
| 710 | % n1 : Number of pixels whose color is contained in the RGB cube |
| 711 | % which this node represents; |
| 712 | % |
| 713 | % n2 : Number of pixels whose color is not represented in a node at |
| 714 | % lower depth in the tree; initially, n2 = 0 for all nodes except |
| 715 | % leaves of the tree. |
| 716 | % |
| 717 | % Sr, Sg, Sb : Sums of the red, green, and blue component values for |
| 718 | % all pixels not classified at a lower depth. The combination of |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 719 | % these sums and n2 will ultimately characterize the mean color of a |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 720 | % set of pixels represented by this node. |
| 721 | % |
| 722 | % E: the distance squared in RGB space between each pixel contained |
| 723 | % within a node and the nodes' center. This represents the quantization |
| 724 | % error for a node. |
| 725 | % |
| 726 | % The format of the ClassifyImageColors() method is: |
| 727 | % |
| 728 | % MagickBooleanType ClassifyImageColors(CubeInfo *cube_info, |
| 729 | % const Image *image,ExceptionInfo *exception) |
| 730 | % |
| 731 | % A description of each parameter follows. |
| 732 | % |
| 733 | % o cube_info: A pointer to the Cube structure. |
| 734 | % |
| 735 | % o image: the image. |
| 736 | % |
| 737 | */ |
| 738 | |
| 739 | static inline void SetAssociatedAlpha(const Image *image,CubeInfo *cube_info) |
| 740 | { |
| 741 | MagickBooleanType |
| 742 | associate_alpha; |
| 743 | |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 744 | associate_alpha=image->alpha_trait == BlendPixelTrait ? MagickTrue : |
cristy | b0a657e | 2012-08-29 00:45:37 +0000 | [diff] [blame] | 745 | MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 746 | if ((cube_info->quantize_info->number_colors == 2) && |
Cristy | beb4c2b | 2017-12-26 19:43:17 -0500 | [diff] [blame] | 747 | ((cube_info->quantize_info->colorspace == LinearGRAYColorspace) || |
| 748 | (cube_info->quantize_info->colorspace == GRAYColorspace))) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 749 | associate_alpha=MagickFalse; |
| 750 | cube_info->associate_alpha=associate_alpha; |
| 751 | } |
| 752 | |
| 753 | static MagickBooleanType ClassifyImageColors(CubeInfo *cube_info, |
| 754 | const Image *image,ExceptionInfo *exception) |
| 755 | { |
| 756 | #define ClassifyImageTag "Classify/Image" |
| 757 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 758 | CacheView |
| 759 | *image_view; |
| 760 | |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 761 | DoublePixelPacket |
Cristy | 2392b4b | 2015-12-04 18:28:23 -0500 | [diff] [blame] | 762 | error, |
| 763 | mid, |
| 764 | midpoint, |
| 765 | pixel; |
| 766 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 767 | MagickBooleanType |
| 768 | proceed; |
| 769 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 770 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 771 | bisect; |
| 772 | |
| 773 | NodeInfo |
| 774 | *node_info; |
| 775 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 776 | size_t |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 777 | count, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 778 | id, |
| 779 | index, |
| 780 | level; |
| 781 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 782 | ssize_t |
| 783 | y; |
| 784 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 785 | /* |
| 786 | Classify the first cube_info->maximum_colors colors to a tree depth of 8. |
| 787 | */ |
| 788 | SetAssociatedAlpha(image,cube_info); |
Cristy | c6c450d | 2020-01-18 11:21:12 -0500 | [diff] [blame] | 789 | if (cube_info->quantize_info->colorspace != image->colorspace) |
| 790 | { |
| 791 | if ((cube_info->quantize_info->colorspace != UndefinedColorspace) && |
| 792 | (cube_info->quantize_info->colorspace != CMYKColorspace)) |
| 793 | (void) TransformImageColorspace((Image *) image, |
| 794 | cube_info->quantize_info->colorspace,exception); |
| 795 | else |
| 796 | if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse) |
| 797 | (void) TransformImageColorspace((Image *) image,sRGBColorspace, |
| 798 | exception); |
| 799 | } |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 800 | midpoint.red=(double) QuantumRange/2.0; |
| 801 | midpoint.green=(double) QuantumRange/2.0; |
| 802 | midpoint.blue=(double) QuantumRange/2.0; |
| 803 | midpoint.alpha=(double) QuantumRange/2.0; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 804 | error.alpha=0.0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 805 | image_view=AcquireVirtualCacheView(image,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 806 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 807 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 808 | const Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 809 | *magick_restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 810 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 811 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 812 | x; |
| 813 | |
| 814 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 815 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 816 | break; |
| 817 | if (cube_info->nodes > MaxNodes) |
| 818 | { |
| 819 | /* |
| 820 | Prune one level if the color tree is too large. |
| 821 | */ |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 822 | PruneLevel(cube_info,cube_info->root); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 823 | cube_info->depth--; |
| 824 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 825 | for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 826 | { |
| 827 | /* |
| 828 | Start at the root and descend the color cube tree. |
| 829 | */ |
cristy | bb66d9c | 2010-10-09 01:40:31 +0000 | [diff] [blame] | 830 | for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 831 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 832 | PixelInfo |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 833 | packet; |
| 834 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 835 | GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 836 | if (IsPixelEquivalent(image,p,&packet) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 837 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 838 | } |
| 839 | AssociateAlphaPixel(image,cube_info,p,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 840 | index=MaxTreeDepth-1; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 841 | bisect=((double) QuantumRange+1.0)/2.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 842 | mid=midpoint; |
| 843 | node_info=cube_info->root; |
| 844 | for (level=1; level <= MaxTreeDepth; level++) |
| 845 | { |
cristy | b4d27c3 | 2014-09-24 12:59:33 +0000 | [diff] [blame] | 846 | double |
| 847 | distance; |
| 848 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 849 | bisect*=0.5; |
| 850 | id=ColorToNodeId(cube_info,&pixel,index); |
| 851 | mid.red+=(id & 1) != 0 ? bisect : -bisect; |
| 852 | mid.green+=(id & 2) != 0 ? bisect : -bisect; |
| 853 | mid.blue+=(id & 4) != 0 ? bisect : -bisect; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 854 | mid.alpha+=(id & 8) != 0 ? bisect : -bisect; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 855 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 856 | { |
| 857 | /* |
| 858 | Set colors of new node to contain pixel. |
| 859 | */ |
| 860 | node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info); |
| 861 | if (node_info->child[id] == (NodeInfo *) NULL) |
cristy | ac21aa2 | 2014-01-15 00:51:18 +0000 | [diff] [blame] | 862 | { |
| 863 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 864 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| 865 | image->filename); |
| 866 | continue; |
| 867 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 868 | if (level == MaxTreeDepth) |
| 869 | cube_info->colors++; |
| 870 | } |
| 871 | /* |
| 872 | Approximate the quantization error represented by this node. |
| 873 | */ |
| 874 | node_info=node_info->child[id]; |
| 875 | error.red=QuantumScale*(pixel.red-mid.red); |
| 876 | error.green=QuantumScale*(pixel.green-mid.green); |
| 877 | error.blue=QuantumScale*(pixel.blue-mid.blue); |
| 878 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 879 | error.alpha=QuantumScale*(pixel.alpha-mid.alpha); |
cristy | b4d27c3 | 2014-09-24 12:59:33 +0000 | [diff] [blame] | 880 | distance=(double) (error.red*error.red+error.green*error.green+ |
| 881 | error.blue*error.blue+error.alpha*error.alpha); |
Cristy | a7edff6 | 2020-07-14 20:41:04 -0400 | [diff] [blame] | 882 | if (IsNaN(distance) != 0) |
cristy | b4d27c3 | 2014-09-24 12:59:33 +0000 | [diff] [blame] | 883 | distance=0.0; |
| 884 | node_info->quantize_error+=count*sqrt(distance); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 885 | cube_info->root->quantize_error+=node_info->quantize_error; |
| 886 | index--; |
| 887 | } |
| 888 | /* |
| 889 | Sum RGB for this leaf for later derivation of the mean cube color. |
| 890 | */ |
| 891 | node_info->number_unique+=count; |
cristy | 1aeeff3 | 2013-02-21 21:51:24 +0000 | [diff] [blame] | 892 | node_info->total_color.red+=count*QuantumScale*ClampPixel(pixel.red); |
| 893 | node_info->total_color.green+=count*QuantumScale*ClampPixel(pixel.green); |
| 894 | node_info->total_color.blue+=count*QuantumScale*ClampPixel(pixel.blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 895 | if (cube_info->associate_alpha != MagickFalse) |
Cristy | 146c482 | 2015-11-23 21:13:27 -0500 | [diff] [blame] | 896 | node_info->total_color.alpha+=count*QuantumScale* |
| 897 | ClampPixel(pixel.alpha); |
| 898 | else |
| 899 | node_info->total_color.alpha+=count*QuantumScale* |
Cristy | 8847db9 | 2018-03-10 16:55:54 -0500 | [diff] [blame] | 900 | ClampPixel((MagickRealType) OpaqueAlpha); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 901 | p+=count*GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 902 | } |
| 903 | if (cube_info->colors > cube_info->maximum_colors) |
| 904 | { |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 905 | PruneToCubeDepth(cube_info,cube_info->root); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 906 | break; |
| 907 | } |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 908 | proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y, |
| 909 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 910 | if (proceed == MagickFalse) |
| 911 | break; |
| 912 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 913 | for (y++; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 914 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 915 | const Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 916 | *magick_restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 917 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 918 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 919 | x; |
| 920 | |
| 921 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 922 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 923 | break; |
| 924 | if (cube_info->nodes > MaxNodes) |
| 925 | { |
| 926 | /* |
| 927 | Prune one level if the color tree is too large. |
| 928 | */ |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 929 | PruneLevel(cube_info,cube_info->root); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 930 | cube_info->depth--; |
| 931 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 932 | for (x=0; x < (ssize_t) image->columns; x+=(ssize_t) count) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 933 | { |
| 934 | /* |
| 935 | Start at the root and descend the color cube tree. |
| 936 | */ |
cristy | bb66d9c | 2010-10-09 01:40:31 +0000 | [diff] [blame] | 937 | for (count=1; (x+(ssize_t) count) < (ssize_t) image->columns; count++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 938 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 939 | PixelInfo |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 940 | packet; |
| 941 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 942 | GetPixelInfoPixel(image,p+count*GetPixelChannels(image),&packet); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 943 | if (IsPixelEquivalent(image,p,&packet) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 944 | break; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 945 | } |
| 946 | AssociateAlphaPixel(image,cube_info,p,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 947 | index=MaxTreeDepth-1; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 948 | bisect=((double) QuantumRange+1.0)/2.0; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 949 | mid=midpoint; |
| 950 | node_info=cube_info->root; |
| 951 | for (level=1; level <= cube_info->depth; level++) |
| 952 | { |
cristy | b4d27c3 | 2014-09-24 12:59:33 +0000 | [diff] [blame] | 953 | double |
| 954 | distance; |
| 955 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 956 | bisect*=0.5; |
| 957 | id=ColorToNodeId(cube_info,&pixel,index); |
| 958 | mid.red+=(id & 1) != 0 ? bisect : -bisect; |
| 959 | mid.green+=(id & 2) != 0 ? bisect : -bisect; |
| 960 | mid.blue+=(id & 4) != 0 ? bisect : -bisect; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 961 | mid.alpha+=(id & 8) != 0 ? bisect : -bisect; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 962 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 963 | { |
| 964 | /* |
| 965 | Set colors of new node to contain pixel. |
| 966 | */ |
| 967 | node_info->child[id]=GetNodeInfo(cube_info,id,level,node_info); |
| 968 | if (node_info->child[id] == (NodeInfo *) NULL) |
cristy | ac21aa2 | 2014-01-15 00:51:18 +0000 | [diff] [blame] | 969 | { |
| 970 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 971 | ResourceLimitError,"MemoryAllocationFailed","%s", |
| 972 | image->filename); |
| 973 | continue; |
| 974 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 975 | if (level == cube_info->depth) |
| 976 | cube_info->colors++; |
| 977 | } |
| 978 | /* |
| 979 | Approximate the quantization error represented by this node. |
| 980 | */ |
| 981 | node_info=node_info->child[id]; |
| 982 | error.red=QuantumScale*(pixel.red-mid.red); |
| 983 | error.green=QuantumScale*(pixel.green-mid.green); |
| 984 | error.blue=QuantumScale*(pixel.blue-mid.blue); |
| 985 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 986 | error.alpha=QuantumScale*(pixel.alpha-mid.alpha); |
cristy | b4d27c3 | 2014-09-24 12:59:33 +0000 | [diff] [blame] | 987 | distance=(double) (error.red*error.red+error.green*error.green+ |
| 988 | error.blue*error.blue+error.alpha*error.alpha); |
Cristy | a7edff6 | 2020-07-14 20:41:04 -0400 | [diff] [blame] | 989 | if (IsNaN(distance) != 0) |
cristy | b4d27c3 | 2014-09-24 12:59:33 +0000 | [diff] [blame] | 990 | distance=0.0; |
| 991 | node_info->quantize_error+=count*sqrt(distance); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 992 | cube_info->root->quantize_error+=node_info->quantize_error; |
| 993 | index--; |
| 994 | } |
| 995 | /* |
| 996 | Sum RGB for this leaf for later derivation of the mean cube color. |
| 997 | */ |
| 998 | node_info->number_unique+=count; |
cristy | 1aeeff3 | 2013-02-21 21:51:24 +0000 | [diff] [blame] | 999 | node_info->total_color.red+=count*QuantumScale*ClampPixel(pixel.red); |
| 1000 | node_info->total_color.green+=count*QuantumScale*ClampPixel(pixel.green); |
| 1001 | node_info->total_color.blue+=count*QuantumScale*ClampPixel(pixel.blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1002 | if (cube_info->associate_alpha != MagickFalse) |
Cristy | 146c482 | 2015-11-23 21:13:27 -0500 | [diff] [blame] | 1003 | node_info->total_color.alpha+=count*QuantumScale* |
| 1004 | ClampPixel(pixel.alpha); |
| 1005 | else |
| 1006 | node_info->total_color.alpha+=count*QuantumScale* |
Cristy | 8847db9 | 2018-03-10 16:55:54 -0500 | [diff] [blame] | 1007 | ClampPixel((MagickRealType) OpaqueAlpha); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1008 | p+=count*GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1009 | } |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1010 | proceed=SetImageProgress(image,ClassifyImageTag,(MagickOffsetType) y, |
| 1011 | image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1012 | if (proceed == MagickFalse) |
| 1013 | break; |
| 1014 | } |
| 1015 | image_view=DestroyCacheView(image_view); |
Cristy | c6c450d | 2020-01-18 11:21:12 -0500 | [diff] [blame] | 1016 | if (cube_info->quantize_info->colorspace != image->colorspace) |
| 1017 | if ((cube_info->quantize_info->colorspace != UndefinedColorspace) && |
| 1018 | (cube_info->quantize_info->colorspace != CMYKColorspace)) |
| 1019 | (void) TransformImageColorspace((Image *) image,sRGBColorspace,exception); |
cristy | ac21aa2 | 2014-01-15 00:51:18 +0000 | [diff] [blame] | 1020 | return(y < (ssize_t) image->rows ? MagickFalse : MagickTrue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1025 | % % |
| 1026 | % % |
| 1027 | % % |
| 1028 | % C l o n e Q u a n t i z e I n f o % |
| 1029 | % % |
| 1030 | % % |
| 1031 | % % |
| 1032 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1033 | % |
| 1034 | % CloneQuantizeInfo() makes a duplicate of the given quantize info structure, |
| 1035 | % or if quantize info is NULL, a new one. |
| 1036 | % |
| 1037 | % The format of the CloneQuantizeInfo method is: |
| 1038 | % |
| 1039 | % QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info) |
| 1040 | % |
| 1041 | % A description of each parameter follows: |
| 1042 | % |
| 1043 | % o clone_info: Method CloneQuantizeInfo returns a duplicate of the given |
| 1044 | % quantize info, or if image info is NULL a new one. |
| 1045 | % |
| 1046 | % o quantize_info: a structure of type info. |
| 1047 | % |
| 1048 | */ |
| 1049 | MagickExport QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info) |
| 1050 | { |
| 1051 | QuantizeInfo |
| 1052 | *clone_info; |
| 1053 | |
Dirk Lemstra | 06344a0 | 2017-10-15 10:10:01 +0200 | [diff] [blame] | 1054 | clone_info=(QuantizeInfo *) AcquireCriticalMemory(sizeof(*clone_info)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1055 | GetQuantizeInfo(clone_info); |
| 1056 | if (quantize_info == (QuantizeInfo *) NULL) |
| 1057 | return(clone_info); |
| 1058 | clone_info->number_colors=quantize_info->number_colors; |
| 1059 | clone_info->tree_depth=quantize_info->tree_depth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1060 | clone_info->dither_method=quantize_info->dither_method; |
| 1061 | clone_info->colorspace=quantize_info->colorspace; |
| 1062 | clone_info->measure_error=quantize_info->measure_error; |
| 1063 | return(clone_info); |
| 1064 | } |
| 1065 | |
| 1066 | /* |
| 1067 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1068 | % % |
| 1069 | % % |
| 1070 | % % |
| 1071 | + C l o s e s t C o l o r % |
| 1072 | % % |
| 1073 | % % |
| 1074 | % % |
| 1075 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1076 | % |
| 1077 | % ClosestColor() traverses the color cube tree at a particular node and |
| 1078 | % determines which colormap entry best represents the input color. |
| 1079 | % |
| 1080 | % The format of the ClosestColor method is: |
| 1081 | % |
| 1082 | % void ClosestColor(const Image *image,CubeInfo *cube_info, |
| 1083 | % const NodeInfo *node_info) |
| 1084 | % |
| 1085 | % A description of each parameter follows. |
| 1086 | % |
| 1087 | % o image: the image. |
| 1088 | % |
| 1089 | % o cube_info: A pointer to the Cube structure. |
| 1090 | % |
| 1091 | % o node_info: the address of a structure of type NodeInfo which points to a |
| 1092 | % node in the color cube tree that is to be pruned. |
| 1093 | % |
| 1094 | */ |
| 1095 | static void ClosestColor(const Image *image,CubeInfo *cube_info, |
| 1096 | const NodeInfo *node_info) |
| 1097 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1098 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1099 | i; |
| 1100 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1101 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1102 | number_children; |
| 1103 | |
| 1104 | /* |
| 1105 | Traverse any children. |
| 1106 | */ |
| 1107 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1108 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1109 | if (node_info->child[i] != (NodeInfo *) NULL) |
| 1110 | ClosestColor(image,cube_info,node_info->child[i]); |
| 1111 | if (node_info->number_unique != 0) |
| 1112 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1113 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1114 | pixel; |
| 1115 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1116 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1117 | alpha, |
| 1118 | beta, |
| 1119 | distance; |
| 1120 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1121 | DoublePixelPacket |
Cristy | 2392b4b | 2015-12-04 18:28:23 -0500 | [diff] [blame] | 1122 | *magick_restrict q; |
| 1123 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1124 | PixelInfo |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 1125 | *magick_restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1126 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1127 | /* |
| 1128 | Determine if this color is "closest". |
| 1129 | */ |
| 1130 | p=image->colormap+node_info->color_number; |
| 1131 | q=(&cube_info->target); |
| 1132 | alpha=1.0; |
| 1133 | beta=1.0; |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 1134 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1135 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1136 | alpha=(double) (QuantumScale*p->alpha); |
| 1137 | beta=(double) (QuantumScale*q->alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1138 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1139 | pixel=alpha*p->red-beta*q->red; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1140 | distance=pixel*pixel; |
cristy | 36fbc3b | 2011-02-09 02:30:04 +0000 | [diff] [blame] | 1141 | if (distance <= cube_info->distance) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1142 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1143 | pixel=alpha*p->green-beta*q->green; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1144 | distance+=pixel*pixel; |
cristy | 36fbc3b | 2011-02-09 02:30:04 +0000 | [diff] [blame] | 1145 | if (distance <= cube_info->distance) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1146 | { |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1147 | pixel=alpha*p->blue-beta*q->blue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1148 | distance+=pixel*pixel; |
cristy | 36fbc3b | 2011-02-09 02:30:04 +0000 | [diff] [blame] | 1149 | if (distance <= cube_info->distance) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1150 | { |
Cristy | 29e8af7 | 2015-10-17 11:43:59 -0400 | [diff] [blame] | 1151 | if (cube_info->associate_alpha != MagickFalse) |
| 1152 | { |
| 1153 | pixel=p->alpha-q->alpha; |
| 1154 | distance+=pixel*pixel; |
| 1155 | } |
cristy | c408040 | 2011-02-09 02:55:58 +0000 | [diff] [blame] | 1156 | if (distance <= cube_info->distance) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1157 | { |
| 1158 | cube_info->distance=distance; |
| 1159 | cube_info->color_number=node_info->color_number; |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | /* |
| 1168 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1169 | % % |
| 1170 | % % |
| 1171 | % % |
| 1172 | % C o m p r e s s I m a g e C o l o r m a p % |
| 1173 | % % |
| 1174 | % % |
| 1175 | % % |
| 1176 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1177 | % |
| 1178 | % CompressImageColormap() compresses an image colormap by removing any |
| 1179 | % duplicate or unused color entries. |
| 1180 | % |
| 1181 | % The format of the CompressImageColormap method is: |
| 1182 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1183 | % MagickBooleanType CompressImageColormap(Image *image, |
| 1184 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1185 | % |
| 1186 | % A description of each parameter follows: |
| 1187 | % |
| 1188 | % o image: the image. |
| 1189 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1190 | % o exception: return any errors or warnings in this structure. |
| 1191 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1192 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1193 | MagickExport MagickBooleanType CompressImageColormap(Image *image, |
| 1194 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1195 | { |
| 1196 | QuantizeInfo |
| 1197 | quantize_info; |
| 1198 | |
| 1199 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 1200 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1201 | if (image->debug != MagickFalse) |
| 1202 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
Cristy | 14b1a18 | 2017-07-21 12:29:32 -0400 | [diff] [blame] | 1203 | if (IsPaletteImage(image) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1204 | return(MagickFalse); |
| 1205 | GetQuantizeInfo(&quantize_info); |
| 1206 | quantize_info.number_colors=image->colors; |
| 1207 | quantize_info.tree_depth=MaxTreeDepth; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1208 | return(QuantizeImage(&quantize_info,image,exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | /* |
| 1212 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1213 | % % |
| 1214 | % % |
| 1215 | % % |
| 1216 | + D e f i n e I m a g e C o l o r m a p % |
| 1217 | % % |
| 1218 | % % |
| 1219 | % % |
| 1220 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1221 | % |
| 1222 | % DefineImageColormap() traverses the color cube tree and notes each colormap |
| 1223 | % entry. A colormap entry is any node in the color cube tree where the |
Dirk Lemstra | 25229f2 | 2020-04-27 15:01:47 +0200 | [diff] [blame] | 1224 | % of unique colors is not zero. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1225 | % |
| 1226 | % The format of the DefineImageColormap method is: |
| 1227 | % |
Dirk Lemstra | 25229f2 | 2020-04-27 15:01:47 +0200 | [diff] [blame] | 1228 | % void DefineImageColormap(Image *image,CubeInfo *cube_info, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1229 | % NodeInfo *node_info) |
| 1230 | % |
| 1231 | % A description of each parameter follows. |
| 1232 | % |
| 1233 | % o image: the image. |
| 1234 | % |
| 1235 | % o cube_info: A pointer to the Cube structure. |
| 1236 | % |
| 1237 | % o node_info: the address of a structure of type NodeInfo which points to a |
| 1238 | % node in the color cube tree that is to be pruned. |
| 1239 | % |
| 1240 | */ |
Dirk Lemstra | 25229f2 | 2020-04-27 15:01:47 +0200 | [diff] [blame] | 1241 | static void DefineImageColormap(Image *image,CubeInfo *cube_info, |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1242 | NodeInfo *node_info) |
| 1243 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1244 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1245 | i; |
| 1246 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1247 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1248 | number_children; |
| 1249 | |
| 1250 | /* |
| 1251 | Traverse any children. |
| 1252 | */ |
| 1253 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1254 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1255 | if (node_info->child[i] != (NodeInfo *) NULL) |
Dirk Lemstra | 25229f2 | 2020-04-27 15:01:47 +0200 | [diff] [blame] | 1256 | DefineImageColormap(image,cube_info,node_info->child[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1257 | if (node_info->number_unique != 0) |
| 1258 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1259 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1260 | alpha; |
| 1261 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1262 | PixelInfo |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 1263 | *magick_restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1264 | |
| 1265 | /* |
| 1266 | Colormap entry is defined by the mean color in this cube. |
| 1267 | */ |
| 1268 | q=image->colormap+image->colors; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1269 | alpha=(double) ((MagickOffsetType) node_info->number_unique); |
cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 1270 | alpha=PerceptibleReciprocal(alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1271 | if (cube_info->associate_alpha == MagickFalse) |
| 1272 | { |
cristy | 1aeeff3 | 2013-02-21 21:51:24 +0000 | [diff] [blame] | 1273 | q->red=(double) ClampToQuantum(alpha*QuantumRange* |
| 1274 | node_info->total_color.red); |
| 1275 | q->green=(double) ClampToQuantum(alpha*QuantumRange* |
| 1276 | node_info->total_color.green); |
| 1277 | q->blue=(double) ClampToQuantum(alpha*QuantumRange* |
| 1278 | node_info->total_color.blue); |
| 1279 | q->alpha=(double) OpaqueAlpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1280 | } |
| 1281 | else |
| 1282 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1283 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1284 | opacity; |
| 1285 | |
cristy | 1aeeff3 | 2013-02-21 21:51:24 +0000 | [diff] [blame] | 1286 | opacity=(double) (alpha*QuantumRange*node_info->total_color.alpha); |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 1287 | q->alpha=(double) ClampToQuantum(opacity); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1288 | if (q->alpha == OpaqueAlpha) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1289 | { |
cristy | 1aeeff3 | 2013-02-21 21:51:24 +0000 | [diff] [blame] | 1290 | q->red=(double) ClampToQuantum(alpha*QuantumRange* |
| 1291 | node_info->total_color.red); |
| 1292 | q->green=(double) ClampToQuantum(alpha*QuantumRange* |
| 1293 | node_info->total_color.green); |
| 1294 | q->blue=(double) ClampToQuantum(alpha*QuantumRange* |
| 1295 | node_info->total_color.blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1296 | } |
| 1297 | else |
| 1298 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1299 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1300 | gamma; |
| 1301 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1302 | gamma=(double) (QuantumScale*q->alpha); |
cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 1303 | gamma=PerceptibleReciprocal(gamma); |
cristy | 1aeeff3 | 2013-02-21 21:51:24 +0000 | [diff] [blame] | 1304 | q->red=(double) ClampToQuantum(alpha*gamma*QuantumRange* |
| 1305 | node_info->total_color.red); |
| 1306 | q->green=(double) ClampToQuantum(alpha*gamma*QuantumRange* |
| 1307 | node_info->total_color.green); |
| 1308 | q->blue=(double) ClampToQuantum(alpha*gamma*QuantumRange* |
| 1309 | node_info->total_color.blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1310 | if (node_info->number_unique > cube_info->transparent_pixels) |
| 1311 | { |
| 1312 | cube_info->transparent_pixels=node_info->number_unique; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1313 | cube_info->transparent_index=(ssize_t) image->colors; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | } |
| 1317 | node_info->color_number=image->colors++; |
| 1318 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /* |
| 1322 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1323 | % % |
| 1324 | % % |
| 1325 | % % |
| 1326 | + D e s t r o y C u b e I n f o % |
| 1327 | % % |
| 1328 | % % |
| 1329 | % % |
| 1330 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1331 | % |
| 1332 | % DestroyCubeInfo() deallocates memory associated with an image. |
| 1333 | % |
| 1334 | % The format of the DestroyCubeInfo method is: |
| 1335 | % |
| 1336 | % DestroyCubeInfo(CubeInfo *cube_info) |
| 1337 | % |
| 1338 | % A description of each parameter follows: |
| 1339 | % |
| 1340 | % o cube_info: the address of a structure of type CubeInfo. |
| 1341 | % |
| 1342 | */ |
| 1343 | static void DestroyCubeInfo(CubeInfo *cube_info) |
| 1344 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1345 | Nodes |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1346 | *nodes; |
| 1347 | |
| 1348 | /* |
| 1349 | Release color cube tree storage. |
| 1350 | */ |
| 1351 | do |
| 1352 | { |
| 1353 | nodes=cube_info->node_queue->next; |
| 1354 | cube_info->node_queue->nodes=(NodeInfo *) RelinquishMagickMemory( |
| 1355 | cube_info->node_queue->nodes); |
| 1356 | cube_info->node_queue=(Nodes *) RelinquishMagickMemory( |
| 1357 | cube_info->node_queue); |
| 1358 | cube_info->node_queue=nodes; |
| 1359 | } while (cube_info->node_queue != (Nodes *) NULL); |
cristy | a321eb7 | 2013-06-23 10:42:37 +0000 | [diff] [blame] | 1360 | if (cube_info->memory_info != (MemoryInfo *) NULL) |
| 1361 | cube_info->memory_info=RelinquishVirtualMemory(cube_info->memory_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1362 | cube_info->quantize_info=DestroyQuantizeInfo(cube_info->quantize_info); |
| 1363 | cube_info=(CubeInfo *) RelinquishMagickMemory(cube_info); |
| 1364 | } |
| 1365 | |
| 1366 | /* |
| 1367 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1368 | % % |
| 1369 | % % |
| 1370 | % % |
| 1371 | % D e s t r o y Q u a n t i z e I n f o % |
| 1372 | % % |
| 1373 | % % |
| 1374 | % % |
| 1375 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1376 | % |
| 1377 | % DestroyQuantizeInfo() deallocates memory associated with an QuantizeInfo |
| 1378 | % structure. |
| 1379 | % |
| 1380 | % The format of the DestroyQuantizeInfo method is: |
| 1381 | % |
| 1382 | % QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info) |
| 1383 | % |
| 1384 | % A description of each parameter follows: |
| 1385 | % |
| 1386 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 1387 | % |
| 1388 | */ |
| 1389 | MagickExport QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info) |
| 1390 | { |
| 1391 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| 1392 | assert(quantize_info != (QuantizeInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 1393 | assert(quantize_info->signature == MagickCoreSignature); |
| 1394 | quantize_info->signature=(~MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1395 | quantize_info=(QuantizeInfo *) RelinquishMagickMemory(quantize_info); |
| 1396 | return(quantize_info); |
| 1397 | } |
| 1398 | |
| 1399 | /* |
| 1400 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1401 | % % |
| 1402 | % % |
| 1403 | % % |
| 1404 | + D i t h e r I m a g e % |
| 1405 | % % |
| 1406 | % % |
| 1407 | % % |
| 1408 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1409 | % |
| 1410 | % DitherImage() distributes the difference between an original image and |
| 1411 | % the corresponding color reduced algorithm to neighboring pixels using |
| 1412 | % serpentine-scan Floyd-Steinberg error diffusion. DitherImage returns |
| 1413 | % MagickTrue if the image is dithered otherwise MagickFalse. |
| 1414 | % |
| 1415 | % The format of the DitherImage method is: |
| 1416 | % |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1417 | % MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info, |
| 1418 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1419 | % |
| 1420 | % A description of each parameter follows. |
| 1421 | % |
| 1422 | % o image: the image. |
| 1423 | % |
| 1424 | % o cube_info: A pointer to the Cube structure. |
| 1425 | % |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1426 | % o exception: return any errors or warnings in this structure. |
| 1427 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1428 | */ |
| 1429 | |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1430 | static DoublePixelPacket **DestroyPixelThreadSet(DoublePixelPacket **pixels) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1431 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1432 | ssize_t |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1433 | i; |
| 1434 | |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1435 | assert(pixels != (DoublePixelPacket **) NULL); |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 1436 | for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++) |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1437 | if (pixels[i] != (DoublePixelPacket *) NULL) |
| 1438 | pixels[i]=(DoublePixelPacket *) RelinquishMagickMemory(pixels[i]); |
| 1439 | pixels=(DoublePixelPacket **) RelinquishMagickMemory(pixels); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1440 | return(pixels); |
| 1441 | } |
| 1442 | |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1443 | static DoublePixelPacket **AcquirePixelThreadSet(const size_t count) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1444 | { |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1445 | DoublePixelPacket |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1446 | **pixels; |
| 1447 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1448 | ssize_t |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1449 | i; |
| 1450 | |
| 1451 | size_t |
| 1452 | number_threads; |
| 1453 | |
cristy | 9357bdd | 2012-07-30 12:28:34 +0000 | [diff] [blame] | 1454 | number_threads=(size_t) GetMagickResourceLimit(ThreadResource); |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1455 | pixels=(DoublePixelPacket **) AcquireQuantumMemory(number_threads, |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1456 | sizeof(*pixels)); |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1457 | if (pixels == (DoublePixelPacket **) NULL) |
| 1458 | return((DoublePixelPacket **) NULL); |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 1459 | (void) memset(pixels,0,number_threads*sizeof(*pixels)); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1460 | for (i=0; i < (ssize_t) number_threads; i++) |
| 1461 | { |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1462 | pixels[i]=(DoublePixelPacket *) AcquireQuantumMemory(count,2* |
Cristy | 2392b4b | 2015-12-04 18:28:23 -0500 | [diff] [blame] | 1463 | sizeof(**pixels)); |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1464 | if (pixels[i] == (DoublePixelPacket *) NULL) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1465 | return(DestroyPixelThreadSet(pixels)); |
| 1466 | } |
| 1467 | return(pixels); |
| 1468 | } |
| 1469 | |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1470 | static inline ssize_t CacheOffset(CubeInfo *cube_info, |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1471 | const DoublePixelPacket *pixel) |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1472 | { |
| 1473 | #define RedShift(pixel) (((pixel) >> CacheShift) << (0*(8-CacheShift))) |
| 1474 | #define GreenShift(pixel) (((pixel) >> CacheShift) << (1*(8-CacheShift))) |
| 1475 | #define BlueShift(pixel) (((pixel) >> CacheShift) << (2*(8-CacheShift))) |
| 1476 | #define AlphaShift(pixel) (((pixel) >> CacheShift) << (3*(8-CacheShift))) |
| 1477 | |
| 1478 | ssize_t |
| 1479 | offset; |
| 1480 | |
cristy | 6f7e042 | 2012-12-25 20:04:53 +0000 | [diff] [blame] | 1481 | offset=(ssize_t) (RedShift(ScaleQuantumToChar(ClampPixel(pixel->red))) | |
| 1482 | GreenShift(ScaleQuantumToChar(ClampPixel(pixel->green))) | |
| 1483 | BlueShift(ScaleQuantumToChar(ClampPixel(pixel->blue)))); |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1484 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 6f7e042 | 2012-12-25 20:04:53 +0000 | [diff] [blame] | 1485 | offset|=AlphaShift(ScaleQuantumToChar(ClampPixel(pixel->alpha))); |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1486 | return(offset); |
| 1487 | } |
| 1488 | |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1489 | static MagickBooleanType FloydSteinbergDither(Image *image,CubeInfo *cube_info, |
| 1490 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1491 | { |
| 1492 | #define DitherImageTag "Dither/Image" |
| 1493 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1494 | CacheView |
| 1495 | *image_view; |
| 1496 | |
Cristy | dd1a91a | 2018-06-17 09:21:49 -0400 | [diff] [blame] | 1497 | const char |
| 1498 | *artifact; |
| 1499 | |
| 1500 | double |
| 1501 | amount; |
| 1502 | |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1503 | DoublePixelPacket |
Cristy | 2392b4b | 2015-12-04 18:28:23 -0500 | [diff] [blame] | 1504 | **pixels; |
| 1505 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1506 | MagickBooleanType |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1507 | status; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1508 | |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 1509 | ssize_t |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 1510 | y; |
| 1511 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1512 | /* |
| 1513 | Distribute quantization error using Floyd-Steinberg. |
| 1514 | */ |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1515 | pixels=AcquirePixelThreadSet(image->columns); |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1516 | if (pixels == (DoublePixelPacket **) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1517 | return(MagickFalse); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1518 | status=MagickTrue; |
Cristy | dd1a91a | 2018-06-17 09:21:49 -0400 | [diff] [blame] | 1519 | amount=1.0; |
| 1520 | artifact=GetImageArtifact(image,"dither:diffusion-amount"); |
| 1521 | if (artifact != (const char *) NULL) |
| 1522 | amount=StringToDoubleInterval(artifact,1.0); |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1523 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1524 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1525 | { |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1526 | const int |
| 1527 | id = GetOpenMPThreadId(); |
| 1528 | |
| 1529 | CubeInfo |
| 1530 | cube; |
| 1531 | |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1532 | DoublePixelPacket |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1533 | *current, |
| 1534 | *previous; |
| 1535 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1536 | Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 1537 | *magick_restrict q; |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 1538 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1539 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1540 | x; |
| 1541 | |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1542 | size_t |
| 1543 | index; |
| 1544 | |
| 1545 | ssize_t |
| 1546 | v; |
| 1547 | |
| 1548 | if (status == MagickFalse) |
| 1549 | continue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1550 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1551 | if (q == (Quantum *) NULL) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1552 | { |
| 1553 | status=MagickFalse; |
cristy | 00cbdd6 | 2011-02-20 17:29:26 +0000 | [diff] [blame] | 1554 | continue; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1555 | } |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1556 | cube=(*cube_info); |
| 1557 | current=pixels[id]+(y & 0x01)*image->columns; |
| 1558 | previous=pixels[id]+((y+1) & 0x01)*image->columns; |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1559 | v=(ssize_t) ((y & 0x01) != 0 ? -1 : 1); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1560 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1561 | { |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1562 | DoublePixelPacket |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1563 | color, |
| 1564 | pixel; |
| 1565 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1566 | ssize_t |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1567 | i; |
| 1568 | |
| 1569 | ssize_t |
| 1570 | u; |
| 1571 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1572 | u=(y & 0x01) != 0 ? (ssize_t) image->columns-1-x : x; |
cristy | 99372ba | 2015-01-29 23:54:45 +0000 | [diff] [blame] | 1573 | AssociateAlphaPixel(image,&cube,q+u*GetPixelChannels(image),&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1574 | if (x > 0) |
| 1575 | { |
Cristy | dd1a91a | 2018-06-17 09:21:49 -0400 | [diff] [blame] | 1576 | pixel.red+=7.0*amount*current[u-v].red/16; |
| 1577 | pixel.green+=7.0*amount*current[u-v].green/16; |
| 1578 | pixel.blue+=7.0*amount*current[u-v].blue/16; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1579 | if (cube.associate_alpha != MagickFalse) |
Cristy | dd1a91a | 2018-06-17 09:21:49 -0400 | [diff] [blame] | 1580 | pixel.alpha+=7.0*amount*current[u-v].alpha/16; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1581 | } |
| 1582 | if (y > 0) |
| 1583 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1584 | if (x < (ssize_t) (image->columns-1)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1585 | { |
| 1586 | pixel.red+=previous[u+v].red/16; |
| 1587 | pixel.green+=previous[u+v].green/16; |
| 1588 | pixel.blue+=previous[u+v].blue/16; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1589 | if (cube.associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1590 | pixel.alpha+=previous[u+v].alpha/16; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1591 | } |
Cristy | dd1a91a | 2018-06-17 09:21:49 -0400 | [diff] [blame] | 1592 | pixel.red+=5.0*amount*previous[u].red/16; |
| 1593 | pixel.green+=5.0*amount*previous[u].green/16; |
| 1594 | pixel.blue+=5.0*amount*previous[u].blue/16; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1595 | if (cube.associate_alpha != MagickFalse) |
Cristy | dd1a91a | 2018-06-17 09:21:49 -0400 | [diff] [blame] | 1596 | pixel.alpha+=5.0*amount*previous[u].alpha/16; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1597 | if (x > 0) |
| 1598 | { |
Cristy | dd1a91a | 2018-06-17 09:21:49 -0400 | [diff] [blame] | 1599 | pixel.red+=3.0*amount*previous[u-v].red/16; |
| 1600 | pixel.green+=3.0*amount*previous[u-v].green/16; |
| 1601 | pixel.blue+=3.0*amount*previous[u-v].blue/16; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1602 | if (cube.associate_alpha != MagickFalse) |
Cristy | dd1a91a | 2018-06-17 09:21:49 -0400 | [diff] [blame] | 1603 | pixel.alpha+=3.0*amount*previous[u-v].alpha/16; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1604 | } |
| 1605 | } |
cristy | 6f7e042 | 2012-12-25 20:04:53 +0000 | [diff] [blame] | 1606 | pixel.red=(double) ClampPixel(pixel.red); |
| 1607 | pixel.green=(double) ClampPixel(pixel.green); |
| 1608 | pixel.blue=(double) ClampPixel(pixel.blue); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1609 | if (cube.associate_alpha != MagickFalse) |
cristy | 6f7e042 | 2012-12-25 20:04:53 +0000 | [diff] [blame] | 1610 | pixel.alpha=(double) ClampPixel(pixel.alpha); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1611 | i=CacheOffset(&cube,&pixel); |
| 1612 | if (cube.cache[i] < 0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1613 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1614 | NodeInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1615 | *node_info; |
| 1616 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1617 | size_t |
dirk | aa6a11c | 2015-10-18 12:21:23 +0200 | [diff] [blame] | 1618 | node_id; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1619 | |
| 1620 | /* |
| 1621 | Identify the deepest node containing the pixel's color. |
| 1622 | */ |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1623 | node_info=cube.root; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1624 | for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1625 | { |
dirk | aa6a11c | 2015-10-18 12:21:23 +0200 | [diff] [blame] | 1626 | node_id=ColorToNodeId(&cube,&pixel,index); |
| 1627 | if (node_info->child[node_id] == (NodeInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1628 | break; |
dirk | aa6a11c | 2015-10-18 12:21:23 +0200 | [diff] [blame] | 1629 | node_info=node_info->child[node_id]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1630 | } |
| 1631 | /* |
| 1632 | Find closest color among siblings and their children. |
| 1633 | */ |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1634 | cube.target=pixel; |
cristy | 7bdaee5 | 2013-02-21 21:29:02 +0000 | [diff] [blame] | 1635 | cube.distance=(double) (4.0*(QuantumRange+1.0)*(QuantumRange+1.0)+ |
| 1636 | 1.0); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1637 | ClosestColor(image,&cube,node_info->parent); |
| 1638 | cube.cache[i]=(ssize_t) cube.color_number; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1639 | } |
| 1640 | /* |
| 1641 | Assign pixel to closest colormap entry. |
| 1642 | */ |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1643 | index=(size_t) cube.cache[i]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1644 | if (image->storage_class == PseudoClass) |
cristy | 99372ba | 2015-01-29 23:54:45 +0000 | [diff] [blame] | 1645 | SetPixelIndex(image,(Quantum) index,q+u*GetPixelChannels(image)); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1646 | if (cube.quantize_info->measure_error == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1647 | { |
cristy | 99372ba | 2015-01-29 23:54:45 +0000 | [diff] [blame] | 1648 | SetPixelRed(image,ClampToQuantum(image->colormap[index].red), |
| 1649 | q+u*GetPixelChannels(image)); |
| 1650 | SetPixelGreen(image,ClampToQuantum(image->colormap[index].green), |
| 1651 | q+u*GetPixelChannels(image)); |
| 1652 | SetPixelBlue(image,ClampToQuantum(image->colormap[index].blue), |
| 1653 | q+u*GetPixelChannels(image)); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1654 | if (cube.associate_alpha != MagickFalse) |
cristy | 99372ba | 2015-01-29 23:54:45 +0000 | [diff] [blame] | 1655 | SetPixelAlpha(image,ClampToQuantum(image->colormap[index].alpha), |
| 1656 | q+u*GetPixelChannels(image)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1657 | } |
| 1658 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1659 | status=MagickFalse; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1660 | /* |
| 1661 | Store the error. |
| 1662 | */ |
cristy | b0de93f | 2013-05-03 13:39:25 +0000 | [diff] [blame] | 1663 | AssociateAlphaPixelInfo(&cube,image->colormap+index,&color); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1664 | current[u].red=pixel.red-color.red; |
| 1665 | current[u].green=pixel.green-color.green; |
| 1666 | current[u].blue=pixel.blue-color.blue; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1667 | if (cube.associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1668 | current[u].alpha=pixel.alpha-color.alpha; |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1669 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 1670 | { |
| 1671 | MagickBooleanType |
| 1672 | proceed; |
| 1673 | |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1674 | proceed=SetImageProgress(image,DitherImageTag,(MagickOffsetType) y, |
| 1675 | image->rows); |
| 1676 | if (proceed == MagickFalse) |
| 1677 | status=MagickFalse; |
| 1678 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1679 | } |
| 1680 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1681 | image_view=DestroyCacheView(image_view); |
cristy | e9717ac | 2011-02-20 16:17:17 +0000 | [diff] [blame] | 1682 | pixels=DestroyPixelThreadSet(pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1683 | return(MagickTrue); |
| 1684 | } |
| 1685 | |
| 1686 | static MagickBooleanType |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1687 | RiemersmaDither(Image *,CacheView *,CubeInfo *,const unsigned int, |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 1688 | ExceptionInfo *); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1689 | |
| 1690 | static void Riemersma(Image *image,CacheView *image_view,CubeInfo *cube_info, |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1691 | const size_t level,const unsigned int direction,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1692 | { |
| 1693 | if (level == 1) |
| 1694 | switch (direction) |
| 1695 | { |
| 1696 | case WestGravity: |
| 1697 | { |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1698 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity, |
| 1699 | exception); |
| 1700 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity, |
| 1701 | exception); |
| 1702 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity, |
| 1703 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1704 | break; |
| 1705 | } |
| 1706 | case EastGravity: |
| 1707 | { |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1708 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity, |
| 1709 | exception); |
| 1710 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity, |
| 1711 | exception); |
| 1712 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity, |
| 1713 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1714 | break; |
| 1715 | } |
| 1716 | case NorthGravity: |
| 1717 | { |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1718 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity, |
| 1719 | exception); |
| 1720 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity, |
| 1721 | exception); |
| 1722 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity, |
| 1723 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1724 | break; |
| 1725 | } |
| 1726 | case SouthGravity: |
| 1727 | { |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1728 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity, |
| 1729 | exception); |
| 1730 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity, |
| 1731 | exception); |
| 1732 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity, |
| 1733 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1734 | break; |
| 1735 | } |
| 1736 | default: |
| 1737 | break; |
| 1738 | } |
| 1739 | else |
| 1740 | switch (direction) |
| 1741 | { |
| 1742 | case WestGravity: |
| 1743 | { |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1744 | Riemersma(image,image_view,cube_info,level-1,NorthGravity, |
| 1745 | exception); |
| 1746 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity, |
| 1747 | exception); |
| 1748 | Riemersma(image,image_view,cube_info,level-1,WestGravity, |
| 1749 | exception); |
| 1750 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity, |
| 1751 | exception); |
| 1752 | Riemersma(image,image_view,cube_info,level-1,WestGravity, |
| 1753 | exception); |
| 1754 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity, |
| 1755 | exception); |
| 1756 | Riemersma(image,image_view,cube_info,level-1,SouthGravity, |
| 1757 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1758 | break; |
| 1759 | } |
| 1760 | case EastGravity: |
| 1761 | { |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1762 | Riemersma(image,image_view,cube_info,level-1,SouthGravity, |
| 1763 | exception); |
| 1764 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity, |
| 1765 | exception); |
| 1766 | Riemersma(image,image_view,cube_info,level-1,EastGravity, |
| 1767 | exception); |
| 1768 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity, |
| 1769 | exception); |
| 1770 | Riemersma(image,image_view,cube_info,level-1,EastGravity, |
| 1771 | exception); |
| 1772 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity, |
| 1773 | exception); |
| 1774 | Riemersma(image,image_view,cube_info,level-1,NorthGravity, |
| 1775 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1776 | break; |
| 1777 | } |
| 1778 | case NorthGravity: |
| 1779 | { |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1780 | Riemersma(image,image_view,cube_info,level-1,WestGravity, |
| 1781 | exception); |
| 1782 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity, |
| 1783 | exception); |
| 1784 | Riemersma(image,image_view,cube_info,level-1,NorthGravity, |
| 1785 | exception); |
| 1786 | (void) RiemersmaDither(image,image_view,cube_info,EastGravity, |
| 1787 | exception); |
| 1788 | Riemersma(image,image_view,cube_info,level-1,NorthGravity, |
| 1789 | exception); |
| 1790 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity, |
| 1791 | exception); |
| 1792 | Riemersma(image,image_view,cube_info,level-1,EastGravity, |
| 1793 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1794 | break; |
| 1795 | } |
| 1796 | case SouthGravity: |
| 1797 | { |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1798 | Riemersma(image,image_view,cube_info,level-1,EastGravity, |
| 1799 | exception); |
| 1800 | (void) RiemersmaDither(image,image_view,cube_info,NorthGravity, |
| 1801 | exception); |
| 1802 | Riemersma(image,image_view,cube_info,level-1,SouthGravity, |
| 1803 | exception); |
| 1804 | (void) RiemersmaDither(image,image_view,cube_info,WestGravity, |
| 1805 | exception); |
| 1806 | Riemersma(image,image_view,cube_info,level-1,SouthGravity, |
| 1807 | exception); |
| 1808 | (void) RiemersmaDither(image,image_view,cube_info,SouthGravity, |
| 1809 | exception); |
| 1810 | Riemersma(image,image_view,cube_info,level-1,WestGravity, |
| 1811 | exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1812 | break; |
| 1813 | } |
| 1814 | default: |
| 1815 | break; |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | static MagickBooleanType RiemersmaDither(Image *image,CacheView *image_view, |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1820 | CubeInfo *cube_info,const unsigned int direction,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1821 | { |
| 1822 | #define DitherImageTag "Dither/Image" |
| 1823 | |
Cristy | 2f4e2b2 | 2017-01-22 17:25:53 -0500 | [diff] [blame] | 1824 | DoublePixelPacket |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1825 | color, |
| 1826 | pixel; |
| 1827 | |
Cristy | 2392b4b | 2015-12-04 18:28:23 -0500 | [diff] [blame] | 1828 | MagickBooleanType |
| 1829 | proceed; |
| 1830 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1831 | CubeInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1832 | *p; |
| 1833 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1834 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1835 | index; |
| 1836 | |
| 1837 | p=cube_info; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1838 | if ((p->x >= 0) && (p->x < (ssize_t) image->columns) && |
| 1839 | (p->y >= 0) && (p->y < (ssize_t) image->rows)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1840 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1841 | Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 1842 | *magick_restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1843 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1844 | ssize_t |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 1845 | i; |
| 1846 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1847 | /* |
| 1848 | Distribute error. |
| 1849 | */ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1850 | q=GetCacheViewAuthenticPixels(image_view,p->x,p->y,1,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 1851 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1852 | return(MagickFalse); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1853 | AssociateAlphaPixel(image,cube_info,q,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1854 | for (i=0; i < ErrorQueueLength; i++) |
| 1855 | { |
| 1856 | pixel.red+=p->weights[i]*p->error[i].red; |
| 1857 | pixel.green+=p->weights[i]*p->error[i].green; |
| 1858 | pixel.blue+=p->weights[i]*p->error[i].blue; |
| 1859 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1860 | pixel.alpha+=p->weights[i]*p->error[i].alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1861 | } |
cristy | 6f7e042 | 2012-12-25 20:04:53 +0000 | [diff] [blame] | 1862 | pixel.red=(double) ClampPixel(pixel.red); |
| 1863 | pixel.green=(double) ClampPixel(pixel.green); |
| 1864 | pixel.blue=(double) ClampPixel(pixel.blue); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1865 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 6f7e042 | 2012-12-25 20:04:53 +0000 | [diff] [blame] | 1866 | pixel.alpha=(double) ClampPixel(pixel.alpha); |
cristy | ca972de | 2010-06-20 23:37:02 +0000 | [diff] [blame] | 1867 | i=CacheOffset(cube_info,&pixel); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1868 | if (p->cache[i] < 0) |
| 1869 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1870 | NodeInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1871 | *node_info; |
| 1872 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1873 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1874 | id; |
| 1875 | |
| 1876 | /* |
| 1877 | Identify the deepest node containing the pixel's color. |
| 1878 | */ |
| 1879 | node_info=p->root; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1880 | for (index=MaxTreeDepth-1; (ssize_t) index > 0; index--) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1881 | { |
| 1882 | id=ColorToNodeId(cube_info,&pixel,index); |
| 1883 | if (node_info->child[id] == (NodeInfo *) NULL) |
| 1884 | break; |
| 1885 | node_info=node_info->child[id]; |
| 1886 | } |
| 1887 | /* |
| 1888 | Find closest color among siblings and their children. |
| 1889 | */ |
| 1890 | p->target=pixel; |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1891 | p->distance=(double) (4.0*(QuantumRange+1.0)*((double) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1892 | QuantumRange+1.0)+1.0); |
| 1893 | ClosestColor(image,p,node_info->parent); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1894 | p->cache[i]=(ssize_t) p->color_number; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1895 | } |
| 1896 | /* |
| 1897 | Assign pixel to closest colormap entry. |
| 1898 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1899 | index=(size_t) p->cache[i]; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1900 | if (image->storage_class == PseudoClass) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1901 | SetPixelIndex(image,(Quantum) index,q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1902 | if (cube_info->quantize_info->measure_error == MagickFalse) |
| 1903 | { |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 1904 | SetPixelRed(image,ClampToQuantum(image->colormap[index].red),q); |
| 1905 | SetPixelGreen(image,ClampToQuantum(image->colormap[index].green),q); |
| 1906 | SetPixelBlue(image,ClampToQuantum(image->colormap[index].blue),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1907 | if (cube_info->associate_alpha != MagickFalse) |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 1908 | SetPixelAlpha(image,ClampToQuantum(image->colormap[index].alpha),q); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1909 | } |
| 1910 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 1911 | return(MagickFalse); |
| 1912 | /* |
| 1913 | Propagate the error as the last entry of the error queue. |
| 1914 | */ |
Cristy | fc31dc5 | 2018-03-10 10:42:46 -0500 | [diff] [blame] | 1915 | (void) memmove(p->error,p->error+1,(ErrorQueueLength-1)* |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1916 | sizeof(p->error[0])); |
cristy | b0de93f | 2013-05-03 13:39:25 +0000 | [diff] [blame] | 1917 | AssociateAlphaPixelInfo(cube_info,image->colormap+index,&color); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1918 | p->error[ErrorQueueLength-1].red=pixel.red-color.red; |
| 1919 | p->error[ErrorQueueLength-1].green=pixel.green-color.green; |
| 1920 | p->error[ErrorQueueLength-1].blue=pixel.blue-color.blue; |
| 1921 | if (cube_info->associate_alpha != MagickFalse) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1922 | p->error[ErrorQueueLength-1].alpha=pixel.alpha-color.alpha; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1923 | proceed=SetImageProgress(image,DitherImageTag,p->offset,p->span); |
| 1924 | if (proceed == MagickFalse) |
| 1925 | return(MagickFalse); |
| 1926 | p->offset++; |
| 1927 | } |
| 1928 | switch (direction) |
| 1929 | { |
| 1930 | case WestGravity: p->x--; break; |
| 1931 | case EastGravity: p->x++; break; |
| 1932 | case NorthGravity: p->y--; break; |
| 1933 | case SouthGravity: p->y++; break; |
| 1934 | } |
| 1935 | return(MagickTrue); |
| 1936 | } |
| 1937 | |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1938 | static MagickBooleanType DitherImage(Image *image,CubeInfo *cube_info, |
| 1939 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1940 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 1941 | CacheView |
| 1942 | *image_view; |
| 1943 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1944 | MagickBooleanType |
| 1945 | status; |
| 1946 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 1947 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1948 | i; |
| 1949 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1950 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1951 | depth; |
| 1952 | |
cristy | fb7e9cd | 2011-02-20 16:26:15 +0000 | [diff] [blame] | 1953 | if (cube_info->quantize_info->dither_method != RiemersmaDitherMethod) |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1954 | return(FloydSteinbergDither(image,cube_info,exception)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1955 | /* |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1956 | Distribute quantization error along a Hilbert curve. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1957 | */ |
Cristy | db3982f | 2019-08-16 08:01:49 -0400 | [diff] [blame] | 1958 | (void) memset(cube_info->error,0,ErrorQueueLength*sizeof(*cube_info->error)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1959 | cube_info->x=0; |
| 1960 | cube_info->y=0; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1961 | i=MagickMax((ssize_t) image->columns,(ssize_t) image->rows); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1962 | for (depth=1; i != 0; depth++) |
| 1963 | i>>=1; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1964 | 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] | 1965 | depth++; |
| 1966 | cube_info->offset=0; |
| 1967 | cube_info->span=(MagickSizeType) image->columns*image->rows; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 1968 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1969 | if (depth > 1) |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 1970 | Riemersma(image,image_view,cube_info,depth-1,NorthGravity,exception); |
| 1971 | status=RiemersmaDither(image,image_view,cube_info,ForgetGravity,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1972 | image_view=DestroyCacheView(image_view); |
| 1973 | return(status); |
| 1974 | } |
| 1975 | |
| 1976 | /* |
| 1977 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1978 | % % |
| 1979 | % % |
| 1980 | % % |
| 1981 | + G e t C u b e I n f o % |
| 1982 | % % |
| 1983 | % % |
| 1984 | % % |
| 1985 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1986 | % |
| 1987 | % GetCubeInfo() initialize the Cube data structure. |
| 1988 | % |
| 1989 | % The format of the GetCubeInfo method is: |
| 1990 | % |
| 1991 | % CubeInfo GetCubeInfo(const QuantizeInfo *quantize_info, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1992 | % const size_t depth,const size_t maximum_colors) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1993 | % |
| 1994 | % A description of each parameter follows. |
| 1995 | % |
| 1996 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 1997 | % |
| 1998 | % o depth: Normally, this integer value is zero or one. A zero or |
| 1999 | % one tells Quantize to choose a optimal tree depth of Log4(number_colors). |
| 2000 | % A tree of this depth generally allows the best representation of the |
| 2001 | % reference image with the least amount of memory and the fastest |
| 2002 | % computational speed. In some cases, such as an image with low color |
| 2003 | % dispersion (a few number of colors), a value other than |
| 2004 | % Log4(number_colors) is required. To expand the color tree completely, |
| 2005 | % use a value of 8. |
| 2006 | % |
| 2007 | % o maximum_colors: maximum colors. |
| 2008 | % |
| 2009 | */ |
| 2010 | static CubeInfo *GetCubeInfo(const QuantizeInfo *quantize_info, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2011 | const size_t depth,const size_t maximum_colors) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2012 | { |
| 2013 | CubeInfo |
| 2014 | *cube_info; |
| 2015 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2016 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2017 | sum, |
| 2018 | weight; |
| 2019 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2020 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2021 | i; |
| 2022 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 2023 | size_t |
| 2024 | length; |
| 2025 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2026 | /* |
| 2027 | Initialize tree to describe color cube_info. |
| 2028 | */ |
Cristy | 8357b5d | 2020-11-22 12:39:10 +0000 | [diff] [blame] | 2029 | cube_info=(CubeInfo *) AcquireMagickMemory(sizeof(*cube_info)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2030 | if (cube_info == (CubeInfo *) NULL) |
| 2031 | return((CubeInfo *) NULL); |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 2032 | (void) memset(cube_info,0,sizeof(*cube_info)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2033 | cube_info->depth=depth; |
| 2034 | if (cube_info->depth > MaxTreeDepth) |
| 2035 | cube_info->depth=MaxTreeDepth; |
| 2036 | if (cube_info->depth < 2) |
| 2037 | cube_info->depth=2; |
| 2038 | cube_info->maximum_colors=maximum_colors; |
| 2039 | /* |
| 2040 | Initialize root node. |
| 2041 | */ |
| 2042 | cube_info->root=GetNodeInfo(cube_info,0,0,(NodeInfo *) NULL); |
| 2043 | if (cube_info->root == (NodeInfo *) NULL) |
| 2044 | return((CubeInfo *) NULL); |
| 2045 | cube_info->root->parent=cube_info->root; |
| 2046 | cube_info->quantize_info=CloneQuantizeInfo(quantize_info); |
cristy | cbda611 | 2012-05-27 20:57:16 +0000 | [diff] [blame] | 2047 | if (cube_info->quantize_info->dither_method == NoDitherMethod) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2048 | return(cube_info); |
| 2049 | /* |
| 2050 | Initialize dither resources. |
| 2051 | */ |
| 2052 | length=(size_t) (1UL << (4*(8-CacheShift))); |
cristy | a321eb7 | 2013-06-23 10:42:37 +0000 | [diff] [blame] | 2053 | cube_info->memory_info=AcquireVirtualMemory(length,sizeof(*cube_info->cache)); |
| 2054 | if (cube_info->memory_info == (MemoryInfo *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2055 | return((CubeInfo *) NULL); |
cristy | a321eb7 | 2013-06-23 10:42:37 +0000 | [diff] [blame] | 2056 | cube_info->cache=(ssize_t *) GetVirtualMemoryBlob(cube_info->memory_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2057 | /* |
| 2058 | Initialize color cache. |
| 2059 | */ |
Cristy | 54a65de | 2019-08-16 08:03:40 -0400 | [diff] [blame] | 2060 | (void) memset(cube_info->cache,(-1),sizeof(*cube_info->cache)*length); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2061 | /* |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 2062 | Distribute weights along a curve of exponential decay. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2063 | */ |
| 2064 | weight=1.0; |
| 2065 | for (i=0; i < ErrorQueueLength; i++) |
| 2066 | { |
cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 2067 | cube_info->weights[ErrorQueueLength-i-1]=PerceptibleReciprocal(weight); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2068 | weight*=exp(log(((double) QuantumRange+1.0))/(ErrorQueueLength-1.0)); |
| 2069 | } |
| 2070 | /* |
| 2071 | Normalize the weighting factors. |
| 2072 | */ |
| 2073 | weight=0.0; |
| 2074 | for (i=0; i < ErrorQueueLength; i++) |
| 2075 | weight+=cube_info->weights[i]; |
| 2076 | sum=0.0; |
| 2077 | for (i=0; i < ErrorQueueLength; i++) |
| 2078 | { |
| 2079 | cube_info->weights[i]/=weight; |
| 2080 | sum+=cube_info->weights[i]; |
| 2081 | } |
| 2082 | cube_info->weights[0]+=1.0-sum; |
| 2083 | return(cube_info); |
| 2084 | } |
| 2085 | |
| 2086 | /* |
| 2087 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2088 | % % |
| 2089 | % % |
| 2090 | % % |
| 2091 | + G e t N o d e I n f o % |
| 2092 | % % |
| 2093 | % % |
| 2094 | % % |
| 2095 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2096 | % |
| 2097 | % GetNodeInfo() allocates memory for a new node in the color cube tree and |
| 2098 | % presets all fields to zero. |
| 2099 | % |
| 2100 | % The format of the GetNodeInfo method is: |
| 2101 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2102 | % NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id, |
| 2103 | % const size_t level,NodeInfo *parent) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2104 | % |
| 2105 | % A description of each parameter follows. |
| 2106 | % |
| 2107 | % o node: The GetNodeInfo method returns a pointer to a queue of nodes. |
| 2108 | % |
| 2109 | % o id: Specifies the child number of the node. |
| 2110 | % |
| 2111 | % o level: Specifies the level in the storage_class the node resides. |
| 2112 | % |
| 2113 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2114 | static NodeInfo *GetNodeInfo(CubeInfo *cube_info,const size_t id, |
| 2115 | const size_t level,NodeInfo *parent) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2116 | { |
| 2117 | NodeInfo |
| 2118 | *node_info; |
| 2119 | |
| 2120 | if (cube_info->free_nodes == 0) |
| 2121 | { |
| 2122 | Nodes |
| 2123 | *nodes; |
| 2124 | |
| 2125 | /* |
| 2126 | Allocate a new queue of nodes. |
| 2127 | */ |
Cristy | 8357b5d | 2020-11-22 12:39:10 +0000 | [diff] [blame] | 2128 | nodes=(Nodes *) AcquireMagickMemory(sizeof(*nodes)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2129 | if (nodes == (Nodes *) NULL) |
| 2130 | return((NodeInfo *) NULL); |
| 2131 | nodes->nodes=(NodeInfo *) AcquireQuantumMemory(NodesInAList, |
| 2132 | sizeof(*nodes->nodes)); |
| 2133 | if (nodes->nodes == (NodeInfo *) NULL) |
| 2134 | return((NodeInfo *) NULL); |
| 2135 | nodes->next=cube_info->node_queue; |
| 2136 | cube_info->node_queue=nodes; |
| 2137 | cube_info->next_node=nodes->nodes; |
| 2138 | cube_info->free_nodes=NodesInAList; |
| 2139 | } |
| 2140 | cube_info->nodes++; |
| 2141 | cube_info->free_nodes--; |
| 2142 | node_info=cube_info->next_node++; |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 2143 | (void) memset(node_info,0,sizeof(*node_info)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2144 | node_info->parent=parent; |
| 2145 | node_info->id=id; |
| 2146 | node_info->level=level; |
| 2147 | return(node_info); |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2152 | % % |
| 2153 | % % |
| 2154 | % % |
| 2155 | % G e t I m a g e Q u a n t i z e E r r o r % |
| 2156 | % % |
| 2157 | % % |
| 2158 | % % |
| 2159 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2160 | % |
| 2161 | % GetImageQuantizeError() measures the difference between the original |
| 2162 | % and quantized images. This difference is the total quantization error. |
| 2163 | % The error is computed by summing over all pixels in an image the distance |
| 2164 | % squared in RGB space between each reference pixel value and its quantized |
| 2165 | % value. These values are computed: |
| 2166 | % |
| 2167 | % o mean_error_per_pixel: This value is the mean error for any single |
| 2168 | % pixel in the image. |
| 2169 | % |
| 2170 | % o normalized_mean_square_error: This value is the normalized mean |
| 2171 | % quantization error for any single pixel in the image. This distance |
| 2172 | % measure is normalized to a range between 0 and 1. It is independent |
| 2173 | % of the range of red, green, and blue values in the image. |
| 2174 | % |
| 2175 | % o normalized_maximum_square_error: Thsi value is the normalized |
| 2176 | % maximum quantization error for any single pixel in the image. This |
| 2177 | % distance measure is normalized to a range between 0 and 1. It is |
| 2178 | % independent of the range of red, green, and blue values in your image. |
| 2179 | % |
| 2180 | % The format of the GetImageQuantizeError method is: |
| 2181 | % |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 2182 | % MagickBooleanType GetImageQuantizeError(Image *image, |
| 2183 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2184 | % |
| 2185 | % A description of each parameter follows. |
| 2186 | % |
| 2187 | % o image: the image. |
| 2188 | % |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 2189 | % o exception: return any errors or warnings in this structure. |
| 2190 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2191 | */ |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 2192 | MagickExport MagickBooleanType GetImageQuantizeError(Image *image, |
| 2193 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2194 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2195 | CacheView |
| 2196 | *image_view; |
| 2197 | |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2198 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2199 | alpha, |
| 2200 | area, |
| 2201 | beta, |
| 2202 | distance, |
| 2203 | maximum_error, |
| 2204 | mean_error, |
| 2205 | mean_error_per_pixel; |
| 2206 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 2207 | ssize_t |
Cristy | 7dbf5b2 | 2019-05-11 08:26:39 -0400 | [diff] [blame] | 2208 | index, |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 2209 | y; |
| 2210 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2211 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 2212 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2213 | if (image->debug != MagickFalse) |
| 2214 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 2215 | image->total_colors=GetNumberColors(image,(FILE *) NULL,exception); |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 2216 | (void) memset(&image->error,0,sizeof(image->error)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2217 | if (image->storage_class == DirectClass) |
| 2218 | return(MagickTrue); |
| 2219 | alpha=1.0; |
| 2220 | beta=1.0; |
| 2221 | area=3.0*image->columns*image->rows; |
| 2222 | maximum_error=0.0; |
| 2223 | mean_error_per_pixel=0.0; |
| 2224 | mean_error=0.0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 2225 | image_view=AcquireVirtualCacheView(image,exception); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2226 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2227 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2228 | const Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 2229 | *magick_restrict p; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2230 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2231 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2232 | x; |
| 2233 | |
| 2234 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2235 | if (p == (const Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2236 | break; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2237 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2238 | { |
Cristy | 7dbf5b2 | 2019-05-11 08:26:39 -0400 | [diff] [blame] | 2239 | index=(ssize_t) GetPixelIndex(image,p); |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 2240 | if (image->alpha_trait == BlendPixelTrait) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2241 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 2242 | alpha=(double) (QuantumScale*GetPixelAlpha(image,p)); |
| 2243 | beta=(double) (QuantumScale*image->colormap[index].alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2244 | } |
cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 2245 | distance=fabs((double) (alpha*GetPixelRed(image,p)-beta* |
| 2246 | image->colormap[index].red)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2247 | mean_error_per_pixel+=distance; |
| 2248 | mean_error+=distance*distance; |
| 2249 | if (distance > maximum_error) |
| 2250 | maximum_error=distance; |
cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 2251 | distance=fabs((double) (alpha*GetPixelGreen(image,p)-beta* |
| 2252 | image->colormap[index].green)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2253 | mean_error_per_pixel+=distance; |
| 2254 | mean_error+=distance*distance; |
| 2255 | if (distance > maximum_error) |
| 2256 | maximum_error=distance; |
cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 2257 | distance=fabs((double) (alpha*GetPixelBlue(image,p)-beta* |
| 2258 | image->colormap[index].blue)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2259 | mean_error_per_pixel+=distance; |
| 2260 | mean_error+=distance*distance; |
| 2261 | if (distance > maximum_error) |
| 2262 | maximum_error=distance; |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2263 | p+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2264 | } |
| 2265 | } |
| 2266 | image_view=DestroyCacheView(image_view); |
Cristy | 76eb72f | 2016-11-27 08:21:05 -0500 | [diff] [blame] | 2267 | image->error.mean_error_per_pixel=(double) mean_error_per_pixel/area; |
| 2268 | image->error.normalized_mean_error=(double) QuantumScale*QuantumScale* |
| 2269 | mean_error/area; |
| 2270 | image->error.normalized_maximum_error=(double) QuantumScale*maximum_error; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2271 | return(MagickTrue); |
| 2272 | } |
| 2273 | |
| 2274 | /* |
| 2275 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2276 | % % |
| 2277 | % % |
| 2278 | % % |
| 2279 | % G e t Q u a n t i z e I n f o % |
| 2280 | % % |
| 2281 | % % |
| 2282 | % % |
| 2283 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2284 | % |
| 2285 | % GetQuantizeInfo() initializes the QuantizeInfo structure. |
| 2286 | % |
| 2287 | % The format of the GetQuantizeInfo method is: |
| 2288 | % |
| 2289 | % GetQuantizeInfo(QuantizeInfo *quantize_info) |
| 2290 | % |
| 2291 | % A description of each parameter follows: |
| 2292 | % |
| 2293 | % o quantize_info: Specifies a pointer to a QuantizeInfo structure. |
| 2294 | % |
| 2295 | */ |
| 2296 | MagickExport void GetQuantizeInfo(QuantizeInfo *quantize_info) |
| 2297 | { |
| 2298 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"..."); |
| 2299 | assert(quantize_info != (QuantizeInfo *) NULL); |
Cristy | 81bfff2 | 2018-03-10 07:58:31 -0500 | [diff] [blame] | 2300 | (void) memset(quantize_info,0,sizeof(*quantize_info)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2301 | quantize_info->number_colors=256; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2302 | quantize_info->dither_method=RiemersmaDitherMethod; |
| 2303 | quantize_info->colorspace=UndefinedColorspace; |
| 2304 | quantize_info->measure_error=MagickFalse; |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 2305 | quantize_info->signature=MagickCoreSignature; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | /* |
| 2309 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2310 | % % |
| 2311 | % % |
| 2312 | % % |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2313 | % K m e a n s I m a g e % |
| 2314 | % % |
| 2315 | % % |
| 2316 | % % |
| 2317 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2318 | % |
| 2319 | % KmeansImage() applies k-means color reduction to an image. This is a |
| 2320 | % colorspace clustering or segmentation technique. |
| 2321 | % |
| 2322 | % The format of the KmeansImage method is: |
| 2323 | % |
| 2324 | % MagickBooleanType KmeansImage(Image *image,const size_t number_colors, |
Cristy | 1fce5c5 | 2019-12-31 09:51:14 -0500 | [diff] [blame] | 2325 | % const size_t max_iterations,const double tolerance, |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2326 | % ExceptionInfo *exception) |
| 2327 | % |
| 2328 | % A description of each parameter follows: |
| 2329 | % |
| 2330 | % o image: the image. |
| 2331 | % |
| 2332 | % o number_colors: number of colors to use as seeds. |
| 2333 | % |
| 2334 | % o max_iterations: maximum number of iterations while converging. |
| 2335 | % |
Cristy | 1fce5c5 | 2019-12-31 09:51:14 -0500 | [diff] [blame] | 2336 | % o tolerance: the maximum tolerance. |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2337 | % |
| 2338 | % o exception: return any errors or warnings in this structure. |
| 2339 | % |
| 2340 | */ |
| 2341 | |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2342 | typedef struct _KmeansInfo |
| 2343 | { |
| 2344 | double |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2345 | red, |
| 2346 | green, |
| 2347 | blue, |
| 2348 | alpha, |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2349 | black, |
| 2350 | count, |
| 2351 | distortion; |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2352 | } KmeansInfo; |
| 2353 | |
| 2354 | static KmeansInfo **DestroyKmeansThreadSet(KmeansInfo **kmeans_info) |
| 2355 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2356 | ssize_t |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2357 | i; |
| 2358 | |
| 2359 | assert(kmeans_info != (KmeansInfo **) NULL); |
| 2360 | for (i=0; i < (ssize_t) GetMagickResourceLimit(ThreadResource); i++) |
| 2361 | if (kmeans_info[i] != (KmeansInfo *) NULL) |
| 2362 | kmeans_info[i]=(KmeansInfo *) RelinquishMagickMemory(kmeans_info[i]); |
| 2363 | kmeans_info=(KmeansInfo **) RelinquishMagickMemory(kmeans_info); |
| 2364 | return(kmeans_info); |
| 2365 | } |
| 2366 | |
| 2367 | static KmeansInfo **AcquireKmeansThreadSet(const size_t number_colors) |
| 2368 | { |
| 2369 | KmeansInfo |
| 2370 | **kmeans_info; |
| 2371 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2372 | ssize_t |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2373 | i; |
| 2374 | |
| 2375 | size_t |
| 2376 | number_threads; |
| 2377 | |
| 2378 | number_threads=(size_t) GetMagickResourceLimit(ThreadResource); |
| 2379 | kmeans_info=(KmeansInfo **) AcquireQuantumMemory(number_threads, |
| 2380 | sizeof(*kmeans_info)); |
| 2381 | if (kmeans_info == (KmeansInfo **) NULL) |
| 2382 | return((KmeansInfo **) NULL); |
| 2383 | (void) memset(kmeans_info,0,number_threads*sizeof(*kmeans_info)); |
| 2384 | for (i=0; i < (ssize_t) number_threads; i++) |
| 2385 | { |
| 2386 | kmeans_info[i]=(KmeansInfo *) AcquireQuantumMemory(number_colors, |
| 2387 | sizeof(**kmeans_info)); |
| 2388 | if (kmeans_info[i] == (KmeansInfo *) NULL) |
| 2389 | return(DestroyKmeansThreadSet(kmeans_info)); |
| 2390 | } |
| 2391 | return(kmeans_info); |
| 2392 | } |
| 2393 | |
Cristy | b5642a6 | 2019-12-31 16:49:38 -0500 | [diff] [blame] | 2394 | static inline double KmeansMetric(const Image *magick_restrict image, |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2395 | const Quantum *magick_restrict p,const PixelInfo *magick_restrict q) |
| 2396 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2397 | double |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2398 | gamma, |
Cristy | b5642a6 | 2019-12-31 16:49:38 -0500 | [diff] [blame] | 2399 | metric, |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2400 | pixel; |
| 2401 | |
| 2402 | gamma=1.0; |
Cristy | b5642a6 | 2019-12-31 16:49:38 -0500 | [diff] [blame] | 2403 | metric=0.0; |
| 2404 | if ((image->alpha_trait != UndefinedPixelTrait) || |
| 2405 | (q->alpha_trait != UndefinedPixelTrait)) |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2406 | { |
Cristy | 793f9c2 | 2020-01-18 14:39:17 -0500 | [diff] [blame] | 2407 | pixel=GetPixelAlpha(image,p)-(q->alpha_trait != UndefinedPixelTrait ? |
| 2408 | q->alpha : OpaqueAlpha); |
Cristy | b5642a6 | 2019-12-31 16:49:38 -0500 | [diff] [blame] | 2409 | metric+=pixel*pixel; |
Cristy | 793f9c2 | 2020-01-18 14:39:17 -0500 | [diff] [blame] | 2410 | if (image->alpha_trait != UndefinedPixelTrait) |
| 2411 | gamma*=QuantumScale*GetPixelAlpha(image,p); |
| 2412 | if (q->alpha_trait != UndefinedPixelTrait) |
| 2413 | gamma*=QuantumScale*q->alpha; |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2414 | } |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2415 | if (image->colorspace == CMYKColorspace) |
| 2416 | { |
Cristy | c300b3a | 2020-01-18 15:14:42 -0500 | [diff] [blame] | 2417 | pixel=QuantumScale*(GetPixelBlack(image,p)-q->black); |
Cristy | b5642a6 | 2019-12-31 16:49:38 -0500 | [diff] [blame] | 2418 | metric+=gamma*pixel*pixel; |
| 2419 | gamma*=QuantumScale*(QuantumRange-GetPixelBlack(image,p)); |
| 2420 | gamma*=QuantumScale*(QuantumRange-q->black); |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2421 | } |
Cristy | b5642a6 | 2019-12-31 16:49:38 -0500 | [diff] [blame] | 2422 | metric*=3.0; |
| 2423 | pixel=QuantumScale*(GetPixelRed(image,p)-q->red); |
Cristy | c6c450d | 2020-01-18 11:21:12 -0500 | [diff] [blame] | 2424 | if (IsHueCompatibleColorspace(image->colorspace) != MagickFalse) |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2425 | { |
Cristy | b5642a6 | 2019-12-31 16:49:38 -0500 | [diff] [blame] | 2426 | if (fabs((double) pixel) > 0.5) |
| 2427 | pixel-=0.5; |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2428 | pixel*=2.0; |
| 2429 | } |
Cristy | b5642a6 | 2019-12-31 16:49:38 -0500 | [diff] [blame] | 2430 | metric+=gamma*pixel*pixel; |
| 2431 | pixel=QuantumScale*(GetPixelGreen(image,p)-q->green); |
| 2432 | metric+=gamma*pixel*pixel; |
| 2433 | pixel=QuantumScale*(GetPixelBlue(image,p)-q->blue); |
| 2434 | metric+=gamma*pixel*pixel; |
| 2435 | return(metric); |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2436 | } |
| 2437 | |
| 2438 | MagickExport MagickBooleanType KmeansImage(Image *image, |
Cristy | 1fce5c5 | 2019-12-31 09:51:14 -0500 | [diff] [blame] | 2439 | const size_t number_colors,const size_t max_iterations,const double tolerance, |
| 2440 | ExceptionInfo *exception) |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2441 | { |
| 2442 | #define KmeansImageTag "Kmeans/Image" |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2443 | #define RandomColorComponent(info) (QuantumRange*GetPseudoRandomValue(info)) |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2444 | |
Cristy | e574a42 | 2019-12-19 19:26:53 -0500 | [diff] [blame] | 2445 | CacheView |
| 2446 | *image_view; |
| 2447 | |
Cristy | 50b321f | 2019-12-20 21:18:19 -0500 | [diff] [blame] | 2448 | const char |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 2449 | *colors; |
Cristy | 50b321f | 2019-12-20 21:18:19 -0500 | [diff] [blame] | 2450 | |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2451 | double |
Cristy | 4bd8319 | 2019-12-31 13:33:57 -0500 | [diff] [blame] | 2452 | previous_tolerance; |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2453 | |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2454 | KmeansInfo |
| 2455 | **kmeans_pixels; |
| 2456 | |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2457 | MagickBooleanType |
Cristy | dc157f0 | 2019-12-19 16:32:14 -0500 | [diff] [blame] | 2458 | verbose, |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2459 | status; |
| 2460 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2461 | ssize_t |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2462 | n; |
| 2463 | |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2464 | size_t |
| 2465 | number_threads; |
| 2466 | |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2467 | assert(image != (Image *) NULL); |
| 2468 | assert(image->signature == MagickCoreSignature); |
| 2469 | if (image->debug != MagickFalse) |
| 2470 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 2471 | assert(exception != (ExceptionInfo *) NULL); |
| 2472 | assert(exception->signature == MagickCoreSignature); |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 2473 | colors=GetImageArtifact(image,"kmeans:seed-colors"); |
| 2474 | if (colors == (const char *) NULL) |
Cristy | 50b321f | 2019-12-20 21:18:19 -0500 | [diff] [blame] | 2475 | { |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2476 | CubeInfo |
| 2477 | *cube_info; |
Cristy | 50b321f | 2019-12-20 21:18:19 -0500 | [diff] [blame] | 2478 | |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2479 | QuantizeInfo |
| 2480 | *quantize_info; |
| 2481 | |
| 2482 | size_t |
| 2483 | colors, |
| 2484 | depth; |
| 2485 | |
| 2486 | /* |
| 2487 | Seed clusters from color quantization. |
| 2488 | */ |
Cristy | 50b321f | 2019-12-20 21:18:19 -0500 | [diff] [blame] | 2489 | quantize_info=AcquireQuantizeInfo((ImageInfo *) NULL); |
Cristy | c6c450d | 2020-01-18 11:21:12 -0500 | [diff] [blame] | 2490 | quantize_info->colorspace=image->colorspace; |
Cristy | 50b321f | 2019-12-20 21:18:19 -0500 | [diff] [blame] | 2491 | quantize_info->number_colors=number_colors; |
| 2492 | quantize_info->dither_method=NoDitherMethod; |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2493 | colors=number_colors; |
| 2494 | for (depth=1; colors != 0; depth++) |
| 2495 | colors>>=2; |
| 2496 | cube_info=GetCubeInfo(quantize_info,depth,number_colors); |
| 2497 | if (cube_info == (CubeInfo *) NULL) |
| 2498 | { |
| 2499 | quantize_info=DestroyQuantizeInfo(quantize_info); |
| 2500 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 2501 | image->filename); |
| 2502 | } |
| 2503 | status=ClassifyImageColors(cube_info,image,exception); |
| 2504 | if (status != MagickFalse) |
| 2505 | { |
| 2506 | if (cube_info->colors > cube_info->maximum_colors) |
| 2507 | ReduceImageColors(image,cube_info); |
Dirk Lemstra | 25229f2 | 2020-04-27 15:01:47 +0200 | [diff] [blame] | 2508 | status=SetImageColormap(image,cube_info,exception); |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2509 | } |
| 2510 | DestroyCubeInfo(cube_info); |
Cristy | 50b321f | 2019-12-20 21:18:19 -0500 | [diff] [blame] | 2511 | quantize_info=DestroyQuantizeInfo(quantize_info); |
| 2512 | if (status == MagickFalse) |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2513 | return(status); |
Cristy | 50b321f | 2019-12-20 21:18:19 -0500 | [diff] [blame] | 2514 | } |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 2515 | else |
| 2516 | { |
| 2517 | char |
| 2518 | color[MagickPathExtent]; |
| 2519 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2520 | const char |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 2521 | *p; |
| 2522 | |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2523 | /* |
| 2524 | Seed clusters from color list (e.g. red;green;blue). |
| 2525 | */ |
Cristy | ff13691 | 2020-01-17 20:39:20 -0500 | [diff] [blame] | 2526 | status=AcquireImageColormap(image,number_colors,exception); |
| 2527 | if (status == MagickFalse) |
| 2528 | return(status); |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 2529 | for (n=0, p=colors; n < (ssize_t) image->colors; n++) |
| 2530 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2531 | const char |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 2532 | *q; |
| 2533 | |
| 2534 | for (q=p; *q != '\0'; q++) |
| 2535 | if (*q == ';') |
| 2536 | break; |
Cristy | 74b3733 | 2019-12-21 19:36:19 -0500 | [diff] [blame] | 2537 | (void) CopyMagickString(color,p,(size_t) MagickMin(q-p+1, |
| 2538 | MagickPathExtent)); |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2539 | (void) QueryColorCompliance(color,AllCompliance,image->colormap+n, |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 2540 | exception); |
| 2541 | if (*q == '\0') |
Cristy | 76b9695 | 2019-12-21 16:00:54 -0500 | [diff] [blame] | 2542 | { |
| 2543 | n++; |
| 2544 | break; |
| 2545 | } |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 2546 | p=q+1; |
| 2547 | } |
| 2548 | if (n < (ssize_t) image->colors) |
| 2549 | { |
| 2550 | RandomInfo |
| 2551 | *random_info; |
| 2552 | |
Cristy | 2816498 | 2019-12-22 08:02:31 -0500 | [diff] [blame] | 2553 | /* |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2554 | Seed clusters from random values. |
Cristy | 2816498 | 2019-12-22 08:02:31 -0500 | [diff] [blame] | 2555 | */ |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 2556 | random_info=AcquireRandomInfo(); |
| 2557 | for ( ; n < (ssize_t) image->colors; n++) |
Cristy | b8f86f8 | 2019-12-22 20:52:26 -0500 | [diff] [blame] | 2558 | { |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2559 | (void) QueryColorCompliance("#000",AllCompliance,image->colormap+n, |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2560 | exception); |
| 2561 | image->colormap[n].red=RandomColorComponent(random_info); |
| 2562 | image->colormap[n].green=RandomColorComponent(random_info); |
| 2563 | image->colormap[n].blue=RandomColorComponent(random_info); |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2564 | if (image->alpha_trait != BlendPixelTrait) |
| 2565 | image->colormap[n].alpha=RandomColorComponent(random_info); |
| 2566 | if (image->colorspace == CMYKColorspace) |
| 2567 | image->colormap[n].black=RandomColorComponent(random_info); |
Cristy | b8f86f8 | 2019-12-22 20:52:26 -0500 | [diff] [blame] | 2568 | } |
Cristy | 6344215 | 2019-12-21 09:47:06 -0500 | [diff] [blame] | 2569 | random_info=DestroyRandomInfo(random_info); |
| 2570 | } |
| 2571 | } |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2572 | /* |
| 2573 | Iterative refinement. |
| 2574 | */ |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2575 | kmeans_pixels=AcquireKmeansThreadSet(number_colors); |
| 2576 | if (kmeans_pixels == (KmeansInfo **) NULL) |
| 2577 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 2578 | image->filename); |
Cristy | 4bd8319 | 2019-12-31 13:33:57 -0500 | [diff] [blame] | 2579 | previous_tolerance=0.0; |
Cristy | dc157f0 | 2019-12-19 16:32:14 -0500 | [diff] [blame] | 2580 | verbose=IsStringTrue(GetImageArtifact(image,"debug")); |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2581 | number_threads=(size_t) GetMagickResourceLimit(ThreadResource); |
Cristy | e574a42 | 2019-12-19 19:26:53 -0500 | [diff] [blame] | 2582 | image_view=AcquireAuthenticCacheView(image,exception); |
Cristy | 746cd9c | 2019-12-20 21:24:43 -0500 | [diff] [blame] | 2583 | for (n=0; n < (ssize_t) max_iterations; n++) |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2584 | { |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2585 | double |
| 2586 | distortion; |
| 2587 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2588 | ssize_t |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2589 | i; |
| 2590 | |
| 2591 | ssize_t |
| 2592 | y; |
| 2593 | |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2594 | for (i=0; i < (ssize_t) number_threads; i++) |
| 2595 | (void) memset(kmeans_pixels[i],0,image->colors*sizeof(*kmeans_pixels[i])); |
| 2596 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2597 | #pragma omp parallel for schedule(dynamic) shared(status) \ |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2598 | magick_number_threads(image,image,image->rows,1) |
| 2599 | #endif |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2600 | for (y=0; y < (ssize_t) image->rows; y++) |
| 2601 | { |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2602 | const int |
| 2603 | id = GetOpenMPThreadId(); |
| 2604 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2605 | Quantum |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2606 | *magick_restrict q; |
| 2607 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2608 | ssize_t |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2609 | x; |
| 2610 | |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2611 | if (status == MagickFalse) |
| 2612 | continue; |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2613 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| 2614 | if (q == (Quantum *) NULL) |
| 2615 | { |
| 2616 | status=MagickFalse; |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2617 | continue; |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2618 | } |
| 2619 | for (x=0; x < (ssize_t) image->columns; x++) |
| 2620 | { |
| 2621 | double |
| 2622 | min_distance; |
| 2623 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2624 | ssize_t |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2625 | i; |
| 2626 | |
| 2627 | ssize_t |
| 2628 | j; |
| 2629 | |
| 2630 | /* |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2631 | Assign each pixel whose mean has the least squared color distance. |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2632 | */ |
| 2633 | j=0; |
Cristy | b5642a6 | 2019-12-31 16:49:38 -0500 | [diff] [blame] | 2634 | min_distance=KmeansMetric(image,q,image->colormap+0); |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2635 | for (i=1; i < (ssize_t) image->colors; i++) |
| 2636 | { |
| 2637 | double |
| 2638 | distance; |
| 2639 | |
| 2640 | if (min_distance <= MagickEpsilon) |
| 2641 | break; |
Cristy | b5642a6 | 2019-12-31 16:49:38 -0500 | [diff] [blame] | 2642 | distance=KmeansMetric(image,q,image->colormap+i); |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2643 | if (distance < min_distance) |
| 2644 | { |
| 2645 | min_distance=distance; |
| 2646 | j=i; |
| 2647 | } |
| 2648 | } |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2649 | kmeans_pixels[id][j].red+=QuantumScale*GetPixelRed(image,q); |
| 2650 | kmeans_pixels[id][j].green+=QuantumScale*GetPixelGreen(image,q); |
| 2651 | kmeans_pixels[id][j].blue+=QuantumScale*GetPixelBlue(image,q); |
Cristy | e574a42 | 2019-12-19 19:26:53 -0500 | [diff] [blame] | 2652 | if (image->alpha_trait != BlendPixelTrait) |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2653 | kmeans_pixels[id][j].alpha+=QuantumScale*GetPixelAlpha(image,q); |
Cristy | e574a42 | 2019-12-19 19:26:53 -0500 | [diff] [blame] | 2654 | if (image->colorspace == CMYKColorspace) |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2655 | kmeans_pixels[id][j].black+=QuantumScale*GetPixelBlack(image,q); |
| 2656 | kmeans_pixels[id][j].count++; |
| 2657 | kmeans_pixels[id][j].distortion+=min_distance; |
Cristy | 746cd9c | 2019-12-20 21:24:43 -0500 | [diff] [blame] | 2658 | SetPixelIndex(image,(Quantum) j,q); |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2659 | q+=GetPixelChannels(image); |
| 2660 | } |
Cristy | 19aadb9 | 2019-12-27 16:57:46 -0500 | [diff] [blame] | 2661 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2662 | status=MagickFalse; |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2663 | } |
Cristy | 19aadb9 | 2019-12-27 16:57:46 -0500 | [diff] [blame] | 2664 | if (status == MagickFalse) |
| 2665 | break; |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2666 | /* |
Cristy | 193f395 | 2019-12-28 11:48:52 -0500 | [diff] [blame] | 2667 | Reduce sums to [0] entry. |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2668 | */ |
| 2669 | for (i=1; i < (ssize_t) number_threads; i++) |
| 2670 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2671 | ssize_t |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2672 | j; |
| 2673 | |
| 2674 | for (j=0; j < (ssize_t) image->colors; j++) |
| 2675 | { |
| 2676 | kmeans_pixels[0][j].red+=kmeans_pixels[i][j].red; |
| 2677 | kmeans_pixels[0][j].green+=kmeans_pixels[i][j].green; |
| 2678 | kmeans_pixels[0][j].blue+=kmeans_pixels[i][j].blue; |
| 2679 | if (image->alpha_trait != BlendPixelTrait) |
| 2680 | kmeans_pixels[0][j].alpha+=kmeans_pixels[i][j].alpha; |
| 2681 | if (image->colorspace == CMYKColorspace) |
| 2682 | kmeans_pixels[0][j].black+=kmeans_pixels[i][j].black; |
| 2683 | kmeans_pixels[0][j].count+=kmeans_pixels[i][j].count; |
| 2684 | kmeans_pixels[0][j].distortion+=kmeans_pixels[i][j].distortion; |
| 2685 | } |
| 2686 | } |
| 2687 | /* |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2688 | Calculate the new means (centroids) of the pixels in the new clusters. |
| 2689 | */ |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2690 | distortion=0.0; |
Cristy | e574a42 | 2019-12-19 19:26:53 -0500 | [diff] [blame] | 2691 | for (i=0; i < (ssize_t) image->colors; i++) |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2692 | { |
Cristy | 5871059 | 2019-12-21 10:04:11 -0500 | [diff] [blame] | 2693 | double |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2694 | gamma; |
Cristy | 5871059 | 2019-12-21 10:04:11 -0500 | [diff] [blame] | 2695 | |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2696 | gamma=PerceptibleReciprocal((double) kmeans_pixels[0][i].count); |
| 2697 | image->colormap[i].red=gamma*QuantumRange*kmeans_pixels[0][i].red; |
| 2698 | image->colormap[i].green=gamma*QuantumRange*kmeans_pixels[0][i].green; |
| 2699 | image->colormap[i].blue=gamma*QuantumRange*kmeans_pixels[0][i].blue; |
Cristy | e574a42 | 2019-12-19 19:26:53 -0500 | [diff] [blame] | 2700 | if (image->alpha_trait != BlendPixelTrait) |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2701 | image->colormap[i].alpha=gamma*QuantumRange*kmeans_pixels[0][i].alpha; |
Cristy | e574a42 | 2019-12-19 19:26:53 -0500 | [diff] [blame] | 2702 | if (image->colorspace == CMYKColorspace) |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2703 | image->colormap[i].black=gamma*QuantumRange*kmeans_pixels[0][i].black; |
| 2704 | distortion+=kmeans_pixels[0][i].distortion; |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2705 | } |
Cristy | dc157f0 | 2019-12-19 16:32:14 -0500 | [diff] [blame] | 2706 | if (verbose != MagickFalse) |
Cristy | 2f0e063 | 2019-12-28 13:59:38 -0500 | [diff] [blame] | 2707 | (void) FormatLocaleFile(stderr,"distortion[%.20g]: %*g %*g\n",(double) n, |
| 2708 | GetMagickPrecision(),distortion,GetMagickPrecision(), |
Cristy | 4bd8319 | 2019-12-31 13:33:57 -0500 | [diff] [blame] | 2709 | fabs(distortion-previous_tolerance)); |
| 2710 | if (fabs(distortion-previous_tolerance) <= tolerance) |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2711 | break; |
Cristy | 4bd8319 | 2019-12-31 13:33:57 -0500 | [diff] [blame] | 2712 | previous_tolerance=distortion; |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2713 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2714 | { |
| 2715 | MagickBooleanType |
| 2716 | proceed; |
| 2717 | |
Cristy | 746cd9c | 2019-12-20 21:24:43 -0500 | [diff] [blame] | 2718 | proceed=SetImageProgress(image,KmeansImageTag,(MagickOffsetType) n, |
| 2719 | max_iterations); |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2720 | if (proceed == MagickFalse) |
| 2721 | status=MagickFalse; |
| 2722 | } |
| 2723 | } |
Cristy | e574a42 | 2019-12-19 19:26:53 -0500 | [diff] [blame] | 2724 | image_view=DestroyCacheView(image_view); |
Cristy | f1f68eb | 2019-12-27 18:22:57 -0500 | [diff] [blame] | 2725 | kmeans_pixels=DestroyKmeansThreadSet(kmeans_pixels); |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2726 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
Cristy | 746cd9c | 2019-12-20 21:24:43 -0500 | [diff] [blame] | 2727 | (void) SetImageProgress(image,KmeansImageTag,(MagickOffsetType) |
| 2728 | max_iterations-1,max_iterations); |
Cristy | b5dffba | 2019-12-19 18:18:00 -0500 | [diff] [blame] | 2729 | if (status == MagickFalse) |
| 2730 | return(status); |
| 2731 | return(SyncImage(image,exception)); |
Cristy | ee6da04 | 2019-12-19 06:16:25 -0500 | [diff] [blame] | 2732 | } |
| 2733 | |
| 2734 | /* |
| 2735 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2736 | % % |
| 2737 | % % |
| 2738 | % % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2739 | % P o s t e r i z e I m a g e % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2740 | % % |
| 2741 | % % |
| 2742 | % % |
| 2743 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2744 | % |
| 2745 | % PosterizeImage() reduces the image to a limited number of colors for a |
| 2746 | % "poster" effect. |
| 2747 | % |
| 2748 | % The format of the PosterizeImage method is: |
| 2749 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2750 | % MagickBooleanType PosterizeImage(Image *image,const size_t levels, |
cristy | cbda611 | 2012-05-27 20:57:16 +0000 | [diff] [blame] | 2751 | % const DitherMethod dither_method,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2752 | % |
| 2753 | % A description of each parameter follows: |
| 2754 | % |
| 2755 | % o image: Specifies a pointer to an Image structure. |
| 2756 | % |
| 2757 | % o levels: Number of color levels allowed in each channel. Very low values |
| 2758 | % (2, 3, or 4) have the most visible effect. |
| 2759 | % |
cristy | cbda611 | 2012-05-27 20:57:16 +0000 | [diff] [blame] | 2760 | % o dither_method: choose from UndefinedDitherMethod, NoDitherMethod, |
| 2761 | % RiemersmaDitherMethod, FloydSteinbergDitherMethod. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2762 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2763 | % o exception: return any errors or warnings in this structure. |
| 2764 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2765 | */ |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2766 | |
cristy | 72844f1 | 2013-04-28 23:52:36 +0000 | [diff] [blame] | 2767 | static inline double MagickRound(double x) |
cristy | 4d72715 | 2011-02-10 19:57:21 +0000 | [diff] [blame] | 2768 | { |
| 2769 | /* |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 2770 | Round the fraction to nearest integer. |
cristy | 4d72715 | 2011-02-10 19:57:21 +0000 | [diff] [blame] | 2771 | */ |
cristy | ae0a3fc | 2013-04-29 08:29:35 +0000 | [diff] [blame] | 2772 | if ((x-floor(x)) < (ceil(x)-x)) |
cristy | 72844f1 | 2013-04-28 23:52:36 +0000 | [diff] [blame] | 2773 | return(floor(x)); |
| 2774 | return(ceil(x)); |
cristy | 4d72715 | 2011-02-10 19:57:21 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2777 | MagickExport MagickBooleanType PosterizeImage(Image *image,const size_t levels, |
cristy | cbda611 | 2012-05-27 20:57:16 +0000 | [diff] [blame] | 2778 | const DitherMethod dither_method,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2779 | { |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2780 | #define PosterizeImageTag "Posterize/Image" |
Cristy | 7b05869 | 2019-10-11 20:20:19 -0400 | [diff] [blame] | 2781 | #define PosterizePixel(pixel) ClampToQuantum((MagickRealType) QuantumRange*( \ |
| 2782 | MagickRound(QuantumScale*pixel*(levels-1)))/MagickMax((ssize_t) levels-1,1)) |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2783 | |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2784 | CacheView |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2785 | *image_view; |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 2786 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2787 | MagickBooleanType |
| 2788 | status; |
| 2789 | |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2790 | MagickOffsetType |
| 2791 | progress; |
| 2792 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2793 | QuantizeInfo |
| 2794 | *quantize_info; |
| 2795 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2796 | ssize_t |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 2797 | i; |
| 2798 | |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 2799 | ssize_t |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2800 | y; |
cristy | 847620f | 2011-02-09 02:24:21 +0000 | [diff] [blame] | 2801 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2802 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 2803 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2804 | if (image->debug != MagickFalse) |
| 2805 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 2806 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 2807 | assert(exception->signature == MagickCoreSignature); |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2808 | if (image->storage_class == PseudoClass) |
| 2809 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
Cristy | 38b42e1 | 2018-03-18 10:13:01 -0400 | [diff] [blame] | 2810 | #pragma omp parallel for schedule(static) shared(progress,status) \ |
Cristy | dce076e | 2017-10-10 19:45:45 -0400 | [diff] [blame] | 2811 | magick_number_threads(image,image,image->colors,1) |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2812 | #endif |
| 2813 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2814 | { |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2815 | /* |
| 2816 | Posterize colormap. |
| 2817 | */ |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2818 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 2819 | image->colormap[i].red=(double) |
| 2820 | PosterizePixel(image->colormap[i].red); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2821 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 2822 | image->colormap[i].green=(double) |
| 2823 | PosterizePixel(image->colormap[i].green); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2824 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 2825 | image->colormap[i].blue=(double) |
| 2826 | PosterizePixel(image->colormap[i].blue); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2827 | if ((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 2828 | image->colormap[i].alpha=(double) |
| 2829 | PosterizePixel(image->colormap[i].alpha); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2830 | } |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2831 | /* |
| 2832 | Posterize image. |
| 2833 | */ |
| 2834 | status=MagickTrue; |
| 2835 | progress=0; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 2836 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2837 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
Cristy | 38b42e1 | 2018-03-18 10:13:01 -0400 | [diff] [blame] | 2838 | #pragma omp parallel for schedule(static) shared(progress,status) \ |
Cristy | 8255ca9 | 2017-10-09 08:37:35 -0400 | [diff] [blame] | 2839 | magick_number_threads(image,image,image->rows,1) |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2840 | #endif |
| 2841 | for (y=0; y < (ssize_t) image->rows; y++) |
| 2842 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2843 | Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 2844 | *magick_restrict q; |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2845 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2846 | ssize_t |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2847 | x; |
| 2848 | |
| 2849 | if (status == MagickFalse) |
| 2850 | continue; |
| 2851 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 2852 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2853 | { |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2854 | status=MagickFalse; |
| 2855 | continue; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2856 | } |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2857 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2858 | { |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2859 | if ((GetPixelRedTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2860 | SetPixelRed(image,PosterizePixel(GetPixelRed(image,q)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2861 | if ((GetPixelGreenTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2862 | SetPixelGreen(image,PosterizePixel(GetPixelGreen(image,q)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2863 | if ((GetPixelBlueTraits(image) & UpdatePixelTrait) != 0) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2864 | SetPixelBlue(image,PosterizePixel(GetPixelBlue(image,q)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2865 | if (((GetPixelBlackTraits(image) & UpdatePixelTrait) != 0) && |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2866 | (image->colorspace == CMYKColorspace)) |
| 2867 | SetPixelBlack(image,PosterizePixel(GetPixelBlack(image,q)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2868 | if (((GetPixelAlphaTraits(image) & UpdatePixelTrait) != 0) && |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 2869 | (image->alpha_trait == BlendPixelTrait)) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 2870 | SetPixelAlpha(image,PosterizePixel(GetPixelAlpha(image,q)),q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 2871 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2872 | } |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2873 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 2874 | status=MagickFalse; |
| 2875 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 2876 | { |
| 2877 | MagickBooleanType |
| 2878 | proceed; |
| 2879 | |
Cristy | fc89eec | 2018-11-11 21:10:06 -0500 | [diff] [blame] | 2880 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 2881 | #pragma omp atomic |
| 2882 | #endif |
| 2883 | progress++; |
| 2884 | proceed=SetImageProgress(image,PosterizeImageTag,progress,image->rows); |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2885 | if (proceed == MagickFalse) |
| 2886 | status=MagickFalse; |
| 2887 | } |
| 2888 | } |
| 2889 | image_view=DestroyCacheView(image_view); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2890 | quantize_info=AcquireQuantizeInfo((ImageInfo *) NULL); |
cristy | d1a2c0f | 2011-02-09 14:14:50 +0000 | [diff] [blame] | 2891 | quantize_info->number_colors=(size_t) MagickMin((ssize_t) levels*levels* |
| 2892 | levels,MaxColormapSize+1); |
cristy | cbda611 | 2012-05-27 20:57:16 +0000 | [diff] [blame] | 2893 | quantize_info->dither_method=dither_method; |
cristy | 3e9cad0 | 2011-02-20 01:42:00 +0000 | [diff] [blame] | 2894 | quantize_info->tree_depth=MaxTreeDepth; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 2895 | status=QuantizeImage(quantize_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2896 | quantize_info=DestroyQuantizeInfo(quantize_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2897 | return(status); |
| 2898 | } |
| 2899 | |
| 2900 | /* |
| 2901 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2902 | % % |
| 2903 | % % |
| 2904 | % % |
| 2905 | + P r u n e C h i l d % |
| 2906 | % % |
| 2907 | % % |
| 2908 | % % |
| 2909 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2910 | % |
| 2911 | % PruneChild() deletes the given node and merges its statistics into its |
| 2912 | % parent. |
| 2913 | % |
| 2914 | % The format of the PruneSubtree method is: |
| 2915 | % |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 2916 | % PruneChild(CubeInfo *cube_info,const NodeInfo *node_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2917 | % |
| 2918 | % A description of each parameter follows. |
| 2919 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2920 | % o cube_info: A pointer to the Cube structure. |
| 2921 | % |
| 2922 | % o node_info: pointer to node in color cube tree that is to be pruned. |
| 2923 | % |
| 2924 | */ |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 2925 | static void PruneChild(CubeInfo *cube_info,const NodeInfo *node_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2926 | { |
| 2927 | NodeInfo |
| 2928 | *parent; |
| 2929 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2930 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2931 | i; |
| 2932 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2933 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2934 | number_children; |
| 2935 | |
| 2936 | /* |
| 2937 | Traverse any children. |
| 2938 | */ |
| 2939 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2940 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2941 | if (node_info->child[i] != (NodeInfo *) NULL) |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 2942 | PruneChild(cube_info,node_info->child[i]); |
Cristy | 9f57fa7 | 2020-09-02 19:22:51 -0400 | [diff] [blame] | 2943 | /* |
| 2944 | Merge color statistics into parent. |
| 2945 | */ |
| 2946 | parent=node_info->parent; |
| 2947 | parent->number_unique+=node_info->number_unique; |
| 2948 | parent->total_color.red+=node_info->total_color.red; |
| 2949 | parent->total_color.green+=node_info->total_color.green; |
| 2950 | parent->total_color.blue+=node_info->total_color.blue; |
| 2951 | parent->total_color.alpha+=node_info->total_color.alpha; |
| 2952 | parent->child[node_info->id]=(NodeInfo *) NULL; |
| 2953 | cube_info->nodes--; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | /* |
| 2957 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2958 | % % |
| 2959 | % % |
| 2960 | % % |
| 2961 | + P r u n e L e v e l % |
| 2962 | % % |
| 2963 | % % |
| 2964 | % % |
| 2965 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2966 | % |
| 2967 | % PruneLevel() deletes all nodes at the bottom level of the color tree merging |
| 2968 | % their color statistics into their parent node. |
| 2969 | % |
| 2970 | % The format of the PruneLevel method is: |
| 2971 | % |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 2972 | % PruneLevel(CubeInfo *cube_info,const NodeInfo *node_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2973 | % |
| 2974 | % A description of each parameter follows. |
| 2975 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2976 | % o cube_info: A pointer to the Cube structure. |
| 2977 | % |
| 2978 | % o node_info: pointer to node in color cube tree that is to be pruned. |
| 2979 | % |
| 2980 | */ |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 2981 | static void PruneLevel(CubeInfo *cube_info,const NodeInfo *node_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2982 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 2983 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2984 | i; |
| 2985 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2986 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2987 | number_children; |
| 2988 | |
| 2989 | /* |
| 2990 | Traverse any children. |
| 2991 | */ |
| 2992 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 2993 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2994 | if (node_info->child[i] != (NodeInfo *) NULL) |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 2995 | PruneLevel(cube_info,node_info->child[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2996 | if (node_info->level == cube_info->depth) |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 2997 | PruneChild(cube_info,node_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | /* |
| 3001 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3002 | % % |
| 3003 | % % |
| 3004 | % % |
| 3005 | + P r u n e T o C u b e D e p t h % |
| 3006 | % % |
| 3007 | % % |
| 3008 | % % |
| 3009 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3010 | % |
| 3011 | % PruneToCubeDepth() deletes any nodes at a depth greater than |
| 3012 | % cube_info->depth while merging their color statistics into their parent |
| 3013 | % node. |
| 3014 | % |
| 3015 | % The format of the PruneToCubeDepth method is: |
| 3016 | % |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 3017 | % PruneToCubeDepth(CubeInfo *cube_info,const NodeInfo *node_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3018 | % |
| 3019 | % A description of each parameter follows. |
| 3020 | % |
| 3021 | % o cube_info: A pointer to the Cube structure. |
| 3022 | % |
| 3023 | % o node_info: pointer to node in color cube tree that is to be pruned. |
| 3024 | % |
| 3025 | */ |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 3026 | static void PruneToCubeDepth(CubeInfo *cube_info,const NodeInfo *node_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3027 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 3028 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3029 | i; |
| 3030 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3031 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3032 | number_children; |
| 3033 | |
| 3034 | /* |
| 3035 | Traverse any children. |
| 3036 | */ |
| 3037 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3038 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3039 | if (node_info->child[i] != (NodeInfo *) NULL) |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 3040 | PruneToCubeDepth(cube_info,node_info->child[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3041 | if (node_info->level > cube_info->depth) |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 3042 | PruneChild(cube_info,node_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | /* |
| 3046 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3047 | % % |
| 3048 | % % |
| 3049 | % % |
| 3050 | % Q u a n t i z e I m a g e % |
| 3051 | % % |
| 3052 | % % |
| 3053 | % % |
| 3054 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3055 | % |
| 3056 | % QuantizeImage() analyzes the colors within a reference image and chooses a |
| 3057 | % fixed number of colors to represent the image. The goal of the algorithm |
| 3058 | % is to minimize the color difference between the input and output image while |
| 3059 | % minimizing the processing time. |
| 3060 | % |
| 3061 | % The format of the QuantizeImage method is: |
| 3062 | % |
| 3063 | % MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3064 | % Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3065 | % |
| 3066 | % A description of each parameter follows: |
| 3067 | % |
| 3068 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 3069 | % |
| 3070 | % o image: the image. |
| 3071 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3072 | % o exception: return any errors or warnings in this structure. |
| 3073 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3074 | */ |
| 3075 | MagickExport MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3076 | Image *image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3077 | { |
| 3078 | CubeInfo |
| 3079 | *cube_info; |
| 3080 | |
| 3081 | MagickBooleanType |
| 3082 | status; |
| 3083 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3084 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3085 | depth, |
| 3086 | maximum_colors; |
| 3087 | |
| 3088 | assert(quantize_info != (const QuantizeInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3089 | assert(quantize_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3090 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3091 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3092 | if (image->debug != MagickFalse) |
| 3093 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3094 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3095 | assert(exception->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3096 | maximum_colors=quantize_info->number_colors; |
| 3097 | if (maximum_colors == 0) |
| 3098 | maximum_colors=MaxColormapSize; |
| 3099 | if (maximum_colors > MaxColormapSize) |
| 3100 | maximum_colors=MaxColormapSize; |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3101 | if (image->alpha_trait != BlendPixelTrait) |
cristy | 400e701 | 2012-08-08 13:41:59 +0000 | [diff] [blame] | 3102 | { |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 3103 | if (SetImageGray(image,exception) != MagickFalse) |
cristy | 400e701 | 2012-08-08 13:41:59 +0000 | [diff] [blame] | 3104 | (void) SetGrayscaleImage(image,exception); |
| 3105 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3106 | depth=quantize_info->tree_depth; |
| 3107 | if (depth == 0) |
| 3108 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3109 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3110 | colors; |
| 3111 | |
| 3112 | /* |
| 3113 | Depth of color tree is: Log4(colormap size)+2. |
| 3114 | */ |
| 3115 | colors=maximum_colors; |
| 3116 | for (depth=1; colors != 0; depth++) |
| 3117 | colors>>=2; |
cristy | cbda611 | 2012-05-27 20:57:16 +0000 | [diff] [blame] | 3118 | if ((quantize_info->dither_method != NoDitherMethod) && (depth > 2)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3119 | depth--; |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3120 | if ((image->alpha_trait == BlendPixelTrait) && (depth > 5)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3121 | depth--; |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 3122 | if (SetImageGray(image,exception) != MagickFalse) |
cristy | a8be0ae | 2014-04-02 21:10:45 +0000 | [diff] [blame] | 3123 | depth=MaxTreeDepth; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3124 | } |
| 3125 | /* |
| 3126 | Initialize color cube. |
| 3127 | */ |
| 3128 | cube_info=GetCubeInfo(quantize_info,depth,maximum_colors); |
| 3129 | if (cube_info == (CubeInfo *) NULL) |
| 3130 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3131 | image->filename); |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 3132 | status=ClassifyImageColors(cube_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3133 | if (status != MagickFalse) |
| 3134 | { |
| 3135 | /* |
Cristy | 8eb55f5 | 2019-10-12 14:14:41 -0400 | [diff] [blame] | 3136 | Reduce the number of colors in the image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3137 | */ |
dirk | c772059 | 2015-07-06 20:46:23 +0000 | [diff] [blame] | 3138 | if (cube_info->colors > cube_info->maximum_colors) |
dirk | bce0859 | 2015-06-27 20:26:03 +0000 | [diff] [blame] | 3139 | ReduceImageColors(image,cube_info); |
dirk | c772059 | 2015-07-06 20:46:23 +0000 | [diff] [blame] | 3140 | status=AssignImageColors(image,cube_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3141 | } |
| 3142 | DestroyCubeInfo(cube_info); |
| 3143 | return(status); |
| 3144 | } |
| 3145 | |
| 3146 | /* |
| 3147 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3148 | % % |
| 3149 | % % |
| 3150 | % % |
| 3151 | % Q u a n t i z e I m a g e s % |
| 3152 | % % |
| 3153 | % % |
| 3154 | % % |
| 3155 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3156 | % |
| 3157 | % QuantizeImages() analyzes the colors within a set of reference images and |
| 3158 | % chooses a fixed number of colors to represent the set. The goal of the |
| 3159 | % algorithm is to minimize the color difference between the input and output |
| 3160 | % images while minimizing the processing time. |
| 3161 | % |
| 3162 | % The format of the QuantizeImages method is: |
| 3163 | % |
| 3164 | % MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3165 | % Image *images,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3166 | % |
| 3167 | % A description of each parameter follows: |
| 3168 | % |
| 3169 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 3170 | % |
| 3171 | % o images: Specifies a pointer to a list of Image structures. |
| 3172 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3173 | % o exception: return any errors or warnings in this structure. |
| 3174 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3175 | */ |
| 3176 | MagickExport MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3177 | Image *images,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3178 | { |
| 3179 | CubeInfo |
| 3180 | *cube_info; |
| 3181 | |
| 3182 | Image |
| 3183 | *image; |
| 3184 | |
| 3185 | MagickBooleanType |
| 3186 | proceed, |
| 3187 | status; |
| 3188 | |
| 3189 | MagickProgressMonitor |
| 3190 | progress_monitor; |
| 3191 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 3192 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3193 | i; |
| 3194 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3195 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3196 | depth, |
| 3197 | maximum_colors, |
| 3198 | number_images; |
| 3199 | |
| 3200 | assert(quantize_info != (const QuantizeInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3201 | assert(quantize_info->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3202 | assert(images != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3203 | assert(images->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3204 | if (images->debug != MagickFalse) |
| 3205 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename); |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3206 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3207 | assert(exception->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3208 | if (GetNextImageInList(images) == (Image *) NULL) |
| 3209 | { |
| 3210 | /* |
| 3211 | Handle a single image with QuantizeImage. |
| 3212 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3213 | status=QuantizeImage(quantize_info,images,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3214 | return(status); |
| 3215 | } |
| 3216 | status=MagickFalse; |
| 3217 | maximum_colors=quantize_info->number_colors; |
| 3218 | if (maximum_colors == 0) |
| 3219 | maximum_colors=MaxColormapSize; |
| 3220 | if (maximum_colors > MaxColormapSize) |
| 3221 | maximum_colors=MaxColormapSize; |
| 3222 | depth=quantize_info->tree_depth; |
| 3223 | if (depth == 0) |
| 3224 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3225 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3226 | colors; |
| 3227 | |
| 3228 | /* |
| 3229 | Depth of color tree is: Log4(colormap size)+2. |
| 3230 | */ |
| 3231 | colors=maximum_colors; |
| 3232 | for (depth=1; colors != 0; depth++) |
| 3233 | colors>>=2; |
cristy | cbda611 | 2012-05-27 20:57:16 +0000 | [diff] [blame] | 3234 | if (quantize_info->dither_method != NoDitherMethod) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3235 | depth--; |
| 3236 | } |
| 3237 | /* |
| 3238 | Initialize color cube. |
| 3239 | */ |
| 3240 | cube_info=GetCubeInfo(quantize_info,depth,maximum_colors); |
| 3241 | if (cube_info == (CubeInfo *) NULL) |
| 3242 | { |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 3243 | (void) ThrowMagickException(exception,GetMagickModule(), |
cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 3244 | ResourceLimitError,"MemoryAllocationFailed","`%s'",images->filename); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3245 | return(MagickFalse); |
| 3246 | } |
| 3247 | number_images=GetImageListLength(images); |
| 3248 | image=images; |
| 3249 | for (i=0; image != (Image *) NULL; i++) |
| 3250 | { |
| 3251 | progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor) NULL, |
| 3252 | image->client_data); |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 3253 | status=ClassifyImageColors(cube_info,image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3254 | if (status == MagickFalse) |
| 3255 | break; |
| 3256 | (void) SetImageProgressMonitor(image,progress_monitor,image->client_data); |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 3257 | proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i, |
| 3258 | number_images); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3259 | if (proceed == MagickFalse) |
| 3260 | break; |
| 3261 | image=GetNextImageInList(image); |
| 3262 | } |
| 3263 | if (status != MagickFalse) |
| 3264 | { |
| 3265 | /* |
| 3266 | Reduce the number of colors in an image sequence. |
| 3267 | */ |
| 3268 | ReduceImageColors(images,cube_info); |
| 3269 | image=images; |
| 3270 | for (i=0; image != (Image *) NULL; i++) |
| 3271 | { |
| 3272 | progress_monitor=SetImageProgressMonitor(image,(MagickProgressMonitor) |
| 3273 | NULL,image->client_data); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3274 | status=AssignImageColors(image,cube_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3275 | if (status == MagickFalse) |
| 3276 | break; |
| 3277 | (void) SetImageProgressMonitor(image,progress_monitor, |
| 3278 | image->client_data); |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 3279 | proceed=SetImageProgress(image,AssignImageTag,(MagickOffsetType) i, |
| 3280 | number_images); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3281 | if (proceed == MagickFalse) |
| 3282 | break; |
| 3283 | image=GetNextImageInList(image); |
| 3284 | } |
| 3285 | } |
| 3286 | DestroyCubeInfo(cube_info); |
| 3287 | return(status); |
| 3288 | } |
| 3289 | |
| 3290 | /* |
| 3291 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3292 | % % |
| 3293 | % % |
| 3294 | % % |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3295 | + Q u a n t i z e E r r o r F l a t t e n % |
| 3296 | % % |
| 3297 | % % |
| 3298 | % % |
| 3299 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3300 | % |
| 3301 | % QuantizeErrorFlatten() traverses the color cube and flattens the quantization |
cristy | a5573bc | 2013-10-26 14:13:34 +0000 | [diff] [blame] | 3302 | % error into a sorted 1D array. This accelerates the color reduction process. |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3303 | % |
| 3304 | % Contributed by Yoya. |
| 3305 | % |
cristy | 46a9a15 | 2015-02-01 15:02:17 +0000 | [diff] [blame] | 3306 | % The format of the QuantizeErrorFlatten method is: |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3307 | % |
dirk | 5289ead | 2015-11-22 10:51:52 +0100 | [diff] [blame] | 3308 | % size_t QuantizeErrorFlatten(const CubeInfo *cube_info, |
cristy | 0690986 | 2013-10-25 21:23:03 +0000 | [diff] [blame] | 3309 | % const NodeInfo *node_info,const ssize_t offset, |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3310 | % double *quantize_error) |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3311 | % |
| 3312 | % A description of each parameter follows. |
| 3313 | % |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3314 | % o cube_info: A pointer to the Cube structure. |
| 3315 | % |
| 3316 | % o node_info: pointer to node in color cube tree that is current pointer. |
| 3317 | % |
| 3318 | % o offset: quantize error offset. |
| 3319 | % |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3320 | % o quantize_error: the quantization error vector. |
| 3321 | % |
| 3322 | */ |
dirk | 5289ead | 2015-11-22 10:51:52 +0100 | [diff] [blame] | 3323 | static size_t QuantizeErrorFlatten(const CubeInfo *cube_info, |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3324 | const NodeInfo *node_info,const ssize_t offset,double *quantize_error) |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3325 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 3326 | ssize_t |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3327 | i; |
| 3328 | |
| 3329 | size_t |
| 3330 | n, |
| 3331 | number_children; |
| 3332 | |
cristy | 0690986 | 2013-10-25 21:23:03 +0000 | [diff] [blame] | 3333 | if (offset >= (ssize_t) cube_info->nodes) |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3334 | return(0); |
| 3335 | quantize_error[offset]=node_info->quantize_error; |
| 3336 | n=1; |
| 3337 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
| 3338 | for (i=0; i < (ssize_t) number_children ; i++) |
| 3339 | if (node_info->child[i] != (NodeInfo *) NULL) |
dirk | 5289ead | 2015-11-22 10:51:52 +0100 | [diff] [blame] | 3340 | n+=QuantizeErrorFlatten(cube_info,node_info->child[i],offset+n, |
cristy | 0690986 | 2013-10-25 21:23:03 +0000 | [diff] [blame] | 3341 | quantize_error); |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3342 | return(n); |
| 3343 | } |
| 3344 | |
| 3345 | /* |
| 3346 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3347 | % % |
| 3348 | % % |
| 3349 | % % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3350 | + R e d u c e % |
| 3351 | % % |
| 3352 | % % |
| 3353 | % % |
| 3354 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3355 | % |
| 3356 | % Reduce() traverses the color cube tree and prunes any node whose |
| 3357 | % quantization error falls below a particular threshold. |
| 3358 | % |
| 3359 | % The format of the Reduce method is: |
| 3360 | % |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 3361 | % Reduce(CubeInfo *cube_info,const NodeInfo *node_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3362 | % |
| 3363 | % A description of each parameter follows. |
| 3364 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3365 | % o cube_info: A pointer to the Cube structure. |
| 3366 | % |
| 3367 | % o node_info: pointer to node in color cube tree that is to be pruned. |
| 3368 | % |
| 3369 | */ |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 3370 | static void Reduce(CubeInfo *cube_info,const NodeInfo *node_info) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3371 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 3372 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3373 | i; |
| 3374 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3375 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3376 | number_children; |
| 3377 | |
| 3378 | /* |
| 3379 | Traverse any children. |
| 3380 | */ |
| 3381 | number_children=cube_info->associate_alpha == MagickFalse ? 8UL : 16UL; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3382 | for (i=0; i < (ssize_t) number_children; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3383 | if (node_info->child[i] != (NodeInfo *) NULL) |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 3384 | Reduce(cube_info,node_info->child[i]); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3385 | if (node_info->quantize_error <= cube_info->pruning_threshold) |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 3386 | PruneChild(cube_info,node_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3387 | else |
| 3388 | { |
| 3389 | /* |
| 3390 | Find minimum pruning threshold. |
| 3391 | */ |
| 3392 | if (node_info->number_unique > 0) |
| 3393 | cube_info->colors++; |
| 3394 | if (node_info->quantize_error < cube_info->next_threshold) |
| 3395 | cube_info->next_threshold=node_info->quantize_error; |
| 3396 | } |
| 3397 | } |
| 3398 | |
| 3399 | /* |
| 3400 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3401 | % % |
| 3402 | % % |
| 3403 | % % |
| 3404 | + R e d u c e I m a g e C o l o r s % |
| 3405 | % % |
| 3406 | % % |
| 3407 | % % |
| 3408 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3409 | % |
| 3410 | % ReduceImageColors() repeatedly prunes the tree until the number of nodes |
| 3411 | % with n2 > 0 is less than or equal to the maximum number of colors allowed |
| 3412 | % in the output image. On any given iteration over the tree, it selects |
| 3413 | % those nodes whose E value is minimal for pruning and merges their |
| 3414 | % color statistics upward. It uses a pruning threshold, Ep, to govern |
| 3415 | % node selection as follows: |
| 3416 | % |
| 3417 | % Ep = 0 |
| 3418 | % while number of nodes with (n2 > 0) > required maximum number of colors |
| 3419 | % prune all nodes such that E <= Ep |
| 3420 | % Set Ep to minimum E in remaining nodes |
| 3421 | % |
| 3422 | % This has the effect of minimizing any quantization error when merging |
| 3423 | % two nodes together. |
| 3424 | % |
| 3425 | % When a node to be pruned has offspring, the pruning procedure invokes |
| 3426 | % itself recursively in order to prune the tree from the leaves upward. |
| 3427 | % n2, Sr, Sg, and Sb in a node being pruned are always added to the |
| 3428 | % corresponding data in that node's parent. This retains the pruned |
| 3429 | % node's color characteristics for later averaging. |
| 3430 | % |
| 3431 | % For each node, n2 pixels exist for which that node represents the |
| 3432 | % smallest volume in RGB space containing those pixel's colors. When n2 |
| 3433 | % > 0 the node will uniquely define a color in the output image. At the |
| 3434 | % beginning of reduction, n2 = 0 for all nodes except a the leaves of |
| 3435 | % the tree which represent colors present in the input image. |
| 3436 | % |
| 3437 | % The other pixel count, n1, indicates the total number of colors |
| 3438 | % within the cubic volume which the node represents. This includes n1 - |
| 3439 | % n2 pixels whose colors should be defined by nodes at a lower level in |
| 3440 | % the tree. |
| 3441 | % |
| 3442 | % The format of the ReduceImageColors method is: |
| 3443 | % |
| 3444 | % ReduceImageColors(const Image *image,CubeInfo *cube_info) |
| 3445 | % |
| 3446 | % A description of each parameter follows. |
| 3447 | % |
| 3448 | % o image: the image. |
| 3449 | % |
| 3450 | % o cube_info: A pointer to the Cube structure. |
| 3451 | % |
| 3452 | */ |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3453 | |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3454 | static int QuantizeErrorCompare(const void *error_p,const void *error_q) |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3455 | { |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3456 | double |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3457 | *p, |
| 3458 | *q; |
| 3459 | |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3460 | p=(double *) error_p; |
| 3461 | q=(double *) error_q; |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3462 | if (*p > *q) |
| 3463 | return(1); |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3464 | if (fabs(*q-*p) <= MagickEpsilon) |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3465 | return(0); |
| 3466 | return(-1); |
| 3467 | } |
| 3468 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3469 | static void ReduceImageColors(const Image *image,CubeInfo *cube_info) |
| 3470 | { |
| 3471 | #define ReduceImageTag "Reduce/Image" |
| 3472 | |
| 3473 | MagickBooleanType |
| 3474 | proceed; |
| 3475 | |
| 3476 | MagickOffsetType |
| 3477 | offset; |
| 3478 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3479 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3480 | span; |
| 3481 | |
| 3482 | cube_info->next_threshold=0.0; |
cristy | 4c47510 | 2015-01-11 15:08:21 +0000 | [diff] [blame] | 3483 | if (cube_info->colors > cube_info->maximum_colors) |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3484 | { |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3485 | double |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3486 | *quantize_error; |
| 3487 | |
| 3488 | /* |
| 3489 | Enable rapid reduction of the number of unique colors. |
| 3490 | */ |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3491 | quantize_error=(double *) AcquireQuantumMemory(cube_info->nodes, |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3492 | sizeof(*quantize_error)); |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3493 | if (quantize_error != (double *) NULL) |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3494 | { |
dirk | 5289ead | 2015-11-22 10:51:52 +0100 | [diff] [blame] | 3495 | (void) QuantizeErrorFlatten(cube_info,cube_info->root,0, |
cristy | 0690986 | 2013-10-25 21:23:03 +0000 | [diff] [blame] | 3496 | quantize_error); |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3497 | qsort(quantize_error,cube_info->nodes,sizeof(double), |
| 3498 | QuantizeErrorCompare); |
cristy | 4c47510 | 2015-01-11 15:08:21 +0000 | [diff] [blame] | 3499 | if (cube_info->nodes > (110*(cube_info->maximum_colors+1)/100)) |
| 3500 | cube_info->next_threshold=quantize_error[cube_info->nodes-110* |
| 3501 | (cube_info->maximum_colors+1)/100]; |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3502 | quantize_error=(double *) RelinquishMagickMemory(quantize_error); |
cristy | b2f9ca8 | 2013-10-25 14:23:25 +0000 | [diff] [blame] | 3503 | } |
| 3504 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3505 | for (span=cube_info->colors; cube_info->colors > cube_info->maximum_colors; ) |
| 3506 | { |
| 3507 | cube_info->pruning_threshold=cube_info->next_threshold; |
| 3508 | cube_info->next_threshold=cube_info->root->quantize_error-1; |
| 3509 | cube_info->colors=0; |
dirk | f20fa43 | 2015-11-22 11:03:51 +0100 | [diff] [blame] | 3510 | Reduce(cube_info,cube_info->root); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3511 | offset=(MagickOffsetType) span-cube_info->colors; |
| 3512 | proceed=SetImageProgress(image,ReduceImageTag,offset,span- |
| 3513 | cube_info->maximum_colors+1); |
| 3514 | if (proceed == MagickFalse) |
| 3515 | break; |
| 3516 | } |
| 3517 | } |
| 3518 | |
| 3519 | /* |
| 3520 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3521 | % % |
| 3522 | % % |
| 3523 | % % |
| 3524 | % R e m a p I m a g e % |
| 3525 | % % |
| 3526 | % % |
| 3527 | % % |
| 3528 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3529 | % |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3530 | % RemapImage() replaces the colors of an image with the closest of the colors |
| 3531 | % from the reference image. |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3532 | % |
| 3533 | % The format of the RemapImage method is: |
| 3534 | % |
| 3535 | % MagickBooleanType RemapImage(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3536 | % Image *image,const Image *remap_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3537 | % |
| 3538 | % A description of each parameter follows: |
| 3539 | % |
| 3540 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 3541 | % |
| 3542 | % o image: the image. |
| 3543 | % |
| 3544 | % o remap_image: the reference image. |
| 3545 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3546 | % o exception: return any errors or warnings in this structure. |
| 3547 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3548 | */ |
| 3549 | MagickExport MagickBooleanType RemapImage(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3550 | Image *image,const Image *remap_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3551 | { |
| 3552 | CubeInfo |
| 3553 | *cube_info; |
| 3554 | |
| 3555 | MagickBooleanType |
| 3556 | status; |
| 3557 | |
| 3558 | /* |
| 3559 | Initialize color cube. |
| 3560 | */ |
| 3561 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3562 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3563 | if (image->debug != MagickFalse) |
| 3564 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 3565 | assert(remap_image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3566 | assert(remap_image->signature == MagickCoreSignature); |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3567 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3568 | assert(exception->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3569 | cube_info=GetCubeInfo(quantize_info,MaxTreeDepth, |
| 3570 | quantize_info->number_colors); |
| 3571 | if (cube_info == (CubeInfo *) NULL) |
| 3572 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3573 | image->filename); |
cristy | 8a11cb1 | 2011-10-19 23:53:34 +0000 | [diff] [blame] | 3574 | status=ClassifyImageColors(cube_info,remap_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3575 | if (status != MagickFalse) |
| 3576 | { |
| 3577 | /* |
| 3578 | Classify image colors from the reference image. |
| 3579 | */ |
| 3580 | cube_info->quantize_info->number_colors=cube_info->colors; |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3581 | status=AssignImageColors(image,cube_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3582 | } |
| 3583 | DestroyCubeInfo(cube_info); |
| 3584 | return(status); |
| 3585 | } |
| 3586 | |
| 3587 | /* |
| 3588 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3589 | % % |
| 3590 | % % |
| 3591 | % % |
| 3592 | % R e m a p I m a g e s % |
| 3593 | % % |
| 3594 | % % |
| 3595 | % % |
| 3596 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3597 | % |
| 3598 | % RemapImages() replaces the colors of a sequence of images with the |
| 3599 | % closest color from a reference image. |
| 3600 | % |
| 3601 | % The format of the RemapImage method is: |
| 3602 | % |
| 3603 | % MagickBooleanType RemapImages(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3604 | % Image *images,Image *remap_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3605 | % |
| 3606 | % A description of each parameter follows: |
| 3607 | % |
| 3608 | % o quantize_info: Specifies a pointer to an QuantizeInfo structure. |
| 3609 | % |
| 3610 | % o images: the image sequence. |
| 3611 | % |
| 3612 | % o remap_image: the reference image. |
| 3613 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3614 | % o exception: return any errors or warnings in this structure. |
| 3615 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3616 | */ |
| 3617 | MagickExport MagickBooleanType RemapImages(const QuantizeInfo *quantize_info, |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3618 | Image *images,const Image *remap_image,ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3619 | { |
| 3620 | CubeInfo |
| 3621 | *cube_info; |
| 3622 | |
| 3623 | Image |
| 3624 | *image; |
| 3625 | |
| 3626 | MagickBooleanType |
| 3627 | status; |
| 3628 | |
| 3629 | assert(images != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3630 | assert(images->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3631 | if (images->debug != MagickFalse) |
| 3632 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",images->filename); |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3633 | assert(exception != (ExceptionInfo *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3634 | assert(exception->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3635 | image=images; |
| 3636 | if (remap_image == (Image *) NULL) |
| 3637 | { |
| 3638 | /* |
| 3639 | Create a global colormap for an image sequence. |
| 3640 | */ |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3641 | status=QuantizeImages(quantize_info,images,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3642 | return(status); |
| 3643 | } |
| 3644 | /* |
| 3645 | Classify image colors from the reference image. |
| 3646 | */ |
| 3647 | cube_info=GetCubeInfo(quantize_info,MaxTreeDepth, |
| 3648 | quantize_info->number_colors); |
| 3649 | if (cube_info == (CubeInfo *) NULL) |
| 3650 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3651 | image->filename); |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3652 | status=ClassifyImageColors(cube_info,remap_image,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3653 | if (status != MagickFalse) |
| 3654 | { |
| 3655 | /* |
| 3656 | Classify image colors from the reference image. |
| 3657 | */ |
| 3658 | cube_info->quantize_info->number_colors=cube_info->colors; |
| 3659 | image=images; |
| 3660 | for ( ; image != (Image *) NULL; image=GetNextImageInList(image)) |
| 3661 | { |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3662 | status=AssignImageColors(image,cube_info,exception); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3663 | if (status == MagickFalse) |
| 3664 | break; |
| 3665 | } |
| 3666 | } |
| 3667 | DestroyCubeInfo(cube_info); |
| 3668 | return(status); |
| 3669 | } |
| 3670 | |
| 3671 | /* |
| 3672 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3673 | % % |
| 3674 | % % |
| 3675 | % % |
| 3676 | % S e t G r a y s c a l e I m a g e % |
| 3677 | % % |
| 3678 | % % |
| 3679 | % % |
| 3680 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3681 | % |
| 3682 | % SetGrayscaleImage() converts an image to a PseudoClass grayscale image. |
| 3683 | % |
| 3684 | % The format of the SetGrayscaleImage method is: |
| 3685 | % |
cristy | 09e8124 | 2015-01-31 17:00:00 +0000 | [diff] [blame] | 3686 | % MagickBooleanType SetGrayscaleImage(Image *image, |
| 3687 | % ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3688 | % |
| 3689 | % A description of each parameter follows: |
| 3690 | % |
| 3691 | % o image: The image. |
| 3692 | % |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3693 | % o exception: return any errors or warnings in this structure. |
| 3694 | % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3695 | */ |
| 3696 | |
| 3697 | #if defined(__cplusplus) || defined(c_plusplus) |
| 3698 | extern "C" { |
| 3699 | #endif |
| 3700 | |
| 3701 | static int IntensityCompare(const void *x,const void *y) |
| 3702 | { |
Cristy | fb2d857 | 2019-10-28 14:28:34 -0400 | [diff] [blame] | 3703 | double |
| 3704 | intensity; |
| 3705 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3706 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3707 | *color_1, |
| 3708 | *color_2; |
| 3709 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3710 | color_1=(PixelInfo *) x; |
| 3711 | color_2=(PixelInfo *) y; |
Cristy | fb2d857 | 2019-10-28 14:28:34 -0400 | [diff] [blame] | 3712 | intensity=GetPixelInfoIntensity((const Image *) NULL,color_1)- |
| 3713 | GetPixelInfoIntensity((const Image *) NULL,color_2); |
| 3714 | if (intensity < (double) INT_MIN) |
| 3715 | intensity=(double) INT_MIN; |
| 3716 | if (intensity > (double) INT_MAX) |
| 3717 | intensity=(double) INT_MAX; |
cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 3718 | return((int) intensity); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | #if defined(__cplusplus) || defined(c_plusplus) |
| 3722 | } |
| 3723 | #endif |
| 3724 | |
cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 3725 | static MagickBooleanType SetGrayscaleImage(Image *image, |
| 3726 | ExceptionInfo *exception) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3727 | { |
cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 3728 | CacheView |
| 3729 | *image_view; |
| 3730 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 3731 | MagickBooleanType |
| 3732 | status; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3733 | |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3734 | PixelInfo |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3735 | *colormap; |
| 3736 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 3737 | ssize_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3738 | i; |
| 3739 | |
Cristy | 7dbf5b2 | 2019-05-11 08:26:39 -0400 | [diff] [blame] | 3740 | size_t |
| 3741 | extent; |
| 3742 | |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 3743 | ssize_t |
| 3744 | *colormap_index, |
| 3745 | j, |
| 3746 | y; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3747 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3748 | assert(image != (Image *) NULL); |
cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 3749 | assert(image->signature == MagickCoreSignature); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3750 | if (image->type != GrayscaleType) |
Cristy | beb4c2b | 2017-12-26 19:43:17 -0500 | [diff] [blame] | 3751 | (void) TransformImageColorspace(image,GRAYColorspace,exception); |
Cristy | 7dbf5b2 | 2019-05-11 08:26:39 -0400 | [diff] [blame] | 3752 | extent=MagickMax(image->colors+1,MagickMax(MaxColormapSize,MaxMap+1)); |
| 3753 | colormap_index=(ssize_t *) AcquireQuantumMemory(extent, |
| 3754 | sizeof(*colormap_index)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3755 | if (colormap_index == (ssize_t *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3756 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3757 | image->filename); |
| 3758 | if (image->storage_class != PseudoClass) |
| 3759 | { |
Cristy | 7dbf5b2 | 2019-05-11 08:26:39 -0400 | [diff] [blame] | 3760 | (void) memset(colormap_index,(-1),extent*sizeof(*colormap_index)); |
dirk | d515240 | 2015-07-04 10:05:10 +0000 | [diff] [blame] | 3761 | if (AcquireImageColormap(image,MaxColormapSize,exception) == MagickFalse) |
Cristy | 0417cea | 2017-07-17 13:47:41 -0400 | [diff] [blame] | 3762 | { |
| 3763 | colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index); |
| 3764 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3765 | image->filename); |
| 3766 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3767 | image->colors=0; |
| 3768 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 3769 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3770 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
Cristy | 38b42e1 | 2018-03-18 10:13:01 -0400 | [diff] [blame] | 3771 | #pragma omp parallel for schedule(static) shared(status) \ |
Cristy | 8255ca9 | 2017-10-09 08:37:35 -0400 | [diff] [blame] | 3772 | magick_number_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3773 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3774 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3775 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 3776 | Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 3777 | *magick_restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3778 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 3779 | ssize_t |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 3780 | x; |
| 3781 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3782 | if (status == MagickFalse) |
| 3783 | continue; |
| 3784 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1, |
| 3785 | exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 3786 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3787 | { |
| 3788 | status=MagickFalse; |
| 3789 | continue; |
| 3790 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3791 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3792 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 3793 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3794 | intensity; |
| 3795 | |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3796 | intensity=ScaleQuantumToMap(GetPixelRed(image,q)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3797 | if (colormap_index[intensity] < 0) |
| 3798 | { |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3799 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 3800 | #pragma omp critical (MagickCore_SetGrayscaleImage) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3801 | #endif |
ImageMagick | 3a177d8 | 2018-10-07 08:12:29 -0400 | [diff] [blame] | 3802 | if (colormap_index[intensity] < 0) |
| 3803 | { |
| 3804 | colormap_index[intensity]=(ssize_t) image->colors; |
| 3805 | image->colormap[image->colors].red=(double) |
| 3806 | GetPixelRed(image,q); |
| 3807 | image->colormap[image->colors].green=(double) |
| 3808 | GetPixelGreen(image,q); |
| 3809 | image->colormap[image->colors].blue=(double) |
| 3810 | GetPixelBlue(image,q); |
| 3811 | image->colors++; |
| 3812 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3813 | } |
cristy | aeded78 | 2012-09-11 23:39:36 +0000 | [diff] [blame] | 3814 | SetPixelIndex(image,(Quantum) colormap_index[intensity],q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 3815 | q+=GetPixelChannels(image); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3816 | } |
| 3817 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 3818 | status=MagickFalse; |
| 3819 | } |
| 3820 | image_view=DestroyCacheView(image_view); |
| 3821 | } |
Cristy | 7dbf5b2 | 2019-05-11 08:26:39 -0400 | [diff] [blame] | 3822 | (void) memset(colormap_index,0,extent*sizeof(*colormap_index)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3823 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 3824 | image->colormap[i].alpha=(double) i; |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3825 | qsort((void *) image->colormap,image->colors,sizeof(PixelInfo), |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3826 | IntensityCompare); |
cristy | aaaff84 | 2013-06-30 02:48:11 +0000 | [diff] [blame] | 3827 | colormap=(PixelInfo *) AcquireQuantumMemory(image->colors,sizeof(*colormap)); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3828 | if (colormap == (PixelInfo *) NULL) |
Dirk Lemstra | 7b604a5 | 2017-07-17 23:13:50 +0200 | [diff] [blame] | 3829 | { |
| 3830 | colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index); |
| 3831 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3832 | image->filename); |
| 3833 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3834 | j=0; |
| 3835 | colormap[j]=image->colormap[0]; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3836 | for (i=0; i < (ssize_t) image->colors; i++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3837 | { |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3838 | if (IsPixelInfoEquivalent(&colormap[j],&image->colormap[i]) == MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3839 | { |
| 3840 | j++; |
| 3841 | colormap[j]=image->colormap[i]; |
| 3842 | } |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3843 | colormap_index[(ssize_t) image->colormap[i].alpha]=j; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3844 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3845 | image->colors=(size_t) (j+1); |
cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 3846 | image->colormap=(PixelInfo *) RelinquishMagickMemory(image->colormap); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3847 | image->colormap=colormap; |
| 3848 | status=MagickTrue; |
cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 3849 | image_view=AcquireAuthenticCacheView(image,exception); |
cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 3850 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
Cristy | 38b42e1 | 2018-03-18 10:13:01 -0400 | [diff] [blame] | 3851 | #pragma omp parallel for schedule(static) shared(status) \ |
Cristy | 8255ca9 | 2017-10-09 08:37:35 -0400 | [diff] [blame] | 3852 | magick_number_threads(image,image,image->rows,1) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3853 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3854 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3855 | { |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 3856 | Quantum |
dirk | 05d2ff7 | 2015-11-18 23:13:43 +0100 | [diff] [blame] | 3857 | *magick_restrict q; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3858 | |
Cristy | f2dc1dd | 2020-12-28 13:59:26 -0500 | [diff] [blame] | 3859 | ssize_t |
cristy | ecc31b1 | 2011-02-13 00:32:29 +0000 | [diff] [blame] | 3860 | x; |
| 3861 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3862 | if (status == MagickFalse) |
| 3863 | continue; |
| 3864 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 3865 | if (q == (Quantum *) NULL) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3866 | { |
| 3867 | status=MagickFalse; |
| 3868 | continue; |
| 3869 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3870 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3871 | { |
| 3872 | SetPixelIndex(image,(Quantum) colormap_index[ScaleQuantumToMap( |
| 3873 | GetPixelIndex(image,q))],q); |
cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 3874 | q+=GetPixelChannels(image); |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 3875 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3876 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 3877 | status=MagickFalse; |
| 3878 | } |
| 3879 | image_view=DestroyCacheView(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 3880 | colormap_index=(ssize_t *) RelinquishMagickMemory(colormap_index); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3881 | image->type=GrayscaleType; |
dirk | f1d8548 | 2015-04-06 00:36:00 +0000 | [diff] [blame] | 3882 | if (SetImageMonochrome(image,exception) != MagickFalse) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3883 | image->type=BilevelType; |
| 3884 | return(status); |
| 3885 | } |
Dirk Lemstra | 25229f2 | 2020-04-27 15:01:47 +0200 | [diff] [blame] | 3886 | |
| 3887 | /* |
| 3888 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3889 | % % |
| 3890 | % % |
| 3891 | % % |
| 3892 | + S e t I m a g e C o l o r m a p % |
| 3893 | % % |
| 3894 | % % |
| 3895 | % % |
| 3896 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3897 | % |
| 3898 | % SetImageColormap() traverses the color cube tree and sets the colormap of |
| 3899 | % the image. A colormap entry is any node in the color cube tree where the |
| 3900 | % of unique colors is not zero. |
| 3901 | % |
| 3902 | % The format of the SetImageColormap method is: |
| 3903 | % |
| 3904 | % MagickBooleanType SetImageColormap(Image *image,CubeInfo *cube_info, |
| 3905 | % ExceptionInfo *node_info) |
| 3906 | % |
| 3907 | % A description of each parameter follows. |
| 3908 | % |
| 3909 | % o image: the image. |
| 3910 | % |
| 3911 | % o cube_info: A pointer to the Cube structure. |
| 3912 | % |
| 3913 | % o exception: return any errors or warnings in this structure. |
| 3914 | % |
| 3915 | */ |
| 3916 | |
| 3917 | MagickBooleanType SetImageColormap(Image *image,CubeInfo *cube_info, |
| 3918 | ExceptionInfo *exception) |
| 3919 | { |
| 3920 | size_t |
| 3921 | number_colors; |
| 3922 | |
| 3923 | number_colors=MagickMax(cube_info->maximum_colors,cube_info->colors); |
| 3924 | if (AcquireImageColormap(image,number_colors,exception) == MagickFalse) |
| 3925 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3926 | image->filename); |
| 3927 | image->colors=0; |
| 3928 | DefineImageColormap(image,cube_info,cube_info->root); |
| 3929 | if (image->colors != number_colors) |
| 3930 | { |
| 3931 | image->colormap=(PixelInfo *) ResizeQuantumMemory(image->colormap, |
| 3932 | image->colors+1,sizeof(*image->colormap)); |
| 3933 | if (image->colormap == (PixelInfo *) NULL) |
| 3934 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 3935 | image->filename); |
| 3936 | } |
| 3937 | return(MagickTrue); |
| 3938 | } |