Generic functions for matrix manipulation, image processing, etc.
function Y = invert(X)
% INVERT Inverts an image.
% Y = INVERT(X) inverts the colors of the image X.
% If X is class uint8, INVERT is the same as Y = 255 - X.
% If X is double and the elements of X are inside [0 1],
% INVERT is the same as Y = 1 - X.
% Otherwise, INVERT is the same as Y = max(X) - X + min(X).
Y = uint8( 255 - double(X));
M = max(X(:));
m = min(X(:));
if M >= 1 & m <= 0
Y = 1 - X;
Y = M - X + m;