?

Log in

No account? Create an account

Previous Entry | Next Entry

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:

Comments

( 4 comments — Leave a comment )
nibot
Oct. 24th, 2005 09:28 pm (UTC)
delphi c? how about delphi delphi? or at least delphi object pascal.
kaolinfire
Oct. 24th, 2005 10:11 pm (UTC)
I probably couldn't tell the difference. Or is that what you're saying? :) It did look a bit like pascal. I've just only heard of Delphi associated with c, and presumed they were that much on crack.
nibot
Oct. 24th, 2005 09:33 pm (UTC)
I would just execute the ImageMagick program "identify" and parse the output.
kaolinfire
Oct. 24th, 2005 10:12 pm (UTC)
If we were on a proper machine, I'd be using php and the info would be a given. This was a test run to make sure I a) understood the code and b) the code actually worked. Now it gets translated and slapped onto a windows IIS POS.
( 4 comments — Leave a comment )

Latest Month

February 2016
S M T W T F S
 123456
78910111213
14151617181920
21222324252627
2829     

Tags

Powered by LiveJournal.com
Designed by chasethestars