snes: add in snes tools

This commit is contained in:
Vince Weaver 2013-01-16 16:44:45 -05:00
parent 3f8081a6b0
commit 33c9828283
13 changed files with 1204 additions and 0 deletions

104
tb_snes/tools/Makefile Normal file
View File

@ -0,0 +1,104 @@
CC = gcc
CFLAGS = -O2 -Wall
LFLAGS =
all: bin2byte color_convert convert_font convert_font_bin \
dump_font make_pal pcx_to_sprite \
pcx_to_tiles_4bpp pcx_to_tiles_8bpp \
ansi_to_tile string_to_bytes convert_font_4bpp
ansi_to_tile: ansi_to_tile.o
$(CC) $(LFLAGS) -o ansi_to_tile ansi_to_tile.o
ansi_to_tile.o: ansi_to_tile.c
$(CC) $(CFLAGS) -c ansi_to_tile.c
bin2byte: bin2byte.o
$(CC) $(LFLAGS) -o bin2byte bin2byte.o
bin2byte.o: bin2byte.c
$(CC) $(CFLAGS) -c bin2byte.c
color_convert: color_convert.o
$(CC) $(LFLAGS) -o color_convert color_convert.o
color_convert.o: color_convert.c
$(CC) $(CFLAGS) -c color_convert.c
convert_font: convert_font.o
$(CC) $(LFLAGS) -o convert_font convert_font.o
convert_font.o: convert_font.c
$(CC) $(CFLAGS) -c convert_font.c
convert_font_4bpp: convert_font_4bpp.o
$(CC) $(LFLAGS) -o convert_font_4bpp convert_font_4bpp.o
convert_font_4bpp.o: convert_font_4bpp.c
$(CC) $(CFLAGS) -c convert_font_4bpp.c
convert_font_bin: convert_font_bin.o
$(CC) $(LFLAGS) -o convert_font_bin convert_font_bin.o
convert_font_bin.o: convert_font_bin.c
$(CC) $(CFLAGS) -c convert_font_bin.c
dump_font: dump_font.o
$(CC) $(LFLAGS) -o dump_font dump_font.o
dump_dont.o: dump_font.c
$(CC) $(CFLAGS) -c dump_font.c
make_pal: make_pal.o
$(CC) $(LFLAGS) -o make_pal make_pal.o
make_pal.o: make_pal.c
$(CC) $(CFLAGS) -c make_pal.c
pcx_to_sprite: pcx_to_sprite.o
$(CC) $(LFLAGS) -o pcx_to_sprite pcx_to_sprite.o
pcx_to_sprite.o: pcx_to_sprite.c
$(CC) $(CFLAGS) -c pcx_to_sprite.c
pcx_to_tiles_4bpp: pcx_to_tiles_4bpp.o
$(CC) $(LFLAGS) -o pcx_to_tiles_4bpp pcx_to_tiles_4bpp.o
pcx_to_tiles_4bpp.o: pcx_to_tiles_4bpp.c
$(CC) $(CFLAGS) -c pcx_to_tiles_4bpp.c
pcx_to_tiles_8bpp: pcx_to_tiles_8bpp.o
$(CC) $(LFLAGS) -o pcx_to_tiles_8bpp pcx_to_tiles_8bpp.o
pcx_to_tiles_8bpp.o: pcx_to_tiles_8bpp.c
$(CC) $(CFLAGS) -c pcx_to_tiles_8bpp.c
string_to_bytes: string_to_bytes.o
$(CC) $(LFLAGS) -o string_to_bytes string_to_bytes.o
string_to_bytes.o: string_to_bytes.c
$(CC) $(CFLAGS) -c string_to_bytes.c
clean:
rm -f *.o *~ bin2byte color_convert convert_font \
convert_font_bin dump_font make_pal pcx_to_sprite \
pcx_to_tiles_4bpp pcx_to_tiles_8bpp \
ansi_to_tile string_to_bytes \
convert_font_4bpp

View File

@ -0,0 +1,146 @@
#include <stdio.h>
int offset=0;
unsigned char font[3][8]={
{0,0,0,0,0,0,0,0}, /* space */
{2,2,7,2,2,7,2,2}, /* # */
{2,5,5,5,5,5,5,2}, /* O */
};
unsigned char screen_byte[8][4]; /* four bit planes */
void putfont(unsigned char letter, int forecolor, int backcolor) {
int fx,fy,plane,i;
int symbol;
if (letter=='#') symbol=1;
else if (letter=='O') symbol=2;
else symbol=0;
//printf("; Symbol %c=%d\n",letter,symbol);
for(fx=0;fx<3;fx++) {
for(fy=0;fy<8;fy++) {
for(plane=0;plane<4;plane++) {
screen_byte[fy][plane]<<=1;
if (font[symbol][fy]&(1<<fx)) {
/* foreground color */
screen_byte[fy][plane]|=((forecolor>>plane)&1);
printf("; fx=%d fy=%d fore=%d sb[%d]=%x\n",
fx,fy,(forecolor>>plane)&1,plane,screen_byte[fy][plane]);
}
else {
/* background color */
screen_byte[fy][plane]|=((backcolor>>plane)&1);
printf("; fx=%d fy=%d back=%d sb[%d]=%x\n",
fx,fy,(backcolor>>plane)&1,plane,screen_byte[fy][plane]);
}
}
}
offset++;
if (offset==8) {
printf("\t; Planes 1 and 0\n");
for(i=0;i<8;i++) {
printf("\t.word $%02x%02x\n",screen_byte[i][1],screen_byte[i][0]);
}
printf("\t; Planes 3 and 2\n");
for(i=0;i<8;i++) {
printf("\t.word $%02x%02x\n",screen_byte[i][3],screen_byte[i][2]);
}
offset=0;
}
}
}
int main(int argc, char **argv) {
char buffer[BUFSIZ];
char *result;
int i,color,newcolor=0;
int fore=0,back=0;
printf("tile_data:\n");
while(1) {
result=fgets(buffer,BUFSIZ,stdin);
if (result<=0) break;
i=0;
while(1) {
/* escape char */
if (buffer[i]==27) {
i++;
if (buffer[i]=='[') {
i++;
}
color=0;
while(1) {
if ((buffer[i]=='m') || (buffer[i]==';')) {
if (color==0) {
newcolor&=0xbf;
}
else if (color==1) {
newcolor|=0x40;
}
else if ((color>=30) && (color<=39)) {
newcolor&=0xc7;
newcolor|=((color-30)&0x7)<<3;
}
else if ((color>=40) && (color<=49)) {
newcolor&=0xf8;
newcolor|=((color-40)&0x7);
}
color=0;
if (buffer[i]=='m') {
printf("; Color=%02x\n",newcolor);
break;
}
i++;
continue;
}
color*=10;
color+=buffer[i]-0x30;
i++;
}
}
else {
printf("; Color=%x\n",newcolor);
if (newcolor==0x47) { fore=1; back=3;}
else if (newcolor==0x4f) { fore=2; back=3;}
else if (newcolor==0x5f) { fore=4; back=3;}
else if (newcolor==0x7f) { fore=5; back=3;}
else if (newcolor==0x7) { fore=0; back=3;}
else if (newcolor==0x78) { fore=5; back=0;}
else printf("; Unknown color %x!\n",newcolor);
putfont(buffer[i],fore,back);
}
if (buffer[i]=='\n') break;
i++;
}
}
printf("tile_palette:\n");
printf("\t.word $0 ; black r=0 g=0 b=0\n");
printf("\t.word $3def ; d. grey r=7d g=7d b=7d\n");
printf("\t.word $3dff ; red r=ff g=7d b=7d\n");
printf("\t.word $56b5 ; l. grey r=aa g=aa b=aa\n");
printf("\t.word $3ff ; yellow r=ff g=ff b=0\n");
printf("\t.word $7fff ; white r=ff g=ff b=ff\n");
return 0;
}

29
tb_snes/tools/bin2byte.c Normal file
View File

@ -0,0 +1,29 @@
/* converts an .incbin type binary include into an ascii text one */
#include <stdio.h>
int main(int argc, char **argv) {
unsigned char blah;
int result,count=0;
while(1) {
result=fread(&blah,1,1,stdin);
if (result<1) break;
if (count%16==0) {
fprintf(stdout,".byte ");
}
fprintf(stdout,"$%02x",blah);
if (count%16!=15) {
fprintf(stdout,",");
}
else {
fprintf(stdout,"\n");
}
count++;
}
return 0;
}

View File

@ -0,0 +1,20 @@
/* converts a 15-bit BGR SNES style color to HTML #RRGGBB style */
#include <stdio.h>
int main(int argc, char **argv) {
int bgr,b,g,r,html;
fscanf(stdin,"%x",&bgr);
b=(bgr>>10)&0x1f;
g=(bgr>>5)&0x1f;
r=bgr&0x1f;
printf("rgb %x %x %x\n",r,g,b);
html=((r<<3)<<16)|((g<<3)<<8)|(b<<3);
printf("HTML: %x\n",html);
return 0;
}

View File

@ -0,0 +1,41 @@
/* converts a VGA type font (well the top half of one) */
/* into a 2bpp SNES tilemap. */
/* mainly for use converting my TB1 font */
#include <stdio.h>
int main(int argc, char **argv) {
int i,j,k;
unsigned char temp;
for(i=0;i<128;i++) {
if ((i>=0x20) && (i<127))
printf("; Char : 0x%x %c\n",i,i);
else
printf("; Char : 0x%x\n",i);
for(j=0;j<16;j++) {
if (fread(&temp,1,1,stdin)<1) return -1;
if (j<8) {
fprintf(stdout,".byte $%02x,$%02x\t",temp,temp);
fprintf(stdout,"; ");
for(k=0;k<8;k++) {
if ((1<<(7-k))&temp) {
printf("#");
}
else {
printf(" ");
}
}
fprintf(stdout,"\n");
}
}
}
return 0;
}

View File

@ -0,0 +1,57 @@
/* converts a VGA type font (well the top half of one) */
/* into a 4bpp SNES tilemap. */
/* mainly for use converting my TB1 font */
#define COLOR 15
#define START_FONT 0x20
#define END_FONT 0x80
#include <stdio.h>
int main(int argc, char **argv) {
int i,j,k,plane;
unsigned char temp_char[16];
/* Skip to right part of code */
for(i=0;i<START_FONT;i++) {
for(j=0;j<16;j++) {
if (fread(&temp_char[j],1,1,stdin)<1) return -1;
}
}
for(i=START_FONT;i<END_FONT;i++) {
if ((i>=0x20) && (i<127))
printf("; Char : 0x%x %c\n",i,i);
else
printf("; Char : 0x%x\n",i);
for(j=0;j<16;j++) {
if (fread(&temp_char[j],1,1,stdin)<1) return -1;
}
for(plane=0;plane<2;plane++) {
fprintf(stdout,"; planes %d and %d\n",plane*2,(plane*2)+1);
for(j=0;j<16;j++) {
if (j<8) {
fprintf(stdout,".byte $%02x,$%02x\t",temp_char[j],temp_char[j]);
fprintf(stdout,"; ");
for(k=0;k<8;k++) {
if ((1<<(7-k))&temp_char[j]) {
printf("#");
}
else {
printf(" ");
}
}
fprintf(stdout,"\n");
}
}
}
}
return 0;
}

View File

@ -0,0 +1,24 @@
/* converts a VGA type font (well the top half of one) */
/* into a 2bpp SNES tilemap. */
/* mainly for use converting my TB1 font */
#include <stdio.h>
int main(int argc, char **argv) {
int i,j;
unsigned char temp;
for(i=0;i<128;i++) {
for(j=0;j<16;j++) {
if (fread(&temp,1,1,stdin)<1) return -1;
if (j<8) {
fwrite(&temp,1,1,stdout); /* plane 1 */
fwrite(&temp,1,1,stdout); /* plane 2 */
}
}
}
return 0;
}

36
tb_snes/tools/dump_font.c Normal file
View File

@ -0,0 +1,36 @@
/* dumps a 2bpp snes tileset to ASCII asm suitable for including */
#include <stdio.h>
int main(int argc, char **argv) {
int i,j,k;
unsigned char temp;
for(i=0;i<256;i++) {
if ((i>=0x20) && (i<127))
printf("Char : 0x%x %c\n",i,i);
else
printf("Char : 0x%x\n",i);
for(j=0;j<16;j++) {
if (fread(&temp,1,1,stdin)<1) return -1;
printf("0x%02x : ",temp);
for(k=0;k<8;k++) {
if ((1<<(7-k))&temp) {
printf("#");
}
else {
printf(" ");
}
}
printf("\n");
}
}
return 0;
}

40
tb_snes/tools/make_pal.c Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
int convert_bgr2rgb(int rgb) {
int r,g,b,bgr;
r=((rgb>>16)>>3)&0x1f;
g=((rgb>>8)>>3)&0x1f;
b=(rgb>>3)&0x1f;
bgr=(b<<10)|(g<<5)|r;
return bgr;
}
int main(int argc, char **argv) {
int i,r,g,b,rgb,bgr;
fprintf(stdout,"; 15-color BGR palette\n");
for(i=0;i<256;i++) {
if (i%4==0) {
fprintf(stdout,"; palette %d?\n",i/4);
}
if (i%4==3) {
r=((i/4)&1)?0:0xff;
g=((i/4)&2)?0:0xff;
b=((i/4)&4)?0:0xff;
rgb=r<<16|g<<8|b;
bgr=convert_bgr2rgb(rgb);
fprintf(stdout,".word $%04x\t; %06x\n",bgr,rgb);
}
else {
fprintf(stdout,".word $0000\n");
}
}
return 0;
}

View File

@ -0,0 +1,214 @@
/* Converts a 24-bit PCX file to a raw file for use in ECE498 Project 1 */
#include <stdio.h> /* For FILE I/O */
#include <string.h> /* For strncmp */
#include <fcntl.h> /* for open() */
#include <unistd.h> /* for lseek() */
#include <sys/stat.h> /* for file modes */
#include <stdlib.h> /* exit() */
/* Convert to 15-bpp bgr */
int rgb2bgr(int r,int g, int b) {
int r2,g2,b2,bgr;
r2=(r>>3)&0x1f;
g2=(g>>3)&0x1f;
b2=(b>>3)&0x1f;
bgr=(b2<<10)|(g2<<5)|r2;
return bgr;
}
int vmwLoadPCX(int pcx_fd) {
int debug=1,bpp;
int x,y;
int i,numacross,planes=0;
int xsize,ysize,plane;
int xmin,ymin,xmax,ymax,version;
unsigned char pcx_header[128];
unsigned char temp_byte;
/* Open the file */
// pcx_fd=open(filename,O_RDONLY);
//if (pcx_fd<0) {
// printf("ERROR! File \"%s\" not found!\n",filename);
// return -1;
// }
/*************** DECODE THE HEADER *************************/
// lseek(pcx_fd,0,SEEK_SET);
read(pcx_fd,&pcx_header,128);
xmin=(pcx_header[5]<<8)+pcx_header[4];
ymin=(pcx_header[7]<<8)+pcx_header[6];
xmax=(pcx_header[9]<<8)+pcx_header[8];
ymax=(pcx_header[11]<<8)+pcx_header[10];
version=pcx_header[1];
bpp=pcx_header[3];
if (debug) {
fprintf(stderr,"Manufacturer: ");
if (pcx_header[0]==10) fprintf(stderr,"Zsoft\n");
else fprintf(stderr,"Unknown %i\n",pcx_header[0]);
fprintf(stderr,"Version: ");
switch(version) {
case 0: fprintf(stderr,"2.5\n"); break;
case 2: fprintf(stderr,"2.8 w palette\n"); break;
case 3: fprintf(stderr,"2.8 w/o palette\n"); break;
case 4: fprintf(stderr,"Paintbrush for Windows\n"); break;
case 5: fprintf(stderr,"3.0+\n"); break;
default: fprintf(stderr,"Unknown %i\n",version);
}
fprintf(stderr,"Encoding: ");
if (pcx_header[2]==1) fprintf(stderr,"RLE\n");
else fprintf(stderr,"Unknown %i\n",pcx_header[2]);
fprintf(stderr,"BitsPerPixelPerPlane: %i\n",bpp);
fprintf(stderr,"File goes from %i,%i to %i,%i\n",xmin,ymin,xmax,ymax);
fprintf(stderr,"Horizontal DPI: %i\n",(pcx_header[13]<<8)+pcx_header[12]);
fprintf(stderr,"Vertical DPI: %i\n",(pcx_header[15]<<8)+pcx_header[14]);
fprintf(stderr,"Number of colored planes: %i\n",pcx_header[65]);
fprintf(stderr,"Bytes per line: %i\n",(pcx_header[67]<<8)+pcx_header[66]);
fprintf(stderr,"Palette Type: %i\n",(pcx_header[69]<<8)+pcx_header[68]);
fprintf(stderr,"Hscreen Size: %i\n",(pcx_header[71]<<8)+pcx_header[70]);
fprintf(stderr,"Vscreen Size: %i\n",(pcx_header[73]<<8)+pcx_header[72]);
}
planes=pcx_header[65];
// if ((version==5) && (bpp==8) && (pcx_header[65]==3)) type=24;
// else if (version==5) type=8;
// else type=0;
xsize=((xmax-xmin)+1);
ysize=((ymax-ymin)+1);
char *output;
output=calloc((xsize*ysize),sizeof(unsigned int));
if (output==NULL) return -1;
x=0; y=0;
while(y<ysize) {
for(plane=0;plane<planes;plane++) {
x=0;
while (x<xsize) {
read(pcx_fd,&temp_byte,1);
if (0xc0 == (temp_byte&0xc0)) {
numacross=temp_byte&0x3f;
read(pcx_fd,&temp_byte,1);
// fprintf(stderr,"%i pixels of %i\n",numacross,temp_byte);
for(i=0;i<numacross;i++) {
output[(y*xsize)+x]=temp_byte;
// printf("%x ",temp_byte);
x++;
}
}
else {
// fprintf(stderr,"%i, %i Color=%i\n",x,y,temp_byte);
// printf("%x ",temp_byte);
output[(y*xsize)+x]=temp_byte;
x++;
}
}
}
y++;
}
int plane0,plane1,plane2,plane3;
printf("sprite_data:\n");
printf("\t; Plane 0 Plane 1\n");
for(y=0;y<ysize;y++){
plane0=0;plane1=0;
for(x=0;x<xsize;x++) {
plane0<<=1;
plane1<<=1;
plane0|=(output[(y*xsize)+x])&1;
plane1|=(((output[(y*xsize)+x])&2)>>1);
}
printf("\t.word $%02x%02x\n",plane1,plane0);
}
printf("\t; Plane 2 Plane 3\n");
for(y=0;y<ysize;y++){
plane2=0;plane3=0;
for(x=0;x<xsize;x++) {
plane2<<=1;
plane3<<=1;
plane2|=(((output[(y*xsize)+x])&4)>>2);
plane3|=(((output[(y*xsize)+x])&8)>>3);
}
printf("\t.word $%02x%02x\n",plane3,plane2);
}
for(i=0;i<1008;i++) {
printf("\t.word $ffff\n");
}
printf("sprite_palette:\n");
/* read in palette */
read(pcx_fd,&temp_byte,1);
if (temp_byte!=12) {
printf("Error! No palette found!\n");
}
else {
int r,g,b;
for(i=0;i<16;i++) {
read(pcx_fd,&temp_byte,1);
r=temp_byte;
read(pcx_fd,&temp_byte,1);
g=temp_byte;
read(pcx_fd,&temp_byte,1);
b=temp_byte;
printf("\t.word $%x\t; r=%x g=%x b=%x\n",rgb2bgr(r,g,b),r,g,b);
}
}
// close(pcx_fd);
return 0;
}
int main(int argc, char **argv) {
int result;//,x,y;
// FILE *fff;
//char filename[]="butterfinger.pcx";
/* read from stdin */
result=vmwLoadPCX(fileno(stdin));
if (result<0) {
fprintf(stderr,"Error reading PCX from stdin\n");
exit(1);
}
// fff=fopen("input.bin","w");
//if (fff==NULL) exit(1);
// fwrite(buffer,sizeof(unsigned int),buffer[0]*buffer[1]+2,stdout);
return 0;
}

View File

@ -0,0 +1,228 @@
/* Converts a 24-bit PCX file to SNES background tiles */
#include <stdio.h> /* For FILE I/O */
#include <string.h> /* For strncmp */
#include <fcntl.h> /* for open() */
#include <unistd.h> /* for lseek() */
#include <sys/stat.h> /* for file modes */
#include <stdlib.h> /* exit() */
/* Convert to 15-bpp bgr */
int rgb2bgr(int r,int g, int b) {
int r2,g2,b2,bgr;
r2=(r>>3)&0x1f;
g2=(g>>3)&0x1f;
b2=(b>>3)&0x1f;
bgr=(b2<<10)|(g2<<5)|r2;
return bgr;
}
int vmwLoadPCX(int pcx_fd) {
int debug=1,bpp;
int x,y;
int i,numacross,planes=0;
int xsize,ysize,plane;
int xmin,ymin,xmax,ymax,version;
unsigned char pcx_header[128];
unsigned char temp_byte;
/* Open the file */
// pcx_fd=open(filename,O_RDONLY);
//if (pcx_fd<0) {
// printf("ERROR! File \"%s\" not found!\n",filename);
// return -1;
// }
/*************** DECODE THE HEADER *************************/
// lseek(pcx_fd,0,SEEK_SET);
read(pcx_fd,&pcx_header,128);
xmin=(pcx_header[5]<<8)+pcx_header[4];
ymin=(pcx_header[7]<<8)+pcx_header[6];
xmax=(pcx_header[9]<<8)+pcx_header[8];
ymax=(pcx_header[11]<<8)+pcx_header[10];
version=pcx_header[1];
bpp=pcx_header[3];
if (debug) {
fprintf(stderr,"Manufacturer: ");
if (pcx_header[0]==10) fprintf(stderr,"Zsoft\n");
else fprintf(stderr,"Unknown %i\n",pcx_header[0]);
fprintf(stderr,"Version: ");
switch(version) {
case 0: fprintf(stderr,"2.5\n"); break;
case 2: fprintf(stderr,"2.8 w palette\n"); break;
case 3: fprintf(stderr,"2.8 w/o palette\n"); break;
case 4: fprintf(stderr,"Paintbrush for Windows\n"); break;
case 5: fprintf(stderr,"3.0+\n"); break;
default: fprintf(stderr,"Unknown %i\n",version);
}
fprintf(stderr,"Encoding: ");
if (pcx_header[2]==1) fprintf(stderr,"RLE\n");
else fprintf(stderr,"Unknown %i\n",pcx_header[2]);
fprintf(stderr,"BitsPerPixelPerPlane: %i\n",bpp);
fprintf(stderr,"File goes from %i,%i to %i,%i\n",xmin,ymin,xmax,ymax);
fprintf(stderr,"Horizontal DPI: %i\n",(pcx_header[13]<<8)+pcx_header[12]);
fprintf(stderr,"Vertical DPI: %i\n",(pcx_header[15]<<8)+pcx_header[14]);
fprintf(stderr,"Number of colored planes: %i\n",pcx_header[65]);
fprintf(stderr,"Bytes per line: %i\n",(pcx_header[67]<<8)+pcx_header[66]);
fprintf(stderr,"Palette Type: %i\n",(pcx_header[69]<<8)+pcx_header[68]);
fprintf(stderr,"Hscreen Size: %i\n",(pcx_header[71]<<8)+pcx_header[70]);
fprintf(stderr,"Vscreen Size: %i\n",(pcx_header[73]<<8)+pcx_header[72]);
}
planes=pcx_header[65];
// if ((version==5) && (bpp==8) && (pcx_header[65]==3)) type=24;
// else if (version==5) type=8;
// else type=0;
xsize=((xmax-xmin)+1);
ysize=((ymax-ymin)+1);
char *output;
output=calloc((xsize*ysize),sizeof(unsigned int));
if (output==NULL) return -1;
x=0; y=0;
while(y<ysize) {
for(plane=0;plane<planes;plane++) {
x=0;
while (x<xsize) {
read(pcx_fd,&temp_byte,1);
if (0xc0 == (temp_byte&0xc0)) {
numacross=temp_byte&0x3f;
read(pcx_fd,&temp_byte,1);
// fprintf(stderr,"%i pixels of %i\n",numacross,temp_byte);
for(i=0;i<numacross;i++) {
output[(y*xsize)+x]=temp_byte;
// printf("%x ",temp_byte);
x++;
}
}
else {
// fprintf(stderr,"%i, %i Color=%i\n",x,y,temp_byte);
// printf("%x ",temp_byte);
output[(y*xsize)+x]=temp_byte;
x++;
}
}
}
y++;
}
#define X_CHUNKSIZE 8
#define Y_CHUNKSIZE 8
unsigned int plane0,plane1,plane2,plane3,offset;
printf("tile_data:\n");
int ychunk,xchunk;
for(ychunk=0;ychunk<ysize/Y_CHUNKSIZE;ychunk++) {
for(xchunk=0;xchunk<xsize/X_CHUNKSIZE;xchunk++) {
printf("\t; Tile %d %d, Plane 0 Plane 1\n",xchunk,ychunk);
for(y=0;y<Y_CHUNKSIZE;y++){
plane0=0;plane1=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane0<<=1;
plane1<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane0|=(output[offset])&1;
plane1|=(((output[offset])&2)>>1);
}
printf("\t.word $%02x%02x\n",plane1,plane0);
}
printf("\t; Plane 2 Plane 3\n");
for(y=0;y<Y_CHUNKSIZE;y++){
plane2=0;plane3=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane2<<=1;
plane3<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane2|=(((output[offset])&4)>>2);
plane3|=(((output[offset])&8)>>3);
}
printf("\t.word $%02x%02x\n",plane3,plane2);
}
}
}
// for(i=0;i<1008;i++) {
// printf("\t.word $ffff\n");
// }
printf("tile_palette:\n");
/* read in palette */
read(pcx_fd,&temp_byte,1);
if (temp_byte!=12) {
printf("Error! No palette found!\n");
}
else {
int r,g,b;
for(i=0;i<16;i++) {
read(pcx_fd,&temp_byte,1);
r=temp_byte;
read(pcx_fd,&temp_byte,1);
g=temp_byte;
read(pcx_fd,&temp_byte,1);
b=temp_byte;
printf("\t.word $%x\t; r=%x g=%x b=%x\n",rgb2bgr(r,g,b),r,g,b);
}
}
// close(pcx_fd);
return 0;
}
int main(int argc, char **argv) {
int result;//,x,y;
// FILE *fff;
//char filename[]="butterfinger.pcx";
/* read from stdin */
result=vmwLoadPCX(fileno(stdin));
if (result<0) {
fprintf(stderr,"Error reading PCX from stdin\n");
exit(1);
}
// fff=fopen("input.bin","w");
//if (fff==NULL) exit(1);
// fwrite(buffer,sizeof(unsigned int),buffer[0]*buffer[1]+2,stdout);
return 0;
}

View File

@ -0,0 +1,230 @@
/* Converts a 24-bit PCX file to 256-color SNES background tiles */
#include <stdio.h> /* For FILE I/O */
#include <string.h> /* For strncmp */
#include <fcntl.h> /* for open() */
#include <unistd.h> /* for lseek() */
#include <sys/stat.h> /* for file modes */
#include <stdlib.h> /* exit() */
/* Convert to 15-bpp bgr */
int rgb2bgr(int r,int g, int b) {
int r2,g2,b2,bgr;
r2=(r>>3)&0x1f;
g2=(g>>3)&0x1f;
b2=(b>>3)&0x1f;
bgr=(b2<<10)|(g2<<5)|r2;
return bgr;
}
/* File already open */
int vmwLoadPCX(int pcx_fd) {
int debug=1,bpp;
int x,y;
int i,numacross,planes=0;
int xsize,ysize,plane;
int xmin,ymin,xmax,ymax,version;
unsigned char pcx_header[128];
unsigned char temp_byte;
/*************** DECODE THE HEADER *************************/
read(pcx_fd,&pcx_header,128);
xmin=(pcx_header[5]<<8)+pcx_header[4];
ymin=(pcx_header[7]<<8)+pcx_header[6];
xmax=(pcx_header[9]<<8)+pcx_header[8];
ymax=(pcx_header[11]<<8)+pcx_header[10];
version=pcx_header[1];
bpp=pcx_header[3];
if (debug) {
fprintf(stderr,"Manufacturer: ");
if (pcx_header[0]==10) fprintf(stderr,"Zsoft\n");
else fprintf(stderr,"Unknown %i\n",pcx_header[0]);
fprintf(stderr,"Version: ");
switch(version) {
case 0: fprintf(stderr,"2.5\n"); break;
case 2: fprintf(stderr,"2.8 w palette\n"); break;
case 3: fprintf(stderr,"2.8 w/o palette\n"); break;
case 4: fprintf(stderr,"Paintbrush for Windows\n"); break;
case 5: fprintf(stderr,"3.0+\n"); break;
default: fprintf(stderr,"Unknown %i\n",version);
}
fprintf(stderr,"Encoding: ");
if (pcx_header[2]==1) fprintf(stderr,"RLE\n");
else fprintf(stderr,"Unknown %i\n",pcx_header[2]);
fprintf(stderr,"BitsPerPixelPerPlane: %i\n",bpp);
fprintf(stderr,"File goes from %i,%i to %i,%i\n",xmin,ymin,xmax,ymax);
fprintf(stderr,"Horizontal DPI: %i\n",(pcx_header[13]<<8)+pcx_header[12]);
fprintf(stderr,"Vertical DPI: %i\n",(pcx_header[15]<<8)+pcx_header[14]);
fprintf(stderr,"Number of colored planes: %i\n",pcx_header[65]);
fprintf(stderr,"Bytes per line: %i\n",(pcx_header[67]<<8)+pcx_header[66]);
fprintf(stderr,"Palette Type: %i\n",(pcx_header[69]<<8)+pcx_header[68]);
fprintf(stderr,"Hscreen Size: %i\n",(pcx_header[71]<<8)+pcx_header[70]);
fprintf(stderr,"Vscreen Size: %i\n",(pcx_header[73]<<8)+pcx_header[72]);
}
planes=pcx_header[65];
xsize=((xmax-xmin)+1);
ysize=((ymax-ymin)+1);
char *output;
output=calloc((xsize*ysize),sizeof(unsigned int));
if (output==NULL) return -1;
x=0; y=0;
while(y<ysize) {
for(plane=0;plane<planes;plane++) {
x=0;
while (x<xsize) {
read(pcx_fd,&temp_byte,1);
if (0xc0 == (temp_byte&0xc0)) {
numacross=temp_byte&0x3f;
read(pcx_fd,&temp_byte,1);
// fprintf(stderr,"%i pixels of %i\n",numacross,temp_byte);
for(i=0;i<numacross;i++) {
output[(y*xsize)+x]=temp_byte;
// printf("%x ",temp_byte);
x++;
}
}
else {
// fprintf(stderr,"%i, %i Color=%i\n",x,y,temp_byte);
// printf("%x ",temp_byte);
output[(y*xsize)+x]=temp_byte;
x++;
}
}
}
y++;
}
#define X_CHUNKSIZE 8
#define Y_CHUNKSIZE 8
unsigned int plane0,plane1,plane2,plane3;
unsigned int plane4,plane5,plane6,plane7,offset;
printf("tile_data:\n");
int ychunk,xchunk;
for(ychunk=0;ychunk<ysize/Y_CHUNKSIZE;ychunk++) {
for(xchunk=0;xchunk<xsize/X_CHUNKSIZE;xchunk++) {
printf("\t; Tile %d %d, Plane 0 Plane 1\n",xchunk,ychunk);
for(y=0;y<Y_CHUNKSIZE;y++){
plane0=0;plane1=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane0<<=1;
plane1<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane0|=(output[offset])&1;
plane1|=(((output[offset])&2)>>1);
}
printf("\t.word $%02x%02x\n",plane1,plane0);
}
printf("\t; Plane 2 Plane 3\n");
for(y=0;y<Y_CHUNKSIZE;y++){
plane2=0;plane3=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane2<<=1;
plane3<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane2|=(((output[offset])&4)>>2);
plane3|=(((output[offset])&8)>>3);
}
printf("\t.word $%02x%02x\n",plane3,plane2);
}
printf("\t; Plane 4 Plane 5\n");
for(y=0;y<Y_CHUNKSIZE;y++){
plane4=0;plane5=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane4<<=1;
plane5<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane4|=(((output[offset])&16)>>4);
plane5|=(((output[offset])&32)>>5);
}
printf("\t.word $%02x%02x\n",plane5,plane4);
}
printf("\t; Plane 6 Plane 7\n");
for(y=0;y<Y_CHUNKSIZE;y++){
plane6=0;plane7=0;
for(x=0;x<X_CHUNKSIZE;x++) {
plane6<<=1;
plane7<<=1;
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
plane6|=(((output[offset])&64)>>6);
plane7|=(((output[offset])&128)>>7);
}
printf("\t.word $%02x%02x\n",plane7,plane6);
}
}
}
printf("tile_palette:\n");
/* read in palette */
read(pcx_fd,&temp_byte,1);
if (temp_byte!=12) {
fprintf(stderr,"Error! No palette found!\n");
}
else {
int r,g,b;
for(i=0;i<256;i++) {
read(pcx_fd,&temp_byte,1);
r=temp_byte;
read(pcx_fd,&temp_byte,1);
g=temp_byte;
read(pcx_fd,&temp_byte,1);
b=temp_byte;
printf("\t.word $%x\t; r=%x g=%x b=%x\n",rgb2bgr(r,g,b),r,g,b);
}
}
return 0;
}
int main(int argc, char **argv) {
int result;
/* read from stdin */
result=vmwLoadPCX(fileno(stdin));
if (result<0) {
fprintf(stderr,"Error reading PCX from stdin\n");
exit(1);
}
return 0;
}

View File

@ -0,0 +1,35 @@
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
char buffer[BUFSIZ],*result;
int i;
int quotes_open=0;
while(1) {
result=fgets(buffer,BUFSIZ,stdin);
if (result==NULL) return 0;
printf("\t.byte ");
for(i=0;i<strlen(buffer);i++) {
if (buffer[i] < 32) {
if (quotes_open) printf("\",");
printf("$%02x",buffer[i]);
if (i<strlen(buffer)-1) printf(", ");
quotes_open=0;
}
else {
if (!quotes_open) printf("\"");
printf("%c",buffer[i]);
quotes_open=1;
}
}
if (quotes_open) printf("\"");
quotes_open=0;
printf("\n");
}
return 0;
}