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.0 required=5.0 tests=none autolearn=disabled version=3.1.3 Received: from mail2-relais-roc.national.inria.fr (mail2-relais-roc.national.inria.fr [192.134.164.83]) by yquem.inria.fr (Postfix) with ESMTP id 12BB8BB84 for ; Mon, 22 Sep 2008 20:49:43 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ak0BALaF10jVJFBbb2dsb2JhbACTGgEMCwUDCREDpDiBZg X-IronPort-AV: E=Sophos;i="4.32,448,1217800800"; d="scan'208";a="15233317" Received: from mx-out.libertysurf.net (HELO mail.libertysurf.net) ([213.36.80.91]) by mail2-smtp-roc.national.inria.fr with ESMTP; 22 Sep 2008 20:49:42 +0200 Received: from [192.168.1.2] (91.168.179.214) by mail.libertysurf.net (7.3.118.8) id 48BBB44D004E05B8 for caml-list@yquem.inria.fr; Mon, 22 Sep 2008 21:49:26 +0200 From: Florent Monnier To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Portable PNG exporter Date: Mon, 22 Sep 2008 16:47:21 +0200 User-Agent: KMail/1.8.2 References: <700d600f0809201037x3358a788v818d488c451ce7bf@mail.gmail.com> <20080920230351.GA9757@annexia.org> In-Reply-To: <20080920230351.GA9757@annexia.org> X-Face: -0"dKXwF0PiXr]fa$^)NJY7$;waqUckGcM7&q,VU?Xv\[=CiVM]g]pDs^xmfU9+Q=Z,OdfMHUR-7Ao%evJh.=aiq,#r0Ux0dm'!l|zeAXj||$>1_(Lv4Hc",&F}sbHeK0`SBA$_|XP MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200809221647.21886.fmonnier@linux-nantes.fr.eu.org> X-Spam: no; 0.00; ocaml:01 ocaml:01 bigarray:01 bigarray:01 printf:01 fprintf:01 pred:01 pred:01 byte:01 printf:01 sprintf:01 sprintf:01 allways:01 height:98 height:98 > 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. =A0A PPM file has such > a simple format that you can write it directly from just about any > language, even a shell script. > > =A0 P3 255 > =A0 followed by xx3 RGB triplets (in decimal, separated > =A0 by writespace) You can reduce the amount of exchanged datas using binary ppm instead of as= cii=20 ppm. In such case the format is: P6\n \n255\n followed by xx3 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) =3D let width =3D Bigarray.Array2.dim1 r_channel and height =3D Bigarray.Array2.dim2 r_channel in Printf.fprintf oc "P6\n%d %d\n255\n" width height; for y =3D 0 to pred height do for x =3D 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=3D96) () =3D let cmd =3D Printf.sprintf "cjpeg -quality %d" quality in (* let cmd =3D Printf.sprintf "ppmtojpeg -quality %d" quality in let cmd =3D Printf.sprintf "convert ppm:- -quality %d jpg:-" quality in *) let ic, oc =3D Unix.open_process cmd in output_ppm ~img ~oc; try while true do let c =3D 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 =3D let cmd =3D Printf.sprintf "convert ppm:- %s:-" format in let ic, oc =3D Unix.open_process cmd in output_ppm ~img ~oc; try while true do let c =3D input_char ic in print_char c done with End_of_file -> () ;; let new_img ~width ~height =3D let all_channels =3D let kind =3D Bigarray.int8_unsigned and layout =3D Bigarray.c_layout in Bigarray.Array3.create kind layout 3 width height in let r_channel =3D Bigarray.Array3.slice_left_2 all_channels 0 and g_channel =3D Bigarray.Array3.slice_left_2 all_channels 1 and b_channel =3D 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 useful= l=20 to remove an external dependency ! Thanks for this piece of code ! =2D-=20 =46lorent