package bimage

  1. Overview
  2. Docs

Source file image.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
open Type
open Color

type layout = Planar | Interleaved

type ('a, 'b, 'c) t = {
  width : int;
  height : int;
  color : 'c Color.t;
  layout : layout;
  data : ('a, 'b) Data.t;
}

let create ?(layout = Interleaved) kind color width height =
  let channels = channels_of_color color in
  let data = Data.create kind (width * height * channels) in
  { width; height; color; layout; data }

let compare a b = Data.compare a.data b.data

let equal a b = Data.equal a.data b.data

let of_data color width height layout data =
  let channels = channels_of_color color in
  if width * height * channels <> Data.length data then Error.exc `Invalid_shape
  else { width; height; color; layout; data }

let like image =
  create ~layout:image.layout (Data.kind image.data) image.color image.width
    image.height

let like_with_kind kind image =
  create ~layout:image.layout kind image.color image.width image.height

let like_with_color color image =
  create ~layout:image.layout (Data.kind image.data) color image.width
    image.height

let like_with_layout layout image =
  create ~layout (Data.kind image.data) image.color image.width image.height

let copy image =
  let data = Data.copy image.data in
  of_data image.color image.width image.height image.layout data

let copy_to ~dest src = Data.copy_to ~dest:dest.data src.data

let random ?(layout = Interleaved) kind color width height =
  let channels = channels_of_color color in
  let data = Data.random kind (width * height * channels) in
  { width; height; color; layout; data }

let channels { color; _ } = channels_of_color color

let[@inline] kind { data; _ } = Data.kind data

let color { color; _ } = color

let layout { layout; _ } = layout

let shape { width; height; color; _ } = (width, height, channels_of_color color)

let[@inline] length { width; height; color; _ } =
  width * height * Color.channels color

let data { data; _ } = data

let empty_pixel image = Pixel.empty (channels image)

let empty_data image = Data.create (kind image) (channels image)

let convert_to ~dest img =
  let dest_k = kind dest in
  let src_k = kind img in
  for i = 0 to length dest - 1 do
    dest.data.{i} <- Kind.convert ~from:src_k dest_k img.data.{i}
  done

let convert k img =
  let dest = create ~layout:img.layout k img.color img.width img.height in
  convert_to ~dest img;
  dest

let of_any_color im color : (('a, 'b, 'c) t, Error.t) result =
  if Color.channels color = Color.channels im.color then
    Ok (of_data color im.width im.height im.layout im.data)
  else Error `Invalid_color

let[@inline] index image x y c =
  match image.layout with
  | Planar -> (image.width * image.height * c) + (y * image.width) + x
  | Interleaved ->
      (y * image.width * image.color.Color.channels)
      + (image.color.Color.channels * x)
      + c

let index_at image offs =
  Data.slice image.data ~offs ~length:image.color.Color.channels

let[@inline] get image x y c =
  let index = index image x y c in
  if index < 0 || index >= length image then Kind.min (kind image)
  else image.data.{index}

let[@inline] set image x y c v =
  let index = index image x y c in
  image.data.{index} <- v

let get_f image x y c =
  let kind = kind image in
  get image x y c |> Kind.to_float kind

let set_f image x y c v =
  let kind = kind image in
  let v = Kind.of_float kind v in
  set image x y c v

let get_norm image x y c =
  let kind = kind image in
  get image x y c |> Kind.to_float kind |> Kind.normalize kind

let set_norm image x y c v =
  let kind = kind image in
  let v = Kind.denormalize kind v |> Kind.of_float kind in
  set image x y c v

let get_pixel image ?dest x y =
  let c = channels image in
  let (Pixel.Pixel px) =
    match dest with Some px -> px | None -> Pixel.empty c
  in
  for i = 0 to c - 1 do
    px.{i} <- get_f image x y i
  done;
  Pixel.Pixel px

let set_pixel image x y (Pixel.Pixel px) =
  let c = channels image in
  for i = 0 to c - 1 do
    set_f image x y i px.{i}
  done

let get_pixel_norm image ?dest x y =
  let c = channels image in
  let (Pixel.Pixel px) =
    match dest with Some px -> px | None -> Pixel.empty c
  in
  for i = 0 to c - 1 do
    px.{i} <- get_norm image x y i
  done;
  Pixel.Pixel px

let set_pixel_norm image x y (Pixel.Pixel px) =
  let c = channels image in
  for i = 0 to c - 1 do
    set_norm image x y i px.{i}
  done

let get_data image ?dest x y =
  let c = channels image in
  let px =
    match dest with Some px -> px | None -> Data.create (kind image) c
  in
  for i = 0 to c - 1 do
    px.{i} <- get image x y i
  done;
  px

let set_data image x y px =
  let c = channels image in
  for i = 0 to c - 1 do
    set image x y i px.{i}
  done

let map_inplace f img = Data.map_inplace f img.data

let map2_inplace f a b = Data.map2_inplace f a.data b.data

let map f img =
  let dest = copy img in
  map_inplace f dest;
  dest

let map2 f img b =
  let dest = copy img in
  map2_inplace f dest b;
  dest

let[@inline] for_each_pixel f ?(x = 0) ?(y = 0) ?width ?height img =
  let width =
    match width with Some w -> min (img.width - x) w | None -> img.width - x
  in
  let height =
    match height with
    | Some h -> min (img.height - y) h
    | None -> img.height - y
  in
  let px = empty_pixel img in
  for j = y to y + height - 1 do
    for i = x to x + width - 1 do
      let px = get_pixel img ~dest:px i j in
      f i j px
    done
  done

let[@inline] for_each f ?(x = 0) ?(y = 0) ?width ?height img =
  let width =
    match width with Some w -> min (img.width - x) w | None -> img.width - x
  in
  let height =
    match height with
    | Some h -> min (img.height - y) h
    | None -> img.height - y
  in
  let px = empty_data img in
  for j = y to y + height - 1 do
    for i = x to x + width - 1 do
      let px = get_data img ~dest:px i j in
      f i j px
    done
  done

let avg ?(x = 0) ?(y = 0) ?width ?height img =
  let width =
    match width with None -> img.width - x | Some w -> min w (img.width - x)
  in
  let height =
    match height with None -> img.height - y | Some h -> min h (img.width - y)
  in
  let avg = Data.create f32 (channels img) in
  let channels = channels img in
  let size = float_of_int (width * height) in
  let kind = kind img in
  for_each
    (fun _x _y px ->
      for i = 0 to channels - 1 do
        avg.{i} <- avg.{i} +. Kind.to_float kind px.{i}
      done)
    ~x ~y ~width ~height img;
  for i = 0 to channels - 1 do
    avg.{i} <- avg.{i} /. size
  done;
  avg

let convert_layout layout im =
  let width, height, _ = shape im in
  let dest = create ~layout (kind im) (color im) width height in
  for_each
    (fun x y px ->
      for i = 0 to Data.length px - 1 do
        dest.data.{index dest x y i} <- px.{i}
      done)
    im;
  dest

let crop im ~x ~y ~width ~height =
  let dest = create ~layout:im.layout (kind im) im.color width height in
  for_each
    (fun i j _ ->
      for c = 0 to channels im - 1 do
        set dest i j c (get im (x + i) (y + j) c)
      done)
    dest;
  dest

let mean_std ?(channel = 0) image =
  let kind = kind image in
  let x1 = ref 0. in
  let x2 = ref 0. in
  for_each
    (fun _x _y px ->
      let f = Kind.to_float kind px.{channel} in
      x1 := !x1 +. f;
      x2 := !x2 +. (f *. f))
    image;
  let len = length image |> float_of_int in
  let mean = !x1 /. len in
  let std = sqrt ((!x2 /. len) -. (mean *. mean)) in
  (mean, std)

let fold f image init = Data.fold f image.data init

let fold2 f a b init = Data.fold2 f a.data b.data init

let fold_data f image init =
  let acc = ref init in
  for_each (fun x y px -> acc := f x y px !acc) image;
  !acc

let fold_data2 f a b init =
  let acc = ref init in
  for_each
    (fun x y px ->
      let px' = get_data b x y in
      acc := f x y px px' !acc)
    a;
  !acc

module Diff = struct
  type diff = (int * int * int, float) Hashtbl.t

  let apply diff image =
    Hashtbl.iter
      (fun (x, y, c) v ->
        let v' = get_norm image x y c in
        set_norm image x y c (v' +. v))
      diff

  let length x = Hashtbl.length x
end

let diff a b =
  let dest = Hashtbl.create 8 in
  let kind = kind a in
  for_each
    (fun x y px ->
      let pxb = get_data b x y in
      for i = 0 to channels a do
        let a = Kind.to_float kind px.{i} |> Kind.normalize kind in
        let b = Kind.to_float kind pxb.{i} |> Kind.normalize kind in
        if a <> b then Hashtbl.replace dest (x, y, i) (a -. b)
      done)
    a;
  dest