DevIL -> BufferedImage

Started by kengaroo, July 08, 2007, 02:17:25

Previous topic - Next topic

kengaroo

Hi, I am trying to load an image and construct an instance of BufferedImage, unfortunately with no luck so far :( The image displayed shows some dots on the white background, maybe you could help me.

IntBuffer scratch = BufferUtils.createIntBuffer(1);

		IL.create();

		IL.ilGenImages(scratch);
		IL.ilBindImage(scratch.get(0));

		IL.ilLoadImage("./test/images/tga32.tga");

		ByteBuffer imageData = null;

		//		 get image attributes
		int width = IL.ilGetInteger(IL.IL_IMAGE_WIDTH);
		int height = IL.ilGetInteger(IL.IL_IMAGE_HEIGHT);
		int bpp = IL.ilGetInteger(IL.IL_IMAGE_BPP);
		int depth = IL.ilGetInteger(IL.IL_IMAGE_DEPTH);
		int bitsPerPixel = IL.ilGetInteger(IL.IL_IMAGE_BITS_PER_PIXEL);
		int bytesPerPixel = IL.ilGetInteger(IL.IL_IMAGE_BYTES_PER_PIXEL);

		imageData = BufferUtils.createByteBuffer(width
				* height * bpp);
		
		IL.ilCopyPixels(0, 0, 0, width, height, 1,
				IL.IL_RGBA, IL.IL_BYTE, imageData);		

		DataBufferSubClass db = new DataBufferSubClass(DataBuffer.TYPE_BYTE,bpp*width*height);
		db.setWrBuf(imageData);		
		
		ColorModel colorModel = new DirectColorModel(bitsPerPixel, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
		
		SampleModel sampleModel = colorModel.createCompatibleSampleModel(width,height);
		WritableRaster writableRaster = new MyWritableRaster(sampleModel, db, new Point(0,0));
		BufferedImage bufferedImage = new BufferedImage(colorModel, writableRaster, true, null);


import java.awt.Point;
import java.awt.image.DataBuffer;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;

public class MyWritableRaster extends WritableRaster {
	public MyWritableRaster(SampleModel sampleModel, DataBuffer dataBuffer,
			Point origin) {
		super(sampleModel, dataBuffer, origin);
	}
}


import java.awt.image.DataBuffer;
import java.nio.ByteBuffer;

public class DataBufferSubClass extends DataBuffer {

	private ByteBuffer wrBuf;

	public void setWrBuf(ByteBuffer wrBuf) {
		this.wrBuf = wrBuf;
	}

	public DataBufferSubClass(int dataType, int size) {
		super(dataType, size);
	}

	public DataBufferSubClass(int dataType, int size, int numBanks) {
		super(dataType, size, numBanks);
	}

	public DataBufferSubClass(int dataType, int size, int numBanks, int offset) {
		super(dataType, size, numBanks, offset);
	}

	public DataBufferSubClass(int dataType, int size, int numBanks,
			int[] offsets) {
		super(dataType, size, numBanks, offsets);
	}

	public int getElem(int bank, int i) {
		return wrBuf.get(i);
	}

	public void setElem(int bank, int i, int val) {
		wrBuf.put(i, (byte) val);
	}

}


Thanks a lot!