#include #include int ScanWord(); int bitvalue[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; /*****************************************************************************/ main ( argc, argv ) int argc; char *argv[]; { FILE *fin, *fout; int width, height; char name[80]; if( argc != 2 ) { printf( "Usage: %s base_name\n", argv[0] ); exit( 1 ); } printf( "Format converter:" ); printf( " \"%s.xbm\" to \"%s.bm\".\n", argv[1], argv[1] ); strcpy( name, argv[1] ); strcat( name, ".xbm" ); fin = fopen( name, "r" ); if( fin == NULL ) { printf( "%s: Can't open input file: %s\n", argv[0], name ); exit( 1 ); } ScanWord( fin, "_width" ); fscanf( fin, "%d", &width ); ScanWord( fin, "_height" ); fscanf( fin, "%d", &height ); printf( "Width=%d, Height=%d\n", width, height ); ScanWord( fin, "static char" ); ScanWord( fin, "[]" ); strcpy( name, argv[1] ); strcat( name, ".bm" ); fout = fopen( name, "w" ); ConvertFile( fin, fout, width, height ); fclose( fout ); fclose( fin ); printf( "Awll done.\n"); } /*****************************************************************************/ ConvertFile( fin, fout, width, height ) FILE *fin, *fout; int width, height; { char hexhi, hexlo; char binhi, binlo; char rv, v; int i; putc( 'B', fout ); putc( 'M', fout ); putc( 0xCB, fout ); putc( 0x02, fout ); v = width / 256; putc( v, fout ); v = width % 256; putc( v, fout ); v = height / 256; putc( v, fout ); v = height % 256; putc( v, fout ); while( ScanWord( fin, "0x" ) != EOF ) { /* extract non-inverted binary value */ hexhi = getc( fin ); hexlo = getc( fin ); binhi = hexhi<='9' ? hexhi-'0' : (hexhi - 7) & 0x0F; binlo = hexlo<='9' ? hexlo-'0' : (hexlo - 7) & 0x0F; v = /*0xFF-*/ (binhi * 16 + binlo); /* reverse bit positions */ rv = 0; for( i=0; i<8; i++ ) { if( v & bitvalue[i] ) { rv |= bitvalue[7 - i]; } } putc( rv, fout ); } } /*****************************************************************************/ int ScanWord( fin, word ) FILE *fin; char word[]; { int i, c; i = 1; while( word[i] != '\0' ) { c = getc( fin ); if( c == EOF ) return( EOF ); if( c == word[i] ) { i++; } else { i = 1; } } return( 'a' ); }