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

  1. /* pbmtorast.c - read a portable bitmap and produce a Sun rasterfile
  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. #include "pbm.h"
  15.  
  16. /* Because of the following include, this program compiles only on Suns. */
  17. #include <pixrect/pixrect_hs.h>
  18.  
  19. main( argc, argv )
  20. int argc;
  21. char *argv[];
  22.     {
  23.     FILE *ifd;
  24.     bit **bits;
  25.     int linebytes;
  26.     int rows, cols, row, col;
  27.     struct pixrect *pr;
  28.     short *data;
  29.     int shortcount, bitcount;
  30.  
  31.     if ( argc > 2 )
  32.     {
  33.     fprintf( stderr, "usage:  %s [pbmfile]\n", argv[0] );
  34.     exit( 1 );
  35.     }
  36.  
  37.     if ( argc == 2 )
  38.     {
  39.     ifd = fopen( argv[1], "r" );
  40.     if ( ifd == NULL )
  41.         {
  42.         fprintf( stderr, "%s: can't open.\n", argv[1] );
  43.         exit( 1 );
  44.         }
  45.     }
  46.     else
  47.     ifd = stdin;
  48.  
  49.     bits = pbm_readpbm( ifd, &cols, &rows );
  50.  
  51.     if ( ifd != stdin )
  52.     fclose( ifd );
  53.     
  54.     if ( (pr = mem_create(cols, rows, 1)) == NULL )
  55.     {
  56.     fprintf( stderr, "Unable to create new pixrect.\n");
  57.     exit( 1 );
  58.     }
  59.  
  60.     data = ((struct mpr_data *)pr->pr_data)->md_image;
  61.     linebytes = ((struct mpr_data *)pr->pr_data)->md_linebytes;
  62.  
  63.     for ( row = 0; row < rows; row++ )
  64.     {
  65.     bitcount = 15;
  66.     shortcount = 0;
  67.     for ( col = 0; col < cols; col++ )
  68.         {
  69.         *(data + shortcount) |= (bits[row][col] << bitcount);
  70.         bitcount--;
  71.         if ( bitcount < 0 )
  72.         {
  73.         bitcount = 15;
  74.         shortcount++;
  75.         }
  76.         }
  77.     data += linebytes / sizeof(short);
  78.     }
  79.  
  80.     pr_dump( pr, stdout, NULL, RT_STANDARD, 0 );
  81.  
  82.     exit( 0 );
  83.     }
  84.