Tue Dec 8 18:02:52 2020 Listing bmtoa.c Page: 1 1: /* 2: 3: bmtoa -- convert X11 bitmap data -> ascii text 4: for MS-DOS 5: !! ** There was the same program on the UNIX. ** 6: 7: revision history 8: 1.0: Nov. 24, 1990 by Dai Ishijima 9: 2.0: Jun. 11, 1991 10: */ 11: 12: 13: #include 14: #include 15: 16: #define BPI 8 17: 18: #define shift --argc; ++argv 19: char *progname; 20: 21: 22: /* skip to string *s */ 23: int skipto(fp, s) 24: FILE *fp; 25: char *s; 26: { 27: int ch; 28: char *t; 29: 30: t = s; 31: while (*t != '\0') { 32: if ((ch = getc(fp)) == EOF) { 33: return(ch); 34: } 35: if (ch == *t) { 36: ++t; 37: } 38: else { 39: t = s; 40: if (ch == *t) { 41: ++t; 42: } 43: } 44: } 45: return(ch); 46: } 47: 48: Tue Dec 8 18:02:52 2020 Listing bmtoa.c Page: 2 49: /* get width, height from bitmap file header */ 50: getsize(fp, width, height) 51: FILE *fp; 52: int *width; 53: int *height; 54: { 55: skipto(fp, "#define"); 56: skipto(fp, "_width"); 57: if (fscanf(fp, "%d", width) < 1) { 58: fprintf(fp, "%s: can't get width\n", progname); 59: exit(1); 60: } 61: skipto(fp, "#define"); 62: skipto(fp, "_height"); 63: if (fscanf(fp, "%d", height) < 1) { 64: fprintf(fp, "%s: can't get height\n", progname); 65: exit(1); 66: } 67: skipto(fp, "bits[]"); 68: skipto(fp, "{"); 69: } 70: 71: 72: /* read bitmap data from *fp */ 73: getdata(fp, n, d) 74: FILE *fp; 75: int n; 76: char *d; 77: { 78: int i; 79: int ch; 80: char s[3]; 81: int x; 82: 83: i = 0; 84: while (i < n) { 85: fscanf(fp, "%2s", s); /* skip "0x" */ 86: fscanf(fp, "%x", &x); 87: d[i] = x; 88: i++; 89: ch = getc(fp); 90: if (ch != ',') { 91: break; 92: } 93: } 94: if (i < n) { 95: fprintf(stderr, "%s: unexpected EOF\n", progname); 96: } 97: } 98: 99: Tue Dec 8 18:02:52 2020 Listing bmtoa.c Page: 3 100: /* return YES, if point (x, y) is set */ 101: int bit(x, y, width, height, d) 102: int x; 103: int y; 104: int width; 105: int height; 106: char *d; 107: { 108: if ((x >= width) || (y >= height)) { 109: return(0); 110: } 111: return( 112: (d[y * ((width + BPI - 1) / BPI) + (x / BPI)] & (1 << (x % BPI))) != 0 113: ); 114: } 115: 116: 117: toascii(width, height, d) 118: int width; 119: int height; 120: char *d; 121: { 122: int x; 123: int y; 124: 125: for (y = 0; y < height; y++) { 126: for (x = 0; x < width; x++) { 127: if (bit(x, y, width, height, d)) { 128: putchar('#'); 129: } 130: else { 131: putchar('-'); 132: } 133: } 134: putchar('\n'); 135: } 136: } 137: 138: 139: bmtoascii(fp) 140: FILE *fp; 141: { 142: int width; 143: int height; 144: int n; 145: char *d; 146: 147: getsize(fp, &width, &height); 148: n = height * ((width + BPI - 1) / BPI); 149: d = (char *)malloc(n); 150: getdata(fp, n, d); 151: toascii(width, height, d); 152: free(d); 153: } 154: 155: Tue Dec 8 18:02:52 2020 Listing bmtoa.c Page: 4 156: main(argc, argv) 157: int argc; 158: char *argv[]; 159: { 160: FILE *fp; 161: 162: progname = *argv; 163: shift; 164: if (argc > 0) { 165: while (argc > 0) { 166: if ((fp = fopen(*argv, "r")) == NULL) { 167: fprintf(stderr, "%s: can't open %s\n", progname, *argv); 168: exit(1); 169: } 170: bmtoascii(fp); 171: fclose(fp); 172: shift; 173: } 174: } 175: else { 176: bmtoascii(stdin); 177: } 178: exit(0); 179: } Tue Dec 8 18:02:52 2020 Listing atobm.c Page: 1 1: /* 2: 3: atobm -- convert text data -> X11 bitmap data 4: 5: revision history 6: Nov. 25, 1990 by Dai Ishijima 7: 8: */ 9: 10: #include 11: 12: #define SIZE 8192 13: #define NEWLINE '\n' 14: #define ITEMS 12 15: 16: #define shift --argc; ++argv 17: 18: char *progname; 19: 20: Tue Dec 8 18:02:52 2020 Listing atobm.c Page: 2 21: getdata(fp, limit, n, d, width, height) 22: FILE *fp; 23: int limit; 24: int *n; 25: int *d; 26: int *width; 27: int *height; 28: { 29: int ch; 30: int b, w; 31: 32: *width = *height = w = 0; 33: *n = 0; 34: b = 0; 35: d[*n] = 0; 36: while ((*n < limit) && ((ch = getc(fp)) != EOF)) { 37: if (ch == NEWLINE) { 38: ++*height; 39: if (*width == 0) { 40: *width = w; 41: } else if (*width != w) { 42: fprintf(stderr, "%s: width inconsistently", progname); 43: fprintf(stderr, " %d, %d\n", *width, w); 44: } 45: w = 0; 46: if (b != 0) { 47: b = 0; 48: ++*n; 49: d[*n] = 0; 50: } 51: } else { 52: if (ch != '-') { 53: d[*n] |= (1 << b); 54: } 55: ++w; 56: ++b; 57: if (b >= 8) { 58: ++*n; 59: d[*n] = 0; 60: b = 0; 61: } 62: } 63: } 64: } 65: 66: Tue Dec 8 18:02:52 2020 Listing atobm.c Page: 3 67: toxbm(n, d) 68: int n; 69: int *d; 70: { 71: int i; 72: 73: for (i = 0; i < n; i++) { 74: printf("0x%02x", d[i]); 75: if (i < n - 1) { 76: if (((i + 1) % ITEMS) == 0) { 77: printf(",\n "); 78: } else { 79: printf(", "); 80: } 81: } 82: } 83: } 84: 85: 86: main(argc, argv) 87: int argc; 88: char *argv[]; 89: { 90: int width; 91: int height; 92: int n; 93: int d[SIZE]; 94: FILE *fp; 95: 96: progname = *argv; 97: shift; 98: fp = stdin; 99: 100: if (argc > 0){ 101: if ((fp = fopen(*argv, "r")) == NULL){ 102: fprintf(stderr, "%s : cannot open\n", progname); 103: exit(1); 104: } 105: } 106: 107: getdata(fp, SIZE, &n, d, &width, &height); 108: 109: if (n != ((width + 7) / 8) * height) { 110: fprintf(stderr, "%s: size inconsistently\n", progname); 111: } 112: printf("#define atobm_width %d\n", width); 113: printf("#define atobm_height %d\n", height); 114: printf("static char atobm_bits[] = {\n "); 115: toxbm(n, d); 116: printf("};\n"); 117: exit(0); 118: }