package cairo2

  1. Overview
  2. Docs

Image surfaces provide the ability to render to memory buffers either allocated by cairo or by the calling code. The supported image formats are those defined in Cairo.Image.format.

type format =
  1. | ARGB32
    (*

    each pixel is a 32-bit quantity, with alpha in the upper 8 bits, then red, then green, then blue. The 32-bit quantities are stored native-endian. Pre-multiplied alpha is used. (That is, 50% transparent red is 0x80800000, not 0x80ff0000.)

    *)
  2. | RGB24
    (*

    each pixel is a 32-bit quantity, with the upper 8 bits unused. Red, Green, and Blue are stored in the remaining 24 bits in that order.

    *)
  3. | A8
    (*

    each pixel is a 8-bit quantity holding an alpha value.

    *)
  4. | A1
    (*

    each pixel is a 1-bit quantity holding an alpha value. Pixels are packed together into 32-bit quantities. The ordering of the bits matches the endianess of the platform. On a big-endian machine, the first pixel is in the uppermost bit, on a little-endian machine the first pixel is in the least-significant bit.

    *)

This is used to identify the memory format of image data.

val create : format -> w:int -> h:int -> Surface.t

Creates an image surface of the specified format and dimensions. Initially the surface contents are all 0. (Specifically, within each pixel, each color or alpha channel belonging to format will be 0. The contents of bits within a pixel, but not belonging to the given format are undefined).

type data8 = (int, Stdlib.Bigarray.int8_unsigned_elt, Stdlib.Bigarray.c_layout) Stdlib.Bigarray.Array1.t

Images represented as an array of 8 bytes values.

type data32 = (int32, Stdlib.Bigarray.int32_elt, Stdlib.Bigarray.c_layout) Stdlib.Bigarray.Array2.t

Images represented as an array of 32 bytes (RGB or RGBA) values.

val create_for_data8 : data8 -> ?stride:int -> format -> w:int -> h:int -> Surface.t

create_for_data8 data format ?stride width height creates an image surface for the provided pixel data. The initial contents of buffer will be used as the initial image contents; you must explicitly clear the buffer, using, for example, Cairo.rectangle and Cairo.fill if you want it cleared.

  • parameter stride

    the number of bytes between the start of rows in the buffer as allocated. This value should always be computed by stride_for_width before allocating the data buffer. (that's what this function does if the argument is not provided).

val create_for_data32 : ?w:int -> ?h:int -> ?alpha:bool -> data32 -> Surface.t

create_for_data32 ?w ?h ?alpha data same as Cairo.Image.create_for_data8 except that the stride will be set so that data.{y,x} refers to the pixel (x,y) (where (0,0) is the top left pixel and the Y axis is directed downwards).

  • parameter w

    the width of the image (default: Array2.dim2 data).

  • parameter h

    the height of the image (default: Array2.dim1 data).

  • parameter alpha

    if true (default), the ARGB32 format is selected, otherwise RGB24 is used.

val get_data8 : Surface.t -> data8

Get the data of the image surface (shared), for direct inspection or modification. A call to Cairo.Surface.mark_dirty or Cairo.Surface.mark_dirty_rectangle is required after the data is modified.

val get_data32 : Surface.t -> data32

Get the data of the image surface (shared), for direct inspection or modification. The 1st (resp. 2nd) dimension of the bigarray correspond to the height (resp. width) of the surface. A call to Cairo.Surface.mark_dirty or Cairo.Surface.mark_dirty_rectangle is required after the data is modified.

  • raises Invalid_argument

    if the format is not ARGB32 or RGB24 because the array dimensions would not reflect the image coordinates.

val get_format : Surface.t -> format

Get the format of the image surface.

val get_width : Surface.t -> int

Get the width of the image surface in pixels.

val get_height : Surface.t -> int

Get the height of the image surface in pixels.

val get_stride : Surface.t -> int

Get the stride of the image surface in bytes. Note that in order to convert this stride in bytes to a stride in the bigarray indices, the type of the surface has to be taken into account: for ARGB32 and RGB24, the stride has to be divided by 4.

val stride_for_width : format -> int -> int

stride_for_width format w a stride value that will respect all alignment requirements of the accelerated image-rendering code within cairo. See create_for_data8.

val output_ppm : Stdlib.out_channel -> ?w:int -> ?h:int -> data32 -> unit

output_ppm ch width height data convenience function to write the subarray of size (width, height) representing an image to the PPM format. The possible alpha channel is ignored.

OCaml

Innovation. Community. Security.