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.
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
]
]