Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Message from discussion Portable PNG exporter
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Richard Jones  
View profile  
 More options Sep 20 2008, 7:04 pm
Newsgroups: fa.caml
From: Richard Jones <r...@annexia.org>
Date: Sat, 20 Sep 2008 23:04:12 UTC
Local: Sat, Sep 20 2008 7:04 pm
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/o...

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.))
      done
    done;
    ignore (Unix.close_process_out chan)

Rich.

--
Richard Jones
Red Hat

_______________________________________________
Caml-list mailing list. Subscription management:
http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
Archives: http://caml.inria.fr
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
Bug reports: http://caml.inria.fr/bin/caml-bugs


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google