Uli Kunkel wrote:
>
> How can I convert an image to black and white?
> Do I have to manually calculate each pixel or is there some class that
> has this functionality?
>
> Thanks for any suggestions.
I have copied somebody's method and modified it.
It uses a dithering algorithm so it looks good.
The problem now is that I want to save a bmp image with a specified dpi
setting in the header.
I found that this can be done for jpeg with JPEGEncodeParam but nothing
for bmp.
So does anybody know hot to change the dpi when writing a bmp image?
Belove is the code for bw dithering:

public static BufferedImage processImage(BufferedImage inputImage) {
// Create a binary image for the results of processing
int w = inputImage.getWidth();
int h = inputImage.getHeight();
BufferedImage outputImage = new BufferedImage(w, h,
BufferedImage.TYPE_BYTE_BINARY);
// Work on a copy of input image because it is modified by diffusion
WritableRaster input = inputImage.copyData(null);
WritableRaster output = outputImage.getRaster();
final int threshold = 128;
float value, error;
for (int y = 0; y < h; ++y)
for (int x = 0; x < w; ++x) {
value = input.getSample(x, y, 0);
// Threshold value and compute error
if (value < threshold) {
output.setSample(x, y, 0, 0);
error = value;
}
else {
output.setSample(x, y, 0, 1);
error = value  255;
}
// Spread error amongst neighbouring pixels
if((x > 0) && (y > 0) && (x < (w1)) && (y < (h1)))
{
value = input.getSample(x+1, y, 0);
input.setSample(x+1, y, 0, clamp(value + 0.4375f * error));
value = input.getSample(x1, y+1, 0);
input.setSample(x1, y+1, 0, clamp(value + 0.1875f * error));
value = input.getSample(x, y+1, 0);
input.setSample(x, y+1, 0, clamp(value + 0.3125f * error));
value = input.getSample(x+1, y+1, 0);
input.setSample(x+1, y+1, 0, clamp(value + 0.0625f * error));
}
}
return outputImage;
}
// Forces a value to a 0255 integer range
public static int clamp(float value) {
return Math.min(Math.max(Math.round(value), 0), 255);
}
