How to write directly to an image buffer

Buffered images are slow; in fact I was able to draw perspective correct texture mapped polygons in software far quicker than I could copy the data to a BufferedImage. And the methods making use of the raster sometimes do not work as the format being used might be byte instead of int.

         // initializing
        int offset = 0;
        int buffer[] = new int [width*height];
        int scansize = width;
        MemoryImageSource _source =new MemoryImageSource(width ,height,_buffer,offset,scansize );
        _source.setAnimated(true);
        _source.setFullBufferUpdates(true);
        Image _imageBuffer= Toolkit.getDefaultToolkit().createImage(_source);
        // setting pixels
        _buffer[x+y*width] = colour;

        // updating
        _source.newPixels();                      // will copy _buffer to _imageBuffer 

t

[quote=“keldon85,post:1,topic:29261”]
you can be set the type of the buffferedimage, and therefore of the raster when you create it.

You can, but in some cases you can get an int instead of a byte - I have not encountered this, but there were a lot of demo’s/examples where they had it return int types. It is always possible that the code was untested (or something), but it leads me to believe that somehow there is some inconsistency with how Java handles it.

It depends how the buffered image was created. If you create it yourself with the constructor, then it will be what you tell it to be. If it was created via GraphicsConfiguration.createCompatibleImage(), then the data type will be unsure. The problem there is if you don’t use GraphicsConfiguration.createCompatibleImage() then you can get slow downs when drawing the image if it is not compatible with the current GraphicsConfiguration. One solution is to hold your data in a ByteBuffer and get the type of data out you need for the WritableRaster. If the data type is an int then you use ByteBuffer.asIntBuffer.get(int[]). Don’t know how quick that would be though.