java - Can't load image into buffered image 2D array -
file newfile = null; bufferedimage oldimage = null; bufferedimage newimage = null; string filename = new string((args[0])); string newfilename = new string(filename.replacefirst(".png", "-tiled.png")); try{ oldimage = imageio.read(new file(filename)); system.out.println("reading complete"); } catch(ioexception e) { system.out.println("error: "+ e); } int width = oldimage.getwidth(); int height = oldimage.getheight(); newimage = new bufferedimage(width, height, bufferedimage.type_int_rgb); int imagepixels [][] = new int [height][width]; (int = 0; < height -1; ++i) { (int j = 0; j < width - 1; ++j) { imagepixels[i][j] = oldimage.getrgb(i,j); //error here } }
it compiles fine, when run program, run 10x10 image, when try , use 800x600 image not work, , error occurs on commented line saying
"exception in thread "main" java.lang.arrayindexoutofboundsexception: coordinate out of bounds! @ sun.awt.image.byteinterleavedraster.getdataelements(unknown source) @ java.awt.image.bufferedimage.getrgb(unknown source) @ imagery.imagetiler.main(imagetiler.java:35)
can spot irritating error?
a little side info: program gets file name command line , loads rgb pixels 2d array.
any appreciated. cheers.
you've got coordinates wrong way around in getrgb
call. image height in y direction, using height bounds of i
... x coordinate.
this page explains java image coordinate system.
Comments
Post a Comment