lundi 17 avril 2017

Loading Images with Red

Loading Images with Red

Red is able reading classical image formats such as bmp, png or jpeg. Tiff is not yet supported. Reading an image with Red is really easy.

request-file

request-file is a native function that gives access to file and directories. Calling request-file creates a window dialog that allows to select the required file. 



If a file is selected, the function returns a string for the file name and none if the user cancels the dialog.  A good practice  is to control that the return string is not none:

tmp: request-file
if not none? tmp [
              ... code
        ]

system/view/screen

Here is included a simple example that illustrates how to read and display images with Red.  Example is self-explanatory except a trick suggested by Didier Cadieu (thanks) that allows to resize image and window according to screen size:
scale: max 1 1 + max (2 * margins/x + rimg/size/x) / system/view/screens/1/size/x 
(4 * margins/y + sInfo/size/y + rimg/size/y + 25) / system/view/screens/1/size/y
Calling Red system/view/screens  gives access to the size (in pixels) of connected monitors to your computer. With this information it is really simple to calculate a scale value used to resize image and window.

loadImage Code

All job is done by loadImage function which controls the string returned by request-file and rescales the main window if necessary.

Red [
    Title:   "Load Image Red VID "
    Author:  "Francois Jouen "
    File:    %loadImage.red
    Needs:   'View
]


margins: 10x10

loadImage: does [
    canvas/image: none
    fName/text: ""
    tmp: request-file
    if not none? tmp [
        t1: now/time/precise
        rimg:  load to-file tmp ; loads image
        ; if image does not fit screen, scale it (Thanks to Didier Cadieu :)
        scale: max 1 1 + max (2 * margins/x + rimg/size/x) /  system/view/screens/1/size/x 
                (4 * margins/y + sInfo/size/y + rimg/size/y + 25) / system/view/screens/1/size/y
    
        win/text: form tmp ;??? pb with 0.6.2
        ; redim window with min size
        win/size/x: 2 * margins/x + max 500 rimg/size/x / scale
        win/size/y: 4 * margins/y + sInfo/size/y + 50 + max 150 rimg/size/y / scale
        ; redim image view
        canvas/size: rimg/size / scale
        ; update base contener
        canvas/image: rimg
        ; update  positions and infos
        
        sInfo/offset/y: win/size/y - margins/y - sInfo/size/y 
        sBar1/text: form rimg/size
        s: copy "1:" 
        append s form scale
        sBar2/text: s
        win/size/y: win/size/y + 25
        fName/size/x: win/size/x - 150
        fName/text: form win/text
        sBar3/data: third now/time/precise - t1
    ]
]

view win: layout [
    title "Load Images"
    origin margins space margins
    ; some stylization for a nice GUI
    style btn: button -1x-1
    style txt: text middle -1x20
    
    btn  "Load" [loadImage]
    btn  "Quit" [quit]
    fName: field 380
    return
    canvas: base 512x512 black 
    return
    sInfo: panel [origin 0x0 
            txt "Image size:" sBar1: field 80x20 
            txt "Scale:" sBar2: field 50x20
            txt "Rendered:" sBar3: field 80x20
    ]
]

and the result



Image is loaded and rescaled and the program gives some information about the image such its size and the rescale factor use for rendering the display. Enjoy!





Aucun commentaire:

Enregistrer un commentaire