-
Notifications
You must be signed in to change notification settings - Fork 0
/
jfifremove.c
51 lines (45 loc) · 1.33 KB
/
jfifremove.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
/* Strip JFIF headers from the JPEG image on stdin, and write
the result to stdout. Won't work unmodified on Windows
because of the text/binary problem. Not thoroughly tested.
Debugged version of code found here:
http://archives.devshed.com/forums/compression-130/question-about-using-jpegtran-for-lossless-compression-of-jpegs-2013044.html
*/
#include <stdio.h>
#include <stdlib.h>
void fail(const char* msg) {
fputs(msg, stderr);
exit(1);
}
void copy_rest(FILE* f, FILE* g) {
char buf[4096];
int len;
while ((len = fread(buf, 1, sizeof buf, f)) > 0) {
if (fwrite(buf, 1, len, g) != len)
fail("write error");
}
if (len < 0)
fail("read error");
}
void skip_jfif(FILE* f, FILE* g) {
int a,b;
a = getc(f); b = getc(f);
if (a != 0xFF || b != 0xD8)
fail("not a JPEG file");
putc(a,g); putc(b,g);
// 0xFFE9 is APP0 marker to begin JFIF segment
while (a = getc(f), b = getc(f), a == 0xFF && b == 0xE0) {
// Next 2 bytes after APP0 are length of JFIF segment *including* APP0
// so seek forward (0x???? - 2) bytes
a = getc(f); b = getc(f);
if (a == 0xEF || b == 0xEF)
fail("stop confusing me with weird test cases");
fseek(f, a * 256 + b - 2, SEEK_CUR);
}
if (a != 0xEF) putc(a,g);
if (b != 0xEF) putc(b,g);
copy_rest(f,g);
}
int main() {
skip_jfif(stdin,stdout);
return 0;
}