home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsm / netpbmsca / pbm / c / pbmtolj < prev    next >
Encoding:
Text File  |  1993-10-04  |  3.5 KB  |  171 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. ** Copyright (C) 1988 by Jef Poskanzer and Michael Haberler.
  16. **
  17. ** Permission to use, copy, modify, and distribute this software and its
  18. ** documentation for any purpose and without fee is hereby granted, provided
  19. ** that the above copyright notice appear in all copies and that both that
  20. ** copyright notice and this permission notice appear in supporting
  21. ** documentation.  This software is provided "as is" without express or
  22. ** implied warranty.
  23. */
  24.  
  25. #include "pbm.h"
  26.  
  27. static int dpi = 75;
  28.  
  29. static void putinit ARGS(( void ));
  30. static void putbit ARGS(( bit b ));
  31. static void putrest ARGS(( void ));
  32. static void putitem ARGS(( void ));
  33.  
  34. int
  35. main( argc, argv )
  36.     int argc;
  37.     char* argv[];
  38.     {
  39.     FILE* ifp;
  40.     bit* bitrow;
  41.     register bit* bP;
  42.     int argn, rows, cols, format, rucols, padright, row;
  43.     register int nzcol, col;
  44.     char* usage = "[-resolution N] [pbmfile]\n\tresolution = [75|100|150|300] (dpi)";
  45.  
  46.  
  47.     pbm_init( &argc, argv );
  48.  
  49.     argn = 1;
  50.  
  51.     /* Check for flags. */
  52.     if ( argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0' )
  53.     {
  54.     if ( pm_keymatch( argv[argn], "-resolution", 2 ) )
  55.         {
  56.         ++argn;
  57.         if ( argn == argc || sscanf( argv[argn], "%d", &dpi ) != 1 )
  58.         pm_usage( usage );
  59.         }
  60.     else
  61.         pm_usage( usage );
  62.     ++argn;
  63.     }
  64.  
  65.     if ( argn != argc )
  66.     {
  67.     ifp = pm_openr( argv[argn] );
  68.     ++argn;
  69.     }
  70.     else
  71.     ifp = stdin;
  72.  
  73.     if ( argn != argc )
  74.     pm_usage( usage );
  75.  
  76.     pbm_readpbminit( ifp, &cols, &rows, &format );
  77.     bitrow = pbm_allocrow( cols );
  78.  
  79.     putinit( );
  80.     for ( row = 0; row < rows; ++row )
  81.     {
  82.     pbm_readpbmrow( ifp, bitrow, cols, format );
  83.  
  84.     /* Find rightmost black pixel. */
  85.     for ( nzcol = cols - 1; nzcol >= 0 && bitrow[nzcol] == PBM_WHITE; --nzcol )
  86.         continue;
  87.  
  88.     /* Round up to the nearest multiple of 8. */
  89.     rucols = ( nzcol + 8 ) / 8;
  90.     rucols = rucols * 8;
  91.     padright = rucols - (nzcol + 1);
  92.  
  93.     /* Transfer raster graphics */
  94.      printf("\033*b%dW",rucols/8);
  95.         for ( col = 0, bP = bitrow; col <= nzcol; ++col, ++bP )
  96.         putbit( *bP );
  97.     for ( col = 0; col < padright; ++col )
  98.         putbit( 0 );
  99.         }
  100.  
  101.     pm_close( ifp );
  102.  
  103.     putrest( );
  104.  
  105.     exit( 0 );
  106.     }
  107.  
  108. static int item, bitsperitem, bitshift, itemsperline, firstitem;
  109.  
  110. static void
  111. putinit( )
  112.     {
  113.     /* Printer reset. */
  114.     printf("\033E");
  115.  
  116.     /* Ensure top margin is zero */
  117.     printf("\033&l0E");
  118.  
  119.     /* Set raster graphics resolution */
  120.     printf("\033*t%dR",dpi);
  121.  
  122.     /* Start raster graphics, relative adressing */
  123.     printf("\033*r1A");
  124.  
  125.     itemsperline = 0;
  126.     bitsperitem = 1;
  127.     item = 0;
  128.     bitshift = 7;
  129.     firstitem = 1;
  130.     }
  131.  
  132. #if __STDC__
  133. static void
  134. putbit( bit b )
  135. #else /*__STDC__*/
  136. static void
  137. putbit( b )
  138. bit b;
  139. #endif /*__STDC__*/
  140.     {
  141.     if ( b == PBM_BLACK )
  142.     item += 1 << bitshift;
  143.     bitshift--;
  144.     if ( bitsperitem == 8 ) {
  145.     putitem( );
  146.         bitshift = 7;
  147.     }
  148.     bitsperitem++;
  149.     }
  150.  
  151. static void
  152. putrest( )
  153.     {
  154.     if ( bitsperitem > 1 )
  155.     putitem( );
  156.  
  157.     /* end raster graphics */
  158.     printf( "\033*rB" );
  159.  
  160.     /* Printer reset. */
  161.     printf("\033E");
  162.     }
  163.  
  164. static void
  165. putitem( )
  166.     {
  167.     putchar( item );
  168.     bitsperitem = 0;
  169.     item = 0;
  170.     }
  171.