When implementing arithmetic operations on pixels, we must keep in mind
that the computed results may exceed the maximum range of pixel values for
a given image type ([0 . . . 255] in the case of 8-bit grayscale images). To avoid
this, we have included the “clamping” statement
if (a > 255) a = 255;
in line 9 of Prog. 4.1, which limits any result to the maximum value 255.
Similarly one should, in general, also limit the results to the minimum value
(0) to avoid negative pixel values (which cannot be represented by this type of
8-bit image), for example by the statement
if (a < 0) a = 0;
This second measure is not necessary in Prog. 4.1 because the intermediate
results can never be negative in this particular operation.