Trapezoid!

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:

	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?

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.

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)?

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.

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!