home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume2 / pbm / Part2 / pbmtops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-07  |  3.6 KB  |  184 lines

  1. /* pbmtops.c - read a portable bitmap and produce a PostScript bitmap file
  2. **
  3. ** Copyright (C) 1988 by Jef Poskanzer.
  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 <stdio.h>
  14. #ifdef    OS_SYSV
  15. #include <string.h>
  16. #else    OS_SYSV
  17. #include <strings.h>
  18. #endif    OS_SYSV
  19. #include "pbm.h"
  20.  
  21. main( argc, argv )
  22. int argc;
  23. char *argv[];
  24.     {
  25.     FILE *ifd;
  26.     bit **bits;
  27.     int argn, rows, cols, rucols, padright, row, col;
  28.     char ch;
  29.     float scale;
  30.     char name[100], *cp;
  31.     char *usage = "usage:  %s [-s scale] [pbmfile]\n";
  32.  
  33.     argn = 1;
  34.     scale = 1.0;
  35.  
  36.     /* Check for flags. */
  37.     if ( argc > argn )
  38.     {
  39.     if ( argv[argn][0] == '-' )
  40.         {
  41.         if ( strcmp( argv[argn], "-s" ) == 0 )
  42.         {
  43.         if ( argc == argn + 1 )
  44.             {
  45.             fprintf( stderr, usage, argv[0] );
  46.             exit( 1 );
  47.             }
  48.         if ( sscanf( argv[argn+1], "%f", &scale ) != 1 )
  49.             {
  50.             fprintf( stderr, usage, argv[0] );
  51.             exit( 1 );
  52.             }
  53.         argn += 2;
  54.         }
  55.         else
  56.         {
  57.         fprintf( stderr, usage, argv[0] );
  58.         exit( 1 );
  59.         }
  60.         }
  61.     }
  62.  
  63.     if ( argc > argn + 1 )
  64.     {
  65.     fprintf( stderr, usage, argv[0] );
  66.     exit( 1 );
  67.     }
  68.  
  69.     if ( argc == argn + 1 )
  70.     {
  71.         ifd = fopen( argv[argn], "r" );
  72.         if ( ifd == NULL )
  73.         {
  74.         fprintf( stderr, "%s: can't open.\n", argv[argn] );
  75.         exit( 1 );
  76.         }
  77.     strcpy( name, argv[argn] );
  78.  
  79. #ifdef    OS_SYSV
  80.     if ( ( cp = strchr( name, '.' ) ) != 0 )
  81. #else    OS_SYSV
  82.     if ( ( cp = index( name, '.' ) ) != 0 )
  83. #endif    OS_SYSV
  84.         *cp = '\0';
  85.     }
  86.     else
  87.     {
  88.     ifd = stdin;
  89.     strcpy( name, "noname" );
  90.     }
  91.  
  92.     bits = pbm_readpbm( ifd, &cols, &rows );
  93.  
  94.     if ( ifd != stdin )
  95.     fclose( ifd );
  96.     
  97.     /* Round cols up to the nearest multiple of 8. */
  98.     rucols = ( cols + 7 ) / 8;
  99.     rucols = rucols * 8;
  100.     padright = rucols - cols;
  101.  
  102.     putinit( name, cols, rows, scale );
  103.     for ( row = 0; row < rows; row++ )
  104.     {
  105.         for ( col = 0; col < cols; col++ )
  106.         putbit( bits[row][col] );
  107.     for ( col = 0; col < padright; col++ )
  108.         putbit( 0 );
  109.         }
  110.     putrest( );
  111.  
  112.     exit( 0 );
  113.     }
  114.  
  115.  
  116. int item, bitsperitem, bitshift, itemsperline, firstitem;
  117.  
  118. putinit( name, cols, rows, scale )
  119. char *name;
  120. int cols, rows;
  121. float scale;
  122.     {
  123.     int scols, srows;
  124.  
  125.     scols = cols * scale * 0.96 + 0.5;    /*   0.96 is the multiple of   */
  126.     srows = rows * scale * 0.96 + 0.5;    /* 72/300 that is closest to 1 */
  127.  
  128.     printf( "%%! %s.ps\n", name );
  129.     printf(
  130.     "%d %d translate\t%% move to lower left corner of box\n",
  131.     300 - ( scols/2 ), 400 - ( srows/2 ) );
  132.     printf( "%d %d scale\t\t%% scale box\n", scols, srows );
  133.     printf( "%d %d 1\t\t%% width height bits/sample\n", cols, rows );
  134.     printf(
  135.     "[ %d 0 0 -%d 0 %d ]\t%% transformation matrix\n", cols, rows, rows );
  136.     printf( "{ <\n" );
  137.  
  138.     itemsperline = 0;
  139.     item = 0;
  140.     bitsperitem = 0;
  141.     bitshift = 7;
  142.     firstitem = 1;
  143.     }
  144.  
  145. putbit( b )
  146. bit b;
  147.     {
  148.     if ( bitsperitem == 8 )
  149.     {
  150.     putitem( );
  151.     }
  152.     if ( ! b )
  153.     item += 1 << bitshift;
  154.     bitsperitem++;
  155.     bitshift--;
  156.     }
  157.  
  158. putrest( )
  159.     {
  160.     if ( bitsperitem > 0 )
  161.     putitem( );
  162.     printf( "\n> }\nimage\nshowpage\n" );
  163.     }
  164.  
  165. putitem( )
  166.     {
  167.     if ( firstitem )
  168.     firstitem = 0;
  169.     else
  170.     putchar( ' ' );
  171.     if ( itemsperline == 20 )
  172.     {
  173.     putchar( '\n' );
  174.     itemsperline = 0;
  175.     }
  176.     if ( itemsperline == 0 )
  177.     printf( "  " );
  178.     itemsperline++;
  179.     printf( "%02x", item );
  180.     item = 0;
  181.     bitsperitem = 0;
  182.     bitshift = 7;
  183.     }
  184.