/*********************************************************************** Program to convert GB code bitmap file format. Row-order bitmap is scanned from left to right, top to bottom. The MSB of the first byte is the upper left bit, the LSB of the last byte is the lower right bit (horizontal scanning). Column-order bitmap is scanned from top to bottom, left to right. The MSB of the first byte is the upper left bit, the LSB of the last byte is the lower right bit (vertical scanning). 24x24 bitmap file with all legal planes filled should have ((0xF7-0xA1+1)*94)*(24*24/8) = (87*94)*72 = 8178*72 = 588816 bytes. ***********************************************************************/ #include #include #include #ifndef BYTE #define BYTE 8 #endif #ifndef FONTSIZE #define FONTSIZE 24 #endif #ifndef QU #define QU 94 #endif #ifdef __MSDOS__ #define RMODE "rb" #define WMODE "wb" #define DIR_DELIM '\\' #else /* unix */ #define RMODE "r" #define WMODE "w" #define DIR_DELIM '/' #endif FILE *ifp, *ofp; char old_buf[BUFSIZ], new_buf[BUFSIZ]; char *program; int fill_qu = 0; int to_row = 1; int fontsize = FONTSIZE; int old_row, old_col; int new_row, new_col; int num_bytes; void usage(); void fill_blk(); int col2row(), row2col(); main(argc, argv) int argc; char **argv; { /* get program name */ if ((program = strrchr(argv[0], DIR_DELIM)) == NULL) program = argv[0]; else program++; /* process command arguments */ /* better to use getopt() */ while (--argc > 0 && (*++argv)[0] == '-') switch ((*argv)[1]) { case 'f': fontsize = atoi(*argv + 2); break; case 'r': to_row = 1; break; case 'c': to_row = 0; break; case 'b': fill_qu = 1; break; default: usage(); break; } if (argc < 2) usage(); /* sanity check */ if ((fontsize % BYTE != 0) || (fontsize == 0)) { (void) fprintf(stderr, "invalid square font %dx%d, font size must be Nx%d\n", fontsize, fontsize, BYTE); exit(-1); } if ((ifp = fopen(argv[0], RMODE)) == NULL) { (void) fprintf(stderr, "cannot open input file: %s\n", argv[0]); exit(-2); } if ((ofp = fopen(argv[1], WMODE)) == NULL) { (void) fprintf(stderr, "cannot open output file: %s\n", argv[1]); exit(-3); } if (to_row) col2row(); else row2col(); (void) fclose(ifp); (void) fclose(ofp); } void usage() { fprintf(stderr, "Usage:\t%s [-fSIZE] [-r|c] [-b] [input] [output]\n\n", program); fprintf(stderr, "\tConvert square GB bitmap format\n"); fprintf(stderr, "\t-f font size if not %dx%d\n", FONTSIZE, FONTSIZE); fprintf(stderr, "\t-r convert from column order to row order (default)\n"); fprintf(stderr, "\t-c convert from row order to column order\n"); fprintf(stderr, "\t-b fill legal plane with blank\n"); exit(1); } void fill_blk(size) int size; { char c = 0; int i; for (i = 0; i < size; i++) (void) fputc(c, ofp); } int col2row() { int i, j; int new_ri, new_ci; int old_ri, old_c0, old_ci; int old_bi; int old_idx; int blk_start = 9 * QU; int blk_end = 15 * QU; int gbcode = 0; new_row = old_col = fontsize; new_col = old_row = fontsize / BYTE; num_bytes = new_row * new_col; while (1) { gbcode++; if (fill_qu && gbcode >= blk_start && gbcode < blk_end) { fill_blk((blk_end - blk_start) * num_bytes); gbcode += blk_end - blk_start; } /* read num_bytes long bitmap of one code */ (void) fread(old_buf, sizeof(char), num_bytes, ifp); if (feof(ifp) || ferror(ifp)) { return; } for (i = 0; i < num_bytes; i++) { new_buf[i] = 0; new_ri = i / new_col; new_ci = i % new_col; old_ri = new_ri / BYTE; old_c0 = new_ci * BYTE; old_bi = new_ri % BYTE; for (j = 0; j < BYTE; j++) { old_ci = old_c0 + j; old_idx = old_ci * old_row + old_ri; new_buf[i] |= ((old_buf[old_idx] << old_bi) & 0x80) >> j; } } if (fwrite(new_buf, sizeof(char), num_bytes, ofp) != num_bytes) { (void) fprintf(stderr, "output error!\n"); exit(-1); } } } int row2col() { int i, j; int new_ri, new_ci; int old_ri, old_r0, old_ci; int old_bi; int old_idx; new_row = old_col = fontsize / BYTE; new_col = old_row = fontsize; num_bytes = new_row * new_col; while (1) { /* read num_bytes long bitmap of one code */ (void) fread(old_buf, sizeof(char), num_bytes, ifp); if (feof(ifp) || ferror(ifp)) { return; } for (i = 0; i < num_bytes; i++) { new_buf[i] = 0; new_ri = i % new_row; new_ci = i / new_row; old_ci = new_ci / BYTE; old_r0 = new_ri * BYTE; old_bi = new_ci % BYTE; for (j = 0; j < BYTE; j++) { old_ri = old_r0 + j; old_idx = old_ri * old_col + old_ci; new_buf[i] |= ((old_buf[old_idx] << old_bi) & 0x80) >> j; } } if (fwrite(new_buf, sizeof(char), num_bytes, ofp) != num_bytes) { (void) fprintf(stderr, "output error!\n"); exit(-1); } } }