awt - Image is drawing sideways to screen (Java BufferedImage, WritableRaster) -
my code drawing image incorrectly. every time try guess what's wrong , fix it, arrayoutofbounds exceptions. array business mixing me up. need help. thanks!
(by way, need extract pixel values , store them can blur, sharpening, , other types of methods on data hand. i'm learning how image processing works.)
this code gives result (the image sideways when should right-side up):
this original image:
image.java:
package mycode; import java.awt.image.bufferedimage; import java.awt.image.writableraster; public class image { public int height; public int width; public int image[][]; public image(bufferedimage openimage) { height = openimage.getheight(); width = openimage.getwidth(); image = new int[openimage.getwidth()][openimage.getheight()]; for(int x = 0; x < openimage.getwidth(); x++) { for(int y = 0; y < openimage.getheight(); y++) { image[x][y] = 0xff & openimage.getrgb(x, y); debug.print(""+integer.tohexstring(image[x][y])); } } } }
refreshview(): (model.image instance of image class above.)
@override public void refreshview(graphics2d g2d) { //draw background image. if(controllerstate.inimagemode && model.image != null) { debug.print("drawing background image"); bufferedimage bufferedimage = new bufferedimage(model.image.width, model.image.height, bufferedimage.type_byte_gray); writableraster wr = bufferedimage.getraster(); for(int x = 0; x < model.image.width; x++) { for(int y = 0; y < model.image.height; y++) { wr.setpixels(0, x, model.image.width, 1, model.image.image[x]); } } g2d.drawimage(bufferedimage, null, 0, 0); } }
it seems have classic x-y row-column mixup problem. think inside of loop want:
wr.setpixels(x, 0, 1, model.image.height, model.image.image[x]);