home *** CD-ROM | disk | FTP | other *** search
/ Fun CD 26 / OTACD26.ISO / archive / game / zutto / zutto.lzh / ZUTTO.C next >
C/C++ Source or Header  |  1998-11-15  |  5KB  |  194 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 *bz,*bf,*mem,pal[512];
  12. char current[128];
  13. int xwide,ywide;
  14. FILE *fp,*fo;
  15. /* PROTO-TYPES */
  16. int decode(unsigned char *,unsigned char *,int *);
  17. void analyze(char *),convps(unsigned char *,char *,int);
  18. void outbmp8(char *,int,int),writelong(long),writeshort(short);
  19.  
  20. void main(int argc,char *argv[])
  21. {
  22.     int i;
  23.     char fn[96],fz[96];
  24.     WIN32_FIND_DATA fd;
  25.     HANDLE h;
  26.     BOOL f;
  27.     if(argc<2){
  28.         OPENFILENAME ofn;
  29.         char szFile[256];
  30.         szFile[0]=0;
  31.         memset(&ofn,0,sizeof(OPENFILENAME));
  32.         ofn.lStructSize=sizeof(OPENFILENAME);
  33.         ofn.hwndOwner=NULL;ofn.nFilterIndex=1;
  34.         ofn.lpstrFilter="ずっといっしょの画像ファイル\0KAO*.DAT;FUKU.DAT;GRP.DAT;OPEN.DAT\0\0";
  35.         ofn.lpstrFile=szFile;ofn.nMaxFile=sizeof(szFile);
  36.         ofn.lpstrFileTitle=NULL;ofn.lpstrInitialDir=NULL;
  37.         ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST;
  38.         if(GetOpenFileName(&ofn)){
  39.             strcpy(fn,szFile);
  40.         }
  41.         else exit(1);
  42.     }
  43.     else strcpy(fn,argv[1]);
  44.     strcpy(current,argv[0]);
  45.     for(i=strlen(current)-1;i>=0;i--)if(current[i]==':' || current[i]=='\\')break;
  46.     current[i+1]=0;
  47.     mem=(unsigned char *)malloc(0x25800);
  48.     bz=(unsigned char *)malloc(0x4B000);
  49.     bf=(unsigned char *)malloc(0x4B000);
  50.     if(mem==NULL || bz==NULL || bf==NULL){
  51.         printf("メモリが足りません。\n");exit(1);
  52.     }
  53.     for(i=0;fn[i];i++)if(fn[i]=='.')break;
  54.     strcpy(&fn[i],".DAT");
  55.     h=FindFirstFile(fn,&fd);f=TRUE;
  56.     for(fn[i]=0;i>=0;i--)if(fn[i]==':' || fn[i]=='\\')break;
  57.     fn[i+1]=0;
  58.     while(h!=INVALID_HANDLE_VALUE && f){
  59.         if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0){
  60.             strcpy(fz,fn);strcat(fz,fd.cFileName);
  61.             printf("%s\n",fz);fflush(stdout);
  62.             if((fp=fopen(fz,"rb"))==0){
  63.                 printf("file open error![%s]\n",fz);exit(1);
  64.             }
  65.             strcpy(fz,fd.cFileName);
  66.             for(i=0;fz[i];i++){
  67.                 if(fz[i]=='.')break;
  68.                 if(fz[i]>='a' && fz[i]<='z')fz[i]-=32;
  69.             }
  70.             fz[i]=0;analyze(fz);fclose(fp);
  71.         }
  72.         f=FindNextFile(h,&fd);
  73.     }
  74.     FindClose(h);free(bf);free(bz);free(mem);exit(0);
  75. }
  76. void analyze(char *fn)
  77. {
  78.     int i,j,k,m;
  79.     if(fn[0]=='K' && fn[1]=='A' && fn[2]=='O')m=0x25800;
  80.     else if(fn[0]=='F' && fn[1]=='U' && fn[2]=='K')m=0x1E000;
  81.     else if(fn[0]=='G' && fn[1]=='R' && fn[2]=='P')m=0xA077;
  82.     else if(fn[0]=='O' && fn[1]=='P' && fn[2]=='E')m=0x18C68;
  83.     else return;
  84.     for(k=0;;){
  85.         if(fread(mem,1,m,fp)<m)break;
  86.         for(j=0;;){
  87.             i=decode(mem,bf,&j);if(i==0)break;
  88.             convps(bf,fn,k);k++;
  89.         }
  90.     }
  91. }
  92. int decode(unsigned char *s,unsigned char *d,int *pp)
  93. {
  94.     int i,p,q,r,t;
  95.     unsigned char b;
  96.     r=*pp;t=(s[r+1]|(s[r]<<8))+r;p=r+12;r+=2;
  97.     if(t==0)return(0);
  98.     for(q=0;p<t;){
  99.         b=s[p++];
  100.         for(i=0;i<10;i++)if(s[r+i]==b)break;
  101.         if(i==10)d[q++]=b;
  102.         else{
  103.             for(i=s[p++];i>0;i--,q++)d[q]=b;
  104.         }
  105.     }
  106.     /*printf("P=%X Q=%X ",p,q);fflush(stdout);*/
  107.     *pp=p;return(q);
  108. }
  109. void convps(unsigned char *s,char *fn,int n)
  110. {
  111.     int i,j;
  112.     j=s[4]&7;
  113.     if(j==2){
  114.         xwide=s[16]|(s[17]<<8);ywide=s[18]|(s[19]<<8);
  115.         memcpy(bz,&s[20],xwide*ywide*2);
  116.     }
  117.     else if(j==1 || j==0){
  118.         if(s[4]&8){
  119.             i=s[16]|(s[17]<<8);i*=s[18];i<<=1;memcpy(pal,&s[20],i);
  120.             i=(s[8]|(s[9]<<8)|(s[10]<<16))+8;
  121.         }
  122.         else i=8;
  123.         ywide=s[i+10]|(s[i+11]<<8);xwide=(s[i+8]|(s[i+9]<<8))<<1;
  124.         memcpy(bz,&s[i+12],xwide*ywide);
  125.         if(j==1)outbmp8(fn,n,256);
  126.         else{
  127.             xwide<<=1;outbmp8(fn,n,16);
  128.         }
  129.     }
  130. }
  131. /* OUTPUT BMP FILE */
  132. void outbmp8(char *fn,int no,int c)
  133. {
  134.     int i,j,sx,xpad;
  135.     char fz[48],fs[4];
  136.     unsigned short k;
  137.     if(no>=0){
  138.         fs[0]=(no>>8)+'0';if(fs[0]>57)fs[0]+=7;
  139.         fs[1]=((no>>4)&15)+'0';if(fs[1]>57)fs[1]+=7;
  140.         fs[2]=(no&15)+'0';if(fs[2]>57)fs[2]+=7;
  141.         fs[3]=0;
  142.     }
  143.     else fs[0]=0;
  144.     if(c==16){
  145.         for(i=xwide*ywide-1;i>=0;i--)bz[i]=((bz[i]&0xF0)>>4)|((bz[i]&0x0F)<<4);
  146.     }
  147.     strcpy(fz,current);strcat(fz,fn);strcat(fz,fs);strcat(fz,".BMP");
  148.     if((fo=fopen(fz,"wb"))==0){
  149.         printf("file create errror.[%s]\n",fz);exit(1);
  150.     }
  151.     sx=(c==256)?xwide:xwide/2;xpad=(4-(sx&3))&3;
  152.     fputc('B',fo);fputc('M',fo);    /* ID */
  153.     writelong((long)c*4L+54L+(long)(sx+xpad)*(long)ywide);    /* FILE SIZE */
  154.     writelong(0L);                    /* RESERVED */
  155.     writelong((long)c*4L+54L);        /* IMAGE OFFSET */
  156.     writelong(40L);                    /* INFO SIZE */
  157.     writelong((long)xwide);            /* dx */
  158.     writelong((long)ywide);            /* dy */
  159.     writeshort(1);                    /* PLAIN */
  160.     if(c==256)writeshort(8);        /* DEPTH */
  161.     else writeshort(4);    
  162.     writelong(0L);                    /* COMPRESS TYPE */
  163.     writelong(0L);
  164.     writelong(0L);                    /* x per meter */
  165.     writelong(0L);                    /* y per meter */
  166.     writelong((long)c);                /* COLOR */
  167.     writelong(0L);
  168.     for(i=0;i<c;i++){
  169.         k=(pal[i*2+1]<<8)|pal[i*2];
  170.         fputc((k&0x7c00)>>7,fo);        /* BLUE */
  171.         fputc((k&0x3e0)>>2,fo);            /* GREEN */
  172.         fputc((k&0x1f)<<3,fo);            /* RED */
  173.         fputc(0,fo);
  174.     }
  175.     for(i=ywide-1;i>=0;i--){
  176.         fwrite(&bz[i*sx],1,sx,fo);
  177.         for(k=0;k<xpad;k++)fputc(0,fo);
  178.     }
  179.     fclose(fo);
  180. }
  181. /* WRITE SHORT VALUE */
  182. void writeshort(short n)
  183. {
  184.     fputc(n&255,fo);fputc((n>>8)&255,fo);
  185. }
  186. /* WRITE LONG VALUE */
  187. void writelong(long n)
  188. {
  189.     fputc((int)(n&255),fo);n/=256L;
  190.     fputc((int)(n&255),fo);n/=256L;
  191.     fputc((int)(n&255),fo);n/=256L;
  192.     fputc((int)(n&255),fo);
  193. }
  194.