LinuxCommandLibrary

pamarith

Perform arithmetic operations in parallel

TLDR

Apply the specified binary function pixel-wise on the two specified images (which must be of the same size)

$ pamarith -[add|subtract|multiply|divide|difference|minimum|maximum|...] [path/to/image1.pam|pbm|pgm|ppm] [path/to/image2.pam|pbm|pgm|ppm]
copy

SYNOPSIS

pamarith op file1.pnm [file2.pnm] [-swap]

PARAMETERS

op
    Required operation: add, sub, mul, div, min, max, diff, and, or, xor

file1.pnm
    First (left) input PNM file, required

file2.pnm
    Second (right) input PNM file; if omitted, duplicates file1

-swap
    Swap file1 and file2 operands

DESCRIPTION

Pamarith is a Netpbm utility for performing arithmetic and logical operations on PNM images (PBM, PGM, PPM, PAM) pixel-by-pixel. It applies operations independently to each sample (R, G, B, etc.) in the images.

Supported operations include add, sub (subtract), mul (multiply), div (divide), min, max, diff (absolute difference), and, or, xor. Inputs must match in width, height, maxval, and tuple type. If the second file is omitted, the first is duplicated.

Arithmetic uses integer math: addition/multiplication clamp results to [0, maxval]; division truncates toward zero; bitwise ops treat samples as bits. The -swap flag interchanges operands.

Output is a PNM file to stdout, preserving input format. Ideal for image math like blending, differencing, or masking in pipelines. For example, darken an image with pamarith mul img.ppm 0.5.pgm (pre-scale 0.5 to integer maxval/2).

Limitations include no floating-point support and clamping on overflow/underflow.

CAVEATS

Inputs must match exactly in dimensions, maxval, and format; mismatch causes error. Division by zero yields zero. Overflow/underflow clamped to [0,maxval]. Bitwise ops best for binary (PBM) images. No support for floating-point or tuple-valued samples.

EXAMPLES

pamarith add img1.ppm img2.ppm > sum.ppm
pamarith sub orig.pgm modified.pgm -swap > diff.pgm
pamarith mul img.ppm 128 > half.ppm (assumes maxval=255)

OVERFLOW HANDLING

Add/mul: result = min(maxval, a + b) or similar clamping. Use pamfunc for advanced math.

HISTORY

Developed by Anselm Lingnau in 1992; integrated into Netpbm suite (successor to PBMPLUS). Generalized earlier format-specific tools like pgmsub(1). Current in Netpbm 11.x, with minor tweaks for PAM support.

SEE ALSO

pnmarith(1), pnm(1), pam(5), pgmarith(1), ppmmul(1)

Copied to clipboard