From: "Janne Hellsten" <jjhellst@gmail.com>
To: "Richard Jones" <rich@annexia.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Portable PNG exporter
Date: Mon, 22 Sep 2008 17:59:58 +0300 [thread overview]
Message-ID: <700d600f0809220759o7df59786o5844c0f34227bcd6@mail.gmail.com> (raw)
In-Reply-To: <20080920230351.GA9757@annexia.org>
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 <rich@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
next prev parent reply other threads:[~2008-09-22 15:00 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 ` [Caml-list] " Richard Jones
2008-09-22 14:47 ` Florent Monnier
2008-09-22 14:59 ` Janne Hellsten [this message]
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=700d600f0809220759o7df59786o5844c0f34227bcd6@mail.gmail.com \
--to=jjhellst@gmail.com \
--cc=caml-list@inria.fr \
--cc=rich@annexia.org \
/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