Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (404)
games submitted by our members
Games in WIP (289)
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  
  Trapezoid!  (Read 801 times)
0 Members and 1 Guest are viewing this topic.
Offline counterp

Senior Member


Medals: 11



« Posted 2012-11-20 00:48:11 »

I was wondering if there are any fast algorithms out there for taking an image and morphing it into the shape of a trapezoid (trapezium).

I found this method:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
   private final float A = W * 0.08f;
   private final float B = W * (1f - 0.08f);

   private final void createTrapezium(BufferedImage src, BufferedImage out) {
      int[] pix = ((DataBufferInt) src.getRaster().getDataBuffer()).getData();
      int[] pix2 = ((DataBufferInt) out.getRaster().getDataBuffer()).getData();
      Arrays.fill(pix2, 0);
      for (int y = 0; y < HEIGHT; y++) {
         int yw = y * W;
         float y_to_h = y / (float) HEIGHT;
         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];
         }
      }
   }


But it's pretty CPU intensive (using this in real-time for a changing background image at 30 FPS)... Is there an alternative to using JAI?
Online sproingie
« Reply #1 - Posted 2012-11-20 01:18:11 »

You mention JAI, so I assume you've seen the thread at http://stackoverflow.com/questions/2500489/fast-perspective-transform-in-java-advanced-imaging-api ... unfortunately you're not going to get anywhere with an Affine transform, so you're kinda out of luck as built-in features go. 

If you're looking to do a transition, like screens swiping from one to the next, you could always pre-render them and just animate through those.  Otherwise the only really viable alternative that'll get you any reasonable quality at decent speed is to go 3d and use OpenGL.
Offline counterp

Senior Member


Medals: 11



« Reply #2 - Posted 2012-11-22 22:51:51 »

It seems that JAI has to be installed, that's why I don't want to use it.. Is there a version that you can just bundle with a project (with native libs)?
Games published by our own members! Check 'em out!
Try the Free Demo of Revenge of the Titans
Online sproingie
« Reply #3 - Posted 2012-11-22 23:13:20 »

You could try ImageMagick bindings, I'm pretty sure that has a perspective transform in it.  There's also JavaCV, which should be able to do anything you imagine once you figure it out.  Neither of those are going to be real-time either.


Offline counterp

Senior Member


Medals: 11



« Reply #4 - Posted 2012-11-23 01:57:58 »

Ugh. Would it be possible to have a 3D background and a 2D foreground if I switch to OpenGL? Time to look up tutorials.

Thanks!
Pages: [1]
  ignore  |  Print  
 
 

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (34 views)
2013-05-17 21:29:12

alaslipknot (42 views)
2013-05-16 21:24:48

gouessej (72 views)
2013-05-16 00:53:38

gouessej (71 views)
2013-05-16 00:17:58

theagentd (79 views)
2013-05-15 15:01:13

theagentd (74 views)
2013-05-15 15:00:54

StreetDoggy (114 views)
2013-05-14 15:56:26

kutucuk (138 views)
2013-05-12 17:10:36

kutucuk (138 views)
2013-05-12 15:36:09

UnluckyDevil (146 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.142 seconds with 21 queries.