Debian Codegraph @

www/quantize/index.html

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1
2
3 <!DOCTYPE html>
4 <html lang="en" data-bs-theme="dark">
5
6
7 <head>
8 <meta charset="utf-8" />
9 <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
10 <title>ImageMagick | Color Reduction Utilizing Adaptive Spatial Subdivision</title>
11 <meta name="description" content="" />
12 <link rel="canonical" href="index.html" />
13 <meta name="robots" content="index, follow" />
14 <meta name="theme-color" content="#000000" />
15 <meta name="google-site-verification" content="_bMOCDpkx9ZAzBwb2kF3PRHbfUUdFj2uO8Jd1AXArz4" />
16 <link rel="shortcut icon" href="../image/wand.png" type="image/png">
17 <link rel="apple-touch-icon" href="../image/wand.png" type="image/png">
18 <link rel="preconnect" href="https://localhost/">
19 <link rel="preload" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" as="style" crossorigin="anonymous">
20 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" crossorigin="anonymous">
21 <!-- Accessibility Enhancement -->
22 <style>
23 html {
24 scroll-padding-top: 70px;
25 }
26
27 a.nav-link:hover,
28 a.dropdown-item:hover {
29 color: #ffc107 !important;
30 text-shadow: 0px 2px 4px rgba(255, 193, 7, 0.8);
31 }
32
33 .btn-outline-warning {
34 color: #cd7f32;
35 border-color: #cd7f32;
36 }
37
38 .navbar-brand {
39 color: #d4af37;
40 transition: color 0.3s ease, text-shadow 0.3s ease;
41 }
42
43 .navbar-brand:hover {
44 color: #e9c03d;
45 text-shadow: 0px 2px 4px rgba(203, 163, 120, 0.6);
46 }
47
48 :root {
49 --bs-code-color: revert;
50 }
51 </style>
52 <!-- Open Graph Meta Tags -->
53 <meta property="og:title" content="ImageMagick | Color Reduction Utilizing Adaptive Spatial Subdivision" />
54 <meta property="og:description" content="" />
55 <meta property="og:image" content="https://imagemagick.org/image/logo.png">
56 <meta property="og:logo" content="https://imagemagick.org/image/logo.png">
57 <meta property="og:url" content="https://imagemagick.org/quantize/">
58 <meta property="og:type" content="website" />
59 <meta property="og:site_name" content="ImageMagick" />
60 <meta property="og:locale" content="en_us" />
61 <!-- Twitter Card Meta Tags -->
62 <meta name="twitter:card" content="summary_large_image" />
63 $site=strtolower("ImageMagick");
64 <meta name="twitter:site" content="@imagemagick" />
65 <meta name="twitter:creator" content="@imagemagick" />
66 <meta name="twitter:title" content="ImageMagick | Color Reduction Utilizing Adaptive Spatial Subdivision" />
67 <meta name="twitter:description" content="$this- />description">
68 <meta name="twitter:image" content="https://imagemagick.org/image/logo.png">
69 <meta name="twitter:image:alt" content="ImageMagick logo and tag line" />
70 <!-- JSON-LD Structured Data -->
71 <script type="application/ld+json">
72 {
73 "@context": "https://schema.org",
74 "@type": "SoftwareApplication",
75 "name": "ImageMagick",
76 "url": "ImageMagick | Color Reduction Utilizing Adaptive Spatial Subdivision",
77 "image": "https://imagemagick.org/image/logo.png",
78 "description": "$this->description",
79 "applicationCategory": "Multimedia",
80 "operatingSystem": "Windows, macOS, Linux, Unix",
81 "softwareVersion": "7.1.2-27",
82 "license": "https://imagemagick.org/license/",
83 "creator": {
84 "@type": "Organization",
85 "name": "ImageMagick Studio LLC",
86 "url": "https://imagemagick.org"
87 },
88 "keywords": [
89 "".ucfirst($title)."",
90 "image processing software",
91 "image conversion tool",
92 "batch image editing",
93 "open-source image editor",
94 "ImageMagick command-line",
95 "resize images ImageMagick",
96 "crop and rotate images",
97 "ImageMagick tutorial",
98 "ImageMagick automation",
99 "ImageMagick for developers",
100 "ImageMagick CLI",
101 "ImageMagick filters and effects",
102 "ImageMagick scripting",
103 "ImageMagick API integration"
104 ],
105 "sameAs": [
106 "https://github.com/ImageMagick",
107 "https://x.com/imagemagick"
108 ],
109 "offers": {
110 "@type": "Offer",
111 "price": "0.00",
112 "priceCurrency": "USD"
113 }
114 }
115 </script>
116 <!-- Google Custom Search -->
117 <script async src="https://localhost/cse.js?cx=8720b81722164487b"></script>
118 <script async src="https://localhost/pagead/js/adsbygoogle.js?client=ca-pub-3129977114552745" crossorigin="anonymous"></script>
119 </head>
120
121 <body>
122 <script>
123 function setTheme(theme) {
124 localStorage.setItem('theme', theme);
125 updateThemeIcon(theme);
126 document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? getSystemTheme() : theme);
127 }
128 function getSystemTheme() {
129 return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
130 }
131 function updateThemeIcon(theme) {
132 const iconMap = {
133 light: '☀️',
134 dark: '🌙',
135 auto: '🌓'
136 };
137 document.getElementById('currentThemeIcon').textContent = iconMap[theme] || '🌓';
138 }
139 document.addEventListener('DOMContentLoaded', () => {
140 const savedTheme = localStorage.getItem('theme') || 'auto';
141 setTheme(savedTheme);
142 });
143 </script>
144 <nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
145 <div class="container-fluid">
146 <a class="navbar-brand fw-bold" href="../index-2.html" title="ImageMagick">ImageMagick</a>
147 <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#magick-navbars" aria-controls="magick-navbars" aria-expanded="false" aria-label="Toggle navigation">
148 <span class="navbar-toggler-icon"></span>
149 </button>
150
151 <div class="collapse navbar-collapse" id="magick-navbars">
152 <ul class="navbar-nav me-auto">
153 <li class="nav-item">
154 <a class="nav-link "" href="../download/index.html">Download</a>
155 </li>
156 <li class="nav-item">
157 <a class="nav-link "" href="../command-line-tools/index.html">Tools</a>
158 </li>
159 <li class="nav-item">
160 <a class="nav-link "" href="../command-line-processing/index.html">CLI</a>
161 </li>
162 <li class="nav-item">
163 <a class="nav-link "" href="../develop/index.html">Develop</a>
164 </li>
165 <li class="nav-item">
166 <a class="nav-link" rel="noopener noreferrer" target="_blank" href="https://github.com/ImageMagick/ImageMagick/discussions">Community</a>
167 </li>
168 </ul>
169 <ul class="navbar-nav ms-auto">
170 <li class="nav-item ms-3">
171 <form class="d-flex form-inline" action="https://imagemagick.org/quantize/">
172 <input class="form-control me-2" type="text" name="q" placeholder="Search site..." aria-label="Search">
173 <button class="btn btn-outline-warning" type="submit" name="sa">Search</button>
174 </form>
175 </li>
176 <li class="nav-item dropdown ms-3">
177 <a class="nav-link dropdown-toggle" href="#" id="themeDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
178 <span id="currentThemeIcon">🌓</span>
179 </a>
180 <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="themeDropdown">
181 <li><a class="dropdown-item" href="#" onclick="setTheme('light')">☀️ Light</a></li>
182 <li><a class="dropdown-item" href="#" onclick="setTheme('dark')">🌙 Dark</a></li>
183 <li><a class="dropdown-item" href="#" onclick="setTheme('auto')">🌓 Auto</a></li>
184 </ul>
185 </li>
186 </ul>
187 </div>
188 </div>
189 </nav>
190
191 <div class="col-lg-8 mx-auto text-body-secondary pt-5 pt-lg-5">
192 <header class="d-flex align-items-center pb-3 mb-5 border-bottom">
193 <div class="mt-3 ms-3">
194 <a href="../index-2.html" class="text-decoration-none">
195 <h1 class="fs-2 mb-1 display-5">Color Reduction Utilizing Adaptive Spatial Subdivision</h1>
196 </a>
197 <h2 class="fs-5 text-muted h4">ImageMagick</h2>
198 </div>
199 </header>
200
201 <main role="main" class="container">
202 <div class="row g-5">
203 <div class="col-md-9">
204 <div>
205 <p class="text-center"><a href="#describe">Algorithm Description</a> • <a href="#measure">Measuring Color Reduction Error</a></p>
206
207 <p class="lead">This document describes how ImageMagick performs color reduction on an image. To fully understand what follows, you should have a knowledge of basic imaging techniques and the tree data structure and terminology.</p>
208
209 <h2><a class="anchor" id="describe"></a>Algorithm Description</h2>
210
211 <p>For purposes of color allocation, an image is a set of <var>n</var> pixels, where each pixel is a point in RGB space. RGB space is a 3-dimensional vector space, and each pixel, <var>p(i)</var>, is defined by an ordered triple of red, green, and blue coordinates, (<var>r(i)</var>, <var>g(i)</var>, <var>b(i)</var>).</p>
212
213 <p>Each primary color component (<var>red</var>, <var>green</var>, or <var>blue</var>) represents an intensity which varies linearly from 0 to a maximum value, <var>Cmax</var>, 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 (<var>Cmax</var>, <var>Cmax</var>, <var>Cmax</var>). ImageMagick requires <var>Cmax</var>= <var>255</var>.</p>
214
215 <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: The vertex nearest the origin in RGB space and the vertex farthest from the origin.</p>
216
217 <p>The tree's root node represents the entire domain, (0,0,0) through (<var>Cmax</var>, <var>Cmax</var>, <var>Cmax</var>). 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>
218
219 <p>The basic algorithm operates in three phases:</p>
220
221 <ol>
222 <li>Classification</li>
223 <li>Reduction</li>
224 <li>Assignment</li>
225 </ol>
226
227 <p><b>Classification</b></p>
228
229 <p>Classification builds a color description tree for the image. Reduction collapses the tree until the number it represents, at most, is the number of colors desired in the output image. Assignment defines the output image's color map and sets each pixel's color by reclassification in the reduced tree. <var>Our goal is to minimize the numerical discrepancies between the original colors and quantized colors</var>. To learn more about quantization error, see <a href="#measure">Measuring Color Reduction Error</a>.</p>
230
231 <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 classification phase for realistic values of <var>Cmax</var>. If color components in the input image are quantized to <var>k</var>-bit precision, so that <var>Cmax</var> = <var>2^k-1</var>, the tree would need <var>k</var> 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:</p>
232
233 <pre class="p-3 mb-2 text-body-secondary bg-body-tertiary"><code>total nodes = 1+Sum(8^i), i=1,k
234
235 For k=8,
236 nodes = 1 + (8^1+8^2+....+8^8)
237 = 1 + 8(8^8 - 1)/(8 - 1)
238 = 19,173,961 </code></pre>
239
240 <p>Therefore, to avoid building a fully populated tree, ImageMagick:</p>
241
242 <ol>
243 <li>initializes data structures for nodes only as they are needed;</li>
244 <li>chooses a maximum depth for the tree as a function of the desired number of colors in the output image (currently the <var>base-two</var> logarithm of <var>Cmax</var>).</li>
245 </ol>
246
247 <pre class="p-3 mb-2 text-body-secondary bg-body-tertiary"><code>For Cmax=255,
248 maximum tree depth = log<sub>2</sub>(256)
249 = 8 </code></pre>
250
251 <p>A tree of this depth generally allows the best representation of the source image with the fastest computational speed and the least amount of memory. However, the default depth is inappropriate for some images. Therefore, the caller can request a specific tree depth.</p>
252
253 <p>For each pixel in the input image, classification 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 pixels' color. It updates the following data for each such node:</p>
254
255 <dl class="row">
256 <dt class="col-md-4">n1</dt>
257 <dd class="col-md-8">number of pixels whose color is contained in the RGB cube which this node represents;</dd>
258 <dt class="col-md-4">n2</dt>
259 <dd class="col-md-8">number of pixels whose color is not represented in a node at lower depth in the tree; initially, <var>n2=0</var> for all nodes except leaves of the tree.</dd>
260 <dt class="col-md-4">Sr,Sg,Sb</dt>
261 <dd class="col-md-8">sums of the <var>red</var>, <var>green</var>, and <var>blue</var> component values for all pixels not classified at a lower depth. The combination of these sums and <var>n2</var> will ultimately characterize the mean color of a set of pixels represented by this node.</dd>
262 <dt class="col-md-4">E</dt>
263 <dd class="col-md-8">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.</dd>
264 </dl>
265
266 <p><b>Reduction</b></p>
267
268 <p>Reduction repeatedly prunes the tree until the number of nodes with <var>n2</var> &gt; <var>0</var> 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 <var>E</var> value is minimal for pruning and merges their color statistics upward. It uses a pruning threshold, <var>Ep</var>, to govern node selection as follows:</p>
269
270 <pre class="p-3 mb-2 text-body-secondary bg-body-tertiary"><code>Ep = 0
271 while number of nodes with (n2 &gt; 0) &gt; required maximum number of colors
272 prune all nodes such that E &lt;= Ep
273 Set Ep to minimum E in remaining nodes </code></pre>
274
275 <p>This has the effect of minimizing any quantization error when merging two nodes together.</p>
276
277 <p>When a node to be pruned has offspring, the pruning procedure invokes itself recursively in order to prune the tree from the leaves upward. The values of <var>n2</var>, <var>Sr</var>, <var>Sg</var>, and <var>Sb</var> 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.</p>
278
279 <p>For each node, <var>n2</var> pixels exist for which that node represents the smallest volume in RGB space containing those pixel's colors. When <var>n2</var> &gt; <var>0</var> the node will uniquely define a color in the output image. At the beginning of reduction, <var>n2</var> = <var>0</var> for all nodes except the leaves of the tree which represent colors present in the input image.</p>
280
281 <p>The other pixel count, <var>n1</var>, indicates the total number of colors within the cubic volume which the node represents. This includes <var>n1</var> - <var>n2</var> pixels whose colors should be defined by nodes at a lower level in the tree.</p>
282
283 <p><b>Assignment</b></p>
284
285 <p>Assignment generates the output image from the pruned tree. The output image consists of two parts:</p>
286 <ol>
287 <li>A color map, which is an array of color descriptions (RGB triples) for each color present in the output image.</li>
288
289 <li>A pixel array, which represents each pixel as an index into the color map array.</li>
290 </ol>
291
292 <p>First, the assignment phase makes one pass over the pruned color description tree to establish the image's color map. For each node with <var>n2</var> &gt; <var>0</var>, it divides <var>Sr</var>, <var>Sg</var>, and <var>Sb</var> by <var>n2</var>. 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.</p>
293
294 <p>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.</p>
295
296 <p>Empirical evidence suggests that the distances in color spaces such as YUV, or YIQ correspond to perceptual color differences more closely than do distances in RGB space. These color spaces may give better results when color reducing an image. Here the algorithm is as described except each pixel is a point in the alternate color space. For convenience, the color components are normalized to the range 0 to a maximum value, <var>Cmax</var>. The color reduction can then proceed as described.</p>
297
298 <h2><a class="anchor" id="measure"></a>Measuring Color Reduction Error</h2>
299
300 <p>Depending on the image, the color reduction error may be obvious or invisible. Images with high spatial frequencies (such as hair or grass) will show error much less than pictures with large smoothly shaded areas (such as faces). This because the high-frequency contour edges introduced by the color reduction process are masked by the high frequencies in the image.</p>
301
302 <p>To measure the difference between the original and color reduced images (the total color reduction error), ImageMagick sums over all pixels in an image the distance squared in RGB space between each original pixel value and its color reduced value. ImageMagick prints several error measurements including the mean error per pixel, the normalized mean error, and the normalized maximum error.</p>
303
304 <p>The normalized error measurement can be used to compare images. In general, the closer the mean error is to zero the more the quantized image resembles the source image. Ideally, the error should be perceptually-based, since the human eye is the final judge of quantization quality.</p>
305
306 <p>These errors are measured and printed when the <a href="../command-line-options/index.html#colors">-colors</a> and <a href="../command-line-options/index.html#verbose">-verbose</a> options are specified on the <a href="../magick/index.html">magick</a> command line:</p>
307
308 <div>
309 <table class="table table-sm table-hover table-striped table-responsive">
310 <tr>
311 <td>mean error per pixel</td>
312 <td>is the mean error for any single pixel in the image.</td>
313 </tr>
314 <tr>
315 <td>normalized mean square error</td>
316 <td>is the normalized mean square 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.</td>
317 </tr>
318 <tr>
319 <td>normalized maximum square error</td>
320 <td>is the largest normalized square quantization error for any single pixel in the image. This distance measure is normalized to a range between of red, green, and blue values in the image.</td>
321 </tr>
322 </table></div>
323
324 </div>
325
326 </div>
327 <div class="col-md-3">
328 <div class="container text-center my-3">
329 <a href="../image/wizard.png" rel="noopener noreferrer" target="_blank">
330 <img alt="The Tao of the Spiritual Warrior" class="img-fluid img-thumbnail" height="1468" loading="lazy" src="../image/wizard.png" width="1104">
331 </a>
332 <p class="mt-2"><iframe src="https://github.com/sponsors/ImageMagick/button" title="Sponsor ImageMagick" height="35" width="107" style="border: 0;"></iframe></p>
333 </div>
334 <div class="container text-center my-3">
335 <script async src="https://localhost/pagead/js/adsbygoogle.js?client=ca-pub-3129977114552745"
336 crossorigin="anonymous"></script>
337 <!-- Banner Ad -->
338 <ins class="adsbygoogle"
339 style="display:block"
340 data-ad-client="ca-pub-3129977114552745"
341 data-ad-slot="1083537442"
342 data-ad-format="auto"
343 data-full-width-responsive="true"></ins>
344 <script>
345 (adsbygoogle = window.adsbygoogle || []).push({});
346 </script>
347 </div>
348 </div>
349 </div>
350 <a aria-label="About ImageMagick" class="btn btn-outline-warning mt-3" href="../index-2.html">About ImageMagick</a>
351 </main><!-- /.container -->
352 <footer class="text-center pt-5 my-5 text-body-secondary border-top">
353 <div class="container-fluid">
354 <a href="../security-policy/index.html">Security</a> •
355 <a href="../news/index.html">News</a>
356 &nbsp;
357 <a href="#"><img class="d-inline" id="wand" alt="And Now a Touch of Magick" width="16" height="16" src="../image/wand.ico"></a>
358 &nbsp;
359 <a href="../links/index.html">Related</a> •
360 <a href="../sitemap/index.html">Sitemap</a>
361 <br />
362 <a href="../support/index.html">Sponsor</a> •
363 <a href="../cite/index.html">Cite</a> •
364 <a href="http://pgp.mit.edu/pks/lookup?op=get&amp;search=0x89AB63D48277377A">Public Key</a> •
365 <a href="https://github.com/ImageMagick/ImageMagick/discussions">Contact Us</a>
366 <br />
367 <a href="https://github.com/imagemagick/imagemagick" rel="noopener noreferrer" target="_blank" aria-label="GitHub"><svg xmlns="http://www.w3.org/2000/svg" class="navbar-nav-svg" viewBox="0 0 512 499.36" width="2%" height="2%" role="img" focusable="false">
368 <title>GitHub</title>
369 <path fill="currentColor" fill-rule="evenodd" d="M256 0C114.64 0 0 114.61 0 256c0 113.09 73.34 209 175.08 242.9 12.8 2.35 17.47-5.56 17.47-12.34 0-6.08-.22-22.18-.35-43.54-71.2 15.49-86.2-34.34-86.2-34.34-11.64-29.57-28.42-37.45-28.42-37.45-23.27-15.84 1.73-15.55 1.73-15.55 25.69 1.81 39.21 26.38 39.21 26.38 22.84 39.12 59.92 27.82 74.5 21.27 2.33-16.54 8.94-27.82 16.25-34.22-56.84-6.43-116.6-28.43-116.6-126.49 0-27.95 10-50.8 26.35-68.69-2.63-6.48-11.42-32.5 2.51-67.75 0 0 21.49-6.88 70.4 26.24a242.65 242.65 0 0 1 128.18 0c48.87-33.13 70.33-26.24 70.33-26.24 14 35.25 5.18 61.27 2.55 67.75 16.41 17.9 26.31 40.75 26.31 68.69 0 98.35-59.85 120-116.88 126.32 9.19 7.9 17.38 23.53 17.38 47.41 0 34.22-.31 61.83-.31 70.23 0 6.85 4.61 14.81 17.6 12.31C438.72 464.97 512 369.08 512 256.02 512 114.62 397.37 0 256 0z" />
370 </svg></a> •
371 <a href="https://twitter.com/imagemagick" rel="noopener noreferrer" target="_blank" aria-label="Twitter"><svg xmlns="http://www.w3.org/2000/svg" class="navbar-nav-svg" viewBox="0 0 300 300" width="2%" height="2%" role="img" focusable="false">
372 <title>Twitter</title>
373 <path d="M178.57 127.15 290.27 0h-26.46l-97.03 110.38L89.34 0H0l117.13 166.93L0 300.25h26.46l102.4-116.59 81.8 116.59h89.34M36.01 19.54H76.66l187.13 262.13h-40.66" />
374 </svg></a>
375 <br />
376 <small>Copyright © 1999 ImageMagick Studio LLC</small>
377 </div>
378 </footer>
379 <div style="visibility: hidden;">
380 <div class="gcse-search"></div>
381 </div>
382 </div>
383
384 <!-- Javascript assets -->
385 <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI" crossorigin="anonymous"></script>
386 </body>
387
388
389 </html>