home *** CD-ROM | disk | FTP | other *** search
/ 8bitfiles.net/archives / archives.tar / archives / genie-commodore-file-library / C128Graphics / VIEW-BM.ARC / BM.C next >
Encoding:
C/C++ Source or Header  |  2019-04-13  |  2.4 KB  |  107 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int    ScanWord();
  5. int    bitvalue[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
  6.  
  7. /*****************************************************************************/
  8. main ( argc, argv )
  9.     int    argc;
  10.     char   *argv[];
  11. {
  12.     FILE   *fin, *fout;
  13.     int    width, height;
  14.     char    outname[80], inname[80];
  15.     int    i;
  16.  
  17.     if( argc < 2 ) {
  18.         printf( "Usage: %s file ...\n", argv[0] );
  19.         exit( 1 );
  20.     }
  21. for (i=1; i<argc; i++) {
  22.     printf( "Format converter:" );
  23.     strcpy( outname, argv[i] );
  24.     if (strlen(outname)>4 && strcmp(&outname[strlen(outname)-4],".xbm")==0){
  25.         outname[strlen(outname)-4] = 0;
  26.     }
  27.     strcpy( inname, outname );
  28.     strcat( inname, ".xbm" );
  29.     strcat( outname, ".bm" );
  30.     printf( " \"%s\" to \"%s\".\n", inname, outname );
  31.  
  32.     fin = fopen( inname, "r" );
  33.     if( fin == NULL ) {
  34.         printf( "%s: Can't open input file: %s\n", argv[0], inname );
  35.         exit( 1 );
  36.     }
  37.     ScanWord( fin, "_width" );
  38.     fscanf( fin, "%d", &width );
  39.     ScanWord( fin, "_height" );
  40.     fscanf( fin, "%d", &height );
  41.     printf( "Width=%d, Height=%d\n", width, height );
  42.     ScanWord( fin, "static char" );
  43.     ScanWord( fin, "[]" );
  44.     fout = fopen( outname, "w" );
  45.     ConvertFile( fin, fout, width, height );
  46.     fclose( fout );
  47.     fclose( fin );
  48. }
  49.     printf( "Awll done.\n");
  50. }
  51.  
  52. /*****************************************************************************/
  53. ConvertFile( fin, fout, width, height )
  54.     FILE   *fin, *fout;
  55.     int    width, height;
  56. {
  57.     char    hexhi, hexlo;
  58.     char    binhi, binlo;
  59.     char    rv, v;
  60.     int    i;
  61.  
  62.     putc( 'B', fout );
  63.     putc( 'M', fout );
  64.     putc( 0xCB, fout );
  65.     putc( 0x02, fout );
  66.     v = width / 256;    putc( v, fout );
  67.     v = width % 256;    putc( v, fout );
  68.     v = height / 256;    putc( v, fout );
  69.     v = height % 256;    putc( v, fout );
  70.     while( ScanWord( fin, "0x" ) != EOF ) {
  71.         /* extract non-inverted binary value */
  72.         hexhi = getc( fin );
  73.         hexlo = getc( fin );
  74.         binhi = hexhi<='9' ? hexhi-'0' : (hexhi - 7) & 0x0F;
  75.         binlo = hexlo<='9' ? hexlo-'0' : (hexlo - 7) & 0x0F;
  76.         v = /*0xFF-*/ (binhi * 16 + binlo);
  77.         /* reverse bit positions */
  78.         rv = 0;
  79.         for( i=0; i<8; i++ ) {
  80.             if( v & bitvalue[i] ) {
  81.                 rv |= bitvalue[7 - i];
  82.             }
  83.         }
  84.         putc( rv, fout );
  85.     }
  86. }
  87.  
  88. /*****************************************************************************/
  89. int ScanWord( fin, word )
  90.     FILE   *fin;
  91.     char    word[];
  92. {
  93.     int    i, c;
  94.  
  95.     i = 1;
  96.     while( word[i] != '\0' ) {
  97.         c = getc( fin );
  98.         if( c == EOF ) return( EOF );
  99.         if( c == word[i] ) {
  100.             i++;
  101.         } else {
  102.             i = 1;
  103.         }
  104.     }
  105.     return( 'a' );
  106. }
  107.