raw image data -> bufferedimage

hey guys :slight_smile:

I’m stuck again :stuck_out_tongue:

I’ve been using these lines to render an image from an array of integers and an indexcolormodel, not excactly what one would call efficient but it works:


        BufferedImage bi = Display.createCompatibleImage(this.pcxwidth, this.pcxheight);
        bi.createGraphics().drawImage(Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(pcxwidth, pcxheight,
                getPalette(), this.imageData, 0, pcxwidth)), null, null);

should be self-explaining, first line creates a compatible image, second creates the image from the int[] and the colormodel, and draws it to the bufferedimage.

now, I recently noticed the WriteableRaster class and thought it might be better to store the imagebytes as an object of this class, so I tried this experimental code instead of the above:


        model = new MultiPixelPackedSampleModel(DataBufferInt.TYPE_INT, this.pcxwidth, this.pcxheight, 8);
        buffer = new DataBufferInt(this.imageData, this.imageBytes);
        WritableRaster sup = WritableRaster.createWritableRaster(model, buffer, new Point(0, 0));
        BufferedImage bi = new BufferedImage(getPalette(), sup, false, null);

it creates a multipixelpackedsamplemodel and a WriteableRaster from the imageData array, both are passed to the bufferedimage constructor. now, this has one big disadvantage: it doesn’t work. the image looks like every ~10th pixel is drawn. :frowning:

ideas?

Got it. The image is 8 bit (as seen in the code :P), but for some reason I used int[] instead of byte[] to store the image data ::slight_smile:

using DataBufferByte it runs fine, and the image renders about ten tims faster :smiley:

Still, it doesn’t use createCompatibleImage… is this an issue? :expressionless: