-
Notifications
You must be signed in to change notification settings - Fork 1
/
jpegsize.c
73 lines (67 loc) · 1.62 KB
/
jpegsize.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* The Codes wrote by [email protected]
* I found it at http://carnage-melon.tom7.org/stuff/jpegsize.c
*/
#include <stdio.h>
/* portions derived from IJG code */
#define readbyte(a,b) do if(((a)=getc((b))) == EOF) return 0; while (0)
#define readword(a,b) do { int cc_=0,dd_=0; \
if((cc_=getc((b))) == EOF \
|| (dd_=getc((b))) == EOF) return 0; \
(a) = (cc_<<8) + (dd_); \
} while(0)
int scanhead (FILE * infile, int * image_width, int * image_height) {
int marker=0;
int dummy=0;
if ( getc(infile) != 0xFF || getc(infile) != 0xD8 ) {
return 0;
}
for (;;) {
int discarded_bytes=0;
readbyte(marker,infile);
while (marker != 0xFF) {
discarded_bytes++;
readbyte(marker,infile);
}
do readbyte(marker,infile); while (marker == 0xFF);
if (discarded_bytes != 0) return 0;
switch (marker) {
case 0xC0:
case 0xC1:
case 0xC2:
case 0xC3:
case 0xC5:
case 0xC6:
case 0xC7:
case 0xC9:
case 0xCA:
case 0xCB:
case 0xCD:
case 0xCE:
case 0xCF: {
readword(dummy,infile); /* usual parameter length count */
readbyte(dummy,infile);
readword((*image_height),infile);
readword((*image_width),infile);
readbyte(dummy,infile);
return 1;
break;
}
case 0xDA:
case 0xD9:
return 0;
default: {
int length;
readword(length,infile);
if (length < 2)
return 0;
length -= 2;
while (length > 0) {
readbyte(dummy, infile);
length--;
} // end of while
} // end of default
break;
} // end of switch
} // end of for
} // end of scanhead