From d75be522869a2430355dfe83a96624f39a8fb370 Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Mon, 31 Aug 2009 00:36:38 -0700 Subject: experimental support for imagemagick backend --- src/bmp.c | 2 ++ src/gif.c | 2 ++ src/imagemagick.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/jpeg.c | 3 +++ src/main.c | 8 ++------ src/meh.h | 9 ++++++++ src/netpbm.c | 11 ++++++++-- src/png.c | 2 ++ 8 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 src/imagemagick.c (limited to 'src') diff --git a/src/bmp.c b/src/bmp.c index 33ba589..6fea4bf 100644 --- a/src/bmp.c +++ b/src/bmp.c @@ -102,6 +102,8 @@ struct image *bmp_open(FILE *f){ b->rowwidth += 4 - (b->rowwidth & 3); } + b->img.fmt = &bmp; + return (struct image *)b; } diff --git a/src/gif.c b/src/gif.c index 17e5d4d..8887efd 100644 --- a/src/gif.c +++ b/src/gif.c @@ -45,6 +45,8 @@ static struct image *gif_open(FILE *f){ g->img.bufwidth = gif->SWidth; g->img.bufheight = gif->SHeight; + g->img.fmt = &giflib; + return (struct image *)g; } diff --git a/src/imagemagick.c b/src/imagemagick.c new file mode 100644 index 0000000..3c4a9cd --- /dev/null +++ b/src/imagemagick.c @@ -0,0 +1,61 @@ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include + +#include "meh.h" + +struct image *imagemagick_open(FILE *f){ + char template[] = "/tmp/fileXXXXXX"; + char *ntmp = mktemp(template); + if(!ntmp){ + perror("mktemp"); + exit(EXIT_FAILURE); + } + + int pid; + if(!(pid = fork())){ + int origfd = fileno(f); + if(lseek(origfd, 0, SEEK_SET) != 0){ + perror("lseek"); + exit(EXIT_FAILURE); + } + + char *argv[6]; + + argv[0] = "convert"; + argv[1] = "-depth"; + argv[2] = "255"; + asprintf(&argv[3], "fd:%i", origfd); + asprintf(&argv[4], "ppm:%s", ntmp); + argv[5] = NULL; + execvp(argv[0], argv); + perror("exec"); + exit(EXIT_FAILURE); + }else{ + int status; + waitpid(pid, &status, 0); + if(status){ + return NULL; + } + fclose(f); + FILE *ftmp; + if(!(ftmp = fopen(ntmp, "rb"))){ + perror("fopen"); + exit(EXIT_FAILURE); + } + return netpbm.open(ftmp); + } +} + +struct imageformat imagemagick = { + imagemagick_open, + NULL, + NULL, + NULL +}; + diff --git a/src/jpeg.c b/src/jpeg.c index 26e60d1..fbe53f6 100644 --- a/src/jpeg.c +++ b/src/jpeg.c @@ -51,6 +51,9 @@ static struct image *jpeg_open(FILE *f){ } j->jerr.pub.error_exit = error_exit; + + j->img.fmt = &libjpeg; + return (struct image *)j; } diff --git a/src/main.c b/src/main.c index 3a669c6..54b5e22 100644 --- a/src/main.c +++ b/src/main.c @@ -22,17 +22,13 @@ static int mode; extern Display *display; /* Supported Formats */ -extern struct imageformat libjpeg; -extern struct imageformat giflib; -extern struct imageformat libpng; -extern struct imageformat bmp; -extern struct imageformat netpbm; struct imageformat *formats[] = { &libjpeg, &bmp, &libpng, &netpbm, &giflib, /* HACK! make gif last (uses read()) */ + &imagemagick, NULL }; @@ -50,7 +46,7 @@ struct image *newimage(FILE *f){ struct imageformat **fmt = formats; for(fmt = formats; *fmt; fmt++){ if((img = (*fmt)->open(f))){ - img->fmt = *fmt; + //img->fmt = *fmt; img->ximg = NULL; img->state = NONE; return img; diff --git a/src/meh.h b/src/meh.h index 8465887..88f36e3 100644 --- a/src/meh.h +++ b/src/meh.h @@ -49,3 +49,12 @@ printf("%s: %li e2 us\n", (x), ((t1.tv_sec - t0.tv_sec) * 1000000 + t1.tv_usec - #define TDEBUG_END(x) #endif +/* Supported Formats */ +extern struct imageformat libjpeg; +extern struct imageformat giflib; +extern struct imageformat libpng; +extern struct imageformat bmp; +extern struct imageformat netpbm; +extern struct imageformat imagemagick; + + diff --git a/src/netpbm.c b/src/netpbm.c index d87872f..ef61c8e 100644 --- a/src/netpbm.c +++ b/src/netpbm.c @@ -54,6 +54,7 @@ struct image *netpbm_open(FILE *f){ fgetc(f); b->f = f; + b->img.fmt = &netpbm; return (struct image *)b; } @@ -66,8 +67,14 @@ static unsigned char readvali(struct netpbm_t *b){ } static unsigned char readvalb(struct netpbm_t *b){ - int val = fgetc(b->f); - return val * 255 / b->maxval; + if(b->maxval == 65535){ + int val = fgetc(b->f) << 8; + val |= fgetc(b->f); + return val * 255 / b->maxval; + }else{ + int val = fgetc(b->f); + return val * 255 / b->maxval; + } } int netpbm_read(struct image *img){ diff --git a/src/png.c b/src/png.c index 9aab2f8..4dea588 100644 --- a/src/png.c +++ b/src/png.c @@ -57,6 +57,8 @@ struct image *png_open(FILE *f){ p->img.bufwidth = png_get_image_width(p->png_ptr, p->info_ptr); p->img.bufheight = png_get_image_height(p->png_ptr, p->info_ptr); + p->img.fmt = &libpng; + return (struct image *)p; } -- cgit v1.2.3