/*********************************************************************

		   MSX SCREEN12 BSAVE FILE -> FM-TOWNS TIFF FILE
   		     by ちにゃと    (NIFTY-Serve ID:GFH01000)

		            1993/12/07       Ver 2.1 L20           

********************************************************************/

                     
#include <io.h>
#include <fcntl.h>
#include <stat.h>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
#include <mos.h>
#include <math.h>
#include <stat.h>

/* TIFF HEADER
+0   +1   +2   +3   +4   +5   +6   +7   +8   +9   +A   +B   +C   +D   +E   +F*/
char tifheader[512] = {
0x49,0x49,0x2A,0x00,
0x08,0x00,0x00,0x00,
0x0F,0x00,
0xFE,0x00,0x04,0x00,0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
0x00,0x01,0x00,0x00,
0x01,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
0xD4,0x00,0x00,0x00,
0x02,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
0x10,0x00,0x00,0x00,
0x03,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,
0x06,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,
0x0A,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,
0x11,0x01,0x04,0x00,0x01,0x00,0x00,0x00,
0x00,0x02,0x00,0x00,
0x15,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,
0x16,0x01,0x04,0x00,0x01,0x00,0x00,0x00,
0xD4,0x00,0x00,0x00,
0x17,0x01,0x04,0x00,0x01,0x00,0x00,0x00,
0x00,0xAA,0x01,0x00,
0x19,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
0xFF,0x7F,0x00,0x00,
0x1A,0x01,0x05,0x00,0x01,0x00,0x00,0x00,
0xF0,0x01,0x00,0x00,
0x1B,0x01,0x05,0x00,0x01,0x00,0x00,0x00,
0xF8,0x01,0x00,0x00,
0x1C,0x01,0x03,0x00,0x01,0x00,0x00,0x00,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,
0x4B,0x00,0x00,0x00,0x02,0x00,0x00,0x00,
0x4B,0x00,0x00,0x00,0x02,0x00,0x00,0x00
};

/* T-BIOS C ﾗｲﾌﾞﾗﾘ ﾍｯﾀﾞｰ */
#include <fmcfrb.h>
char fname[256];
char filepath[256];
char sccext[10];
char srsext[10];
char scchead[10];
char sccdata[256*64][4];
char tifdata[256*64][8];
char wildfname[100][256];
unsigned char r[4];
unsigned char g[4];
unsigned char b[4];
unsigned char y[4];
unsigned char k,kj;
int bb[4];
int rshift,gshift,bshift;
int sccsize;
int i,j,wildcard;
int w_ret;
int mode;
int fileno;
int namelen;
int flong;
int findflag;
int wildct;
static struct find_t wildfbuf;
static unsigned long ret,pan;

/************************************************************
【オプション解析】

		概要	ｵﾌﾟｼｮﾝﾊﾟﾗﾒ-ﾀの解析を行い結果を返す
		用法	optionset( argc, *argv[] );
		引数	int		argc
				char	*argv[]
		戻り値 	int		0	正常終了
						-1	ｵﾌﾟｼｮﾝの指定が正しくない
************************************************************/

int optionset( int argc, char *argv[] ){

	int		count;
	char	c;
	ret=0;

	strcpy(sccext,"SCC");
	strcpy(srsext,"SRS");
	rshift=0;
	gshift=0;
	bshift=0;	
	ret = 0;
	findflag=0;
	/* ｵﾌﾟｼｮﾝ ﾊﾟﾗﾒｰﾀ 解析 */
	if ( argc == 1 ) {
		ret=-1;
	} 
	else{
		for ( count = 1; count < argc; count++ ){
            c = *argv[count];                        /* 先頭文字 get */
            if ( c == '/' || c == '-' ){
                c = *(argv[count] + 1);                /* 2 文字目 get */
                switch ( c ){
                    case 'h':
                    case 'H':
                    case '?':
                        ret = -1;
                        break;

                    case 'r':
                    case 'R':
    	                rshift = *(argv[count] + 2);
                        rshift -= 0x30; 
                        if ((*(argv[count] + 3)>0x30) &&
                        		(*(argv[count] + 3)<0x34)   ){
                        	rshift=rshift*10+(*(argv[count] + 3))-0x30;
                        }
                        break;

                    case 'g':
                    case 'G':
    	                gshift = *(argv[count] + 2);
                        gshift-=0x30;
                        if ((*(argv[count] + 3)>0x30) &&
                        		(*(argv[count] + 3)<0x34)   ){
                        	gshift=gshift*10+(*(argv[count] + 3))-0x30;
                        }
                        break;

                    case 'b':
                    case 'B':
    	                bshift = *(argv[count] + 2);
                        bshift-=0x30;
                        if ((*(argv[count] + 3)>0x30) &&
                        		(*(argv[count] + 3)<0x34)   ){
                        	bshift=bshift*10+(*(argv[count] + 3))-0x30;
                        }
                        break;

  	                /* 無効 ﾊﾟﾗﾒｰﾀ */
    	            default:
                    ret = -1;
                }		/* <switch> */
            }
            else{
               	strcpy(fname, argv[count] );
         	}
        }	/* <for> */
  	}
	if(ret==-1){
		printf("****************************************************\n");
		printf("   MSX SCREEN12 BSAVE FILE -> FM-TOWNS TIFF FILE\n");
		printf("        by ちにゃと    (NIFTY-Serve ID:GFH01000)\n");
		printf("            1993/12/07       Ver 2.1 L20        \n");
		printf("****************************************************\n");
		printf("\n");
		printf("用法) RUN386  SCC2TIF.EXP [-Rx] [-Gx] [-Bx] FILENAME\n");
		printf("\n");
		printf("-Rx -Gx -Bx   RGBの強さを調整  x=0〜31 \n");
		printf("              省略時は -R0 -G0 -B0\n");
		printf("              \n");
		printf("\n");
		printf("FILENAME      拡張子が .SCC .SRS  のみ\n");
		printf("              ワイルドカ−ドも指定可能\n");
		printf("\n");
		printf("ex1)  RUN386 SCC2TIF.EXP SAMPLE.SCC\n");
		printf("ex2)  RUN386 SCC2TIF.EXP A:*.SCC\n");
		printf("ex3)  RUN386 SCC2TIF.EXP A:\\SCC\\SAMPLE*.SCC\n");
		printf("\n");
		printf("\n");
		printf("\n");
		return(-1);
	}
	namelen=strlen(fname);
	flong=namelen;
	for ( i =namelen-1; (i+1)>0 ;i-- ){
		fname[i]=toupper(fname[i]);
	}
	for ( i =namelen-1; (i+1)>0 ;i-- ){
		if ( fname[i] != 0x5c ){
			flong -= 1;
		}
		else {
			findflag=1;
			break;
		}
	}
	if(findflag==0){
		flong=1;
		for ( i = 0; i <255 ;i ++ ){
			if ( fname[i] != 0x3a ){
				flong += 1;
			}
			else {
				findflag=2;
				break;
			}
		}
	}
	if(findflag!=0){
		strncpy( filepath,fname,flong );
	}
	flong =strlen(fname);
	for (i=0;i<3;i++){
		if(fname[flong-3+i]!=sccext[i]&&fname[flong-3+i]!=(sccext[i]+0x20)&&
		   fname[flong-3+i]!=srsext[i]&&fname[flong-3+i]!=(srsext[i]+0x20) )
		{
			printf("拡張子が .SCC でも .SRS でもありません｡\n");
			return(-1);
		}
	}
	return ret;
}
/************************************************************

						sccload

************************************************************/
int sccload()

{
char fname2[256];

strcpy(fname2,fname);
printf("%s  ",fname);


	if (_access(fname2,0) !=0 ){
		printf("no scc file");
		return(-1);
	}
	else{
		pan=_open(fname2,_O_BINARY | _O_RDWR );
		if (pan== -1 ){
			return(-1);
		}
		ret=_read(pan,scchead,7);
		if (ret== -1){
			_close(pan);
			return(-1);
		}
		else{
			if(scchead[0]!=0xfe){
				printf("BSAVE FORMATではありません｡\n");
				_close(pan);
				return(-1);
			}
			sccsize=scchead[4]*256+scchead[3]+1;
			for( ; ; ){
				ret = _read(pan,&sccdata[0][0],256*250);
				if (ret == -1 ){
					_close(pan);
					printf("読み込みエラ−が発生しました。\n");
					return(-1);
				}
				else{
					_close(pan);
					break;
				}
			}
		}
	}
	return(0);
}
/***************************************************************
【ﾃﾞ-ﾀﾌｧｲﾙを作成する】

		概要	
		用法	tifsave();
		引数	無し
***************************************************************/

int tifsave ( void )
{
	char fname2[256];
	int		wsize;
	flong = 0;

	for ( i = 0; i <251 ;i ++ ){
		if ( fname[i] != 0x2e ){
			flong += 1;
		}
		else {
			break;
		}
	}
	strncpy( fname2,fname,flong );
	fname2[flong]=0x00;
	strcat( fname2,".TIF");
	fname2[flong+4]=0x00;
	tifheader[0x2a]=sccsize/256;
	tifheader[0x7e]=sccsize/256;


	if ( _access( fname2, 0 ) == 0 ) {
		printf( " 同名のファイルがあります。上書きします。" );
		ret = _unlink ( fname2 );
	}
	ret = 0;
	pan = _open( fname2,_O_BINARY | _O_RDWR | _O_CREAT, _S_IREAD | _S_IWRITE );
	if ( pan == -1) {
		printf("  書き込みのオ−プンに失敗しました。 \n" );
		ret = -1;
	}
	if ( ret == 0 ) {
		wsize = _write( pan, &tifheader[0], 512  );
		if ( wsize == -1 || wsize != 512 ) {
			printf("  ヘッダ書き込みに失敗しました。\n" );
			_close( pan );
			return(-1);
		}
		else{

			wsize = _write( pan, &tifdata[0][0], sccsize*2  );
			if ( wsize == -1 || wsize != ( sccsize*2 ) ) {
				printf("  デ−タ書き込みに失敗しました。\n" );
				_close( pan );
				return(-1);
			}
			else{
				printf("\n");
			}
		}
	}
	return (0);
}


/*********************************************************

			  			convert
			  			
*********************************************************/
int convert(){

	for(i=0;i<sccsize/4;i++){
		y[0]=sccdata[i][0] / 8;
		y[1]=sccdata[i][1] / 8;
		y[2]=sccdata[i][2] / 8;
		y[3]=sccdata[i][3] / 8;
		k = ( sccdata[i][0] & 7 )+ (sccdata[i][1] & 3 )*8;
		kj= ( sccdata[i][2] & 7 )+ (sccdata[i][3] & 3 )*8;
		for(j=0;j<4;j++){
			bb[j]=y[j];
			r[j]=y[j];
			g[j]=y[j];
			b[j]=y[j];
			if((sccdata[i][1] & 4 )==0){
				g[j]+=k;
				bb[j]-=k;
			}
			else{
				g[j]-=(32-k);
				bb[j]+=(32-k);
			}
			if((sccdata[i][3] & 4 )==0){
				r[j]+=kj;
				bb[j]-=kj*2;
			}
			else{
				r[j]-=(32-kj);
				bb[j]+=(32-kj)*2;
			}
			if( bb[j] >= 0 ){
				b[j]+=(bb[j]/4);
			}
			else{
				if( b[j] > (-1)*((bb[j]+2)/4) ){
					b[j]+=(bb[j]/4);
				}
				else{
					b[j]=0;
				}
			}
			r[j]+=rshift;
			g[j]+=gshift;
			b[j]+=bshift;
			if ((r[j] & 0x80)!=0){
				r[j]=0;
			}
			if ((g[j] & 0x80)!=0){
				g[j]=0;
			}
			if ((b[j] & 0x80)!=0){
				b[j]=0;
			}
			if (r[j]>31){
				r[j]=31;
			}
			if (g[j]>31){
				g[j]=31;
			}
			if (b[j]>31){
				b[j]=31;
			}
			tifdata[i][j*2+0]=(g[j]*1024+r[j]*32+b[j])%256;
			tifdata[i][j*2+1]=(g[j]*1024+r[j]*32+b[j])/256;
		}
	}
	return(0);
}

/*********************************************************

			  			メイン

*********************************************************/


void main( int argc, char *argv[] ){
	fileno=0;
	mode=0x16;
	if(optionset( argc, argv )!=0){
		exit(-1);
	}
	for ( i = 0; i <251 ;i ++ ){
		if ( fname[i] != 0x2a ){
			flong += 1;
		}
		else {
			wildcard=1;
			break;
		}
	}
	if(wildcard==1){
		w_ret=_dos_findfirst((char*)fname,mode,&wildfbuf);
		if(w_ret){
			printf("指定のワイルドカ−ドが見つかりません\n");
			exit(-1);
		}
		else{
			strcpy(wildfname[fileno],wildfbuf.name);
			fileno++;
			for (;;){
				w_ret=_dos_findnext(&wildfbuf);
				if(w_ret){
					break;
				}
				strcpy(wildfname[fileno],wildfbuf.name);
				fileno++;
			}
		}
		for(wildct=0;wildct<fileno;wildct++){
			strcpy(fname,filepath);
			strcat(fname,wildfname[wildct]);
 			if(sccload()!=0){
				exit(-1);
			}
			if(convert()!=0){
				exit(-1);
			}
			strcpy(fname,wildfname[wildct]);
			if(tifsave()!=0){
				exit(-1);
			}
		}
	}
	else{
		if(sccload()!=0){
			exit(-1);
		}
		if(convert()!=0){
			exit(-1);
		}
		findflag=0;
		namelen=strlen(fname);
		flong=namelen;
		for ( i =namelen-1; (i+1)>0 ;i-- ){
			if ( fname[i] != 0x5c ){
				flong -= 1;
			}
			else {
				findflag=1;
				break;
			}
		}
		if(findflag==0){
			flong=1;
			for ( i = 0; i <255 ;i ++ ){
				if ( fname[i] != 0x3a ){
					flong += 1;
				}
				else {
					findflag=2;
					break;
				}
			}
		}
		if(findflag!=0){
			for(i=flong;i<namelen;i++){
				fname[i-flong]=fname[i];
			}
			fname[i-flong]=0;
		}
		if(tifsave()!=0){
			exit(-1);
		}
	}
	exit(0);
}
