Script Library: 1213 scripts
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 
View scriptLicenseDownload documentation as: HTML or editable
Download scriptHistoryOther scripts by: vincentecuye

Documentation for: oneliner-posterize.r


REBOL

Posterize

Author: Vincent Ecuyer
Date: 31-Jan-2013
File: oneliner-posterize.r

Contents:

1. Purpose
2. Usage
3. Examples
4. Commented Code

1. Purpose

A short function for creating 'posterization' effects on images

2. Usage

'posterize applies a type of color reduction on images, ignoring the less significative bits of each color channel, the result having flat colors zones instead of gradual transitions.

result: posterize value (image!) depth (integer!)

where 'depth is between 1 and 7, with 1 keeping only 8 colors, and 7 with the less noticeable effect. The result is darker so a brightness correction would be appropriate: for the strongest setting, with depth = 1 (8 colors), half the brightness is lost, so applying a VID 'effect like “multiply 255” would do the trick.

Other 'depth values:

  • depth = 0 or less gives a black result (0 bit/color channel),
  • depth = 8 does nothing (8 bits/color channel as the original),
  • depth > 8 gives a false color effect (but darker so one should compensate with 'multiply or similar).

3. Examples

; save a posterized version of a picture 
save/png %img-dest.png posterize load %img-src.png 3 

; directly used in a display 
view layout [
    image posterize load http://www.rebol.com/view/demos/nyc.jpg 1 
        effect [multiply 255]
]

4. Commented Code

posterize: func [
    "Applies a posterize effect (color reduction) and returns the result."
    value [image!] "Source image."
    depth [integer!] "Color depth: bits / color channel." 
    /local t
][
    ; ands the source with a mask having only the most significant bits left 
    value
    and
    make image! reduce [
        ; same size as the source
        value/size

        ; trashes the n (= depth) least significant bits with an
        ; integer division and multiplication
        255.255.255 / (t: 2 ** (8 - depth)) * t
    ]
]
MakeDoc2 by REBOL- 31-Jan-2013