From: Richard Jones <rich@annexia.org>
To: Janne Hellsten <jjhellst@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Portable PNG exporter
Date: Sun, 21 Sep 2008 00:03:51 +0100 [thread overview]
Message-ID: <20080920230351.GA9757@annexia.org> (raw)
In-Reply-To: <700d600f0809201037x3358a788v818d488c451ce7bf@mail.gmail.com>
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.))
done
done;
ignore (Unix.close_process_out chan)
Rich.
--
Richard Jones
Red Hat
next prev parent reply other threads:[~2008-09-20 23:03 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-09-20 17:37 Janne Hellsten
2008-09-20 23:03 ` Richard Jones [this message]
2008-09-22 14:47 ` [Caml-list] " Florent Monnier
2008-09-22 14:59 ` Janne Hellsten
2008-09-22 15:56 ` Daniel Bünzli
2008-09-21 0:47 ` Jon Harrop
2008-09-21 19:50 ` malc
2008-09-21 20:02 ` Janne Hellsten
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20080920230351.GA9757@annexia.org \
--to=rich@annexia.org \
--cc=caml-list@inria.fr \
--cc=jjhellst@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox