Friday 16 November 2012

Image composition

The following script shows different  way to compose an image, notice that the image in the upper right is interactive:
Here the source:
rebol[
    title: "Composition By Mask and Color Map"
    author: "James Marsden (aka Icarii)"
    date: 2003-05-24
    purpose: "To demonstrate different techniques for image composition"
]
;this is used to create an opacity mask for overlay
create-mask: func [mask c /local img [image!] v [integer!]][
    img: copy mask
    for v 1 (img/size/x * img/size/y) 1 [if not (pick mask v) = c [poke img v 0.0.0.255]]
    return img
]
;this is used to create an inverted opacity mask for overlay
create-not-mask: func [mask c /local img [image!] v [integer!]][
    img: copy mask
    for v 1 (img/size/x * img/size/y) 1 [if (pick mask v) = c [poke img v 0.0.0.255]]
    return img
]
;this is used to overlay an image onto another
merge-color: func [src dest c /local img [image!] v [integer!]][
    img: copy dest
    if (length? src) = (length? img)[
        for v 1 (img/size/x * img/size/y) 1 [if (pick img v) = c [poke img v pick src v]]      
    ]
    return img
]
merge-not-color: func [src dest c /local img [image!] v [integer!]][
    img: copy dest
    if (length? src) = (length? img)[
        for v 1 (img/size/x * img/size/y) 1 [if not (pick img v) = c [poke img v pick src v]]      
    ]
    return img
]
;load in test images
msk: load http://www.mustard.co.nz/rebol/vsml-insignia-mask.png
hil: load http://www.mustard.co.nz/rebol/vsml-insignia-hilight.png
norm: load http://www.mustard.co.nz/rebol/vsml-insignia-normal.png
;view some examples of masking and merging etc.
view center-face layout/size [
    backcolor pink
    origin 0x0
    space 0x0
    style boxen box 64x64 with [edge: none] effect [key 0.0.0.255]
    label 32.32.32 "Mask, Normal and Highlighted Images + An Interactive Image (red bordered image)" font [shadow: none name: font-sans-serif style: none]
    across
    boxen with [image: msk] tab
    boxen with [image: norm] tab
    boxen with [image: hil] tab
    boxen 68x68 edge [size: 2x2 color: red effect: 'bevel] with [image: norm mask: msk lastcolor: none] feel [
        detect: func [f e /local v [integer!] c [tuple!]][
            if find [move] e/type [
                v: (e/offset/x - f/offset/x) + ((e/offset/y - f/offset/y) * f/mask/size/x)
                c: pick f/mask v
                if not f/lastcolor = c [
                    f/lastcolor: c
                    ;f/image: create-mask msk c
                    f/image: merge-color hil merge-not-color norm msk c c
                ]
                show f
            ]
            e
        ]
    ]
    return
    below
    label 32.32.32 "Masking by color (with transparency)" font [shadow: none name: font-sans-serif style: none]
    across
    boxen with [image: create-mask msk 255.0.0] tab
    boxen with [image: create-mask msk 0.255.0] tab
    boxen with [image: create-mask msk 0.0.255] tab
    boxen with [image: create-mask msk 255.255.255] return
    below
    label 32.32.32 "Masking excluding color (with transparency)" font [shadow: none name: font-sans-serif style: none]
    across
    boxen with [image: create-not-mask msk 255.0.0] tab
    boxen with [image: create-not-mask msk 0.255.0] tab
    boxen with [image: create-not-mask msk 0.0.255] tab
    boxen with [image: create-not-mask msk 255.255.255]     return
    below
    label 32.32.32 "Compositing by color" font [shadow: none name: font-sans-serif style: none]
    across
    boxen with [image: merge-color hil msk 255.0.0.0] tab
    boxen with [image: merge-color hil msk 0.255.0.0] tab
    boxen with [image: merge-color hil msk 0.0.255.0] tab
    boxen with [image: merge-color hil msk 255.255.255.0]   return
    below
    label 32.32.32 "Compositing excluding color" font [shadow: none name: font-sans-serif style: none]
    across
    boxen with [image: merge-not-color hil msk 255.0.0.0] tab
    boxen with [image: merge-not-color hil msk 0.255.0.0] tab
    boxen with [image: merge-not-color hil msk 0.0.255.0] tab
    boxen with [image: merge-not-color hil msk 255.255.255.0] return
    below
    label 32.32.32 "Multi-level Compositing" font [shadow: none name: font-sans-serif style: none]
    across
   
    boxen with [image: merge-color norm merge-not-color hil msk 255.0.0.0 255.0.0.0] tab
    boxen with [image: merge-color norm merge-not-color hil msk 0.255.0.0 0.255.0.0] tab
    boxen with [image: merge-color norm merge-not-color hil msk 0.0.255.0 0.0.255.0] tab
    boxen with [image: merge-color norm merge-not-color hil msk 255.255.255.0 255.255.255.0] return
   
] 384x512

No comments:

Post a Comment