package camlimages

  1. Overview
  2. Docs
Image processing library

Install

dune-project
 Dependency

Authors

Maintainers

Sources

camlimages-5.0.4.tar.bz2
sha256=1c9a68bdc3d348c9f859d490dadf384926213e47a584159832f7fc4a20242865
md5=1ddba74d210b86a899b5d6565f45c2dc

doc/src/camlimages.core/colorhist.ml.html

Source file colorhist.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
(***********************************************************************)
(*                                                                     *)
(*                           Objective Caml                            *)
(*                                                                     *)
(*            François Pessaux, projet Cristal, INRIA Rocquencourt     *)
(*            Pierre Weis, projet Cristal, INRIA Rocquencourt          *)
(*            Jun Furuse, projet Cristal, INRIA Rocquencourt           *)
(*                                                                     *)
(*  Copyright 1999-2004,                                               *)
(*  Institut National de Recherche en Informatique et en Automatique.  *)
(*  Distributed only by permission.                                    *)
(*                                                                     *)
(***********************************************************************)

(* $Id: colorhist.ml,v 1.1 2006/11/28 15:43:28 rousse Exp $ *)

open Color

type t = int array

let create () = Array.make 256 0

let total_samples t = Array.fold_left (fun st x -> st + x) 0 t

let store_sample t rgb =
  let brightness = Color.brightness rgb in
  t.(brightness) <- t.(brightness) + 1

let normalize keep t =
  let total = total_samples t in
  if total = 0 then raise (Failure "histgram is empty");
  let cut_samples = truncate ((float total) *. (1.0 -. keep) /. 2.0) in

  let rec find_limit update sum b =
    let sum = sum + t.(b) in
    if sum > cut_samples then b
    else find_limit update sum (update b)
  in

  let min = find_limit ((+) 1) 0 0 
  and max = find_limit ((+) (-1)) 0 255
  in

  if max - min = 0 then fun x -> x
  else
    let new_b_array = Array.init 256 (fun b ->
      if b <= min then 0 
      else if b >= max then 255
      else 255 * (b - min) / (max - min + 1))
    in
    fun rgb ->
      let b = Color.brightness rgb in
      if b = 0 then {r=0; g=0; b=0}
      else begin 
      	let new_b = new_b_array.(b) in
  	let color_fix c =
  	  if c < 0 then 0 else if c > 255 then 255 else c
  	in
  	{ r = color_fix (rgb.r * new_b / b);
  	  g = color_fix (rgb.g * new_b / b);
  	  b = color_fix (rgb.b * new_b / b) }
      end

open OImages

let gamma log img =
  let table =
    Array.init 256 (fun x -> truncate (((float x /. 255.0) ** log) *. 255.0)) in
  let filter =
    fun rgb ->
      { r = table.(rgb.r);
      	g = table.(rgb.g);
      	b = table.(rgb.b) }
  in
  let img' = new rgb24 img#width img#height in
  for x = 0 to img#width - 1 do
    for y = 0 to img#height - 1 do
      img'#unsafe_set x y (filter (img#unsafe_get x y))
    done
  done;
  img'

let filter f keep img =
  let hist = create () in
  for x = 0 to img#width - 1 do
    for y = 0 to img#height - 1 do
      store_sample hist (img#unsafe_get x y)
    done
  done;
(*
  let dx = img#width / 100 in
  let dy = img#height / 100 in
  for x = 0 to (img#width - 1) / dx do
    for y = 0 to (img#height - 1) / dy do
      store_sample hist (img#unsafe_get (x*dx) (y*dy))
    done
  done;
*)
  let filter = f keep hist in
  let img' = new rgb24 img#width img#height in
  for x = 0 to img#width - 1 do
    for y = 0 to img#height - 1 do
      img'#unsafe_set x y (filter (img#unsafe_get x y))
    done
  done;
  img'  
OCaml

Innovation. Community. Security.