Skip to content

Instantly share code, notes, and snippets.

@d4vidB
Last active August 29, 2015 14:04
let floyd_steinberg matrix (width, height) style =
(*We convert the image in grey level according to style*)
let new_matrix = grey matrix (width, height) style and
old_color = ref 0 and new_color = ref 0 and error = ref 0 in
for i = 1 to width - 2 do
for j = 1 to height - 2 do
(*We save the color of the current pixel*)
old_color := get_r new_matrix.(i).(j);
if !old_color > 127 then
new_color := 255
else
new_color := 0;
(*We replace the current pixel by the new color*)
new_matrix.(i).(j) <- (!new_color, !new_color, !new_color);
(*We calculate the error value that will be distributed to the neighboring pixels*)
error := !old_color - !new_color;
let color1 = fix_rgb (get_r new_matrix.(i + 1).(j) + iof (0.4375 *. foi !error)) and
color2 = fix_rgb (get_r new_matrix.(i - 1).(j + 1) + iof (0.1875 *. foi !error)) and
color3 = fix_rgb (get_r new_matrix.(i).(j + 1) + iof (0.3125 *. foi !error)) and
color4 = fix_rgb (get_r new_matrix.(i + 1).(j + 1) + iof (0.0625 *. foi !error)) in
new_matrix.(i+1).(j) <- (color1, color1, color1);
new_matrix.(i-1).(j+1) <- (color2, color2, color2);
new_matrix.(i).(j+1) <- (color3, color3, color3);
new_matrix.(i+1).(j+1) <- (color4, color4, color4);
done;
done;
new_matrix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment