summary refs log tree commit diff
path: root/pnmcolormap.html
blob: 95fe49b9d5c79b14b2be561f27d190a2eb50f77c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.3//EN">
<html><head><title>Pnmcolormap User Manual</title></head>
<body>
<h1>pnmcolormap</h1>
Updated: 21 February 2023
<br>
<a href="#index">Table Of Contents</a>

<h2>NAME</h2>

pnmcolormap - create quantization color map for a Netpbm image

<h2 id="synopsis">SYNOPSIS</h2>

<b>pnmcolormap</b>

[<b>-center</b>|<b>-meancolor</b>|<b>-meanpixel</b>]

[<b>-spreadbrightness</b>|<b>-spreadluminosity</b>]

[<b>-splitpixelct</b>|<b>-splitcolorct</b>|<b>-splitspread</b>]

[<b>-sort</b>]

[<b>-square</b>] 

[<b>-verbose</b>] 

[<b>-debug</b>] 

<i>ncolors</i>|<b>all</b>

[<i>pnmfile</i>]

<h2 id="description">DESCRIPTION</h2>

<p>This program is part of <a href="index.html">Netpbm</a>.

<p><b>pnmcolormap</b> reads a PNM or PAM image as input, chooses
<i>ncolors</i> colors to best represent the image and writes a PNM
color map defining them as output.  A PAM image may actually contain
tuples of any kind, but <b>pnmcolormap</b>'s concept of the tuple values
that best represent the ones present in the image may not make sense if
the tuple type isn't RGB or GRAYSCALE.  The design of the program, and
the rest of this manual, assumes the tuples represent colors.

<p>You can use this map as input to <b>pnmremap</b> on the same input
image to quantize the colors in that image, I.e. produce a similar
image with fewer colors.  <b>pnmquant</b> does both the <b>pnmcolormap</b>
and <b>pnmremap</b> steps for you.

<p>A PNM colormap is a PNM image of any dimensions that contains at
least one pixel of each color in the set of colors it represents.  The
ones <b>pnmcolormap</b> generates have exactly one pixel of each color,
except where padding is necessary with the <b>-square</b> option.

<p>The quantization method is Heckbert's "median cut".
See <a href="#quant">QUANTIZATION METHOD</a>.

<p>The output image is of the same format (PBM, PGM, PPM, PAM) as the
input image.  Note that a colormap of a PBM image is not very
interesting.

<p>The colormap generally has the same maxval as the input image, but
<b>pnmcolormap</b> may reduce it if there are too many colors in the
input, as part of its quantization algorithm.

<p><b>pnmcolormap</b> works on a multi-image input stream.  In that
case, it produces one colormap that applies to all of the colors in
all of the input images.  All the images must have the same format,
depth, and maxval (but may have different height and width).  This is
useful if you need to quantize a bunch of images that will form a
movie or otherwise be used together -- you generally want them all to
draw from the same palette, whereas computing a colormap separately
from each image would make the same color in two images map to
different colors.  Before Netpbm 10.31 (December 2005), <b>pnmcolormap</b>
ignored any image after the first.

<p>If you want to create a colormap without basing it on the colors in
an input image, <b>pamseq</b>, <b>ppmmake</b>, and <b>pnmcat</b> can
be useful.

<h2 id="parameters">PARAMETERS</h2>

<p>The single parameter, which is required, is the number of colors you want
in the output colormap.  <b>pnmcolormap</b> may produce a color map with
slightly fewer colors than that.  You may specify <b>all</b> to get a colormap
of every color in the input image (no quantization).  When you specify
<b>all</b>, the function is essentially the same as that of <b>ppmhist
-map</b>.  <b>ppmhist</b> is much older.


<h2 id="options">OPTIONS</h2>

<p>In addition to the options common to all programs based on libnetpbm
(most notably <b>-quiet</b>, see <a href="index.html#commonoptions">
Common Options</a>), <b>pnmcolormap</b> recognizes the following
command line options:

<p>All options can be abbreviated to their shortest unique prefix.  You
may use two hyphens instead of one to designate an option.  You may
use either white space or an equals sign between an option name and
its value.

<dl compact>
<dt><b>-sort</b>

<dd>This option causes the output colormap to be sorted by the red
component intensity, then the green, then the blue in ascending order.
This is an insertion sort, so it is not very fast on large colormaps.
Sorting is useful because it allows you to compare two sets of colors.

<dt><b>-square</b>

<dd>By default, <b>pnmcolormap</b> produces as the color map a PPM
image with one row and with one column for each color in the colormap.
This option causes <b>pnmcolormap</b> instead to produce a PPM image
that is within one row or column of being square, with the last pixel
duplicated as necessary to create a number of pixels which is such an
almost-perfect square.

<dt><b>-verbose</b>

<dd>This option causes <b>pnmcolormap</b> to display messages to
Standard Error about the quantization.  <dt><b>-center</b>

<dt><b>-debug</b>

<dd>This option causes <b>pnmcolormap</b> to display messages to
Standard Error about internal workings.

<dt><b>-center</b>

<dt><b>-meancolor</b>

<dt><b>-meanpixel</b>

<dt><b>-spreadbrightness</b>

<dt><b>-spreadluminosity</b>

<dt><b>-splitpixelct</b>

<dt><b>-splitcolorct</b>

<dt><b>-splitspread</b>

<dd>These options control the quantization algorithm.  See <a
href="#quant">QUANTIZATION METHOD</a>.

</dl>


<h2 id="quant">QUANTIZATION METHOD</h2>

<p>A quantization method is a way to choose which colors, being fewer
in number than in the input, you want in the output.
<b>pnmcolormap</b> uses Heckbert's "median cut" quantization
method.

<p>This method involves separating all the colors into
"boxes," each holding colors that represent about the same
number of pixels.  You start with one box and split boxes in two until
the number of boxes is the same as the number of colors you want in
the output, and choose one color to represent each box.

<p>There are three ways <b>pnmcolormap</b> can choose the box to split in
  each step:
  <ul>
    <li>Split the box containing the most pixels.  This is the default,
      and you can select it explicitly with option <b>-splitpixelct</b>.
    <li>Split the box containing the most colors.  This appears to be useful
      for academic purposes only.  Select this with option
      <b>-splitcolorct</b>.
    <li>Split the box containing the largest color spread.  Select this
      with option <b>-splitspread</b>.  This can produce a better result for
      small details with colors not found elsewhere in the image.
  </ul>

<p><b>-splitpixelct</b>, <b>splitcolorct</b>, and <b>splitspread</b> were new
  in Netpbm 10.88 (September 2019).  Before that, <b>pnmcolormap</b> always
  splits the box containing the most pixels.
  
<p>When you split a box, you do it so each sub-box has the same number of
pixels (except one sub-box has more if the full box has an odd number), with
the &quot;greatest&quot; pixels in one sub-box and the &quot;least&quot;
pixels in the other.  "Greater," for a particular box, means it is brighter in
the color component (red, green, blue) which has the largest spread in that
box.  <b>pnmcolormap</b> gives you two ways to define "largest spread.": 1)
largest spread of brightness; 2) largest spread of contribution to the
luminosity of the color.  E.g. red is weighted much more than blue.  Select
among these with the <b>-spreadbrightness</b> and <b>-spreadluminosity</b>
options.  The default is <b>-spreadbrightness</b>.  Where there are multiple
colors of the median magnitude, they are distributed arbitrarily among between
the subboxes.  This arbitrary distribution depends upon what the system's
<b>qsort</b> function does with multiple equal values, so <b>pnmcolormap</b>
may produce slightly different results on different systems.

<p><b>pnmcolormap</b> provides three ways of choosing a color to represent a
box: 1) the center color - the color halfway between the greatest and least
colors in the box, using the above definition of "greater"; 2) the mean of the
colors (each component averaged separately by brightness) in the box; 3) the
mean weighted by the number of pixels of a color in the image.

<p>Select among these with the <b>-center</b>, <b>-meancolor</b>, and
<b>-meanpixel</b> options.  The default is <b>-center</b>.

<p>Note that in all three methods, there may be colors in the output
which do not appear in the input at all.

<p>Also note that the color chosen to represent the colors in Box A the best
may also represent a color in Box B better than the color chosen to represent
the colors in Box B the best.  This is true for various measures of goodness
of representation of one color by another.  In particular, if you
use <b>pnmremap</b> to map the colors in the very image that you used to
create the color map to the colors in that colormap, the colors in Box B will
often map to the color <b>pnmcolormap</b> chose to represent some other box
and in fact the color <b>pnmcolormap</b> chose to represent Box B may not
appear in the <b>pnmremap</b> output at all.
  

<h2 id="references">REFERENCES</h2>

"Color Image Quantization for Frame Buffer Display" by Paul Heckbert,
SIGGRAPH '82 Proceedings, page 297.

<h2 id="seealso">SEE ALSO</h2>

<b><a href="pnmremap.html">pnmremap</a></b>,
<b><a href="pnmquant.html">pnmquant</a></b>,
<b><a href="ppmquantall.html">ppmquantall</a></b>,
<b><a href="pamgetcolor.html">pamgetcolor</a></b>,
<b><a href="pamdepth.html">pamdepth</a></b>,
<b><a href="ppmdither.html">ppmdither</a></b>,
<b><a href="pamseq.html">pamseq</a></b>,
<b><a href="ppmmake.html">ppmmake</a></b>,
<b><a href="pnmcat.html">pnmcat</a></b>,
<b><a href="ppm.html">ppm</a></b>

<h2 id="history">HISTORY</h2>

<p>Before Netpbm 10.15 (April 2003), <b>pnmcolormap</b> used a lot
more memory for large images because it kept the entire input image in
memory.  Now, it processes it a row at a time, but because it
sometimes must make multiple passes through the image, it first copies
the input into a temporary seekable file if it is not already in a seekable
file.

<p><b>pnmcolormap</b> first appeared in Netpbm 9.23 (January 2002).
Before that, its function was available only as part of the function
of <b>pnmquant</b> (which was derived from the much older
<b>ppmquant</b>).  Color quantization really has two main subfunctions, so
Netpbm 9.23 split it out into two separate programs:
<b>pnmcolormap</b> and <b>pnmremap</b> and then Netpbm 9.24 replaced
<b>pnmquant</b> with a program that simply calls <b>pnmcolormap</b> and
<b>pnmremap</b>.

<h2 id="author">AUTHOR</h2>

Copyright (C) 1989, 1991 by Jef Poskanzer.


<hr>
<h2 id="index">Table Of Contents</h2>
<ul>
<li><a href="#synopsis">SYNOPSIS</a>
<li><a href="#description">DESCRIPTION</a>
<li><a href="#parameters">PARAMETERS</a>
<li><a href="#options">OPTIONS</a>
<li><a href="#quant">QUANTIZATION METHOD</a>
<li><a href="#references">REFERENCES</a>
<li><a href="#seealso">SEE ALSO</a>
<li><a href="#history">HISTORY</a>
<li><a href="#author">AUTHOR</a>
</ul>
</body>
</html>