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.3 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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id AE4E8BB84 for ; Mon, 22 Sep 2008 17:00:19 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ak4DAM5P10jAXQIniGdsb2JhbACSWz4BAQEVIp0OhgUBAoN8 X-IronPort-AV: E=Sophos;i="4.32,446,1217800800"; d="scan'208";a="29460181" Received: from concorde.inria.fr ([192.93.2.39]) by mail4-smtp-sop.national.inria.fr with ESMTP; 22 Sep 2008 17:00:19 +0200 Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id m8MF0HhZ010966 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Mon, 22 Sep 2008 17:00:18 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AhUBAEdQ10hC+VLlmWdsb2JhbACSWz4BAQEBAQgLCAkRA50KhgQBAoN8 X-IronPort-AV: E=Sophos;i="4.32,446,1217800800"; d="scan'208";a="17224654" Received: from wx-out-0506.google.com ([66.249.82.229]) by mail3-smtp-sop.national.inria.fr with ESMTP; 22 Sep 2008 17:00:00 +0200 Received: by wx-out-0506.google.com with SMTP id s18so525125wxc.0 for ; Mon, 22 Sep 2008 07:59:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=rQ/BY9MOy6+WeAb+GLp0D7Z5HyQO9/ZMjpVKMfj7Zz8=; b=jkTX6VjaVHcBMnAi4PZEOOpVfyHKQzh9KEHkN+NrA/pq0obrxnLn2tObpHLiUWaXhA SsLCGzl8XPEZIqacW94NLfbxdIItG3FE9OMerpomM8wvxWusgWFsq/zvFAYBuFzwM0Dc BPed0g6YLOYuBFlFBWrC8u9dpah/4osuLSAwc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=neqi1bBiJpviPz63Mbr53NqsyAsLVhP2GoxHgDJV3pILj36wOMshYlB8KRVSuAOv7v WpWMc8UxPS5UnfIQbs/HahTCAa8wdv8PbpTEIEtyueXLdXMVkdxIOfIWBJuesZAUiFlF /S3a+Lb6RUgtf9okZTENg4iwe3ueIuOBl3i5s= Received: by 10.142.180.19 with SMTP id c19mr1460353wff.322.1222095598321; Mon, 22 Sep 2008 07:59:58 -0700 (PDT) Received: by 10.142.240.21 with HTTP; Mon, 22 Sep 2008 07:59:58 -0700 (PDT) Message-ID: <700d600f0809220759o7df59786o5844c0f34227bcd6@mail.gmail.com> Date: Mon, 22 Sep 2008 17:59:58 +0300 From: "Janne Hellsten" To: "Richard Jones" Subject: Re: [Caml-list] Portable PNG exporter Cc: caml-list@inria.fr In-Reply-To: <20080920230351.GA9757@annexia.org> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <700d600f0809201037x3358a788v818d488c451ce7bf@mail.gmail.com> <20080920230351.GA9757@annexia.org> X-Miltered: at concorde with ID 48D7B301.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; ocaml:01 rewrote:01 ocaml:01 bitmap:01 bitmap:01 zlib:01 framebuffer:01 camlimages:01 hmmm:01 camlimages:01 long-time:01 cygwin:01 cygwin:01 iter:01 byte:01 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 here: http://code.google.com/p/aihiot/source/browse/trunk/gfx/save_bitmap/c/save_bitmap.c - 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 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 versa. > 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 http://code.google.com/p/aihiot/source/browse/trunk/gfx/save_bitmap/ocaml/tga.ml: (** 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; done done Janne