-
Notifications
You must be signed in to change notification settings - Fork 0
/
captdefilter.c
217 lines (197 loc) · 4.78 KB
/
captdefilter.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "word.h"
#include "hiscoa-decompress.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
struct page {
size_t reserve;
size_t size;
uint8_t *data;
};
static struct page page;
static bool hi_mode = false;
static unsigned line_size;
static struct hiscoa_params hiscoa_params;
static uint8_t *page_reserve(size_t size)
{
size_t reserve = page.reserve;
while (page.size + size > reserve)
reserve *= 2;
if (reserve != page.reserve) {
page.reserve = reserve;
page.data = realloc(page.data, page.reserve);
if (! page.data)
abort();
}
return page.data + page.size;
}
static void page_add(const uint8_t *buf, size_t size)
{
memcpy(page_reserve(size), buf, size);
page.size += size;
}
static void page_output(void)
{
unsigned w = line_size * 8;
unsigned h = page.size / line_size;
printf("P4\n#\n%u %u\n", w, h);
fwrite(page.data, line_size, h, stdout);
fprintf(stderr, "output page %ux%u px\n", w, h);
page.size = 0;
}
static void dump(const uint8_t *buf, size_t size)
{
size_t i;
for (i = 1; i <= size; ++i) {
fprintf(stderr, " %02x", buf[i - 1]);
if (i % 16 == 0 || i == size)
fprintf(stderr, "\n");
}
}
static void decode_hiscoa_params(const uint8_t *buf, size_t size)
{
hiscoa_params.origin_3 = (int8_t)buf[0];
hiscoa_params.origin_5 = (int8_t)buf[1];
/* buf[2] - ??? */
/* buf[3] - ??? */
hiscoa_params.origin_0 = (int8_t) buf[4]; /* ??? */
hiscoa_params.origin_2 = (int8_t) buf[5];
hiscoa_params.origin_4 = (int16_t) WORD(buf[6], buf[7]);
}
static void decode_hiscoa_band_data(const uint8_t *buf, size_t size, unsigned lines)
{
const uint8_t *src;
size_t srcsize;
uint8_t *dest;
size_t destsize = 0;
unsigned type;
src = buf;// + 4;
srcsize = size;// - 4;
hiscoa_decompress_band((const void **)&src, &srcsize,
NULL, &destsize, line_size, &hiscoa_params);
dest = page_reserve(destsize);
src = buf;// + 4;
srcsize = size;// - 4;
type = hiscoa_decompress_band((const void **)&src, &srcsize,
dest, &destsize, line_size, &hiscoa_params);
page.size += destsize;
fprintf(stderr, " eob = %u, lines = %u, expected bytes = %u\n",
type, lines, lines * line_size);
fprintf(stderr, " unconsumed size = %u, decompressed size = %u\n",
(unsigned) srcsize, (unsigned) destsize);
}
static void decode_hiscoa_band(const uint8_t *buf, size_t size)
{
unsigned lines = WORD(buf[2], buf[3]);
decode_hiscoa_band_data(buf + 4, size - 4, lines);
}
static void dispatch(uint16_t cmd, const uint8_t *buf, size_t size)
{
switch (cmd) {
case 0xD0A9:
fprintf(stderr, " --(multi-command)--\n");
while (size) {
uint16_t cc = WORD(buf[0], buf[1]);
unsigned cs = WORD(buf[2], buf[3]);
dispatch(cc, buf + 4, cs - 4);
buf += cs;
size -= cs;
}
break;
case 0xD0A0:
fprintf(stderr, " -(compression parameters)-\n");
dump(buf, size);
line_size = WORD(buf[26], buf[27]);
fprintf(stderr, " decoded: L=%u bytes, %u pixels\n", line_size, line_size * 8);
break;
case 0xD0A4:
fprintf(stderr, " -(Hi-SCoA parameters)-\n");
dump(buf, size);
decode_hiscoa_params(buf, size);
hi_mode = true;
fprintf(stderr, " decoded: [0]=%i+L [2]=%i+L [4]=%i [3]=%i [5]=%i\n",
hiscoa_params.origin_0, hiscoa_params.origin_2,
hiscoa_params.origin_4,
hiscoa_params.origin_3, hiscoa_params.origin_5);
break;
case 0xC0A0:
if (! hi_mode) {
fprintf(stderr, " -(SCoA data)-\n");
dump(buf, 16);
} else {
fprintf(stderr, " -(Hi-SCoA data from printer debug)-\n");
dump(buf, 4);
decode_hiscoa_band_data(buf, size, 70);
}
break;
case 0x8000:
case 0x8200:
fprintf(stderr, " -(Hi-SCoA data)-\n");
dump(buf, 4);
decode_hiscoa_band(buf, size);
break;
case 0xC0A4:
fprintf(stderr, " --end--\n");
page_output();
break;
default:
dump(buf, size);
break;
}
}
int main(int argc, char **argv)
{
unsigned iband;
static uint8_t buf[1<<20];
FILE *input = stdin;
if (argc > 1) {
input = fopen(argv[1], "r");
if (! input)
abort();
}
page.reserve = 1024;
page.size = 0;
page.data = malloc(page.reserve);
if (! page.data)
abort();
while (1) {
int s;
uint16_t cmd;
uint32_t len;
size_t pos;
s = fread(buf, 1, 4, input);
if (s == 0)
break;
if (s != 4) {
fprintf(stderr, "! unable to read header\n");
break;
}
pos = 4;
cmd = WORD(buf[0], buf[1]);
switch (cmd) {
case 0x8200:
fread(buf + pos, 1, 4, input);
pos += 4;
len = WORD(buf[6], buf[7]);
len <<= 16;
len += WORD(buf[4], buf[5]);
break;
default:
len = WORD(buf[2], buf[3]);
break;
}
fprintf(stderr, "CMD %04X len=%u\n", cmd, len);
if (fread(buf + pos, 1, len - pos, input) != len - pos) {
fprintf(stderr, "! unable to read %u bytes\n", len - pos);
break;
}
dispatch(cmd, buf + pos, len - pos);
}
if (page.size)
page_output();
if (argc > 1)
fclose(input);
return 0;
}