-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
read_ndf.c
56 lines (49 loc) · 1 KB
/
read_ndf.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *ifp;
void find_jpeg()
{
unsigned short mark=0;
int c;
do mark = (mark << 8) + (c = fgetc(ifp));
while (mark != 0xffd8 && c != EOF);
}
void write_jpeg (char *iname, int index)
{
char *oname;
FILE *ofp;
unsigned short mark=0;
int c;
oname = malloc (strlen (iname) + 12);
if (!oname) return;
sprintf (oname, "%s.%d.jpg", iname, index);
if (!(ofp = fopen (oname, "wb"))) {
perror (oname);
return;
}
putc (0xff, ofp);
putc (0xd8, ofp);
do {
mark = (mark << 8) + (c = fgetc(ifp));
putc (mark, ofp);
} while (mark != 0xffd9 && c != EOF);
}
int main (int argc, char **argv)
{
int arg;
for (arg=1; arg < argc; arg++) {
if (!(ifp = fopen (argv[arg], "rb"))) {
perror (argv[arg]);
continue;
}
find_jpeg();
write_jpeg (argv[arg], 2);
find_jpeg();
write_jpeg (argv[arg], 1);
if (ftell(ifp) & 1) fgetc(ifp);
write_jpeg (argv[arg], 3);
fclose (ifp);
}
return 0;
}