## Transformations

Questions regarding game mechanics and graphic programming should go here.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

### Transformations

I'm working with transforming images(simple ones, like lines, circles,etc). I use a transformation matrix. Right now, I rotate a pixel by multiplying its coordinates with a transformation matrix and plotting the nearest resulting pixel. This results in a few pixels being 'lost'. After a number of successive transforms, all I'm left with is garbage... One possible way to circumvent this issue would be to rotate the 'operation' of drawing a line, that is, just the start and end point and draw the line again. But suppose I have a more arbitrary image...?

You can't conquer the game if you can't conquer yourself.

Jetru

Posts: 751
Joined: Sat Oct 18, 2003 11:46 pm
Location: Bangalore,India

I would work the other way around. For each pixel in the final image, find out which point it should come from (multiply by the inverse of the transformation matrix to do this), then find the closest pixel or interpolate several pixels to extract a color from the original image.

If you do several of these transformations you will always lose quality.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

But shouldn't this produce similar results to the previous method?

Interpolating would mean 'averaging the color' of the possible four source pixels(or so) based on their 'exposure' to the resulting transformed pixel

You can't conquer the game if you can't conquer yourself.

Jetru

Posts: 751
Joined: Sat Oct 18, 2003 11:46 pm
Location: Bangalore,India

Jetru wrote:But shouldn't this produce similar results to the previous method?

If your transformation is "scale everything up by a factor of 10", your method results in only one pixel in 100 being set in the destination image, while mine results in every pixel being set, although many pixels will be set to the same color.

Interpolating would mean 'averaging the color' of the possible four source pixels(or so) based on their 'exposure' to the resulting transformed pixel

Something like that. If the image you are dealing with is a photo, just finding the closest pixel will result in pixelization. You'll get much better results with some careful interpolation.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Ok I did what you suggested and the quality is significantly better. But I still need to improve it a bit more. Rotating an image some 5 times makes it totally waste!

So i was wondering about interpolation. I googled but found nothing really instructive. I have a pixel, I apply the transformation matrix and I get a pixel. I need to average the color of the destination pixel based on the source pixel. Do I take 9 pixels surrounding the source pixel? And just give my center pixel a 50% weight and add the rest to the pixel? What would be a particularly good technique?

You can't conquer the game if you can't conquer yourself.

Jetru

Posts: 751
Joined: Sat Oct 18, 2003 11:46 pm
Location: Bangalore,India

What type of images are you manipulating? Photos? You can try with some bilinear interpolation (in the style of Gouraud shading).

I don't understand what you are trying to do but, could you just remember the chain of manipulations and the original image? That way you won't lose any precision when compounding these operations.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Not actually...I'm actually manipulating things like lines, circles, polygons and maybe just random scribbles. I'm working on a paint like program from scratch(no libraries for lines, etc etc)(Project...don't blame me).

Translating layers works fine as its just moving around pixels. Scaling them works a lot better and since there aren't many gradients, its pretty decent. But rotation is a pain! It looks terrible after a few rotates...

You can't conquer the game if you can't conquer yourself.

Jetru

Posts: 751
Joined: Sat Oct 18, 2003 11:46 pm
Location: Bangalore,India

Can you store your scene as a list of primitives, apply the transformations on the primitives and then redraw? The way you are doing it now, I don't think there is a good solution.

Alvaro
Moderator

Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Hmm Ok I should hit meself. This is wat my prof suggested...I thought it was stupid! I can do it, but it'll take a long time because my proggie isn't built that way...

You can't conquer the game if you can't conquer yourself.

Jetru

Posts: 751
Joined: Sat Oct 18, 2003 11:46 pm
Location: Bangalore,India