int i;
int j;
int[][] frame;
int frameElement;
IntMatrixToken message = (IntMatrixToken) input.get(0);
frame = message.intMatrix();
// Construct a color distribution histogram for the input image:
// Assuming the color bound for the input 0 and 255. If color detected
// that has color either bigger than 255 OR small than 0, then throw an
// illegal action exception.
for (i = 0; i < 256; i++) {
colorHistogram[i] = 0;
}
int pixels = frame.length * frame[0].length;
for (i = 0; i < frame.length; i++) {
for (j = 0; j < frame[i].length; j++) {
frameElement = frame[i][j];
if ((frameElement < 0) || (frameElement > 255)) {
throw new IllegalActionException("ImageContrast:"
+ "input image pixel contains at" + j + "," + i
+ "with value" + frameElement
+ "that is out of bounds."
+ " Not between 0 and 255.");
}
colorHistogram[frameElement]++;
}
}
//Construct the cdf of the color distribution histogram
//colorHistogram[0] = colorHistogram[0]
for (i = 1; i < 256; i++) {
colorHistogram[i] = colorHistogram[i - 1] + colorHistogram[i];
}
// Search each pixel in the image and re-map it to a new
// color number to make a new relatively even color distribution
// image.
int distributionConstant = pixels / 255;
for (i = 0; i < frame.length; i++) {
for (j = 0; j < frame[i].length; j++) {
frameElement = frame[i][j];
frame[i][j] = colorHistogram[frameElement]
/ distributionConstant;
}
}
message = new IntMatrixToken(frame);
output.send(0, message);
}