langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
14
Task/Bitmap-Histogram/OCaml/bitmap-histogram-1.ocaml
Normal file
14
Task/Bitmap-Histogram/OCaml/bitmap-histogram-1.ocaml
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
type histogram = int array
|
||||
|
||||
let get_histogram ~img:gray_channel =
|
||||
let width = Bigarray.Array2.dim1 gray_channel
|
||||
and height = Bigarray.Array2.dim2 gray_channel in
|
||||
let t = Array.make 256 0 in
|
||||
for x = 0 to pred width do
|
||||
for y = 0 to pred height do
|
||||
let v = gray_get_pixel_unsafe gray_channel x y in
|
||||
t.(v) <- t.(v) + 1;
|
||||
done;
|
||||
done;
|
||||
(t: histogram)
|
||||
;;
|
||||
15
Task/Bitmap-Histogram/OCaml/bitmap-histogram-2.ocaml
Normal file
15
Task/Bitmap-Histogram/OCaml/bitmap-histogram-2.ocaml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
let histogram_median (h : histogram) =
|
||||
|
||||
let from = 0 and to_ = 255 in
|
||||
let left = h.(from) and right = h.(to_) in
|
||||
|
||||
let rec aux from to_ left right =
|
||||
if from = to_
|
||||
then (from)
|
||||
else
|
||||
if left < right
|
||||
then aux (succ from) to_ (left + h.(from)) right
|
||||
else aux from (pred to_) left (right + h.(to_))
|
||||
in
|
||||
aux from to_ left right
|
||||
;;
|
||||
24
Task/Bitmap-Histogram/OCaml/bitmap-histogram-3.ocaml
Normal file
24
Task/Bitmap-Histogram/OCaml/bitmap-histogram-3.ocaml
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
let () =
|
||||
let img = read_ppm ~filename:"/tmp/foo.ppm" in
|
||||
|
||||
let width, height = get_dims img in
|
||||
let res = new_img ~width ~height in
|
||||
|
||||
let g_img = to_grayscale ~img in
|
||||
let h = get_histogram g_img in
|
||||
let m = histogram_median h in
|
||||
|
||||
let light = (255, 255, 0)
|
||||
and dark = (127, 0, 127) in
|
||||
|
||||
for x = 0 to pred width do
|
||||
for y = 0 to pred height do
|
||||
let v = gray_get_pixel_unsafe g_img x y in
|
||||
if v > m
|
||||
then put_pixel_unsafe res light x y
|
||||
else put_pixel_unsafe res dark x y
|
||||
done;
|
||||
done;
|
||||
|
||||
output_ppm ~oc:stdout ~img:res;
|
||||
;;
|
||||
Loading…
Add table
Add a link
Reference in a new issue