#include #include #include "txd.h" void writepixel(unsigned char rgba[3], int isalpha, FILE *bmp) { unsigned char alphavalue; if (!isalpha) { putc(rgba[2], bmp); /* B */ putc(rgba[1], bmp); /* G */ putc(rgba[0], bmp); /* R */ } else if (isalpha) { alphavalue = rgba[3]; if (alphavalue > 0) alphavalue = alphavalue*2 - 1; putc(alphavalue, bmp); putc(alphavalue, bmp); putc(alphavalue, bmp); } } void makebitmaps(int width, int height, int bpp, int hasalpha, FILE *texture, FILE *alpha) { int i, j; struct bmp_header *bhtexture; struct bmp_header *bhalpha; struct bmp_infoheader *bihtexture; struct bmp_infoheader *bihalpha; /* Make Bitmap header for texture */ bhtexture = (struct bmp_header *) malloc(0x0E); bhtexture->id[0] = 'B'; bhtexture->id[1] = 'M'; bhtexture->size = 0x0E + 0x28 + height * width * 3; bhtexture->reserved = 0; bhtexture->offset = 0x36; fwrite(bhtexture, 0x0E, 1, texture); /* Make Infoheader for texture */ bihtexture = (struct bmp_infoheader *) malloc(0x28); bihtexture->size = 0x28; bihtexture->width = width; bihtexture->height = height; bihtexture->planes = 1; bihtexture->bpp = 24; bihtexture->compression = 0; bihtexture->imagesize = width*height*3; bihtexture->xresolution = 1; bihtexture->yresolution = 1; bihtexture->ncolors = 0; bihtexture->impcolors = 0; fwrite(bihtexture, 0x28, 1, texture); /* Write Pixels */ for (i = width*(height-1); i >= 0; i -= width) for (j = 0; j < width; j++) { if (bpp == 4 || bpp == 8) writepixel(palette[indices[i+j]], 0, texture); else if (bpp == 32) writepixel(pixel[i+j], 0, texture); } if (hasalpha) { /* Make Bitmap header for Alpha */ bhalpha = (struct bmp_header *) malloc(0x0E); bhalpha->id[0] = 'B'; bhalpha->id[1] = 'M'; bhalpha->size = 0x0E + 0x28 + height * width * 3; bhalpha->reserved = 0; bhalpha->offset = 0x36; fwrite(bhalpha, 0x0E, 1, alpha); /* Make Infoheader for Alpha */ bihalpha = (struct bmp_infoheader *) malloc(0x28); bihalpha->size = 0x28; bihalpha->width = width; bihalpha->height = height; bihalpha->planes = 1; bihalpha->bpp = 24; bihalpha->compression = 0; bihalpha->imagesize = width*height*3; bihalpha->xresolution = 1; bihalpha->yresolution = 1; bihalpha->ncolors = 0; bihalpha->impcolors = 0; fwrite(bihalpha, 0x28, 1, alpha); /* Write pixels */ for (i = width*(height-1); i >= 0; i -= width) for (j = 0; j < width; j++) { if (bpp == 4 || bpp == 8) writepixel(palette[indices[i+j]], 1, alpha); else if (bpp == 32) writepixel(pixel[i+j], 1, alpha); } } }