Results 1 to 6 of 6
  1. #1
    Mazharul is offline Member
    Join Date
    Aug 2008
    Posts
    12
    Rep Power
    0

    Default 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

    Java 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);
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    an error is reported like ...
    If you don't post the full text of the message, how is anyone to know what it is? Need FULL TEXT OF MESSAGE! ITS VERY IMPORTANT!

  3. #3
    Mazharul is offline Member
    Join Date
    Aug 2008
    Posts
    12
    Rep Power
    0

    Default

    Sir
    error reported:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Coordinate out of bounds!
    at sun.awt.image.ByteInterleavedRaster.getSample(Unkn own Source)
    at BufferConversion.processImage(BufferConversion.jav a:55)
    at BufferConversion.getContent(BufferConversion.java: 10)
    at BufferConversion.main(BufferConversion.java:125)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    There are many getSample() calls in your program. Which one is at line 55?
    Did You left off the index value from the error message?
    What are the values of the first 2 args in the call to getSample() at line 55?

  5. #5
    Mazharul is offline Member
    Join Date
    Aug 2008
    Posts
    12
    Rep Power
    0

    Default

    // Spread error amongst neighbouring pixels

    value = input.getSample(x+1, y, 0);
    input.setSample(x+1, y, 0, clamp(value + 0.4375f * error));
    //Error is here
    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));

    }
    sir...
    values of the first 2 args in the call to getSample() at line 55 is pixel values which is situated at west-south conrner of (x,y) co-ordinate.

    sir why error is not reported due to code fragment which is above of previous segment
    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) {
    //it is not reporting error
    value = input.getSample(x, y, 0);

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    Add some println()s to show the values of x and y and also the values of h and w.
    What was the index value when it was out of bounds? Was it in the errror message?

Similar Threads

  1. Add an image to JFrame
    By Eranga in forum AWT / Swing
    Replies: 4
    Last Post: 02-01-2010, 03:09 PM
  2. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 04:28 PM
  3. Getting warnings reported by Connection object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:57 AM
  4. Loading An Image Help Please!
    By shaungoater in forum Java 2D
    Replies: 2
    Last Post: 01-09-2008, 08:14 AM
  5. Replies: 0
    Last Post: 01-08-2008, 05:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •