File: image_create.ml

package info (click to toggle)
ocaml-cairo2 0.6.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 716 kB
  • sloc: ml: 2,955; ansic: 2,132; makefile: 24; sh: 17
file content (47 lines) | stat: -rw-r--r-- 1,201 bytes parent folder | download | duplicates (3)
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
open Printf
open Cairo
open Bigarray

let create() =
  let data = Array1.create int8_unsigned c_layout 360_000 in
  Gc.finalise (fun _ -> eprintf "DESTROY bigarray 'data'\n%!") data;
  let surf = Image.create_for_data8 data Image.RGB24 ~w:300 ~h:300 in
  Cairo.create surf

let () =
  let cr = create() in
  printf "With Cairo handle:\n%!";
  set_source_rgb cr 1. 1. 1.;
  rectangle cr 0. 0. ~w:300. ~h:300.;
  fill cr;

  Gc.compact();  Gc.compact();
  set_source_rgb cr 1. 0. 0.;
  move_to cr 10. 150.;
  set_font_size cr 100.;
  show_text cr "Hello";
  Gc.compact();  Gc.compact();

  eprintf "- Write image\n%!";
  PNG.write (get_target cr) "test_image.png";
  eprintf "- Finish surface\n%!";
  Surface.finish (get_target cr);
  Gc.compact()


(* Test for stride < 0 (not handled for now) and for incoherent width
   / stride *)
let () =
  let mat = Array1.create int8_unsigned c_layout 80_000 in
  let test_stride stride =
    try
      let surf = Image.create_for_data8 mat Image.A8 ~w:100 ~h:100 ~stride in
      assert(Image.get_stride surf = stride)
    with Error INVALID_STRIDE ->
      assert(stride < 100)
  in

  test_stride 108;
  test_stride 99;
  test_stride 0;
  test_stride (-108);