quasi random (kaolinfire) wrote,
quasi random
kaolinfire

in my uptime?

I just converted some delphi c to c, in the hope of helping one of my programmers write it in asp. that was disturbingly fun. :)


#include <stdio.h>

bail(unsigned char a, unsigned char b) {
	if (a != b) {
		printf("Invalid JFIF: %x != %x\n",a,b);
		exit(-1);
	}
}

int isin(unsigned char a, const unsigned char* parameterless) {
	int i;
	int found=0;
	for (i=0;i<parameterless[0] && !found;i++) {
		if (a == parameterless[i+1]) found = 1;
	}
	return found;
}

int readword(FILE* foo) {
	unsigned char b; int word=0;
	fread(&b,1,1,foo);
	word = 256 * b;
	fread(&b,1,1,foo);
	word += b;
	return word;
}

main(int argc, char** argv) {
	int i, len, width=-1, height=-1;
	FILE *foo;
	unsigned char b;
	static const unsigned char parameterless[]={8,0x01,0xd1,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7};
	static const unsigned char sig[]={2,0xff,0xd8};
	if (argc != 2) {
		printf("sizeof takes a single argument -- a jpeg filename\n");
		exit(0);
	}
	printf("attempting to open: %s\n",argv[1]);
	foo = fopen(argv[1],"rb");
	if (!foo) {
		printf("ERROR OPENING FILE\n");
	}

	for (i=0;i<sig[0];i++) {
		fread(&b,1,1,foo); 
		printf("%x\n",b);
		bail(b,sig[i+1]);
	}

	while (fread(&b,1,1,foo)) {
		while (b == 0xff) {
			fread(&b,1,1,foo);
			if (b == 0xc0 || b == 0xc1) {
				fseek(foo,3,SEEK_CUR);
				height = readword(foo);
				width = readword(foo);
			} else {
				if (!isin(b,parameterless)) {
					len = readword(foo);
					fseek(foo,len-2,SEEK_CUR);
					fread(&b,1,1,foo);
				} else {
					b = 0xff; // keep going :)
				}
			}
		}
	}
	printf("width: %d, height: %d\n",width,height);
}




All that to get the width/height of a jpeg. I spent close to four hours trying to figure it out on my own, from the JFIF specs (I don't know if this handles the SPIFF spec, but I don't seem to have any JPEGs in SPIFF). And that was after an hour or two just _digging_ for the JFIF spec (not realizing I wanted JFIF as opposed to JPEG, for instance...)
Tags: c, jifif, jpeg
Subscribe

  • feedback loops

    Ah, feedback loops. I was kind of out of sorts, yesterday, and for some reason had a lot of diet coke (to try to feel better, though I "knew" it…

  • What would I say?

    What would I say, if I were here? It's 2014, almost 2015—though on and off this year, I've been sure it was 2015. Something about that number. Next…

  • a list of games....

    A friend recently asked for a list of all the games I have available. And I'd made most of this list up a week ago, for someone else, and figured,…

  • Post a new comment

    Error

    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 4 comments