Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Creating small BufferedImages from large BufferedImage  (Read 2951 times)
0 Members and 1 Guest are viewing this topic.
Offline jamesvanboxtel

Junior Newbie





« Posted 2008-06-12 22:38:36 »

Hello Java Gaming Community.

I heard from Sun that this is the place to post about Java2D performance questions.

I am working on the Java Instructional Gaming Engine. We are trying to speed up the performance on our spritesheet loading code. Basically we have an XML file and Image pair that are loaded at the beginning of games. Each animation has to copy out all of the frames out of the main image.

From my testing I have found that the larger the main image is, the longer it takes to load. While the obvious solution might be just to not use big images, it is bound to happen when students are using the engine.

I have extracted essentially what we are doing into the code below. We are doing a little more than this with setting frame properties etc. but this is basically it.

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  
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

public class LoadTest {
   
   Frame frame;
   URL sheetURL;
   BufferedImage sheetImage;
   
   public static void main(String[] args) {
      //System.setProperty("sun.java2d.opengl", "True");
           
      new LoadTest();
   }
   
   public LoadTest()
   {
      long st = System.nanoTime();
      frame = new Frame();
     
      sheetURL = ClassLoader.getSystemResource("bugs.png");
     
      try {
         sheetImage = ImageIO.read(new BufferedInputStream(sheetURL
               .openStream()));

      } catch (IOException e) {
         sheetImage = null;
         System.err.println("IO Exception while loading image '"
               + sheetURL.toString() + "'");
         return;
      }
     
      for(int i=0; i<10; ++i)
         load(i);      
     
      long t = ((System.nanoTime()-st)/(1000000));
      System.out.println("Total Load Test took " + t + " ms");
   }
   
   public boolean load(int i)
   {      
      int width = 20;
      int height = 20;
      int top = i * height;
      int left = 0;
      int rows = 1;
      int columns = 10;
     
      long st = System.nanoTime();
           
      BufferedImage[] bframes = new BufferedImage[rows * columns];
      for (int y = 0; y < rows; ++y) {
         for (int x = 0; x < columns; ++x) {
            bframes[y * (columns) + x] = createImageResource(
                  sheetImage, Transparency.BITMASK, width,
                  height, left + width * x, top + height * y);
         }
      }      
      long t = ((System.nanoTime()-st)/(1000000));
      System.out.println("Created Image List with " + (rows*columns) + " elements in " +
               t + " ms for resource ");
     
      return false;
   }

   protected BufferedImage createImageResource(final BufferedImage originalImg,
         final int transparency, final int w, final int h,
         final int xoffset, final int yoffset) {

      BufferedImage r = drawImage(originalImg, transparency, w, h, -xoffset,
            -yoffset);
      return r;
   }
   
   protected BufferedImage drawImage(final Image i, final int transparency, final int w, final int h,
         final int xoffset, final int yoffset)
   {
      GraphicsConfiguration gc = frame.getGraphicsConfiguration();
      BufferedImage image = gc.createCompatibleImage(w, h, transparency);
      image.getGraphics().drawImage(i, xoffset, yoffset, null);
     
      return image;
   }
}


I have attached the images to this post. Here are my run times for each.
bugs.png - Total Load Test took 1206 ms
bugs_large.png Total Load Test took 17638 ms

Do you guys have any suggestions on how we could speed this up?

Thanks,
Offline fletchergames

Senior Member





« Reply #1 - Posted 2008-06-13 16:45:39 »

I think the main problem is that the large image isn't getting hardware accelerated.  It can't be stored in video memory because it's too large.  Try changing it to something no larger than 1024x768.  Assuming you can view that resolution on your screen and have an adequate video card, it will probably be much faster if I'm right.

There is probably some way to load the image in chunks without storing it in a larger image.   javax.imageio.ImageReader has something involving tiles, which makes me think it supports this capability.  I'm not sure how it works though.

If you figure out how to use javax.imageio.ImageReader in this manner, I would appreciate it if you posted the code.  I'm a bit curious about it.

There are a couple minor things I noticed.  First, your drawImage method should call dispose() on the Graphics context after you finished drawing the image.  I doubt that is causing the problem, but dispose should free up some kind of resources.

Second, you could optimize the loops that split the image up into tiles by reducing the multiplications to additions.  This probably won't make enough of a difference, but it something I would always do with multiplications in an inner loop.  Here's some code to explain what I mean:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
int iFrame = 0;
int yOffsetInSourceImage = top;
for (int y = 0; y < rows; ++y) {
   int xOffsetInSourceImage = left;

   for (int x = 0; x < columns; ++x) {
      bframes[iFrame] = createImageResource(sheetImage, Transparency.BITMASK, width,
         height, xOffsetInSourceImage, yOffsetInSourceImage);

      //update index and x-offset
     iFrame++;
      xOffsetInSourceImage += width;
   }

   //update y-offset
  yOffsetInSourceImage += height;
}


I believe that is correct unless I'm interpreting some of your variables wrong.

Addition is a bit faster than multiplication, though this optimization would probably have a neglible effect.  It might even slow things down if it prevents some other memory from being cached by the CPU.
Offline jamesvanboxtel

Junior Newbie





« Reply #2 - Posted 2008-06-13 17:08:05 »

Thanks for the reply Fletcher.

I was experimenting with it more and trying Volatile Images and I discovered the problem.

When you load in a png with ImageIO it can sometimes be read in as a translucent image. If I copied that to a BITMASK buffered image it was then able to be accelerated.

I am still looking for a way to force initially loading it as a BITMASK transparency, but for now this works.

While the addition optimization might help, I am going to hold off since this is only done once during loading, not in a game loop.

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

JGO Coder


Medals: 4



« Reply #3 - Posted 2008-06-14 14:20:42 »

Ah thanks fletchergames for that tip, I knew addition was faster but didn't think of doing my loops like so.

Current project - Rename and Sort
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.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (26 views)
2014-09-07 01:11:22

Longarmx (26 views)
2014-09-07 01:10:19

mitcheeb (34 views)
2014-09-04 23:08:59
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!