Retaining image aspect ratio

8 posts / 0 new
Last post
jap
jap's picture
Offline
Last seen: 2 weeks 3 days ago
Joined: 2011-06-04 05:53
Retaining image aspect ratio
Hi, Can a bitmap image object be made scalable so that the image's aspect ratio stays always the same? Possible with the help of the surrounding layout gadget? Right now I have an image button which adapts to the window size: when I resize the window, the image button resizes as well filling the whole window. The problem is that the image gets distorted when the window has "wrong dimensions". That is something I'd like to avoid.
thomas
thomas's picture
Offline
Last seen: 3 days 5 hours ago
Joined: 2011-05-16 14:23
Re: Retaining image aspect ratio
Could you post a piece of code? (A working example, not an excerpt.) How do you get the image scaled? Perhaps it's easy to add acpect handling, perhaps not. Depends on what you have so far.
jap
jap's picture
Offline
Last seen: 2 weeks 3 days ago
Joined: 2011-06-04 05:53
Re: Retaining image aspect ratio
Unfortunately it's part of a larger program already and tied to global settings. However, this is what I've done. First I load the image:
  1. pImageObject = BitMapObject,
  2. BITMAP_SourceFile, DEF_IMAGE_FILE,
  3. BITMAP_Screen, pScreen,
  4. IA_Scalable, TRUE,
  5. BitMapEnd;
Then I calculate minimum size for the image (ulImageMinWidth and ulImageMinHeight) and use the minimum width and height as a "normal" image size, i.e. I make the image smaller. I noticed that this is required in order to scaling to work.
  1. /* Set image size */
  2. IIntuition->SetAttrs (
  3. pImageObject,
  4. IA_Width, ulImageMinWidth,
  5. IA_Height, ulImageMinHeight,
  6. TAG_DONE );
Then I create and open a window.
  1. pWindowObject =
  2. WA_PubScreen, pScreen,
  3. WA_MinWidth, ulImageMinWidth,
  4. WA_MinHeight, ulImageMinHeight,
  5. WA_Width, ulWindowInitWidth, /* Make the window wider than the minimum width. */
  6. WA_Height, ulWindowInitHeight, /* Make the window higher than the minimum height. */
  7. WA_SizeGadget, TRUE,
  8. WA_DragBar, TRUE,
  9. WA_DepthGadget, TRUE,
  10. WA_CloseGadget, TRUE,
  11. WA_Activate, TRUE,
  12. WA_GimmeZeroZero, TRUE,
  13. WINDOW_Position, WPOS_CENTERSCREEN,
  14. /* Gadgets */
  15. WINDOW_ParentGroup, VLayoutObject,
  16. LAYOUT_AddChild, ButtonObject,
  17. GA_ID, GID_IMAGE,
  18. GA_RelVerify, TRUE,
  19. GA_TabCycle, FALSE,
  20. BUTTON_BevelStyle, BVS_NONE,
  21. BUTTON_RenderImage, pImageObject,
  22. ButtonEnd,
  23. End,
  24. End;
  25.  
  26. pWindow = RA_OpenWindow ( pWindowObject );
Now when you resize the window, the image stretches accordingly filling the whole window.
salass00
salass00's picture
Offline
Last seen: 4 days 11 hours ago
Joined: 2011-02-03 11:27
Re: Retaining image aspect ratio
If you don't want the button to be sized over it's minimum size just set CHILD_WeightedWidth and CHILD_WeightedHeight to 0 (note that these tags are for the layout containing the button and should be placed right after the LAYOUT_AddChild tag).
jap
jap's picture
Offline
Last seen: 2 weeks 3 days ago
Joined: 2011-06-04 05:53
Re: Retaining image aspect ratio
Thanks but I don't want to prevent that. I have an image which is larger than the screen. The goal is to display the whole image in a window and retain its aspect ratio. User can resize the window and the image should resize as well: fill the window but keep the aspect ratio. If for example the user resizes the window so that it's too tall for the image, image is scaled so that it fills the window horizontally and empty space is added above and below the image so that image's aspect ratio doesn't change. The above code fragments make the image fill the whole window both horizontally and vertically, which is not good because the image loses its aspect ratio.
thomas
thomas's picture
Offline
Last seen: 3 days 5 hours ago
Joined: 2011-05-16 14:23
Re: Retaining image aspect ratio
This functionality is not (yet) in bitmap.image. You could write your own image class which keeps the aspect ratio when scaling the image. Here is an example: http://thomas-rapp.homepage.t-online.de/examples/scaleimg.lha There is also an example I made some time ago of a gadget class which scales a given image: http://forum.hyperion-entertainment.biz/viewtopic.php?f=26&t=1414#p16948
jap
jap's picture
Offline
Last seen: 2 weeks 3 days ago
Joined: 2011-06-04 05:53
Re: Retaining image aspect ratio
Thanks thomas! I solved the problem by enclosing the image button with a layout gadget and modifying the layout gadget's LAYOUT_LeftSpacing, LAYOUT_RightSpacing, LAYOUT_TopSpacing, and LAYOUT_BottomSpacing settings when the window size changes. However, it doesn't work perfectly. When you resize the window very fast, Reaction sometimes gets confused and fails to update the gadgets properly resulting oversized left or top spacing or distorted image. Your image class example seems to work much better. If I can't fix the update problem, I'll use your code. Thanks again!
thomas
thomas's picture
Offline
Last seen: 3 days 5 hours ago
Joined: 2011-05-16 14:23
Re: Retaining image aspect ratio
Here is another solution: http://thomas-rapp.homepage.t-online.de/examples/scalespace.c It uses a space.gadget instead of a button and draws the image inside its render hook.
Log in or register to post comments