Loading Blog Search...

Tuesday, August 24, 2004

Programming Camlimages

Core Manual is here. Please refer to for how to programming o'caml with camlimages and other development kits. An example 1.ml to write image to bmp file:

open Graphics;;
open Images;; (*before versioni 2.4, it's Image*)
open Rgb24;;
open_graph "";;
 let sx, sy = size_x (), size_y () in
  let maxr = min (sx / 4) (sy / 4) and maxc = 256 in
  while not (key_pressed ()) do
    let x, y, r, color =
      Random.int sx, Random.int sy, Random.int maxr,
      rgb (Random.int maxc) (Random.int maxc) (Random.int maxc) in
    Graphics.set_color color; fill_circle x y r
  done              ;;
 let img = Graphic_image.image_of (get_image 0 0 (size_x ()) (size_y()));;
Bmp.save "ddd" [] (Images.Rgb24 img);;
Here, the third para of Bmp.save is Images.t; Graphic_image.image_of returns a value with type of Rgb24.t; Images.t is a sum type:
type t=
   | Index8 of Index8.t
   | Rgb24 of Rgb24.t
   | Index16 of Index16.t
   | Rgba32 of Rgba32.t
   | Cmyk32 of Cmyk32.t
thus, (Images.Rgb24 img)'s type is Image.t
To load needed modules, there are several methods.
  1. during compile: ocamlc -I /usr/lib/ocaml/camlimages ci_core.cma graphics.cma ci_graphics.cma ci_bmp.cma 1.ml
  2. in top-level: start ocaml, and then: #directory "/usr/lib/ocaml/camlimages";; #load "ci_core.cma";; #load "graphics.cma";; #load "ci_graphics.cma";; #load "ci_graphics.cma";; #load "ci_bmp.cma";;
    furthurmore, by adding these commands to 1.ml, it can be interpreted by ocaml directly.

No comments: