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-20 (23:03)
From: Richard Jones <rich@a...>
Subject: Re: [Caml-list] Portable PNG exporter
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?

> I decided to write a simple self-contained .png exporter that wouldn't
> depend on any external libraries (not even libpng or zlib).  I thought
> someone else might benefit from this piece of code and I thus made it
> freely available here:
> http://code.google.com/p/aihiot/source/browse/trunk/gfx/save_bitmap/ocaml/png.ml

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.

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.  A PPM file has such
a simple format that you can write it directly from just about any
language, even a shell script.

  P3 <width> <height> 255
  followed by <width>x<height>x3 RGB triplets (in decimal, separated
  by writespace)

As a concrete example in OCaml:

  open Printf
  let () =
    let chan = Unix.open_process_out "pnmtopng > /tmp/image.png" in
    fprintf chan "P3 128 128 255\n";
    for i = 0 to 127 do
      for j = 0 to 127 do
        let r = float (i+j) /. 256. in
        let g = float (i*2) /. 256. in
        let b = float j /. 128. in
        fprintf chan "%d %d %d\n"
	  (int_of_float (r*.255.))
	  (int_of_float (g*.255.))
	  (int_of_float (b*.255.))
    ignore (Unix.close_process_out chan)


Richard Jones
Red Hat