Please login or register.

Login with username, password and session length

Author Topic: DevIL -> BufferedImage  (Read 5211 times)


  • Newbie
  • *
  • Offline Offline
  • Posts: 1
DevIL -> BufferedImage
« on: July 08, 2007, 02:17:25 »

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.

Code: [Select]
IntBuffer scratch = BufferUtils.createIntBuffer(1);




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);

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);

Code: [Select]

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);

Code: [Select]

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!