I am trying to create a (average) greyscale of an image, and I do not seem to get the values for each pixel. I think I am not doing the right calculations when summarizing the average, but I can not find what is wrong.
public byte[] changePixelValues(byte blue, byte green, byte red) {
//Convert to greyscale by average
byte avg;
int sum = 0;
sum +=blue;
sum += green;
sum += red;
avg = (byte) (sum / 3);
//System.out.println("avg: " + avg);
byte[] values = new byte[3];
values[0] = avg;
values[1] = avg;
values[2] = avg;
return values;
Is there something that I missed in the type conversion?
EDIT:
The input is the following where byte[] data is the raw data from a BufferedImage, which in this case does not have an alphaRaster.
for (int i = 0; i < data.length; i += 3) {
byte [] newData = changer.changePixelValues(data[i], data[i+1], data[i+2]);
System.arraycopy(newData,0,data,i,3);
}