Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Caml-list] Portable PNG exporter

75 views
Skip to first unread message

Janne Hellsten

unread,
Sep 20, 2008, 1:37:33 PM9/20/08
to caml...@inria.fr
Hi list,

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).

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

Janne

_______________________________________________
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

Richard Jones

unread,
Sep 20, 2008, 7:04:12 PM9/20/08
to Janne Hellsten, caml...@inria.fr
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

Jon Harrop

unread,
Sep 20, 2008, 7:46:56 PM9/20/08
to caml...@yquem.inria.fr
On Saturday 20 September 2008 18:37:22 Janne Hellsten wrote:
> 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/p
>ng.ml

Wicked! Now, who is going to write a minimal self-contained JPEG emitter? ;-)

--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e

malc

unread,
Sep 21, 2008, 3:51:06 PM9/21/08
to Janne Hellsten, caml...@inria.fr
On Sat, 20 Sep 2008, Janne Hellsten wrote:

> Hi list,
>
> 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).
>
> 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

Few things that hurt the eye:

Int32.of_string "[number]" is unnecessary, OCaml(not sure which version
this was introduced in though) can read various specific size ints
natively:

# 0n, 0l, 0L;;
- : nativeint * int32 * int64 = (0n, 0l, 0L)

Secondly `Printf.fprintf chnl "string"' looks rather expensive compared
to `output_string chnl "string"'.

--
mailto:av1...@comtv.ru

Janne Hellsten

unread,
Sep 21, 2008, 4:02:35 PM9/21/08
to malc, caml...@inria.fr
> Few things that hurt the eye:
>
> Int32.of_string "[number]" is unnecessary, OCaml(not sure which version
> this was introduced in though) can read various specific size ints
> natively:
>
> # 0n, 0l, 0L;;
> - : nativeint * int32 * int64 = (0n, 0l, 0L)

Thanks, I didn't know about this. I'm glad to learn there's a better
way to do this.

> Secondly `Printf.fprintf chnl "string"' looks rather expensive compared
> to `output_string chnl "string"'.

Speed wasn't one of my concerns when I wrote this. The temporary
lists that are created per pixel are going to be hurt perf much more
than those printfs. I changed the code nevertheless to use
output_string.

Replacing the functional style of accumulating CRC and Adler to use
imperative style would allow for a clean rewrite that would be much
faster.

Janne

Janne Hellsten

unread,
Sep 22, 2008, 11:00:28 AM9/22/08
to Richard Jones, caml...@inria.fr
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 <ri...@annexia.org> 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

Daniel Bünzli

unread,
Sep 22, 2008, 11:57:20 AM9/22/08
to OCaml Mailing List

Le 22 sept. 08 à 16:59, Janne Hellsten a écrit :

> - 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.

and you can compress them later with pngcrush [1].

Daniel

[1] http://en.wikipedia.org/wiki/Pngcrush

Florent Monnier

unread,
Sep 22, 2008, 2:50:16 PM9/22/08
to caml...@yquem.inria.fr
> For reference, I've found the easiest way to export PNGs (in any
> language, not just OCaml) is to use netpbm.

Probably with any language, but perhaps not any OS ?

> 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)

You can reduce the amount of exchanged datas using binary ppm instead of ascii
ppm. In such case the format is:

P6\n<width in ascii> <height in ascii>\n255\n
followed by <width>x<height>x3 RGB triplets of octect (in binary)

Here is the OCaml code I use, as well for jpeg:

let output_ppm ~oc ~img:(_, r_channel, g_channel, b_channel) =
let width = Bigarray.Array2.dim1 r_channel
and height = Bigarray.Array2.dim2 r_channel in
Printf.fprintf oc "P6\n%d %d\n255\n" width height;
for y = 0 to pred height do
for x = 0 to pred width do
(* output_byte doesn't raise any exception about the range *)
output_char oc (char_of_int r_channel.{x,y});
output_char oc (char_of_int g_channel.{x,y});
output_char oc (char_of_int b_channel.{x,y});
done;
done;
output_char oc '\n';
flush oc
;;

(* you can use different conversion commands (convert is from ImageMagick) *)
let print_jpeg ~img ?(quality=96) () =
let cmd = Printf.sprintf "cjpeg -quality %d" quality in
(*
let cmd = Printf.sprintf "ppmtojpeg -quality %d" quality in
let cmd = Printf.sprintf "convert ppm:- -quality %d jpg:-" quality in
*)
let ic, oc = Unix.open_process cmd in
output_ppm ~img ~oc;
try
while true do
let c = input_char ic in
print_char c
done
with End_of_file -> ()
;;

(* output any of the hundred formats ImageMagick knows *)
let print_file ~img ~format =
let cmd = Printf.sprintf "convert ppm:- %s:-" format in
let ic, oc = Unix.open_process cmd in
output_ppm ~img ~oc;
try
while true do
let c = input_char ic in
print_char c
done
with End_of_file -> ()
;;

let new_img ~width ~height =
let all_channels =
let kind = Bigarray.int8_unsigned
and layout = Bigarray.c_layout
in
Bigarray.Array3.create kind layout 3 width height
in
let r_channel = Bigarray.Array3.slice_left_2 all_channels 0
and g_channel = Bigarray.Array3.slice_left_2 all_channels 1
and b_channel = Bigarray.Array3.slice_left_2 all_channels 2
in
(all_channels,
r_channel,
g_channel,
b_channel)
;;
_________________________________________

Anyway I'm very pleased to see this png exporter, while it's allways usefull
to remove an external dependency !
Thanks for this piece of code !

--
Florent

0 new messages