home *** CD-ROM | disk | FTP | other *** search
/ Fun CD 26 / OTACD26.ISO / archive / game / dcast / dcast.lzh / DCAST.C next >
C/C++ Source or Header  |  1998-07-05  |  8KB  |  331 lines

  1. /******************************
  2.  * DOUBLE CAST  BMP CONVERTER *
  3.  * Programmed By Melody-Yoshi *
  4.  ******************************/
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8. #include<windows.h>
  9. #include<commdlg.h>
  10. /* GLOBAL */
  11. unsigned char *mem,*bz,pal[512];
  12. char current[128];
  13. int xwide,ywide,bitl;
  14. FILE *fp,*fo;
  15. unsigned char bitcode;
  16. /* PROTO-TYPES */
  17. int decode(unsigned char *,unsigned char *),analyze(char *);
  18. void outbmp(char *,int),outbmp8(char *,int,int);
  19. void writeshort(short),writelong(long);
  20. int loadbit(int,unsigned char*,int *),load1bit(unsigned char *,int *);
  21.  
  22. void main(int argc,char *argv[])
  23. {
  24.     int i,fsize;
  25.     char fn[64],fz[64];
  26.     WIN32_FIND_DATA fd;
  27.     HANDLE h;
  28.     BOOL f;
  29.     if(argc<2){
  30.         OPENFILENAME ofn;
  31.         char szFile[256];
  32.         szFile[0]=0;
  33.         memset(&ofn,0,sizeof(OPENFILENAME));
  34.         ofn.lStructSize=sizeof(OPENFILENAME);
  35.         ofn.hwndOwner=NULL;ofn.nFilterIndex=1;
  36.         ofn.lpstrFilter="ダブルキャストの画像ファイル\0*.GPX;*.GPL\0\0";
  37.         ofn.lpstrFile=szFile;ofn.nMaxFile=sizeof(szFile);
  38.         ofn.lpstrFileTitle=NULL;ofn.lpstrInitialDir=NULL;
  39.         ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST;
  40.         if(GetOpenFileName(&ofn)){
  41.             strcpy(fn,szFile);
  42.         }
  43.         else exit(1);
  44.     }
  45.     else strcpy(fn,argv[1]);
  46.     strcpy(current,argv[0]);
  47.     for(i=strlen(current)-1;i>=0;i--)if(current[i]==':' || current[i]=='\\')break;
  48.     current[i+1]=0;
  49.     bz=(unsigned char *)malloc(0xE1000);
  50.     mem=(unsigned char *)malloc(0x9F000);
  51.     if(mem==NULL || bz==NULL){
  52.         printf("メモリが足りません。\n");exit(1);
  53.     }
  54.     for(i=0;fn[i];i++){
  55.         if(fn[i]>='a' && fn[i]<='z')fn[i]-=32;
  56.     }
  57.     for(fn[i]=0;i>=0;i--)if(fn[i]==':' || fn[i]=='\\')break;
  58.     h=FindFirstFile(fn,&fd);f=TRUE;
  59.     fn[i+1]=0;
  60.     while(h!=INVALID_HANDLE_VALUE && f){
  61.         if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0){
  62.             strcpy(fz,fn);strcat(fz,fd.cFileName);
  63.             printf("%s\n",fz);fflush(stdout);
  64.             if((fp=fopen(fz,"rb"))==0){
  65.                 printf("file open error![%s]\n",fz);exit(1);
  66.             }
  67.             fseek(fp,0L,2);fsize=(int)ftell(fp);fseek(fp,0L,0);
  68.             fread(mem,1,fsize,fp);fclose(fp);
  69.             strcpy(fz,fd.cFileName);
  70.             for(i=0;fz[i];i++)if(fz[i]=='.')break;
  71.             fz[i]=0;
  72.             analyze(fz);
  73.         }
  74.         f=FindNextFile(h,&fd);
  75.     }
  76.     FindClose(h);free(mem);free(bz);exit(0);
  77. }
  78. int analyze(char *fn)
  79. {
  80.     int i,j,k,m;
  81.     m=1;pal[1]=0xAB;
  82.     if(mem[0]==0x83 && mem[1]==0xA1){
  83.         i=decode(bz,mem);
  84.         if(i>0){
  85.             m=0;
  86.             if(pal[1]==0xAB)outbmp(fn,-1);
  87.             else outbmp8(fn,-1,256);
  88.         }
  89.     }
  90.     else if(mem[0]==0 && mem[1]==0 && mem[3]==0){
  91.         j=mem[2];
  92.         for(k=0;k<j;k++){
  93.             i=mem[k*4+4]|(mem[k*4+5]<<8)|(mem[k*4+6]<<16);
  94.             i=decode(bz,&mem[i]);
  95.             if(i>0){
  96.                 m=0;
  97.                 if(pal[1]==0xAB)outbmp(fn,k);
  98.                 else outbmp8(fn,k,256);
  99.             }
  100.         }
  101.     }
  102.     return(m);
  103. }
  104. int decode(unsigned char *c,unsigned char *b)
  105. {
  106.     int i,j,k,m,p,q,r,w,x;
  107.     unsigned short s[256];
  108.     if(b[16]!=1 && b[16]!=2)return(-1);
  109.     for(i=0;i<256;i++)s[i]=i;
  110.     xwide=(b[29]<<8)|b[28];ywide=(b[31]<<8)|b[30];p=32;
  111.     if(b[17]){
  112.         i=b[p]|(b[p+1]<<8);j=b[p+2];k=i*j*2;
  113.         memcpy(pal,&b[p+4],k);p=(p+k+7)&0xFFFFFFFC;
  114.     }
  115.     switch(b[16]&15){
  116.         case 1:w=xwide*ywide;break;
  117.         case 2:w=xwide*ywide*2;pal[1]=0xAB;break;
  118.     }
  119.     memset(c,0,w);
  120.     for(bitl=q=r=0;q<w;){
  121.         if(load1bit(b,&p)){
  122.             if(load1bit(b,&p)){
  123.                 if(load1bit(b,&p)){
  124.                     j=loadbit(4,b,&p)+1;
  125.                     i=0;while(load1bit(b,&p))i++;
  126.                     m=(loadbit(i,b,&p)+(1<<i));
  127.                     switch(b[16]&15){
  128.                         case 0:break;
  129.                         case 1:
  130.                                 for(i=0;i<m;i++,q++)c[q]=c[q-j];
  131.                                 break;
  132.                         case 2:
  133.                                 for(i=0,j<<=1;i<m;i++,q+=2){
  134.                                     c[q]=c[q-j];c[q+1]=c[q-j+1];
  135.                                 }
  136.                                 break;
  137.                     }
  138.                 }
  139.                 else{
  140.                     j=loadbit(5,b,&p);
  141.                     i=0;while(load1bit(b,&p))i++;
  142.                     m=(loadbit(i,b,&p)+(1<<i));
  143.                     switch(b[16]&15){
  144.                         case 0:break;
  145.                         case 1:
  146.                                 x=q%xwide;i=q-x;
  147.                                 if(x<16)i=i-xwide+j;
  148.                                 else if(x>xwide-16)i=i+j-32;
  149.                                 else i=q-xwide+j-16;
  150.                                 for(j=0;j<m;j++,q++,i++)c[q]=c[i];
  151.                                 break;
  152.                         case 2:
  153.                                 i=q/2;x=i%xwide;i=i-x;
  154.                                 if(x<16)i=i-xwide+j;
  155.                                 else if(x>xwide-16)i=i+j-32;
  156.                                 else i=q/2-xwide+j-16;
  157.                                 for(j=0,i<<=1;j<m;j++,q+=2,i+=2){
  158.                                     c[q]=c[i];c[q+1]=c[i+1];
  159.                                 }
  160.                                 break;
  161.                     }
  162.                 }
  163.             }
  164.             else{
  165.                 j=loadbit(4,b,&p)+1;k=loadbit(5,b,&p);
  166.                 i=0;while(load1bit(b,&p))i++;
  167.                 m=(loadbit(i,b,&p)+(1<<i));
  168.                 switch(b[16]&15){
  169.                     case 0:break;
  170.                     case 1:
  171.                             x=q%xwide;i=q-x;
  172.                             if(x<16)i=i-xwide+k;
  173.                             else if(x>xwide-16)i=i+k-32;
  174.                             else i=q-xwide+k-16;
  175.                             i-=xwide*j;
  176.                             for(j=0;j<m;j++,q++,i++)c[q]=c[i];
  177.                             break;
  178.                     case 2:
  179.                             i=q/2;x=i%xwide;i=i-x;
  180.                             if(x<16)i=i-xwide+k;
  181.                             else if(x>xwide-16)i=i+k-32;
  182.                             else i=q/2-xwide+k-16;
  183.                             i-=xwide*j;
  184.                             for(j=0,i<<=1;j<m;j++,q+=2,i+=2){
  185.                                 c[q]=c[i];c[q+1]=c[i+1];
  186.                             }
  187.                             break;
  188.                 }
  189.             }
  190.         }
  191.         else{
  192.             switch(b[16]&15){
  193.                 case 0: break;
  194.                 case 1: c[q++]=b[p++];break;
  195.                 case 2:
  196.                     k=b[p++];
  197.                     if(k&0x80){
  198.                         k=s[((((k&0x7F)<<1)|load1bit(b,&p))+r)&255];
  199.                         c[q]=k/256;c[q+1]=k&255;q+=2;
  200.                     }
  201.                     else{
  202.                         k=(k<<8)|b[p++];c[q]=k/256;c[q+1]=k&255;q+=2;
  203.                         r=(r-1)&255;s[r]=k;
  204.                     }
  205.                     break;
  206.             }
  207.         }
  208.     }
  209.     return(q);
  210. }
  211. /* OUTPUT BMP FILE */
  212. void outbmp8(char *fn,int no,int c)
  213. {
  214.     int i,j,sx,xpad;
  215.     char fz[48],fs[4];
  216.     unsigned short k;
  217.     if(no>=0){
  218.         fs[0]=(no>>4)+'0';if(fs[0]>57)fs[0]+=7;
  219.         fs[1]=(no&15)+'0';if(fs[1]>57)fs[1]+=7;
  220.         fs[2]=0;
  221.     }
  222.     else fs[0]=0;
  223.     if(c==16){
  224.         for(i=xwide*ywide-1;i>=0;i--)bz[i]=((bz[i]&0xF0)>>4)|((bz[i]&0x0F)<<4);
  225.     }
  226.     strcpy(fz,current);strcat(fz,fn);strcat(fz,fs);strcat(fz,".BMP");
  227.     if((fo=fopen(fz,"wb"))==0){
  228.         printf("file create errror.[%s]\n",fz);exit(1);
  229.     }
  230.     sx=(c==256)?xwide:xwide/2;xpad=(4-(sx&3))&3;
  231.     fputc('B',fo);fputc('M',fo);    /* ID */
  232.     writelong((long)c*4L+54L+(long)(sx+xpad)*(long)ywide);    /* FILE SIZE */
  233.     writelong(0L);                    /* RESERVED */
  234.     writelong((long)c*4L+54L);        /* IMAGE OFFSET */
  235.     writelong(40L);                    /* INFO SIZE */
  236.     writelong((long)xwide);            /* dx */
  237.     writelong((long)ywide);            /* dy */
  238.     writeshort(1);                    /* PLAIN */
  239.     if(c==256)writeshort(8);        /* DEPTH */
  240.     else writeshort(4);    
  241.     writelong(0L);                    /* COMPRESS TYPE */
  242.     writelong(0L);
  243.     writelong(0L);                    /* x per meter */
  244.     writelong(0L);                    /* y per meter */
  245.     writelong((long)c);                /* COLOR */
  246.     writelong(0L);
  247.     for(i=0;i<c;i++){
  248.         k=(pal[i*2+1]<<8)|pal[i*2];
  249.         fputc((k&0x7c00)>>7,fo);        /* BLUE */
  250.         fputc((k&0x3e0)>>2,fo);            /* GREEN */
  251.         fputc((k&0x1f)<<3,fo);            /* RED */
  252.         fputc(0,fo);
  253.     }
  254.     for(i=ywide-1;i>=0;i--){
  255.         fwrite(&bz[i*sx],1,sx,fo);
  256.         for(k=0;k<xpad;k++)fputc(0,fo);
  257.     }
  258.     fclose(fo);
  259. }
  260. void outbmp(char *fn,int no)
  261. {
  262.     int x,y,xpad;
  263.     char fz[64],fs[4];
  264.     if(no>=0){
  265.         fs[0]=no/16+'0';if(fs[0]>57)fs[0]+=7;
  266.         fs[1]=(no&15)+'0';if(fs[1]>57)fs[1]+=7;
  267.         fs[2]=0;
  268.     }
  269.     else fs[0]=0;
  270.     strcpy(fz,current);strcat(fz,fn);strcat(fz,fs);strcat(fz,".BMP");
  271.     for(y=xwide*ywide*2-2,x=xwide*ywide*3-3;y>=0;y-=2,x-=3){
  272.         xpad=(bz[y]<<8)|bz[y+1];
  273.         bz[x+2]=(xpad&0x1F)<<3;
  274.         bz[x+1]=(xpad&0x3E0)>>2;
  275.         bz[x]=(xpad&0x7C00)>>7;
  276.     }
  277.     if((fo=fopen(fz,"wb"))==0){
  278.         printf("file create error![%s]\n",fz);exit(1);
  279.     }
  280.     xpad=(4-((xwide*3)&3))&3;
  281.     fputc('B',fo);fputc('M',fo);    /* ID */
  282.     writelong(54L+(long)xwide*(long)ywide*3L+(long)xpad*(long)ywide);   /* FILE
  283. SIZE */
  284.     writelong(0L);                    /* RESERVED */
  285.     writelong(54L);                    /* IMAGE OFFSET */
  286.     writelong(40L);                    /* INFO SIZE */
  287.     writelong((long)xwide);            /* dx */
  288.     writelong((long)ywide);            /* dy */
  289.     writeshort(1);                    /* PLAIN */
  290.     writeshort(24);                    /* DEPTH */
  291.     writelong(0L);                    /* COMPRESS TYPE */
  292.     writelong(0L);
  293.     writelong(0L);                    /* x per meter */
  294.     writelong(0L);                    /* y per meter */
  295.     writelong(0L);
  296.     writelong(0L);
  297.     for(y=ywide-1;y>=0;y--){
  298.         fwrite(&bz[y*xwide*3],1,xwide*3,fo);
  299.         for(x=0;x<xpad;x++)fputc(0,fo);
  300.     }
  301.     fclose(fo);
  302. }
  303. /* WRITE SHORT VALUE */
  304. void writeshort(short n)
  305. {
  306.     fputc(n&255,fo);fputc((n>>8)&255,fo);
  307. }
  308. /* WRITE LONG VALUE */
  309. void writelong(long n)
  310. {
  311.     fputc((int)(n&255),fo);n/=256L;
  312.     fputc((int)(n&255),fo);n/=256L;
  313.     fputc((int)(n&255),fo);n/=256L;
  314.     fputc((int)(n&255),fo);
  315. }
  316. /* READ n BIT */
  317. int loadbit(int sz,unsigned char *m,int *p)
  318. {
  319.     int i,j;
  320.     for(i=j=0;i<sz;i++)j=(j<<1)|load1bit(m,p);
  321.     return(j);
  322. }
  323. /* READ 1 BIT */
  324. int load1bit(unsigned char *m,int *p)
  325. {
  326.     if(bitl==0){
  327.         bitl=8;bitcode=m[(*p)++];
  328.     }
  329.     bitl--;return((bitcode>>bitl)&1);
  330. }
  331.