home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pbm / pbmtog3.c < prev    next >
C/C++ Source or Header  |  1993-12-20  |  5KB  |  247 lines

  1. /* pbmtog3.c - read a portable bitmap and produce a Group 3 FAX file
  2. **
  3. ** Copyright (C) 1989 by Paul Haeberli <paul@manray.sgi.com>.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14. #include "g3.h"
  15.  
  16. static void tofax ARGS(( bit* bitrow, int n ));
  17. static void putwhitespan ARGS(( int c ));
  18. static void putblackspan ARGS(( int c ));
  19. static void putcode ARGS(( tableentry* te ));
  20. static void puteol ARGS(( void ));
  21. static void putinit ARGS(( void ));
  22. static void putbit ARGS(( int d ));
  23. static void flushbits ARGS(( void ));
  24.  
  25. static int reversebits;
  26.  
  27. int
  28. main( argc, argv )
  29.     int argc;
  30.     char* argv[];
  31.     {
  32.     FILE* ifp;
  33.     bit* bitrow;
  34.     int argn, rows, cols, format, row, col, i;
  35.     char* usage = " [-reversebits] [pbmfile]";
  36.  
  37.  
  38.     pbm_init( &argc, argv );
  39.  
  40.     argn = 1;
  41.     reversebits = 0;
  42.  
  43.     if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  44.     {
  45.     if ( pm_keymatch( argv[argn], "-reversebits", 2 ) )
  46.         reversebits = 1;
  47.     else
  48.         pm_usage( usage );
  49.     ++argn;
  50.     }
  51.     
  52.     if ( argn == argc )
  53.     ifp = stdin;
  54.     else
  55.     {
  56.     ifp = pm_openr( argv[argn] );
  57.     ++argn;
  58.     }
  59.     
  60.     if ( argn != argc )
  61.     pm_usage( usage );
  62.  
  63.     pbm_readpbminit( ifp, &cols, &rows, &format );
  64.     bitrow = pbm_allocrow( cols );
  65.  
  66.     /* Write out four extra rows to get things stabilized. */
  67.     putinit();
  68.     puteol();
  69.  
  70.     /* Write out bitmap. */
  71.     for ( row = 0; row < rows; ++row )
  72.     {
  73.     pbm_readpbmrow( ifp, bitrow, cols, format );
  74.     tofax( bitrow, cols );
  75.     }
  76.  
  77.     /* And finish off. */
  78.     for( i = 0; i < 6; ++i )
  79.     puteol( );
  80.     flushbits( );
  81.  
  82.     pm_close( ifp );
  83.  
  84.     exit( 0 );
  85.     }
  86.  
  87. static void
  88. tofax(bitrow,n)
  89.     bit* bitrow;
  90.     int n;
  91. {
  92.     int c;
  93.  
  94.     while(n>0) {
  95.     c = 0;
  96.     while(*bitrow == PBM_WHITE && n>0) {
  97.         ++bitrow;
  98.         ++c;
  99.         --n;
  100.     }
  101.     putwhitespan(c);
  102.     c = 0;
  103.     if(n==0)
  104.         break;
  105.     while(*bitrow == PBM_BLACK && n>0) {
  106.         ++bitrow;
  107.         ++c;
  108.         --n;
  109.     }
  110.     putblackspan(c);
  111.     }
  112.     puteol();
  113. }
  114.  
  115. static void
  116. putwhitespan(c)
  117.     int c;
  118. {
  119.     int tpos;
  120.     tableentry* te;
  121.  
  122.     do {
  123.         if(c>=64) {
  124.             if (c < 1792) {
  125.                 tpos = (c - 64) / 64;
  126.                 te = mwtable+tpos;
  127.                 }
  128.             else {
  129.                 tpos = (c - 1792) / 64;
  130.                 if (tpos > ((2560 - 1792) / 64))
  131.                     tpos = (2560 - 1792) / 64;
  132.                 te = extable+tpos;
  133.                 }
  134.         c -= te->count;
  135.         putcode(te);
  136.         }
  137.         tpos = c;
  138.         if (tpos >= 64)
  139.             tpos = 63;
  140.         te = twtable+tpos;
  141.         c -= te->count;
  142.         putcode(te);
  143.         if (c)
  144.           putblackspan(0);
  145.     } while (c);
  146. }
  147.  
  148. static void
  149. putblackspan(c)
  150.     int c;
  151. {
  152.     int tpos;
  153.     tableentry* te;
  154.  
  155.     do {
  156.         if(c>=64) {
  157.             if (c < 1792) {
  158.                 tpos = (c - 64) / 64;
  159.                 te = mbtable+tpos;
  160.                 }
  161.             else {
  162.                 tpos = (c - 1792) / 64;
  163.                 if (tpos > ((2560 - 1792) / 64))
  164.                     tpos = (2560 - 1792) / 64;
  165.                 te = extable+tpos;
  166.                 }
  167.         c -= te->count;
  168.         putcode(te);
  169.         }
  170.         tpos = c;
  171.         if (tpos >= 64)
  172.             tpos = 63;
  173.         te = tbtable+tpos;
  174.         c -= te->count;
  175.         putcode(te);
  176.         if (c)
  177.           putwhitespan(0);
  178.     } while (c);
  179. }
  180.  
  181. static void
  182. putcode(te)
  183.     tableentry* te;
  184. {
  185.     unsigned int mask;
  186.     int code;
  187.  
  188.     mask = 1<<(te->length-1);
  189.     code = te->code;
  190.     while(mask) {
  191.      if(code&mask)
  192.         putbit(1);
  193.     else
  194.         putbit(0);
  195.     mask >>= 1;
  196.     }
  197.  
  198. }
  199.  
  200. static void
  201. puteol()
  202. {
  203.     int i;
  204.  
  205.     for(i=0; i<11; ++i)
  206.     putbit(0);
  207.     putbit(1);
  208. }
  209.  
  210. static int shdata;
  211. static int shbit;
  212.  
  213. static void
  214. putinit()
  215. {
  216.     shdata = 0;
  217.     shbit = reversebits ? 0x01 : 0x80;
  218. }
  219.  
  220. static void
  221. putbit(d)
  222. int d;
  223. {
  224.     if(d) 
  225.     shdata = shdata|shbit;
  226.     if ( reversebits )
  227.     shbit = shbit<<1;
  228.     else
  229.     shbit = shbit>>1;
  230.     if((shbit&0xff) == 0) {
  231.     putchar(shdata);
  232.     shdata = 0;
  233.     shbit = reversebits ? 0x01 : 0x80;
  234.     }
  235. }
  236.  
  237. static void
  238. flushbits( )
  239. {
  240.     if ( ( reversebits && shbit != 0x01 ) ||
  241.      ( ! reversebits && shbit != 0x80 ) ) {
  242.     putchar(shdata);
  243.     shdata = 0;
  244.     shbit = reversebits ? 0x01 : 0x80;
  245.     }
  246. }
  247.