blob: 41855eecafbcd2508ade0a4a16b42dd9e1d9e835 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" value="application/xhtml+xml" />
<meta name="verify-v1" content="g222frIIxcQTrvDR3NBRUSKP3AnMNoqxOkIniCEkV7U=" />
<link rel="meta" type="application/rdf+xml" title="ICI" href="http://imagemagick.org/ici.rdf" />
<style type="text/css" media="screen,projection"><!--
@import url("../../www/magick.css");
--></style>
<link rel="shortcut icon" href="../../images/wand.ico" type="images/vnd.microsoft.icon"/>
<title>ImageMagick: MagickCore, C API for ImageMagick: Reduce the Number of Unique Colors in an Image</title>
<meta http-equiv="Content-Language" content="en-US"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="Reply-to" content="magick-users@imagemagick.org"/>
<meta name="Generator" content="PHP"/>
<meta name="Keywords" content="magickcore, c, api, for, imagemagick:, reduce, the, number, of, unique, colors, in, an, image, ImageMagick, ImageMagic, MagickCore, MagickWand, PerlMagick, Magick++, RMagick, PythonMagick, JMagick, TclMagick, Image, Magick, Magic, Wand, ImageMagickObject, Swiss, Army, Knife, Image, Processing"/>
<meta name="Description" content="ImageMagick® is a software suite to create, edit, and compose bitmap images. It can read, convert and write images in a variety of formats (about 100) including GIF, JPEG, JPEG-2000, PNG, PDF, PhotoCD, TIFF, and DPX. Use ImageMagick to translate, flip, mirror, rotate, scale, shear and transform images, adjust image colors, apply various special effects, or draw text, lines, polygons, ellipses and Bézier curves. ImageMagick is free software delivered as a ready-to-run binary distribution or as source code that you can freely use, copy, modify, and distribute. Its license is compatible with the GPL. It runs on all major operating systems. The functionality of ImageMagick is typically utilized from the command line or you can use the features from programs written in your favorite programming language. Choose from these interfaces: MagickCore (C), MagickWand (C), ChMagick (Ch), Magick++ (C++), JMagick (Java), L-Magick (Lisp), PascalMagick (Pascal), PerlMagick (Perl), MagickWand for PHP (PHP), PythonMagick (Python), RMagick (Ruby), or TclMagick (Tcl/TK). With a language interface, use ImageMagick to modify or create images automagically and dynamically."/>
<meta name="Rating" content="GENERAL"/>
<meta name="Robots" content="INDEX, FOLLOW"/>
<meta name="Generator" content="ImageMagick Studio LLC"/>
<meta name="Author" content="ImageMagick Studio LLC"/>
<meta name="Revisit-after" content="2 DAYS"/>
<meta name="Resource-type" content="document"/>
<meta name="Copyright" content="Copyright (c) 1999-2010 ImageMagick Studio LLC"/>
<meta name="Distribution" content="Global"/>
</head>
<body id="www-imagemagick-org">
<div class="titlebar">
<a href="../../index.html">
<img src="../../images/script.png" alt="[ImageMagick]"
style="width: 350px; height: 60px; margin: 28px auto; float: left;" /></a>
<a href="http://www.networkredux.com">
<img src="../../images/networkredux.png" alt="[sponsor]"
style="margin: 45px auto; border: 0px; float: left;" /></a>
<a href="http://www.imagemagick.org/discourse-server/">
<img src="../../images/logo.jpg" alt=""
style="width: 114px; height: 118px; border: 0px; float: right;" /></a>
<a href="../../index.html">
<img src="../../images/sprite.jpg" alt=""
style="width: 114px; height: 118px; border: 0px; float: right;" /></a>
</div>
<div class="eastbar">
<div class="menu">
<a href="../../index.html">About ImageMagick</a>
</div>
<div class="sep"></div>
<div class="menu">
<a href="../../www/command-line-tools.html">Command-line Tools</a>
</div>
<div class="sub">
<a href="../../www/command-line-processing.html">Processing</a>
</div>
<div class="sub">
<a href="../../www/command-line-options.html">Options</a>
</div>
<div class="sub">
<a href="http://www.imagemagick.org/Usage/">Usage</a>
</div>
<div class="menu">
<a href="../../www/api.html">Program Interfaces</a>
</div>
<div class="sub">
<a href="../../www/magick-wand.html">MagickWand</a>
</div>
<div class="sub">
<a href="../../www/magick-core.html">MagickCore</a>
</div>
<div class="sub">
<a href="../../www/perl-magick.html">PerlMagick</a>
</div>
<div class="sub">
<a href="../../Magick++/">Magick++</a>
</div>
<div class="menu">
<a href="../../www/architecture.html">Architecture</a>
</div>
<div class="sep"></div>
<div class="menu">
<a href="../../www/install-source.html">Install from Source</a>
</div>
<div class="sub">
<a href="../../www/install-source.html#unix">Unix</a>
</div>
<div class="sub">
<a href="../../www/install-source.html#windows">Windows</a>
</div>
<div class="menu">
<a href="../../www/binary-releases.html">Binary Releases</a>
</div>
<div class="sub">
<a href="../../www/binary-releases.html#unix">Unix</a>
</div>
<div class="sub">
<a href="../../www/binary-releases.html#macosx">Mac OS X</a>
</div>
<div class="sub">
<a href="../../www/binary-releases.html#windows">Windows</a>
</div>
<div class="menu">
<a href="../../www/resources.html">Resources</a>
</div>
<div class="sep"></div>
<div class="menu">
<a href="../../www/download.html">Download</a>
</div>
<div class="sep"></div>
<div class="menu">
<a href="../http://www.imagemagick.org/script/search.php">Search</a>
</div>
<div class="sep"></div>
<div class="menu">
<a href="../../www/sitemap.html">Site Map</a>
</div>
<div class="sub">
<a href="../../www/links.html">Links</a>
</div>
<div class="sep"></div>
<div class="menu">
<a href="../../www/sponsors.html">Sponsors:</a>
<div class="sponsbox">
<div class="sponsor">
<a href="http://www.abi-stoff.de/abizeitung/" title="Abibuch">Abizeitung</a><!-- 20101101000200 -->
</div>
<div class="sponsor">
<a href="http://www.deko.net">Deko.net</a><!-- 201101010600 Peterssen-->
</div>
<div class="sponsor">
<a href="http://www.tomsgutscheine.de">Tom's Gutscheine</a><!-- 201005010360 invendio.de-->
</div>
<div class="sponsor">
<a href="http://www.online-kredit-index.de">Kredit</a><!-- 201007010120 Buchhorn -->
</div>
<div class="sponsor">
<a href="http://www.blumenversender.com">Blumenversand</a><!-- 201005010120 -->
</div>
<div class="sponsor">
<a href="http://www.print24.de/">Druckerei</a><!-- 201009010720 -->
</div>
<div class="sponsor">
<a href="http://www.goyax.de">Börse</a><!-- 201004010240 Gewiese digital-finance.de -->
</div>
<div class="sponsor">
<a href="http://www.allesdruck.de">Druckerei Online</a><!-- 201012011200 allesdruck.de-->
</div>
</div>
</div>
</div>
<div class="main">
<p class="navigation-index">[<a href="#QuantizeImage">QuantizeImage</a> &bull; <a href="#AcquireQuantizeInfo">AcquireQuantizeInfo</a> &bull; <a href="#CloneQuantizeInfo">CloneQuantizeInfo</a> &bull; <a href="#CompressImageColormap">CompressImageColormap</a> &bull; <a href="#DestroyQuantizeInfo">DestroyQuantizeInfo</a> &bull; <a href="#GetImageQuantizeError">GetImageQuantizeError</a> &bull; <a href="#GetQuantizeInfo">GetQuantizeInfo</a> &bull; <a href="#PosterizeImage">PosterizeImage</a> &bull; <a href="#QuantizeImage">QuantizeImage</a> &bull; <a href="#QuantizeImages">QuantizeImages</a> &bull; <a href="#RemapImage">RemapImage</a> &bull; <a href="#RemapImages">RemapImages</a> &bull; <a href="#SetGrayscaleImage">SetGrayscaleImage</a>]</p>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="QuantizeImage">QuantizeImage</a></h2>
<div class="doc-section">
<p>QuantizeImage() takes a standard RGB or monochrome images and quantizes them down to some fixed number of colors.</p></ol>
<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></ol>
<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></ol>
<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:</p>
<pre class="text">
the origin.
</pre>
<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></ol>
<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></ol>
<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></ol>
<p>A complete tree would require 19,173,961 nodes for k = 8, Cmax = 255.</p>
<h5>avoid building a fully populated tree, QUANTIZE</h5>
<ol><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></ol>
<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>
<pre class="text">
n1: Number of pixels whose color is contained in the RGB cube which
this node represents;
</pre>
<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></ol>
<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></ol>
<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></ol>
<p>Reduction repeatedly prunes the tree until the number of nodes with n2 > 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></ol>
<p>Ep = 0 while number of nodes with (n2 > 0) > required maximum number of colors prune all nodes such that E <= Ep Set Ep to minimum E in remaining nodes</p></ol>
<p>This has the effect of minimizing any quantization error when merging two nodes together.</p></ol>
<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. 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.</p></ol>
<p>For each node, n2 pixels exist for which that node represents the smallest volume in RGB space containing those pixel's colors. When n2 > 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.</p></ol>
<p>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.</p></ol>
<p>Assignment generates the output image from the pruned tree. The output</p>
<h5>parts</h5>
<ol><p>(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.</p></ol>
<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 n2 > 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.</p></ol>
<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></ol>
<p>This method is based on a similar algorithm written by Paul Raveling.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="AcquireQuantizeInfo">AcquireQuantizeInfo</a></h2>
<div class="doc-section">
<p>AcquireQuantizeInfo() allocates the QuantizeInfo structure.</p></ol>
<p>The format of the AcquireQuantizeInfo method is:</p>
<pre class="code">
QuantizeInfo *AcquireQuantizeInfo(const ImageInfo *image_info)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>image_info</h5>
<ol><p>the image info.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="CloneQuantizeInfo">CloneQuantizeInfo</a></h2>
<div class="doc-section">
<p>CloneQuantizeInfo() makes a duplicate of the given quantize info structure, or if quantize info is NULL, a new one.</p></ol>
<p>The format of the CloneQuantizeInfo method is:</p>
<pre class="code">
QuantizeInfo *CloneQuantizeInfo(const QuantizeInfo *quantize_info)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>clone_info</h5>
<ol><p>Method CloneQuantizeInfo returns a duplicate of the given quantize info, or if image info is NULL a new one.</p></ol>
<h5>quantize_info</h5>
<ol><p>a structure of type info.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="CompressImageColormap">CompressImageColormap</a></h2>
<div class="doc-section">
<p>CompressImageColormap() compresses an image colormap by removing any duplicate or unused color entries.</p></ol>
<p>The format of the CompressImageColormap method is:</p>
<pre class="code">
MagickBooleanType CompressImageColormap(Image *image)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>image</h5>
<ol><p>the image.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="DestroyQuantizeInfo">DestroyQuantizeInfo</a></h2>
<div class="doc-section">
<p>DestroyQuantizeInfo() deallocates memory associated with an QuantizeInfo structure.</p></ol>
<p>The format of the DestroyQuantizeInfo method is:</p>
<pre class="code">
QuantizeInfo *DestroyQuantizeInfo(QuantizeInfo *quantize_info)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>quantize_info</h5>
<ol><p>Specifies a pointer to an QuantizeInfo structure.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="GetImageQuantizeError">GetImageQuantizeError</a></h2>
<div class="doc-section">
<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>
<pre class="text">
o mean_error_per_pixel: This value is the mean error for any single
pixel in the image.
</pre>
<h5>normalized_mean_square_error</h5>
<ol><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></ol>
<h5>normalized_maximum_square_error</h5>
<ol><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></ol>
<p>The format of the GetImageQuantizeError method is:</p>
<pre class="code">
MagickBooleanType GetImageQuantizeError(Image *image)
</pre>
<p>A description of each parameter follows.</p></ol>
<h5>image</h5>
<ol><p>the image.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="GetQuantizeInfo">GetQuantizeInfo</a></h2>
<div class="doc-section">
<p>GetQuantizeInfo() initializes the QuantizeInfo structure.</p></ol>
<p>The format of the GetQuantizeInfo method is:</p>
<pre class="code">
GetQuantizeInfo(QuantizeInfo *quantize_info)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>quantize_info</h5>
<ol><p>Specifies a pointer to a QuantizeInfo structure.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="PosterizeImage">PosterizeImage</a></h2>
<div class="doc-section">
<p>PosterizeImage() reduces the image to a limited number of colors for a "poster" effect.</p></ol>
<p>The format of the PosterizeImage method is:</p>
<pre class="code">
MagickBooleanType PosterizeImage(Image *image,const unsigned long levels,
const MagickBooleanType dither)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>image</h5>
<ol><p>Specifies a pointer to an Image structure.</p></ol>
<h5>levels</h5>
<ol><p>Number of color levels allowed in each channel. Very low values (2, 3, or 4) have the most visible effect.</p></ol>
<h5>dither</h5>
<ol><p>Set this integer value to something other than zero to dither the mapped image.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="QuantizeImage">QuantizeImage</a></h2>
<div class="doc-section">
<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></ol>
<p>The format of the QuantizeImage method is:</p>
<pre class="code">
MagickBooleanType QuantizeImage(const QuantizeInfo *quantize_info,
Image *image)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>quantize_info</h5>
<ol><p>Specifies a pointer to an QuantizeInfo structure.</p></ol>
<h5>image</h5>
<ol><p>the image.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="QuantizeImages">QuantizeImages</a></h2>
<div class="doc-section">
<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></ol>
<p>The format of the QuantizeImages method is:</p>
<pre class="code">
MagickBooleanType QuantizeImages(const QuantizeInfo *quantize_info,
Image *images)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>quantize_info</h5>
<ol><p>Specifies a pointer to an QuantizeInfo structure.</p></ol>
<h5>images</h5>
<ol><p>Specifies a pointer to a list of Image structures.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="RemapImage">RemapImage</a></h2>
<div class="doc-section">
<p>RemapImage() replaces the colors of an image with the closest color from a reference image.</p></ol>
<p>The format of the RemapImage method is:</p>
<pre class="code">
MagickBooleanType RemapImage(const QuantizeInfo *quantize_info,
Image *image,const Image *remap_image)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>quantize_info</h5>
<ol><p>Specifies a pointer to an QuantizeInfo structure.</p></ol>
<h5>image</h5>
<ol><p>the image.</p></ol>
<h5>remap_image</h5>
<ol><p>the reference image.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="RemapImages">RemapImages</a></h2>
<div class="doc-section">
<p>RemapImages() replaces the colors of a sequence of images with the closest color from a reference image.</p></ol>
<p>The format of the RemapImage method is:</p>
<pre class="code">
MagickBooleanType RemapImages(const QuantizeInfo *quantize_info,
Image *images,Image *remap_image)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>quantize_info</h5>
<ol><p>Specifies a pointer to an QuantizeInfo structure.</p></ol>
<h5>images</h5>
<ol><p>the image sequence.</p></ol>
<h5>remap_image</h5>
<ol><p>the reference image.</p></ol>
</div>
<h2><a href="http://www.imagemagick.org/api/MagickCore/quantize
_8c.html" target="source" name="SetGrayscaleImage">SetGrayscaleImage</a></h2>
<div class="doc-section">
<p>SetGrayscaleImage() converts an image to a PseudoClass grayscale image.</p></ol>
<p>The format of the SetGrayscaleImage method is:</p>
<pre class="code">
MagickBooleanType SetGrayscaleImage(Image *image)
</pre>
<p>A description of each parameter follows:</p></ol>
<h5>image</h5>
<ol><p>The image.</p></ol>
</div>
</div>
<div id="linkbar">
<!-- <span id="linkbar-west">&nbsp;</span> -->
<span id="linkbar-center">
<a href="http://www.imagemagick.org/discourse-server/">Discourse Server</a> &bull;
<a href="http://www.imagemagick.org/MagickStudio/scripts/MagickStudio.cgi">Studio</a>
</span>
<span id="linkbar-east">&nbsp;</span>
</div>
<div class="footer">
<span id="footer-west">&copy; 1999-2010 ImageMagick Studio LLC</span>
<span id="footer-east"> <a href="../http://www.imagemagick.org/script/contact.php">Contact the Wizards</a></span>
</div>
<div style="clear: both; margin: 0; width: 100%; "></div>
</body>
</html>