views:

597

answers:

3

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.

A: 

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.

MarkusQ
+5  A: 

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
nlucaroni
Thanx for you answer, but what I really need is the few lines that read the *.bmp file and convert it into a color array, your librairies are greate, but I dont need all that stuff, so can you help please!
martani_net
libraries or roll your own, what other options do you expect to have?
nlucaroni
A: 

If you don't want to use CAMLIMAGE, usually raw RGB or PNM/PPM (which have an easy to create header format followed by RGB values) images are used. ImageMagick allows you to then view this formats or convert them into more usable formats.