dimanche 16 avril 2017

Red Image! Datatype

Red Image! Datatype


As many modern languages,  Red represents image as a very simple structure:

img: declare struct! [
   width   [integer!]
   height   [integer!]
   data      [pointer!]
]

Both integers give the size of the image and the pointer gives access to the content of the image.
Created and loaded images by Red are in ARGB format where A is the image transparency and RGB each channel for Red, Green and blue value. Red images are 4-channels. Image pixels are represented as tuple!
Images are 8-bit and internally use bytes [0..255] as a binary string of hexadecimal values. 

How to create an image with Red

This is trivial, use make image! as for example:
img: make image! reduce [640x480 red]. This instruction creates a 640x480 red pixels image.  You'll find in Red documentation (http://www.red-lang.org/p/documentation.html) all information you need for creating simple or sophisticated images.
Now let's try an exemple to generate random images. Basically we create a simple function which can generate a random colour for the whole image or a random value for all pixels of the image according to the refinement of the function. Then the retuned image generated by the function is updated in a base face.

Red [
    Title:   "Random test "
    Author:  "Francois Jouen"
    File:    %random.red
    Needs:   'View
]

rcvRandomImage: function [size [pair!] value [tuple!] /uniform /alea return: [image!]
"Create a random uniform or pixel random image"
][
    case [
        uniform [img: make image! reduce [size random value]]
        alea    [img: make image! reduce [size black] forall img [img/1: random value ]]
    ] 
    img
]


margins: 10x10

; ***************** Test Program ****************************
view win: layout [
        title "Rand Tests"
        button 80 "Uniform"  [canvas/image: rcvRandomImage/uniform 512x512 255.255.255.0]
        button 80 "Random" [canvas/image: rcvRandomImage/alea 512x512 255.255.255.0]
        button 50 "Quit" [quit]
        return
        canvas: base 512x512 black  
]





Aucun commentaire:

Enregistrer un commentaire