/******************************************************************************* * bmp3.c * Microsoft Windows bitmap (BMP) file functions. * * 1.0 04-13-92 drt. First cut. * 1.1 08-25-92 drt. Renamed bmp_header members. * * Copyright (c)1992-94, David R. Tribble. */ /* includes */ #include #include #include #include #include #include #include "bmp.h" /******************************************************************************* * buff * Converts data area `d' of `len' bytes into a readble hex string. * * returns * Pointer to a static string buffer. */ static const char * buff(void *d, int len) { static char s[20]; int i; char * sp; /* convert d[0..len-1] into hex string */ for (sp = s, i = 0; i < len; i++, sp += 3) sprintf(sp, "%.2X ", ((unsigned char *)d)[i]); for ( ; i < 5; i++, sp += 3) sprintf(sp, " "); sp[-1] = '\0'; return s; } /******************************************************************************* * bmp_compr * Convert BMP compression type `type' into its name. * * returns * Pointer to a static string buffer. */ static const char * bmp_compr(long type) { switch (type) { case BMP_CMP_NONE: return "None"; case BMP_CMP_RLE8: return "RLE-8"; case BMP_CMP_RLE4: return "RLE-4"; default: return "Unknown"; } } /******************************************************************************* * bmp_dump_hdr * Write contents of BMP file header to file `out' from struct `hdr'. * * returns * Zero, or -1 on error. */ int bmp_dump_hdr(FILE *out, const struct bmp_header *hdr) { struct os2_bmp_header * os2; if (hdr == NULL) return -1; /* fprintf(out, "%Fp:\n", (void far *)hdr); */ fprintf(out, " %s id %.4X\n", buff(&hdr->id, sizeof(hdr->id)), hdr->id); fprintf(out, " %s filesize %ld\n", buff(&hdr->filesize, sizeof(hdr->filesize)), hdr->filesize); fprintf(out, " %s r %.2X %.2X %.2X %.2X\n", buff(&hdr->r[0], sizeof(hdr->r)), hdr->r[0], hdr->r[1], hdr->r[2], hdr->r[3]); fprintf(out, " %s headersize %lu\n", buff(&hdr->headersize, sizeof(hdr->headersize)), hdr->headersize); if (hdr->infoSize <= 12) { fprintf(out, " %s infoSize %lu\n", buff(&hdr->infoSize, sizeof(os2->infoSize)), hdr->infoSize); fprintf(out, " %s width %lu\n", buff(&hdr->width, sizeof(os2->width)), hdr->width); fprintf(out, " %s depth %lu\n", buff(&hdr->depth, sizeof(os2->depth)), hdr->depth); fprintf(out, " %s biPlanes %u\n", buff(&hdr->biPlanes, sizeof(os2->biPlanes)), hdr->biPlanes); fprintf(out, " %s bits %u, %s colors\n", buff(&hdr->bits, sizeof(os2->bits)), hdr->bits, (hdr->bits == 1 ? "2" : hdr->bits == 2 ? "4" : hdr->bits == 4 ? "16" : hdr->bits == 8 ? "256" : hdr->bits == 24 ? "16M" : "?")); } else { fprintf(out, " %s infoSize %lu\n", buff(&hdr->infoSize, sizeof(hdr->infoSize)), hdr->infoSize); fprintf(out, " %s width %lu\n", buff(&hdr->width, sizeof(hdr->width)), hdr->width); fprintf(out, " %s depth %lu\n", buff(&hdr->depth, sizeof(hdr->depth)), hdr->depth); fprintf(out, " %s biPlanes %u\n", buff(&hdr->biPlanes, sizeof(hdr->biPlanes)), hdr->biPlanes); fprintf(out, " %s bits %u, %s colors\n", buff(&hdr->bits, sizeof(hdr->bits)), hdr->bits, (hdr->bits == 1 ? "2" : hdr->bits == 2 ? "4" : hdr->bits == 4 ? "16" : hdr->bits == 8 ? "256" : hdr->bits == 24 ? "16M" : "?")); fprintf(out, " %s biCompression %lu, %s\n", buff(&hdr->biCompression, sizeof(hdr->biCompression)), hdr->biCompression, bmp_compr(hdr->biCompression)); fprintf(out, " %s biSizeImage %lu\n", buff(&hdr->biSizeImage, sizeof(hdr->biSizeImage)), hdr->biSizeImage); fprintf(out, " %s biXPelsPerMeter %lu, %1.0f dpi\n", buff(&hdr->biXPelsPerMeter, sizeof(hdr->biXPelsPerMeter)), hdr->biXPelsPerMeter, hdr->biXPelsPerMeter / 39.37008); fprintf(out, " %s biYPelsPerMeter %lu, %1.0f dpi\n", buff(&hdr->biYPelsPerMeter, sizeof(hdr->biYPelsPerMeter)), hdr->biYPelsPerMeter, hdr->biYPelsPerMeter / 39.37008); fprintf(out, " %s biClrUsed %lu\n", buff(&hdr->biClrUsed, sizeof(hdr->biClrUsed)), hdr->biClrUsed); fprintf(out, " %s biClrImportant %lu\n", buff(&hdr->biClrImportant, sizeof(hdr->biClrImportant)), hdr->biClrImportant); } return 0; } /******************************************************************************* * bmp_dump_rgbtab * Write contents of BMP RGB color table `tab', of size `num'. * * returns * Zero. */ int bmp_dump_rgbtab(FILE *out, const struct bmp_rgb tab[], unsigned num) { register unsigned i, j; if (tab == NULL) return -1; if (num == 0) return 0; if (num < 16) { for (i = 0; i < num; i++) fprintf(out, " %.2X<%.2X:%.2X:%.2X:%.2X>\n", i, tab[i].r, tab[i].g, tab[i].b, tab[i].a); } else { for (i = 0; i < num/4; i++) { for (j = 0; j < num; j += num/4) fprintf(out, " %.2X<%.2X:%.2X:%.2X:%.2X>", i+j, tab[i+j].r, tab[i+j].g, tab[i+j].b, tab[i+j].a); fprintf(out, "\n"); } } return 0; } /* end bmp3.c */