#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
FILE *inf, *outf;
char *outfname;
unsigned char c;
int x, y, z;
struct {
char ident[2];
long size;
short res1, res2;
long offset;
long sizeheader;
long width, height;
short planes;
short bpp;
long compression;
long imgsize;
long widthpels;
long heightpels;
long numcolors;
long importantcolors;
} bmpinfo;

if (argc == 1)
{
fprintf(stderr, "bmp2bin: No input file specified.\n");
exit(-1);
}

if ((inf = fopen(argv[1], "rb")) == NULL)
{
fprintf(stderr, "bmp2bin: Can't open input file \"%s\".\n", argv[1]);
exit(-1);
}

if (fread(&bmpinfo.ident, sizeof(bmpinfo.ident), 1, inf) != 1)
{
fprintf(stderr, "bmp2bin: Can't read bitmap identifier. (Wow that's weird.)\n");
fclose(inf);
exit(-1);
}

if (fread(&bmpinfo.size, sizeof(bmpinfo) - 2, 1, inf) != 1)
{
fprintf(stderr, "bmp2bin: Can't read bitmap info. (Wow that\'s weird.)\n");
fclose(inf);
exit(-1);
}

if (memcmp(&(bmpinfo.ident), "BM", 2))
{
fprintf(stderr, "bmp2bin: File is not a bitmap.\n");
fclose(inf);
exit(-1);
}

if ((bmpinfo.width != 32) || (bmpinfo.height != 32))
{
fprintf(stderr, "bmp2bin: Picture width is not 32 x 32. %ld x %ld\n", bmpinfo.width, bmpinfo.height);
fclose(inf);
exit(-1);
}

if (bmpinfo.bpp != 1)
{
fprintf(stderr, "bmp2bin: Not a 1-bit (monochrome) bitmap.\n");
fclose(inf);
exit(-1);
}

if (bmpinfo.compression != 0)
{
fprintf(stderr, "bmp2bin: Bitmap is compressed. (That's bad.)\n");
fclose(inf);
exit(-1);
}

if ((outfname = (char *)malloc(strlen(argv[1]) + 5)) == NULL)
{
fprintf(stderr, "bmp2bin: Can\'t allocate memory. (Fer Chrissake, it\'s only %d bytes!)\n", strlen(argv[1] + 5));
fclose(inf);
exit(-1);
}

outfname = strcat(argv[1], ".z80");

if ((outf = fopen(outfname, "wt")) == NULL)
{
fprintf(stderr, "bmp2bin: Can\'t open output file \"%s\".\n", outfname);
fclose(inf);
fclose(outf);
exit(-1);
}

fprintf(outf, "%s:\n", argv[1]);

fseek(inf, 4 * 31 + 8, SEEK_CUR);

for (x = 0; x < 8; x++)
{
fprintf(outf, ".db\t");
for (y = 0; y < 4; y++)
{
for (z = 0; z < 4; z++)
{
fread(&c, 1, 1, inf);
if (y != 0 || z != 0)
fprintf(outf, ", ");
c = ~c;
fprintf(outf, "%03Xh", c);
}
fseek(inf, -8, SEEK_CUR);
}
fprintf(outf, "\n");
}






fclose(inf);
fclose(outf);
exit(0);
}





