home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume5 / pbm3 / part3 / pbmtolj.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-02-03  |  3.0 KB  |  160 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. ** Copyright (C) 1988 by Jef Poskanzer and Michael Haberler.
  11. **
  12. ** Permission to use, copy, modify, and distribute this software and its
  13. ** documentation for any purpose and without fee is hereby granted, provided
  14. ** that the above copyright notice appear in all copies and that both that
  15. ** copyright notice and this permission notice appear in supporting
  16. ** documentation.  This software is provided "as is" without express or
  17. ** implied warranty.
  18. */
  19.  
  20. #include <stdio.h>
  21. #ifdef    OS_SYSV
  22. #include <string.h>
  23. #else    OS_SYSV
  24. #include <strings.h>
  25. #endif    OS_SYSV
  26. #include "pbm.h"
  27.  
  28. int dpi = 75;
  29.     
  30. main( argc, argv )
  31. int argc;
  32. char *argv[];
  33.     {
  34.     FILE *ifd;
  35.     bit **bits;
  36.     int argn, rows, cols, rucols, padright, row, col;
  37.     char ch;
  38.     char *usage = 
  39. "usage:  %s [-r resolution] [pbmfile]\n\t\
  40. resolution = [75|100|150|300] (dpi)\n";
  41.  
  42.     argn = 1;
  43.  
  44.     /* Check for flags. */
  45.     if ( argc > argn )
  46.     {
  47.     if ( argv[argn][0] == '-' )
  48.         {
  49.         if ( strcmp( argv[argn], "-r" ) == 0 )
  50.         {
  51.         if ( argc == argn + 1 )
  52.             {
  53.             fprintf( stderr, usage, argv[0] );
  54.             exit( 1 );
  55.             }
  56.         if ( sscanf( argv[argn+1], "%d", &dpi ) != 1 )
  57.             {
  58.             fprintf( stderr, usage, argv[0] );
  59.             exit( 1 );
  60.             }
  61.         argn += 2;
  62.         }
  63.         else
  64.         {
  65.         fprintf( stderr, usage, argv[0] );
  66.         exit( 1 );
  67.         }
  68.         }
  69.     }
  70.  
  71.     if ( argc > argn + 1 )
  72.     {
  73.     fprintf( stderr, usage, argv[0] );
  74.     exit( 1 );
  75.     }
  76.  
  77.     if ( argc == argn + 1 )
  78.     {
  79.         ifd = fopen( argv[argn], "r" );
  80.         if ( ifd == NULL )
  81.         {
  82.         fprintf( stderr, "%s: can't open.\n", argv[argn] );
  83.         exit( 1 );
  84.         }
  85.     }
  86.     else
  87.     ifd = stdin;
  88.  
  89.     bits = pbm_readpbm( ifd, &cols, &rows );
  90.  
  91.     if ( ifd != stdin )
  92.     fclose( ifd );
  93.  
  94.     /* Round cols up to the nearest multiple of 8. */
  95.     rucols = ( cols + 7 ) / 8;
  96.     rucols = rucols * 8;
  97.     padright = rucols - cols;
  98.  
  99.     putinit( );
  100.     for ( row = 0; row < rows; row++ )
  101.     {
  102.     /* Transfer raster graphics */
  103.      printf("\033*b%dW",rucols/8);
  104.         for ( col = 0; col < cols; col++ )
  105.         putbit( bits[row][col] );
  106.     for ( col = 0; col < padright; col++ )
  107.         putbit( 0 );
  108.         }
  109.     putrest( );
  110.  
  111.     exit( 0 );
  112.     }
  113.  
  114.  
  115. int item, bitsperitem, bitshift, itemsperline, firstitem;
  116.  
  117. putinit( )
  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. putbit( b )
  133. bit b;
  134.     {
  135.     if ( bitsperitem == 8 ) {
  136.     putitem( );
  137.         bitshift = 7;
  138.     }
  139.     if ( b )
  140.     item += 1 << bitshift;
  141.     bitsperitem++;
  142.     bitshift--;
  143.     }
  144.  
  145. putrest( )
  146.     {
  147.     if ( bitsperitem > 1 )
  148.     putitem( );
  149.  
  150.     /* end raster graphics */
  151.     printf( "\033*rB" );
  152.     }
  153.  
  154. putitem( )
  155.     {
  156.     putchar( item );
  157.     bitsperitem = 0;
  158.     item = 0;
  159.     }
  160.