Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Another magnification routine (UPDATED)
#2
(04-17-2024, 04:11 AM)TerryRitchie Wrote: I wrote an image magnification routine that magnifies an image, not the screen. I know there are other very well written magnification routines on the forum. However, I needed one that settings could be changed that affect the behavior of the magnifier screen. I also needed a routine that would maintain the aspect ratio of an image as seen on the screen. 

The image to magnify must at least match the screen dimensions (ie stretching a small image onto a large screen will give undesirable results). You can use the 2080x2080 image I provided in the ZIP file below to test the code.

This is what I came up with. It could easily be adapted to a simple library if needed.

Code: (Select All)
'+---------------------------------------------------------------------+
'| Image Magnifier Proof of Concept                                    |
'| User has control over many of the settings for the magnifier        |
'| (See the "Set magnifier screen variables" section below.            |
'|                                                                     |
'| By Terry Ritchie  04/16/24                                          |
'|                                                                     |
'| The image and screen must be at least the same dimensions.          |
'| Using an image that is smaller than the screen and then stretched   |
'| will yeild undesirable results.                                     |
'|                                                                     |
'| Images that have been stretched to a screen with a different aspect |
'| ratio than the image is ok. The magnified image will retain the new |
'| aspect ratio.                                                       |
'|                                                                     |
'| This routine does not magnify the screen, it magnifies the image.   |
'+---------------------------------------------------------------------+

OPTION _EXPLICIT '          declare those variables!

'+------------------------------------------------------------------+
'| Magnification will work regardless of screen aspect ratio chosen |
'+------------------------------------------------------------------+

CONST SCREENWIDTH% = 960 '  screen width   (widescreen 16:9 ratio)
CONST SCREENHEIGHT% = 540 ' screen height

'CONST SCREENWIDTH% = 1024 ' screen width   (4:3 ratio)
'CONST SCREENHEIGHT% = 768 ' screen height

'CONST SCREENWIDTH% = 800 '  screen width    (square 1:1 ratio)
'CONST SCREENHEIGHT% = 800 ' screen height

TYPE MAGSCREEN '            MAGNIFIER SCREEN PROPERTIES
    Image AS LONG '         image to magnify '                                    (set)
    MagImage AS LONG '      magnified image holder                                (calculate)
    Width AS INTEGER '      magnifier screen width  (not including border)        (set)
    Height AS INTEGER '     magnifier screen height (not including border)        (set)
    MagWidth AS INTEGER '   magnifier screen width  (including border)            (calculate)
    MagHeight AS INTEGER '  magnifier screen height (including border)            (calculate)
    Border AS INTEGER '     size of border around magnifier screen in pixels      (set)
    Magnify AS SINGLE '     magnification level (1=100%, 1.5=150%, 2-200%, etc..) (set)
    MagX AS INTEGER '       upper left x coordinate of magnifier screen           (set)
    MagY AS INTEGER '       upper left y coordinate of magnifier screen           (set)
    MinX AS INTEGER '       minimum x coordinate of magnifier screen              (set)
    MaxX AS INTEGER '       maximum x coordinate of magnifier screen              (set)
    MinY AS INTEGER '       minimum y coordinate of magnifier screen              (set)
    MaxY AS INTEGER '       maximum y coordinate of magnifier screen              (set)
    ScreenX AS INTEGER '    x center point coordinate on screen                   (calculate)
    ScreenY AS INTEGER '    y center point coordinate on screen                   (calculate)
    ImageX AS INTEGER '     upper left x coordinate on image to be magnified      (calculate)
    ImageY AS INTEGER '     upper left y coordinate on image to be magnified      (calculate)
    RatioX AS SINGLE '      ratio of image width to screen width                  (calculate)
    RatioY AS SINGLE '      ratio of image height to screen height                (calculate)
    LengthX AS INTEGER '    width of area on image to magnify                     (calculate)
    LengthY AS INTEGER '    height of area on image to magnify                    (calculate)
END TYPE

'+-------------------+
'| Declare variables |
'+-------------------+

DIM MagScreen AS MAGSCREEN '           define the magnifier screen properties
DIM Image AS LONG '                    the image to magnify

'+-------------------------------------------------------+
'| Set magnifier screen variables (user defined options) |
'+-------------------------------------------------------+

Image = _LOADIMAGE("qb64pe.png", 32) ' load 2080x2080 image to magnify
MagScreen.Width = 200 '                set width  of magnifier screen (not including border)
MagScreen.Height = 200 '               set height of magnifier screen (not including border)
MagScreen.Border = 10 '                set border width/height around magnifier screen in pixels
MagScreen.Magnify = 2 '                set magnification (200%)
MagScreen.MinX = 0 '                   set minimum x coordinate of magnifier screen (including border)
MagScreen.MaxX = SCREENWIDTH - 1 '     set maximum x coordinate of magnifier screen (including border)
MagScreen.MinY = 0 '                   set minimum y coordinate of magnifier screen (including border)
MagScreen.MaxY = SCREENHEIGHT - 1 '    set maximum y coordinate of magnifier screen (including border)
MagScreen.Image = Image '              set image to magnify

'+--------------------------------------+
'| Calculate magnifier screen variables |
'+--------------------------------------+

MagScreen.MagImage = _NEWIMAGE(MagScreen.Width + MagScreen.Border * 2, MagScreen.Height + MagScreen.Border * 2, 32) ' magnified image holder
MagScreen.MagWidth = _WIDTH(MagScreen.MagImage) '                                            width of magnifier screen  (including border)
MagScreen.MagHeight = _HEIGHT(MagScreen.MagImage) '                                          height of magnifier screen (including border)
MagScreen.RatioX = _WIDTH(Image) / SCREENWIDTH '                                             ratio of screen width to image width
MagScreen.RatioY = _HEIGHT(Image) / SCREENHEIGHT '                                           ratio of screen height to image height
MagScreen.LengthX = MagScreen.Width * MagScreen.RatioX / MagScreen.Magnify '                 width of area on image to magnify
MagScreen.LengthY = MagScreen.Height * MagScreen.RatioY / MagScreen.Magnify '                height of area on image to magnify

CLS , _RGB32(0, 0, 0), MagScreen.MagImage '                                                  remove transparency from the magnifier screen
SCREEN _NEWIMAGE(SCREENWIDTH, SCREENHEIGHT, 32) '                                            enter a graphics screen

DO
    _LIMIT 30 '                                                                              30 frames per second
    _PUTIMAGE , Image, 0 '                                                                   clear the screen with image to magnify
    WHILE _MOUSEINPUT: WEND '                                                                get latest mouse update
    MagScreen.ScreenX = _MOUSEX '                                                            current x coordinate location of mouse pointer
    MagScreen.ScreenY = _MOUSEY '                                                            current y coordinate location of mouse pointer
    MagScreen.ImageX = MagScreen.ScreenX * MagScreen.RatioX - MagScreen.LengthX * .5 '       left x coordinate on image to be magnified
    MagScreen.ImageY = MagScreen.ScreenY * MagScreen.RatioY - MagScreen.LengthY * .5 '       upper y coordinate on image to be magnified
    IF MagScreen.ImageX < 0 THEN MagScreen.ImageX = 0 '                                      left x coordinate must be at least 0
    IF MagScreen.ImageY < 0 THEN MagScreen.ImageY = 0 '                                      upper y coordinate must be at least 0
    IF MagScreen.ImageX + MagScreen.LengthX > _WIDTH(Image) - 1 THEN '
        MagScreen.ImageX = _WIDTH(Image) - 1 - MagScreen.LengthX '                           maximum upper lext x allowed
    END IF
    IF MagScreen.ImageY + MagScreen.LengthY > _HEIGHT(Image) - 1 THEN
        MagScreen.ImageY = _HEIGHT(Image) - 1 - MagScreen.LengthY '                          maximum upper left y allowed
    END IF
    MagScreen.MagX = MagScreen.ScreenX - MagScreen.Width * .5 - MagScreen.Border '           upper left x coordinate of magnifier screen
    MagScreen.MagY = MagScreen.ScreenY - MagScreen.Height * .5 - MagScreen.Border '          upper left y coordinate of magnifier screen
    IF MagScreen.MagX < MagScreen.MinX THEN '                                                x above minimum limit?
        MagScreen.MagX = MagScreen.MinX '                                                    no, set x coordinate to minimum allowed
    ELSEIF MagScreen.MagX + MagScreen.Width + MagScreen.Border * 2 > MagScreen.MaxX THEN '   x below maximum limit?
        MagScreen.MagX = MagScreen.MaxX + 1 - MagScreen.Width - MagScreen.Border * 2 '       no, set x coordinate to maximum allowed
    END IF
    IF MagScreen.MagY < MagScreen.MinY THEN '                                                y above minimum limit?
        MagScreen.MagY = MagScreen.MinY '                                                    no, set y coordinate to minimum allowed
    ELSEIF MagScreen.MagY + MagScreen.Height + MagScreen.Border * 2 > MagScreen.MaxY THEN '  y below maximum limit?
        MagScreen.MagY = MagScreen.MaxY + 1 - MagScreen.Height - MagScreen.Border * 2 '      no, set y coordinate to maximum allowed
    END IF
    _PUTIMAGE (MagScreen.Border, MagScreen.Border)-_
              (MagScreen.MagWidth - 1 - MagScreen.Border, MagScreen.MagHeight - 1 - MagScreen.Border),_
              Image, MagScreen.MagImage,_
              (MagScreen.ImageX, MagScreen.ImageY)-_
              (MagScreen.ImageX + MagScreen.LengthX, MagScreen.ImageY + MagScreen.LengthY) ' get magnified area and place in magnifier screen
    _PUTIMAGE (MagScreen.MagX, MagScreen.MagY), MagScreen.MagImage '                         display the magnified screen
    _DISPLAY '                                                                               update screen with changes
LOOP UNTIL _KEYDOWN(27) '                                                                    leave when ESC key pressed
_FREEIMAGE Image '                                                                           remove image to magnify from RAM
_FREEIMAGE MagScreen.MagImage '                                                              remove magnified image from RAM
SYSTEM '                                                                                     return to the operating system

Smooooth! Vey nice.
I probably won't use it, but I think there are a few "Graphicators" who will.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply


Messages In This Thread
RE: Another magnification routine - by PhilOfPerth - 04-17-2024, 05:46 AM



Users browsing this thread: 1 Guest(s)