home *** CD-ROM | disk | FTP | other *** search
/ Fun CD 26 / OTACD26.ISO / archive / game / dokip / dokip.lzh / DOKIP.C next >
C/C++ Source or Header  |  1997-05-15  |  4KB  |  154 lines

  1. /******************************
  2.  *   DOKIDOKI PRETTY LEAGUE   *
  3.  *       BMP Converter        *
  4.  * Programmed By Melody-Yoshi *
  5.  ******************************/
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include<stdlib.h>
  9. unsigned char *bz,*mem,pal[512];
  10. char fn[64],fz[64];
  11. FILE *fp,*fo;
  12. int xwide,ywide,xseek,xpad;
  13. void conv08(void);
  14. void writelong(long),writeshort(short);
  15. int runlength(int,int);
  16. void writebmphead08(short);
  17. long getlong(unsigned char *,int);
  18. short getshort(unsigned char *,int);
  19.  
  20. void main(int argc,char *argv[])
  21. {
  22.     int i,j,k,sz,cnt;
  23.     if(argc<2){
  24.         printf("dokip [file]\n");exit(1);
  25.     }
  26.     strcpy(fn,argv[1]);
  27.     for(i=0;fn[i];i++)if(fn[i]=='.')break;
  28.     fn[i]=0;strcpy(fz,fn);strcat(fz,".pac");
  29.     if((fp=fopen(fz,"rb"))==0){
  30.         printf("file open error![%s]\n",fz);exit(1);
  31.     }
  32.     strcpy(fz,fn);
  33.     for(fz[i]=0;i>=0;i--)if(fz[i]==':' || fz[i]=='\\')break;
  34.     strcpy(fn,&fz[i+1]);
  35.     fseek(fp,0L,2);sz=(int)ftell(fp);fseek(fp,0L,0);
  36.     mem=(unsigned char *)malloc(sz);
  37.     bz=(unsigned char *)malloc(0x20000);
  38.     if(mem==NULL || bz==NULL){
  39.         printf("insufficient memory.\n");exit(1);
  40.     }
  41.     fread(mem,1,sz,fp);fclose(fp);
  42.     for(cnt=j=0;;cnt++,j+=24)if(cnt!=getlong(mem,j))break;
  43.     if(cnt==0){
  44.         printf("This file cannot convert.\n");exit(1);
  45.     }
  46.     for(i=j=0;i<cnt;i++,j+=24){
  47.         k=runlength((int)getlong(mem,j+16),(int)getlong(mem,j+20));
  48.         if(bz[0]!=0x10 || bz[1] || bz[2] || bz[3] || bz[4]!=9)continue;
  49.         strcpy(fz,fn);
  50.         if(i){
  51.             pal[0]=i/16;pal[1]=i&15;pal[2]=0;
  52.             pal[0]=(pal[0]<10)?pal[0]+'0':pal[0]+55;
  53.             pal[1]=(pal[1]<10)?pal[1]+'0':pal[1]+55;
  54.             strcat(fz,(char *)pal);
  55.         }
  56.         strcat(fz,".BMP");
  57.         if((fo=fopen(fz,"wb"))==0){
  58.             printf("file open error![%s]\n",fz);exit(1);
  59.         }
  60.         conv08();fclose(fo);
  61.     }
  62.     free(bz);free(mem);exit(0);
  63. }
  64. int runlength(int p,int size)
  65. {
  66.     int i,j,q;
  67.     size+=p;
  68.     for(q=0;p<size;){
  69.         j=mem[p+1];p+=2;
  70.         if(mem[p-2]==0){
  71.             i=mem[p++];for(;j>0;j--,q++)bz[q]=i;
  72.         }
  73.         else{
  74.             for(;j>0;j--,p++,q++)bz[q]=mem[p];
  75.         }
  76.     }
  77.     return(q);
  78. }
  79. void conv08(void)
  80. {
  81.     int i,j,p;
  82.     i=getshort(bz,16)<<1;j=getshort(bz,18);i*=j;
  83.     for(j=0;j<i;j++)pal[j]=bz[j+20];
  84.     p=(int)getlong(bz,8)+16;
  85.     xwide=getshort(bz,p)<<1;ywide=getshort(bz,p+2);p+=4;
  86.     xseek=xwide;xpad=(4-(xseek&3))&3;xseek+=xpad;
  87.     writebmphead08(256);
  88.     fseek(fo,1078L+(long)(ywide-1)*(long)xseek,0);
  89.     for(i=0;i<ywide;i++){
  90.         fwrite(&bz[p],1,xwide,fo);p+=xwide;
  91.         for(j=0;j<xpad;j++)fputc(0,fo);
  92.         fseek(fo,(long)(-xseek*2),1);
  93.     }
  94. }
  95. /* WRITE BMP HEADER */
  96. void writebmphead08(short c)
  97. {
  98.     short i;
  99.     unsigned short k;
  100.     fputc('B',fo);fputc('M',fo);    /* ID */
  101.     writelong((long)c*4L+54L+(long)(xwide+xpad)*(long)ywide);    /* FILE SIZE */
  102.     writelong(0L);                    /* RESERVED */
  103.     writelong((long)c*4L+54L);        /* IMAGE OFFSET */
  104.     writelong(40L);                    /* INFO SIZE */
  105.     writelong((long)xwide);            /* dx */
  106.     writelong((long)ywide);            /* dy */
  107.     writeshort(1);                    /* PLAIN */
  108.     if(c==256)writeshort(8);        /* DEPTH */
  109.     else writeshort(4);    
  110.     writelong(0L);                    /* COMPRESS TYPE */
  111.     writelong(0L);
  112.     writelong(0L);                    /* x per meter */
  113.     writelong(0L);                    /* y per meter */
  114.     writelong((long)c);                /* COLOR */
  115.     writelong(0L);
  116.     for(i=0;i<c;i++){
  117.         k=pal[i*2]+(pal[i*2+1]<<8);
  118.         fputc((k&0x7c00)>>7,fo);        /* BLUE */
  119.         fputc((k&0x3e0)>>2,fo);            /* GREEN */
  120.         fputc((k&0x1f)<<3,fo);            /* RED */
  121.         fputc(0,fo);
  122.     }
  123.     k=xwide+xpad;
  124.     for(i=0;i<k;i++)bz[i]=0;
  125.     for(i=0;i<ywide;i++){
  126.         if(fwrite(bz,1,k,fo)!=k){
  127.             printf("file write error!\n");exit(1);
  128.         }
  129.     }
  130. }
  131. /* WRITE SHORT VALUE */
  132. void writeshort(short n)
  133. {
  134.     fputc(n&255,fo);fputc((n>>8)&255,fo);
  135. }
  136. /* WRITE LONG VALUE */
  137. void writelong(long n)
  138. {
  139.     fputc((int)(n&255),fo);n/=256L;
  140.     fputc((int)(n&255),fo);n/=256L;
  141.     fputc((int)(n&255),fo);n/=256L;
  142.     fputc((int)(n&255),fo);
  143. }
  144. /* READ LONG VALUE */
  145. long getlong(unsigned char *m,int p)
  146. {
  147.     return(m[p]|(m[p+1]*256)|(m[p+2]*65536));
  148. }
  149. /* READ SHORT VALUE */
  150. short getshort(unsigned char *m,int p)
  151. {
  152.     return(m[p]|(m[p+1]*256));
  153. }
  154.