![]() |
|
Stretching an Image |
someone972
Member #7,719
August 2006
![]() |
How do I keep the aspect ratio of an image when using stretch_blit???? ______________________________________ |
Matthew Leverton
Supreme Loser
January 1999
![]() |
By multiplying the height and width by the same value. |
FMC
Member #4,431
March 2004
![]() |
You just need to multiply both height and width by the same number, to keep the proportion. stretch_blit(source, dest, 0, 0, source->w, source->h, dest_x, dest_y, source->w*2, source->h*2); //stretch_blit(BITMAP *source, BITMAP *dest, int source_x, source_y, source_width, source_height, int dest_x, dest_y, dest_width, dest_height);
[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites] |
someone972
Member #7,719
August 2006
![]() |
Still a little unclear on this... ______________________________________ |
FMC
Member #4,431
March 2004
![]() |
What? The code i posted IS an example... [FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites] |
Neil Walker
Member #210
April 2000
![]() |
erm, pretend you wish to scale it by a factor of 5, for the dest_width and dest_height, multiply the original bitmap width and height by 5 Neil. wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie |
Albin Engström
Member #8,110
December 2006
![]() |
FMC just did.. by multiplying with the same value you keep the aspect ratio.. what's so hard too understand? |
someone972
Member #7,719
August 2006
![]() |
Sorry i posted while you posted, see edit above. So I would do float aspect_ratio = bitmap->w/bitmap->h ______________________________________ |
Albin Engström
Member #8,110
December 2006
![]() |
SCREEN_W / image->w..(is the multiply value) this will fill the image on the horizontal axis.. |
someone972
Member #7,719
August 2006
![]() |
Sorry about all the repeat questions, I keep writing a post while others are being put on. Thanks for the help!8-) ______________________________________ |
FMC
Member #4,431
March 2004
![]() |
Do something like: int sW,sH; sW = SCREEN_W / source->w; sH = SCREEN_H / source->h; if(sW < sH) // you must multiply for the SMALLEST size stretch_blit(source, dest, 0, 0, source->w, source->h, dest_x, dest_y, source->w*sW, source->h*sW); else stretch_blit(source, dest, 0, 0, source->w, source->h, dest_x, dest_y, source->w*sH, source->h*sH);
[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites] |
BAF
Member #2,981
December 2002
![]() |
Em, that code won't work, FMC. This is probably closer to what he wants (you don't want to just multiply by the screen width/height, you would get a HUGE image). [edit] Did you change your code? I didn't see the division in your sw/sh before. Anyway, your code still won't work, because it will come out as an integer, not a float. |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
someone972 said: I want the picture to fill either the width or height of the screen. How do I find what number to multiply them by. I am also using many different sizes of images. If the aspect ratio of the screen and the image are not the same , you need to adjust the scale accordingly. Find the ratio of screen to source bitmap for both x and y. Now use the smaller of the two for the actual stretching ratio. Okay so take for example a tall picture like 100 X 200 using a screen of 640 X 480. Since the smaller ratio is screen_y to source_y and stretch_blit doesn't accept a ratio as its parameter , you need to scale the x yourself. and since the height of the screen is completely filled , the destination height Note : static_cast<int> only truncates the decimal portion , you may want to round instead. If you want to center it then subtract the destination_width from the screen_width and divide by 2 to find the left or top position. Since it's height already takes up the whole screen , destination_y will just be 0. So for this case use : This will be scaled up to the maximum size that will fit in the screen and will retain its original aspect ratio. Now you just need to take into account the other cases (where screen_to_source ratio's of x and y are equal and where the x ratio is smaller). My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
BAF
Member #2,981
December 2002
![]() |
I just posted the code that does that. |
someone972
Member #7,719
August 2006
![]() |
Thanks everyone for your help! I'm still having a few issues, but I'll work them out.;D ______________________________________ |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
BAF said: I just posted the code that does that. BAF's code restated : This is not the same as :
BAF - Quick example using your code It's nice when things can be coded nice and simply , but it just didn't work this time.:-/ My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
FMC
Member #4,431
March 2004
![]() |
Quote: [edit] Did you change your code? I didn't see the division in your sw/sh before. Anyway, your code still won't work, because it will come out as an integer, not a float. I blame the int thingy for it being 1am when i posted Quote: It's nice when things can be coded nice and simply , but it just didn't work this time What's wrong with my code? float sW,sH; sW = SCREEN_W / source->w; sH = SCREEN_H / source->h; if(sW < sH) // you must multiply for the SMALLEST size stretch_blit(source, dest, 0, 0, source->w, source->h, dest_x, dest_y, source->w*sW, source->h*sW); else stretch_blit(source, dest, 0, 0, source->w, source->h, dest_x, dest_y, source->w*sH, source->h*sH);
[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites] |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
FMC - int a = 3; int b = 4; cout << (a / b) << endl;// will print 0 - integer division truncates any decimal portion int x = 5; float y = 4.5; cout << (x*y) << endl;// will it print 20 or 22.5? Does y get converted to int type or does x get converted to float type to perform the multiplication? If the result ends up as float type , you passed a bad parameter unless the compiler supplies a conversion for you , which it shouldn't do in a function call. If it ends up as int type then the compiler (un)helpfully converted y from 4.5 to 4 for you which gives you 20 instead of 22.5 for the scaled up size. You need to cast the source->w and source->h to a float first , multiply by the appropriate scale and then cast it back to an integer. This may still give you an off by one error due to truncation in the cast back to an integer. This wouldn't be very noticeable in the smaller dimension of the magnified picture but it might be noticeable in the larger dimension (the one that gets magnified to SCREEN_W or SCREEN_H). Imagine the screen was a sub-bitmap of the screen and had been outlined nicely but then there was a black line just above the bottom border. No fun. Your way is actually a lot simpler than mine , I must not have read your post closely enough. Sorry.:-/ My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
aj5555
Member #9,033
September 2007
|
why does stretch_blit() crash when you pass is out-of-bounds values, shouldn't it just clip ? |
ImLeftFooted
Member #3,935
October 2003
![]() |
I wish you were coding a webpage, so i could just say: convert 900x800 input.png output.png
But alas, it sounds like you aren't, so heres some code that should do what convert would have done in that scenario, plus some added centering.
The code hasn't been compiled or tested so YMMV, but you hopefully get the idea. |
|