1 | /* pack bmp file v0.1 */
|
---|
2 |
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <stdlib.h>
|
---|
5 |
|
---|
6 |
|
---|
7 | int main(int argc, char *argv[])
|
---|
8 | {
|
---|
9 | FILE *in, *out;
|
---|
10 | int c, n, i, size;
|
---|
11 | int byte;
|
---|
12 |
|
---|
13 |
|
---|
14 | if (argc < 2)
|
---|
15 | {
|
---|
16 | in = stdin;
|
---|
17 | out = stdout;
|
---|
18 | }
|
---|
19 | else
|
---|
20 | if (argc < 3)
|
---|
21 | {
|
---|
22 | in = fopen(argv[1], "rb");
|
---|
23 | out = stdout;
|
---|
24 | }
|
---|
25 | else
|
---|
26 | if (argc < 4)
|
---|
27 | {
|
---|
28 | in = fopen(argv[1], "rb");
|
---|
29 | out = fopen(argv[2], "wb");
|
---|
30 | }
|
---|
31 |
|
---|
32 | if ((byte = fgetc(in)) == EOF)
|
---|
33 | return 0;
|
---|
34 |
|
---|
35 | fprintf(out, "PKXX");
|
---|
36 |
|
---|
37 | for(;;)
|
---|
38 | {
|
---|
39 | int count = 0;
|
---|
40 | int next;
|
---|
41 |
|
---|
42 | if ((next = fgetc(in)) == EOF)
|
---|
43 | break;
|
---|
44 |
|
---|
45 | if ((byte == next) ||
|
---|
46 | (byte & 0xC0) == 0xC0)
|
---|
47 | {
|
---|
48 | // find other bytes
|
---|
49 | count = 1;
|
---|
50 |
|
---|
51 | for(;;)
|
---|
52 | {
|
---|
53 | if (byte != next)
|
---|
54 | break;
|
---|
55 | else
|
---|
56 | count++;
|
---|
57 |
|
---|
58 | if ((next = fgetc(in)) == EOF)
|
---|
59 | break;
|
---|
60 |
|
---|
61 | if (count >= 0x3F)
|
---|
62 | break;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (count == 0 && (byte & 0xC0) != 0xC0)
|
---|
67 | {
|
---|
68 | fputc(byte, out);
|
---|
69 | }
|
---|
70 | else
|
---|
71 | {
|
---|
72 | fputc((0xC0 | count), out);
|
---|
73 | fputc(byte, out);
|
---|
74 | }
|
---|
75 | byte = next;
|
---|
76 | }
|
---|
77 |
|
---|
78 | // write header
|
---|
79 | fseek(out, 0, SEEK_END);
|
---|
80 | size = ftell(out);
|
---|
81 | fseek(out, 2, SEEK_SET);
|
---|
82 |
|
---|
83 | fwrite(&size, 2, 1, out);
|
---|
84 |
|
---|
85 | fclose(in);
|
---|
86 | fclose(out);
|
---|
87 |
|
---|
88 | return 0;
|
---|
89 | }
|
---|