Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  Real time manipulating an image to a trapezium  (Read 4168 times)
0 Members and 1 Guest are viewing this topic.
Offline zammbi

JGO Coder


Medals: 4



« Posted 2009-03-21 15:13:08 »

I want to see if I can do perspective rendering.

After much searching around on google I found http://forums.sun.com/thread.jspa?threadID=5262491
which had a basic solution. But it seemed to be slow at 500x400 images and therefore unable to be used for realtime.

I want to know if anyone knows of a quicker way using only Java2D? What about copying 1-2 pixels strips and resizing them and painting on another image?
Just wanting to know if anyone else has worked in this area before I waste many hours playing around.


Current project - Rename and Sort
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2009-03-21 15:51:31 »

The slow line of code is this:

dest.setRGB(destX, y, src.getRGB(x, y));


It is much faster if you read all pixels from a BufferedImage into an int[], do all your operations on it, and then write your int[] back into your BufferedImage in 1 operation.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Online CommanderKeith
« Reply #2 - Posted 2009-03-21 19:29:42 »

If it's a scaling operation, you could probably just use the AffineTransform class.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2009-03-21 20:10:17 »

You can't scale a rectangle into a trapezium

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline SimonH
« Reply #4 - Posted 2009-03-21 20:13:20 »

You can't scale a rectangle into a trapezium
You can skew and clip two half images... not ideal though - manipulating the bits would be better (oo-er missus!)

People make games and games make people
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2009-03-21 21:04:15 »

I don't know if manipulating pixel data is cheap enough for real-time?

If you want to do stuff like this, it might make sense to go and get a simple grasp of OpenGL. Images are drawn using 4 vertices, so it's easy to reshape an image however you want.

See my work:
OTC Software
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2009-03-21 22:20:50 »

You can skew and clip two half images... not ideal though - manipulating the bits would be better (oo-er missus!)

Then you lose a lot of center pixels, instead of 'scaling' them

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline zammbi

JGO Coder


Medals: 4



« Reply #7 - Posted 2009-03-22 02:14:59 »

Quote
If you want to do stuff like this, it might make sense to go and get a simple grasp of OpenGL. Images are drawn using 4 vertices, so it's easy to reshape an image however you want.
Its just a simple effect I wanted to try in my game, which the game is only Java2D.
If its too slow then I wont do it. So this is just purely for fun...

Quote
It is much faster if you read all pixels from a BufferedImage into an int[], do all your operations on it, and then write your int[] back into your BufferedImage in 1 operation.
Thanks. Can someone show some code to get me started? I'm not great on the pixel stuff....

Anyone know if this option will be an AffineTransform in Java 7 or so?


Current project - Rename and Sort
Offline zammbi

JGO Coder


Medals: 4



« Reply #8 - Posted 2009-03-22 04:09:10 »

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  
    private static BufferedImage createTrapezium(BufferedImage src) {

        int w = src.getWidth();
        int h = src.getHeight();
        float A = w * .1f;
        float B = w * .9f;
        //float C = 0;
       //float D = w;

        int[] pix = new int[h*w];
        int[] pix2 = new int[h*w];
       
        pix = (int[]) src.getRaster().getDataElements(0, 0,w,h,  null);
        for (int y = 0; y < h; y++) {
            int yw = y*w;
           
           final float y_to_h = (float) y / (float) h;
           //float C_A = A;
          final float C_A_offset = A* (1 - y_to_h);
           //float offset = C_A_offset;
          final float trapeziumLine = -C_A_offset + B + (w - B) * y_to_h;
           final float k = trapeziumLine / w;
                     
            for (int x = 0; x < w; x++) {
                int destX = Math.min(w - 1, Math.round(C_A_offset + x * k));
                pix2[yw+destX] =  pix[yw+x];
 
            }
        }
        src.getRaster().setDataElements(0,0,w,h,pix2);
        return src;
    }

All right I got this. Wondering if this still can be improved?
The shape will always stay the same, and I would only render a square of it. So the top width is the would be the width of the square.

Edit:I just tried code on my game on my system, it can do it fine 30fps, but I believe it wont like slower computer so much, so any improvements will be great.

Current project - Rename and Sort
Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #9 - Posted 2009-03-22 10:37:38 »

this is 9.8 times faster  Grin
Plus you don't change the original image anymore.
You need that out image in the same size as src image.
There is still some room for improvement using fixed integer math.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
private void mycreateTrapezium(BufferedImage src, BufferedImage out, float f) {
      int w = src.getWidth();
      int h = src.getHeight();
      if (f < 0)
         f = 0;
      if (f >= 1)
         f = 1;
      float A = w * f;
      float B = w * (1f - f);
      int[] pix = ((DataBufferInt) src.getRaster().getDataBuffer()).getData();
      int[] pix2 = ((DataBufferInt) out.getRaster().getDataBuffer()).getData();
      for (int i = 0; i < pix2.length; i++) pix2[i] = 0; // clear the image
     for (int y = 0; y < h; y++) {
         int yw = y * w;
         float y_to_h = (float) y / (float) h;
         float C_A_offset = A * (1 - y_to_h);
         float trapeziumLine = -C_A_offset + B + (w - B) * y_to_h;
         float k = trapeziumLine / w;
         for (int x = 0; x < w; x++) {
            int destX = (int) (C_A_offset + x * k);
            pix2[yw + destX] = pix[yw + x];
         }
      }
   }

lots of sillystupid games: http://www.emaggame.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zammbi

JGO Coder


Medals: 4



« Reply #10 - Posted 2009-03-22 11:16:14 »

Thanks!, that does seem to do the trick nicely.
It seems only to be using 5ish% more then my original cpu usage, I believe.

Time to play around and get it ready for its release  Smiley

Anymore improvements are also welcome.

Current project - Rename and Sort
Offline zammbi

JGO Coder


Medals: 4



« Reply #11 - Posted 2009-03-26 03:28:37 »

Seems this effect is going well, but its not totally what I wanted.
The effect I want is perspective rendering. So further away should be smaller, which atm is only getting skinnier.
I don't want to miss strips of pixels, that's the only way I know how to do this effect.
Any help in this area would be great.

Current project - Rename and Sort
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 744
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2009-03-26 20:27:12 »

For high quality rendering you probably have to roll your own 1D sampler, or even 2D sampler for best results. Don't forget mipmaps for ultimate results Smiley

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline data-loss

Junior Newbie





« Reply #13 - Posted 2009-04-09 14:33:02 »

You can also use JAI (Java Advanced Imaging) and the PerspectiveWarp class it offers. It allows you to draw any image into any 4-edges polygon. JAI also runs in Pure-Java mode, which is performing well enough on my system - up to 100fps on a 400x300 dynamically rendered image.
Offline zammbi

JGO Coder


Medals: 4



« Reply #14 - Posted 2009-04-09 14:38:43 »

Thanks for that info.
I have currently just left it for now, I might convert the game over to JavaFX when its supported on Linux and use the perspective effect on that.

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.

Riven (6 views)
2014-07-29 12:53:52

Dwinin (7 views)
2014-07-29 10:59:34

E.R. Fleming (21 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!