bwmod — arithmetically modify pixel values in black and white bw file


bwmod [-c ] [ -a add -s sub -m mult -d div -A -e exp -r root -S shift -M and -O or -X xor -R -t ] >


bwmod will perform a series of arithmetic operations on each 8-bit pixel of its input/output stream. This is a bw(5) file format. The pixel values range from 0 to 255 and are internally operated on in floating point form (unless the -c option has been specified) so that dynamic range, truncation, and sign are not a problem. On output they are converted back to 8-bit unsigned values, with clipping to 0 and 255 for values less than 0 and greater than 255. If any pixels are clipped, the numbers of pixels clipped, low and high, are reported to standard error on completion.

If the -c option is specified, then intermediate results are stored as signed characters, though the command line arguments remain in floating point format. This is useful when one wishes to take advantage of wrap-around characteristics of binary mathematics.

Any number of operations can be given and they are applied in order. The choices are: -a val to add a value. -s val to subtract a value. -m val to multiply by a value. -d val to divide by a value. -A to take absolute value. -e val to exponentiate, i.e. raise to the power val. -r val to take the valth root. -S val to shift by val bits. If val is positive, this uses C-language "^<<" operator, doing left-shift; if val is negative, this uses C-language ">>" operator, doing right-shift by -val bits. This is a bitwise operation, and if it is about to be used, both the pixel value and val are truncated to integer. Unless -c is in effect, the resulting new value will again be placed in floating-point. (So it's "user beware" if this option is applied to a non-integer value.) -M val to do "and" operation with val. See -S for "user beware" about bitwise operation. -O val to do (inclusive) "or" operation with val. See -S for "user beware" about bitwise operation. -X val to do "exclusive or" operation with val. See -S for "user beware" about bitwise operation. -R to do rounding to the nearest integer. -t to do truncation to integer; notice the automatic use of truncation for the bitwise options (-S, -M, -O, and -X).

All arguments can be floating point values. Note that this command can be applied to color pix(5) files also, but no distinction can be made between color planes.


The command bwmod -m-1 -a255 < >

will produce the negative of a file by first multiplying by minus one, and then adding 255 to shift the pixels back into the 0->255 range. (I.e., replace each pixel value by 255 minus that value.)

One can apply a gamma factor to a file as follows: bwmod -d255 -r2.2 -m255 < >

which will first normalize the pixel values (i.e., put them into 0->1, noting the internal use of floating point), then take the 2.2 root of each value, and finally scale the pixel values back to 0->255. [Note however that applying gammas this way is not recommended since a function call per pixel results!]

Suppose you ran bwstat on a file and found min=80, max=210, mean=100. If you wish to expand the range of intensities, while still preserving the mean, this can be done by: bwmod -s100 -m1.409 -a100 < >

where 1.409 results from (255.0-100.0)/(210.0-100.0), which is what one would use to boost the max value (210) to full scale (255) after subtracting the mean (100). Subtracting and adding 100 makes the multiply occur "about that point"; i.e., values less than 100 get smaller, and those above get higher. A for this example can be created with these three command lines:

gencolor -r 11 80 > gencolor -r 262131 100 >> gencolor -r 2 210 >>


brlcad(1), pix(5), bw(5)




This software is Copyright (c) 1989-2016 by the United States Government as represented by U.S. Army Research Laboratory.


Reports of bugs or problems should be submitted via electronic mail to <>.