/* Program: BMP2ASC.C Author : Kim Moser Date : 10/06/1993 System : IBM PC / Borland C 3.0 Descrip: Reads BMP file and outputs an ASCII representation of it. Usage : */ #include #include static void usage(void); static void usage(void) { fputs("\ BMP2ASC v1.0 10/06/1993 Copyright (C) Kim Moser All Rights Reserved\n\ Usage: BMP2ASC filename\n\ Sends output to stdout (which can be redirected to a file).\n", stderr ); exit(1); } typedef struct { char id[2]; /* Expected to be "BM" */ long size; int res1; int res2; long offBits; long size2; /* Always 40? */ long width; long height; int planes; int bitCount; long compression; long sizeImage; long xPelsPerMeter; long yPelsPerMeter; long colorUsed; long colorImportant; } BMP_HEADER; static void process(BMP_HEADER* hdr, unsigned char* data, int plane, int scanline_size, int bytesPerPixel); static void process(BMP_HEADER* hdr, unsigned char* data, int plane, int scanline_size, int bytesPerPixel) { int x, y, z; for (y=hdr->height-1; y >=0; y--) { for (x=0; x < hdr->width; x++) { unsigned char* p = data + y*scanline_size + x*bytesPerPixel; printf("%02x", (int) p[plane]); } putchar('\n'); } } static char* PLANE_NAMES[] = { "RED", "GREEN", "BLUE" }; main(int argc, char** argv) { char* fname; FILE* fp; BMP_HEADER hdr; unsigned char* data=NULL; size_t data_size; size_t scanline_size; /* Bytes per scanline */ int bytesPerPixel; /* hdr.bitCount / 8 */ size_t tmp_size_t; int plane; if (argc < 2) usage(); fname = argv[1]; do { /* So 'while' exits loop */ if ((fp = fopen(fname, "rb")) == NULL) { fprintf(stderr, "Error opening file '%s'.\n", fname ); break; } if (fread(&hdr, 1, sizeof(hdr), fp) != sizeof(hdr)) { fprintf(stderr, "Error reading header.\n"); break; } bytesPerPixel = hdr.bitCount / 8; scanline_size = (size_t) (hdr.width+1) * bytesPerPixel; data_size = scanline_size * hdr.height; fprintf(stderr, "type='%c%c'\n" "size=%ld\n" "res1=%d\n" "res2=%d\n" "offbits=%ld\n" "size2=%ld\n" "width=%ld\n" "height=%ld\n" "planes=%d\n" "bitcount=%d (%d bytes per pixel)\n" "compression=%ld\n" "sizeimage=%ld\n" "xPelsPerMeter=%ld\n" "yPelsPerMeter=%ld\n" "colorUsed=%ld\n" "colorImportant=%ld\n" "scanline_size=%d\n" "data_size=%d\n\n", hdr.id[0], hdr.id[1], hdr.size, hdr.res1, hdr.res2, hdr.offBits, hdr.size2, hdr.width, hdr.height, hdr.planes, hdr.bitCount, bytesPerPixel, hdr.compression, hdr.sizeImage, hdr.xPelsPerMeter, hdr.yPelsPerMeter, hdr.colorUsed, hdr.colorImportant, scanline_size, data_size ); if (hdr.compression) { fprintf(stderr, "Sorry, I can't handle compressed files.\n"); break; } if (hdr.bitCount % 8) { fprintf(stderr, "Sorry, I can handle files only in which the bit count is an even multiple of 8.\n"); break; } if ((data = malloc(data_size)) == NULL) { fprintf(stderr, "malloc() failed for %u bytes.\n", data_size ); break; } if ((tmp_size_t = fread(data, 1, data_size, fp)) != data_size) { fprintf(stderr, "fread() returned %u for %u.\n", tmp_size_t, data_size ); } for (plane=0; plane < 3; plane++) { printf("%s plane:\n", PLANE_NAMES[plane] ); process(&hdr, data, plane, scanline_size, bytesPerPixel); printf("\n"); } } while (0); if (data != NULL) free(data); if (fclose(fp)) { fprintf(stderr, "Error closing file '%s'.\n", fname ); } return (0); }