From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id B7DA6BBAF for ; Sun, 21 Sep 2008 01:03:53 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwCAMYd1UjAXQImiGdsb2JhbACTHAEBARUioi6BZg X-IronPort-AV: E=Sophos;i="4.32,439,1217800800"; d="scan'208";a="17177630" Received: from discorde.inria.fr ([192.93.2.38]) by mail3-smtp-sop.national.inria.fr with ESMTP; 21 Sep 2008 01:03:53 +0200 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id m8KN3qor030730 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Sun, 21 Sep 2008 01:03:53 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ApwCAD4e1UhQRFuwiGdsb2JhbACTHAEBARUioi+BZg X-IronPort-AV: E=Sophos;i="4.32,439,1217800800"; d="scan'208";a="15178920" Received: from furbychan.cocan.org ([80.68.91.176]) by mail2-smtp-roc.national.inria.fr with ESMTP/TLS/AES256-SHA; 21 Sep 2008 01:03:52 +0200 Received: from rich by furbychan.cocan.org with local (Exim 4.63) (envelope-from ) id 1KhBUV-0002fM-Ei; Sun, 21 Sep 2008 00:03:51 +0100 Date: Sun, 21 Sep 2008 00:03:51 +0100 To: Janne Hellsten Cc: caml-list@inria.fr Subject: Re: [Caml-list] Portable PNG exporter Message-ID: <20080920230351.GA9757@annexia.org> References: <700d600f0809201037x3358a788v818d488c451ce7bf@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <700d600f0809201037x3358a788v818d488c451ce7bf@mail.gmail.com> User-Agent: Mutt/1.5.13 (2006-08-11) From: Richard Jones X-Miltered: at discorde with ID 48D58158.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; framebuffer:01 ocaml:01 camlimages:01 ocaml:01 hmmm:01 zlib:01 bitmap:01 printf:01 fprintf:01 fprintf:01 20,:98 height:98 height:98 wrote:01 unix:01 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 255 followed by xx3 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.)) done done; ignore (Unix.close_process_out chan) Rich. -- Richard Jones Red Hat