| 1 |
/* $Id$ */ |
| 2 |
/* |
| 3 |
* Copyright (c) 1990-1996 Sam Leffler |
| 4 |
* Copyright (c) 1991-1996 Silicon Graphics, Inc. |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, distribute, and sell this software and |
| 7 |
* its documentation for any purpose is hereby granted without fee, provided |
| 8 |
* that (i) the above copyright notices and this permission notice appear in |
| 9 |
* all copies of the software and related documentation, and (ii) the names of |
| 10 |
* Sam Leffler and Silicon Graphics may not be used in any advertising or |
| 11 |
* publicity relating to the software without the specific, prior written |
| 12 |
* permission of Sam Leffler and Silicon Graphics. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
| 15 |
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
| 16 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 17 |
* |
| 18 |
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
| 19 |
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
| 20 |
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
| 21 |
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
| 22 |
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
| 23 |
* OF THIS SOFTWARE. |
| 24 |
*/ |
| 25 |
#include <sys/types.h> |
| 26 |
#include <sys/stat.h> |
| 27 |
#include <time.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <ctype.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <string.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include "tiffio.h" |
| 34 |
|
| 35 |
#include "PageSize.h" |
| 36 |
#include "Class2Params.h" |
| 37 |
#include "CallID.h" |
| 38 |
#include "Sys.h" |
| 39 |
|
| 40 |
#include "port.h" |
| 41 |
|
| 42 |
extern const char* fmtTime(time_t t); |
| 43 |
|
| 44 |
/* |
| 45 |
* This tests whether the tif file is a "fax" image. |
| 46 |
* Traditional fax images are MH, MR, MMR, but some can |
| 47 |
* be JPEG, JBIG, and possibly others. So we use |
| 48 |
* TIFFTAG_FAXRECVPARAMS as a "fax" image identifier, |
| 49 |
* and if it's not there, then we resort to traditional |
| 50 |
* tactics. |
| 51 |
*/ |
| 52 |
static bool |
| 53 |
isFAXImage(TIFF* tif) |
| 54 |
{ |
| 55 |
#ifdef TIFFTAG_FAXRECVPARAMS |
| 56 |
uint32 v; |
| 57 |
if (TIFFGetField(tif, TIFFTAG_FAXRECVPARAMS, &v) && v != 0) |
| 58 |
return (true); |
| 59 |
#endif |
| 60 |
uint16 w; |
| 61 |
if (TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &w) && w != 1) |
| 62 |
return (false); |
| 63 |
if (TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &w) && w != 1) |
| 64 |
return (false); |
| 65 |
if (!TIFFGetField(tif, TIFFTAG_COMPRESSION, &w) || |
| 66 |
(w != COMPRESSION_CCITTFAX3 && w != COMPRESSION_CCITTFAX4)) |
| 67 |
return (false); |
| 68 |
if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &w) || |
| 69 |
(w != PHOTOMETRIC_MINISWHITE && w != PHOTOMETRIC_MINISBLACK)) |
| 70 |
return (false); |
| 71 |
return (true); |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
sanitize(fxStr& s) |
| 76 |
{ |
| 77 |
for(u_int i = 0; i < s.length(); i++) { |
| 78 |
if (!isascii(s[i]) || !isprint(s[i])) s[i] = '?'; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
static void |
| 83 |
usage (const char* app) |
| 84 |
{ |
| 85 |
printf(_("usage: %s [-n] [-S fmt] [-s fmt] [-e fmt] [-E fmt] [-D]\n\n"), app); |
| 86 |
printf(_("\t-n\tPrint FAX filename\n")); |
| 87 |
printf(_("\t-C d\tQuoted CSV-style output with <d> as the deliminator\n")); |
| 88 |
printf(_("\t-c d\tCSV-style output with <d> as the deliminator\n")); |
| 89 |
printf(_("\t-r\traw format - values outputed with no names\n")); |
| 90 |
|
| 91 |
printf(_(" Raw format options:\n")); |
| 92 |
printf(_("\t-S fmt\tUse fmt for the fax start format\n")); |
| 93 |
printf(_("\t-s fmt\tUse fmt for the field start format\n")); |
| 94 |
printf(_("\t-e fmt\tUse fmt for the field end format\n")); |
| 95 |
printf(_("\t-E fmt\tUse fmt for the fax end format\n")); |
| 96 |
} |
| 97 |
|
| 98 |
static const char* |
| 99 |
escapedString (const char*src) |
| 100 |
{ |
| 101 |
char* res; |
| 102 |
int len; |
| 103 |
len = strlen(src); |
| 104 |
res = (char*)malloc(len+1); |
| 105 |
if (res) |
| 106 |
{ |
| 107 |
char* dst = res; |
| 108 |
for (int i = 0; i <= len; i++) |
| 109 |
{ |
| 110 |
switch (src[i]) |
| 111 |
{ |
| 112 |
case '\\': |
| 113 |
switch (src[++i]) |
| 114 |
{ |
| 115 |
case 'n': *dst++ = '\n'; break; |
| 116 |
case 'r': *dst++ = '\r'; break; |
| 117 |
case 't': *dst++ = '\t'; break; |
| 118 |
default: |
| 119 |
*dst++ = src[i]; |
| 120 |
} |
| 121 |
break; |
| 122 |
case '%': |
| 123 |
*dst++ = src[i]; |
| 124 |
switch (src[++i]) |
| 125 |
{ |
| 126 |
case 'n': *dst++ = '%'; break; |
| 127 |
default: |
| 128 |
*dst++ = src[i]; |
| 129 |
} |
| 130 |
break; |
| 131 |
|
| 132 |
default: |
| 133 |
*dst++ = src[i]; |
| 134 |
} |
| 135 |
} |
| 136 |
} else |
| 137 |
{ |
| 138 |
exit(ENOMEM); |
| 139 |
} |
| 140 |
return res; |
| 141 |
} |
| 142 |
|
| 143 |
static const char* faxStart = "%s:\n"; |
| 144 |
static const char* fieldStart = "%10s: "; |
| 145 |
static const char* fieldEnd = "\n"; |
| 146 |
static const char* faxEnd = ""; |
| 147 |
|
| 148 |
static void |
| 149 |
printStart (const char* filename) |
| 150 |
{ |
| 151 |
printf(faxStart, filename); |
| 152 |
} |
| 153 |
|
| 154 |
static void |
| 155 |
printField (const char* val_fmt, const char* name, ...) |
| 156 |
{ |
| 157 |
printf(fieldStart, name); |
| 158 |
va_list ap; |
| 159 |
va_start(ap,name); |
| 160 |
vprintf(val_fmt, ap); |
| 161 |
va_end(ap); |
| 162 |
printf(fieldEnd, name); |
| 163 |
} |
| 164 |
|
| 165 |
static void |
| 166 |
printEnd (const char* filename) |
| 167 |
{ |
| 168 |
printf(faxEnd, filename); |
| 169 |
} |
| 170 |
|
| 171 |
int |
| 172 |
main(int argc, char** argv) |
| 173 |
{ |
| 174 |
const char* appName = argv[0]; |
| 175 |
bool debug = false; |
| 176 |
bool baseName = false; |
| 177 |
int c; |
| 178 |
|
| 179 |
while ((c = getopt(argc, argv, "C:c:rnbS:s:e:E:D")) != -1) |
| 180 |
switch (c) { |
| 181 |
case '?': |
| 182 |
usage(appName); |
| 183 |
return 0; |
| 184 |
case 'C': |
| 185 |
faxStart = "\"%s\""; |
| 186 |
fieldStart = escapedString(fxStr::format("%%0.0s%s\"", optarg)); |
| 187 |
fieldEnd = "\""; |
| 188 |
faxEnd = "\n"; |
| 189 |
break; |
| 190 |
case 'c': |
| 191 |
faxStart = "%s"; |
| 192 |
fieldStart = escapedString(fxStr::format("%%0.0s%s", optarg)); |
| 193 |
fieldEnd = ""; |
| 194 |
faxEnd = "\n"; |
| 195 |
break; |
| 196 |
case 'r': |
| 197 |
faxStart = ""; |
| 198 |
fieldStart = "%0.0s"; |
| 199 |
fieldEnd = "\n"; |
| 200 |
faxEnd = ""; |
| 201 |
break; |
| 202 |
case 'n': |
| 203 |
faxStart = ""; |
| 204 |
break; |
| 205 |
case 'b': |
| 206 |
baseName = true; |
| 207 |
break; |
| 208 |
case 'S': |
| 209 |
faxStart = escapedString(optarg); |
| 210 |
break; |
| 211 |
case 's': |
| 212 |
fieldStart = escapedString(optarg); |
| 213 |
break; |
| 214 |
case 'e': |
| 215 |
fieldEnd = escapedString(optarg); |
| 216 |
break; |
| 217 |
case 'E': |
| 218 |
faxEnd = escapedString(optarg); |
| 219 |
break; |
| 220 |
case 'D': |
| 221 |
debug = true; |
| 222 |
break; |
| 223 |
} |
| 224 |
|
| 225 |
if (debug) |
| 226 |
fprintf(stderr, "faxStart='%s'\nfieldStart='%s'\nfieldEnd='%s'\nfaxEnd'%s'\n", |
| 227 |
faxStart,fieldStart,fieldEnd,faxEnd); |
| 228 |
|
| 229 |
while (optind < argc) { |
| 230 |
const char* name = argv[optind]; |
| 231 |
|
| 232 |
if (baseName) { |
| 233 |
const char* r = strrchr(name, '/'); |
| 234 |
if (r) |
| 235 |
name = r+1; |
| 236 |
} |
| 237 |
|
| 238 |
printStart(name); |
| 239 |
TIFFSetErrorHandler(NULL); |
| 240 |
TIFFSetWarningHandler(NULL); |
| 241 |
TIFF* tif = TIFFOpen(argv[optind], "r"); |
| 242 |
if (tif == NULL) { |
| 243 |
printf(_("Could not open %s; either not TIFF or corrupted.\n"), |
| 244 |
argv[optind]); |
| 245 |
return (1); |
| 246 |
} |
| 247 |
bool ok = isFAXImage(tif); |
| 248 |
if (!ok) { |
| 249 |
printf(_("Does not look like a facsimile?\n")); |
| 250 |
return (1); |
| 251 |
} |
| 252 |
|
| 253 |
Class2Params params; |
| 254 |
params.jp = 0; |
| 255 |
uint32 v; |
| 256 |
float vres = 3.85; // XXX default |
| 257 |
float hres = 8.03; |
| 258 |
#ifdef TIFFTAG_FAXRECVPARAMS |
| 259 |
if (TIFFGetField(tif, TIFFTAG_FAXRECVPARAMS, &v)) { |
| 260 |
params.decode((u_int) v); // page transfer params |
| 261 |
// inch & metric resolutions overlap and are distinguished by yres |
| 262 |
TIFFGetField(tif, TIFFTAG_YRESOLUTION, &vres); |
| 263 |
switch ((u_int) vres) { |
| 264 |
case 100: |
| 265 |
params.vr = VR_200X100; |
| 266 |
break; |
| 267 |
case 200: |
| 268 |
params.vr = VR_200X200; |
| 269 |
break; |
| 270 |
case 400: |
| 271 |
params.vr = VR_200X400; |
| 272 |
break; |
| 273 |
case 300: |
| 274 |
params.vr = VR_300X300; |
| 275 |
break; |
| 276 |
} |
| 277 |
} else { |
| 278 |
#endif |
| 279 |
uint16 compression = 0; |
| 280 |
(void) TIFFGetField(tif, TIFFTAG_COMPRESSION, &compression); |
| 281 |
if (compression == COMPRESSION_JBIG) { |
| 282 |
params.df = DF_JBIG; |
| 283 |
} else if (compression == COMPRESSION_JPEG) { |
| 284 |
params.df = 0; |
| 285 |
params.jp = JP_COLOR; |
| 286 |
} else if (compression == COMPRESSION_CCITTFAX4) { |
| 287 |
params.df = DF_2DMMR; |
| 288 |
} else { |
| 289 |
uint32 g3opts = 0; |
| 290 |
TIFFGetField(tif, TIFFTAG_GROUP3OPTIONS, &g3opts); |
| 291 |
params.df = (g3opts&GROUP3OPT_2DENCODING ? DF_2DMR : DF_1DMH); |
| 292 |
} |
| 293 |
if (TIFFGetField(tif, TIFFTAG_YRESOLUTION, &vres)) { |
| 294 |
uint16 resunit = RESUNIT_INCH; // TIFF spec default |
| 295 |
TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT, &resunit); |
| 296 |
if (resunit == RESUNIT_INCH) |
| 297 |
vres /= 25.4; |
| 298 |
if (resunit == RESUNIT_NONE) |
| 299 |
vres /= 720.0; // postscript units ? |
| 300 |
if (TIFFGetField(tif, TIFFTAG_XRESOLUTION, &hres)) { |
| 301 |
if (resunit == RESUNIT_INCH) |
| 302 |
hres /= 25.4; |
| 303 |
if (resunit == RESUNIT_NONE) |
| 304 |
hres /= 720.0; // postscript units ? |
| 305 |
} |
| 306 |
} |
| 307 |
params.setRes((u_int) hres, (u_int) vres); // resolution |
| 308 |
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &v); |
| 309 |
params.setPageWidthInPixels((u_int) v); // page width |
| 310 |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &v); |
| 311 |
params.setPageLengthInMM((u_int)(v / vres)); // page length |
| 312 |
#ifdef TIFFTAG_FAXRECVPARAMS |
| 313 |
} |
| 314 |
#endif |
| 315 |
char* cp; |
| 316 |
#ifdef TIFFTAG_FAXDCS |
| 317 |
if (TIFFGetField(tif, TIFFTAG_FAXDCS, &cp) && strncmp(cp, "00 00 00", 8) != 0) { |
| 318 |
// cannot trust br from faxdcs as V.34-Fax does not provide it there |
| 319 |
u_int brhold = params.br; |
| 320 |
fxStr faxdcs(cp); |
| 321 |
sanitize(faxdcs); |
| 322 |
params.asciiDecode((const char*) faxdcs); // params per Table 2/T.30 |
| 323 |
params.setFromDCS(params); |
| 324 |
params.br = brhold; |
| 325 |
} |
| 326 |
#endif |
| 327 |
fxStr sender = ""; |
| 328 |
CallID callid; |
| 329 |
if (TIFFGetField(tif, TIFFTAG_IMAGEDESCRIPTION, &cp)) { |
| 330 |
while (cp[0] != '\0' && cp[0] != '\n') { // sender |
| 331 |
sender.append(cp[0]); |
| 332 |
cp++; |
| 333 |
} |
| 334 |
sanitize(sender); |
| 335 |
u_int i = 0; |
| 336 |
while (cp[0] == '\n') { |
| 337 |
cp++; |
| 338 |
callid.resize(i+1); |
| 339 |
while (cp[0] != '\0' && cp[0] != '\n') { |
| 340 |
callid[i].append(cp[0]); |
| 341 |
cp++; |
| 342 |
} |
| 343 |
sanitize(callid[i]); |
| 344 |
i++; |
| 345 |
} |
| 346 |
} else |
| 347 |
sender = _("<unknown>"); |
| 348 |
printField("%s", _("Sender"), (const char*) sender); |
| 349 |
#ifdef TIFFTAG_FAXSUBADDRESS |
| 350 |
if (TIFFGetField(tif, TIFFTAG_FAXSUBADDRESS, &cp)) { |
| 351 |
fxStr subaddr(cp); |
| 352 |
sanitize(subaddr); |
| 353 |
printField("%s", _("SubAddr"), (const char*) subaddr); |
| 354 |
} |
| 355 |
#endif |
| 356 |
fxStr date; |
| 357 |
if (TIFFGetField(tif, TIFFTAG_DATETIME, &cp)) { // time received |
| 358 |
date = cp; |
| 359 |
sanitize(date); |
| 360 |
} else { |
| 361 |
struct stat sb; |
| 362 |
fstat(TIFFFileno(tif), &sb); |
| 363 |
char buf[80]; |
| 364 |
strftime(buf, sizeof (buf), |
| 365 |
"%Y:%m:%d %H:%M:%S %Z", localtime(&sb.st_mtime)); |
| 366 |
date = buf; |
| 367 |
} |
| 368 |
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &v); |
| 369 |
float h = v / (params.vr == VR_NORMAL ? 3.85 : |
| 370 |
params.vr == VR_200X100 ? 3.94 : |
| 371 |
params.vr == VR_FINE ? 7.7 : |
| 372 |
params.vr == VR_200X200 ? 7.87 : |
| 373 |
params.vr == VR_R8 ? 15.4 : |
| 374 |
params.vr == VR_200X400 ? 15.75 : |
| 375 |
params.vr == VR_300X300 ? 11.81 : 15.4); |
| 376 |
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &v); |
| 377 |
float w = v / (params.vr == VR_NORMAL ? 8.0 : |
| 378 |
params.vr == VR_200X100 ? 8.00 : |
| 379 |
params.vr == VR_FINE ? 8.00 : |
| 380 |
params.vr == VR_200X200 ? 8.00 : |
| 381 |
params.vr == VR_R8 ? 8.00 : |
| 382 |
params.vr == VR_200X400 ? 8.00 : |
| 383 |
params.vr == VR_300X300 ? 12.01 : 16.01); |
| 384 |
time_t time = 0; |
| 385 |
u_int npages = 0; // page count |
| 386 |
do { |
| 387 |
npages++; |
| 388 |
#ifdef TIFFTAG_FAXRECVTIME |
| 389 |
if (TIFFGetField(tif, TIFFTAG_FAXRECVTIME, &v)) |
| 390 |
time += v; |
| 391 |
#endif |
| 392 |
} while (TIFFReadDirectory(tif)); |
| 393 |
TIFFClose(tif); |
| 394 |
|
| 395 |
printField("%u", _("Pages"), npages); |
| 396 |
if (params.vr == VR_NORMAL) |
| 397 |
printField(_("Normal"), _("Quality")); |
| 398 |
else if (params.vr == VR_FINE) |
| 399 |
printField(_("Fine"), _("Quality")); |
| 400 |
else if (params.vr == VR_R8) |
| 401 |
printField(_("Superfine"), _("Quality")); |
| 402 |
else if (params.vr == VR_R16) |
| 403 |
printField(_("Hyperfine"), _("Quality")); |
| 404 |
else |
| 405 |
printField(_("%u lines/inch"), _("Quality"), params.verticalRes()); |
| 406 |
PageSizeInfo* info = PageSizeInfo::getPageSizeBySize(w, h); |
| 407 |
if (info) |
| 408 |
printField("%s", _("Page"), info->name()); |
| 409 |
else |
| 410 |
printField(_("%u by %u"), _("Page"), params.pageWidth(), (u_int) h); |
| 411 |
delete info; |
| 412 |
printField("%s", _("Received"), (const char*) date); |
| 413 |
printField("%s", _("TimeToRecv"), time == 0 ? _("<unknown>") : fmtTime(time)); |
| 414 |
printField("%s", _("SignalRate"), params.bitRateName()); |
| 415 |
printField("%s", _("DataFormat"), params.dataFormatName()); |
| 416 |
printField("%s", _("ErrCorrect"), params.ec == EC_DISABLE ? _("No") : _("Yes")); |
| 417 |
for (u_int i = 0; i < callid.size(); i++) { |
| 418 |
// formatting will mess up if i gets bigger than one digit |
| 419 |
fxStr fmt(fxStr::format(_("CallID%u"), i+1)); |
| 420 |
printField("%s", (const char*)fmt, (const char*) callid.id(i)); |
| 421 |
} |
| 422 |
printEnd(name); |
| 423 |
optind++; |
| 424 |
} |
| 425 |
return (0); |
| 426 |
} |