• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1
2
3
4
5<!DOCTYPE html>
6<html lang="en">
7<head>
8    <title>ImageMagick: MagickCore, C API for ImageMagick: Reduce the Number of Unique Colors in an Image</title>
9  <meta charset="utf-8" />
10  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
11  <meta name="viewport" content="width=device-width, initial-scale=1" />
12  <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
13  <meta name="application-name" content="ImageMagick"/>
14  <meta name="description" content="ImageMagick® is a software suite to create, edit, compose, or convert bitmap images. It can read and write images in a variety of formats (over 200) including PNG, JPEG, JPEG-2000, GIF, WebP, Postscript, PDF, and SVG. Use ImageMagick to resize, flip, mirror, rotate, distort, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves."/>
15  <meta name="application-url" content="http://www.imagemagick.org"/>
16  <meta name="generator" content="PHP"/>
17  <meta name="keywords" content="magickcore, c, api, for, imagemagick:, reduce, the, number, of, unique, colors, in, an, image, ImageMagick, PerlMagick, image processing, image, photo, software, Magick++, OpenMP, convert"/>
18  <meta name="rating" content="GENERAL"/>
19  <meta name="robots" content="INDEX, FOLLOW"/>
20  <meta name="generator" content="ImageMagick Studio LLC"/>
21  <meta name="author" content="ImageMagick Studio LLC"/>
22  <meta name="revisit-after" content="2 DAYS"/>
23  <meta name="resource-type" content="document"/>
24  <meta name="copyright" content="Copyright (c) 1999-2016 ImageMagick Studio LLC"/>
25  <meta name="distribution" content="Global"/>
26  <meta name="magick-serial" content="P131-S030410-R485315270133-P82224-A6668-G1245-1"/>
27  <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4" />
28  <link rel="icon" href="../images/wand.png"/>
29  <link rel="shortcut icon" href="../images/wand.ico"/>
30  <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Roboto:900,400,400italic,700,700italic,300,300italic|Open+Sans:300italic,400italic,700italic,300,400,600,700">
31  <link rel="stylesheet" href="../css/magick.html"/>
32</head>
33<body>
34<div class="main">
35<div class="magick-masthead">
36  <div class="container">
37    <script async="async" src="http://localhost/pagead/js/adsbygoogle.js"></script>    <ins class="adsbygoogle"
38         style="display:block"
39         data-ad-client="ca-pub-3129977114552745"
40         data-ad-slot="6345125851"
41         data-ad-format="auto"></ins>
42    <script>
43      (adsbygoogle = window.adsbygoogle || []).push({});
44    </script>
45    <nav class="magick-nav">
46      <a class="magick-nav-item " href="../index.html">Home</a>
47      <a class="magick-nav-item " href="../binary-releases.html">Download</a>
48      <a class="magick-nav-item " href="../command-line-tools.html">Tools</a>
49      <a class="magick-nav-item " href="../command-line-processing.html">Command-line</a>
50      <a class="magick-nav-item " href="../resources.html">Resources</a>
51      <a class="magick-nav-item " href="api.html">Develop</a>
52      <a class="magick-nav-item " href="http://www.imagemagick.org/script/search.php">Search</a>
53      <a class="magick-nav-item pull-right" href="https://www.imagemagick.org/discourse-server/">Community</a>
54    </nav>
55  </div>
56</div>
57<div class="container">
58<div class="magick-header">
59<p class="text-center"><a href="quantize.html#QuantizeImage">QuantizeImage</a> &#8226; <a href="quantize.html#AcquireQuantizeInfo">AcquireQuantizeInfo</a> &#8226; <a href="quantize.html#CloneQuantizeInfo">CloneQuantizeInfo</a> &#8226; <a href="quantize.html#CompressImageColormap">CompressImageColormap</a> &#8226; <a href="quantize.html#DestroyQuantizeInfo">DestroyQuantizeInfo</a> &#8226; <a href="quantize.html#GetImageQuantizeError">GetImageQuantizeError</a> &#8226; <a href="quantize.html#GetQuantizeInfo">GetQuantizeInfo</a> &#8226; <a href="quantize.html#PosterizeImage">PosterizeImage</a> &#8226; <a href="quantize.html#QuantizeImage">QuantizeImage</a> &#8226; <a href="quantize.html#QuantizeImages">QuantizeImages</a> &#8226; <a href="quantize.html#RemapImage">RemapImage</a> &#8226; <a href="quantize.html#RemapImages">RemapImages</a> &#8226; <a href="quantize.html#SetGrayscaleImage">SetGrayscaleImage</a></p>
60
61<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="QuantizeImage">QuantizeImage</a></h2>
62
63<p>QuantizeImage() takes a standard RGB or monochrome images and quantizes them down to some fixed number of colors.</p>
64
65<p>For purposes of color allocation, an image is a set of n pixels, where each pixel is a point in RGB space.  RGB space is a 3-dimensional vector space, and each pixel, Pi,  is defined by an ordered triple of red, green, and blue coordinates, (Ri, Gi, Bi).</p>
66
67<p>Each primary color component (red, green, or blue) represents an intensity which varies linearly from 0 to a maximum value, Cmax, which corresponds to full saturation of that color.  Color allocation is defined over a domain consisting of the cube in RGB space with opposite vertices at (0,0,0) and (Cmax, Cmax, Cmax).  QUANTIZE requires Cmax = 255.</p>
68
69<p>The algorithm maps this domain onto a tree in which each node represents a cube within that domain.  In the following discussion these cubes are defined by the coordinate of two opposite vertices (vertex nearest the origin in RGB space and the vertex farthest from the origin).</p>
70
71<p>The tree's root node represents the entire domain, (0,0,0) through (Cmax,Cmax,Cmax).  Each lower level in the tree is generated by subdividing one node's cube into eight smaller cubes of equal size. This corresponds to bisecting the parent cube with planes passing through the midpoints of each edge.</p>
72
73<p>The basic algorithm operates in three phases: Classification, Reduction, and Assignment.  Classification builds a color description tree for the image.  Reduction collapses the tree until the number it represents, at most, the number of colors desired in the output image. Assignment defines the output image's color map and sets each pixel's color by restorage_class in the reduced tree.  Our goal is to minimize the numerical discrepancies between the original colors and quantized colors (quantization error).</p>
74
75<p>Classification begins by initializing a color description tree of sufficient depth to represent each possible input color in a leaf. However, it is impractical to generate a fully-formed color description tree in the storage_class phase for realistic values of Cmax.  If colors components in the input image are quantized to k-bit precision, so that Cmax= 2k-1, the tree would need k levels below the root node to allow representing each possible input color in a leaf.  This becomes prohibitive because the tree's total number of nodes is 1 + sum(i=1, k, 8k).</p>
76
77<p>A complete tree would require 19,173,961 nodes for k = 8, Cmax = 255.</p>
78<dt>avoid building a fully populated tree, QUANTIZE</dt>
79<p>(1) Initializes data structures for nodes only as they are needed;  (2) Chooses a maximum depth for the tree as a function of the desired number of colors in the output image (currently log2(colormap size)).</p>
80
81<p>For each pixel in the input image, storage_class scans downward from the root of the color description tree.  At each level of the tree it identifies the single node which represents a cube in RGB space containing the pixel's color.  It updates the following data for each such node:</p>
82
83<pre class="text">
84    n1: Number of pixels whose color is contained in the RGB cube which
85    this node represents;
86</pre>
87
88<p>n2: Number of pixels whose color is not represented in a node at lower depth in the tree;  initially,  n2 = 0 for all nodes except leaves of the tree.</p>
89
90<p>Sr, Sg, Sb: Sums of the red, green, and blue component values for all pixels not classified at a lower depth. The combination of these sums and n2 will ultimately characterize the mean color of a set of pixels represented by this node.</p>
91
92<p>E: the distance squared in RGB space between each pixel contained within a node and the nodes' center.  This represents the quantization error for a node.</p>
93
94<p>Reduction repeatedly prunes the tree until the number of nodes with n2 &gt; 0 is less than or equal to the maximum number of colors allowed in the output image.  On any given iteration over the tree, it selects those nodes whose E count is minimal for pruning and merges their color statistics upward. It uses a pruning threshold, Ep, to govern node selection as follows:</p>
95
96<dd>
97</dd>
98
99<dd> Ep = 0 while number of nodes with (n2 &gt; 0) &gt; required maximum number of colors prune all nodes such that E &lt;= Ep Set Ep to minimum E in remaining nodes </dd>
100
101<dd> This has the effect of minimizing any quantization error when merging two nodes together. </dd>
102
103<dd> When a node to be pruned has offspring, the pruning procedure invokes itself recursively in order to prune the tree from the leaves upward. n2,  Sr, Sg,  and  Sb in a node being pruned are always added to the corresponding data in that node's parent.  This retains the pruned node's color characteristics for later averaging. </dd>
104
105<dd> For each node, n2 pixels exist for which that node represents the smallest volume in RGB space containing those pixel's colors.  When n2 &gt; 0 the node will uniquely define a color in the output image. At the beginning of reduction,  n2 = 0  for all nodes except a the leaves of the tree which represent colors present in the input image. </dd>
106
107<dd> The other pixel count, n1, indicates the total number of colors within the cubic volume which the node represents.  This includes n1 - n2 pixels whose colors should be defined by nodes at a lower level in the tree. </dd>
108
109<dd> Assignment generates the output image from the pruned tree.  The output </dd>
110<dl class="dl-horizontal">
111<dt>parts</dt>
112<dd>(1)  A color map, which is an array of color descriptions (RGB triples) for each color present in the output image;  (2)  A pixel array, which represents each pixel as an index into the color map array. </dd>
113
114<dd> First, the assignment phase makes one pass over the pruned color description tree to establish the image's color map.  For each node with n2  &gt; 0, it divides Sr, Sg, and Sb by n2 .  This produces the mean color of all pixels that classify no lower than this node.  Each of these colors becomes an entry in the color map. </dd>
115
116<dd> Finally,  the assignment phase reclassifies each pixel in the pruned tree to identify the deepest node containing the pixel's color.  The pixel's value in the pixel array becomes the index of this node's mean color in the color map. </dd>
117
118<dd> This method is based on a similar algorithm written by Paul Raveling. </dd>
119
120<dd>  </dd>
121</dl>
122<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="AcquireQuantizeInfo">AcquireQuantizeInfo</a></h2>
123
124<p>AcquireQuantizeInfo() allocates the QuantizeInfo structure.</p>
125
126<p>The format of the AcquireQuantizeInfo method is:</p>
127
128<pre class="text">
129QuantizeInfo *AcquireQuantizeInfo(const ImageInfo *image_info)
130</pre>
131
132<p>A description of each parameter follows:</p>
133
134<dd>
135</dd>
136
137<dd> </dd>
138<dl class="dl-horizontal">
139<dt>image_info</dt>
140<dd>the image info. </dd>
141
142<dd>  </dd>
143</dl>
144<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="CloneQuantizeInfo">CloneQuantizeInfo</a></h2>
145
146<p>CloneQuantizeInfo() makes a duplicate of the given quantize info structure, or if quantize info is NULL, a new one.</p>
147
148<p>The format of the CloneQuantizeInfo method is:</p>
149
150<pre class="text">
151QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info)
152</pre>
153
154<p>A description of each parameter follows:</p>
155
156<dd>
157</dd>
158
159<dd> </dd>
160<dl class="dl-horizontal">
161<dt>clone_info</dt>
162<dd>Method CloneQuantizeInfo returns a duplicate of the given quantize info, or if image info is NULL a new one. </dd>
163
164<dd> </dd>
165<dt>quantize_info</dt>
166<dd>a structure of type info. </dd>
167
168<dd>  </dd>
169</dl>
170<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="CompressImageColormap">CompressImageColormap</a></h2>
171
172<p>CompressImageColormap() compresses an image colormap by removing any duplicate or unused color entries.</p>
173
174<p>The format of the CompressImageColormap method is:</p>
175
176<pre class="text">
177MagickBooleanType CompressImageColormap(Image *image,
178  ExceptionInfo *exception)
179</pre>
180
181<p>A description of each parameter follows:</p>
182
183<dd>
184</dd>
185
186<dd> </dd>
187<dl class="dl-horizontal">
188<dt>image</dt>
189<dd>the image. </dd>
190
191<dd> </dd>
192<dt>exception</dt>
193<dd>return any errors or warnings in this structure. </dd>
194
195<dd>  </dd>
196</dl>
197<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="DestroyQuantizeInfo">DestroyQuantizeInfo</a></h2>
198
199<p>DestroyQuantizeInfo() deallocates memory associated with an QuantizeInfo structure.</p>
200
201<p>The format of the DestroyQuantizeInfo method is:</p>
202
203<pre class="text">
204QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info)
205</pre>
206
207<p>A description of each parameter follows:</p>
208
209<dd>
210</dd>
211
212<dd> </dd>
213<dl class="dl-horizontal">
214<dt>quantize_info</dt>
215<dd>Specifies a pointer to an QuantizeInfo structure. </dd>
216
217<dd>  </dd>
218</dl>
219<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="GetImageQuantizeError">GetImageQuantizeError</a></h2>
220
221<p>GetImageQuantizeError() measures the difference between the original and quantized images.  This difference is the total quantization error. The error is computed by summing over all pixels in an image the distance squared in RGB space between each reference pixel value and its quantized value.  These values are computed:</p>
222
223<pre class="text">
224    o mean_error_per_pixel:  This value is the mean error for any single
225pixel in the image.
226</pre>
227
228<dt>normalized_mean_square_error</dt>
229<p>This value is the normalized mean quantization error for any single pixel in the image.  This distance measure is normalized to a range between 0 and 1.  It is independent of the range of red, green, and blue values in the image.</p>
230
231<dt>normalized_maximum_square_error</dt>
232<p>Thsi value is the normalized maximum quantization error for any single pixel in the image.  This distance measure is normalized to a range between 0 and 1.  It is independent of the range of red, green, and blue values in your image.</p>
233
234<p>The format of the GetImageQuantizeError method is:</p>
235
236<pre class="text">
237MagickBooleanType GetImageQuantizeError(Image *image,
238  ExceptionInfo *exception)
239</pre>
240
241<p>A description of each parameter follows.</p>
242
243<dt>image</dt>
244<p>the image.</p>
245
246<dt>exception</dt>
247<p>return any errors or warnings in this structure.</p>
248
249<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="GetQuantizeInfo">GetQuantizeInfo</a></h2>
250
251<p>GetQuantizeInfo() initializes the QuantizeInfo structure.</p>
252
253<p>The format of the GetQuantizeInfo method is:</p>
254
255<pre class="text">
256GetQuantizeInfo(QuantizeInfo *quantize_info)
257</pre>
258
259<p>A description of each parameter follows:</p>
260
261<dd>
262</dd>
263
264<dd> </dd>
265<dl class="dl-horizontal">
266<dt>quantize_info</dt>
267<dd>Specifies a pointer to a QuantizeInfo structure. </dd>
268
269<dd>  </dd>
270</dl>
271<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="PosterizeImage">PosterizeImage</a></h2>
272
273<p>PosterizeImage() reduces the image to a limited number of colors for a "poster" effect.</p>
274
275<p>The format of the PosterizeImage method is:</p>
276
277<pre class="text">
278MagickBooleanType PosterizeImage(Image *image,const size_t levels,
279  const DitherMethod dither_method,ExceptionInfo *exception)
280</pre>
281
282<p>A description of each parameter follows:</p>
283
284<dd>
285</dd>
286
287<dd> </dd>
288<dl class="dl-horizontal">
289<dt>image</dt>
290<dd>Specifies a pointer to an Image structure. </dd>
291
292<dd> </dd>
293<dt>levels</dt>
294<dd>Number of color levels allowed in each channel.  Very low values (2, 3, or 4) have the most visible effect. </dd>
295
296<dd> </dd>
297<dt>dither_method</dt>
298<dd>choose from UndefinedDitherMethod, NoDitherMethod, RiemersmaDitherMethod, FloydSteinbergDitherMethod. </dd>
299
300<dd> </dd>
301<dt>exception</dt>
302<dd>return any errors or warnings in this structure. </dd>
303
304<dd>  </dd>
305</dl>
306<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="QuantizeImage">QuantizeImage</a></h2>
307
308<p>QuantizeImage() analyzes the colors within a reference image and chooses a fixed number of colors to represent the image.  The goal of the algorithm is to minimize the color difference between the input and output image while minimizing the processing time.</p>
309
310<p>The format of the QuantizeImage method is:</p>
311
312<pre class="text">
313MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
314  Image *image,ExceptionInfo *exception)
315</pre>
316
317<p>A description of each parameter follows:</p>
318
319<dd>
320</dd>
321
322<dd> </dd>
323<dl class="dl-horizontal">
324<dt>quantize_info</dt>
325<dd>Specifies a pointer to an QuantizeInfo structure. </dd>
326
327<dd> </dd>
328<dt>image</dt>
329<dd>the image. </dd>
330
331<dd> </dd>
332<dt>exception</dt>
333<dd>return any errors or warnings in this structure. </dd>
334
335<dd>  </dd>
336</dl>
337<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="QuantizeImages">QuantizeImages</a></h2>
338
339<p>QuantizeImages() analyzes the colors within a set of reference images and chooses a fixed number of colors to represent the set.  The goal of the algorithm is to minimize the color difference between the input and output images while minimizing the processing time.</p>
340
341<p>The format of the QuantizeImages method is:</p>
342
343<pre class="text">
344MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
345  Image *images,ExceptionInfo *exception)
346</pre>
347
348<p>A description of each parameter follows:</p>
349
350<dd>
351</dd>
352
353<dd> </dd>
354<dl class="dl-horizontal">
355<dt>quantize_info</dt>
356<dd>Specifies a pointer to an QuantizeInfo structure. </dd>
357
358<dd> </dd>
359<dt>images</dt>
360<dd>Specifies a pointer to a list of Image structures. </dd>
361
362<dd> </dd>
363<dt>exception</dt>
364<dd>return any errors or warnings in this structure. </dd>
365
366<dd>  </dd>
367</dl>
368<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="RemapImage">RemapImage</a></h2>
369
370<p>RemapImage() replaces the colors of an image with the closest of the colors from the reference image.</p>
371
372<p>The format of the RemapImage method is:</p>
373
374<pre class="text">
375MagickBooleanType RemapImage(const QuantizeInfo *quantize_info,
376  Image *image,const Image *remap_image,ExceptionInfo *exception)
377</pre>
378
379<p>A description of each parameter follows:</p>
380
381<dd>
382</dd>
383
384<dd> </dd>
385<dl class="dl-horizontal">
386<dt>quantize_info</dt>
387<dd>Specifies a pointer to an QuantizeInfo structure. </dd>
388
389<dd> </dd>
390<dt>image</dt>
391<dd>the image. </dd>
392
393<dd> </dd>
394<dt>remap_image</dt>
395<dd>the reference image. </dd>
396
397<dd> </dd>
398<dt>exception</dt>
399<dd>return any errors or warnings in this structure. </dd>
400
401<dd>  </dd>
402</dl>
403<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="RemapImages">RemapImages</a></h2>
404
405<p>RemapImages() replaces the colors of a sequence of images with the closest color from a reference image.</p>
406
407<p>The format of the RemapImage method is:</p>
408
409<pre class="text">
410MagickBooleanType RemapImages(const QuantizeInfo *quantize_info,
411  Image *images,Image *remap_image,ExceptionInfo *exception)
412</pre>
413
414<p>A description of each parameter follows:</p>
415
416<dd>
417</dd>
418
419<dd> </dd>
420<dl class="dl-horizontal">
421<dt>quantize_info</dt>
422<dd>Specifies a pointer to an QuantizeInfo structure. </dd>
423
424<dd> </dd>
425<dt>images</dt>
426<dd>the image sequence. </dd>
427
428<dd> </dd>
429<dt>remap_image</dt>
430<dd>the reference image. </dd>
431
432<dd> </dd>
433<dt>exception</dt>
434<dd>return any errors or warnings in this structure. </dd>
435
436<dd>  </dd>
437</dl>
438<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize_8c.html" id="SetGrayscaleImage">SetGrayscaleImage</a></h2>
439
440<p>SetGrayscaleImage() converts an image to a PseudoClass grayscale image.</p>
441
442<p>The format of the SetGrayscaleImage method is:</p>
443
444<pre class="text">
445MagickBooleanType SetGrayscaleImage(Image *image,
446  ExceptionInfo *exception)
447</pre>
448
449<p>A description of each parameter follows:</p>
450
451<dd>
452</dd>
453
454<dd> </dd>
455<dl class="dl-horizontal">
456<dt>image</dt>
457<dd>The image. </dd>
458
459<dd> </dd>
460<dt>exception</dt>
461<dd>return any errors or warnings in this structure. </dd>
462
463<dd>  </dd>
464</dl>
465</div>
466  <footer class="magick-footer">
467    <p><a href="../support.html">Donate</a> •
468     <a href="../sitemap.html">Sitemap</a> •
469    <a href="../links.html">Related</a> •
470    <a href="../architecture.html">Architecture</a>
471</p>
472    <p><a href="quantize.html#">Back to top</a> •
473    <a href="http://pgp.mit.edu:11371/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a> •
474    <a href="http://www.imagemagick.org/script/contact.php">Contact Us</a></p>
475        <p><small>©  1999-2016 ImageMagick Studio LLC</small></p>
476  </footer>
477</div><!-- /.container -->
478
479  <script src="https://localhost/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
480  <script src="../js/magick.html"></script>
481</div>
482</body>
483</html>
484