diff options
author | John Hawthorn <jhawthor@uvic.ca> | 2008-05-19 20:04:22 -0700 |
---|---|---|
committer | John Hawthorn <jhawthor@uvic.ca> | 2008-05-19 20:04:22 -0700 |
commit | bfbeab9e9a7446ed6b40c0ad557d631c0d738c6c (patch) | |
tree | 227e1d9caded47241edb21f55151eaebe262d923 | |
parent | 02a0e01267d6c5be936b32360968199013e8096d (diff) | |
download | mirror-meh-bfbeab9e9a7446ed6b40c0ad557d631c0d738c6c.tar.gz mirror-meh-bfbeab9e9a7446ed6b40c0ad557d631c0d738c6c.tar.bz2 mirror-meh-bfbeab9e9a7446ed6b40c0ad557d631c0d738c6c.zip |
prepare for other file types
-rw-r--r-- | src/jpeg.c | 8 | ||||
-rw-r--r-- | src/jpeg.h | 2 | ||||
-rw-r--r-- | src/main.c | 38 |
3 files changed, 28 insertions, 20 deletions
@@ -19,11 +19,10 @@ static void error_exit(j_common_ptr cinfo){ exit(1); } -unsigned char *loadjpeg(char *filename, int *width, int *height){ +unsigned char *loadjpeg(FILE *infile, int *width, int *height){ struct jpeg_decompress_struct cinfo; struct error_mgr jerr; - FILE *infile; JSAMPARRAY buffer; int row_stride; int i = 0; @@ -31,11 +30,6 @@ unsigned char *loadjpeg(char *filename, int *width, int *height){ int y; unsigned char *retbuf; - if((infile = fopen(filename, "rb")) == NULL){ - fprintf(stderr, "can't open %s\n", filename); - exit(1); - } - cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = error_exit; @@ -1,3 +1,3 @@ -unsigned char *loadjpeg(char *filename, int *width, int *height); +unsigned char *loadjpeg(FILE *, int *, int *); @@ -109,7 +109,31 @@ XImage *create_image_from_buffer(unsigned char *buf, int width, int height, int XInitImage (img); return img; -} +} + +unsigned char *loadbuf(const char *filename, int *bufwidth, int *bufheight){ + unsigned char buf[4]; + FILE *f; + + if((f = fopen(filename, "rb")) == NULL){ + fprintf(stderr, "Cannot open '%s'\n", filename); + return NULL; + } + fread(buf, 1, 4, f); + rewind(f); + if(buf[0] == 0xff && buf[1] == 0xd8){ + return loadjpeg(f, bufwidth, bufheight); + }else if(!memcmp("\x89PNG", buf, 4)){ + printf("IT'S A PNG!!!\n"); + return NULL; + }else if(!memcmp("GIF", buf, 3)){ + printf("IT'S A GIF!!!\n"); + return NULL; + }else{ + fprintf(stderr, "Unknown file type: '%s'\n", filename); + return NULL; + } +} void init(){ display = XOpenDisplay (NULL); @@ -187,19 +211,11 @@ void run(char *images[], int length){ } } if(redraw){ - struct timeval tv0; - struct timeval tv1; if(!buf){ - printf("loading..."); - gettimeofday(&tv0, NULL); - buf = loadjpeg(images[i], &bufwidth, &bufheight); + buf = loadbuf(images[i], &bufwidth, &bufheight); assert(buf); - gettimeofday(&tv1, NULL); - printf(" %i milliseconds\n", (tv1.tv_sec - tv0.tv_sec) * 1000 + (tv1.tv_usec - tv0.tv_usec)/1000); } if(!img){ - printf("scaling & converting..."); - gettimeofday(&tv0, NULL); if(width * bufheight > height * bufwidth){ imagewidth = bufwidth * height / bufheight; imageheight = height; @@ -220,8 +236,6 @@ void run(char *images[], int length){ } img = create_image_from_buffer(buf, imagewidth, imageheight, bufwidth, bufheight); assert(img); - gettimeofday(&tv1, NULL); - printf(" %i milliseconds\n", (tv1.tv_sec - tv0.tv_sec) * 1000 + (tv1.tv_usec - tv0.tv_usec)/1000); } printf("Displaying\n"); XFillRectangle(display, window, gc, 0, 0, fillw, fillh); |