choptest.c++
上传用户:weiyuanprp
上传日期:2020-05-20
资源大小:1169k
文件大小:4k
源码类别:

传真(Fax)编程

开发平台:

C/C++

  1. /* $Id: choptest.c++,v 1.4 2006/06/22 15:06:36 faxguy Exp $ */
  2. /*
  3.  * Copyright (c) 1994-1996 Sam Leffler
  4.  * Copyright (c) 1994-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. /*
  27.  * Program for testing page chopping support.
  28.  *
  29.  * Usage: choptest input.tif
  30.  */
  31. #include <unistd.h>
  32. #include "Class2.h"
  33. #include "MemoryDecoder.h"
  34. #include "tiffio.h"
  35. #include "Sys.h"
  36. const char* appName;
  37. void
  38. usage()
  39. {
  40.     fprintf(stderr, "usage: %s [-a] [-t threshold] input.tifn", appName);
  41.     exit(-1);
  42. }
  43. void
  44. fatal(const char* fmt ...)
  45. {
  46.     fprintf(stderr, "%s: ", appName);
  47.     va_list ap;
  48.     va_start(ap, fmt);
  49.     vfprintf(stderr, fmt, ap);
  50.     va_end(ap);
  51.     fputs(".n", stderr);
  52.     exit(-1);
  53. }
  54. int
  55. main(int argc, char* argv[])
  56. {
  57.     extern int optind;
  58.     extern char* optarg;
  59.     float minChop = 3.0; // chop if >= 3" white space at bottom
  60.     u_int minRows;
  61.     bool doAll = false;
  62.     int c;
  63.     appName = argv[0];
  64.     while ((c = Sys::getopt(argc, argv, "t:a")) != -1)
  65. switch (c) {
  66. case 'a':
  67.     doAll = true;
  68.     break;
  69. case 't':
  70.     minChop = atof(optarg);
  71.     break;
  72. case '?':
  73.     usage();
  74.     /*NOTREACHED*/
  75. }
  76.     if (argc - optind != 1)
  77. usage();
  78.     TIFF* tif = TIFFOpen(argv[optind], "r");
  79.     if (!tif)
  80. fatal("%s: Cannot open, or not a TIFF file", argv[optind]);
  81.     uint16 comp;
  82.     TIFFGetField(tif, TIFFTAG_COMPRESSION, &comp);
  83.     if (comp != COMPRESSION_CCITTFAX3 && comp != COMPRESSION_CCITTFAX4)
  84. fatal("%s: Not a Group 3 or Group 4-encoded TIFF file", argv[optind]);
  85.     Class2Params params;
  86.     params.vr = VR_NORMAL;
  87.     params.wd = WD_A4;
  88.     params.ln = LN_INF;
  89.     params.df = DF_1DMH;
  90.     printf("Chop %s >=%.2g" of white space at the bottom.n"
  91. , doAll ? "all pages with" : "last page if"
  92. , minChop
  93.     );
  94.     do {
  95. if (doAll || TIFFLastDirectory(tif)) {
  96.     uint32 l;
  97.     TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &l);
  98.     if (l < 1500) {
  99. params.vr = VR_NORMAL;
  100. minRows = (u_int)(98*minChop);
  101.     } else {
  102. params.vr = VR_FINE;
  103. minRows = (u_int)(196*minChop);
  104.     }
  105.     uint32 opts = 0;
  106.     TIFFGetField(tif, TIFFTAG_GROUP3OPTIONS, &opts);
  107.     params.df = (opts & GROUP3OPT_2DENCODING) ? DF_2DMR : DF_1DMH;
  108.     uint16 fillorder;
  109.     TIFFGetFieldDefaulted(tif, TIFFTAG_FILLORDER, &fillorder);
  110.     uint32* stripbytecount;
  111.     (void) TIFFGetField(tif, TIFFTAG_STRIPBYTECOUNTS, &stripbytecount);
  112.     u_int totbytes = (u_int) stripbytecount[0];
  113.     if (totbytes > 0) {
  114. u_char* data = new u_char[totbytes];
  115. if (TIFFReadRawStrip(tif, 0, data, totbytes) >= 0) {
  116.     MemoryDecoder dec(data, totbytes);
  117.     dec.scanPageForBlanks(fillorder, params);
  118.     if (dec.getLastBlanks() > minRows) {
  119. printf(
  120.     "Chop %u rows, strip was %lu bytes, need only %lun"
  121.     , dec.getLastBlanks()
  122.     , (u_long) totbytes
  123.     , (u_long) (dec.getEndOfPage() - data)
  124. );
  125.     } else {
  126. printf("Don't chop, found %u rows, need %u rowsn"
  127.     , dec.getLastBlanks()
  128.     , minRows
  129. );
  130.     }
  131. }
  132. delete data;
  133.     }
  134. }
  135.     } while (TIFFReadDirectory(tif));
  136.     return (0);
  137. }