Pixel arithmetic [src]

Pixel arithmetic

These operations perform pixel arithmetic, that is, they perform an arithmetic operation, such as addition, on every pixel in an image or a pair of images. All (except in a few cases noted below) will work with images of any type or any mixture of types, of any size and of any number of bands.

For binary operations, if the number of bands differs, one of the images must have one band. In this case, an n-band image is formed from the one-band image by joining n copies of the one-band image together, and then the two n-band images are operated upon.

In the same way, for operations that take an array constant, such as vips_remainder_const(), you can mix single-element arrays or single-band images freely.

Arithmetic operations try to preserve precision by increasing the number of bits in the output image when necessary. Generally, this follows the ANSI C conventions for type promotion, so multiplying two VIPS_FORMAT_UCHAR images together, for example, produces a VIPS_FORMAT_USHORT image, and taking the vips_cos() of a VIPS_FORMAT_USHORT image produces VIPS_FORMAT_FLOAT image.

After processing, use vips_cast() and friends to take then format back down again.vips_cast_uchar(), for example, will cast any image down to 8-bit unsigned.

Images have an interpretation: a meaning for the pixel values. With VIPS_INTERPRETATION_sRGB, for example, the first three bands will be interpreted (for example, by a saver like vips_jpegsave()) as R, G and B, with values in 0 - 255, and any fourth band will be interpreted as an alpha channel.

After arithmetic, you may wish to change the interpretation (for example to save as 16-bit PNG). Use vips_copy() to change the interpretation without changing pixels.

For binary arithmetic operations, type promotion occurs in two stages. First, the two input images are cast up to the smallest common format, that is, the type with the smallest range that can represent the full range of both inputs. This conversion can be represented as a table:

Smallest common format

in2/in1 uchar char ushort short uint int float double complex double complex
uchar ushort short ushort short uint int float double complex double complex
char short short short short int int float double complex double complex
ushort ushort short ushort short uint int float double complex double complex
short short short short short int int float double complex double complex
uint uint int uint int uint int float double complex double complex
int int int int int int int float double complex double complex
float float float float float float float float double complex double complex
double double double double double double double double double double complex double complex
complex complex complex complex complex complex complex complex double complex complex double complex
double complex double complex double complex double complex double complex double complex double complex double complex double complex double complex double complex

In the second stage, the operation is performed between the two identical types to form the output. The details vary between operations, but generally the principle is that the output type should be large enough to represent the whole range of possible values, except that int never becomes float.

Functions

Enumerations