Bitmap/Read an image through a pipe

From Rosetta Code
Revision as of 12:57, 11 December 2008 by rosettacode>ShinTakezou (task first definition and C code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Bitmap/Read an image through a pipe
You are encouraged to solve this task according to the task description, using any language you may know.

This task is the opposite of the PPM conversion through a pipe. In this task, using a delegate tool (like cjpeg, one of the netpbm package, or convert of the ImageMagick package) we read an image file and load it into the data storage type defined here. We can also use the code from Read ppm file, so that we can use it like a bridge between the foreign image format and our simple data storage.


C

Here I've used convert by ImageMagick. It is up to the program to understand the source file type; in this way, we can read theoretically any image format ImageMagick can handle. The get_ppm function defined in Read ppm file is used.

<c>#include <stdio.h>

  1. define MAXFILENAMELEN 256
  2. define MAXFULLCMDBUF MAXCMDBUF+MAXFILENAMELEN

image read_image(char *name) {

     FILE *pipe;
     char buf[MAXFULLCMDBUF];
     image im;
     
     FILE *test = fopen(name, "r");
     if ( test == NULL ) {
        fprintf(stderr, "cannot open file %s\n", name);
        return NULL;
     }
     fclose(test);
     
     snprintf(buf, MAXFULLCMDBUF, "convert %s ppm:-", name);
     pipe = popen(buf, "r");
     if ( pipe != NULL )
     {
          im = get_ppm(pipe);
          pclose(pipe);
          return im;
     }
     return NULL;

}</c>