diff options
| -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);  | 
