home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fun CD 26
/
OTACD26.ISO
/
archive
/
game
/
zutto
/
zutto.lzh
/
ZUTTO.C
next >
Wrap
C/C++ Source or Header
|
1998-11-15
|
5KB
|
194 lines
/******************************
* ずっといっしょ BMP変換 *
* Programmed By Melody-Yoshi *
******************************/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#include<commdlg.h>
/* GLOBAL */
unsigned char *bz,*bf,*mem,pal[512];
char current[128];
int xwide,ywide;
FILE *fp,*fo;
/* PROTO-TYPES */
int decode(unsigned char *,unsigned char *,int *);
void analyze(char *),convps(unsigned char *,char *,int);
void outbmp8(char *,int,int),writelong(long),writeshort(short);
void main(int argc,char *argv[])
{
int i;
char fn[96],fz[96];
WIN32_FIND_DATA fd;
HANDLE h;
BOOL f;
if(argc<2){
OPENFILENAME ofn;
char szFile[256];
szFile[0]=0;
memset(&ofn,0,sizeof(OPENFILENAME));
ofn.lStructSize=sizeof(OPENFILENAME);
ofn.hwndOwner=NULL;ofn.nFilterIndex=1;
ofn.lpstrFilter="ずっといっしょの画像ファイル\0KAO*.DAT;FUKU.DAT;GRP.DAT;OPEN.DAT\0\0";
ofn.lpstrFile=szFile;ofn.nMaxFile=sizeof(szFile);
ofn.lpstrFileTitle=NULL;ofn.lpstrInitialDir=NULL;
ofn.Flags=OFN_HIDEREADONLY|OFN_PATHMUSTEXIST;
if(GetOpenFileName(&ofn)){
strcpy(fn,szFile);
}
else exit(1);
}
else strcpy(fn,argv[1]);
strcpy(current,argv[0]);
for(i=strlen(current)-1;i>=0;i--)if(current[i]==':' || current[i]=='\\')break;
current[i+1]=0;
mem=(unsigned char *)malloc(0x25800);
bz=(unsigned char *)malloc(0x4B000);
bf=(unsigned char *)malloc(0x4B000);
if(mem==NULL || bz==NULL || bf==NULL){
printf("メモリが足りません。\n");exit(1);
}
for(i=0;fn[i];i++)if(fn[i]=='.')break;
strcpy(&fn[i],".DAT");
h=FindFirstFile(fn,&fd);f=TRUE;
for(fn[i]=0;i>=0;i--)if(fn[i]==':' || fn[i]=='\\')break;
fn[i+1]=0;
while(h!=INVALID_HANDLE_VALUE && f){
if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0){
strcpy(fz,fn);strcat(fz,fd.cFileName);
printf("%s\n",fz);fflush(stdout);
if((fp=fopen(fz,"rb"))==0){
printf("file open error![%s]\n",fz);exit(1);
}
strcpy(fz,fd.cFileName);
for(i=0;fz[i];i++){
if(fz[i]=='.')break;
if(fz[i]>='a' && fz[i]<='z')fz[i]-=32;
}
fz[i]=0;analyze(fz);fclose(fp);
}
f=FindNextFile(h,&fd);
}
FindClose(h);free(bf);free(bz);free(mem);exit(0);
}
void analyze(char *fn)
{
int i,j,k,m;
if(fn[0]=='K' && fn[1]=='A' && fn[2]=='O')m=0x25800;
else if(fn[0]=='F' && fn[1]=='U' && fn[2]=='K')m=0x1E000;
else if(fn[0]=='G' && fn[1]=='R' && fn[2]=='P')m=0xA077;
else if(fn[0]=='O' && fn[1]=='P' && fn[2]=='E')m=0x18C68;
else return;
for(k=0;;){
if(fread(mem,1,m,fp)<m)break;
for(j=0;;){
i=decode(mem,bf,&j);if(i==0)break;
convps(bf,fn,k);k++;
}
}
}
int decode(unsigned char *s,unsigned char *d,int *pp)
{
int i,p,q,r,t;
unsigned char b;
r=*pp;t=(s[r+1]|(s[r]<<8))+r;p=r+12;r+=2;
if(t==0)return(0);
for(q=0;p<t;){
b=s[p++];
for(i=0;i<10;i++)if(s[r+i]==b)break;
if(i==10)d[q++]=b;
else{
for(i=s[p++];i>0;i--,q++)d[q]=b;
}
}
/*printf("P=%X Q=%X ",p,q);fflush(stdout);*/
*pp=p;return(q);
}
void convps(unsigned char *s,char *fn,int n)
{
int i,j;
j=s[4]&7;
if(j==2){
xwide=s[16]|(s[17]<<8);ywide=s[18]|(s[19]<<8);
memcpy(bz,&s[20],xwide*ywide*2);
}
else if(j==1 || j==0){
if(s[4]&8){
i=s[16]|(s[17]<<8);i*=s[18];i<<=1;memcpy(pal,&s[20],i);
i=(s[8]|(s[9]<<8)|(s[10]<<16))+8;
}
else i=8;
ywide=s[i+10]|(s[i+11]<<8);xwide=(s[i+8]|(s[i+9]<<8))<<1;
memcpy(bz,&s[i+12],xwide*ywide);
if(j==1)outbmp8(fn,n,256);
else{
xwide<<=1;outbmp8(fn,n,16);
}
}
}
/* OUTPUT BMP FILE */
void outbmp8(char *fn,int no,int c)
{
int i,j,sx,xpad;
char fz[48],fs[4];
unsigned short k;
if(no>=0){
fs[0]=(no>>8)+'0';if(fs[0]>57)fs[0]+=7;
fs[1]=((no>>4)&15)+'0';if(fs[1]>57)fs[1]+=7;
fs[2]=(no&15)+'0';if(fs[2]>57)fs[2]+=7;
fs[3]=0;
}
else fs[0]=0;
if(c==16){
for(i=xwide*ywide-1;i>=0;i--)bz[i]=((bz[i]&0xF0)>>4)|((bz[i]&0x0F)<<4);
}
strcpy(fz,current);strcat(fz,fn);strcat(fz,fs);strcat(fz,".BMP");
if((fo=fopen(fz,"wb"))==0){
printf("file create errror.[%s]\n",fz);exit(1);
}
sx=(c==256)?xwide:xwide/2;xpad=(4-(sx&3))&3;
fputc('B',fo);fputc('M',fo); /* ID */
writelong((long)c*4L+54L+(long)(sx+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+1]<<8)|pal[i*2];
fputc((k&0x7c00)>>7,fo); /* BLUE */
fputc((k&0x3e0)>>2,fo); /* GREEN */
fputc((k&0x1f)<<3,fo); /* RED */
fputc(0,fo);
}
for(i=ywide-1;i>=0;i--){
fwrite(&bz[i*sx],1,sx,fo);
for(k=0;k<xpad;k++)fputc(0,fo);
}
fclose(fo);
}
/* 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);
}