home *** CD-ROM | disk | FTP | other *** search
/ Fun CD 26 / OTACD26.ISO / archive / game / tarms / tarms.lzh / TARMS.C next >
C/C++ Source or Header  |  1998-12-23  |  6KB  |  226 lines

  1. /******************************
  2.  * サウザンドアームズ BMP変換 *
  3.  * Programmed By Melody-Yoshi *
  4.  ******************************/
  5. #include<stdio.h>
  6. #include<string.h>
  7. #include<stdlib.h>
  8. #include<windows.h>
  9. #include<commdlg.h>
  10. /* GLOBAL */
  11. unsigned char pal[0x200],*mem,*bz,*tb;
  12. char current[128];
  13. int xwide,ywide;
  14. FILE *fp,*fo;
  15.  
  16. /* PROTO-TYPES */
  17. int decode(unsigned char *,unsigned char *,int *);
  18. void analyze(void);
  19. void outbmp8(char *,int,int),writelong(long),writeshort(short);
  20.  
  21. void main(int argc,char *argv[])
  22. {
  23.     int i;
  24.     char fn[96],fz[96];
  25.     WIN32_FIND_DATA fd;
  26.     HANDLE h;
  27.     BOOL f;
  28.     if(argc<2){
  29.         OPENFILENAME ofn;
  30.         char szFile[256];
  31.         szFile[0]=0;
  32.         memset(&ofn,0,sizeof(OPENFILENAME));
  33.         ofn.lStructSize=sizeof(OPENFILENAME);
  34.         ofn.hwndOwner=NULL;ofn.nFilterIndex=1;
  35.         ofn.lpstrFilter="サウザンドアームズの画像ファイル\0*.STM\0\0";
  36.         ofn.lpstrFile=szFile;ofn.nMaxFile=sizeof(szFile);
  37.         ofn.lpstrFileTitle=NULL;ofn.lpstrInitialDir=NULL;
  38.         ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST;
  39.         if(GetOpenFileName(&ofn)){
  40.             strcpy(fn,szFile);
  41.         }
  42.         else exit(1);
  43.     }
  44.     else strcpy(fn,argv[1]);
  45.     strcpy(current,argv[0]);
  46.     for(i=strlen(current)-1;i>=0;i--)if(current[i]==':' || current[i]=='\\')break;
  47.     current[i+1]=0;
  48.     mem=(unsigned char *)malloc(0x86000);
  49.     bz=(unsigned char *)malloc(0x4B000);
  50.     tb=(unsigned char *)malloc(0xF000);
  51.     if(mem==NULL || bz==NULL || tb==NULL){
  52.         printf("メモリが足りません。\n");exit(1);
  53.     }
  54.     for(i=0;fn[i];i++)if(fn[i]=='.')break;
  55.     fn[i]=0;strcpy(fz,fn);strcat(fz,".STM");
  56.     h=FindFirstFile(fz,&fd);f=TRUE;
  57.     for(;i>=0;i--)if(fn[i]==':' || fn[i]=='\\')break;
  58.     fn[i+1]=0;
  59.     while(h!=INVALID_HANDLE_VALUE && f){
  60.         if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0){
  61.             strcpy(fz,fn);strcat(fz,fd.cFileName);
  62.             printf("%s\n",fz);fflush(stdout);
  63.             if((fp=fopen(fz,"rb"))==0){
  64.                 printf("file open error![%s]\n",fz);exit(1);
  65.             }
  66.             analyze();fclose(fp);
  67.         }
  68.         f=FindNextFile(h,&fd);
  69.     }
  70.     free(tb);free(mem);free(bz);exit(0);
  71. }
  72. void analyze(void)
  73. {
  74.     int i,j,k,m,t,v,w;
  75.     char fn[16];
  76.     for(k=0;;k+=0x7E0){
  77.         fread(&tb[k],1,0x800,fp);memcpy(&tb[k],&tb[k+32],0x7E0);
  78.         if(tb[k+0x7DC]+tb[k+0x7DD]+tb[k+0x7DE]+tb[k+0x7DF]==0)break;
  79.     }
  80.     for(m=0,k+=0x7E0;m<k;m+=20)if(tb[m]==0)break;
  81.     m/=20;
  82.     for(i=0;i<m;i++){
  83.         j=i*20;
  84.         memcpy(fn,&tb[j],12);fn[12]=0;
  85.         for(k=0;k<12;k++)if(fn[k]=='.')break;
  86.         if(fn[k+1]=='T' && fn[k+2]=='E' && fn[k+3]=='X'){
  87.             fn[k]=0;
  88.             k=(tb[j+16]|(tb[j+17]<<8)|(tb[j+18]<<16)|(tb[j+19]<<24))<<11;
  89. #ifdef SODINA
  90.             printf("NAME=%s SEEK=%X ",fn,k);fflush(stdout);
  91. #endif
  92.             fseek(fp,(long)k,0);
  93.             k=t=tb[j+12]|(tb[j+13]<<8)|(tb[j+14]<<16);
  94. #ifdef SODINA
  95.             printf("READ=%X\n",k);fflush(stdout);
  96. #endif
  97.             for(j=0;k>0;k-=0x7E0,j+=0x7E0){
  98.                 fread(&mem[j],1,0x800,fp);memcpy(&mem[j],&mem[j+32],0x7E0);
  99.             }
  100.             for(j=4,k=0,v=256;j<t;){
  101.                 w=v;j+=decode(&mem[j],bz,&w);
  102.                 if(w==512){
  103.                     memcpy(pal,bz,512);v=256;
  104.                 }
  105.                 else if(w==32){
  106.                     memcpy(pal,bz,32);v=16;
  107.                 }
  108.                 else{
  109.                     if(w>63){
  110.                         outbmp8(fn,k,v);k++;
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.     }
  116. }
  117. int decode(unsigned char *s,unsigned char *d,int *k)
  118. {
  119.     unsigned char f,g;
  120.     int m,p,q,t,x,y;
  121.     f=s[3]&12;t=s[0]|(s[1]<<8)|s[2];p=12;
  122.     xwide=(s[8]|(s[9]<<8))<<1;ywide=s[10]|(s[11]<<8);
  123.     switch(f){
  124.         case 0: t+=p;
  125.             for(q=0;p<t;){
  126.                 m=s[p++];
  127.                 if(m&0x80){
  128.                     for(m=256-m;m>0;m--,p++,q++)d[q]=s[p];
  129.                 }
  130.                 else{
  131.                     for(g=s[p++];m>0;m--,q++)d[q]=g;
  132.                 }
  133.             }
  134.             break;
  135.         case 8: t+=p;x=0;y=ywide;memset(d,0,xwide*ywide);
  136.             for(q=0;p<t;){
  137.                 m=s[p++];
  138.                 if(m&0x80){
  139.                     for(m=256-m;m>0;m--,p++){
  140.                         d[q]=s[p];q+=xwide;y--;
  141.                         if(y==0){
  142.                             y=ywide;q=x++;
  143.                         }
  144.                     }
  145.                 }
  146.                 else{
  147.                     for(g=s[p++];m>0;m--){
  148.                         d[q]=g;q+=xwide;y--;
  149.                         if(y==0){
  150.                             y=ywide;q=x++;
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.             break;
  156.         default: memcpy(d,&s[p],t);p+=t;q=t;break;
  157.     }
  158.     if(*k == 16)xwide<<=1;
  159. #ifdef SODINA
  160.     printf("P=%X Q=%X\n",p,q);fflush(stdout);
  161. #endif
  162.     *k=t;return((p+3)&0xFFFFFC);
  163. }
  164. /* OUTPUT BMP FILE */
  165. void outbmp8(char *fn,int no,int c)
  166. {
  167.     int i,j,sx,xpad;
  168.     char fz[48],fs[4];
  169.     unsigned short k;
  170.     if(no>=0){
  171.         fs[0]=(no>>4)+'0';if(fs[0]>57)fs[0]+=7;
  172.         fs[1]=(no&15)+'0';if(fs[1]>57)fs[1]+=7;
  173.         fs[2]=0;
  174.     }
  175.     else fs[0]=0;
  176.     if(c==16){
  177.         for(i=xwide*ywide-1;i>=0;i--)bz[i]=((bz[i]&0xF0)>>4)|((bz[i]&0x0F)<<4);
  178.     }
  179.     strcpy(fz,current);strcat(fz,fn);strcat(fz,fs);strcat(fz,".BMP");
  180.     if((fo=fopen(fz,"wb"))==0){
  181.         printf("file create errror.[%s]\n",fz);exit(1);
  182.     }
  183.     sx=(c==256)?xwide:xwide/2;xpad=(4-(sx&3))&3;
  184.     fputc('B',fo);fputc('M',fo);    /* ID */
  185.     writelong((long)c*4L+54L+(long)(sx+xpad)*(long)ywide);  /* FILE SIZE */
  186.     writelong(0L);                  /* RESERVED */
  187.     writelong((long)c*4L+54L);          /* IMAGE OFFSET */
  188.     writelong(40L);                 /* INFO SIZE */
  189.     writelong((long)xwide);         /* dx */
  190.     writelong((long)ywide);         /* dy */
  191.     writeshort(1);                  /* PLAIN */
  192.     if(c==256)writeshort(8);        /* DEPTH */
  193.     else writeshort(4);
  194.     writelong(0L);                  /* COMPRESS TYPE */
  195.     writelong(0L);
  196.     writelong(0L);                  /* x per meter */
  197.     writelong(0L);                  /* y per meter */
  198.     writelong((long)c);                 /* COLOR */
  199.     writelong(0L);
  200.     for(i=0;i<c;i++){
  201.         k=(pal[i*2+1]<<8)|pal[i*2];
  202.         fputc((k&0x7C00)>>7,fo);        /* BLUE */
  203.         fputc((k&0x3E0)>>2,fo);         /* GREEN */
  204.         fputc((k&0x1F)<<3,fo);          /* RED */
  205.         fputc(0,fo);
  206.     }
  207.     for(i=ywide-1;i>=0;i--){
  208.         fwrite(&bz[i*sx],1,sx,fo);
  209.         for(k=0;k<xpad;k++)fputc(0,fo);
  210.     }
  211.     fclose(fo);
  212. }
  213. /* WRITE SHORT VALUE */
  214. void writeshort(short n)
  215. {
  216.     fputc(n&255,fo);fputc((n>>8)&255,fo);
  217. }
  218. /* WRITE LONG VALUE */
  219. void writelong(long n)
  220. {
  221.     fputc((int)(n&255),fo);n/=256L;
  222.     fputc((int)(n&255),fo);n/=256L;
  223.     fputc((int)(n&255),fo);n/=256L;
  224.     fputc((int)(n&255),fo);
  225. }
  226.