home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fun CD 26
/
OTACD26.ISO
/
archive
/
game
/
dokip
/
dokip.lzh
/
DOKIP.C
next >
Wrap
C/C++ Source or Header
|
1997-05-15
|
4KB
|
154 lines
/******************************
* DOKIDOKI PRETTY LEAGUE *
* BMP Converter *
* Programmed By Melody-Yoshi *
******************************/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
unsigned char *bz,*mem,pal[512];
char fn[64],fz[64];
FILE *fp,*fo;
int xwide,ywide,xseek,xpad;
void conv08(void);
void writelong(long),writeshort(short);
int runlength(int,int);
void writebmphead08(short);
long getlong(unsigned char *,int);
short getshort(unsigned char *,int);
void main(int argc,char *argv[])
{
int i,j,k,sz,cnt;
if(argc<2){
printf("dokip [file]\n");exit(1);
}
strcpy(fn,argv[1]);
for(i=0;fn[i];i++)if(fn[i]=='.')break;
fn[i]=0;strcpy(fz,fn);strcat(fz,".pac");
if((fp=fopen(fz,"rb"))==0){
printf("file open error![%s]\n",fz);exit(1);
}
strcpy(fz,fn);
for(fz[i]=0;i>=0;i--)if(fz[i]==':' || fz[i]=='\\')break;
strcpy(fn,&fz[i+1]);
fseek(fp,0L,2);sz=(int)ftell(fp);fseek(fp,0L,0);
mem=(unsigned char *)malloc(sz);
bz=(unsigned char *)malloc(0x20000);
if(mem==NULL || bz==NULL){
printf("insufficient memory.\n");exit(1);
}
fread(mem,1,sz,fp);fclose(fp);
for(cnt=j=0;;cnt++,j+=24)if(cnt!=getlong(mem,j))break;
if(cnt==0){
printf("This file cannot convert.\n");exit(1);
}
for(i=j=0;i<cnt;i++,j+=24){
k=runlength((int)getlong(mem,j+16),(int)getlong(mem,j+20));
if(bz[0]!=0x10 || bz[1] || bz[2] || bz[3] || bz[4]!=9)continue;
strcpy(fz,fn);
if(i){
pal[0]=i/16;pal[1]=i&15;pal[2]=0;
pal[0]=(pal[0]<10)?pal[0]+'0':pal[0]+55;
pal[1]=(pal[1]<10)?pal[1]+'0':pal[1]+55;
strcat(fz,(char *)pal);
}
strcat(fz,".BMP");
if((fo=fopen(fz,"wb"))==0){
printf("file open error![%s]\n",fz);exit(1);
}
conv08();fclose(fo);
}
free(bz);free(mem);exit(0);
}
int runlength(int p,int size)
{
int i,j,q;
size+=p;
for(q=0;p<size;){
j=mem[p+1];p+=2;
if(mem[p-2]==0){
i=mem[p++];for(;j>0;j--,q++)bz[q]=i;
}
else{
for(;j>0;j--,p++,q++)bz[q]=mem[p];
}
}
return(q);
}
void conv08(void)
{
int i,j,p;
i=getshort(bz,16)<<1;j=getshort(bz,18);i*=j;
for(j=0;j<i;j++)pal[j]=bz[j+20];
p=(int)getlong(bz,8)+16;
xwide=getshort(bz,p)<<1;ywide=getshort(bz,p+2);p+=4;
xseek=xwide;xpad=(4-(xseek&3))&3;xseek+=xpad;
writebmphead08(256);
fseek(fo,1078L+(long)(ywide-1)*(long)xseek,0);
for(i=0;i<ywide;i++){
fwrite(&bz[p],1,xwide,fo);p+=xwide;
for(j=0;j<xpad;j++)fputc(0,fo);
fseek(fo,(long)(-xseek*2),1);
}
}
/* WRITE BMP HEADER */
void writebmphead08(short c)
{
short i;
unsigned short k;
fputc('B',fo);fputc('M',fo); /* ID */
writelong((long)c*4L+54L+(long)(xwide+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]+(pal[i*2+1]<<8);
fputc((k&0x7c00)>>7,fo); /* BLUE */
fputc((k&0x3e0)>>2,fo); /* GREEN */
fputc((k&0x1f)<<3,fo); /* RED */
fputc(0,fo);
}
k=xwide+xpad;
for(i=0;i<k;i++)bz[i]=0;
for(i=0;i<ywide;i++){
if(fwrite(bz,1,k,fo)!=k){
printf("file write error!\n");exit(1);
}
}
}
/* 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);
}
/* READ LONG VALUE */
long getlong(unsigned char *m,int p)
{
return(m[p]|(m[p+1]*256)|(m[p+2]*65536));
}
/* READ SHORT VALUE */
short getshort(unsigned char *m,int p)
{
return(m[p]|(m[p+1]*256));
}