diff --git a/snes/msu1/msu1conv.c b/snes/msu1/msu1conv.c new file mode 100644 index 0000000..148b0c4 --- /dev/null +++ b/snes/msu1/msu1conv.c @@ -0,0 +1,104 @@ +#include +#include +#include +#include + +uint16_t tilemap[28][18]; + +void preparetilemap(void) { + uint16_t x, y; + uint16_t tilecnt = 0; + for(y=0; y<18; y+=2) { + for(x=0; x<28; x++) { + tilemap[x][y] = tilecnt; + tilemap[x][y+1] = tilecnt + 0x10; + tilecnt++; + if(!(tilecnt & 0xf)) tilecnt += 0x10; + } + } +} + +void converttile(char *in, char *out, uint8_t x, uint8_t y) { + uint32_t srctilestart = (x*8)+(y*8*224); + uint32_t tgttilestart = tilemap[x][y]*64; + + char *tin = in + srctilestart; + char *tout = out + tgttilestart; + + memset(tout, 0, 64); + + int px, py; + for(py = 0; py < 8; py++) { + for(px = 0; px < 8; px++) { + tout[ 0+2*py] |= ((tin[px+224*py] & 0x01) << 7) >> px; + tout[ 1+2*py] |= ((tin[px+224*py] & 0x02) << 6) >> px; + tout[16+2*py] |= ((tin[px+224*py] & 0x04) << 5) >> px; + tout[17+2*py] |= ((tin[px+224*py] & 0x08) << 4) >> px; + tout[32+2*py] |= ((tin[px+224*py] & 0x10) << 3) >> px; + tout[33+2*py] |= ((tin[px+224*py] & 0x20) << 2) >> px; + tout[48+2*py] |= ((tin[px+224*py] & 0x40) << 1) >> px; + tout[49+2*py] |= ((tin[px+224*py] & 0x80) << 0) >> px; + } + } +} + +void convertpalette(char *in, char *out) { + uint16_t tgtcol; + int pc; + uint8_t r, g, b; + for(pc = 0; pc < 256; pc++) { + b = in[pc*3+0]; + g = in[pc*3+1]; + r = in[pc*3+2]; + tgtcol = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); + out[pc*2+0] = tgtcol & 0xff; + out[pc*2+1] = tgtcol >> 8; + } +} + +void readflip(FILE *in, char *inbuf) { + int y; + for(y=0; y<144; y++) { + fread(inbuf+224*(143-y), 224, 1, in); + } +} + +int main(int argc, char **argv) { + preparetilemap(); + uint16_t x,y; + for(y=0; y<18; y++) { + for(x=0; x<28; x++) { + printf("%03x ", tilemap[x][y]); + } + printf("\n"); + } + char inbuf[32256], outbuf[32512]; + char inpal[768], outpal[512]; + + FILE *in, *out; + int fileno; + uint16_t numcolors; + char filename[80]; + out=fopen("out.msu", "wb"); + for(fileno = 1; fileno < 9290; fileno++) { + sprintf(filename, "%08d.tga", fileno); + in=fopen(filename, "rb"); + fseek(in, 5, SEEK_SET); + fread(&numcolors, 2, 1, in); + fseek(in, 18, SEEK_SET); + fread(inpal, numcolors*3, 1, in); + readflip(in, inbuf); +// fread(inbuf, 32256, 1, in); + fclose(in); + for(y=0; y<18; y++) { + for(x=0; x<28; x++) { + converttile(inbuf, outbuf, x, y); + } + } + fwrite(outbuf, 32512, 1, out); + convertpalette(inpal, outpal); + fwrite(outpal, 512, 1, out); + } + fclose(out); + return 0; +}