home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 309.lha / PBM_PLUS / pbm / xbmtopbm.c < prev    next >
C/C++ Source or Header  |  1980-12-04  |  4KB  |  159 lines

  1. /* xbmtopbm.c - read an X bitmap file and produce a portable bitmap
  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 <sys/types.h>
  15. #include "pbm.h"
  16.  
  17. main( argc, argv )
  18. int argc;
  19. char *argv[];
  20.     {
  21.     FILE *ifd;
  22.     register bit *bitrow, *bP;
  23.     int rows, cols, row, col, charcount;
  24.     char *data, mask;
  25.  
  26.     pm_progname = argv[0];
  27.  
  28.     if ( argc > 2 )
  29.     pm_usage( "[bitmapfile]" );
  30.     
  31.     if ( argc == 2 )
  32.     ifd = pm_openr( argv[1] );
  33.     else
  34.     ifd = stdin;
  35.  
  36.     ReadBitmapFile( ifd, &cols, &rows, &data );
  37.  
  38.     pm_close( ifd );
  39.  
  40.     pbm_writepbminit( stdout, cols, rows );
  41.     bitrow = pbm_allocrow( cols );
  42.  
  43.     for ( row = 0; row < rows; row++ )
  44.     {
  45.     charcount = 0;
  46.     mask = 1;
  47.     for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
  48.         {
  49.         if ( charcount >= 8 )
  50.         {
  51.         data++;
  52.         charcount = 0;
  53.         mask = 1;
  54.         }
  55.         *bP = ( *data & mask ) ? PBM_BLACK : PBM_WHITE;
  56.         charcount++;
  57.         mask = mask << 1;
  58.         }
  59.     data++;
  60.     pbm_writepbmrow( stdout, bitrow, cols );
  61.     }
  62.  
  63.     exit( 0 );
  64.     }
  65.  
  66.  
  67. #ifdef    SYSV
  68. #include <string.h>
  69. #define rindex strrchr
  70. #else    SYSV
  71. #include <strings.h>
  72. #endif    SYSV
  73.  
  74. #define MAX_LINE 200
  75.  
  76. ReadBitmapFile( stream, widthP, heightP, dataP )
  77. FILE *stream;
  78. int *widthP, *heightP;
  79. char **dataP;
  80.     {
  81.     char line[MAX_LINE], name_and_type[MAX_LINE];
  82.     char *ptr, *t;
  83.     int bytes, bytes_per_line, value, version10p, raster_length, padding;
  84.  
  85.     *widthP = *heightP = -1;
  86.  
  87.     for ( ; ; )
  88.     {
  89.     if ( ! fgets( line, MAX_LINE, stream ) )
  90.         break;
  91.     if ( strlen( line ) == MAX_LINE - 1 )
  92.         pm_error( "line too long", 0,0,0,0,0 );
  93.  
  94.     if (sscanf(line, "#define %s %d", name_and_type, &value) == 2)
  95.         {
  96.         if ( ! (t = rindex( name_and_type, '_' )) )
  97.         t = name_and_type;
  98.         else
  99.         t++;
  100.         if ( ! strcmp( "width", t ) )
  101.         *widthP = value;
  102.         if ( ! strcmp( "height", t ) )
  103.         *heightP = value;
  104.         continue;
  105.         }
  106.     
  107.     if ( sscanf( line, "static short %s = {", name_and_type ) == 1 )
  108.         {
  109.         version10p = 1;
  110.         break;
  111.         }
  112.     else if ( sscanf( line, "static char %s = {", name_and_type ) == 1 )
  113.         {
  114.         version10p = 0;
  115.         break;
  116.         }
  117.     else
  118.         continue;
  119.     }
  120.  
  121.     if ( ! (t = rindex( name_and_type, '_' )) )
  122.     t = name_and_type;
  123.     else
  124.     t++;
  125.     
  126.     if ( *widthP == -1 )
  127.     pm_error( "invalid width", 0,0,0,0,0 );
  128.     if ( *heightP == -1 )
  129.     pm_error( "invalid height", 0,0,0,0,0 );
  130.  
  131.     padding = 0;
  132.     if ( ((*widthP % 16) >= 1) && ((*widthP % 16) <= 8) && version10p )
  133.     padding = 1;
  134.  
  135.     bytes_per_line = (*widthP+7)/8 + padding;
  136.     
  137.     raster_length =  bytes_per_line * *heightP;
  138.     *dataP = (char *) malloc( raster_length );
  139.     if ( *dataP == (char *) 0 )
  140.     pm_error( "out of memory", 0,0,0,0,0 );
  141.  
  142.     if ( version10p )
  143.     for ( bytes = 0, ptr = *dataP; bytes < raster_length; bytes += 2 )
  144.         {
  145.         if ( fscanf( stream, " 0x%x%*[,}]%*[ \n]", &value ) != 1 )
  146.         pm_error( "error scanning bits item", 0,0,0,0,0 );
  147.         *(ptr++) = value & 0xff;
  148.         if ( (! padding) || ((bytes+2) % bytes_per_line) )
  149.         *(ptr++) = value >> 8;
  150.         }
  151.     else
  152.         for ( bytes = 0, ptr = *dataP; bytes < raster_length; bytes++ )
  153.         {
  154.         if ( fscanf( stream, " 0x%x%*[,}]%*[ \n]", &value ) != 1 )
  155.             pm_error( "error scanning bits item", 0,0,0,0,0 );
  156.         *(ptr++) = value;
  157.         }
  158.     }
  159.