fast bitmap manipulation

hi,
i recently started to port some of my old 2d-demos to java. for the effects (plasma etc.) i need direct access to pixel data of the offscreen image.
after playing around with filters and pixelgrabbers, my fastest implementation uses the BufferedImage class. however, for every pixel one needs to call a method setRGB/getRGB. direct access to an array would be faster, wouldn’t it?

does somebody have an idea how i could speed up the access? is this possible with the Raster class?

thx

Try something like this:


package com.sas.specialfx.effects;

import com.sas.specialfx.Main;
import com.sas.specialfx.DisplayObject;
import java.net.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.imageio.ImageIO;

public class BumpMap1 extends DisplayObject {

    private BufferedImage img = null;
    private BufferedImage map = null;
    private VolatileImage drawing = null;
    private DataBufferByte imgRasterBuffer = null;
    private int imgWidth = 0;
    private int imgHeight = 0;
    private int imgX = 0;
    private int imgY = 0;
    private float normalVectors[][];
    private int imgColors[][];
    private int mouseX = 0, mouseY = 0;
    private boolean changed = true;

    public BumpMap1(String propName) {
        super();
        setUpdateInterval(0);

        URL url = BumpMap1.class.getResource("bumpmap1/img.png");
        try
        {
            img = ImageIO.read(url);
        }
        catch(java.io.IOException x)
        {
            System.out.println(x.getMessage());
        }
        imgWidth = img.getWidth();
        imgHeight = img.getHeight();
        imgX = (Main.getInstance().displayWidth/2)- (imgWidth/2);
        imgY = (Main.getInstance().displayHeight/2)- (imgHeight/2);
        imgRasterBuffer = (DataBufferByte)(img.getRaster().getDataBuffer());

        url = BumpMap1.class.getResource("bumpmap1/map.png");
        try
        {
            map = ImageIO.read(url);
        }
        catch(java.io.IOException x)
        {
            System.out.println(x.getMessage());
        }

        normalVectors = new float[imgWidth * imgHeight][3];
        findNormalVectors (normalVectors);

        imgColors = new int[imgWidth * imgHeight][3];
        imageToRGBArray (imgColors);

        drawing = Main.getInstance().getGraphicsConfiguration().createCompatibleVolatileImage(imgWidth,imgHeight);
    }

    public void mouseMoved(java.awt.event.MouseEvent evt) {
        synchronized(img)
        {
            mouseX = evt.getX();
            mouseY = evt.getY();
            changed = true;
        }
    }

    void imageToRGBArray (int RGBarray[][])
    {
        // Store RGB values into array
        for (int x = 0; x < imgWidth; x++)
        {
            for (int y = 0; y < imgHeight; y++)
            {
                int c = img.getRGB(x, y);
                int loc = y * imgWidth + x;
                RGBarray[loc][0] = (c&(255<<16))>>16;
                RGBarray[loc][1] = (c&(255<<8))>>8;
                RGBarray[loc][2] = c&255;
            }
        }
    }

    // fills up the array with normal vectors of the image
    void findNormalVectors (float nv[][])
    {

        for (int x = 1; x < imgWidth - 1; x++)
        {
            for (int y = 1; y < imgHeight - 1; y++)
            {
                int c = map.getRGB(x + 1, y);
                int x0r = (c&(255<<16))>>16;
                int x0g = (c&(255<<8))>>8;
                int x0b = c&255;

                c = map.getRGB(x - 1, y);
                int x1r = (c&(255<<16))>>16;
                int x1g = (c&(255<<8))>>8;
                int x1b = c&255;
                
                c = map.getRGB(x, y + 1);
                int y0r = (c&(255<<16))>>16;
                int y0g = (c&(255<<8))>>8;
                int y0b = c&255;

                c = map.getRGB(x, y - 1);
                int y1r = (c&(255<<16))>>16;
                int y1g = (c&(255<<8))>>8;
                int y1b = c&255;
                
                float Xd = (x0r - x1r)
                    + (x0g - x1g)
                    + (x0b - x1b);

                float Yd = (y0r - y1r)
                    + (y0g - y1g)
                    + (y0b - y1b);

                // maximum for Xd, Yd is: (MAX - 0) + (MAX - 0) + (MAX - 0) = 3 * MAX

                Xd /= (float)(3 * 255);
                Yd /= (float)(3 * 255);
                
                float Nx = Xd;
                float Ny = Yd;
                float Nz = (float)(1 - Math.sqrt ((Xd * Xd) + (Yd * Yd)));
                
                if (Nz < 0.0f)
                    Nz = 0.0f;

                nv[y * imgWidth + x][0] = Nx;
                nv[y * imgWidth + x][1] = Ny;
                nv[y * imgWidth + x][2] = Nz;
            }
        }
    }

    float bumpIntensity (int pixelX, int pixelY)
    {
        float Nx, Ny, Nz;
        
        // get normal vector of map
        int pval = pixelY * imgWidth + pixelX;
        Nx = normalVectors[pval][0];
        Ny = normalVectors[pval][1];
        Nz = normalVectors[pval][2];
        
        // make vector from pixel to light
        float lightvX = (float)(mouseX - pixelX);
        float lightvY = (float)(mouseY - pixelY);
        float lightvZ = (float)(30.0f  - 0.0f); //30 is how big the light is
        
        // normalize
        float length = (float)Math.sqrt (lightvX*lightvX + lightvY*lightvY + lightvZ*lightvZ);
        
        lightvX /= length;
        lightvY /= length;
        lightvZ /= length;
        
        // take dot product
        float intensity = (Nx * lightvX + Ny * lightvY + Nz * lightvZ);
        
        if (intensity < 0.0f)
            intensity = 0.0f;
        
        return intensity;
    }

    public void display(Graphics g) {
        if(changed)
        {
            synchronized(img)
            {
                int x, y;
                float intensity;
                
                byte newR, newG, newB;
                byte[] data = imgRasterBuffer.getData();

                for (x = 0; x < imgWidth; x++)
                {
                    for (y = 0; y < imgHeight; y++)
                    {
                        intensity = bumpIntensity (x, y);
                        
                        // brighten slightly (ambient)
                        intensity += 0.18;
                        if (intensity > 1.0)
                            intensity = (float)1.0;
                        
                        int[] yva = imgColors[y * imgWidth + x];
                        newR = (byte)(intensity * yva[0]);
                        newG = (byte)(intensity * yva[1]);
                        newB = (byte)(intensity * yva[2]);
                        
                        int index = (((y*imgWidth)*3)+(x*3));
                        data[index]=newR;
                        data[index+1]=newG;
                        data[index+2]=newB;
                    }
                }
                Graphics vg = drawing.getGraphics();
                vg.drawImage(img,0,0,null);
                vg.dispose();
                changed = false;
            }
        }
        g.drawImage(drawing, 0, 0, null);
    }

    public void update(long currentTime) {
    }

    public void cleanup() {
        img.flush();
        map.flush();
        drawing.flush();
    }
}

Hey i was looking at the Java port of http://www.gaffer.org/tinyptc/ , which allows really fast pixel acces.
It uses the image consumer model. Calling update(newPixels), calls setPixels (0,0,width,height,colorModel,newPixels,0,width);
It runs super quick.

The other way to do it is with MemoryImageSource.
Im not sure if this is the best way, but it works for me. :slight_smile:
Harley.


    int size = 800*600;
    int[] pixels = new int[size];
    
    MemoryImageSource memImg;
    DirectColorModel colorModel = new DirectColorModel(32,0x00ff0000,0x0000ff00,0x000000ff,0xff000000);

// load pixels
    memImg = new MemoryImageSource  (800,600,pixels,0,600);
    memImg.setAnimated (true);


// then in the run method, update the pixels.
   memImg.newPixels (pixels, colorModel, 0, 800);

// render the image
   g.drawImage(createImage(memImg),0,0,null);      
    


I’ve used MemoryImageSource in my test applet. Requires Java1.4 plugin. See “sorsazippi” link for sources. Don’t know is this the best way to manipulate buffer data and then render the graphics but seem to work ok.
http://www.mbnet.fi/~akini/java/colorfader/