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-image-to-pgm.r


REBOL

oneliner-image-to-pgm.r

Author: Vincent Ecuyer
Date: 30-Jan-2013

Contents:

1. to-pgm
1.1 Purpose
1.2 Usage
1.3 Example
1.4 Commented Code
2. alpha-to-pgm
2.1 Purpose
2.2 Usage
2.3 Example
2.4 Commented Code

1. to-pgm

1.1 Purpose

Short code to save an rgb channel from an image! as a gray image in PGM binary (P5) format. It's mostly useful for interacting with the Netpbm tools.

1.2 Usage

to-pgm value (image!) channel ('r, 'g, or 'b)

1.3 Example

In REBOL 2:

write/binary %imageTest-red.pgm to-pgm logo.gif 'r 
write/binary %imageTest-green.pgm to-pgm logo.gif 'g 
write/binary %imageTest-blue.pgm to-pgm logo.gif 'b

In REBOL 3, 'write is always in binary:

write %imageTest.pgm to-pgm make image! [320x256 127.0.0] 'r

1.4 Commented Code

to-pgm: func [
    "Converts an image RGB channel to a PGM binary format"
    value [image!] "Image source"
    channel [word!] "Selected channel: 'r, 'g, or 'b"
] [
    ; equivalent to "to-binary rejoin" : we want a binary! result
    join #{} [

        ; the binary PGM file header, followed by a <space>
        "P5 "

        ; the image size, in the form width <space> height
        replace form value/size "x" " " 

        ; a <space>, the max gray value, and a <newline>
        " 255^(0A)" 

        ; selects only one channel (r, g, or b) and skips others
        extract/index value/rgb 3 index? find [r g b] channel
    ]
]

2. alpha-to-pgm

2.1 Purpose

Short code to save the alpha channel of a as a gray image in PGM binary (P5) format. It's useful as a companion for a PPM rgb file, as the PPM format can't store alpha/transparency data with the rgb.

2.2 Usage

alpha-to-pgm value (image!)

2.3 Example

write/binary %imageTest-alpha.pgm alpha-to-pgm logo.gif

2.4 Commented Code

alpha-to-pgm: func [
    "Converts the alpha channel of an image to a PGM binary format"
    value [image!] "Image source"
] [
    ; equivalent to "to-binary rejoin" : we want a binary! result
    join #{} [

        ; the binary PGM file header, followed by a <space>
        "P5 " 

        ; the image size, in the form width <space> height
        replace form value/size "x" " " 

        ; a <space>, the max gray value, and a <newline>
        " 255^(0A)" 

        ; the alpha channel
        value/alpha
    ]
]
MakeDoc2 by REBOL- 29-Jan-2013