Results 1 to 3 of 3
  1. #1
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Converting a buffered image into an int[] array, back [unsolved]

    Long story short, I want to get the int[] array of RGB values from a BufferedImage
    The original BufferedImage is called startImage, for which the RGB values go into startPixels

    Next I have another int array called endPixels, and an image called endImage. When the program is done, endPixels will be dithered from startPixels but for the time being they are identical.

    Then I want endPixels to be converted to endImage, which is then written to a file.

    Here is the code:

    Java Code:
    import java.awt.image.BufferedImage;
    import java.awt.image.WritableRaster;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    public class DitherMain {
    
    	static String imageString = "/IMG_3462.jpg";
    	
    	BufferedImage startImage;
    	int[] startPixels;
    	BufferedImage endImage;
    	int[] endPixels;
    	int width, height;
    	
    	public static void main(String[] args){
    		new DitherMain(loadImage(imageString));
    	}
    	
    	//transforms the image and writes the new one into the project
    	DitherMain(BufferedImage img){
    		startImage = img;
    		width = img.getWidth();
    		height = img.getHeight();
    		startPixels = new int[width*height];
    		img.getRGB(0, 0, width, height, startPixels, 0, width);
    
    		//System.out.println(startPixels[0]);
    		transformPixels();
    		
    		endImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    		WritableRaster raster = (WritableRaster) endImage.getData();
    		raster.setPixels(0,0,width,height,endPixels);
    		
    		File file = new File("/RESULT.png");
    		try {
    			ImageIO.write(startImage, "jpg", file);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	
    	void transformPixels(){
    		//endPixels will one day be different from startPixels, but for now it is idenitcial
    		endPixels = startPixels;
    	}
    	
    	public static BufferedImage loadImage(String fileName){	
    		BufferedImage img;
    		
    		try{
    			img = ImageIO.read(DitherMain.class.getResource(fileName));
    		} catch (Exception e) {
    			e.printStackTrace();
    			throw new RuntimeException(e);
    		}
    		return img;
    	}
    }
    Now, when I run it I get this error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1228800
    at java.awt.image.SinglePixelPackedSampleModel.setPix els(Unknown Source)
    at java.awt.image.WritableRaster.setPixels(Unknown Source)
    at sun.awt.image.SunWritableRaster.setPixels(Unknown Source)
    at DitherMain.<init>(DitherMain.java:35)
    at DitherMain.main(DitherMain.java:19)

    1228800 is width*height for this specific image. I don't see what should be going out of bounds.

    I really don't understand what the problem with this code is, but seeing as I've never written something like this before I assume there are probably a lot.
    In any case, can any of you guys shed some light on this?

    Thanks
    Last edited by nhmllr; 12-08-2012 at 03:23 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Converting a buffered image into an int[] array, then converting it back

    What line throws the exception?

  3. #3
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Re: Converting a buffered image into an int[] array, then converting it back

    It says in the error that it breaks at line 35, which is

    Java Code:
    raster.setPixels(0,0,width,height,endPixels);

Similar Threads

  1. Converting image to hexadecimal
    By abdique63 in forum New To Java
    Replies: 11
    Last Post: 04-26-2012, 12:02 PM
  2. Converting image to Point array
    By Aster in forum Java 2D
    Replies: 1
    Last Post: 12-10-2011, 02:49 PM
  3. Replies: 2
    Last Post: 10-04-2010, 02:50 PM
  4. Replies: 1
    Last Post: 10-03-2010, 06:29 AM
  5. Converting Image to byte array[] ?
    By afflictedd2 in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-11-2009, 11:33 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
  •