Bitmap/PPM conversion through a pipe

From Rosetta Code
Revision as of 02:34, 6 December 2008 by rosettacode>Blue Prawn (initial version with OCaml)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Bitmap/PPM conversion through a pipe
You are encouraged to solve this task according to the task description, using any language you may know.

Using the data storage type defined on this page for raster images, delegate writing a JPEG file through a pipe using the output_ppm function defined on this other page.

There are various utilities that can be used for this task, for example: cjpeg (package "jpeg-progs" on Linux), ppmtojpeg (package "netpbm" on Linux), convert (from ImageMagick, multi-platform).

OCaml

<ocaml>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 -> ()
</ocaml>