PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Stretch and Zoom

Fzammetti wrote:
In my mind, when I think of a stretch, I'm thinking of what amounts to a distortion effect... if I stretch an image horizontally right, the image's area would expand but only to the right... when I think of a scaling though, I'm thinking zoom actually, as in a magnification of the image that takes up the same screen area (or alternatively is expanded, which could mean a stretch).

Let me ask this of you and anyone else: is there an example of the stretch function I could see? It is possible what I'm thinking of is already there in the stretch function and I just don't realize it.


Ok, here's how it's done. I havn't compiled this code so it might not work but atleast it'll give you the general idea of how it should be done and you can start from there.

The BlitStretch() functions stretches the image data found in the source rectangle so that it fits in the destination rectangle. So, for instance, if you do something like this:
Code:

Surface* image = LoadImage(display,"filename");
display->BlitStretch(&Rect;(0,0,64,32),image);
you will blit the image found in filename to the screen at x=0, y=0 and with the size of width=64, height=32. So, if the image was a 32x32 tile, it has now been stretched to twice it's size in the x direction.

What you wanted was a zoom effect. Imagine using PhotoShop (or some other graphical tool) and you select the zoom. You have to point somewhere on the image to zoom, and that point will be the new center of the view. You have to do the same thing if you want to zoom with PF. You need to find your zooming center.

So, first of all. You want to set the destination rectangle. Say you want to zoom in on the center of your image and display it at the upper left corner of the screen. Here's how you can do.
Code:
Rect destRect = Rect(0,0,image->GetWidth(), image->GetHeight);

Ok. Simple enough. Now, all you have to do is to set the source rectangle to half the size of the image and center it on the zoom point (the center of the image in our example).
Code:
int imageWidth = image->GetWidth();
int imageHeight = image->GetHeight();
Rect sourceRect = Rect(imageWidth/4, imageHeight/4, imageWidth*3/4, imageHeight*3/4);

What did we do here? Well, we knew we wanted to zoom the image a factor of two. And we wanted to zoom toward the center of the image. So, we constructed a destination rectangle with the same size as the image, and a source rectangle with half it's size. That way, every pixel in the source rectangle will be stretched to twice it's size and this is the zoom effect. To center the zoom, we just removed/added halv the size of the zoom rectangle to the zooming point. I could have written
Code:
Rect sourceRect = Rect(zoomX-imageWidth/4, zoomY-imageHeight/4, zoomX+imageWidth/4, zoomY+imageHeight/4)

to make it a bit more generic, where zoomX/zoomY are the coordinates of the zooming point (image/2)

Anyways.. This is sort of the way to do it, but as I mentioned earlier, I havn't compiled and tested the code, so you might have some initial problems setting it up. For instance, the center of the zoom might be off by a pixel. As far as I can initially see, and please correct me if I'm wrong here, the source and destination rectangles should be from the beginning of the first, upper left, pixel down to one row below and one column beyond the last, lower right, pixel, in order to blit the whole image.

What the #ยค%# did he just say?!? Well, look at the Blit demo for instance. The fps numbers are a bitmap of 10 numbers with the size x=8, y=11. And the Blit source rectangle is Rect(num*8,0,(num+1)*8,11). So, to make it easy, the first number rectangle reaches from the upper left pixel (0,0) to one more than the lower right (7+1,10+1).

Also, you can't center a pixel of even size (=2*n pixels where n is a positive integer) to a single pixel and depending on the purpose of this zoom effect, this also must be considered.

Hope this will help you out.
F'ing fantastic adde! Thank you VERY much for taking the time to write that up. I'm sure I'm not the only one that will find that very useful. I think now I can finally port that silly rotozoom loader I did for some BBS about ten years ago to PocketPC! :)

As soon as I get around to it I'll let you know if there are any problems when I try and use this. I suspect it'll go pretty well though.

Thanks again!
Conan · 17 Dec 2002, 11:38 UTC — My thanks also
I'm also very grateful for this post. It's a complex function without any real docs and I would have spent a few hours working it all out. If I ever get time I will turn out another article with worked examples ( if I ever get time )
It seems what is obvious for one (me) is not obvious to everyone =(.

StretchBlitt() works exactly like Win32's StrechBlt() function... It takes a source rect on the source surface and stretch it to fit inside the destination rect on the destination surface. It is your responsability to setup these two rectangles properly to get the desired effect.

Thank you adde for clearing this up.
Glad I could be of some assistance. :D
Conan · 30 Dec 2002, 15:19 UTC — additional info on stretch
When I tried the above code as it's written I received compiler errors on the Rect setup "display->BlitStretch(&Rect;(0,0,64,32),image);".
To add the stretch/zoom into the rotate demo I added 2 rects as shown into the rotate.h header file and added the code lines shown just before the first BlitRotated:-
Code:
Rect* ScreenRect;
Rect* SourceRect;
Rect ScreenRect(0,0,239,319);
int imageWidth = m_image->GetWidth();
int imageHeight = m_image->GetHeight();
Rect SourceRect(imageWidth/4, imageHeight/4, imageWidth*3/4, imageHeight*3/4);
display->BlitStretch(ScreenRect,m_image,&SourceRect;);

This zooms a part of the image being rotated to fill the screen as a background. I commented out the display clear as that was not needed anymore.
adde · 30 Dec 2002, 16:20 UTC — additional info on stretch
Conan wrote:
When I tried the above code as it's written I received compiler errors on the Rect setup "display->BlitStretch(&Rect;(0,0,64,32),image);".


I'm sorry Conan. I wrote the note briefly and didn't have time to compile it. As one can see in the file "rasterizer.h" the declaration of the BlitStretch function is like this:
Code:
void BlitStretch( const Rect& dest, const Surface* source, const Rect* srcRect = 0 );
which means that to make my code work, you need to remove the & operator on Rect. I thought that the dest.rect. was a pointer just as src.rect, but no.

If you plan to blit the whole source Surface, you don't need to suply a source rectangle. Notice the =0 in the declaration of BlitStretch. If you then take a look at the definition of BlitStretch, you'll notice that if the source rectangle equals zero, it will be calculated to have the same size as the source Surface.

Also, as you did Conan, one should initialize the Rects directly, like this:
Code:
Rect sourceRect(imageWidth/4, imageHeight/4, imageWidth*3/4, imageHeight*3/4);
and not by using the copy constructor (of RECT).

So, you don't need the first two lines in your code. Francly, I'm surprised you don't get redefinition error at compile time (or perhaps you only get warnings and choose to ignore them =).

And perhaps you should have Rect(0,0,240,320), as the rectangle ignores the last line and row.
Conan · 30 Dec 2002, 17:39 UTC — thanks for the additional feedback
I will have a further play once my PC starts working again ( XP re-install happening as I type elsewhere)

The reason I define the Rects & other variables in a separate .h module is to do with being able to reference them in any .cpp file within my project. I define everything up front & then the items can be accessed anywhere. Essentially I have a 'mygame.h' where just about everything is defined. This is all to do with writing a large game across multiple sources. It may not be the best way but it works fine.
fzammetti · 03 Jan 2003, 05:52 UTC — Ok, I feel stupid...
I must not be getting something here...

I'm doing the following:

Code:

Surface* imgPVL = LoadImage(display, IDR_PVL);
PV_Width = imgPVLogo->GetWidth();
PV_Height = imgPVLogo->GetHeight();
PV_X = (240 - PV_Width) / 2;
PV_Y = (320 - PV_Height) / 2;
display->BlitStretch(Rect(PV_X, PV_Y, PV_Width, PV_Height), imgPVL);

What I'm EXPECTING to see is my image centered on the screen at it's real size. It should look, or so I think, just like a regular blit.

What I'm seeing though is the image at I think the correct screen coordinates to be centered, but it looks to be half-sized in both directions.

I don't quite see why this would be, but I'm sure I'm going to feel incredibly stupid once someone points out what I expect is something obvious.

Any clues? Thanks all!
fzammetti · 03 Jan 2003, 06:00 UTC — Forget it..
Never mind... I defined the rect wrong... needed to be PV_X, PV_Y, PV_Width + PV_X, PV_Height + PV_Y.

Told you it would be stupid and obvious! DOH!
Wow, this is some good stuff, and makes life easier, PocketFrog.

I intend to find some time (that's the real challenge for me, unfortunately), and make a sprite class that'll automatically build a lookup table at declaration of the sprite so that you can push an integer from 0 to 64 as the "Z" coordinate with 32 being the normal size/distance should a scaling flag be set (so the routine will draw to a destination rectangle on every frame).

It's actually pretty simple, I can't wait to get into this. I finally have to learn Windows programming...I'm certainly not too thrilled with eVB performance, my Pocket Circus game runs darned slow on some Pocket PC's.
Friggin Joe · 13 Jan 2003, 18:20 UTC
Oops, didn't log in.