Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  fast bitmap manipulation  (Read 2217 times)
0 Members and 1 Guest are viewing this topic.
Offline Crest

Senior Newbie




Java games rock!


« Posted 2002-12-25 17:54:47 »

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
Offline zparticle

Senior Devvie




Thick As A Brick


« Reply #1 - Posted 2002-12-25 19:54:24 »

Try something like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
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();
    }
}

Offline Harley Rana

Junior Devvie




Java games rock!


« Reply #2 - Posted 2002-12-25 21:39:55 »

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. Smiley
Harley.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
    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);      
   


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline whome

Junior Devvie




Carte Noir Java


« Reply #3 - Posted 2003-01-03 16:08:34 »

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/
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (48 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!