Debian Codegraph @

libhylafax/Class2Params.c++

raw
1 /* $Id$ */
2 /*
3 * Copyright (c) 1990-1996 Sam Leffler
4 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
5 * HylaFAX is a trademark of Silicon Graphics
6 *
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
14 *
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18 *
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
25 */
26 #include "config.h"
27 #include "Class2Params.h"
28 #include "Sys.h"
29 #include "t.30.h"
30
31 Class2Params::Class2Params()
32 {
33 vr = (u_int) -1;
34 br = (u_int) -1;
35 wd = (u_int) -1;
36 ln = (u_int) -1;
37 df = (u_int) -1;
38 ec = (u_int) -1;
39 bf = (u_int) -1;
40 st = (u_int) -1;
41 jp = (u_int) -1;
42 }
43
44 int
45 Class2Params::operator==(const Class2Params& other) const
46 {
47 return vr == other.vr
48 && br == other.br
49 && wd == other.wd
50 && ln == other.ln
51 && df == other.df
52 && ec == other.ec
53 && bf == other.bf
54 && st == other.st
55 && jp == other.jp;
56 }
57
58 int
59 Class2Params::operator!=(const Class2Params& other) const
60 {
61 return !(*this == other);
62 }
63
64 fxStr
65 Class2Params::cmd(bool class2UseHex, bool ecm20, bool doDFbitmap, bool useJP) const
66 {
67 u_int unset = (u_int) -1;
68 fxStr comma(",");
69 fxStr notation;
70 if (class2UseHex)
71 notation = "%X";
72 else
73 notation = "%u";
74 fxStr s;
75 if (vr != unset) s.append(fxStr::format(notation, vr));
76 s.append(comma);
77 if (br != unset) s.append(fxStr::format(notation, br));
78 s.append(comma);
79 if (wd != unset) s.append(fxStr::format(notation, wd));
80 s.append(comma);
81 if (ln != unset) s.append(fxStr::format(notation, ln));
82 s.append(comma);
83 if (doDFbitmap) {
84 /*
85 * We need to produce a T.32 Amendment 1 data format
86 * extension bitmap rather than a point-value.
87 */
88 u_int dfmap = 0;
89 if (df & BIT(DF_2DMR)) dfmap |= 0x1;
90 if (df & BIT(DF_2DMMR)) dfmap |= 0x3;
91 if (df & BIT(DF_JBIG)) dfmap |= 0xC; // JBIG L0 (4) + JBIG (8)
92 if (df != unset) s.append(fxStr::format(notation, dfmap));
93 } else
94 if (df != unset) s.append(fxStr::format(notation, df != 4 ? df : 8)); // we don't do JBIG L0
95 s.append(comma);
96 if (ec != unset) s.append(fxStr::format(notation, ec - (ecm20 && ec ? 1 : 0)));
97 s.append(comma);
98 if (bf != unset) s.append(fxStr::format(notation, bf));
99 s.append(comma);
100 if (st != unset) s.append(fxStr::format(notation, st));
101 if (useJP) {
102 s.append(comma);
103 if (df != unset) s.append(fxStr::format(notation, jp));
104 }
105 return s;
106 }
107
108 bool
109 Class2Params::is2D() const
110 {
111 return (DF_2DMR <= df && df <= DF_2DMMR);
112 }
113
114 /*
115 * Tables for mapping a T.30 DIS to Class 2
116 * subparameter code values.
117 */
118 u_int Class2Params::DISdfTab[2] = {
119 DF_1DMH, // !DIS_2DENCODE
120 DF_2DMR // DIS_2DENCODE
121 };
122 u_int Class2Params::DISvrTab[2] = {
123 VR_NORMAL, // !DIS_7MMVRES
124 VR_FINE // DIS_7MMVRES
125 };
126 /*
127 * Beware that this table returns the ``best speed''
128 * based on the signalling capabilities of the DIS.
129 */
130 u_int Class2Params::DISbrTab[16] = {
131 BR_2400, // 0x0 V27ter fall-back (BR_4800 could be assumed, T.30 Table 2 Note 3)
132 BR_14400, // 0x1 undefined
133 BR_9600, // 0x2 undefined
134 BR_14400, // 0x3 undefined
135 BR_4800, // 0x4 V27ter
136 BR_14400, // 0x5 reserved
137 BR_4800, // 0x6 reserved
138 BR_14400, // 0x7 reserved
139 BR_9600, // 0x8 V29
140 BR_14400, // 0x9 undefined
141 BR_9600, // 0xA undefined
142 BR_14400, // 0xB undefined
143 BR_9600, // 0xC V27ter+V29
144 BR_14400, // 0xD V27ter+V29+V17 (not V.33, post-1994)
145 BR_14400, // 0xE V27ter+V29+V33 (invalid, post-1994, T.30 Table 2 Notes 31, 32)
146 BR_14400, // 0xF undefined
147 };
148 u_int Class2Params::DISwdTab[4] = {
149 WD_A4, // DISWIDTH_A4
150 WD_A3, // DISWIDTH_A3
151 WD_B4 // DISWIDTH_B4
152 };
153 u_int Class2Params::DISlnTab[4] = {
154 LN_A4, // DISLENGTH_A4
155 LN_INF, // DISLENGTH_UNLIMITED
156 LN_B4, // DISLENGTH_B4
157 LN_A4 // undefined
158 };
159 u_int Class2Params::DISstTab[8] = {
160 ST_20MS, // DISMINSCAN_20MS
161 ST_40MS, // DISMINSCAN_40MS
162 ST_10MS, // DISMINSCAN_10MS
163 ST_10MS2, // DISMINSCAN_10MS2
164 ST_5MS, // DISMINSCAN_5MS
165 ST_40MS2, // DISMINSCAN_40MS2
166 ST_20MS2, // DISMINSCAN_20MS2
167 ST_0MS // DISMINSCAN_0MS
168 };
169 u_int Class2Params::DCSbrTab[16] = {
170 BR_2400, // 0x0/2400 V27
171 BR_14400, // 0x1/14400 V17
172 BR_14400, // 0x2/14400 V33
173 0, // 0x3/undefined
174 BR_4800, // 0x4/4800 V27
175 BR_12000, // 0x5/12000 V17
176 BR_12000, // 0x6/12000 V33
177 0, // 0x7/undefined
178 BR_9600, // 0x8/9600 V29
179 BR_9600, // 0x9/9600 V17
180 BR_9600, // 0xA/9600 V33
181 0, // 0xB/undefined
182 BR_7200, // 0xC/7200 V29
183 BR_7200, // 0xD/7200 V17
184 BR_7200, // 0xE/7200 V33
185 0, // 0xF/undefined
186 };
187
188 /*
189 * Convert a T.30 DIS to a Class 2 parameter block.
190 *
191 * Note that DIS is a list of *capabilities* and not necessarily maximums.
192 * Thus the values here can be set to bitmaps rather than settings.
193 */
194 void
195 Class2Params::setFromDIS(FaxParams& dis_caps)
196 {
197 assign(dis_caps);
198 u_int dis = 0;
199 u_int xinfo = 0;
200
201 dis |= getByte(0) << 16;
202 dis |= getByte(1) << 8;
203 dis |= getByte(2) << 0;
204
205 xinfo |= getByte(3) << 24;
206 xinfo |= getByte(4) << 16;
207 xinfo |= getByte(5) << 8;
208 xinfo |= getByte(6) << 0;
209
210 setFromDIS(dis, xinfo);
211
212 if (ec != EC_DISABLE) {
213 if (dis_caps.isBitEnabled(FaxParams::BITNUM_JBIG_BASIC)) df |= BIT(DF_JBIG);
214 if (dis_caps.isBitEnabled(FaxParams::BITNUM_JPEG)) jp |= BIT(JP_GREY);
215 if (dis_caps.isBitEnabled(FaxParams::BITNUM_FULLCOLOR)) {
216 if (jp & BIT(JP_GREY)) jp |= BIT(JP_COLOR);
217 }
218 }
219 }
220
221 /*
222 * Convert a T.30 DIS to a Class 2 parameter block.
223 */
224 void
225 Class2Params::setFromDIS(u_int dis, u_int xinfo)
226 {
227 // VR is a bitmap of available settings, not a maximum
228 vr = DISvrTab[(dis & DIS_7MMVRES) >> 9];
229 if (xinfo & DIS_METRES) {
230 if (xinfo & DIS_200X400) vr |= VR_R8;
231 if (xinfo & DIS_400X400) vr |= VR_R16;
232 }
233 if (xinfo & DIS_INCHRES) {
234 vr |= VR_200X100;
235 if (dis & DIS_7MMVRES) vr |= VR_200X200;
236 if (xinfo & DIS_200X400) vr |= VR_200X400;
237 }
238 if (xinfo & DIS_300X300) vr |= VR_300X300;
239 /*
240 * Beware that some modems (e.g. the Supra) indicate they
241 * support the V.17 bit rates, but not the normal V.27+V.29
242 * signalling rates. The DISbrTab is NOT setup to mark the
243 * V.27 and V.29 if V.17 is set. Instead we let the upper
244 * layers select appropriate signalling rate knowing that
245 * we'll fall back to something that the modem will support.
246 */
247 if ((dis & DIS_V8) && (xinfo & DIS_ECMODE))
248 br = BR_33600; // Is V.8 only used by V.34 (SuperG3) faxes?
249 else
250 br = DISbrTab[(dis & DIS_SIGRATE) >> 10];
251 wd = DISwdTab[(dis & DIS_PAGEWIDTH) >> 6];
252 ln = DISlnTab[(dis & DIS_PAGELENGTH) >> 4];
253
254 // DF here is a bitmap
255 df = BIT(DF_1DMH); // required support for all G3 facsimile
256 if ((xinfo & DIS_G4COMP) && (xinfo & DIS_ECMODE)) // MMR requires ECM
257 df |= BIT(DF_2DMMR);
258 if (xinfo & DIS_2DUNCOMP)
259 df |= BIT(DF_2DMRUNCOMP);
260 if (dis & DIS_2DENCODE)
261 df |= BIT(DF_2DMR);
262
263 if (xinfo & DIS_ECMODE)
264 ec = (dis & DIS_FRAMESIZE) ? EC_ENABLE64 : EC_ENABLE256;
265 else
266 ec = EC_DISABLE;
267 bf = BF_DISABLE; // XXX from xinfo
268 st = DISstTab[(dis & DIS_MINSCAN) >> 1];
269 jp = 0;
270 }
271
272 /*
273 * Convert a T.30 DCS to a Class 2 parameter block.
274 */
275 void
276 Class2Params::setFromDCS(FaxParams& dcs_caps)
277 {
278 assign(dcs_caps);
279 u_int dcs = 0;
280 u_int xinfo = 0;
281
282 dcs |= getByte(0) << 16;
283 dcs |= getByte(1) << 8;
284 dcs |= getByte(2) << 0;
285
286 xinfo |= getByte(3) << 24;
287 xinfo |= getByte(4) << 16;
288 xinfo |= getByte(5) << 8;
289 xinfo |= getByte(6) << 0;
290
291 setFromDCS(dcs, xinfo);
292
293 if (dcs_caps.isBitEnabled(FaxParams::BITNUM_LETTER_SIZE) ||
294 dcs_caps.isBitEnabled(FaxParams::BITNUM_LEGAL_SIZE)) {
295 // we map letter and legal onto WD_A4 + LN_INF for convenience
296 wd = WD_A4;
297 ln = LN_INF;
298 }
299 // Dex 855 sets MMR when also indicating JBIG. We deliberately let JBIG override.
300 if (dcs_caps.isBitEnabled(FaxParams::BITNUM_JBIG_BASIC)) df = DF_JBIG;
301 if (dcs_caps.isBitEnabled(FaxParams::BITNUM_JBIG_L0)) df = DF_JBIG;
302 if (dcs_caps.isBitEnabled(FaxParams::BITNUM_JPEG)) jp = JP_GREY;
303 if (dcs_caps.isBitEnabled(FaxParams::BITNUM_FULLCOLOR)) {
304 if (jp == JP_GREY) jp = JP_COLOR;
305 }
306 /*
307 * ITU T.30 does not specify that bits 16 (MR) or 31 (MMR) must be set to zero if color fax is used;
308 * and ITU T.32 Table 21 provides a data field, "JP", for JPEG support separate from "DF" for data
309 * format and does not specify that DF is meaningless in DCS when JP is used; but because T.4/T.6
310 * (MH/MR/MMR), JBIG, and JPEG are distinct formats from each other, we must conclude that any
311 * indication of JPEG in DCS must, therefore, invalidate any indication in DCS of MH/MR/MMR/JBIG.
312 * Otherwise, having both df and jp be non-zero will be confusing and possibly cause problems.
313 */
314 if (jp != JP_NONE) df = 0; // Yes, this is DF_1DMH, but there is no "DF_NONE".
315 if (ec == EC_DISABLE &&
316 (df == DF_2DMMR || df == DF_JBIG || jp == JP_GREY || jp == JP_COLOR)) {
317 // MMR, JBIG, and JPEG require ECM... we've seen cases where fax
318 // senders screw up and don't signal ECM but do send ECM-framed
319 // image data in the signalled format, and an RTN will break protocol,
320 // and thus a failure, so we correct the sender's mistake
321 // guessing at 256-byte ECM since 64-byte is so rarely used.
322 ec = EC_ENABLE256;
323 }
324 }
325
326 /*
327 * Convert a T.30 DCS to a Class 2 parameter block.
328 */
329 void
330 Class2Params::setFromDCS(u_int dcs, u_int xinfo)
331 {
332 setFromDIS(dcs, xinfo);
333 // override DIS setup
334 br = DCSbrTab[(dcs & DCS_SIGRATE) >> 10];
335 if (xinfo & DCS_INCHRES) {
336 if (xinfo & DCS_400X400) vr = VR_R16; // rather than adding a VR_400X400
337 else if (xinfo & DCS_300X300) vr = VR_300X300;
338 else if (xinfo & DCS_200X400) vr = VR_200X400;
339 else if (dcs & DCS_7MMVRES) vr = VR_200X200;
340 else vr = VR_200X100;
341 } else { // bit 44 of DCS is 0
342 // some manufacturers don't send DCS_INCHRES with DCS_300X300
343 if (xinfo & DCS_300X300) vr = VR_300X300;
344 else if (xinfo & DCS_400X400) vr = VR_R16;
345 else if (xinfo & DCS_200X400) vr = VR_R8;
346 else vr = DISvrTab[(dcs & DCS_7MMVRES) >> 9];
347 }
348
349 // DF here is a setting, not a bitmap, max of DF_2DMMR (JPEG, JBIG set later)
350 if (df & BIT(DF_2DMMR)) df = DF_2DMMR;
351 else if (df & BIT(DF_2DMR)) df = DF_2DMR;
352 else df = DF_1DMH;
353
354 if (xinfo & DCS_ECMODE)
355 ec = (xinfo & DCSFRAME_64) ? EC_ENABLE64 : EC_ENABLE256;
356 else
357 ec = EC_DISABLE;
358 }
359
360 #define CHECKPARAM(a, b, c) (c ? a & BIT(b) : a == b)
361
362 /*
363 * Set the T.30 DIS/DCS bits that reflects the parameters.
364 * isDIS distinguishes between DIS and DCS
365 *
366 * Class2Params may be capability bitmaps (modemParams) or may be
367 * settings (params), we must be capable of handling both.
368 */
369 void
370 Class2Params::update(bool isDIS)
371 {
372 setupT30(0,0);
373 setBit(BITNUM_T4RCVR, true); // Table 2-T.30 Notes 19 & 20
374 /*
375 * RESOLUTIONS
376 *
377 * We set both resolution units preferences in order to
378 * allow the sender to choose between them. If it has
379 * prepared an inch-based document, that's how we want it.
380 * VR is bitmapped natively in all cases.
381 */
382 if (vr & VR_FINE || vr & VR_200X200) setBit(BITNUM_VR_FINE, true); // R8 x 7.7 l/mm
383 if (vr & VR_R8 || vr & VR_200X400) setBit(BITNUM_VR_R8, true); // R8 x 15.4 l/mm
384 if (vr & VR_R16) setBit(BITNUM_VR_R16, true); // R16 x 15.4 l/mm
385 if (vr & VR_300X300) setBit(BITNUM_VR_300X300, true);// 300 x 300 dpi
386 if (!isDIS && (vr & VR_200X200 || vr & VR_200X400 || vr & VR_300X300)) {
387 setBit(BITNUM_INCH_RES, true); // indicate inch-based resolution usage
388 }
389
390 /*
391 * SIGNALING RATE
392 *
393 * Converting br into a meaningful T.30
394 * representation is an inexact science because
395 * 1) V.17 speeds overlap V.33 and V.29, and
396 * 2) br indicates speed, and T.30
397 * indicates modulation also, and
398 * 3) V.34 overlaps V.17, V.33, V.29, and V.27.
399 *
400 * As V.33 is no longer part of T.30 as of 1994,
401 * we do not set it here.
402 *
403 * V.17 support requires both V.29 and V.27 support.
404 * V.29 and V.27 support are independent.
405 * V.34 requires V.8 support.
406 *
407 * The imperfect assumptions are that:
408 * BR_2400 -- BR_4800 are V.27
409 * BR_7200 -- BR_9600 are V.29
410 * BR_12000 - BR_14400 are V.17
411 * BR_16800 - BR_33600 are V.34
412 *
413 * The protocol drivers will certainly need to reproduce
414 * this independently.
415 */
416 if (isDIS) { // DIS
417 if (CHECKPARAM(br, BR_14400, true)) setBit(BITNUM_SIGRATE_14, true);
418 if (CHECKPARAM(br, BR_9600, true)) setBit(BITNUM_SIGRATE_11, true);
419 if (CHECKPARAM(br, BR_4800, true)) setBit(BITNUM_SIGRATE_12, true);
420 if (CHECKPARAM(br, BR_33600, true)) setBit(BITNUM_V8_CAPABLE, true);
421 } else { // DCS
422 // no bits set for V.34 rates and V.27ter 2400
423 if (CHECKPARAM(br, BR_14400, false)) setBit(BITNUM_SIGRATE_14, true);
424 if (CHECKPARAM(br, BR_12000, false)) {
425 setBit(BITNUM_SIGRATE_14, true);
426 setBit(BITNUM_SIGRATE_12, true);
427 }
428 if (CHECKPARAM(br, BR_9600, false)) setBit(BITNUM_SIGRATE_11, true);
429 if (CHECKPARAM(br, BR_7200, false)) {
430 setBit(BITNUM_SIGRATE_11, true);
431 setBit(BITNUM_SIGRATE_12, true);
432 }
433 if (CHECKPARAM(br, BR_4800, false)) setBit(BITNUM_SIGRATE_12, true);
434 }
435
436 /*
437 * RECORDING WIDTH
438 *
439 * Post-1994 T.30 revisions have three page width
440 * options: A4 (215 mm), B4 (255 mm), and A3 (303 mm).
441 * Earlier revisions also included A5 (151 mm) and
442 * A6 (107 mm).
443 *
444 * Trying to support A5 and A6 is likely to only
445 * be counterproductive, thus we do not support them.
446 *
447 * A3 support requires B4 support.
448 * A4 support is required.
449 */
450 if (CHECKPARAM(wd, WD_A3, isDIS)) setBit(BITNUM_WIDTH_18, true);
451 else if (CHECKPARAM(wd, WD_B4, isDIS)) setBit(BITNUM_WIDTH_17, true);
452
453 /*
454 * RECORDING LENGTH
455 *
456 * Three possibilities: A4 (297 mm), B4 (364 mm), and unlimited.
457 * A4 page length support is required.
458 *
459 * Using "unlimited" page length is the only way to get the popular
460 * "US-Legal" page size.
461 */
462 if (CHECKPARAM(ln, LN_INF, isDIS)) setBit(BITNUM_LENGTH_20, true);
463 else if (CHECKPARAM(ln, LN_B4, isDIS)) setBit(BITNUM_LENGTH_19, true);
464
465 /*
466 * DATA FORMAT
467 *
468 * Options: MH (required), MR, MMR, JBIG, JPEG.
469 *
470 * ECM support is required for MMR, JBIG, and JPEG.
471 *
472 * There are other data format options in T.30
473 * such as T.81, and T.43.
474 */
475 if (CHECKPARAM(df, DF_2DMR, isDIS)) setBit(BITNUM_2DMR, true);
476 if (CHECKPARAM(df, DF_2DMMR, isDIS) && (CHECKPARAM(ec, EC_ENABLE64, isDIS) || CHECKPARAM(ec, EC_ENABLE256, isDIS)))
477 setBit(BITNUM_2DMMR, true);
478 if (CHECKPARAM(df, DF_JBIG, isDIS) && (CHECKPARAM(ec, EC_ENABLE64, isDIS) || CHECKPARAM(ec, EC_ENABLE256, isDIS)))
479 setBit(BITNUM_JBIG_BASIC, true);
480 if (CHECKPARAM(jp, JP_GREY, isDIS) && (CHECKPARAM(ec, EC_ENABLE64, isDIS) || CHECKPARAM(ec, EC_ENABLE256, isDIS)))
481 setBit(BITNUM_JPEG, true);
482 if (CHECKPARAM(jp, JP_COLOR, isDIS) && (CHECKPARAM(ec, EC_ENABLE64, isDIS) || CHECKPARAM(ec, EC_ENABLE256, isDIS))) {
483 setBit(BITNUM_JPEG, true);
484 setBit(BITNUM_FULLCOLOR, true);
485 }
486
487 /*
488 * MINIMUM SCANLINE TIME
489 */
490 if (CHECKPARAM(st, ST_5MS, isDIS) ||
491 CHECKPARAM(st, ST_20MS2, isDIS) ||
492 CHECKPARAM(st, ST_40MS2, isDIS) ||
493 CHECKPARAM(st, ST_0MS, isDIS)) {
494 setBit(BITNUM_ST_21, true);
495 }
496 if (CHECKPARAM(st, ST_10MS, isDIS) ||
497 CHECKPARAM(st, ST_10MS2, isDIS) ||
498 CHECKPARAM(st, ST_20MS2, isDIS) ||
499 CHECKPARAM(st, ST_0MS, isDIS)) {
500 setBit(BITNUM_ST_22, true);
501 }
502 if (CHECKPARAM(st, ST_40MS, isDIS) ||
503 CHECKPARAM(st, ST_10MS2, isDIS) ||
504 CHECKPARAM(st, ST_40MS2, isDIS) ||
505 CHECKPARAM(st, ST_0MS, isDIS)) {
506 setBit(BITNUM_ST_23, true);
507 }
508
509 /*
510 * ECM
511 *
512 * 64-byte and 256-byte frame-size options.
513 * A receiver must support both or neither.
514 *
515 * A sender must support no ECM. Senders
516 * should honor the preference of the receiver.
517 */
518 if (CHECKPARAM(ec, EC_ENABLE64, isDIS)) {
519 setBit(BITNUM_ECM, true);
520 if (isDIS) setBit(BITNUM_FRAMESIZE_DIS, true);
521 else setBit(BITNUM_FRAMESIZE_DCS, true);
522 }
523 if (CHECKPARAM(ec, EC_ENABLE256, isDIS)) {
524 setBit(BITNUM_ECM, true);
525 if (isDIS) setBit(BITNUM_FRAMESIZE_DIS, false);
526 else setBit(BITNUM_FRAMESIZE_DCS, false);
527 }
528 }
529
530 /*
531 * Return the number of bytes that can be
532 * transferred at the selected signalling
533 * rate in <ms> milliseconds.
534 */
535 u_int
536 Class2Params::transferSize(u_int ms) const
537 {
538 return (bitRate()/8 * ms) / 1000;
539 }
540
541 /*
542 * Return the minimum number of bytes in a
543 * scanline as determined by the signalling
544 * rate, vertical resolution, and min-scanline
545 * time parameters.
546 */
547 u_int
548 Class2Params::minScanlineSize() const
549 {
550 static const u_int stTimes[8] =
551 { 0, 5, 10, 10, 20, 20, 40, 40 };
552 u_int ms = stTimes[st&7];
553 if ((st & 1) == 0 && vr > VR_NORMAL)
554 ms /= 2;
555 return transferSize(ms);
556 }
557
558 u_int
559 Class2Params::pageWidth() const
560 {
561 u_int widths[8] = {
562 1728, // 1728 in 215 mm line
563 2048, // 2048 in 255 mm line
564 2432, // 2432 in 303 mm line
565 1216, // 1216 in 151 mm line
566 864, // 864 in 107 mm line
567 1728, // undefined
568 1728, // undefined
569 1728, // undefined
570 };
571 switch (vr) {
572 case VR_300X300:
573 widths[0] = 2592;
574 widths[1] = 3072;
575 widths[2] = 3648;
576 widths[3] = 1824;
577 widths[4] = 1296;
578 break;
579 case VR_R16:
580 widths[0] = 3456;
581 widths[1] = 4096;
582 widths[2] = 4864;
583 widths[3] = 2432;
584 widths[4] = 1728;
585 break;
586 case VR_NORMAL:
587 case VR_FINE:
588 case VR_R8:
589 case VR_200X100:
590 case VR_200X200:
591 case VR_200X400:
592 // nothing
593 break;
594 }
595 return (widths[wd&7]);
596 }
597
598 void
599 Class2Params::setPageWidthInMM(u_int w)
600 {
601 /*
602 * We get the width in MM from the q file. Basically,
603 * faxing only deals with three page widths defined by
604 * pixel-widths: 1728, 2048, and 2432 (in R8 resolution).
605 * The associated page size names are A4, B4, and A3.
606 * However, the calculated size in millimeters differs
607 * between pixel-widths and page size.
608 * 1728 pixels = 215 mm : A4 = 209.903 mm
609 * 2048 pixels = 255 mm : B4 = 250.119 mm
610 * 2432 pixels = 303 mm : A3 = 297.039 mm
611 * The client program may therefore use a range of
612 * pagewidth (mm) values. We must interpret wisely.
613 */
614 wd = (w > 270 ? WD_A3 : w > 230 ? WD_B4 : WD_A4);
615 }
616
617 void
618 Class2Params::setPageWidthInPixels(u_int w)
619 {
620 /*
621 * Here we attempt to determine the WD parameter with
622 * a pixel width which is impossible to be perfect
623 * because there are colliding values. However,
624 * since we don't use > WD_A3, this is fine.
625 */
626 wd = (w == 1728 ? WD_A4 :
627 w == 2048 ? WD_B4 :
628 w == 2432 ? WD_A3 :
629 w == 3456 ? WD_A4 :
630 w == 4096 ? WD_B4 :
631 w == 4864 ? WD_A3 :
632 w == 2592 ? WD_A4 :
633 w == 3072 ? WD_B4 :
634 w == 3648 ? WD_A3 :
635 WD_A4);
636 }
637
638 u_int
639 Class2Params::pageLength() const
640 {
641 static const u_int lengths[4] = {
642 297, // A4 paper
643 364, // B4 paper
644 (u_int) -1, // unlimited
645 280, // US letter (used internally)
646 };
647 return (lengths[ln&3]);
648 }
649
650 void
651 Class2Params::setPageLengthInMM(u_int l)
652 {
653 ln = (l == (u_int) -1 ? LN_INF :
654 l <= 280 ? LN_LET :
655 l <= 300 ? LN_A4 :
656 l <= 380 ? LN_B4 :
657 LN_INF);
658 }
659
660 u_int
661 Class2Params::horizontalRes() const
662 {
663 /*
664 * Technically horizontal resolution depends upon the
665 * the number of pixels across the page and the page width.
666 * But, these are just used for writing TIFF tags for
667 * received faxes, so we do this to accomodate the session
668 * parameters, even though it may be slightly off.
669 */
670 return (vr == VR_NORMAL ? 204 :
671 vr == VR_FINE ? 204 :
672 vr == VR_R8 ? 204 :
673 vr == VR_R16 ? 408 :
674 vr == VR_200X100 ? 200 :
675 vr == VR_200X200 ? 200 :
676 vr == VR_200X400 ? 200 :
677 vr == VR_300X300 ? 300 :
678 (u_int) -1);
679 }
680
681 u_int
682 Class2Params::verticalRes() const
683 {
684 return (vr == VR_NORMAL ? 98 :
685 vr == VR_FINE ? 196 :
686 vr == VR_R8 ? 391 :
687 vr == VR_R16 ? 391 :
688 vr == VR_200X100 ? 100 :
689 vr == VR_200X200 ? 200 :
690 vr == VR_200X400 ? 400 :
691 vr == VR_300X300 ? 300 :
692 (u_int) -1);
693 }
694
695 void
696 Class2Params::setRes(u_int xres, u_int yres)
697 {
698 vr = (xres > 300 && yres > 391 ? VR_R16 :
699 xres > 204 && yres > 250 ? VR_300X300 :
700 yres > 391 ? VR_200X400 :
701 yres > 250 ? VR_R8 :
702 yres > 196 ? VR_200X200 :
703 yres > 150 ? VR_FINE :
704 yres > 98 ? VR_200X100 : VR_NORMAL);
705 }
706
707 fxStr
708 Class2Params::encodePage() const
709 {
710 int v = (vr&3) | ((wd&7)<<1) | ((ln&3)<<4) | ((df&3)<<6);
711 return fxStr(v, "%02x");
712 }
713
714 void
715 Class2Params::decodePage(const char* s)
716 {
717 u_int v = (u_int) strtoul(s, NULL, 16);
718 vr = (v>>0) & 1;
719 wd = (v>>1) & 7;
720 ln = (v>>4) & 3;
721 if (ln == LN_LET) // force protocol value
722 ln = LN_A4;
723 df = (v>>6) & 3;
724 }
725
726 u_int
727 Class2Params::encode() const
728 {
729 return (vr > 4 ? ((vr>>4)&7)<<0 : (vr&7)<<0) // push inch resolutions
730 | ((br&15)<<3)
731 | ((wd&7)<<9)
732 | ((ln&3)<<12)
733 | ((df&3)<<14)
734 | ((ec == EC_DISABLE ? 0 : 1)<<16) // boolean value
735 | ((bf&1)<<17)
736 | ((st&7)<<18)
737 | (1<<21) // this is the version identifier
738 ;
739 }
740
741 void
742 Class2Params::decode(u_int v)
743 {
744 if (v>>21 == 1) { // check version
745 vr = ((v>>0) & 7); // VR is a bitmap
746 br = (v>>3) & 15;
747 wd = (v>>9) & 7;
748 ln = (v>>12) & 3;
749 if (ln == LN_LET) // force protocol value
750 ln = LN_A4;
751 df = (v>>14) & 3;
752 ec = (v>>16) & 1;
753 bf = (v>>17) & 1;
754 st = (v>>18) & 7;
755 } else { // original version
756 vr = (v>>0) & 1;
757 br = (v>>1) & 7;
758 wd = (v>>4) & 7;
759 ln = (v>>7) & 3;
760 if (ln == LN_LET) // force protocol value
761 ln = LN_A4;
762 df = (v>>9) & 3;
763 ec = (v>>11) & 1;
764 bf = (v>>12) & 1;
765 st = (v>>13) & 7;
766 }
767 }
768
769 u_int
770 Class2Params::getMinSpeed() const
771 {
772 u_int minspeed = BR_14400;
773 for (int speed = BR_14400; speed >= BR_2400; speed--) {
774 if (br & BIT(speed)) minspeed = speed;
775 }
776 return (minspeed);
777 }
778
779 u_int
780 Class2Params::encodeCaps() const
781 {
782 // we can't use all of BR_ALL because we're limited to 32 bits
783 // all ECM support is reduced to "1" (EC_ENABLE64) if any ECM is available
784 return (vr&VR_ALL)
785 | ((br&(BR_2400|BR_4800|BR_7200|BR_9600|BR_12000|BR_14400))<<8)
786 | ((wd&WD_ALL)<<14)
787 | ((ln&LN_ALL)<<19)
788 | ((df&DF_ALL)<<22)
789 | ((ec&EC_DISABLE)<<26)
790 | ((ec&EC_ALL != 0 ? 1 : 0)<<27)
791 | ((bf&BF_ALL)<<28)
792 | ((st&ST_ALL)<<30)
793 ;
794 }
795
796 void
797 Class2Params::decodeCaps(u_int v)
798 {
799 vr = (v>>0) & VR_ALL;
800 br = (v>>8) & (BR_2400|BR_4800|BR_7200|BR_9600|BR_12000|BR_14400); // see encoding
801 wd = (v>>14) & WD_ALL;
802 ln = (v>>19) & LN_ALL;
803 df = (v>>22) & DF_ALL;
804 ec = (v>>26) & (EC_DISABLE|EC_ENABLE64); // see encoding
805 bf = (v>>28) & BF_ALL;
806 st = (v>>30) & ST_ALL;
807 }
808
809 /*
810 * Routines for printing some Class 2 capabilities.
811 */
812 const char* Class2Params::verticalResNames[65] = {
813 N_("3.85 line/mm"),
814 N_("7.7 line/mm"),
815 N_("15.4 line/mm"), "",
816 N_("R16 x 15.4 line/mm"), "", "", "",
817 N_("200 x 100 dpi"), "", "", "", "", "", "", "",
818 N_("200 x 200 dpi"), "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
819 N_("200 x 400 dpi"), "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
820 N_("300 x 300 dpi")
821 };
822
823 const char* Class2Params::verticalResName() const
824 { return (verticalResNames[vr & VR_ALL]); }
825
826 const char* Class2Params::bestVerticalResName() const
827 {
828 /*
829 * "Best" is determined by comparing pixels per square
830 * area of image. Thus 300x300 is "better" than 200x400.
831 */
832 u_int res = VR_NORMAL; // required default
833 if (vr & VR_200X100) res = VR_200X100;
834 if (vr & VR_FINE) res = VR_FINE;
835 if (vr & VR_200X200) res = VR_200X200;
836 if (vr & VR_R8) res = VR_R8;
837 if (vr & VR_200X400) res = VR_200X400;
838 if (vr & VR_300X300) res = VR_300X300;
839 if (vr & VR_R16) res = VR_R16;
840 return(verticalResNames[res]);
841 }
842
843
844 const char* Class2Params::bitRateNames[16] = {
845 N_("2400 bit/s"), // BR_2400
846 N_("4800 bit/s"), // BR_4800
847 N_("7200 bit/s"), // BR_7200
848 N_("9600 bit/s"), // BR_9600
849 N_("12000 bit/s"), // BR_12000
850 N_("14400 bit/s"), // BR_14400
851 N_("16800 bit/s"), // BR_16800
852 N_("19200 bit/s"), // BR_19200
853 N_("21600 bit/s"), // BR_21600
854 N_("24000 bit/s"), // BR_24000
855 N_("26400 bit/s"), // BR_26400
856 N_("28800 bit/s"), // BR_28800
857 N_("31200 bit/s"), // BR_31200
858 N_("33600 bit/s"), // BR_33600
859 N_("0 bit/s"), // 14 ???
860 N_("0 bit/s"), // 15 ???
861 };
862 const char* Class2Params::bitRateName() const
863 { return (bitRateNames[br&15]); }
864 u_int
865 Class2Params::bitRate() const
866 {
867 static const u_int brRates[16] = {
868 2400, // BR_2400
869 4800, // BR_4800
870 7200, // BR_7200
871 9600, // BR_9600
872 12000, // BR_12000
873 14400, // BR_14400
874 16800, // BR_16800
875 19200, // BR_19200
876 21600, // BR_21600
877 24000, // BR_24000
878 26400, // BR_26400
879 28800, // BR_28800
880 31200, // BR_31200
881 33600, // BR_33600
882 14400, // 14? XXX
883 14400, // 15? XXX
884 };
885 return (brRates[br & 15]);
886 }
887
888 const char* Class2Params::dataFormatNames[7] = {
889 N_("1-D MH"), // DF_1DMH
890 N_("2-D MR"), // DF_2DMR
891 N_("2-D Uncompressed Mode"), // DF_2DMRUNCOMP
892 N_("2-D MMR"), // DF_2DMMR
893 N_("JBIG"), // DF_JBIG
894 N_("JPEG Greyscale"), // JP_GREY
895 N_("JPEG Full-Color") // JP_COLOR
896 };
897 const char* Class2Params::dataFormatName() const
898 {
899 u_int dfid = df+(jp > 0 && jp < (u_int) -1 ? jp + 4 : 0);
900 return (dataFormatNames[dfid > 6 ? 0 : dfid]);
901 }
902
903 fxStr
904 Class2Params::dataFormatsName()
905 {
906 fxStr formats = N_("MH");
907 if (df & BIT(DF_2DMR)) formats.append(N_(", MR"));
908 if (df & BIT(DF_2DMMR)) formats.append(N_(", MMR"));
909 if (df & BIT(DF_JBIG)) formats.append(N_(", JBIG"));
910 // since color requires greyscale, just say one or the other
911 if (jp & BIT(JP_COLOR)) formats.append(N_(", JPEG Full-Color"));
912 else if (jp & BIT(JP_GREY)) formats.append(N_(", JPEG Greyscale"));
913 return (formats);
914 }
915
916 const char* Class2Params::pageWidthNames[8] = {
917 N_("A4 page width (215 mm)"),
918 N_("B4 page width (255 mm)"),
919 N_("A3 page width (303 mm)"),
920 N_("page width 151 mm"),
921 N_("page width 107 mm"),
922 N_("undefined page width (wd=5)"),
923 N_("undefined page width (wd=6)"),
924 N_("undefined page width (wd=7)"),
925 };
926 const char* Class2Params::pageWidthName() const
927 { return (pageWidthNames[wd&7]); }
928
929 const char* Class2Params::pageLengthNames[4] = {
930 N_("A4 page length (297 mm)"),
931 N_("B4 page length (364 mm)"),
932 N_("unlimited page length"),
933 N_("invalid page length (ln=3)"),
934 };
935 const char* Class2Params::pageLengthName() const
936 { return (pageLengthNames[ln&3]); }
937
938 const char* Class2Params::scanlineTimeNames[8] = {
939 N_("0 ms/scanline"),
940 N_("5 ms/scanline"),
941 N_("10 ms, 5 ms/scanline"),
942 N_("10 ms/scanline"),
943 N_("20 ms, 10 ms/scanline"),
944 N_("20 ms/scanline"),
945 N_("40 ms, 20 ms/scanline"),
946 N_("40 ms/scanline"),
947 };
948 const char* Class2Params::scanlineTimeName() const
949 { return (scanlineTimeNames[st&7]); }
950 const char* Class2Params::ecmNames[8] = {
951 N_("no ECM"),
952 N_("T.30 Annex A, 64-byte ECM"),
953 N_("T.30 Annex A, 256-byte ECM"),
954 N_("T.30 Annex C, half duplex ECM"),
955 N_("T.30 Annex C, full duplex ECM"),
956 N_("undefined ECM"),
957 N_("undefined ECM"),
958 N_("undefined ECM")
959 };
960 const char* Class2Params::ecmName() const
961 { return (ecmNames[ec&7]); }