Bitmap/PPM conversion through a pipe: Difference between revisions

From Rosetta Code
Content added Content deleted
(C)
No edit summary
Line 1: Line 1:
{{task|Raster graphics operations}}
{{task|Raster graphics operations}}


Using the data storage type defined [[Basic_bitmap_storage|on this page]] for raster images, delegate writing a JPEG file through a '''pipe''' using the <code>output_ppm</code> function defined [[Write_ppm_file|on this other page]].
Using the data storage type defined [[Basic_bitmap_storage|on this page]] for raster images, delegate writing a JPEG file through a '''pipe''' using the <tt>output_ppm</tt> function defined [[Write_ppm_file|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).
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).
Line 9: Line 9:
This one uses the ImageMagick <tt>convert</tt> tool.
This one uses the ImageMagick <tt>convert</tt> tool.


<c>#define MAXCMDBUF 100
<lang c>#define MAXCMDBUF 100
void print_jpg(image img, int qual)
void print_jpg(image img, int qual)
{
{
Line 25: Line 25:
pclose(pipe);
pclose(pipe);
}
}
}</c>
}</lang>


The code that writes to the pipe is the same of [[Write_ppm_file|output_ppm]] of course. A complete example is
The code that writes to the pipe is the same of [[Write_ppm_file|output_ppm]] of course. A complete example is


<c>int main()
<lang c>int main()
{
{
image img;
image img;
Line 39: Line 39:
free_img(img);
free_img(img);
}
}
</c>
</lang>


In order to make it working, you must link it with the raster image functions given by the codes [[Bresenham's_line_algorithm#C|here]] and [[Basic_bitmap_storage#C|here]]
In order to make it working, you must link it with the raster image functions given by the codes [[Bresenham's_line_algorithm#C|here]] and [[Basic_bitmap_storage#C|here]]
Line 45: Line 45:
== {{Header|OCaml}} ==
== {{Header|OCaml}} ==


<ocaml>let print_jpeg ~img ?(quality=96) () =
<lang ocaml>let print_jpeg ~img ?(quality=96) () =
let cmd = Printf.sprintf "cjpeg -quality %d" quality in
let cmd = Printf.sprintf "cjpeg -quality %d" quality in
(*
(*
Line 59: Line 59:
done
done
with End_of_file -> ()
with End_of_file -> ()
;;</ocaml>
;;</lang>

Revision as of 15:43, 3 February 2009

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

C

This one uses the ImageMagick convert tool.

<lang c>#define MAXCMDBUF 100 void print_jpg(image img, int qual) {

  char buf[MAXCMDBUF];
  unsigned int n;
  FILE *pipe;
  
  snprintf(buf, MAXCMDBUF, "convert ppm:- -quality %d jpg:-", qual);
  pipe = popen(buf, "w");
  if ( pipe != NULL )
  {
          fprintf(pipe, "P6\n%d %d\n255\n", img->width, img->height);
          n = img->width * img->height;
          fwrite(img->buf, sizeof(pixel), n, pipe);
          pclose(pipe);
  }

}</lang>

The code that writes to the pipe is the same of output_ppm of course. A complete example is

<lang c>int main() {

     image img;
     
     img = alloc_img(100,100);
     fill_img(img, 50, 20, 200);
     draw_line(img, 0, 0, 80, 80, 255, 0, 0);
     print_jpg(img, 75);
     free_img(img);

} </lang>

In order to make it working, you must link it with the raster image functions given by the codes here and here

OCaml

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