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