-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimgsplit.c
125 lines (100 loc) · 1.96 KB
/
imgsplit.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void
usage (void)
{
fprintf (stderr, "usage: imgsplit\n");
exit (1);
}
int
PCX_D (char *src,char *dest,unsigned src_size)
{
char num;
char *orig_dest = dest;
do {
if ((*src&0xc0)==0xc0) {
num=*(src++)&0x3f; src_size--;
while ((num--)>0) {*(dest++)=*src;}
src++;src_size--;
} else {
(*dest++)=*(src++);src_size--;
};
} while(src_size);
return (dest - orig_dest);
}
int
read_img_frame (FILE *inf, char *rgb)
{
unsigned char map[256 * 3];
unsigned int len;
char compressed[64 * 1024];
int idx;
unsigned char *up;
char ipix[320 * 200];
len = fread (&map, 1, sizeof map, inf);
if (len == 0)
return (-1);
if (len != sizeof map)
goto bad;
if (fread (&len, 4, 1, inf) != 1)
goto bad;
if (len > sizeof compressed) {
fprintf (stderr, "frame too big\n");
goto bad;
}
if (fread (compressed, 1, len, inf) != len)
goto bad;
PCX_D (compressed, ipix, len);
for (idx = 0; idx < 320 * 200; idx++) {
up = map + (ipix[idx] & 0xff) * 3;
*rgb++ = *up++ * 4;
*rgb++ = *up++ * 4;
*rgb++ = *up++ * 4;
}
return (0);
bad:
fprintf (stderr, "bad img file\n");
exit (1);
}
int
main (int argc, char **argv)
{
int c;
char *inname;
FILE *inf;
int fnum;
char rgb[320 * 200 * 3];
char outname[1000];
FILE *outf;
while ((c = getopt (argc, argv, "")) != EOF) {
switch (c) {
default:
usage ();
}
}
if (optind >= argc)
usage ();
inname = argv[optind++];
if (optind != argc)
usage ();
if ((inf = fopen (inname, "rb")) == NULL) {
fprintf (stderr, "can't open %s\n", inname);
exit (1);
}
fnum = 0;
while (1) {
fnum++;
if (read_img_frame (inf, rgb) < 0)
break;
sprintf (outname, "c%02d.ppm", fnum);
if ((outf = fopen (outname, "wb")) == NULL) {
fprintf (stderr, "can't create %s\n", outname);
exit (1);
}
fprintf (outf, "P6\n320 200\n255\n");
fwrite (rgb, 1, 3 * 320 * 200, outf);
fclose (outf);
}
return (0);
}