mirror of
https://github.com/deater/tb1.git
synced 2025-01-02 22:31:47 +00:00
snes: pcx_to_tiles_4bpp
clean up code, add command line argument for label name
This commit is contained in:
parent
2687dfe6a2
commit
8e77643bb4
@ -7,22 +7,24 @@
|
|||||||
#include <sys/stat.h> /* for file modes */
|
#include <sys/stat.h> /* for file modes */
|
||||||
#include <stdlib.h> /* exit() */
|
#include <stdlib.h> /* exit() */
|
||||||
|
|
||||||
|
char label_prefix[BUFSIZ]="tile";
|
||||||
|
|
||||||
/* Convert to 15-bpp bgr */
|
/* Convert to 15-bpp bgr */
|
||||||
int rgb2bgr(int r,int g, int b) {
|
static int rgb2bgr(int r,int g, int b) {
|
||||||
int r2,g2,b2,bgr;
|
int r2,g2,b2,bgr;
|
||||||
|
|
||||||
r2=(r>>3)&0x1f;
|
r2=(r>>3)&0x1f;
|
||||||
g2=(g>>3)&0x1f;
|
g2=(g>>3)&0x1f;
|
||||||
b2=(b>>3)&0x1f;
|
b2=(b>>3)&0x1f;
|
||||||
|
|
||||||
bgr=(b2<<10)|(g2<<5)|r2;
|
bgr=(b2<<10)|(g2<<5)|r2;
|
||||||
|
|
||||||
return bgr;
|
return bgr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int vmwLoadPCX(int pcx_fd) {
|
static int vmwLoadPCX(int pcx_fd) {
|
||||||
|
|
||||||
int debug=1,bpp;
|
int debug=1,bpp;
|
||||||
int x,y;
|
int x,y;
|
||||||
int i,numacross,planes=0;
|
int i,numacross,planes=0;
|
||||||
@ -30,36 +32,28 @@ int vmwLoadPCX(int pcx_fd) {
|
|||||||
int xmin,ymin,xmax,ymax,version;
|
int xmin,ymin,xmax,ymax,version;
|
||||||
unsigned char pcx_header[128];
|
unsigned char pcx_header[128];
|
||||||
unsigned char temp_byte;
|
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 *************************/
|
/*************** DECODE THE HEADER *************************/
|
||||||
|
|
||||||
// lseek(pcx_fd,0,SEEK_SET);
|
// lseek(pcx_fd,0,SEEK_SET);
|
||||||
|
|
||||||
read(pcx_fd,&pcx_header,128);
|
read(pcx_fd,&pcx_header,128);
|
||||||
|
|
||||||
xmin=(pcx_header[5]<<8)+pcx_header[4];
|
xmin=(pcx_header[5]<<8)+pcx_header[4];
|
||||||
ymin=(pcx_header[7]<<8)+pcx_header[6];
|
ymin=(pcx_header[7]<<8)+pcx_header[6];
|
||||||
|
|
||||||
xmax=(pcx_header[9]<<8)+pcx_header[8];
|
xmax=(pcx_header[9]<<8)+pcx_header[8];
|
||||||
ymax=(pcx_header[11]<<8)+pcx_header[10];
|
ymax=(pcx_header[11]<<8)+pcx_header[10];
|
||||||
|
|
||||||
version=pcx_header[1];
|
version=pcx_header[1];
|
||||||
bpp=pcx_header[3];
|
bpp=pcx_header[3];
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
|
|
||||||
fprintf(stderr,"Manufacturer: ");
|
fprintf(stderr,"Manufacturer: ");
|
||||||
if (pcx_header[0]==10) fprintf(stderr,"Zsoft\n");
|
if (pcx_header[0]==10) fprintf(stderr,"Zsoft\n");
|
||||||
else fprintf(stderr,"Unknown %i\n",pcx_header[0]);
|
else fprintf(stderr,"Unknown %i\n",pcx_header[0]);
|
||||||
|
|
||||||
fprintf(stderr,"Version: ");
|
fprintf(stderr,"Version: ");
|
||||||
|
|
||||||
switch(version) {
|
switch(version) {
|
||||||
@ -73,19 +67,19 @@ int vmwLoadPCX(int pcx_fd) {
|
|||||||
fprintf(stderr,"Encoding: ");
|
fprintf(stderr,"Encoding: ");
|
||||||
if (pcx_header[2]==1) fprintf(stderr,"RLE\n");
|
if (pcx_header[2]==1) fprintf(stderr,"RLE\n");
|
||||||
else fprintf(stderr,"Unknown %i\n",pcx_header[2]);
|
else fprintf(stderr,"Unknown %i\n",pcx_header[2]);
|
||||||
|
|
||||||
fprintf(stderr,"BitsPerPixelPerPlane: %i\n",bpp);
|
fprintf(stderr,"BitsPerPixelPerPlane: %i\n",bpp);
|
||||||
fprintf(stderr,"File goes from %i,%i to %i,%i\n",xmin,ymin,xmax,ymax);
|
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,"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,"Vertical DPI: %i\n",(pcx_header[15]<<8)+pcx_header[14]);
|
||||||
|
|
||||||
fprintf(stderr,"Number of colored planes: %i\n",pcx_header[65]);
|
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,"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,"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,"Hscreen Size: %i\n",(pcx_header[71]<<8)+pcx_header[70]);
|
||||||
fprintf(stderr,"Vscreen Size: %i\n",(pcx_header[73]<<8)+pcx_header[72]);
|
fprintf(stderr,"Vscreen Size: %i\n",(pcx_header[73]<<8)+pcx_header[72]);
|
||||||
|
|
||||||
}
|
}
|
||||||
planes=pcx_header[65];
|
planes=pcx_header[65];
|
||||||
|
|
||||||
@ -97,15 +91,15 @@ int vmwLoadPCX(int pcx_fd) {
|
|||||||
ysize=((ymax-ymin)+1);
|
ysize=((ymax-ymin)+1);
|
||||||
|
|
||||||
char *output;
|
char *output;
|
||||||
|
|
||||||
output=calloc((xsize*ysize),sizeof(unsigned int));
|
output=calloc((xsize*ysize),sizeof(unsigned int));
|
||||||
if (output==NULL) return -1;
|
if (output==NULL) return -1;
|
||||||
|
|
||||||
x=0; y=0;
|
x=0; y=0;
|
||||||
|
|
||||||
while(y<ysize) {
|
while(y<ysize) {
|
||||||
for(plane=0;plane<planes;plane++) {
|
for(plane=0;plane<planes;plane++) {
|
||||||
x=0;
|
x=0;
|
||||||
while (x<xsize) {
|
while (x<xsize) {
|
||||||
read(pcx_fd,&temp_byte,1);
|
read(pcx_fd,&temp_byte,1);
|
||||||
if (0xc0 == (temp_byte&0xc0)) {
|
if (0xc0 == (temp_byte&0xc0)) {
|
||||||
@ -131,30 +125,30 @@ int vmwLoadPCX(int pcx_fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define X_CHUNKSIZE 8
|
#define X_CHUNKSIZE 8
|
||||||
#define Y_CHUNKSIZE 8
|
#define Y_CHUNKSIZE 8
|
||||||
|
|
||||||
|
|
||||||
unsigned int plane0,plane1,plane2,plane3,offset;
|
unsigned int plane0,plane1,plane2,plane3,offset;
|
||||||
|
|
||||||
printf("tile_data:\n");
|
printf("%s_data:\n",label_prefix);
|
||||||
int ychunk,xchunk;
|
int ychunk,xchunk;
|
||||||
for(ychunk=0;ychunk<ysize/Y_CHUNKSIZE;ychunk++) {
|
for(ychunk=0;ychunk<ysize/Y_CHUNKSIZE;ychunk++) {
|
||||||
for(xchunk=0;xchunk<xsize/X_CHUNKSIZE;xchunk++) {
|
for(xchunk=0;xchunk<xsize/X_CHUNKSIZE;xchunk++) {
|
||||||
printf("\t; Tile %d %d, Plane 0 Plane 1\n",xchunk,ychunk);
|
printf("\t; Tile %d %d, Plane 0 Plane 1\n",xchunk,ychunk);
|
||||||
|
|
||||||
for(y=0;y<Y_CHUNKSIZE;y++){
|
for(y=0;y<Y_CHUNKSIZE;y++){
|
||||||
plane0=0;plane1=0;
|
plane0=0;plane1=0;
|
||||||
for(x=0;x<X_CHUNKSIZE;x++) {
|
for(x=0;x<X_CHUNKSIZE;x++) {
|
||||||
plane0<<=1;
|
plane0<<=1;
|
||||||
plane1<<=1;
|
plane1<<=1;
|
||||||
|
|
||||||
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
|
offset=((ychunk*Y_CHUNKSIZE+y)*xsize)+(xchunk*X_CHUNKSIZE)+x;
|
||||||
plane0|=(output[offset])&1;
|
plane0|=(output[offset])&1;
|
||||||
plane1|=(((output[offset])&2)>>1);
|
plane1|=(((output[offset])&2)>>1);
|
||||||
}
|
}
|
||||||
printf("\t.word $%02x%02x\n",plane1,plane0);
|
printf("\t.word $%02x%02x\n",plane1,plane0);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\t; Plane 2 Plane 3\n");
|
printf("\t; Plane 2 Plane 3\n");
|
||||||
for(y=0;y<Y_CHUNKSIZE;y++){
|
for(y=0;y<Y_CHUNKSIZE;y++){
|
||||||
plane2=0;plane3=0;
|
plane2=0;plane3=0;
|
||||||
@ -170,14 +164,8 @@ int vmwLoadPCX(int pcx_fd) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// for(i=0;i<1008;i++) {
|
printf("%s_palette:\n",label_prefix);
|
||||||
// printf("\t.word $ffff\n");
|
|
||||||
// }
|
|
||||||
|
|
||||||
printf("tile_palette:\n");
|
|
||||||
|
|
||||||
/* read in palette */
|
/* read in palette */
|
||||||
read(pcx_fd,&temp_byte,1);
|
read(pcx_fd,&temp_byte,1);
|
||||||
@ -185,8 +173,8 @@ int vmwLoadPCX(int pcx_fd) {
|
|||||||
printf("Error! No palette found!\n");
|
printf("Error! No palette found!\n");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
int r,g,b;
|
int r,g,b;
|
||||||
for(i=0;i<16;i++) {
|
for(i=0;i<16;i++) {
|
||||||
read(pcx_fd,&temp_byte,1);
|
read(pcx_fd,&temp_byte,1);
|
||||||
r=temp_byte;
|
r=temp_byte;
|
||||||
read(pcx_fd,&temp_byte,1);
|
read(pcx_fd,&temp_byte,1);
|
||||||
@ -196,7 +184,7 @@ int vmwLoadPCX(int pcx_fd) {
|
|||||||
printf("\t.word $%x\t; r=%x g=%x b=%x\n",rgb2bgr(r,g,b),r,g,b);
|
printf("\t.word $%x\t; r=%x g=%x b=%x\n",rgb2bgr(r,g,b),r,g,b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// close(pcx_fd);
|
// close(pcx_fd);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -204,25 +192,16 @@ int vmwLoadPCX(int pcx_fd) {
|
|||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
int result;//,x,y;
|
int result;
|
||||||
// FILE *fff;
|
|
||||||
|
|
||||||
//char filename[]="butterfinger.pcx";
|
|
||||||
|
|
||||||
/* read from stdin */
|
/* read from stdin */
|
||||||
|
|
||||||
result=vmwLoadPCX(fileno(stdin));
|
result=vmwLoadPCX(fileno(stdin));
|
||||||
|
|
||||||
if (result<0) {
|
if (result<0) {
|
||||||
fprintf(stderr,"Error reading PCX from stdin\n");
|
fprintf(stderr,"Error reading PCX from stdin\n");
|
||||||
exit(1);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user