Last active
June 15, 2019 11:47
-
-
Save Lupus/33331edf6de6fd6feec8a929e3020d41 to your computer and use it in GitHub Desktop.
Some simple set of functions to manipulate RGB images in ReasonML (only PPM output format is supported)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Image = { | |
type channel_t = | |
Bigarray.Array2.t(char, Bigarray.int8_unsigned_elt, Bigarray.c_layout); | |
type t = { | |
width: int, | |
height: int, | |
r_channel: channel_t, | |
g_channel: channel_t, | |
b_channel: channel_t, | |
}; | |
let make = (width, height) => { | |
let make_channel = () => | |
Bigarray.(Array2.create(char, c_layout, width, height)); | |
{ | |
width, | |
height, | |
r_channel: make_channel(), | |
g_channel: make_channel(), | |
b_channel: make_channel(), | |
}; | |
}; | |
let set_rgb = (img, ~x, ~y, ~rgb as (r, g, b)) => { | |
(img.r_channel).{x, y} = char_of_int(r); | |
(img.g_channel).{x, y} = char_of_int(g); | |
(img.b_channel).{x, y} = char_of_int(b); | |
}; | |
let fill = (img, ~rgb) => { | |
for (y in 0 to pred(img.height)) { | |
for (x in 0 to pred(img.width)) { | |
set_rgb(img, ~x, ~y, ~rgb); | |
}; | |
}; | |
}; | |
let get_rgb = (img, ~x, ~y) => { | |
( | |
(img.r_channel).{x, y} |> int_of_char, | |
(img.g_channel).{x, y} |> int_of_char, | |
(img.b_channel).{x, y} |> int_of_char, | |
); | |
}; | |
let output_ppm = (img, oc) => { | |
Printf.fprintf(oc, "P6\n%d %d\n255\n", img.width, img.height); | |
for (y in 0 to pred(img.height)) { | |
for (x in 0 to pred(img.width)) { | |
Out_channel.output_char(oc, (img.r_channel).{x, y}); | |
Out_channel.output_char(oc, (img.g_channel).{x, y}); | |
Out_channel.output_char(oc, (img.b_channel).{x, y}); | |
}; | |
}; | |
Out_channel.output_char(oc, '\n'); | |
Out_channel.flush(oc); | |
}; | |
}; | |
module Thick_line_image = { | |
include Image; | |
let height = 30; | |
let make = width => Image.make(width, height); | |
let set_rgb = (img, ~x, ~rgb) => { | |
for (y in 0 to pred(height)) { | |
Image.set_rgb(img, ~x, ~y, ~rgb); | |
}; | |
}; | |
let set_rgb_range = (img, ~x_range as (x_start, x_end), ~rgb) => { | |
for (x in x_start to x_end) { | |
set_rgb(img, ~x, ~rgb); | |
}; | |
}; | |
let get_rgb = (img, ~x, ~y) => { | |
Image.get_rgb(img, ~x, ~y=0); | |
}; | |
}; | |
module Colors = { | |
let aqua = (0, 255, 255); | |
let black = (0, 0, 0); | |
let blue = (0, 0, 255); | |
let fuchsia = (255, 0, 255); | |
let gray = (128, 128, 128); | |
let green = (0, 128, 0); | |
let lime = (0, 255, 0); | |
let maroon = (128, 0, 0); | |
let navy = (0, 0, 128); | |
let olive = (128, 128, 0); | |
let purple = (128, 0, 128); | |
let red = (255, 0, 0); | |
let silver = (192, 192, 192); | |
let teal = (0, 128, 128); | |
let white = (255, 255, 255); | |
let yellow = (255, 255, 0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment