Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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  
  2D Rendering Advice  (Read 1701 times)
0 Members and 1 Guest are viewing this topic.
Offline castlec

Junior Newbie





« Posted 2007-07-11 14:37:54 »

I am writing an application that decodes image data from a network stream and then renders it.  I need two rendering modes, one for on screen rendering and one for image rendering to disk.  Because the data is often raw pixels, I am currently using the code below to allow access to the pixel data directly and then copy the changed pixels to the regular BufferedImage.  When the data stream has a line or rectangle encoded, I draw directly to the BufferedImage.  Is there a faster/smarter way to do this?  I have tried accessing the pixels stored in the DataBufferInt rather than using the pixel[] below and performance is horrendous.

Thanks in advance for any suggestions.

pixels = new int[width * height];
memImageSrc = new MemoryImageSource(width, height, colorModel, pixels, 0, width);
memImageSrc.setAnimated(true);
      
memImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
memGraphics = memImage.getGraphics();
pixelsSource =   new MemoryImageSource(width, height, colorModel, pixels, 0, width);
pixelsSource.setAnimated(true);
rawPixelsImage = Toolkit.getDefaultToolkit().createImage(pixelsSource);
Offline brackeen

Junior Member





« Reply #1 - Posted 2007-07-11 15:03:37 »

Not sure if this will help, but here's code for accessing pixel[] arrays in a BufferedImage quickly, so you don't have to copy from a MemoryImageSource.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
        int w = getWidth();
        int h = getHeight();
        int[] pixels = new int[w * h];
       
        DirectColorModel colorModel =
            new DirectColorModel(24, 0xff0000, 0x00ff00, 0x0000ff);

        SampleModel sampleModel = new SinglePixelPackedSampleModel(
            DataBuffer.TYPE_INT, w, h, new int[] { 0xff0000, 0x00ff00, 0x0000ff });
       
        DataBuffer dataBuffer = new DataBufferInt(pixels, w * h);
       
        WritableRaster raster = Raster.createWritableRaster(
            sampleModel, dataBuffer, new Point(0,0));
       
        awtImage = new BufferedImage(colorModel, raster, true, new Hashtable());
Offline castlec

Junior Newbie





« Reply #2 - Posted 2007-07-11 17:04:34 »

Not sure if this will help, but here's code for accessing pixel[] arrays in a BufferedImage quickly, so you don't have to copy from a MemoryImageSource.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
        int w = getWidth();
        int h = getHeight();
        int[] pixels = new int[w * h];
       
        DirectColorModel colorModel =
            new DirectColorModel(24, 0xff0000, 0x00ff00, 0x0000ff);

        SampleModel sampleModel = new SinglePixelPackedSampleModel(
            DataBuffer.TYPE_INT, w, h, new int[] { 0xff0000, 0x00ff00, 0x0000ff });
       
        DataBuffer dataBuffer = new DataBufferInt(pixels, w * h);
       
        WritableRaster raster = Raster.createWritableRaster(
            sampleModel, dataBuffer, new Point(0,0));
       
        awtImage = new BufferedImage(colorModel, raster, true, new Hashtable());



And does that end up being quicker than using the WritableRaster that is supplied by the BufferedImage automatically?  I tried
1  
int[] pixels = ((DataBufferInt)memImage.getRaster().getDataBuffer()).getData(0);

and it ends up being much slower than the version I have now
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mickleness

Senior Newbie





« Reply #3 - Posted 2007-07-28 05:26:42 »

My two cents:

I recommend never ever touching the DataBuffer of an image.  I explain why here:
http://javagraphics.blogspot.com/2007/04/managed-images.html

I've had good performance with:
bi.getRaster().setDataElements(x,y,w,h,data)

But "good" is a vague word depending on the size of the animation, the minimum system specs of your app, etc.
Offline keldon85

Senior Member


Medals: 1



« Reply #4 - Posted 2007-07-28 07:51:00 »


And does that end up being quicker than using the WritableRaster that is supplied by the BufferedImage automatically?  I tried
It ends up being much faster than what is supplied by BufferedImage automatically. You actually write directly to the pixels[] buffer

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.

atombrot (25 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!