home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pbm / pbmtoatk.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  5KB  |  195 lines

  1. /* pbmtoatk.c - convert portable bitmap to Andrew Toolkit raster object
  2. **
  3. ** Copyright (C) 1991 by Bill Janssen.
  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. int process_atk_byte ARGS((int *pcurcount, unsigned char *pcurbyte, FILE *file,
  17.                       unsigned char newbyte, int eolflag));
  18. int write_atk_bytes ARGS((FILE *file, unsigned char curbyte, unsigned int curcount));
  19. #define DEFAULTSCALE (1<<16)
  20. #define RASTERVERSION 2
  21. #define TRUE        1
  22. #define FALSE        0
  23.  
  24. int
  25. main( argc, argv )
  26.     int argc;
  27.     char *argv[];
  28.     {
  29.     FILE *ifd;
  30.     bit *bitrow;
  31.     register bit *bP;
  32.     int rows, cols, format, padright, row;
  33.     register int col;
  34.     char name[100], *cp;
  35.     static char hexchar[] = "0123456789abcdef";
  36.     unsigned char curbyte, newbyte;
  37.     int curcount, gather, line;
  38.  
  39.  
  40.     pbm_init ( &argc, argv );
  41.  
  42.     if ( argc > 2 )
  43.     pm_usage( "[pbmfile]" );
  44.  
  45.     if ( argc == 2 )
  46.     {
  47.     ifd = pm_openr( argv[1] );
  48.     strcpy( name, argv[1] );
  49.     if ( strcmp( name, "-" ) == 0 )
  50.         strcpy( name, "noname" );
  51.  
  52.     if ( ( cp = index( name, '.' ) ) != 0 )
  53.         *cp = '\0';
  54.     }
  55.     else
  56.     {
  57.     ifd = stdin;
  58.     strcpy( name, "noname" );
  59.     }
  60.  
  61.     pbm_readpbminit( ifd, &cols, &rows, &format );
  62.     bitrow = pbm_allocrow( cols );
  63.  
  64.     /* Compute padding to round cols up to the nearest multiple of 16. */
  65.     padright = ( ( cols + 15 ) / 16 ) * 16 - cols;
  66.  
  67.     printf ("\\begindata{raster,%d}\n", 1);
  68.     printf ("%ld %ld %ld %ld ", RASTERVERSION, 
  69.          0, DEFAULTSCALE, DEFAULTSCALE);
  70.     printf ("%ld %ld %ld %ld\n",
  71.          0, 0, cols, rows);    /* subraster */
  72.     printf ("bits %ld %ld %ld\n", 1, cols, rows);
  73.  
  74.     for ( row = 0; row < rows; row++ )
  75.     {
  76.       pbm_readpbmrow( ifd, bitrow, cols, format );
  77.       bP = bitrow;
  78.       gather = 0;
  79.       newbyte = 0;
  80.       curbyte = 0;
  81.       curcount = 0;
  82.       col = 0;
  83.       while (col < cols)
  84.         {
  85.           if (gather > 7)
  86.         {
  87.           process_atk_byte (&curcount, &curbyte, stdout, newbyte, FALSE);
  88.           gather = 0;
  89.           newbyte = 0;
  90.         }
  91.           newbyte = (newbyte << 1) | (*bP++);
  92.           gather += 1;
  93.           col += 1;
  94.         }
  95.  
  96.       if (gather > 0)
  97.         {
  98.           newbyte = (newbyte << (8 - gather));
  99.           process_atk_byte (&curcount, &curbyte, stdout, newbyte, TRUE);
  100.         }
  101.         }
  102.  
  103.     pm_close( ifd );
  104.     
  105.     printf ("\\enddata{raster, %d}\n", 1);
  106.  
  107.     exit( 0 );
  108.   }
  109.  
  110. write_atk_bytes (file, curbyte, curcount)
  111.      FILE *file;
  112.      unsigned char curbyte;
  113.      unsigned int curcount;
  114. {
  115.     /* codes for data stream */
  116. #define WHITEZERO    'f'
  117. #define WHITETWENTY    'z'
  118. #define BLACKZERO    'F'
  119. #define BLACKTWENTY    'Z'
  120. #define OTHERZERO    0x1F
  121.  
  122. #define    WHITEBYTE    0x00
  123. #define    BLACKBYTE    0xFF
  124.  
  125.     /* WriteRow table for conversion of a byte value to two character
  126. hex representation */
  127.  
  128.     static unsigned char hex[16] = {
  129.     '0', '1', '2', '3', '4', '5', '6', '7',
  130.     '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  131.     };
  132.  
  133.   switch (curbyte) {
  134.   case WHITEBYTE:
  135.     while (curcount > 20) 
  136.       fputc(WHITETWENTY, file),
  137.       curcount -= 20;
  138.     fputc(WHITEZERO + curcount, file);
  139.     break;
  140.   case BLACKBYTE:
  141.     while (curcount > 20) 
  142.       fputc(BLACKTWENTY, file),
  143.       curcount -= 20;
  144.     fputc(BLACKZERO + curcount, file);
  145.     break;
  146.   default:
  147.     while (curcount > 16)
  148.       fputc(OTHERZERO+16, file),
  149.       fputc(hex[curbyte / 16], file),
  150.       fputc(hex[curbyte & 15], file),
  151.       curcount -= 16;
  152.     if (curcount > 1)
  153.       fputc(OTHERZERO+curcount, file);
  154.     else ;  /* the byte written will represent a single instance */
  155.     fputc(hex[curbyte / 16], file);
  156.     fputc(hex[curbyte & 15], file);
  157.   }
  158. }
  159.  
  160. process_atk_byte (pcurcount, pcurbyte, file, newbyte, eolflag)
  161. int *pcurcount;
  162. unsigned char *pcurbyte;
  163. FILE *file;
  164. unsigned char newbyte;
  165. int eolflag;
  166. {
  167.     int curcount = *pcurcount;
  168.     unsigned char curbyte = *pcurbyte;
  169.  
  170.     if (curcount < 1)
  171.     {
  172.     *pcurbyte = curbyte = newbyte;
  173.     *pcurcount = curcount = 1;
  174.     }
  175.     else if (newbyte == curbyte)
  176.     {
  177.     *pcurcount = (curcount += 1);
  178.     }
  179.  
  180.     if (curcount > 0 && newbyte != curbyte)
  181.       {
  182.     write_atk_bytes (file, curbyte, curcount);
  183.     *pcurcount = 1;
  184.     *pcurbyte = newbyte;
  185.       }
  186.  
  187.     if (eolflag)
  188.       {
  189.     write_atk_bytes (file, *pcurbyte, *pcurcount);
  190.     fprintf (file, " |\n");
  191.     *pcurcount = 0;
  192.     *pcurbyte = 0;
  193.       }
  194. }
  195.