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

  1. /* pbmtoxwd.c - read a portable bitmap and produce an X11 window dump
  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    SYSV
  15. #include <string.h>
  16. #define index strchr
  17. #else    SYSV
  18. #include <strings.h>
  19. #endif    SYSV
  20. #include "pbm.h"
  21. #include "x11wd.h"
  22.  
  23. main( argc, argv )
  24. int argc;
  25. char *argv[];
  26.     {
  27.     FILE *ifd;
  28.     register bit *bitrow, *bP;
  29.     int rows, cols, format, padright, row, col;
  30.     char name[100], *cp;
  31.  
  32.     pm_progname = argv[0];
  33.  
  34.     if ( argc > 2 )
  35.     pm_usage( "[pbmfile]" );
  36.  
  37.     if ( argc == 2 )
  38.     {
  39.     ifd = pm_openr( argv[1] );
  40.     strcpy( name, argv[1] );
  41.     if ( strcmp( name, "-" ) == 0 )
  42.         strcpy( name, "noname" );
  43.  
  44.     if ( ( cp = index( name, '.' ) ) != 0 )
  45.         *cp = '\0';
  46.     }
  47.     else
  48.     {
  49.     ifd = stdin;
  50.     strcpy( name, "noname" );
  51.     }
  52.  
  53.     pbm_readpbminit( ifd, &cols, &rows, &format );
  54.     bitrow = pbm_allocrow( cols );
  55.     
  56.     /* Compute padding to round cols up to the nearest multiple of 32. */
  57.     padright = ( ( cols + 31 ) / 32 ) * 32 - cols;
  58.  
  59.     putinit( cols, rows, name );
  60.     for ( row = 0; row < rows; row++ )
  61.     {
  62.     pbm_readpbmrow( ifd, bitrow, cols, format );
  63.         for ( col = 0, bP = bitrow; col < cols; col++, bP++ )
  64.         putbit( *bP );
  65.     for ( col = 0; col < padright; col++ )
  66.         putbit( 0 );
  67.         }
  68.  
  69.     if ( ifd != stdin )
  70.     fclose( ifd );
  71.  
  72.     putrest( );
  73.  
  74.     exit( 0 );
  75.     }
  76.  
  77.  
  78. long item;
  79. int bitsperitem, bitshift;
  80.  
  81.  
  82. putinit( cols, rows, name )
  83. int cols, rows;
  84. char *name;
  85.     {
  86.     X11WDFileHeader h11;
  87.     X11XColor color;
  88.  
  89.     h11.header_size = sizeof(h11) + (strlen(name) + 1) * sizeof(char);
  90.     h11.file_version = X11WD_FILE_VERSION;
  91.     h11.pixmap_format = ZPixmap;
  92.     h11.pixmap_depth = 1;
  93.     h11.pixmap_width = cols;
  94.     h11.pixmap_height = rows;
  95.     h11.xoffset = 0;
  96.     h11.byte_order = MSBFirst;
  97.     h11.bitmap_unit = 32;
  98.     h11.bitmap_bit_order = MSBFirst;
  99.     h11.bitmap_pad = 32;
  100.     h11.bits_per_pixel = 1;
  101.     h11.bytes_per_line = ( ( cols + 31 ) / 32 ) * 4;
  102.     h11.visual_class = 0;
  103.     h11.red_mask = 0;
  104.     h11.green_mask = 0;
  105.     h11.blue_mask = 0;
  106.     h11.bits_per_rgb = 1;
  107.     h11.colormap_entries = 2;
  108.     h11.ncolors = 2;
  109.     h11.window_width = cols;
  110.     h11.window_height = rows;
  111.     h11.window_x = 0;            /* arbitrary */
  112.     h11.window_y = 0;            /* arbitrary */
  113.     h11.window_bdrwidth = 0;
  114.     fwrite( &h11, sizeof(h11), 1, stdout );
  115.  
  116.     fwrite( name, sizeof(char), strlen(name) + 1, stdout );
  117.  
  118.     color.pixel = 0;
  119.     color.red = 65535;
  120.     color.green = 65535;
  121.     color.blue = 65535;
  122.     color.flags = 7;
  123.     color.pad = 0;
  124.     fwrite( &color, sizeof(color), 1, stdout );
  125.     color.pixel = 1;
  126.     color.red = 0;
  127.     color.green = 0;
  128.     color.blue = 0;
  129.     fwrite( &color, sizeof(color), 1, stdout );
  130.  
  131.     item = 0;
  132.     bitsperitem = 0;
  133.     bitshift = 31;
  134.     }
  135.  
  136. putbit( b )
  137. bit b;
  138.     {
  139.     if ( bitsperitem == 32 )
  140.     putitem( );
  141.     bitsperitem++;
  142.     if ( b == PBM_BLACK )
  143.     item += 1 << bitshift;
  144.     bitshift--;
  145.     }
  146.  
  147. putrest( )
  148.     {
  149.     if ( bitsperitem > 0 )
  150.     putitem( );
  151.     }
  152.  
  153. putitem( )
  154.     {
  155.     fwrite( &item, sizeof(item), 1, stdout );
  156.     item = 0;
  157.     bitsperitem = 0;
  158.     bitshift = 31;
  159.     }
  160.