{
    "mode": "man",
    "parameter": "pamarith",
    "section": "1",
    "url": "https://www.chedong.com/phpMan.php/man/pamarith/1/json",
    "generated": "2026-08-06T12:37:45Z",
    "synopsis": "pamarith -add | -subtract | -multiply | -divide | -difference | -minimum | -maximum | -mean |\n-equal  |  -compare  |  -and  | -or | -nand | -nor | -xor | -shiftleft | -shiftright [-close‐\nness=N] pamfile1 pamfile2 ...\nAll options can be abbreviated to their shortest unique prefix.  You may use two hyphens  in‐\nstead  of  one.  You may separate an option name and its value with white space instead of an\nequals sign.",
    "sections": {
        "NAME": {
            "content": "pamarith - perform arithmetic on two Netpbm images\n\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "pamarith -add | -subtract | -multiply | -divide | -difference | -minimum | -maximum | -mean |\n-equal  |  -compare  |  -and  | -or | -nand | -nor | -xor | -shiftleft | -shiftright [-close‐\nness=N] pamfile1 pamfile2 ...\n\nAll options can be abbreviated to their shortest unique prefix.  You may use two hyphens  in‐\nstead  of  one.  You may separate an option name and its value with white space instead of an\nequals sign.\n\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This program is part of Netpbm(1).\n\npamarith reads two or more PBM, PGM, PPM, or PAM images as input.  It performs the  specified\nbinary  arithmetic  operation on their sample values and produces an output of a format which\nis the more general of the two input formats.  The two input images must be of the same width\nand height.  The arithmetic is performed on each pair of identically located tuples to gener‐\nate the identically located tuple of the output.\n\nFor functions that are commutative and associative, pamarith applies\nthe binary function repetitively on as many input images as you supply.  For\nexample, for -add , the output is the sum of all the inputs.  For\nother functions (e.g. -subtract), the program fails if you supply\nmore than two input images.  (Before Netpbm 10.93 (December 2020), the\nprogram always failed with more than two input images).\n\nFor some other functions, pamarith could theoretically compute a\nmeaningful result for multiple arguments, but it fails nonetheless if you\ngive more than two input images.  -mean and -equal are in that\ncategory.\n\n\nMost of what pamarith does is not meaningful for visual images.  It\nworks toward Netpbm's secondary purpose of just manipulating arbitrary\nmatrices of numbers.\n\n\nFor the purpose of the calculation, it assumes any PBM, PGM, or PPM input image is the equiv‐\nalent PAM image of tuple type BLACKANDWHITE, GRAYSCALE, or RGB, respectively, and if it  pro‐\nduces a PBM, PGM, or PPM output, produces the equivalent of the PAM image which is the result\nof the calculation.\n\nThe  first pamfile argument identifies the \"left\" argument image; the second pamfile argument\nidentifies the \"right\" one.\n\nIf the output is PAM, the tuple type is the same as the tuple type of the left input image.\n\npamarith performs the arithmetic on each pair of identically located tuples in the two  input\nimages.\n\nThe arithmetic operation is in all cases fundamentally a function from two integers to an in‐\nteger  (but  see  below - the functions are defined in ways that you can effectively e.g. add\nreal numbers).  The operation is performed on two tuples as follows.  The  two  input  images\nmust have the same depth, or one of them must have depth one.  pamarith fails if one of these\nis not the case.\n\nIf they have the same depth, pamarith simply carries out the arithmetic one sample at a time.\nI.e.  if  at a particular position the left input image contains the tuple (s1,s2,...,sN) and\nthe right input image contains the tuple (t1,t2,...tN), and the function is f, then the  out‐\nput image contains the tuple (f(s1,t1),f(s2,t2),...,f(sN,tN)).\n\nIf  one of the images has depth 1, the arithmetic is performed between the one sample in that\nimage and each of the samples in the other.  I.e. if at a particular position the left  input\nimage  contains the tuple (s) and the right input image contains the tuple (t1,t2,...tN), and\nthe function is f, then the output image contains the tuple (f(s,t1),f(s,t2),...,f(s,tN)).\n\n\n",
            "subsections": [
                {
                    "name": "PBM Oddness",
                    "content": "If you're familiar with the PBM format, you may find pamarith's operation on PBM images to be\nnonintuitive.  Because in PBM black is represented as 1 and white as 0, you might be  expect‐\ning black minus black to be white.\n\nBut  the  PBM format is irrelevant, because pamarith operates on the numbers found in the PAM\nequivalent (see above).  In a PAM black and white image, black is 0 and white is 1.  So black\nminus black is black.\n\n\n"
                },
                {
                    "name": "Maxval",
                    "content": "The meanings of the samples with respect to the maxval varies according to the  function  you\nselect.\n\nIn PAM images in general, the most usual meaning of a sample (the one that applies when a PAM\nimage represents a visual image), is that it represents a fraction of some maximum.  The max‐\nval  of the image corresponds to some maximum value (in the case of a visual image, it corre‐\nsponds to \"full intensity.\"), and a sample value divided by the maxval gives the fraction.\n\nFor pamarith, this interpretation applies to the regular arithmetic  functions:  -add,  -sub‐\ntract, -multiply, -divide, -difference, -minimum, -maximum, -mean, -equal, and -compare.  For\nthose, you should think of the arguments and result as numbers in the range [0,1).  For exam‐\nple, if the maxval of the left argument image is 100 and the maxval of the right argument im‐\nage  is  200  and the maxval of the output image is 200, and the left sample value in an -add\ncalculation is 50 and the right sample is 60, the actual calculation is  50/100  +  60/200  =\n160/200, and the output sample value is 160.\n\nFor  these  functions,  pamarith makes the output image's maxval the maximum of the two input\nmaxvals, except with -equal  and -compare.  For -equal, the output maxval is always  1.   For"
                },
                {
                    "name": "-compare",
                    "content": ""
                },
                {
                    "name": "-compare",
                    "content": "tween 10.18 and 10.26 (January 2005), it changed to being exactly 2).\n\nIf  the  result  of  a  calculation falls outside the range [0, 1), pamarith clips it -- i.e.\nconsiders it to be zero or 1-.\n\nIn many cases, where both your input maxvals are the same, you can just think of  the  opera‐\ntion  as taking place between the sample values directly, with no consideration of the maxval\nexcept for the clipping.  E.g. an -add of sample value 5 to  sample  value  8  yields  sample\nvalue 13.\n\nBut  with  -multiply,  this  doesn't  work.  Say your two input images have maxval 255, which\nmeans the output image also has maxval 255.  Consider a location in the image where the input\nsample values are 5 and 10.  You might think the multiplicative product of those would  yield\n50 in the output.  But pamarith carries out the arithmetic on the fractions 5/255 and 10/255.\nIt multiplies those together and then rescales to the output maxval, giving a sample value in\nthe output PAM of 50/255 rounded to the nearest integer: 0.\n\nWith  the bit string operations, the maxval has a whole different meaning.  The operations in\nquestion are: -and, -or, -nand, -nor, -xor, and -shiftleft, -shiftright.\n\nWith these, each sample value in one or both input images, and in the  output  image,  repre‐\nsents  a  bit string, not a number.  The maxval tells how wide the bit string is.  The maxval\nmust be a full binary count (a power of two minus one, such as 0xff) and the number  of  ones\nin  it  is  the width of the bit string.  For the dyadic bit string operations (that's every‐\nthing but the shift functions), the maxvals of the input images must be the same and pamarith\nmakes the maxval of the output image the same.\n\nFor the bit shift operations, the output maxval is the same as the left  input  maxval.   The\nright input image (which contains the shift counts) can have any maxval and the maxval is ir‐\nrelevant  to  the interpretation of the samples.  The sample value is the actual shift count.\nBut it's still required that no sample value exceed the maxval.\n\n\n\nNOTE: UNARY FUNCTIONS\npamarith applies only binary functions.  If you want to apply a unary function, e.g. \"halve\",\nto a single image, use pamfunc.\n\n\n"
                }
            ]
        },
        "OPTIONS": {
            "content": "In addition to the options common to all programs based on libnetpbm  (most  notably  -quiet,\nsee \u001b]8;;index.html#commonoptions\u001b\\ Common Options\u001b]8;;\u001b\\ ), pamarith recognizes the following command line options:\n\n",
            "subsections": [
                {
                    "name": "The Function",
                    "content": "These options select the function that pamarith applies.\n\nYou must specify one of these, and cannot specify more than one.\n\n\n\n"
                },
                {
                    "name": "-add",
                    "content": ""
                },
                {
                    "name": "-subtract",
                    "content": "Subtracts a value in the right input image from a value in the left input image.\n\n"
                },
                {
                    "name": "-minimum",
                    "content": "Chooses the smaller value of the two.\n\n"
                },
                {
                    "name": "-maximum",
                    "content": "Chooses the larger value of the two.\n\n"
                },
                {
                    "name": "-difference",
                    "content": "Calculates the absolute value of the difference.\n\n"
                },
                {
                    "name": "-multiply",
                    "content": "Does  an  ordinary  arithmetic multiplication, but tends to produce nonobvious results\nbecause of the way pamarith interprets sample values.  See \u001b]8;;#maxval\u001b\\Maxval\u001b]8;;\u001b\\ .\n\n"
                },
                {
                    "name": "-divide",
                    "content": "Divides a value in the left input image by the value in the right  input  image.   But\nlike  -multiply,  it tends to produce nonobvious results.  Note that pamarith clipping\nbehavior makes this of little use when the left argument (dividend)  is  greater  than\nthe  right argument (divisor) -- the result in that case is always the maxval.  If the\ndivisor is 0, the result is the maxval.\n\n-divide was new in Netpbm 10.30 (October 2005).\n\n"
                },
                {
                    "name": "-equal",
                    "content": "not.  Note that the output maxval is always 1 for -equal.\n\nIf  the  maxvals  of the input images are not identical, pamarith may claim two values\nare not equal when in fact they are, because of the precision with which it  does  the\narithmetic.\n\nYou can make the equality test approximate with the -closeness option.  This gives the\npercentage of maxval by which the samples can differ and still be considered equal.\n\n-equal was new in Netpbm 10.93 (December 2020).\n\n"
                },
                {
                    "name": "-compare",
                    "content": "Produces  the value 0 when the value in the left input image is less than the value in\nthe right input image, 1 when the values are equal, and 2 when  the  left  is  greater\nthan the right.\n\nIf  the  maxvals  of the input images are not identical, pamarith may claim two values\nare not equal when in fact they are, because of the precision with which it  does  the\narithmetic.  However, it will never say A is greater than B if A is less than B.\n\n-compare was new in Netpbm 10.13 (December 2002).\n\n"
                },
                {
                    "name": "-and -nand -or -nor -xor",
                    "content": "These  consider  the input and output images to contain bit strings; they compute bit‐\nwise logic operations.  Note that if the maxval is 1, you can also look  at  these  as\nlogic operations on boolean input values.  See section \u001b]8;;#maxval\u001b\\Maxval\u001b]8;;\u001b\\  for the special meaning\nof maxval with respect to bit string operations such as these.\n\n"
                },
                {
                    "name": "-shiftleft -shiftright",
                    "content": "These  consider  the  left  input image and output image to contain bit strings.  They\ncompute a bit shift operation, with bits falling off the left or right end and  zeroes\nshifting  in, as opposed to bits off one end to the other.  The right input image sam‐\nple value is the number of bit positions to shift.\n\nNote that the maxval (see \u001b]8;;#maxval\u001b\\Maxval\u001b]8;;\u001b\\ ) determines the width of the frame within which  you\nare shifting.\n\n\n\n"
                },
                {
                    "name": "Other",
                    "content": ""
                },
                {
                    "name": "-closeness",
                    "content": "This changes the meaning of -equal.  It is not valid with any other function.  See the\ndescription of -equal.\n\n\n\n"
                }
            ]
        },
        "SEE ALSO": {
            "content": "pamfunc(1), pamsummcol(1), pamsumm(1), pnminvert(1), pambrighten(1), ppmdim(1), pnmconvol(1),\npamdepth(1), pnmpsnr(1), pnm(1), pam(1)\n\n\n",
            "subsections": []
        },
        "HISTORY": {
            "content": "pamarith replaced pnmarith in Netpbm 10.3 (June 2002).\n\nIn Netpbm 10.3 through 10.8, though, pamarith was not backward compatible because it required\nthe  input images to be of the same depth, so you could not multiply a PBM by a PPM as is of‐\nten done for masking.  (It was not intended at the time that pnmarith would be  removed  from\nNetpbm -- the plan was just to rewrite it to use pamarith; it was removed by mistake).\n\nBut  starting with Netpbm 10.9 (September 2002), pamarith allows the images to have different\ndepths as long as one of them has depth 1, and that made  it  backward  compatible  with  pn‐\nmarith.\n\nThe original pnmarith did not have the -mean option.\n\nThe -compare option was added in Netpbm 10.13 (December 2002).\n\nThe bit string operations were added in Netpbm 10.27 (March 2005).\n\nThe -divide option was added in Netpbm 10.30 (October 2005).\n\nThe ability to have more than one input (operand) was added in Netpbm 10.93 (December 2020).\n\nThe -equal option was added in Netpbm 10.93 (December 2020).\n",
            "subsections": []
        },
        "DOCUMENT SOURCE": {
            "content": "This  manual  page  was  generated by the Netpbm tool 'makeman' from HTML source.  The master\ndocumentation is at\n\nhttp://netpbm.sourceforge.net/doc/pamarith.html\n\nnetpbm documentation                       24 October 2020                   Pamarith User Manual(1)",
            "subsections": []
        }
    },
    "summary": "pamarith - perform arithmetic on two Netpbm images",
    "flags": [
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": ""
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "Subtracts a value in the right input image from a value in the left input image."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "Chooses the smaller value of the two."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "Chooses the larger value of the two."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "Calculates the absolute value of the difference."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "Does an ordinary arithmetic multiplication, but tends to produce nonobvious results because of the way pamarith interprets sample values. See \u001b]8;;#maxval\u001b\\Maxval\u001b]8;;\u001b\\ ."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "Divides a value in the left input image by the value in the right input image. But like -multiply, it tends to produce nonobvious results. Note that pamarith clipping behavior makes this of little use when the left argument (dividend) is greater than the right argument (divisor) -- the result in that case is always the maxval. If the divisor is 0, the result is the maxval. -divide was new in Netpbm 10.30 (October 2005)."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "not. Note that the output maxval is always 1 for -equal. If the maxvals of the input images are not identical, pamarith may claim two values are not equal when in fact they are, because of the precision with which it does the arithmetic. You can make the equality test approximate with the -closeness option. This gives the percentage of maxval by which the samples can differ and still be considered equal. -equal was new in Netpbm 10.93 (December 2020)."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "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. If the maxvals of the input images are not identical, pamarith may claim two values are not equal when in fact they are, because of the precision with which it does the arithmetic. However, it will never say A is greater than B if A is less than B. -compare was new in Netpbm 10.13 (December 2002)."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "These consider the input and output images to contain bit strings; they compute bit‐ wise logic operations. Note that if the maxval is 1, you can also look at these as logic operations on boolean input values. See section \u001b]8;;#maxval\u001b\\Maxval\u001b]8;;\u001b\\ for the special meaning of maxval with respect to bit string operations such as these."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "These consider the left input image and output image to contain bit strings. They compute a bit shift operation, with bits falling off the left or right end and zeroes shifting in, as opposed to bits off one end to the other. The right input image sam‐ ple value is the number of bit positions to shift. Note that the maxval (see \u001b]8;;#maxval\u001b\\Maxval\u001b]8;;\u001b\\ ) determines the width of the frame within which you are shifting."
        },
        {
            "flag": "",
            "long": null,
            "arg": null,
            "description": "This changes the meaning of -equal. It is not valid with any other function. See the description of -equal."
        }
    ],
    "examples": [],
    "see_also": [
        {
            "name": "pamfunc",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pamfunc/1/json"
        },
        {
            "name": "pamsummcol",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pamsummcol/1/json"
        },
        {
            "name": "pamsumm",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pamsumm/1/json"
        },
        {
            "name": "pnminvert",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pnminvert/1/json"
        },
        {
            "name": "pambrighten",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pambrighten/1/json"
        },
        {
            "name": "ppmdim",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/ppmdim/1/json"
        },
        {
            "name": "pnmconvol",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pnmconvol/1/json"
        },
        {
            "name": "pamdepth",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pamdepth/1/json"
        },
        {
            "name": "pnmpsnr",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pnmpsnr/1/json"
        },
        {
            "name": "pnm",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pnm/1/json"
        },
        {
            "name": "pam",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/pam/1/json"
        }
    ],
    "tldr": {
        "source": "official",
        "description": "Apply a binary function on two Netpbm images.",
        "examples": [
            {
                "description": "Apply the specified binary function pixel-wise on the two specified images (which must be of the same size)",
                "command": "pamarith -{{add|subtract|multiply|divide|difference|minimum|maximum|...}} {{path/to/image1.pam|pbm|pgm|ppm}} {{path/to/image2.pam|pbm|pgm|ppm}}"
            }
        ]
    }
}