diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bmp.c | 2 | ||||
-rw-r--r-- | src/gif.c | 2 | ||||
-rw-r--r-- | src/imagemagick.c | 61 | ||||
-rw-r--r-- | src/jpeg.c | 3 | ||||
-rw-r--r-- | src/main.c | 8 | ||||
-rw-r--r-- | src/meh.h | 9 | ||||
-rw-r--r-- | src/netpbm.c | 11 | ||||
-rw-r--r-- | src/png.c | 2 |
8 files changed, 90 insertions, 8 deletions
@@ -102,6 +102,8 @@ struct image *bmp_open(FILE *f){ b->rowwidth += 4 - (b->rowwidth & 3); } + b->img.fmt = &bmp; + return (struct image *)b; } @@ -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 <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/wait.h> + +#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 +}; + @@ -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; } @@ -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; @@ -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){ @@ -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; } |