home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fun CD 26
/
OTACD26.ISO
/
archive
/
game
/
tarms
/
tarms.lzh
/
TARMS.C
next >
Wrap
C/C++ Source or Header
|
1998-12-23
|
6KB
|
226 lines
/******************************
* サウザンドアームズ BMP変換 *
* Programmed By Melody-Yoshi *
******************************/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#include<commdlg.h>
/* GLOBAL */
unsigned char pal[0x200],*mem,*bz,*tb;
char current[128];
int xwide,ywide;
FILE *fp,*fo;
/* PROTO-TYPES */
int decode(unsigned char *,unsigned char *,int *);
void analyze(void);
void outbmp8(char *,int,int),writelong(long),writeshort(short);
void main(int argc,char *argv[])
{
int i;
char fn[96],fz[96];
WIN32_FIND_DATA fd;
HANDLE h;
BOOL f;
if(argc<2){
OPENFILENAME ofn;
char szFile[256];
szFile[0]=0;
memset(&ofn,0,sizeof(OPENFILENAME));
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=NULL;ofn.nFilterIndex=1;
ofn.lpstrFilter="サウザンドアームズの画像ファイル\0*.STM\0\0";
ofn.lpstrFile=szFile;ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFileTitle=NULL;ofn.lpstrInitialDir=NULL;
ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST;
if(GetOpenFileName(&ofn)){
strcpy(fn,szFile);
}
else exit(1);
}
else strcpy(fn,argv[1]);
strcpy(current,argv[0]);
for(i=strlen(current)-1;i>=0;i--)if(current[i]==':' || current[i]=='\\')break;
current[i+1]=0;
mem=(unsigned char *)malloc(0x86000);
bz=(unsigned char *)malloc(0x4B000);
tb=(unsigned char *)malloc(0xF000);
if(mem==NULL || bz==NULL || tb==NULL){
printf("メモリが足りません。\n");exit(1);
}
for(i=0;fn[i];i++)if(fn[i]=='.')break;
fn[i]=0;strcpy(fz,fn);strcat(fz,".STM");
h=FindFirstFile(fz,&fd);f=TRUE;
for(;i>=0;i--)if(fn[i]==':' || fn[i]=='\\')break;
fn[i+1]=0;
while(h!=INVALID_HANDLE_VALUE && f){
if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0){
strcpy(fz,fn);strcat(fz,fd.cFileName);
printf("%s\n",fz);fflush(stdout);
if((fp=fopen(fz,"rb"))==0){
printf("file open error![%s]\n",fz);exit(1);
}
analyze();fclose(fp);
}
f=FindNextFile(h,&fd);
}
free(tb);free(mem);free(bz);exit(0);
}
void analyze(void)
{
int i,j,k,m,t,v,w;
char fn[16];
for(k=0;;k+=0x7E0){
fread(&tb[k],1,0x800,fp);memcpy(&tb[k],&tb[k+32],0x7E0);
if(tb[k+0x7DC]+tb[k+0x7DD]+tb[k+0x7DE]+tb[k+0x7DF]==0)break;
}
for(m=0,k+=0x7E0;m<k;m+=20)if(tb[m]==0)break;
m/=20;
for(i=0;i<m;i++){
j=i*20;
memcpy(fn,&tb[j],12);fn[12]=0;
for(k=0;k<12;k++)if(fn[k]=='.')break;
if(fn[k+1]=='T' && fn[k+2]=='E' && fn[k+3]=='X'){
fn[k]=0;
k=(tb[j+16]|(tb[j+17]<<8)|(tb[j+18]<<16)|(tb[j+19]<<24))<<11;
#ifdef SODINA
printf("NAME=%s SEEK=%X ",fn,k);fflush(stdout);
#endif
fseek(fp,(long)k,0);
k=t=tb[j+12]|(tb[j+13]<<8)|(tb[j+14]<<16);
#ifdef SODINA
printf("READ=%X\n",k);fflush(stdout);
#endif
for(j=0;k>0;k-=0x7E0,j+=0x7E0){
fread(&mem[j],1,0x800,fp);memcpy(&mem[j],&mem[j+32],0x7E0);
}
for(j=4,k=0,v=256;j<t;){
w=v;j+=decode(&mem[j],bz,&w);
if(w==512){
memcpy(pal,bz,512);v=256;
}
else if(w==32){
memcpy(pal,bz,32);v=16;
}
else{
if(w>63){
outbmp8(fn,k,v);k++;
}
}
}
}
}
}
int decode(unsigned char *s,unsigned char *d,int *k)
{
unsigned char f,g;
int m,p,q,t,x,y;
f=s[3]&12;t=s[0]|(s[1]<<8)|s[2];p=12;
xwide=(s[8]|(s[9]<<8))<<1;ywide=s[10]|(s[11]<<8);
switch(f){
case 0: t+=p;
for(q=0;p<t;){
m=s[p++];
if(m&0x80){
for(m=256-m;m>0;m--,p++,q++)d[q]=s[p];
}
else{
for(g=s[p++];m>0;m--,q++)d[q]=g;
}
}
break;
case 8: t+=p;x=0;y=ywide;memset(d,0,xwide*ywide);
for(q=0;p<t;){
m=s[p++];
if(m&0x80){
for(m=256-m;m>0;m--,p++){
d[q]=s[p];q+=xwide;y--;
if(y==0){
y=ywide;q=x++;
}
}
}
else{
for(g=s[p++];m>0;m--){
d[q]=g;q+=xwide;y--;
if(y==0){
y=ywide;q=x++;
}
}
}
}
break;
default: memcpy(d,&s[p],t);p+=t;q=t;break;
}
if(*k == 16)xwide<<=1;
#ifdef SODINA
printf("P=%X Q=%X\n",p,q);fflush(stdout);
#endif
*k=t;return((p+3)&0xFFFFFC);
}
/* OUTPUT BMP FILE */
void outbmp8(char *fn,int no,int c)
{
int i,j,sx,xpad;
char fz[48],fs[4];
unsigned short k;
if(no>=0){
fs[0]=(no>>4)+'0';if(fs[0]>57)fs[0]+=7;
fs[1]=(no&15)+'0';if(fs[1]>57)fs[1]+=7;
fs[2]=0;
}
else fs[0]=0;
if(c==16){
for(i=xwide*ywide-1;i>=0;i--)bz[i]=((bz[i]&0xF0)>>4)|((bz[i]&0x0F)<<4);
}
strcpy(fz,current);strcat(fz,fn);strcat(fz,fs);strcat(fz,".BMP");
if((fo=fopen(fz,"wb"))==0){
printf("file create errror.[%s]\n",fz);exit(1);
}
sx=(c==256)?xwide:xwide/2;xpad=(4-(sx&3))&3;
fputc('B',fo);fputc('M',fo); /* ID */
writelong((long)c*4L+54L+(long)(sx+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+1]<<8)|pal[i*2];
fputc((k&0x7C00)>>7,fo); /* BLUE */
fputc((k&0x3E0)>>2,fo); /* GREEN */
fputc((k&0x1F)<<3,fo); /* RED */
fputc(0,fo);
}
for(i=ywide-1;i>=0;i--){
fwrite(&bz[i*sx],1,sx,fo);
for(k=0;k<xpad;k++)fputc(0,fo);
}
fclose(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);
}