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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
// fax-encoder.cc -- convert scanlines to fascimile format
// Copyright (C) 2008 SEIKO EPSON CORPORATION
//
// This file is part of the 'iscan' program.
//
// The 'iscan' program is free-ish software.
// You can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation;
// either version 2 of the License or at your option any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of FITNESS
// FOR A PARTICULAR PURPOSE or MERCHANTABILITY.
// See the GNU General Public License for more details.
//
// You should have received a verbatim copy of the GNU General Public
// License along with this program; if not, write to:
//
// Free Software Foundation, Inc.
// 59 Temple Place, Suite 330
// Boston, MA 02111-1307 USA
//
// As a special exception, the copyright holders give permission
// to link the code of this program with the esmod library and
// distribute linked combinations including the two. You must obey
// the GNU General Public License in all respects for all of the
// code used other then esmod.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "fax-encoder.hh"
#include <vector>
#include <stdint.h>
#define WHITE false
#define BLACK true
namespace iscan
{
using namespace std;
struct code {
unsigned int bits;
unsigned int code;
};
static const size_t g3_make_up_min = 64;
static const size_t g3_make_up_inc = 64;
static const size_t g3_extra_make_up_min = 1792;
static const size_t g3_extra_make_up_max = 2560;
//! Terminating codes for white runs of less than 64 pixels.
static const struct code g3_white_terminal[] =
{
{ 8, 0x35 },
{ 6, 0x07 },
{ 4, 0x07 },
{ 4, 0x08 },
{ 4, 0x0b },
{ 4, 0x0c },
{ 4, 0x0e },
{ 4, 0x0f },
{ 5, 0x13 },
{ 5, 0x14 },
{ 5, 0x07 },
{ 5, 0x08 },
{ 6, 0x08 },
{ 6, 0x03 },
{ 6, 0x34 },
{ 6, 0x35 },
{ 6, 0x2a },
{ 6, 0x2b },
{ 7, 0x27 },
{ 7, 0x0c },
{ 7, 0x08 },
{ 7, 0x17 },
{ 7, 0x03 },
{ 7, 0x04 },
{ 7, 0x28 },
{ 7, 0x2b },
{ 7, 0x13 },
{ 7, 0x24 },
{ 7, 0x18 },
{ 8, 0x02 },
{ 8, 0x03 },
{ 8, 0x1a },
{ 8, 0x1b },
{ 8, 0x12 },
{ 8, 0x13 },
{ 8, 0x14 },
{ 8, 0x15 },
{ 8, 0x16 },
{ 8, 0x17 },
{ 8, 0x28 },
{ 8, 0x29 },
{ 8, 0x2a },
{ 8, 0x2b },
{ 8, 0x2c },
{ 8, 0x2d },
{ 8, 0x04 },
{ 8, 0x05 },
{ 8, 0x0a },
{ 8, 0x0b },
{ 8, 0x52 },
{ 8, 0x53 },
{ 8, 0x54 },
{ 8, 0x55 },
{ 8, 0x24 },
{ 8, 0x25 },
{ 8, 0x58 },
{ 8, 0x59 },
{ 8, 0x5a },
{ 8, 0x5b },
{ 8, 0x4a },
{ 8, 0x4b },
{ 8, 0x32 },
{ 8, 0x33 },
{ 8, 0x34 },
};
//! Terminating codes for black runs of less than 64 pixels.
static const struct code g3_black_terminal[] =
{
{ 10, 0x37 },
{ 3, 0x02 },
{ 2, 0x03 },
{ 2, 0x02 },
{ 3, 0x03 },
{ 4, 0x03 },
{ 4, 0x02 },
{ 5, 0x03 },
{ 6, 0x05 },
{ 6, 0x04 },
{ 7, 0x04 },
{ 7, 0x05 },
{ 7, 0x07 },
{ 8, 0x04 },
{ 8, 0x07 },
{ 9, 0x18 },
{ 10, 0x17 },
{ 10, 0x18 },
{ 10, 0x08 },
{ 11, 0x67 },
{ 11, 0x68 },
{ 11, 0x6c },
{ 11, 0x37 },
{ 11, 0x28 },
{ 11, 0x17 },
{ 11, 0x18 },
{ 12, 0xca },
{ 12, 0xcb },
{ 12, 0xcc },
{ 12, 0xcd },
{ 12, 0x68 },
{ 12, 0x69 },
{ 12, 0x6a },
{ 12, 0x6b },
{ 12, 0xd2 },
{ 12, 0xd3 },
{ 12, 0xd4 },
{ 12, 0xd5 },
{ 12, 0xd6 },
{ 12, 0xd7 },
{ 12, 0x6c },
{ 12, 0x6d },
{ 12, 0xda },
{ 12, 0xdb },
{ 12, 0x54 },
{ 12, 0x55 },
{ 12, 0x56 },
{ 12, 0x57 },
{ 12, 0x64 },
{ 12, 0x65 },
{ 12, 0x52 },
{ 12, 0x53 },
{ 12, 0x24 },
{ 12, 0x37 },
{ 12, 0x38 },
{ 12, 0x27 },
{ 12, 0x28 },
{ 12, 0x58 },
{ 12, 0x59 },
{ 12, 0x2b },
{ 12, 0x2c },
{ 12, 0x5a },
{ 12, 0x66 },
{ 12, 0x67 },
};
//! Make up codes for white runs of 64 to 1728 + 63 pixels.
static const struct code g3_white_make_up[] =
{ // runlength 64 + index * 64
{ 5, 0x1b },
{ 5, 0x12 },
{ 6, 0x17 },
{ 7, 0x37 },
{ 8, 0x36 },
{ 8, 0x37 },
{ 8, 0x64 },
{ 8, 0x65 },
{ 8, 0x68 },
{ 8, 0x67 },
{ 9, 0xcc },
{ 9, 0xcd },
{ 9, 0xd2 },
{ 9, 0xd3 },
{ 9, 0xd4 },
{ 9, 0xd5 },
{ 9, 0xd6 },
{ 9, 0xd7 },
{ 9, 0xd8 },
{ 9, 0xd9 },
{ 9, 0xda },
{ 9, 0xdb },
{ 9, 0x98 },
{ 9, 0x99 },
{ 9, 0x9a },
{ 6, 0x18 },
{ 9, 0x9b },
};
//! Make up codes for black runs of 64 to 1728 + 63 pixels.
static const struct code g3_black_make_up[] =
{ // runlength 64 + index * 64
{ 10, 0x0f },
{ 12, 0xc8 },
{ 12, 0xc9 },
{ 12, 0x5b },
{ 12, 0x33 },
{ 12, 0x34 },
{ 12, 0x35 },
{ 13, 0x6c },
{ 13, 0x6d },
{ 13, 0x4a },
{ 13, 0x4b },
{ 13, 0x4c },
{ 13, 0x4d },
{ 13, 0x72 },
{ 13, 0x73 },
{ 13, 0x74 },
{ 13, 0x75 },
{ 13, 0x76 },
{ 13, 0x77 },
{ 13, 0x52 },
{ 13, 0x53 },
{ 13, 0x54 },
{ 13, 0x55 },
{ 13, 0x5a },
{ 13, 0x5b },
{ 13, 0x64 },
{ 13, 0x65 },
};
//! Additional make up codes for run of more than 1792 pixels.
static const struct code g3_extra_make_up[] =
{ // runlength 1792 + index * 64
{ 11, 0x08 },
{ 11, 0x0c },
{ 11, 0x0d },
{ 12, 0x12 },
{ 12, 0x13 },
{ 12, 0x14 },
{ 12, 0x15 },
{ 12, 0x16 },
{ 12, 0x17 },
{ 12, 0x1c },
{ 12, 0x1d },
{ 12, 0x1e },
{ 12, 0x1f },
};
static string transform (vector<size_t>& runs);
//! Converts a packed \a line of pixels into FAX G3 encoded scanline.
/*! This functions merely collects the run lengths into a vector and
passes that vector off to transform().
*/
string
fax_encoder::operator() (const byte_type* line, size_type n)
{
bool colour = WHITE;
size_t length = 0;
vector<size_t> runs;
uint8_t bit = 0x80;
n *= 8;
while (0 < n--)
{
if (colour == bool ((*line | ~bit) & bit))
{
++length;
}
else
{
runs.push_back (length);
colour = (WHITE == colour ? BLACK : WHITE);
length = 1;
}
bit >>= 1;
if (0 == bit)
{
bit = 0x80;
++line;
}
}
runs.push_back (length);
return transform (runs);
}
//! Converts a vector of run lengths into a FAX G3 encoded string.
/*! The string always starts with an end-of-line marker and will be
filled if necessary.
*/
static string
transform (vector<size_t>& runs)
{
vector<size_t>::iterator it = runs.begin ();
bool colour = WHITE;
string result;
unsigned char ch = 0x00;
size_t i = 0;
unsigned int mask = 1 << 11;
unsigned int code = 1;
while (mask)
{
if (code & mask) ch |= (1 << (7 - i % 8));
++i;
mask >>= 1;
if (0 == i % 8)
{
result.push_back (ch);
ch = 0x00;
}
}
while (runs.end () != it)
{
bool terminal = false;
do
{
const struct code *c = NULL;
if (g3_extra_make_up_max <= *it)
{
size_t index = ((g3_extra_make_up_max - g3_extra_make_up_min)
/ g3_make_up_inc);
c = g3_extra_make_up + index;
*it -= g3_extra_make_up_max;
}
else if (g3_extra_make_up_min <= *it)
{
size_t index = (*it - g3_extra_make_up_min) / g3_make_up_inc;
c = g3_extra_make_up + index;
*it -= g3_extra_make_up_min + index * g3_make_up_inc;
}
else if (g3_make_up_min <= *it)
{
size_t index = (*it - g3_make_up_min) / g3_make_up_inc;
c = ((WHITE == colour ? g3_white_make_up : g3_black_make_up)
+ index);
*it -= g3_make_up_min + index * g3_make_up_inc;
}
else
{
c = ((WHITE == colour ? g3_white_terminal : g3_black_terminal)
+ *it);
*it = 0;
terminal = true;
}
unsigned int mask = 1 << (c->bits - 1);
unsigned int code = c->code;
while (mask)
{
if (code & mask) ch |= (1 << (7 - i % 8));
++i;
mask >>= 1;
if (0 == i % 8)
{
result.push_back (ch);
ch = 0x00;
}
}
}
while (!terminal);
++it;
colour = (WHITE == colour ? BLACK : WHITE);
}
if (0 != i % 8)
result.push_back (ch);
return result;
}
} // namespace iscan
|