1Pamscale User Manual(0)                                Pamscale User Manual(0)
2
3
4

NAME

6       pamscale - scale a Netpbm image
7

SYNOPSIS

9          pamscale
10             [
11                scale_factor
12                |
13                {-xyfit | -xyfill | -xysize} cols rows
14                |
15                -reduce reduction_factor
16                |
17                [-xsize=cols | -width=cols | -xscale=factor]
18                [-ysize=rows | -height=rows | -yscale=factor]
19                |
20                -pixels n
21             ]
22             [
23                [-verbose]
24                [
25                   -nomix
26                   |
27                   -filter=functionName [-window=functionName]
28                ]
29                [-linear]
30             }
31             [pnmfile]
32
33
34       Minimum  unique abbreviation of option is acceptable.  You may use dou‐
35       ble hyphens instead of single hyphen to denote options.   You  may  use
36       white space in place of the equals sign to separate an option name from
37       its value.
38
39

DESCRIPTION

41       This program is part of Netpbm(1).
42
43       pamscale scales a Netpbm image by a specified factor, or  scales  indi‐
44       vidually horizontally and vertically by specified factors.
45
46       You can either enlarge (scale factor > 1) or reduce (scale factor < 1).
47
48
49   The Scale Factors
50       The  options -width, -height, -xsize, -ysize, -xscale, -yscale, -xyfit,
51       -xyfill, -reduce, and -pixels control the amount of scaling.  For back‐
52       ward  compatibility,  there  is also -xysize and the scale_factor argu‐
53       ment, but you shouldn't use those.
54
55       -width and -height specify the width and height in pixels you want  the
56       resulting  image  to  be.  See below for rules when you specify one and
57       not the other.
58
59       -xsize and -ysize are synonyms for -width and -height, respectively.
60
61       -xscale and -yscale tell the factor by which you  want  the  width  and
62       height  of  the  image to change from source to result (e.g.  -xscale 2
63       means you want to double the width; -xscale .5 means you want to  halve
64       it).  See below for rules when you specify one and not the other.
65
66       When  you specify an absolute size or scale factor for both dimensions,
67       pamscale scales each dimension independently without  consideration  of
68       the aspect ratio.
69
70       If  you  specify  one  dimension  as a pixel size and don't specify the
71       other dimension, pamscale scales the unspecified dimension to  preserve
72       the aspect ratio.
73
74       If  you  specify  one dimension as a scale factor and don't specify the
75       other dimension, pamscale leaves the  unspecified  dimension  unchanged
76       from the input.
77
78       If you specify the scale_factor parameter instead of dimension options,
79       that is the scale factor for both  dimensions.   It  is  equivalent  to
80       -xscale=scale_factor -yscale=scale_factor.
81
82       Specifying  the -reduce reduction_factor option is equivalent to speci‐
83       fying the scale_factor  parameter, where scale_factor is the reciprocal
84       of reduction_factor.
85
86       -xyfit  specifies  a  bounding box.  pamscale scales the input image to
87       the largest size that fits within the box, while preserving its  aspect
88       ratio.   -xysize  is  a synonym for this.  Before Netpbm 10.20 (January
89       2004), -xyfit did not exist, but -xysize did.
90
91       -xyfill is similar, but pamscale scales the input image to the smallest
92       size  that completely fills the box, while preserving its aspect ratio.
93       This option has existed since Netpbm 10.20 (January 2004).
94
95       -pixels specifies a maximum total number of  output  pixels.   pamscale
96       scales  the image down to that number of pixels.  If the input image is
97       already no more than that many pixels, pamscale just copies it as  out‐
98       put; pamscale does not scale up with -pixels.
99
100       If  you  enlarge  by  a  factor of 3 or more, you should probably add a
101       pnmsmooth step; otherwise, you can  see  the  original  pixels  in  the
102       resulting image.
103
104
105
106   Usage Notes
107       A  useful  application  of pamscale is to blur an image.  Scale it down
108       (without -nomix) to discard some information, then  scale  it  back  up
109       using pamstretch.
110
111       Or scale it back up with pamscale and create a 'pixelized' image, which
112       is sort of a computer-age version of blurring.
113
114
115
116   Transparency
117       pamscale understands transparency and properly mixes pixels considering
118       the pixels' transparency.
119
120       Proper  mixing does not mean just mixing the transparency value and the
121       color component values separately.  In a PAM image, a  pixel  which  is
122       not  opaque  represents  a  color that contains light of the foreground
123       color indicated explicitly in the PAM and light of a  background  color
124       to be named later.  But the numerical scale of a color component sample
125       in a PAM is as if the pixel is opaque.  So a pixel that is supposed  to
126       contain half-strength red light for the foreground plus some light from
127       the background has a red color sample that says full red and  a  trans‐
128       parency  sample that says 50% opaque.  In order to mix pixels, you have
129       to first convert the color sample  values  to  numbers  that  represent
130       amount  of  light  directly (i.e. multiply by the opaqueness) and after
131       mixing, convert back (divide by the opaqueness).
132
133
134   Input And Output Image Types
135       pamscale produces output of the same type (and tuple type if  the  type
136       is  PAM)  as  the input, except if the input is PBM.  In that case, the
137       output is PGM with maxval 255.  The purpose of this is to  allow  mean‐
138       ingful  pixel  mixing.  Note that there is no equivalent exception when
139       the input is PAM.  If the PAM input tuple type  is  BLACKANDWHITE,  the
140       PAM  output tuple type is also BLACKANDWHITE, and you get no meaningful
141       pixel mixing.
142
143       If you want PBM output with PBM input, use pamditherbw to convert  pam‐
144       scale's output to PBM.  Also consider pbmreduce.
145
146       pamscale's  function is essentially undefined for PAM input images that
147       are not of tuple type RGB,  GRAYSCALE,  BLACKANDWHITE,  or  the  _ALPHA
148       variations  of those.  (By standard Netpbm backward compatibility, this
149       includes PBM, PGM, and PPM images).
150
151       You might think it would have an obvious effect on other  tuple  types,
152       but  remember  that  the aforementioned tuple types have gamma-adjusted
153       sample values, and pamscale uses that fact in its calculations.  And it
154       treats a transparency plane different from any other plane.
155
156       pamscale  does  not  simply  reject  unrecognized  tuple  types because
157       there's a possibility that just by coincidence you can get useful func‐
158       tion  out of it with some other tuple type and the right combination of
159       options (consider -linear in particular).
160
161
162
163   Methods Of Scaling
164       There are numerous ways to scale an image.  pamscale implements a bunch
165       of them; you select among them with invocation options.
166
167       Pixel Mixing
168
169       Pamscale's default method is pixel mixing.  To understand this, imagine
170       the source image as composed of square tiles.  Each tile is a pixel and
171       has uniform color.  The tiles are all the same size.  Now take a trans‐
172       parent sheet the size of the target image, marked with a square grid of
173       tiles  the same size.  Stretch or compress the source image to the size
174       of the sheet and lay the sheet over the source.
175
176       Each cell in the overlay grid stands for a pixel of the  target  image.
177       For  example,  if you are scaling a 100x200 image up by 1.5, the source
178       image is 100 x 200 tiles, and the transparent sheet is  marked  off  in
179       150 x 300 cells.
180
181       Each  cell  covers  parts of multiple tiles.  To make the target image,
182       just color in each cell with the color which is the average of the col‐
183       ors  the cell covers -- weighted by the amount of that color it covers.
184       A cell in our example might cover 4/9 of a blue  tile,  2/9  of  a  red
185       tile,  2/9  of  a  green  tile, and 1/9 of a white tile.  So the target
186       pixel would be somewhat unsaturated blue.
187
188       When you are scaling up or down by an integer, the results are  simple.
189       When  scaling up, pixels get duplicated.  When scaling down, pixels get
190       thrown away.  In either case, the colors in the target image are a sub‐
191       set of those in the source image.
192
193       When  the  scale factor is weirder than that, the target image can have
194       colors that didn't exist in the original.  For  example,  a  red  pixel
195       next  to  a  white pixel in the source might become a red pixel, a pink
196       pixel, and a white pixel in the target.
197
198       This method tends to replicate what the human  eye  does  as  it  moves
199       closer  to  or  further away from an image.  It also tends to replicate
200       what the human eye sees, when far enough away to make the  pixelization
201       disappear,  if  an  image is not made of pixels and simply stretches or
202       shrinks.
203
204       Discrete Sampling
205
206       Discrete sampling is basically the same thing as  pixel  mixing  except
207       that,  in the model described above, instead of averaging the colors of
208       the tiles the cell covers, you pick the one color that covers the  most
209       area.
210
211       The result you see is that when you enlarge an image, pixels get dupli‐
212       cated and when you reduce an image, some pixels get discarded.
213
214       The advantage of this is that you end up with an image  made  from  the
215       same color palette as the original.  Sometimes that's important.
216
217       The  disadvantage  is that it distorts the picture.  If you scale up by
218       1.5 horizontally, for example, the even numbered input pixels are  dou‐
219       bled in the output and the odd numbered ones are copied singly.  If you
220       have a bunch of one pixel wide lines in the source, you may  find  that
221       some  of  them  stretch  to  2  pixels,  others remain 1 pixel when you
222       enlarge.  When you reduce, you may find that some of the  lines  disap‐
223       pear completely.
224
225       You select discrete sampling with pamscale's -nomix option.
226
227       Actually,  -nomix  doesn't  do exactly what I described above.  It does
228       the scaling in two passes - first horizontal, then vertical.  This  can
229       produce slightly different results.
230
231       There is one common case in which one often finds it burdensome to have
232       pamscale make up colors that weren't there  originally:  Where  one  is
233       working  with  an image format such as GIF that has a limited number of
234       possible colors per image.  If you take a GIF with 256 colors,  convert
235       it  to  PPM,  scale by .625, and convert back to GIF, you will probably
236       find that the reduced image has way more than 256 colors, and therefore
237       cannot be converted to GIF.  One way to solve this problem is to do the
238       reduction with discrete sampling instead of pixel mixing.   Probably  a
239       better  way  is  to  do  the  pixel mixing, but then color quantize the
240       result with pnmquant before converting to GIF.
241
242       When the scale factor is an integer (which means  you're  scaling  up),
243       discrete  sampling  and pixel mixing are identical -- output pixels are
244       always just N copies of the input pixels.  In this case,  though,  con‐
245       sider  using  pamstretch  instead  of  pamscale to get the added pixels
246       interpolated instead of just copied and thereby get a smoother enlarge‐
247       ment.
248
249       pamscale's  discrete  sampling  is faster than pixel mixing, but pamen‐
250       large is faster still.  pamenlarge works only on integer enlargements.
251
252       discrete sampling (-nomix) was new in Netpbm 9.24 (January 2002).
253
254
255       Resampling
256
257       Resampling assumes that the source image is a discrete sampling of some
258       original  continuous image.  That is, it assumes there is some non-pix‐
259       elized original image and each pixel of the source image is simply  the
260       color  of  that  image at a particular point.  Those points, naturally,
261       are the intersections of a square grid.
262
263       The idea of resampling is just to compute  that  original  image,  then
264       sample it at a different frequency (a grid of a different scale).
265
266       The  problem,  of  course, is that sampling necessarily throws away the
267       information you need to rebuild the original image.  So we have to make
268       a bunch of assumptions about the makeup of the original image.
269
270       You  tell pamscale to use the resampling method by specifying the -fil‐
271       ter option.  The value of this option is the name of a  function,  from
272       the set listed below.
273
274       To  explain  resampling, we are going to talk about a simple one dimen‐
275       sional scaling -- scaling a single row  of  grayscale  pixels  horizon‐
276       tally.  If you can understand that, you can easily understand how to do
277       a whole image: Scale each of the rows of the image, then scale each  of
278       the  resulting  columns.   And scale each of the color component planes
279       separately.
280
281       As a first step in resampling,  pamscale  converts  the  source  image,
282       which  is  a set of discrete pixel values, into a continuous step func‐
283       tion.  A step function is a  function  whose  graph  is  a  staircase-y
284       thing.
285
286       Now,  we convolve the step function with a proper scaling of the filter
287       function that you identified with -filter.  If you don't know what  the
288       mathematical concept of convolution (convolving) is, you are officially
289       lost.  You cannot understand this explanation.  The result of this con‐
290       volution  is the imaginary original continuous image we've been talking
291       about.
292
293       Finally, we make target pixels by picking values from that function.
294
295       To understand what is going on, we use Fourier analysis:
296
297       The idea is that the only difference between our step function and  the
298       original  continuous  function  (remember  that we constructed the step
299       function from the source image, which is itself a sampling of the orig‐
300       inal continuous function) is that the step function has a bunch of high
301       frequency Fourier components added.  If  we  could  chop  out  all  the
302       higher frequency components of the step function, and know that they're
303       all higher than any frequency in the original function, we'd  have  the
304       original function back.
305
306       The resampling method assumes that the original function was sampled at
307       a high enough frequency to form a perfect sampling.  A perfect sampling
308       is one from which you can recover exactly the original continuous func‐
309       tion.  The Nyquist theorem says that as long as your sample rate is  at
310       least  twice  the highest frequency in your original function, the sam‐
311       pling is perfect.  So we assume that the image is a sampling  of  some‐
312       thing  whose  highest  frequency is half the sample rate (pixel resolu‐
313       tion) or less.  Given that, our filtering  does  in  fact  recover  the
314       original continuous image from the samples (pixels).
315
316       To  chop out all the components above a certain frequency, we just mul‐
317       tiply the Fourier transform of the step function by a  rectangle  func‐
318       tion.
319
320       We  could  find the Fourier transform of the step function, multiply it
321       by a rectangle function, and then Fourier transform  the  result  back,
322       but  there's an easier way.  Mathematicians tell us that multiplying in
323       the frequency domain is equivalent to convolving in  the  time  domain.
324       That  means multiplying the Fourier transform of F by a rectangle func‐
325       tion R is the same as convolving F with the  Fourier  transform  of  R.
326       It's a lot better to take the Fourier transform of R, and build it into
327       pamscale than to have pamscale take the Fourier transform of the  input
328       image dynamically.
329
330       That leaves only one question:  What is the Fourier transform of a rec‐
331       tangle function?  Answer: sinc.  Recall from math that sinc is  defined
332       as sinc(x) = sin(PI*x)/PI*x.
333
334       Hence,  when  you specify -filter=sinc, you are effectively passing the
335       step function of the source image through a low pass  frequency  filter
336       and recovering a good approximation of the original continuous image.
337
338       Refiltering
339
340       There's  another twist: If you simply sample the reconstructed original
341       continuous image at the new sample rate, and that new sample rate isn't
342       at  least twice the highest frequency in the original continuous image,
343       you won't get a perfect sampling.  In fact, you'll get  something  with
344       ugly  aliasing  in  it.   Note that this can't be a problem when you're
345       scaling up (increasing the sample rate), because the fact that the  old
346       sample  rate  was above the Nyquist level means so is the new one.  But
347       when scaling down, it's a problem.  Obviously,  you  have  to  give  up
348       image quality when scaling down, but aliasing is not the best way to do
349       it.  It's better just to remove  high  frequency  components  from  the
350       original  continuous image before sampling, and then get a perfect sam‐
351       pling of that.
352
353       Therefore, pamscale filters out frequencies above half the  new  sample
354       rate before picking the new samples.
355
356       Approximations
357
358       Unfortunately,  pamscale doesn't do the convolution precisely.  Instead
359       of evaluating the filter function at every  point,  it  samples  it  --
360       assumes  that  it  doesn't change any more often than the step function
361       does.  pamscale could actually do the true integration  fairly  easily.
362       Since the filter functions are built into the program, the integrals of
363       them could be too.  Maybe someday it will.
364
365       There is one more complication with the  Fourier  analysis.   sinc  has
366       nonzero  values  on  out to infinity and minus infinity.  That makes it
367       hard to compute a convolution with it.  So instead,  there  are  filter
368       functions  that  approximate sinc but are nonzero only within a manage‐
369       able range.  To get those, you multiply the sinc function by  a  window
370       function, which you select with the -window option.  The same holds for
371       other filter functions that go on forever like sinc.  By default, for a
372       filter  that needs a window function, the window function is the Black‐
373       man function.
374
375       Filter Functions Besides Sinc
376
377       The math described above works only with sinc as the  filter  function.
378       pamscale  offers  many  other  filter functions, though.  Some of these
379       approximate sinc and are faster to compute.  For most of them,  I  have
380       no  idea  of  the mathematical explanation for them, but people do find
381       they give pleasing results.  They may not be  based  on  resampling  at
382       all,  but just exploit the convolution that is coincidentally part of a
383       resampling calculation.
384
385       For some filter functions, you can tell just by looking at the convolu‐
386       tion how they vary the resampling process from the perfect one based on
387       sinc:
388
389       The impulse filter assumes that the original  continuous  image  is  in
390       fact  a  step function -- the very one we computed as the first step in
391       the resampling.  This is mathematically equivalent to the discrete sam‐
392       pling method.
393
394       The  box  (rectangle)  filter assumes the original image is a piecewise
395       linear function.  Its graph just looks like straight  lines  connecting
396       the  pixel values.  This is mathematically equivalent to the pixel mix‐
397       ing method (but mixing brightness, not light intensity,  so  like  pam‐
398       scale  -linear)  when  scaling down, and interpolation (ala pamstretch)
399       when scaling up.
400
401       Gamma
402
403       pamscale assumes the underlying continuous function is  a  function  of
404       brightness (as opposed to light intensity), and therefore does all this
405       math using the gamma-adjusted numbers found in a PNM or PAM image.  The
406       -linear  option is not available with resampling (it causes pamscale to
407       fail), because it wouldn't be useful enough to justify the  implementa‐
408       tion effort.
409
410       Resampling (-filter) was new in Netpbm 10.20 (January 2004).
411
412       The filter functions
413
414       Here  is  a  list of the function names you can specify for the -filter
415       option.  For most of them, you're on your own to figure out  just  what
416       the  function  is  and  what kind of scaling it does.  These are common
417       functions from mathematics.
418
419
420
421       point  The graph of this is a single point at X=0, Y=1.
422
423
424       box    The graph of this is a rectangle sitting on the X axis and  cen‐
425              tered on the Y axis with height 1 and base 1.
426
427
428       triangle
429              The graph of this is an isosceles triangle sitting on the X axis
430              and centered on the Y axis with height 1 and base 2.
431
432
433       quadratic
434
435       cubic
436
437       catrom
438
439       mitchell
440
441       gauss
442
443       sinc
444
445       bessel
446
447       hanning
448
449       hamming
450
451       blackman
452
453       kaiser
454
455       normal
456
457       hermite
458
459       lanczos
460              Not documented
461
462
463
464
465   Linear vs Gamma-adjusted
466       The pixel mixing scaling method described above involves intensities of
467       pixels  (more  precisely, it involves individual intensities of primary
468       color components of pixels).  But the PNM and PNM-equivalent PAM  image
469       formats  represent intensities with gamma-adjusted numbers that are not
470       linearly proportional to intensity.  So pamscale, by default,  performs
471       a  calculation on each sample read from its input and each sample writ‐
472       ten to its output to convert between these gamma-adjusted  numbers  and
473       internal intensity-proportional numbers.
474
475       Sometimes you are not working with true PNM or PAM images, but rather a
476       variation in which the sample values are in fact directly  proportional
477       to  intensity.   If  so,  use the -linear option to tell pamscale this.
478       pamscale then will skip the conversions.
479
480       The conversion takes time.  In one experiment, it increased by a factor
481       of 10 the time required to reduce an image.  And the difference between
482       intensity-proportional values and gamma-adjusted values  may  be  small
483       enough that you would barely see a difference in the result if you just
484       pretended that the gamma-adjusted values were in fact intensity-propor‐
485       tional.   So  just  to save time, at the expense of some image quality,
486       you can specify -linear even when you have true PPM  input  and  expect
487       true PPM output.
488
489       For  the  first  13 years of Netpbm's life, until Netpbm 10.20 (January
490       2004), pamscale's predecessor pnmscale always treated the  PPM  samples
491       as  intensity-proportional even though they were not, and drew few com‐
492       plaints.  So using -linear as a lie is a  reasonable  thing  to  do  if
493       speed  is important to you.  But if speed is important, you also should
494       consider the -nomix option and pnmscalefixed.
495
496       Another technique to consider is to convert your PNM image to the  lin‐
497       ear  variation  with pnmgamma, run pamscale on it and other transforma‐
498       tions that like linear PNM, and then convert it back to true  PNM  with
499       pnmgamma -ungamma.  pnmgamma is often faster than pamscale in doing the
500       conversion.
501
502       With -nomix, -linear has no effect.  That's because pamscale  does  not
503       concern  itself  with  the meaning of the sample values in this method;
504       pamscale just copies numbers from its input to its output.
505
506
507
508   Precision
509       pamscale uses floating point arithmetic internally.  There is  a  speed
510       cost associated with this.  For some images, you can get the acceptable
511       results (in fact, sometimes identical results)  faster  with  pnmscale‐
512       fixed,  which uses fixed point arithmetic.  pnmscalefixed may, however,
513       distort your image a little.  See the pnmscalefixed user manual  for  a
514       complete discussion of the difference.
515
516

SEE ALSO

518       pnmscalefixed(1),  pamstretch(1),  pamditherbw(1),  pbmreduce(1), pbmp‐
519       scale(1), pamenlarge(1),  pnmsmooth(1),  pamcut(1),  pnmgamma(1),  pnm‐
520       scale(1), pnm(1), pam(1)
521
522

HISTORY

524       pamscale  was new in Netpbm 10.20 (January 2004).  It was adapted from,
525       and obsoleted, pnmscale.  pamscale's primary difference  from  pnmscale
526       is  that it handles the PAM format and uses the "pam" facilities of the
527       Netpbm programming library.  But it also added the resampling class  of
528       scaling  method.  Furthermore, it properly does its pixel mixing arith‐
529       metic (by default) using intensity-proportional values instead  of  the
530       gamma-adjusted  values  the  pnmscale  uses.   To  get the old pnmscale
531       arithmetic, you can specify the -linear option.
532
533       The intensity proportional stuff came out  of  suggestions  by  Adam  M
534       Costello in January 2004.
535
536       The  resampling  algorithms  are  mostly taken from code contributed by
537       Michael Reinelt in December 2003.
538
539       The version of pnmscale from which pamscale was derived, itself evolved
540       out of the original Pbmplus version of pnmscale by Jef Poskanzer (1989,
541       1991).  But none of that original code remains.
542
543
544
545netpbm documentation           18 February 2005        Pamscale User Manual(0)
Impressum