home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fun CD 26
/
OTACD26.ISO
/
archive
/
game
/
nextk
/
nextk.lzh
/
NEXTK.C
next >
Wrap
C/C++ Source or Header
|
1997-07-07
|
4KB
|
169 lines
/******************************
* ネクストキング BMP変換 *
* Programmed By Melody-Yoshi *
******************************/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
unsigned char *mem,*by,*bz,pal[512];
FILE *fp,*fo;
struct tbs{
int p,size,f;
};
typedef struct tbs* tb;
tb tbp;
int xwide,ywide,xseek,xpad;
void writebmphead08(short);
void writelong(long),writeshort(short);
void analyze(char *,int),copyn88(int,int,int,int);
int decode(int,int);
void main(int argc,char *argv[])
{
char fn[64],fz[64];
int i,fsize;
if(argc<2){
printf("nextk [file].bin\n");exit(1);
}
strcpy(fn,argv[1]);for(i=0;fn[i];i++)if(fn[i]=='.')break;
strcpy(&fn[i],".bin");
if((fp=fopen(fn,"rb"))==0){
printf("file open error![%s]\n",fn);exit(1);
}
strcpy(fz,fn);
for(fz[i]=0;i>=0;i--)if(fz[i]==':' || fz[i]=='\\')break;
strcpy(fn,&fz[i+1]);
fseek(fp,0L,2);fsize=ftell(fp);fseek(fp,0L,0);
bz=(unsigned char *)malloc(0x28000);
by=(unsigned char *)malloc(0x28000);
mem=(unsigned char *)malloc(fsize+16);
if(mem==NULL || bz==NULL || by==NULL){
printf("insufficient memory!\n");exit(1);
}
fread(mem,1,fsize,fp);fclose(fp);
analyze(fn,fsize);
exit(0);
}
void analyze(char *fn,int fsize)
{
int cnt,i,j,k,q,s,dx,dy,sx,sy;
char fz[64],fs[4];
fs[2]=0;cnt=mem[0]|(mem[1]<<8);
tbp=(tb)malloc(sizeof(struct tbs)*cnt);
for(q=4,i=0;i<cnt;i++,q+=4){
tbp[i].f=0;tbp[i].p=mem[q]|(mem[q+1]<<8)|(mem[q+2]<<16);
}
for(i=0;i<cnt-1;i++)tbp[i].size=tbp[i+1].p-tbp[i].p;
tbp[i].size=fsize-tbp[i].p;
for(i=0;i<cnt;i++)if(tbp[i].size==0x204)tbp[i].f=1;
xwide=320;
for(i=j=0;i<cnt;i++){
#ifdef DEBUG
for(s=0;s<8;s++)fprintf(stderr,"%2X ",mem[tbp[i].p+s]);
fprintf(stderr,"\n");
#endif
if(tbp[i].f)continue;
q=tbp[i].p;dx=(mem[q]|(mem[q+1]<<8))*2;dy=mem[q+2]+((mem[q+3]&1)<<8);
if((mem[q+3]&0xFC)!=0x40)continue;
sx=(mem[q+4]|(mem[q+5]<<8));sy=(mem[q+6]|(mem[q+7]<<8));
if(sx>320)sx=0;
if(sy>256)sy=0;
ywide=(sy+dy>240)?sy+dy:240;
decode(q+8,dx*dy);
for(s=0;s<cnt;s++)if(tbp[s].f)break;
if(dx==320 && dy>=240 && j){
for(s++;s<cnt;s++)if(tbp[s].f)break;
}
if(s==cnt)continue;
copyn88(sx,sy,dx,dy);
for(k=0,q=tbp[s].p+4;k<512;k++,q++)pal[k]=mem[q];
fs[0]=j/16+'0';if(fs[0]>0x39)fs[0]+=7;
fs[1]=(j&15)+'0';j++;if(fs[1]>0x39)fs[1]+=7;
xseek=xwide;xpad=(4-(xseek&3))&3;xseek+=xpad;
strcpy(fz,fn);strcat(fz,fs);strcat(fz,".BMP");
if((fo=fopen(fz,"wb"))==0){
printf("file create error![%s]\n",fz);exit(1);
}
writebmphead08(256);
for(s=ywide-1;s>=0;s--){
fwrite(&by[s*xwide],1,xwide,fo);
for(k=0;k<xpad;k++)fputc(0,fo);
}
fclose(fo);
}
free(tbp);
}
int decode(int r,int dl)
{
int k,m,p,cnt,dist;
for(p=k=0;p<dl;k>>=1){
if((k&0xFF00)==0)k=mem[r++]|0xFF00;
if(k&1)bz[p++]=mem[r++];
else{
k>>=1;if((k&0xFF00)==0)k=mem[r++]|0xFF00;
m=mem[r++];
if(k&1){
cnt=((m&0xC0)>>6)+2;dist=(m&0x3F)-64;
}
else{
m=(m<<8)|mem[r++];cnt=((m&0xF000)>>12)+2;dist=(m&0xFFF)-4096;
if(cnt==2){
cnt=mem[r++]+2;if(cnt==2)k=cnt=0;
}
}
for(m=0;m<cnt;m++,p++)bz[p]=bz[p+dist];
}
}
return(p);
}
void copyn88(int sx,int sy,int dx,int dy)
{
int i,j,k,m;
for(i=xwide*ywide-1;i>=0;i--)by[i]=0;
for(i=k=0;i<dy;i++){
m=(sy+i)*xwide+sx;
for(j=0;j<dx;j++,k++)by[m+j]=bz[k];
}
}
/* WRITE BMP HEADER */
void writebmphead08(short c)
{
short i;
unsigned short k;
fputc('B',fo);fputc('M',fo); /* ID */
writelong((long)c*4L+54L+(long)(xwide+xpad)*(long)ywide); /* FILE SIZE */
writelong(0L); /* RESERVED */
writelong((long)c*4L+54L); /* IMAGE OFFSET */
writelong(40L); /* INFO SIZE */
writelong((long)xwide); /* dx */
writelong((long)ywide); /* dy */
writeshort(1); /* PLAIN */
if(c==256)writeshort(8); /* DEPTH */
else writeshort(4);
writelong(0L); /* COMPRESS TYPE */
writelong(0L);
writelong(0L); /* x per meter */
writelong(0L); /* y per meter */
writelong((long)c); /* COLOR */
writelong(0L);
for(i=0;i<c;i++){
k=pal[i*2]+(pal[i*2+1]<<8);
fputc((k&0x7c00)>>7,fo); /* BLUE */
fputc((k&0x3e0)>>2,fo); /* GREEN */
fputc((k&0x1f)<<3,fo); /* RED */
fputc(0,fo);
}
}
/* WRITE SHORT VALUE */
void writeshort(short n)
{
fputc(n&255,fo);fputc((n>>8)&255,fo);
}
/* WRITE LONG VALUE */
void writelong(long n)
{
fputc((int)(n&255),fo);n/=256L;
fputc((int)(n&255),fo);n/=256L;
fputc((int)(n&255),fo);n/=256L;
fputc((int)(n&255),fo);
}