Chris00 / ocaml-cairo

Binding to Cairo, a 2D Vector Graphics Library.
GNU Lesser General Public License v3.0
54 stars 8 forks source link

Image.create_for_data32 mixes width and height #14

Closed codename68 closed 5 years ago

codename68 commented 5 years ago

Hi,

the function 'create_for_data32' mixes width and height: the first dimension of the Bigarray.Array2 should be the height and the second the width.

The following code produces a 8 by 4 pixels png file, with transparent pixels everywhere but for the white pixel (4, 0), but I expected it to be at (1, 0).

let main () =
  let ba = Bigarray.(Array2.create Int32 C_layout 8 4) in
  Bigarray.Array2.fill ba (Int32.zero);
  Bigarray.Array2.set ba 1 0 (Int32.minus_one);
  let surf = Cairo.Image.create_for_data32 ba in
  Cairo.PNG.write surf "test.png"

let () = main ()

The following code bypasses the bug:

let corrected () =
  (* switch widths and heights *)
  let width = 8 in
  let height = 4 in
  let ba = Bigarray.(Array2.create Int32 C_layout 4 8) in
  Bigarray.Array2.fill ba (Int32.zero);
  Bigarray.Array2.set ba 0 1 (Int32.minus_one);
  (* reinterpret data *)
  let gba = Bigarray.genarray_of_array2 ba in
  let ba = Bigarray.reshape_2 gba width height in
  let surf = Cairo.Image.create_for_data32 ba in
  Cairo.PNG.write surf "test2.png"

let () = corrected ()
Chris00 commented 5 years ago

Thanks for reporting!

Chris00 commented 5 years ago

Tested with:

let () =
  let ba = Bigarray.(Array2.create Int32 C_layout 8 4) in
  Bigarray.Array2.fill ba 0l;
  ba.{0, 1} <- -1l;
  ba.{1, 0} <-  0xFFFF0000l;
  ba.{7, 0} <-  0xFF0000FFl;
  let surf = Cairo.Image.create_for_data32 ba in
  Cairo.PNG.write surf "test.png"
Chris00 commented 5 years ago

Release pending.