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

  1. /* pbmtolj.c - read a portable bitmap and produce a LaserJet bitmap file
  2. **    
  3. **    based on pbmtops.c
  4. **
  5. **    Michael Haberler HP Vienna mah@hpuviea.uucp
  6. **                   mcvax!tuvie!mah
  7. **    misfeatures: 
  8. **        no positioning
  9. **
  10. **      Bug fix Dec 12, 1988 :
  11. **              lines in putbit() reshuffled 
  12. **              now runs OK on HP-UX 6.0 with X10R4 and HP Laserjet II
  13. **      Bo Thide', Swedish Institute of Space Physics, Uppsala <bt@irfu.se>
  14. **
  15. **    Flags added December, 1993:
  16. **        -noreset to suppress printer reset code
  17. **        -float to suppress positioning code (such as it is)
  18. **    Wim Lewis, Seattle <wiml@netcom.com>
  19. **
  20. ** Copyright (C) 1988 by Jef Poskanzer and Michael Haberler.
  21. **
  22. ** Permission to use, copy, modify, and distribute this software and its
  23. ** documentation for any purpose and without fee is hereby granted, provided
  24. ** that the above copyright notice appear in all copies and that both that
  25. ** copyright notice and this permission notice appear in supporting
  26. ** documentation.  This software is provided "as is" without express or
  27. ** implied warranty.
  28. */
  29.  
  30. #include "pbm.h"
  31.  
  32. static int dpi = 75;
  33. static int floating = 0;  /* suppress the ``ESC & l 0 E'' ? */
  34. static int resets = 3;    /* bit mask for when to emit printer reset seq */
  35.  
  36. static void putinit ARGS(( void ));
  37. static void putbit ARGS(( bit b ));
  38. static void putrest ARGS(( void ));
  39. static void putitem ARGS(( void ));
  40.  
  41. int
  42. main( argc, argv )
  43.     int argc;
  44.     char* argv[];
  45.     {
  46.     FILE* ifp;
  47.     bit* bitrow;
  48.     register bit* bP;
  49.     int argn, rows, cols, format, rucols, padright, row;
  50.     register int nzcol, col;
  51.     char* usage = "[-noreset|-float|-resolution N] [pbmfile]\n\tresolution = [75|100|150|300] (dpi)";
  52.  
  53.     pbm_init( &argc, argv );
  54.  
  55.     argn = 1;
  56.  
  57.     /* Check for flags. */
  58.     while ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  59.     {
  60.     if ( pm_keymatch( argv[argn], "-resolution", 2 ) )
  61.         {
  62.         ++argn;
  63.         if ( argn == argc || sscanf( argv[argn], "%d", &dpi ) != 1 )
  64.         pm_usage( usage );
  65.         }
  66.     else if ( pm_keymatch( argv[argn], "-float", 2 ) )
  67.         {
  68.         floating = 1;
  69.         }
  70.     else if ( pm_keymatch( argv[argn], "-noreset", 2 ) )
  71.         {
  72.         resets = 0;
  73.         }
  74.     else
  75.         pm_usage( usage );
  76.     ++argn;
  77.     }
  78.  
  79.     if ( argn != argc )
  80.     {
  81.     ifp = pm_openr( argv[argn] );
  82.     ++argn;
  83.     }
  84.     else
  85.     ifp = stdin;
  86.  
  87.     if ( argn != argc )
  88.     pm_usage( usage );
  89.  
  90.     pbm_readpbminit( ifp, &cols, &rows, &format );
  91.     bitrow = pbm_allocrow( cols );
  92.  
  93.     putinit( );
  94.     for ( row = 0; row < rows; ++row )
  95.     {
  96.     pbm_readpbmrow( ifp, bitrow, cols, format );
  97.  
  98.     /* Find rightmost black pixel. */
  99.     for ( nzcol = cols - 1; nzcol >= 0 && bitrow[nzcol] == PBM_WHITE; --nzcol )
  100.         continue;
  101.  
  102.     /* Round up to the nearest multiple of 8. */
  103.     rucols = ( nzcol + 8 ) / 8;
  104.     rucols = rucols * 8;
  105.     padright = rucols - (nzcol + 1);
  106.  
  107.     /* Transfer raster graphics */
  108.      printf("\033*b%dW",rucols/8);
  109.         for ( col = 0, bP = bitrow; col <= nzcol; ++col, ++bP )
  110.         putbit( *bP );
  111.     for ( col = 0; col < padright; ++col )
  112.         putbit( 0 );
  113.         }
  114.  
  115.     pm_close( ifp );
  116.  
  117.     putrest( );
  118.  
  119.     exit( 0 );
  120.     }
  121.  
  122. static int item, bitsperitem, bitshift, itemsperline, firstitem;
  123.  
  124. static void
  125. putinit( )
  126.     {
  127.     if(resets & 1)
  128.     {
  129.     /* Printer reset. */
  130.     printf("\033E");
  131.     }
  132.  
  133.     if(!floating)
  134.     {
  135.     /* Ensure top margin is zero */
  136.     printf("\033&l0E");
  137.     }
  138.  
  139.     /* Set raster graphics resolution */
  140.     printf("\033*t%dR",dpi);
  141.  
  142.     /* Start raster graphics, relative adressing */
  143.     printf("\033*r1A");
  144.  
  145.     itemsperline = 0;
  146.     bitsperitem = 1;
  147.     item = 0;
  148.     bitshift = 7;
  149.     firstitem = 1;
  150.     }
  151.  
  152. #if __STDC__
  153. static void
  154. putbit( bit b )
  155. #else /*__STDC__*/
  156. static void
  157. putbit( b )
  158. bit b;
  159. #endif /*__STDC__*/
  160.     {
  161.     if ( b == PBM_BLACK )
  162.     item += 1 << bitshift;
  163.     bitshift--;
  164.     if ( bitsperitem == 8 ) {
  165.     putitem( );
  166.         bitshift = 7;
  167.     }
  168.     bitsperitem++;
  169.     }
  170.  
  171. static void
  172. putrest( )
  173.     {
  174.     if ( bitsperitem > 1 )
  175.     putitem( );
  176.  
  177.     /* end raster graphics */
  178.     printf( "\033*rB" );
  179.  
  180.     if(resets & 2)
  181.     {
  182.     /* Printer reset. */
  183.     printf("\033E");
  184.     }
  185.     }
  186.  
  187. static void
  188. putitem( )
  189.     {
  190.     putchar( item );
  191.     bitsperitem = 0;
  192.     item = 0;
  193.     }
  194.