Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a bitmap in OCAML?

I want to read a bitmap file (from the file system) using OCAML and store the pixels (the colors) inside an array which have th dimension of the bitmap, each pixel will take one cell in the array.

I found the function Graphics.dump_image image -> color array array but it doesn't read from a file.

like image 974
0xFF Avatar asked Mar 04 '09 23:03

0xFF


3 Answers

CAMLIMAGE should do it. There is also a debian package (libcamlimage-ocmal-dev), as well as an installation through godi, if you use that to manage your ocaml packages.

As a useful example of reading and manipulating images in ocaml, I suggest looking over the code for a seam removal algorithm over at eigenclass.

You can also, as stated by jonathan --but not well-- call C functions from ocaml, such as ImageMagick. Although you're going to do a lot of manipulation of the image data to bring the image into ocaml, you can always write c for all your functions to manipulate the image as an abstract data type --this seems to be completely opposite of what you want though, writing most of the program in C not ocaml.

Since I recently wanted to play around with camlimages (and had some trouble installing it --I had to modify two of the ml files from compilation errors, very simple ones though). Here is a quick program, black_and_white.ml, and how to compile it. This should get someone painlessly started with the package (especially, dynamic image generation):

   let () =
       let width  = int_of_string Sys.argv.(1)
       and length = int_of_string Sys.argv.(2)
       and name   = Sys.argv.(3)
       and black = {Color.Rgb.r = 0; g=0; b=0; }
       and white = {Color.Rgb.r = 255; g=255; b=255; } in
       let image = Rgb24.make width length black in
       for i = 0 to width-1 do
           for j = 0 to (length/2) - 1 do
               Rgb24.set image i j white;
           done;
       done;
       Png.save name [] (Images.Rgb24 image)

And to compile,

ocamlopt.opt -I /usr/local/lib/ocaml/camlimages/ ci_core.cmxa graphics.cmxa ci_graphics.cmxa ci_png.cmxa black_and_white.ml -o black_and_white

And to run,

./black_and_white 20 20 test1.png
like image 102
nlucaroni Avatar answered Sep 20 '22 14:09

nlucaroni


I don't know of an out-of-the box way to do it. You could open the file with open_in and read it byte at a time with input_char, suck in the header and the data and build up the color array array that way for simple formats (e.g. BMPs) but for anything like JPGs or PNGs a roll your-own solution would probably be more work than you want to get into.

like image 27
MarkusQ Avatar answered Sep 20 '22 14:09

MarkusQ


You could also use one of the numerous SDL bindings for OCaml, specifically the SDL_image ones, which let you load all kinds of images easily, and provides functions to access individual pixels and raw data as an array.

OCamlSDL is a popular one.

like image 37
didierc Avatar answered Sep 19 '22 14:09

didierc