PAMARITH(1)

πŸ’– PAMARITH β€” the Pamarith User Manual, General Commands Manual edition ✨

okay bestie, sit down, hydrate, we're doing arithmetic on images and it's gonna be a whole vibe πŸ¦‹


🌸 NAME 🌸

pamarith β€” do math on two Netpbm images. yes. literally. two images walk into a bar and the bartender does arithmetic on their pixels πŸ’…


✨ SYNOPSIS ✨

pamarith -add | -subtract | -multiply | -divide | -difference | -minimum
| -maximum | -mean | -equal | -compare | -and | -or | -nand | -nor |
-xor | -shiftleft | -shiftright [-closeness=N] pamfile1 pamfile2 ...

okay so house rules, and they're giving generous:

  • every option can be abbreviated to its shortest unique prefix β€” yes, really, you can be lazy, it's fine 🫢
  • you may use two hyphens instead of one. slay.
  • you may separate an option name from its value with white space instead of an equals sign. chef's kiss πŸ’–

πŸ‘‘ DESCRIPTION πŸ‘‘

so this program? it's part of Netpbm(1). family reunion energy 🌈

pamarith reads two or more PBM, PGM, PPM, or PAM images as input. it runs the binary arithmetic operation you picked on their sample values, and produces output in whichever of the two input formats is the more general one. the two input images must be the same width and height β€” no exceptions, no negotiating. the math happens on each pair of identically located tuples, and that generates the identically located tuple in the output. periodt.

for functions that are commutative and associative, pamarith just keeps applying the binary function over and over on however many input images you hand it. for example, with -add, the output is the sum of all the inputs. for other functions (hi, -subtract πŸ’€), the program fails if you give it more than two input images.

fun lore drop: before Netpbm 10.93 (December 2020), it always failed with more than two input images. glow-up moment ✨

for some other functions, pamarith could theoretically compute something meaningful with multiple arguments, but it fails anyway if you give it more than two input images. -mean and -equal live in that category. can't sit with us, sorry πŸŽ€

ngl, most of what pamarith does is not meaningful for visual images. it's out here serving Netpbm's secondary purpose: just manipulating arbitrary matrices of numbers. main character of linear algebra, honestly.

for the purpose of the calculation, it assumes any PBM, PGM, or PPM input image is the equivalent PAM image of tuple type BLACKANDWHITE, GRAYSCALE, or RGB, respectively. and if it produces a PBM, PGM, or PPM output, it produces the equivalent of the PAM image that is the result of the calculation. it's giving format shapeshifter.

the first pamfile argument identifies the "left" argument image. the second pamfile argument identifies the "right" one. left, right. like a duet 🫢

if the output is PAM, the tuple type is the same as the tuple type of the left input image. left image stays winning πŸ‘‘

pamarith performs the arithmetic on each pair of identically located tuples in the two input images.

the arithmetic operation is, in all cases, fundamentally a function from two integers to an integer β€” but see below, because the functions are defined in ways that let you effectively, like, add real numbers. the operation is performed on two tuples like this:

the two input images must have the same depth, or one of them must have depth one. if neither is true, pamarith fails. that's the vibe check and it does not pass. πŸ’€

if they have the same depth, pamarith simply carries out the arithmetic one sample at a time. i.e. if at a particular position the left input image contains the tuple (s1,s2,...,sN) and the right input image contains the tuple (t1,t2,...tN), and the function is f, then the output image contains the tuple:

(f(s1,t1),f(s2,t2),...,f(sN,tN))

if one of the images has depth 1, the arithmetic is performed between the one sample in that image and each of the samples in the other. i.e. if at a particular position the left input image contains the tuple (s) and the right input image contains the tuple (t1,t2,...tN), and the function is f, then the output image contains the tuple:

(f(s,t1),f(s,t2),...,f(s,tN))

πŸ–€ PBM Oddness πŸ–€

if you're familiar with the PBM format, you may find pamarith's operation on PBM images to be nonintuitive. because in PBM, black is represented as 1 and white as 0, you might be expecting black minus black to be white. bestie... no.

but the PBM format is irrelevant here, because pamarith operates on the numbers found in the PAM equivalent (see above). in a PAM black and white image, black is 0 and white is 1. so black minus black is black. it's giving anticlimactic but correct πŸŽ€


πŸ”₯ Maxval πŸ”₯

the meanings of the samples with respect to the maxval vary according to the function you select. iykyk.

in PAM images in general, the most usual meaning of a sample (the one that applies when a PAM image represents a visual image) is that it represents a fraction of some maximum. the maxval of the image corresponds to some maximum value (in the case of a visual image, it corresponds to "full intensity."), and a sample value divided by the maxval gives the fraction. math is mathing πŸ’…

for pamarith, this interpretation applies to the regular arithmetic functions: -add, -subtract, -multiply, -divide, -difference, -minimum, -maximum, -mean, -equal, and -compare. for those, you should think of the arguments and result as numbers in the range [0,1).

example time, because I love an example πŸ“ β€” if the maxval of the left argument image is 100 and the maxval of the right argument image is 200 and the maxval of the output image is 200, and the left sample value in an -add calculation is 50 and the right sample is 60, the actual calculation is:

50/100 + 60/200 = 160/200

and the output sample value is 160.

for these functions, pamarith makes the output image's maxval the maximum of the two input maxvals β€” except with -equal and -compare. for -equal, the output maxval is always 1. for -compare, it is always 2.

history lesson, no cap: before Netpbm 10.14 (February 2003), there was no exception for -compare. in 10.14, the exception was just that the maxval was at least 2, and sometime between 10.18 and 10.26 (January 2005), it changed to being exactly 2. character development ✨

if the result of a calculation falls outside the range [0, 1), pamarith clips it β€” i.e. considers it to be zero or 1-. it's not mad, it's just setting boundaries πŸ™ˆ

in many cases, where both your input maxvals are the same, you can just think of the operation as taking place between the sample values directly, with no consideration of the maxval except for the clipping. e.g. an -add of sample value 5 to sample value 8 yields sample value 13. easy. iconic.

but with -multiply, this doesn't work. 😭 say your two input images have maxval 255, which means the output image also has maxval 255. consider a location in the image where the input sample values are 5 and 10. you might think the multiplicative product of those would yield 50 in the output. but pamarith carries out the arithmetic on the fractions 5/255 and 10/255. it multiplies those together and then rescales to the output maxval, giving a sample value in the output PAM of 50/255 rounded to the nearest integer: 0. not me getting humbled by -multiply πŸ’€

with the bit string operations, the maxval has a whole different meaning. the operations in question are: -and, -or, -nand, -nor, -xor, and -shiftleft, -shiftright.

with these, each sample value in one or both input images, and in the output image, represents a bit string, not a number. the maxval tells you how wide the bit string is. the maxval must be a full binary count (a power of two minus one, such as 0xff) and the number of ones in it is the width of the bit string. for the dyadic bit string operations (that's everything but the shift functions), the maxvals of the input images must be the same and pamarith makes the maxval of the output image the same. matchy matchy πŸŽ€

for the bit shift operations, the output maxval is the same as the left input maxval. the right input image (which contains the shift counts) can have any maxval and the maxval is irrelevant to the interpretation of the samples. the sample value is the actual shift count. but it's still required that no sample value exceed the maxval. rules are rules 🫢


⭐ NOTE: UNARY FUNCTIONS ⭐

pamarith applies only binary functions. if you want to apply a unary function, e.g. "halve", to a single image, use pamfunc. different tool, different girlie. go say hi πŸ’–


✨ OPTIONS ✨

Okay so first things first, bestie πŸ’– β€” pamarith inherits all the options that every program built on libnetpbm gets. The most famous one is -quiet, and there's a whole chunk of the docs called Common Options that covers the whole squad. But then, on top of all that, pamarith has its OWN little roster, and that's what we're going over now. 🫢


πŸŽ€ The Function

These are the options that pick the function pamarith actually applies to your pixels. Math time, slay. πŸ”₯

Real talk: you HAVE to specify exactly one of these. Not zero. Not two. One. Specify more than one and it's not gonna go how you want, periodt.
  • -add β€” adds the two values together. And if the result is bigger than maxval? It gets clipped. Sorry babe, that's the ceiling. πŸ’…
  • -subtract β€” takes a value in the right input image and subtracts it from a value in the left input image.
  • -minimum β€” picks the smaller value of the two. The humble one. πŸŽ€
  • -maximum β€” picks the larger value of the two. Main character energy, obviously. πŸ‘‘
  • -difference β€” works out the absolute value of the difference.
  • -multiply β€” does a normal, ordinary arithmetic multiplication... but it tends to give you results that are kinda nonobvious, and that's because of how pamarith interprets sample values. Go read Maxval if you wanna know why. πŸ”
  • -divide β€” divides a value in the left input image by the value in the right input image. But like -multiply, it tends to hand you nonobvious results. And here's the tea: because of the way pamarith clips, this is of little use when the left argument (the dividend) is greater than the right argument (the divisor) β€” in that case the result is ALWAYS the maxval. And if the divisor is 0? Result is the maxval. Every time. 😭

-divide was new in Netpbm 10.30 (October 2005).

  • -equal β€” produces maxval when the values in the two images are equal, and zero when they're not. Note that the output maxval is always 1 for -equal. ⭐

Now, if the maxvals of the input images aren't identical, pamarith may claim two values aren't equal when honestly they are β€” that's just the precision it does its arithmetic with, no cap.

You can make the equality test approximate though, with the -closeness option. That one gives you the percentage of maxval by which the samples are allowed to differ and still count as equal. 🫢

-equal was new in Netpbm 10.93 (December 2020).

  • -compare β€” produces the value 0 when the value in the left input image is less than the value in the right input image, 1 when the values are equal, and 2 when the left is greater than the right.

Same deal as before: if the maxvals of the input images aren't identical, pamarith may claim two values aren't equal when in fact they are, because of the precision of its arithmetic. BUT β€” and this is the iconic part β€” it will never say A is greater than B if A is less than B. Never. πŸ’―

-compare was new in Netpbm 10.13 (December 2002).

  • -and, -nand, -or, -nor, -xor β€” these ones look at your input and output images as containing bit strings, and they compute bitwise logic operations on them. Fun fact: if the maxval is 1, you can also read these as logic operations on boolean input values. 🧠 And go check the section Maxval for the special meaning maxval has when it comes to bit string operations like these.
  • -shiftleft, -shiftright β€” these treat the left input image and the output image as containing bit strings. They compute a bit shift operation, where bits fall off the left or right end and zeroes shift in β€” as opposed to bits going off one end and coming back around at the other. The right input image sample value is the number of bit positions to shift by. ✨

And just so you know: the maxval (see Maxval) is what determines the width of the frame you're shifting inside of.


🌸 Other

  • -closeness β€” this one changes the meaning of -equal. It is not valid with any other function, so don't try it, bestie. See the description of -equal above. πŸŽ€

πŸ’– SEE ALSO πŸ’–

pamfunc(1), pamsummcol(1), pamsumm(1), pnminvert(1), pambrighten(1), ppmdim(1), pnmconvol(1), pamdepth(1), pnmpsnr(1), pnm(1), pam(1)


πŸ¦‹ HISTORY πŸ¦‹

pamarith replaced pnmarith back in Netpbm 10.3 (June 2002). Glow up. ✨

But plot twist β€” through Netpbm 10.3 to 10.8, pamarith was not backward compatible, because it required the input images to be of the same depth. Which meant you couldn't multiply a PBM by a PPM the way people often do for masking. 😭 And here's the mess: it was not intended at the time that pnmarith would be removed from Netpbm β€” the plan was just to rewrite it so it'd use pamarith, and it got removed by mistake. Oops.

BUT β€” starting with Netpbm 10.9 (September 2002), pamarith allows the images to have different depths, as long as one of them has depth 1. And that made it backward compatible with pnmarith again. We love a redemption arc. 🫢

The original pnmarith did not have the -mean option.

The -compare option was added in Netpbm 10.13 (December 2002).

The bit string operations were added in Netpbm 10.27 (March 2005).

The -divide option was added in Netpbm 10.30 (October 2005).

The ability to have more than one input (operand) was added in Netpbm 10.93 (December 2020).

The -equal option was added in Netpbm 10.93 (December 2020).


🌈 DOCUMENT SOURCE 🌈

This manual page was generated by the Netpbm tool makeman from HTML source. The master documentation lives at:

http://netpbm.sourceforge.net/doc/pamarith.html

And that's the whole page, bestie!! πŸŽ€ This came from the netpbm documentation, it's the Pamarith User Manual(1), dated 24 October 2020. Go forth and do iconic arithmetic. πŸ’…πŸ’–

πŸ“¦ where this page comes from