#include <stddef.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <jpeglib.h>
#include <jerror.h>
#include <string.h>
void commandline_error(char *progname) {
	fprintf(stderr,"%s: Error. Command line arguments missing or invalid. Usage:\n",progname);
	fprintf(stderr,"%s [-whbs|--html] foo.jpg\n",progname);
	fprintf(stderr,"Flag:	Effect:\n");
	fprintf(stderr," -w     outputs width of image\n");
	fprintf(stderr," -h     outputs height of image\n");
	fprintf(stderr," -b     outputs width x height\n");
    fprintf(stderr," -s     suppress newline after output\n");
	fprintf(stderr," --html	outputs width and height appropriate for use in HTML <img> tags\n");
	fprintf(stderr,"\nFor coding convenience please put separate each flag\n");
	exit(0);
}


int main(int nargs, char *arg[]) {
	int i;
	char *filename = 0;
	/* create a struct to hold command line information */
	struct {
		unsigned int generate_width:1;
		unsigned int generate_height:1;
		unsigned int generate_html:1;
		unsigned int suppress_newline:1;
	} flags;

	/* libjpeg stuff */
		/* this is the object that holds all image information */
	struct jpeg_decompress_struct cinfo;
		/* the error manager */
    struct jpeg_error_mgr jerr;

	/* The file to read */
	FILE * infile;

	/* tells the image where to send its errors */
   	cinfo.err = jpeg_std_error(&jerr);
	/* make the decompressed image object */
    jpeg_create_decompress(&cinfo);


	if (nargs == 1) commandline_error(arg[0]);

	/* initialize the struct */
	flags.generate_width=0;
	flags.generate_height=0;
	flags.generate_html=0;
	flags.suppress_newline=0;
	
	/* parse the command line input */
	for (i=1;i<nargs;i++) {
		if ((strcmp("-w",arg[i])==0)&&(flags.generate_html==0)) {
			flags.generate_width=1;
		} else if ((strcmp("-h",arg[i])==0)&&(flags.generate_html==0)) {
			flags.generate_height=1;
		} else if ((strcmp("-b",arg[i])==0)&&(flags.generate_html==0)) {
			flags.generate_width=1;
			flags.generate_height=1;
		} else if ((strcmp("-html",arg[i])==0)&&(flags.generate_width==0)&&(flags.generate_height==0)) {
			flags.generate_html=1;
		} else if ((strcmp("-s",arg[i])==0)) {
			flags.suppress_newline=1;
		} else if (i== nargs-1) {
			filename = arg[i];
		} else {
			commandline_error(arg[0]);
		}
	}


	
	if (filename == 0) {
		fprintf(stderr,"No file specified!\n");
		exit(0);
	}

	if ((flags.generate_html==0)&&(flags.generate_width==0)&&(flags.generate_height==0)) {
		flags.generate_html = 1;
	}

    if ((infile = fopen(filename, "rb")) == NULL) {
        fprintf(stderr, "can't open %s\n", filename);
        exit(1);
    }

	/* tell the image where to find it's information/data */
    jpeg_stdio_src(&cinfo, infile);		

	/* read in the image header */
	jpeg_read_header(&cinfo, TRUE);

	/* output the relavent information */
	if (flags.generate_width) {
		printf("%d",cinfo.image_width);
	} 
	
	if (flags.generate_width && flags.generate_height) {
		printf(" x ");
	} 
	
	if (flags.generate_height) {
		printf("%d",cinfo.image_height);
	} 
	
	if (flags.generate_html) {
		printf("WIDTH=%d HEIGHT=%d",cinfo.image_width,cinfo.image_height);
	} 
	
	if (flags.suppress_newline == 0) {
		printf("\n");
	}
	
	/* we're done with this image so we destroy it */
	jpeg_destroy_decompress(&cinfo);
	return 0;
}
