English version
Accueil     Ŕ propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis ŕ jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml ŕ l'adresse ocaml.org.

Browse thread
Portable PNG exporter
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-09-22 (15:00)
From: Janne Hellsten <jjhellst@g...>
Subject: Re: [Caml-list] Portable PNG exporter
As my tiny PNG exporter got more attention than I anticipated, let me
clarify a couple of things:

- My OCaml code is based on a C function written by Tero Karras.  I
  merely rewrote his masterpiece in OCaml.  The C version is available


- The trick is to save out uncompressed PNGs.  This gets rid of the
  zlib dependency.  Although the files get bigger, uncompressed .png
  is still very useful.

On Sun, Sep 21, 2008 at 2:03 AM, Richard Jones <rich@annexia.org> wrote:
> On Sat, Sep 20, 2008 at 08:37:22PM +0300, Janne Hellsten wrote:
>> While working on a graphics related problem, I needed to save the
>> contents of a Graphics framebuffer to a .png file.  Quick googling for
>> "ocaml png" didn't bring up any results for libraries that would be
>> easy to install via GODI.  I am aware of CamlImages but unfortunately
>> I have never been successful at installing it due its heavy dependence
>> on external libraries.  I also often work on Windows and installing
>> external OCaml libraries on Windows is usually a major PITA (if
>> possible at all).
> Hmmm .. GODI?

If you meant why didn't I install CamlImages via GODI, well, not
because of lack of trying.  I quickly got into a conf packages hell
trying to figure out which devel packages I'm missing.  It wasn't the
first time I was trying to install it and I quickly decided against
spending time on it.  This was on Linux.

If you meant GODI on Windows, well..

I like GODI and am a long-time user of it.  However, installation on
Windows is not very easy and (at least last time I tried) requires
Cygwin.  I prefer to work natively on Windows and I really dislike
emulating Unix with Cygwin (slow, broken, you name it).

Now, if I was the only one hacking on my code on Cygwin+GODI, things
would be pretty OK and I could live with the occasional "I can't get
GODI to install on my broken Cygwin installation/this package doesn't
compile on Cygwin/...".  The problem is that all my collegues use
Windows and aren't familiar with OCaml, GODI or Cygwin.  If something
goes wrong during GODI or a particular package installation, they
won't be able to solve the issue without my help.  This has pretty
effectively ruined my attempts to use OCaml for internal tools.

My OCaml environment nowadays consists of the following:

- OCaml MSVC version (Win32 installer, no Cygwin required)

- OMake (Win32 installer, no Cygwin required during installation or use)

- No external libraries (with the exception of ExtLib which is just a
  bunch of .ml files and thus easy to build)

This obviously limits what I can do with OCaml.  In fact, I have often
needed to resort to using Python (the horror!) for some of my scripts.
Everyone's already got Python installed and so my scripts work without
any installation.

It's too easy to dismiss Windows installation problems by suggesting
everyone to just adopt Linux.  Unfortunately that's not an option for
me.  I see value in being able to port from Unix to Windows and vice

> Your code is surprisingly elegant .. I didn't think it was possible to
> write out a PNG file in such few lines.  I wonder if it would be more
> concise using bitstring.

Doing 32-bit (<> 31-bit) integer on OCaml was a bit of a pain and it
shows.  If Bitstring helps you write more concise 32-bit ALU ops, then
I'd imagine the code would be more concise.  Manipulating bit fields
was not a big issue with .png though.

Updating Adler and CRC in functional style was probably not a good
idea, an imperative version would be easier to read and maintain.  Not
to mention feeling a bit naughty when doing I/O inside a fold_left. :)

> For reference, I've found the easiest way to export PNGs (in any
> language, not just OCaml) is to use netpbm.  Simply fork pnmtopng
> using Unix.open_process_out and write a PPM file.

I usually write out .tga which is also very easy to write and most
programs open it.  I wanted to have .png to be able to view them in
generated HTML reports.

Saving a .tga is quite easy, copy&pasting from

(** Save a .tga file to chnl. *)
let write_tga_chnl chnl pixels w h =
  let header =
    [0; 0; 2; 0; 0; 0; 0; 0; 0; 0; 0; 0;
     w land 255; w lsr 8;
     h land 255; h lsr 8; 32; 8] in
  List.iter (fun e -> output_byte chnl e) header;
  for y = 0 to h-1 do
    for x = 0 to w-1 do
      let c = pixels.(x+(h-1-y)*w) in (* h-1-y = Flip image *)
      output_byte chnl (c land 255);
      output_byte chnl ((c lsr 8) land 255);
      output_byte chnl ((c lsr 16) land 255);
      output_byte chnl 255;