views:

351

answers:

2

I've written a class that I use for image processing in my Java program. The class cannot load an image properly, the 'data' field has the format [row][column][a=0,r=1,g=2,b=3]. When I try to load an image, the data is filled with 0s.

public class Image {
    public int[][][] data;

    public int width;
    public int height;
    Image(int width, int height, java.awt.Color color) {
        this.data = new int [height][width][4];
    }
    Image(java.lang.String path) {
        this.load(path);
    }
    public void load(java.lang.String path) {
        java.awt.Image image = new javax.swing.ImageIcon(path).getImage();
        int[][][] data = new int[image.getHeight(null)][image.getWidth(null)][4];
        int[] oneDPix = new int[image.getHeight(null) * image.getWidth(null)];

        this.width = image.getWidth(null);
        this.height = image.getHeight(null);

        java.awt.image.PixelGrabber pixelGrabber = new java.awt.image.PixelGrabber(image, 0, 0, image.getWidth(null), image.getHeight(null), oneDPix, 0, image.getWidth(null));

        for(int row = 0; row < image.getHeight(null); row++) {
            int[] aRow = new int[image.getWidth(null)];
            for(int col = 0; col < image.getWidth(null);col++) {
                int element = row * image.getWidth(null) + col;
                aRow[col] = oneDPix[element];
            }
            for(int col = 0;col < image.getWidth(null);col++){
                data[row][col][0] = (aRow[col] >> 24)& 0xFF;
                data[row][col][1] = (aRow[col] >> 16) & 0xFF;
                data[row][col][2] = (aRow[col] >> 8) & 0xFF;
                data[row][col][3] = (aRow[col]) & 0xFF;
            }
        }
        this.data = data;
    }
    public void save(java.lang.String path) throws java.io.IOException{
        int[] oneDPix = new int[this.width * this.height * 4];
        for(int row = 0, cnt = 0; row < this.height; row++){
            for(int col = 0; col < this.width; col++) {
                oneDPix[cnt] = ((this.data[row][col][0] << 24)
                                       & 0xFF000000)
                         | ((this.data[row][col][1] << 16)
                                       & 0x00FF0000)
                          | ((this.data[row][col][2] << 8)
                                       & 0x0000FF00)
                               | ((this.data[row][col][3])
                                       & 0x000000FF);
                cnt++;
            }
        }
        java.awt.Image image = java.awt.Toolkit.getDefaultToolkit()
                .createImage(new java.awt.image.MemoryImageSource
                (width, height,oneDPix, 0, width));

        java.io.File imagefile = new java.io.File(path);
        String ext = ".png";
        java.awt.image.BufferedImage bufferedImage = new java.awt.image.BufferedImage
                (this.width, this.height, java.awt.image.BufferedImage.TYPE_INT_ARGB);
        bufferedImage.getGraphics().drawImage(image, 0, 0, null);
        javax.imageio.ImageIO.write(bufferedImage, ext,imagefile);

    }
}

What am I doing wrong?

+1  A: 

You never tell the pixelGrabber to grabPixels(). Read the PixelGrabber documentation for details.

Jonathan Feinberg
Ah... That was stupid of me.Thank you for your help.
Steven
+1  A: 

Your first problem is that you do not start the PixelGrabber, so add this after you create the PixeGrabber:

try {
    pixelGrabber.grabPixels();
} catch (InterruptedException e) {
    e.printStackTrace();
}

Second problem is that when you call createImage(), it creates image with width and height -1. So the output is zero-length file.

EDIT. To write the image use "png" instead of ".png" for the extension

tulskiy
I've just tried saving an image file and the output is a zero length file, how do I get the image to recognize it's true size?
Steven
See my last EDIT.
tulskiy