Binarizarion of gray image/ArrayIndexOutOfBound error reported
Hi.. …. all
When creating binary image of a gray image by dithering by Floyd –steinberg error diffusion method which is depicted in the function “processImage(BufferedImage inputImage)” an error is reported like: Exception in thread “main”java.lang.ArrayIndexOutOfBound………[/B]
Program is compiled by Jcreator..............
Or can you suggest me something different ………….method implemented by Java.......
Secondly I want to write in file row-by-row akin to image i.e file cursor will be next line after writing 1st row pixels values of image
Code:
Function: which create binary image according to dithering by Floyd –steinberg error diffusion method
public static BufferedImage processImage(BufferedImage inputImage) {
// Create a binary image for the results of processing
int w = inputImage.getWidth()-1;
int h = inputImage.getHeight()-1;
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;
int 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
value = input.getSample(x+1, y, 0);
input.setSample(x+1, y, 0, clamp(value + 0.4375f * error));
value = input.getSample(x-1, y+1, 0);
input.setSample(x-1, 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 0-255 integer range
public static int clamp(float value) {
return Math.min(Math.max(Math.round(value), 0), 255);
}
Main Program Code:
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class BufferConversion {
private JScrollPane getContent(BufferedImage image) {
BufferedImage img=processImage(image);
int[][] data = extractData(img);
String path = "bufferConversion.txt";
writeToFile(data, path);
JLabel label = new JLabel(new ImageIcon(image), JLabel.CENTER);
return new JScrollPane(label);
}
public static BufferedImage processImage(BufferedImage inputImage) {
// Create a binary image for the results of processing
int w = inputImage.getWidth()-1;
int h = inputImage.getHeight()-1;
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;
int 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
value = input.getSample(x+1, y, 0);
input.setSample(x+1, y, 0, clamp(value + 0.4375f * error));
value = input.getSample(x-1, y+1, 0);
input.setSample(x-1, 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 0-255 integer range
public static int clamp(float value) {
return Math.min(Math.max(Math.round(value), 0), 255);
}
private int[][] extractData(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();
System.out.printf("w = %d h = %d%n", w, h);
WritableRaster raster = image.getRaster();
int[][] data = new int[h][w];
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
data[y][x] = raster.getSample(x, y, 0);
}
}
return data;
}
private void writeToFile(int[][] data, String path) {
int h = data.length;
int w = data[0].length;
int[] array = new int[h*w];
for(int y = 0; y < h; y++) {
for(int x = 0; x < w; x++) {
int index = y*w + x;
array[index] = data[y][x];
}
}
try {
File file = new File(path);
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(file)));
for(int i = 0; i < array.length; i++) {
String s = String.valueOf(array[i]) + " ";
bw.write(s, 0, s.length());
}
bw.close();
} catch(IOException e) {
System.out.println("write error: " + e.getMessage());
}
}
public static void main(String[] args) throws IOException {
String path = "C:/shobuj.jpg";
BufferedImage image = ImageIO.read(new File(path));
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new BufferConversion().getContent(image));
f.setSize(500,400);
f.setLocation(200,200);
f.setVisible(true);
}
}