package ocaml-canvas

  1. Overview
  2. Docs

Image data manipulation functions

type t

An abstract type representing an image data

val create : (int * int) -> t

create size creates an empty image data of the given size.

Exceptions:

val createFromPNG : string -> t React.event

createFromPNG filename creates an image data with the contents of PNG file filename. The returned event will be triggered once the image is loaded.

Exceptions:

val getSize : t -> int * int

getSize id returns the size of image data id

val fill : t -> Color.t -> unit

fill id c fills the image data id with the given color c

val sub : t -> pos:(int * int) -> size:(int * int) -> t

sub c ~pos ~size returns a copy of the pixel data at position pos of size size in image data id. Any pixel outside the image bounds is considered to be transparent black.

Exceptions:

  • Invalid_argument if either component of size is outside the range 1-32767
val blit : dst:t -> dpos:(int * int) -> src:t -> spos:(int * int) -> size:(int * int) -> unit

blit ~dst ~dpos ~src ~spos ~size copies the area specified by spos and size from image data src to image data dst at position dpos. If the given position and size yield an inconsistent area, this has no effect.

Exceptions:

  • Invalid_argument if either component of size is outside the range 1-32767
val getPixel : t -> (int * int) -> Color.t

getPixel id pos returns the color of the pixel at position pos in image data id. If pos is outside the image bounds, returns the transparent black color.

val putPixel : t -> (int * int) -> Color.t -> unit

putPixel id pos c sets the color of the pixel at position pos in image data id to color c. If pos is outside the image bounds, this has no effect.

val importPNG : t -> pos:(int * int) -> string -> t React.event

importPNG id ~pos filename loads the file filename into image data id at position pos. Any pixel that falls outside the image bounds is ignored. The returned event will be triggered once the image is loaded.

Exceptions:

val exportPNG : t -> string -> unit

exportPNG id filename saves the contents of image data id to a file with name filename

Exceptions:

type t_repr = (int, Stdlib.Bigarray.int8_unsigned_elt, Stdlib.Bigarray.c_layout) Stdlib.Bigarray.Array3.t

Image data's internal representation is a big array of dimension 3 (height, width, component), with the components in BGRA order

val of_bigarray : t_repr -> t

of_bigarray ba reinterprets a big array ba as an image data. The big array must be of dimension 3 (height, width, component), with the components in BGRA order. The underlying memory will be shared between the image data and the big array.

Exceptions:

  • Invalid_argument if the first or second dimension of ba is outside the range 1-32767, or if the third dimension of ba is not 4
val to_bigarray : t -> t_repr

to_bigarray id reinterprets an image data id as a big array. The resulting big array will be of dimension 3 (height, width, component), with the components in BGRA order. The underlying memory will be shared between the image data and the big array.