Vips – 8.0: Operator index > By section > Arithmetic
Description: Reference for Vips-8.0: Operator index > By section > Arithmetic

#### Operator index > By section > Arithmetic [src]

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

- `vips_add()`
- `vips_sum()`
- `vips_subtract()`
- `vips_multiply()`
- `vips_divide()`
- `vips_linear()`
- `vips_linear1()`
- `vips_remainder()`
- `vips_remainder_const()`
- `vips_remainder_const1()`
- `vips_invert()`
- `vips_abs()`
- `vips_sign()`
- `vips_clamp()`
- `vips_maxpair()`
- `vips_minpair()`
- `vips_round()`
- `vips_floor()`
- `vips_ceil()`
- `vips_rint()`
- `vips_math()`
- `vips_sin()`
- `vips_cos()`
- `vips_tan()`
- `vips_asin()`
- `vips_acos()`
- `vips_atan()`
- `vips_exp()`
- `vips_exp10()`
- `vips_log()`
- `vips_log10()`
- `vips_sinh()`
- `vips_cosh()`
- `vips_tanh()`
- `vips_asinh()`
- `vips_acosh()`
- `vips_atanh()`
- `vips_complex()`
- `vips_polar()`
- `vips_rect()`
- `vips_conj()`
- `vips_complex2()`
- `vips_cross_phase()`
- `vips_complexget()`
- `vips_real()`
- `vips_imag()`
- `vips_complexform()`
- `vips_relational()`
- `vips_equal()`
- `vips_notequal()`
- `vips_less()`
- `vips_lesseq()`
- `vips_more()`
- `vips_moreeq()`
- `vips_relational_const()`
- `vips_equal_const()`
- `vips_notequal_const()`
- `vips_less_const()`
- `vips_lesseq_const()`
- `vips_more_const()`
- `vips_moreeq_const()`
- `vips_relational_const1()`
- `vips_equal_const1()`
- `vips_notequal_const1()`
- `vips_less_const1()`
- `vips_lesseq_const1()`
- `vips_more_const1()`
- `vips_moreeq_const1()`
- `vips_boolean()`
- `vips_andimage()`
- `vips_orimage()`
- `vips_eorimage()`
- `vips_lshift()`
- `vips_rshift()`
- `vips_boolean_const()`
- `vips_andimage_const()`
- `vips_orimage_const()`
- `vips_eorimage_const()`
- `vips_lshift_const()`
- `vips_rshift_const()`
- `vips_boolean_const1()`
- `vips_andimage_const1()`
- `vips_orimage_const1()`
- `vips_eorimage_const1()`
- `vips_lshift_const1()`
- `vips_rshift_const1()`
- `vips_math2()`
- `vips_pow()`
- `vips_wop()`
- `vips_atan2()`
- `vips_math2_const()`
- `vips_pow_const()`
- `vips_wop_const()`
- `vips_atan2_const()`
- `vips_math2_const1()`
- `vips_pow_const1()`
- `vips_wop_const1()`
- `vips_atan2_const1()`
- `vips_avg()`
- `vips_deviate()`
- `vips_min()`
- `vips_max()`
- `vips_stats()`
- `vips_measure()`
- `vips_find_trim()`
- `vips_getpoint()`
- `vips_hist_find()`
- `vips_hist_find_ndim()`
- `vips_hist_find_indexed()`
- `vips_hough_line()`
- `vips_hough_circle()`
- `vips_project()`
- `vips_profile()`

## Enumerations

- `VipsOperationMath`
- `VipsOperationMath2`
- `VipsOperationRound`
- `VipsOperationRelational`
- `VipsOperationBoolean`
- `VipsOperationComplex`
- `VipsOperationComplex2`
- `VipsOperationComplexget`