/*
			V0x RECORD & PLAY

			Hiroshi TODA
			1993 9/1	for GUI

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winb.h>
#include <te.h>
#include <fntb.h>
#include <gui.h>
#include <file_dlg.h>
#include <egb.h>
#include <mos.h>
#include <snd.h>
#include <math.h>
#include "video.h"
#include "ugoku.h"
#include "vdsysx.h"

#define RED_DIVI 12		/* 赤分割数 */
#define GRE_DIVI 8		/* 緑分割数 */
#define BLU_DIVI 6		/* 青分割数 */
#define YEL_DIVI 7		/* 黄分割数 */

/* VDXSUB.Cの関数 */
extern v_cmp_frame_dsp( int ) ;
extern file_kakuchousi_set( char *, char *, char * ) ;
/* ugoku.libの関数 */
extern vd_wide_02_init( int, int, int ) ;
extern vd_wide_01_2( char * ) ;
extern vd_wide_02_c( char * ) ;

extern char *guiEgbPtr;			/* EGB work */

/* 絵の大きさを可変出来る様にするため横と縦の大きさをグローバル変数に */
int v02xd = 128, v02yd = 96;


/* もしvdsys.tabが見つからなかった場合のためのテーブルを作成 */

	/* Yデータ曲線変換のための変数の型の宣言 */

typedef struct
{
	int		ofst ;			/* offset(全範囲を0〜94とした時の初期値) */
	int		n ;				/* cv2のデータ数 */
	int		m1 ;			/* 倍率 */
	int		m2 ;			/* 倍率 */

	short int cv1[64] ;		/* yc to rgb に使う変換(4倍値) */
	char	cv2[1024] ;		/* rgb to yc に使う変換 */

} YCURVE ;

/*		//	基準輝度分割表
		0,    1,     2,     3,     4,     5,     6,     7,     8,     9,
		10,   11,    12,    13,    14,    15,    16,    17,    18,    19,
		20,   21,    22,    23,    24,    25,    26,    27,    28.3,  29.6,
		31,   32.3,  33.6,  35,    36.3,  37.6,  39,    40.5,  42,    43.5,
		45,   46.5,  48,    49.5,  51,    52.5,  54,    55.5,  57,    58.5,
		60,   61.5,  63,    64.5,  66,    67.5,  69,    70.5,  72,    73.5,
		75,   76.5,  78,    79.5,
*/

static YCURVE	ycv =
	{
		5,
		486,
		12,
		6,

		{
			6,  18, 30, 42, 54, 66, 78, 90, 102,114,
			126,138,150,162,174,186,198,210,222,234,
			246,258,270,282,294,306,318,332,348,364,
			380,396,412,428,444,460,477,495,513,531,
			549,567,585,603,621,639,657,675,693,711,
			729,747,765,783,801,819,837,855,873,891,
			909,927,945,963
		},

		{
			0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3,
			3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6,
			6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9,
			10,10,10,10,10,10,11,11,11,11,11,11,12,12,12,12,12,12,13,13,
			13,13,13,13,14,14,14,14,14,14,15,15,15,15,15,15,16,16,16,16,
			16,16,17,17,17,17,17,17,18,18,18,18,18,18,19,19,19,19,19,19,
			20,20,20,20,20,20,21,21,21,21,21,21,22,22,22,22,22,22,23,23,
			23,23,23,23,24,24,24,24,24,24,25,25,25,25,25,25,26,26,26,26,
			26,26,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,29,29,
			29,29,29,29,29,29,30,30,30,30,30,30,30,30,31,31,31,31,31,31,

			31,31,32,32,32,32,32,32,32,32,33,33,33,33,33,33,33,33,34,34,
			34,34,34,34,34,34,35,35,35,35,35,35,35,35,36,36,36,36,36,36,
			36,36,36,37,37,37,37,37,37,37,37,37,38,38,38,38,38,38,38,38,
			38,39,39,39,39,39,39,39,39,39,40,40,40,40,40,40,40,40,40,41,
			41,41,41,41,41,41,41,41,42,42,42,42,42,42,42,42,42,43,43,43,
			43,43,43,43,43,43,44,44,44,44,44,44,44,44,44,45,45,45,45,45,
			45,45,45,45,46,46,46,46,46,46,46,46,46,47,47,47,47,47,47,47,
			47,47,48,48,48,48,48,48,48,48,48,49,49,49,49,49,49,49,49,49,
			50,50,50,50,50,50,50,50,50,51,51,51,51,51,51,51,51,51,52,52,
			52,52,52,52,52,52,52,53,53,53,53,53,53,53,53,53,54,54,54,54,

			54,54,54,54,54,55,55,55,55,55,55,55,55,55,56,56,56,56,56,56,
			56,56,56,57,57,57,57,57,57,57,57,57,58,58,58,58,58,58,58,58,
			58,59,59,59,59,59,59,59,59,59,60,60,60,60,60,60,60,60,60,61,
			61,61,61,61,61,61,61,61,62,62,62,62,62,62,62,62,62,63,63,63,
			63,63,63,63,63,63
		}
	} ;

direct_v02_table( int k, char *table )
{
	char *v02table1, *v02table2, *brgtoyc, *yytoy ;

	v02table1 = table ;
	v02table2 = v02table1 + 32768 ;
	brgtoyc   = v02table2 + 512   ;
	yytoy     = brgtoyc   + 65536 ;

	v00_to_v02_conv_brg_set_buffer( v02table1, k ) ;

	v00_to_v02_conv_ytable_set( v02table2, yytoy ) ;

	v00_to_v02_conv_yc_set_buffer( brgtoyc, v02table1, k ) ;

	return 0;
}

/***** 直接係数を計算しながら算出する 1993 12 *****/

/* 輝度*256+色相 → b+r*32+g*1024 */

yc_to_brg_no4( rt, n )
double rt ;
int n ;
{
	extern double v00_to_v02_conv_blue_rate( double, double ) ;
	extern double v00_to_v02_conv_yellow_rate( double, double ) ;
	extern double v00_to_v02_conv_red_rate( double, double, double ) ;
	extern double v00_to_v02_conv_green_rate( double, double, double ) ;

	int b0, r0, g0 ;
	double yy, y, cr, cb, rate ;
	int m ;

	m = ( (n >> 8) & 0x3f ) ;
	m = ycv.cv1[ m ] + ycv.m1 * ycv.ofst ;
	y = m ;
	yy = y * 31 / ( 93 * ycv.m1 ) ;
	y = y / ( 93 * ycv.m1 ) ;

	cr = ( ( n & 0xff ) % (RED_DIVI + GRE_DIVI) - GRE_DIVI ) + .5 ;
	cb = ( ( n & 0xff ) / (RED_DIVI + GRE_DIVI) - YEL_DIVI ) + .5 ;

	if( cb >= 0 )
	{
		rate = v00_to_v02_conv_blue_rate( rt, y ) ;
	    cb = cb * 15.5 * rate / BLU_DIVI ;
	}
	else
	{
		rate = v00_to_v02_conv_yellow_rate( rt, y ) ;
	    cb = cb * 15.5 * rate / YEL_DIVI ;
	}

	if( cr >= 0 )
	{
		rate = v00_to_v02_conv_red_rate( rt, y, cb/31 ) ;
	    cr = cr * 15.5 * rate / RED_DIVI ;
	}
	else
	{
		rate = v00_to_v02_conv_green_rate( rt, y, cb/31 ) ;
	    cr = cr * 15.5 * rate / GRE_DIVI ;
	}

	g0 = yy -  .7144070603*cr    -  .3443692095*cb + .5 ;
	r0 = yy + 1.402581175*cr                       + .5 ;
	b0 = yy                      + 1.773042678*cb  + .5 ;

	if( g0 < 0 )g0 = 0;
	if( g0 > 31 )g0 = 31;
	if( r0 < 0 )r0 = 0;
	if( r0 > 31 )r0 = 31;
	if( b0 < 0 )b0 = 0;
	if( b0 > 31 )b0 = 31;
	return  b0 + r0*32 + g0*1024;
}

/* b+r*32+g*1024 → 輝度*256+色相 */

brg_to_yc_no4( rt, n )
double rt ;
int n ;
{
	extern double v00_to_v02_conv_blue_rate( double, double ) ;
	extern double v00_to_v02_conv_yellow_rate( double, double ) ;
	extern double v00_to_v02_conv_red_rate( double, double, double ) ;
	extern double v00_to_v02_conv_green_rate( double, double, double ) ;

	int b, r, g ;
	int cr, cb, yy ;
	double yyd, crd, cbd, y, rate ;
	double cbadd, cradd ;
	int m ;

	b = ( n & 0x1f );
	r = ( (n >> 5) & 0x1f );
	g = ( (n >> 10) & 0x1f );

	yyd =   .5870*g + .2990*r + .1140*b ;
	crd = - .4185*g + .4998*r - .0813*b ;
	cbd = - .3311*g - .1686*r + .4997*b ;

	m = ( yyd * 93 / 31 - ycv.ofst ) * ycv.m2 ;

	if( m <= 0 )
	{
		yy = 0 ;
	}
	else if( m < ycv.n )
	{
		yy = ycv.cv2[ m ] ;
	}
	else
	{
		yy = 63 ;
	}

	y = yyd/31 ;

	if( y < (double)( ycv.cv1[ 0 ] + ycv.m1 * ycv.ofst )
														 / ( 93 * ycv.m1 ) )
	{
		y = (double)( ycv.cv1[ 0 ] + ycv.m1 * ycv.ofst ) / 372 ;
	}
	else if( y < (double)( ycv.cv1[ 63 ] + ycv.m1 * ycv.ofst )
														 / ( 93 * ycv.m1 ) )
	{
		y = y ;
	}
	else
	{
		y = (double)( ycv.cv1[ 63 ] + ycv.m1 * ycv.ofst )
														 / ( 93 * ycv.m1 ) ;
	}

	if( crd >= 0 )
	{
		rate = v00_to_v02_conv_red_rate( rt, y, cbd/31 ) ;
	    cr = crd * RED_DIVI / rate / 15.5 + GRE_DIVI ;
	}
	else
	{
		rate = v00_to_v02_conv_green_rate( rt, y, cbd/31 ) ;
	    cr = crd * GRE_DIVI / rate / 15.5 + GRE_DIVI ;
	}

	if( cbd >= 0 )
	{
		rate = v00_to_v02_conv_blue_rate( rt, y ) ;
	    cb = cbd * BLU_DIVI / rate / 15.5 + YEL_DIVI ;
	}
	else
	{
		rate = v00_to_v02_conv_yellow_rate( rt, y ) ;
	    cb = cbd * YEL_DIVI / rate / 15.5 + YEL_DIVI ;
	}

	if( cr < 0 )cr = 0;
	if( cr > (RED_DIVI + GRE_DIVI - 1) )cr = RED_DIVI + GRE_DIVI - 1 ;
	if( cb < 0 )cb = 0;
	if( cb > (YEL_DIVI + BLU_DIVI - 1) )cb = YEL_DIVI + BLU_DIVI - 1 ;
	if( cr + cb*(RED_DIVI + GRE_DIVI) > 255 )cb-- ;

	if( yy < 0 )yy = 0;
	if( yy > 63 )yy = 63;

	return  cr + cb*(RED_DIVI + GRE_DIVI) + ( yy << 8 );
}

/* 係数計算ルーチン( 0<=y<=1,0<=cb<=1) */

double v00_to_v02_conv_blue_rate( rt, y )
double rt, y ;
{
	double rate0, rate1, ret ;

	rate0 = y / .114 ;

	rate1 = (1 - y) / (1 - .114) ;

	ret = rt ;
	if( ret > rate0 )
		ret = rate0 ;
	if( ret > rate1 )
		ret = rate1 ;
	if( y < .114 )
		ret = .3*ret + .7*rt ;
	else
		ret = .7*ret + .3*rt ;
	if( ret < .12 )
		ret = .12 ;

	return ret ;
}

double v00_to_v02_conv_yellow_rate( rt, y )
double rt, y ;
{
	double rate0, rate1, ret ;

	rate0 = y / .886 ;

	rate1 = (1 - y) / (1 - .886) ;

	ret = rt ;
	if( ret > rate0 )
		ret = rate0 ;
	if( ret > rate1 )
		ret = rate1 ;
	if( y > .886 )
		ret = .3*ret + .7*rt ;
	else
		ret = .7*ret + .3*rt ;
	if( ret < .14 )
		ret = .14 ;

	return ret ;
}

/* 必ず cbの実際の値が決まってからやること */

double v00_to_v02_conv_red_rate( rt, y, cb )
double rt, y, cb ;
{
	double c, rate0, rate1, rate2, rate3, ret ;

	c = ( y    -  .3443692095*cb ) / .7144070603 ;	/* cr : g=0 point */
	rate0 = c / .5 ;
	if( rate0 < 0 )
		rate0 = 0 ;

	c = ( 1 - y ) / 1.402581175 ;					/* cr : r=max point */
	rate1 = c / .5 ;
	if( rate1 < 0 )
		rate1 = 0 ;

	rate2 = y / .299 ;

	rate3 = (1 - y) / (1 - .299) ;

	ret = rt ;
	if( ret > rate0 )
		ret = rate0 ;
	if( ret > rate1 )
		ret = rate1 ;
	if( ret > rate2 )
		ret = rate2 ;
	if( ret > rate3 )
		ret = rate3 ;
	if( y < .299 )
		ret = .3*ret + .7*rt ;
	else
		ret = .7*ret + .3*rt ;
	if( ret < .27 )
		ret = .27 ;

	return ret ;
}

/* 必ず cbの実際の値が決まってからやること */

double v00_to_v02_conv_green_rate( rt, y, cb )
double rt, y, cb ;
{
	double c, rate0, rate1, rate2, rate3, ret ;

	c = - y / 1.402581175 ;							/* cr : r=0 point */
	rate0 = c / ( - .5 ) ;
	if( rate0 < 0 )
		rate0 = 0 ;

	c = ( y - .3443692095*cb - 1 ) / .7144070603 ;	/* cr : g=max point */
	rate1 = c / ( - .5 ) ;
	if( rate1 < 0 )
		rate1 = 0 ;

	rate2 = y / .587 ;

	rate3 = (1 - y) / (1 - .587) ;

	ret = rt ;
	if( ret > rate0 )
		ret = rate0 ;
	if( ret > rate1 )
		ret = rate1 ;
	if( ret > rate2 )
		ret = rate2 ;
	if( ret > rate3 )
		ret = rate3 ;
	ret = .7*ret + .3*rt ;
	if( ret < .16 )
		ret = .16 ;

	return ret ;
}



v00_to_v02_conv_yc_set_buffer( buffer, brg_buf, rate )
char *buffer, *brg_buf;
int rate;
{
	int i, temp;
	int p[] = {
		    16384*0+0,   16384*0+64,  16384*0+128, 16384*0+192,
		    16384*0+256, 16384*0+320, 16384*0+384, 16384*0+448,
		    16384*1+0,   16384*1+64,  16384*1+128, 16384*1+192,
		    16384*1+256, 16384*1+320, 16384*1+384, 16384*1+448,
		    16384*2+0,   16384*2+64,  16384*2+128, 16384*2+192,
		    16384*2+256, 16384*2+320, 16384*2+384, 16384*2+448,
		    16384*3+0,   16384*3+64,  16384*3+128, 16384*3+192,
		    16384*3+256, 16384*3+320, 16384*3+384, 16384*3+448,
	};

	for( i=0 ; i<32768 ; i++ ){			/* 32768 */
		WORD( buffer + i*2 ) = brg_to_yc_no4( .0625*rate + .25, i );
		temp = WORD( brg_buf + WORD( buffer + i*2 )*2 );
		pokew( p[i/1024]+512*((i%1024)/32)+(i%32)*2+32768 + 0x40000,
		 0x104, temp );
	}
	return 0;
}

	/* サブ */
v00_to_v02_conv_brg_set_buffer( buffer, rate )
char *buffer;
int rate;
{
	int i;

	for( i=0 ; i<8192*2 ; i++ ){
		WORD( buffer + i*2 ) = yc_to_brg_no4( .0625*rate + .25, i );
		pokew( i*2 + 0x40000, 0x104, WORD( buffer + i*2 ) );
	}
	return 0;
}

/*
	v00 → v02 conv.
*/

	/* サブ */
/* move box ( 8*8dot )指定されたboxを転送 */

	/* デジタル的検索 */

cnv_movebox8_cmp(flt, new_ad,x1,y1, old_ad,x2,y2)
int flt, x1,y1, x2,y2;
char *new_ad, *old_ad;
{
	int x, y, ad, n, k, d1, d2, flt1, flt2, flt3 ;
	int cr1, cr2, cb1, cb2 ;
	char *ad1, *ad2 ;

	int f1[] = { 0, 0, 0, 1, 1, 1, 1, 1, 1 };	/* Y */
	int f2[] = { 0, 0, 1, 1, 1, 1, 1, 1, 1 };	/* Cr */
	int f3[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1 };	/* Cb */

	int xhf, yhf;	/* v02xd, v02ydの半分の値 */
	xhf = v02xd >> 1;
	yhf = v02yd >> 1;

	if( flt > 8 )flt = 8;
	flt1 = f1[ flt ];
	flt2 = f2[ flt ];
	flt3 = f3[ flt ];
	if( y1 < v02yd ){	/* 輝度 */
	    if(  (y2*v02xd + x2*2 < 0)
	     || (y2*v02xd + x2*2 > v02xd*(v02yd - 8) + v02xd-16) )
		return -1;
	    ad1 = new_ad + y1*v02xd + x1*2;
	    ad2 = old_ad + y2*v02xd + x2*2;
	    n = 0;
	    for( y=0 ; y<8 ; y++ ){
		k = 0;
		for( x=0 ; x<8 ; x++ ){
		    ad = y*v02xd + x*2;
		    d1 = BYTE(ad1+ad) - BYTE(ad2+ad);
		    if( d1 < 0 )d1 = -d1;
		    d2 = BYTE(ad1+ad+1) - BYTE(ad2+ad+1);
		    if( d2 < 0 )d2 = -d2;

		    if( d1 <= flt1 && d2 <= flt1 )k++;
		    else {
			if( k > 1 )n = n + k;
			k = 0;
		    }
		}
		if( k > 1 )n = n + k;
	    }
	}
	else {		/* 色相 */
	    y1 = y1 - v02yd;
	    y2 = y2 - v02yd;
	    if(  (y2*xhf + x2 < 0) || (y2*xhf + x2 > xhf*(yhf-8) + xhf-8) )
		return -1;
	    ad1 = new_ad + v02xd*v02yd + y1*xhf + x1;
	    ad2 = old_ad + v02xd*v02yd + y2*xhf + x2;
	    n = 0;
	    for( y=0 ; y<8 ; y++ ){
		k = 0;
		for( x=0 ; x<8 ; x++ ){
		    ad = y*xhf + x;

		    cr1 = ( BYTE(ad1+ad) % (RED_DIVI + GRE_DIVI) ) ; /* cr */
		    cr2 = ( BYTE(ad2+ad) % (RED_DIVI + GRE_DIVI) ) ;
		    d1 = 0 ;
		    if( (cr1 <= GRE_DIVI - 1) && (cr1 - cr2 >= 0)
		     && (cr1 - cr2 <= flt2)
		    )
			d1 = 1 ;
		    if( (cr1 >= GRE_DIVI) && (cr2 - cr1 >= 0)
		     && (cr2 - cr1 <= flt2)
		    )
			d1 = 1 ;

		    cb1 = ( BYTE(ad1+ad) / (RED_DIVI + GRE_DIVI) ) ; /* cb */
		    cb2 = ( BYTE(ad2+ad) / (RED_DIVI + GRE_DIVI) ) ;
		    d2 = 0 ;
		    if( (cb1 <= YEL_DIVI - 1) && (cb1 - cb2 >= 0)
		     && (cb1 - cb2 <= flt3)
		    )
			d2 = 1 ;
		    if( (cb1 >= YEL_DIVI) && (cb2 - cb1 >= 0)
		     && (cb2 - cb1 <= flt3)
		    )
			d2 = 1 ;

		    if( d1 && d2 )k++;
		    else {
			if( k > 1 )n = n + k;
			k = 0;
		    }
		}
		if( k > 1 )n = n + k;
	    }
	}
	return n;
}

	/* アナログ的動きサーチ */

cnv_movebox8_cmp_2(flt, new_ad,x1,y1, old_ad,x2,y2)
int flt, x1,y1, x2,y2;
char *new_ad, *old_ad;
{
	int x, y, ad, n, d1, d2;
	char *ad1, *ad2;

	int xhf, yhf;	/* v02xd, v02ydの半分の値 */
	xhf = v02xd >> 1;
	yhf = v02yd >> 1;

	if( y1 < v02yd ){	/* 輝度 */
	    if(  (y2*v02xd + x2*2 < 0)
	     || (y2*v02xd + x2*2 > v02xd*(v02yd-8) + v02xd-16) )
		return -1;
	    ad1 = new_ad + y1*v02xd + x1*2;
	    ad2 = old_ad + y2*v02xd + x2*2;
	    n = 0;
	    for( y=0 ; y<8 ; y++ ){
		for( x=0 ; x<16 ; x++ ){
		    ad = y*v02xd + x;
		    d1 = BYTE(ad1+ad) - BYTE(ad2+ad);
		    if( d1 < 0 )d1 = -d1;
		    n = n + 100 - d1;
		}
	    }
	}
	else {		/* 色相 */
	    y1 = y1 - v02yd;
	    y2 = y2 - v02yd;
	    if(  (y2*xhf + x2 < 0) || (y2*xhf + x2 > xhf*(yhf - 8) +xhf-8) )
		return -1;
	    ad1 = new_ad + v02xd*v02yd + y1*xhf + x1;
	    ad2 = old_ad + v02xd*v02yd + y2*xhf + x2;
	    n = 0;
	    for( y=0 ; y<8 ; y++ ){
		for( x=0 ; x<8 ; x++ ){
		    ad = y*xhf + x;
		    d1 = ( BYTE(ad1+ad) % (RED_DIVI + GRE_DIVI) )
		       - ( BYTE(ad2+ad) % (RED_DIVI + GRE_DIVI) ) ; /* cr */
		    if( d1 < 0 )d1 = -d1;
		    d2 = ( BYTE(ad1+ad) / (RED_DIVI + GRE_DIVI) )
		       - ( BYTE(ad2+ad) / (RED_DIVI + GRE_DIVI) ) ; /* cb */
		    if( d2 < 0 )d2 = -d2;
		    n = n +100 - d1 - d2;
		}
	    }
	}
	return n;
}

cnv_movebox8
(
 i, flt, rate, step, data2_buf, new_ad, old_ad, data1_buf, make_buf, check_buf
)
int i, flt, rate, step;
char *make_buf, *data2_buf, *data1_buf, *check_buf, *new_ad, *old_ad;
{
	int p1, p2, n;
	int j, j1, j2, j3;
	int x, y, x1, y1, x2, y2, kx, ky, d;
	int max, a1, a2, temp, temp2, total, addr1, addr2, last;
	char *mksub, *ycsub;

	WORD( data2_buf+i ) = 0x3008; i+=2;	/* move box 8 */
	p1 = i; i+=4;			/* size point */
	p2 = i; i+=4;		/* data 個数 count point */
	n = 0;			/* data 個数 count */

	int xhf, yhf;	/* v02xd, v02ydの半分の値 */
	xhf = v02xd >> 1;
	yhf = v02yd >> 1;

	mksub = data2_buf+0x1000;	/* 旧makeデータをもう1枚作る */
	total = v02xd * v02yd * 3 / 4 ;
	for( j=0 ; j<total ; j+=4 )
		DWORD( mksub+j ) = DWORD( make_buf+j );
	ycsub = mksub + total;		/* 旧YCデータをもう1枚作る */
	total = v02xd * v02yd * 5 / 4 ;
	for( j=0 ; j<total ; j+=4 )
		DWORD( ycsub+j ) = DWORD( old_ad + j );

	last = 0;
	total = v02yd * 3 / 2 ;
	for( y=0 ; y<total ; y+=8 ){

	    if( y > total-8 )
		y = total-8 ;

	    for( x=0 ; x<xhf ; x+=8 ){
		temp2 = cnv_movebox8_cmp(flt, new_ad,x,y, old_ad,x,y);
		if( temp2 >= 64-4 )goto mov02;
			/* アナログサーチ */
		max = 0;
		kx = x; ky = y;
		x1 = x; y1 = y;
		for( d=step ; d>0 ; d-- ){
		  for( j2 = -1 ; j2 <= 1 ; j2++ ){
		     for( j3 = -1 ; j3 <= 1 ; j3++ ){
			x2 = x1 + j3*d;
			y2 = y1 + j2*d;
			if( x2 < 0 )x2 = 0;
			if( y2 < 0 )y2 = 0;
			temp
			=cnv_movebox8_cmp_2(flt, new_ad,x,y, old_ad,x2,y2);
			if( temp > max ){
			    max = temp;
			    kx = x2; ky = y2;
			}
		     }
		  }
		  x1 = kx; y1 = ky;
		}
			/* デジタルサーチ */
		max = 0;
		for( d=step ; d>0 ; d-- ){
		  for( j2 = -1 ; j2 <= 1 ; j2++ ){
		     for( j3 = -1 ; j3 <= 1 ; j3++ ){
			x2 = x1 + j3*d;
			y2 = y1 + j2*d;
			if( x2 < 0 )x2 = 0;	/* はみ出しを切る */
			if( y2 < 0 )y2 = 0;
			temp
			=cnv_movebox8_cmp(flt, new_ad,x,y, old_ad,x2,y2);
			if( temp > max ){
			    max = temp;
			    kx = x2; ky = y2;
			}
		     }
		  }
		  x1 = kx; y1 = ky;
		}

		if( max > temp2+4 ){
		    addr1 = y*xhf + x - last;
		    last = y*xhf + x;
		    if( addr1<0xff ){
			BYTE( data2_buf+i ) = addr1;
			i += 1;
		    }
		    if( (addr1>=0xff) && (addr1<0xffff) ){
			BYTE( data2_buf+i ) = 0xff;
			WORD( data2_buf+i+1 ) = addr1;
			i += 3;
		    }
		    if( addr1>=0xffff ){
			BYTE( data2_buf+i ) = 0xff;
			WORD( data2_buf+i+1 ) = 0xffff;
			DWORD( data2_buf+i+3 ) = addr1;
			i += 7;
		    }
		    addr2 = ky*xhf + kx - last;
		    			 /* addr2は符号付データ */
		    if( addr2>=-127 && addr2<=127 ){
			BYTE( data2_buf+i ) = addr2;
			i += 1;
			goto mov01;
		    }
		    if( addr2>=-32767 && addr2<=32767 ){
			BYTE( data2_buf+i ) = 0x80;
			WORD( data2_buf+i+1 ) = addr2;
			i += 3;
			goto mov01;
		    }
		    if( addr2 < -32767 || addr2 > 32767 ){
			BYTE( data2_buf+i ) = 0x80;
			WORD( data2_buf+i+1 ) = 0x8000;
			DWORD( data2_buf+i+3 ) = addr2;
			i += 7;
			goto mov01;
		    }
	     mov01: n++;
				/* make data remove */
		    for( j2=0 ; j2<8 ; j2++ ){
			for( j3=0 ; j3<8 ; j3++ ){
			    j1 = ( y + j2 )*xhf + x + j3;
			    a1
			    = BYTE( mksub + (ky + j2)*xhf + kx+j3 );
			    BYTE( make_buf + j1 ) = a1;
			    a2 = BYTE( data1_buf + j1 );
			    if(
			       cnv_cmp_data( a1, a2, j1, rate ) == 0
			       &&
			       BYTE( check_buf + j1 ) == 0
			    ){
				BYTE( check_buf + j1 ) = 1;
//				j = ( (j1/(v02xd/2)) << 9 )
//				  + ( (j1%(v02xd/2)) << 1 );
//				xorw( j+(v02xd*2), 0x104, 0xffff );
			    }
			}
		    }
				/* old_ad data remove */
		    if( y < v02yd ){
			for( j2=0 ; j2<8 ; j2++ ){
			    for( j3=0 ; j3<8 ; j3++ ){
				j1 = j2*v02xd + j3*2;
				WORD( old_ad + y*v02xd + x*2 + j1 )
				= WORD( ycsub + ky*v02xd + kx*2 + j1 );
			    }
			}
		    }
		    else {
			for( j2=0 ; j2<8 ; j2++ ){
			    for( j3=0 ; j3<8 ; j3++ ){
				j1 = j2*xhf + j3 + v02xd*v02yd;
				BYTE( old_ad + (y-v02yd)*xhf + x+j1 )
				= BYTE( ycsub +(ky-v02yd)*xhf+kx+j1);
			    }
			}
		    }
		}
	mov02:  ;
	    }
	}
	DWORD( data2_buf+p2 ) = n;
	if( n == 0 )i = p2;
	DWORD( data2_buf+p1 ) = i - p2;
	return i;
}

	/* サブ */
/* movsb */
cnv_movsb_0x2104( i, rate, data2_buf, data1_buf, make_buf, check_buf )
int i, rate;
char *data1_buf, *make_buf, *check_buf, *data2_buf;
{
	int p1, p6;
	int j, j3, j4;
	int k, k1, k2, last, kankaku;
	int a1, a2;
	int n3;
	int total;

	total = v02xd * v02yd * 3 / 4 ;

	WORD( data2_buf+i ) = 0x2104; i+=2;	/* movsb brock */
	p1 = i; i+=4;			/* size point */
	p6 = i; i+=4;		/* d-address count point */
	n3 = 0;			/* d-address count */
	k = 0; k1 = 0;		/* data長count係数 */
	last = 0;
	for( j3=0 ; j3<total ; j3++ ){
		a1 = BYTE( data1_buf + j3 );
		a2 = BYTE( check_buf + j3 );
		if( a2 == 0 ){
		    k++;
		    BYTE( check_buf + j3 ) = 1;

//			j = ( (j3/(v02xd/2)) << 9 ) + ( (j3%(v02xd/2)) << 1 );
//			xorw( j+(v02xd*2), 0x104, 0xffff );

		    BYTE( make_buf + j3 ) = a1;
		    if( k == 1 ){
			n3++;
			k2 = j3;			/* address記憶 */
			kankaku = j3 - last;
			if( kankaku<0xff ){
			    BYTE( data2_buf+i ) = kankaku;
			    i += 1;
			}
			if( (kankaku>=0xff) && (kankaku<0xffff) ){
			    BYTE( data2_buf+i ) = 0xff;
			    WORD( data2_buf+i+1 ) = kankaku;
			    i += 3;
			}
			if( kankaku>=0xffff ){
			    BYTE( data2_buf+i ) = 0xff;
			    WORD( data2_buf+i+1 ) = 0xffff;
			    DWORD( data2_buf+i+3 ) = kankaku;
			    i += 7;
			}
		    }
		    if( k<0xff ){
			BYTE( data2_buf+i ) = k;
			k1 = 1;
		    }
		    if( (k>=0xff) && (k<0xffff) ){
			BYTE( data2_buf+i ) = 0xff;
			WORD( data2_buf+i+1 ) = k;
			k1 = 3;
		    }
		    if( k>=0xffff ){
			BYTE( data2_buf+i ) = 0xff;
			WORD( data2_buf+i+1 ) = 0xffff;
			DWORD( data2_buf+i+3 ) = k;
			k1 = 7;
		    }
		}
		else {
		    if( k ){
			i = i + k1;
			k = 0; k1 = 0;
			last = j3;
			for( j4=k2 ; j4<j3 ; j4++ ){ /* data write */
			    BYTE( data2_buf+i ) = BYTE( make_buf+j4 );
			    i++;
			}
		    }
		}
	movs01: ;
	}
movs02: if( k ){
		i = i + k1;
		k = 0; k1 = 0;
		for( j4=k2 ; j4<j3 ; j4++ ){ /* data write */
		    BYTE( data2_buf+i ) = BYTE( make_buf+j4 );
		    i++;
		}
	}
	DWORD( data2_buf+p6 ) = n3;
	if( n3 == 0 )i = p6;
	DWORD( data2_buf+p1 ) = i - p6;
	return i;
}

/* skip_check1 = 1:skip ( for stosb ) */

	/* サブのサブ(4つが一番よい結果がでる1993 7 ) */
cnv_stosb_skip_check4( data, n, rate, data1_buf, check_buf )
int data, n, rate;
char *data1_buf, *check_buf;
{			/* 4つdataと同じ色が続くもの以外はスキップ */
	int a, i;
	int total;

	total = v02xd * v02yd * 3 / 4 ;
	if( n > total-4 )
		return 1;
		/* ↓2つ続きの処理済直後はok */
	if( ( n > 1 )
	 && ( (DWORD( check_buf+n-2 ) & 0x00ffffff )== 0x000101 ) )
		return 0;
	if( BYTE( check_buf+n ) )return 1;
	for( i=0 ; i<4 ; i++ ){
		if( BYTE( check_buf + n + i ) )
		{
			if( i > 1 )
				return 0 ;
			if( WORD( check_buf+n+i ) == 0x0101 )
				return 0 ;
		}
		a = BYTE( data1_buf + n + i );
		if( cnv_cmp_data( data, a, n+i, rate ) )return 1;
	}
	return 0;
}

	/* サブのサブ */
cnv_stosb_skip_check( k, data, n, rate, data1_buf, check_buf )
int k, data, n, rate;
char *data1_buf, *check_buf;
{			/* k個のdataと同じ色が続くもの以外はスキップ */
	int a, i;
	int total;

	total = v02xd * v02yd * 3 / 4 ;
	if( n > total-k )return 1;
	for( i=0 ; i<k ; i++ )
	{
		if( BYTE( check_buf + n + i ) )
			return 1;
		a = BYTE( data1_buf + n + i );
		if( cnv_cmp_data( data, a, n+i, rate ) )
			return 1;
	}
	return 0;
}

	/* サブ */
/* stosb */
cnv_stosb_0x1104( i, rate, data2_buf, data1_buf, make_buf, check_buf )
int i, rate;
char *data1_buf, *make_buf, *check_buf, *data2_buf;
{
	int p1, p2, p3, p6;
	int j, j1, j3;
	int k, k1, last, kankaku;
	int data;
	int a1, a2;
	int n1, n3;
	int total;

	int zentai_kaisu, renzoku ;

	total = v02xd * v02yd * 3 / 4 ;
	WORD( data2_buf+i ) = 0x1104; i+=2;	/* stosb brock */
	p1 = i; i+=4;			/* size point */
	p2 = i; i+=4;			/* data count point */
	n1 = 0;				/* data count */

	renzoku = 8 ;

	for( zentai_kaisu=0 ; zentai_kaisu<3 ; zentai_kaisu++ )
	{

	  for( j1=0 ; j1<total ; j1++ ){
	    if( BYTE( check_buf+j1 ) )goto stos03;	/* 処理済skip */
	    data = BYTE( data1_buf+j1 );
	    if(
	      cnv_stosb_skip_check(
	       renzoku, data, j1, rate, data1_buf, check_buf )
	    )goto stos03;		/* skip check */
	    p3 = i;			/* data top point */
	    BYTE( data2_buf+i ) = data; i+=1;	/* data */
	    p6 = i; i+=4;		/* d-address count point(DWORD) */
	    n3 = 0;			/* d-address count */
	    k = 0; k1 = 0;		/* data長count係数 */
	    last = 0;			/* 間隔check用 */
	    for( j3=0 ; j3<total ; j3++ ){
		a2 = BYTE( check_buf+j3 );
		if( a2 ){			/* 処理済skip */
		    if( k ){
			last = j3;
			i = i + k1;
			k = 0; k1 = 0;
		    }
		    goto stos01;
		}
		a1 = BYTE( data1_buf + j3 );
		if( a2 == 0				 /* 手つかず */
		    && cnv_cmp_data( data,a1,j3,rate ) == 0 
							/* 同色と認め */ 
		){
		    if( k == 0 )if( 
		 	cnv_stosb_skip_check4(
		 	   data, j3, rate, data1_buf, check_buf
		 	)
		    )goto stos01;
		    k++;
		    BYTE( make_buf + j3 ) = data;
		    BYTE( check_buf + j3 ) = 1;

//			j = ( (j3/(v02xd/2)) << 9 ) + ( (j3%(v02xd/2)) << 1 );
//			xorw( j+(v02xd*2), 0x104, 0xffff );

		    if( k == 1 ){
			n3++;
			kankaku = j3 - last;
			if( kankaku<0xff ){
			    BYTE( data2_buf+i ) = kankaku;
			    i += 1;
			}
			if( (kankaku>=0xff) && (kankaku<0xffff) ){
			    BYTE( data2_buf+i ) = 0xff;
			    WORD( data2_buf+i+1 ) = kankaku;
			    i += 3;
			}
			if( kankaku>=0xffff ){
			    BYTE( data2_buf+i ) = 0xff;
			    WORD( data2_buf+i+1 ) = 0xffff;
			    DWORD( data2_buf+i+3 ) = kankaku;
			    i += 7;
			}
		    }
		    if( k<0xff ){
			BYTE( data2_buf+i ) = k;
			k1 = 1;
		    }
		    if( (k>=0xff) && (k<0xffff) ){
			BYTE( data2_buf+i ) = 0xff;
			WORD( data2_buf+i+1 ) = k;
			k1 = 3;
		    }
		    if( k>=0xffff ){
			BYTE( data2_buf+i ) = 0xff;
			WORD( data2_buf+i+1 ) = 0xffff;
			DWORD( data2_buf+i+3 ) = k;
			k1 = 7;
		    }
		}
		else {
		    if( k ){
			last = j3;
			i = i + k1;
			k = 0; k1 = 0;
		    }
		}
	stos01: ;
	    }
    stos02: i = i + k1;
	    DWORD( data2_buf+p6 ) = n3;
	    if( n3 ){
		n1++;
	    }
	    else i = p3;
    stos03: ;
	  }

	  renzoku-- ;

	}

	DWORD( data2_buf+p2 ) = n1;
	if( n1 == 0 )i = p2;
	DWORD( data2_buf+p1 ) = i - p2;
	return i;
}

v00_to_v02_soft( soft, old_ad, new_ad )
int soft ;
char  *old_ad, *new_ad;
{
	int x, y, xk, yk, data1, data2, n, n2, add;
	int total;
	int xhf, yhf;	/* v02xd, v02ydの半分の値 */
	int sft1, sft2, sft3, s ;

	int sft[][10] = {
				{ 0,0,0, 0,1,0, 0,0,0 },
				{ 0,0,0, 1,1,1, 0,0,0 },
//				{ 1,2,1, 2,2,2, 1,2,1 },
				{ 1,2,1, 1,2,1, 1,2,1 },
				{ 1,1,1, 1,1,1, 1,1,1 },
	};
	int s1[] = { 0, 1, 2 };	/* Y */
	int s2[] = { 0, 1, 3 };	/* Cr */
	int s3[] = { 0, 1, 3 };	/* Cb */

	if( soft > 2 )soft = 2;
	if( soft < 0 )soft = 0;
	sft1 = s1[ soft ];
	sft2 = s2[ soft ];
	sft3 = s3[ soft ];

	total = v02xd * v02yd ;
	xhf = v02xd >> 1;
	yhf = v02yd >> 1;
	for( y=0 ; y<v02yd ; y++ ){	/* 輝度 */
	    for( x=0 ; x<v02xd ; x++ ){
		data1 = 0;
		n = 0;
		s = 0 ;

		for( yk=y-1 ; yk<y+2 ; yk++ ){
		    for( xk=x-1 ; xk<x+2 ; xk++ ){

			if( yk>=0 && yk<v02yd && xk>=0 && xk<v02xd ){
			    add = yk*v02xd + xk;
			    data1 = data1 + BYTE( new_ad + add )*sft[sft1][s];
			    n = n + sft[sft1][s] ;
			}
			s++ ;

		    }
		}
		if( n )data1 = ( data1 + (data1 % n ) ) / n;
		add = y*v02xd + x;
		BYTE( old_ad + add ) = data1;
	    }
	}

	for( y=0 ; y<yhf ; y++ ){	/* 色相 */
	    for( x=0 ; x<xhf ; x++ ){
		data1 = 0; data2 = 0;
		n = 0 ;
		n2 = 0 ;
		s = 0 ;

		for( yk=y-1 ; yk<y+2 ; yk++ ){
		    for( xk=x-1 ; xk<x+2 ; xk++ ){

			if( yk>=0 && yk<yhf && xk>=0 && xk<xhf ){
			    add = total + yk*xhf + xk;
			    data1 = data1
			    	    + ( BYTE( new_ad + add )
			    	    % (RED_DIVI + GRE_DIVI) ) * sft[sft2][s];
			    data2 = data2
			            + ( BYTE( new_ad + add )
			    	    / (RED_DIVI + GRE_DIVI) ) * sft[sft3][s];
			    n = n + sft[sft2][s] ;
			    n2 = n2 + sft[sft3][s] ;
			}
			s++;

		    }
		}
		if( n ){
			data1 = ( data1 + (data1 % n ) ) / n;
			data2 = ( data2 + (data2 % n2 ) ) / n2;
		}
		add = total + y*xhf + x;
		if( data1 + (RED_DIVI + GRE_DIVI)*data2 > 255 )data2-- ;
		BYTE( old_ad + add ) = data1 + (RED_DIVI + GRE_DIVI)*data2 ;
	    }
	}
	return 0;
}

/* フィルターに使用 */
v00_to_v02_filter( flt, new_ad, old_ad )
int flt;
char *new_ad, *old_ad;
{
	int i, d0, d1, cr0, cb0, cr1, cb1, flt1, flt2, flt3 ;
	int total, bf, rf ;
	int filt[][10] = {
				{ 0,1,2,3,4,5,6,7,8,9 },
				{ 0,0,2,3,4,5,6,7,8,9 },
				{ 0,0,0,2,4,5,6,7,8,9 },
	};
	int f1[] = { 0, 0, 0, 1, 1, 1, 1, 1, 2 };	/* Y */
	int f2[] = { 0, 0, 1, 1, 1, 1, 1, 1, 1 };	/* Cr */
	int f3[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1 };	/* Cb */

	total = v02xd * v02yd ;

	if( flt > 8 )flt = 8;
	flt1 = f1[ flt ];
	flt2 = f2[ flt ];
	flt3 = f3[ flt ];
	if( flt ){
	    for( i=0 ; i<total ; i++ ){
		d0 = BYTE( new_ad + i );
		d1 = BYTE( old_ad + i );
		if( (d0-d1 >= 0) && (d0-d1 <= 8) )
		{
		    d0 = d1 + filt[ flt1 ][ d0-d1 ];
		}
		if( (d1-d0 > 0) && (d1-d0 <= 8) )
		{
		    d0 = d1 - filt[ flt1 ][ d1-d0 ];
		}
		BYTE( new_ad + i ) = d0;
	    }
	    for( i=total ; i<(total + total/4) ; i++ ){

		d0 = BYTE( new_ad + i );
		d1 = BYTE( old_ad + i );
		cr0 = d0 % (RED_DIVI + GRE_DIVI) ;
		cr1 = d1 % (RED_DIVI + GRE_DIVI) ;
		cb0 = d0 / (RED_DIVI + GRE_DIVI) ;
		cb1 = d1 / (RED_DIVI + GRE_DIVI) ;

		/* cr0<=GRE_DIVI-1等は色が薄くなるのを防ぐ */

		bf = 0 ;
		if(
		 (cb0-cb1 >= 0) && (cb0-cb1 <= 8)
		 && ((flt >= 5) || (cb0 <= YEL_DIVI-1))
//		 && ((flt >= 5) || (cb0 <= YEL_DIVI))
		  )
			bf = 1 ;
		if(
		 (cb1-cb0 > 0) && (cb1-cb0 <= 8)
		 && ((flt >= 4) || (cb0 >= YEL_DIVI))
//		 && ((flt >= 4) || (cb0 >= YEL_DIVI-1))
		  )
			bf = -1 ;
		rf = 0 ;
		if(
		 (cr0-cr1 >= 0) && (cr0-cr1 <= 8)
		 && ((flt >= 6) || (cr0 <= GRE_DIVI-1))
		  )
		 	rf = 1 ;
		if(
		 (cr1-cr0 > 0) && (cr1-cr0 <= 8)
		 && ((flt >= 7) || (cr0 >= GRE_DIVI))
//		 && ((flt >= 7) || (cr0 >= GRE_DIVI-1))
		  )
		 	rf = -1 ;

		if( bf > 0 && rf )
			cb0 = cb1 + filt[ flt3 ][ cb0-cb1 ];
		if( bf < 0 && rf )
			cb0 = cb1 - filt[ flt3 ][ cb1-cb0 ];

		if( rf > 0 && bf )
			cr0 = cr1 + filt[ flt2 ][ cr0-cr1 ];
		if( rf < 0 && bf )
			cr0 = cr1 - filt[ flt2 ][ cr1-cr0 ];

		if( cr0 + (RED_DIVI + GRE_DIVI)*cb0 > 255 )cb0-- ;
		BYTE( new_ad + i ) = cr0 + (RED_DIVI + GRE_DIVI)*cb0 ;
	    }
	}
	return 0;
}

/* ソフトネスに使用 */
v00_to_v02_s_filter( flt, new_ad, old_ad )
int flt;
char *new_ad, *old_ad;
{
	int i, d0, d1, cr0, cb0, cr1, cb1, flt1, flt2, flt3;
	int total;
	int filt[][10] = {
				{ 0,1,2,3,4,5,6,7,8,9 },
				{ 0,0,2,3,4,5,6,7,8,9 },
				{ 0,0,1,3,4,5,6,7,8,9 },
				{ 0,0,0,2,4,5,6,7,8,9 },
				{ 0,0,0,0,2,4,6,7,8,9 },
				{ 0,0,0,0,0,2,4,6,8,9 },
				{ 0,0,0,0,0,0,2,4,6,8 },
				{ 0,0,0,0,0,0,0,2,4,7 },
				{ 0,0,0,0,0,0,0,0,3,7 },
				{ 0,0,0,0,0,0,0,0,0,4 },
	};
	int f1[] = { 0, 0, 1, 2, 3, 4, 5, 6, 7 };
	int f2[] = { 0, 1, 1, 2, 3, 4, 5, 6, 7 };
	int f3[] = { 0, 1, 1, 2, 3, 4, 5, 6, 7 };

	total = v02xd * v02yd ;
	if( flt > 8 )flt = 8;
	flt1 = f1[ flt ];
	flt2 = f2[ flt ];
	flt3 = f3[ flt ];
	if( flt ){
	    for( i=0 ; i<total ; i++ ){
		d0 = BYTE( new_ad + i );
		d1 = BYTE( old_ad + i );
		if( (d0-d1 >= 0) && (d0-d1 <= 8) ){
		    d0 = d1 + filt[ flt1 ][ d0-d1 ];
		}
		if( (d1-d0 > 0) && (d1-d0 <= 8) ){
		    d0 = d1 - filt[ flt1 ][ d1-d0 ];
		}
		BYTE( new_ad + i ) = d0;
	    }
	    for( i=total ; i<(total + total/4) ; i++ ){
		d0 = BYTE( new_ad + i );
		d1 = BYTE( old_ad + i );
		cr0 = d0 % (RED_DIVI + GRE_DIVI) ;
		cr1 = d1 % (RED_DIVI + GRE_DIVI) ;
		cb0 = d0 / (RED_DIVI + GRE_DIVI) ;
		cb1 = d1 / (RED_DIVI + GRE_DIVI) ;

		if( (cr0-cr1 >= 0) && (cr0-cr1 <= 8) )
			cr0 = cr1 + filt[ flt2 ][ cr0-cr1 ];
		if( (cr1-cr0 > 0) && (cr1-cr0 <= 8) )
			cr0 = cr1 - filt[ flt2 ][ cr1-cr0 ];

		if( (cb0-cb1 >= 0) && (cb0-cb1 <= 8) )
			cb0 = cb1 + filt[ flt3 ][ cb0-cb1 ];
		if( (cb1-cb0 > 0) && (cb1-cb0 <= 8) )
			cb0 = cb1 - filt[ flt3 ][ cb1-cb0 ];

		if( cr0 + (RED_DIVI + GRE_DIVI)*cb0 > 255 )cb0-- ;
		BYTE( new_ad + i ) = cr0 + (RED_DIVI + GRE_DIVI)*cb0 ;
	    }
	}
	return 0;
}

	/* サブ(瞬間圧縮) */
v00_to_v02_conv_cmp3
(
flt, rate, step, soft, mode,
data2_buf, new_ad, old_ad, data1_buf, make_buf, check_buf
)
int flt, rate, step, soft, mode;
char *data2_buf, *new_ad, *old_ad, *data1_buf, *make_buf, *check_buf;
{
	int i, j, k ;

		/* mono */
	if( mode )
		for( k=v02xd*v02yd ; k<( v02xd*v02yd + v02xd*v02yd/4 ) ; k++ )
			BYTE( new_ad + k )
			 = GRE_DIVI + (RED_DIVI + GRE_DIVI)*YEL_DIVI ;

	v00_to_v02_conv_yc_to_cmpyc( data1_buf, new_ad );

	i = 0;					/* data counter */

	WORD( data2_buf + 0 ) = 1 ;
	WORD( data2_buf + 2 ) = 0x2104 ;
	DWORD( data2_buf + 4 ) = 8 + v02xd*v02yd*2*3/8 ;
	DWORD( data2_buf + 8 ) = 1 ;
	BYTE( data2_buf + 12 ) = 0 ;
	BYTE( data2_buf + 13 ) = 0xff ;
	WORD( data2_buf + 14 ) = v02xd*v02yd*2*3/8 ;

	i += 16 ;

	for( j=0 ; j<(v02xd*v02yd*2*3/8) ; j++ )
	{
		BYTE( data2_buf+16 + j ) = BYTE( data1_buf + j ) ;
		BYTE( make_buf + j ) = BYTE( data1_buf + j ) ;
	}

	i += (v02xd*v02yd*2*3/8) ;

	return i;
}

	/* サブ */
v00_to_v02_conv_cmp2
(
flt, rate, step, soft, mode,
data2_buf, new_ad, old_ad, data1_buf, make_buf, check_buf
)
int flt, rate, step, soft, mode;
char *data2_buf, *new_ad, *old_ad, *data1_buf, *make_buf, *check_buf;
{
	int i, j, k, a1, a2;
	int total;

	char *new_ad_2 ;
	char *new_ad_3 ;
	char *new_ad_4 ;

	new_ad_2 = new_ad   + v02xd * v02yd * 5 / 4 ;
	new_ad_3 = new_ad_2 + v02xd * v02yd * 5 / 4 ;
	new_ad_4 = new_ad_3 + v02xd * v02yd * 5 / 4 ;
			/* new_adにはYCデータが4つ入らなければいけない */

	total = v02xd * v02yd * 3 / 4 ;

		/* 画面clear */
//	for( k=0 ; k<256 ; k++ ){
//		for( j=v02xd ; j<256 ; j++ ){
//			pokew( k*512+j*2, 0x104, 0 );
//		}
//	}
		/* check area clear */
	for( k=0 ; k<total ; k+=4 ){
		DWORD( check_buf+k ) = 0;	/* clear */
	}
		/* mono */
	if( mode )
		for( k=v02xd*v02yd ; k<( v02xd*v02yd + v02xd*v02yd/4 ) ; k++ )
			BYTE( new_ad + k )
			 = GRE_DIVI + (RED_DIVI + GRE_DIVI)*YEL_DIVI ;

		/* 綺麗なycをnew_ad_2に保存 */
	for( k=0 ; k<( v02xd * v02yd * 5 / 4 ) ; k++ )
		BYTE( new_ad_2 + k ) = BYTE( new_ad + k ) ;

		/* new_ad_4を使ってsoft→new_ad */
	v00_to_v02_soft( 2, new_ad_4, new_ad );
	v00_to_v02_s_filter( soft, new_ad, new_ad_4 );


		/* 綺麗なycをnew_ad_3に転送 */
	for( k=0 ; k<( v02xd * v02yd * 5 / 4 ) ; k++ )
		BYTE( new_ad_3 + k ) = BYTE( new_ad_2 + k ) ;
		/* new_ad_4を使ってsoft→new_ad_3 */
	v00_to_v02_soft( 1, new_ad_4, new_ad_3 );
	v00_to_v02_s_filter( soft, new_ad_3, new_ad_4 );

		/*
		 ↑以上の処理で
			new_ad   : ソフト係数2のソフトネス
			new_ad_2 : 原画
			new_ad_3 : ソフト係数1のソフトネス
			new_ad_4 : ソフト係数1の為のワーク(強いソフト)
		  ただしソフト係数1は横方向のみのソフト
	//	        ソフト係数2は全方向のソフト化
		        ソフト係数2は縦方向のソフト化
		*/

	i = 0;				/* data counter */
	WORD( data2_buf+i ) = 2;
	if( step )WORD( data2_buf+i ) += 1;	/* 移動補償 */
	i+=2;
	v00_to_v02_conv_yc_to_cmpyc( data1_buf, new_ad );
	if( step ){				/* 移動補償 */
		i = cnv_movebox8
		( i, flt, rate, step, data2_buf, new_ad, old_ad,
		data1_buf, make_buf, check_buf );
	}
	v00_to_v02_filter( flt, new_ad, old_ad );
	v00_to_v02_conv_yc_to_cmpyc( data1_buf, new_ad );

		/* checkのやり直し */
	for( k=0 ; k<total ; k++ ){
		a1 = BYTE( data1_buf+k ); a2 = BYTE( make_buf+k );
		if( cnv_cmp_data( a1,a2,k,rate ) == 0 ){
			BYTE( check_buf+k ) = 1;	/* 処理印 */
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0xffff );
		}
		else {
			BYTE( check_buf+k ) = 0;	/* 未処理 */
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0x0000 );
		}
	}
	for( k=1 ; k<total-1 ; k++ ){
		if( ( DWORD( check_buf+k-1 ) & 0x00ffffff ) == 0x000100
			||
		      BYTE( check_buf+k ) == 0
		){
			if( k < (v02xd * v02yd / 2) )
				WORD( new_ad+k*2 ) = WORD( new_ad_3+k*2 ) ;
			else
				BYTE( new_ad + k + v02xd*v02yd/2 )
				= BYTE( new_ad_3 + k + v02xd*v02yd/2 ) ;

			BYTE( check_buf+k ) = 0;
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0x0000 );
		}
	}
	v00_to_v02_conv_yc_to_cmpyc( data1_buf, new_ad );
				/* cmp. YCデータの計算し直し */

		/* stosb */
	i = cnv_stosb_0x1104(
	 i, 0, data2_buf, data1_buf, make_buf, check_buf );

		/* checkのやり直し */
	for( k=0 ; k<total ; k++ ){
		a1 = BYTE( data1_buf+k ); a2 = BYTE( make_buf+k );
		if( cnv_cmp_data( a1,a2,k,rate ) == 0 ){
			BYTE( check_buf+k ) = 1;	/* 処理印 */
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0xffff );
		}
		else {
			if( k < (v02xd * v02yd / 2) )
				WORD( new_ad+k*2 ) = WORD( new_ad_2+k*2 ) ;
			else
				BYTE( new_ad + k + v02xd*v02yd/2 )
				= BYTE( new_ad_2 + k + v02xd*v02yd/2 ) ;
					/* 処理解除部分のnew_adはnew_ad_2 */

			BYTE( check_buf+k ) = 0;	/* 未処理 */
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0x0000 );
		}
	}
	v00_to_v02_conv_yc_to_cmpyc( data1_buf, new_ad );
				/* cmp. YCデータの計算し直し */

		/* checkのやり直し */
	for( k=0 ; k<total ; k++ ){
		a1 = BYTE( data1_buf+k ); a2 = BYTE( make_buf+k );
		if( cnv_cmp_data( a1,a2,k,rate ) == 0 ){
			BYTE( check_buf+k ) = 1;	/* 処理印 */
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0xffff );
		}
	}
		/* 孤立した点は処理解除 */
	for( k=1 ; k<total-1 ; k++ ){
		if( ( DWORD( check_buf+k-1 ) & 0x00ffffff ) == 0x000100
			||
		      DWORD( check_buf+k-1 ) == 0x00010100
			||
		      BYTE( check_buf+k ) == 0
		){
			if( DWORD( check_buf+k-1 ) == 0x01010001 )
			{
			    if( k < (v02xd * v02yd / 2) )
				WORD( new_ad+k*2 ) = WORD( new_ad_3+k*2 ) ;
			    else
				BYTE( new_ad + k + v02xd*v02yd/2 )
				= BYTE( new_ad_3 + k + v02xd*v02yd/2 ) ;
					/* 処理解除部分のnew_adはnew_ad_3 */
			}
			else
			{
			    if( k < (v02xd * v02yd / 2) )
				WORD( new_ad+k*2 ) = WORD( new_ad_2+k*2 ) ;
			    else
				BYTE( new_ad + k + v02xd*v02yd/2 )
				= BYTE( new_ad_2 + k + v02xd*v02yd/2 ) ;
					/* 処理解除部分のnew_adはnew_ad_2 */
			}

			BYTE( check_buf+k ) = 0;
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0x0000 );
		}
	}
	v00_to_v02_conv_yc_to_cmpyc( data1_buf, new_ad );
					/* cmp. YCデータの計算し直し */

		/* movsb */
	i = cnv_movsb_0x2104(
	 i, 0, data2_buf, data1_buf, make_buf, check_buf );

	for( k=0 ; k<( v02xd * v02yd * 5 / 4 ) ; k++ )
	{
		BYTE( old_ad + k ) = BYTE( new_ad + k ) ;
	}			/* 次の処理のためにnew_adをold_adに */

	return i;
}

	/* サブ */
v00_to_v02_conv_cmp1
(
flt, rate, step, soft, mode,
data2_buf, new_ad, old_ad, data1_buf, make_buf, check_buf
)
int flt, rate, step, soft, mode;
char *data2_buf, *new_ad, *old_ad, *data1_buf, *make_buf, *check_buf;
{
	int i, j, k, m1, m2, n, data, d, temp;
	int total, a1, a2 ;

	char *new_ad_2 ;
	char *new_ad_3 ;
	char *new_ad_4 ;

	new_ad_2 = new_ad   + v02xd * v02yd * 5 / 4 ;
	new_ad_3 = new_ad_2 + v02xd * v02yd * 5 / 4 ;
	new_ad_4 = new_ad_3 + v02xd * v02yd * 5 / 4 ;
			/* new_adにはYCデータが4つ入らなければいけない */

	total = v02xd * v02yd * 3 / 4 ;

//	for( i=0 ; i<256 ; i++ ){
//		for( j=v02xd ; j<256 ; j++ ){
//			pokew( i*512+j*2, 0x104, 0 );
//		}
//	}
		/* check area clear */
	for( k=0 ; k<total ; k+=4 ){
		DWORD( check_buf+k ) = 0;	/* clear */
	}
		/* mono */
	if( mode )
		for( k=v02xd*v02yd ; k<( v02xd*v02yd + v02xd*v02yd/4 ) ; k++ )
			BYTE( new_ad + k )
			 = GRE_DIVI + (RED_DIVI + GRE_DIVI)*YEL_DIVI ;

		/* 綺麗なycをnew_ad_2に保存 */
	for( k=0 ; k<( v02xd * v02yd * 5 / 4 ) ; k++ )
		BYTE( new_ad_2 + k ) = BYTE( new_ad + k ) ;
		/* new_ad_4を使ってsoft */
	v00_to_v02_soft( 2, new_ad_4, new_ad );
	v00_to_v02_s_filter( soft, new_ad, new_ad_4 );
		/* 綺麗なycをnew_ad_3に転送 */
	for( k=0 ; k<( v02xd * v02yd * 5 / 4 ) ; k++ )
		BYTE( new_ad_3 + k ) = BYTE( new_ad_2 + k ) ;
		/* new_ad_4を使ってsoft */
	v00_to_v02_soft( 1, new_ad_4, new_ad_3 );
	v00_to_v02_s_filter( soft, new_ad_3, new_ad_4 );

	v00_to_v02_conv_yc_to_cmpyc( data1_buf, new_ad );
	i = 0;					/* data counter */
	WORD( data2_buf+i ) = 3; i+=2;		/* brock 数 */

	m1 = v02xd * 8 / 2 ;
	m2 = total / m1;

	WORD( data2_buf+i ) = 0x1404; i+=2;	/* stosd brock */
	DWORD( data2_buf+i ) = 4+14*m2; i+=4;	/* data size */
	DWORD( data2_buf+i ) = m2; i+=4;		/* data 数 */

	for( j=0 ; j<m2 ; j++ ){
		d = j*m1;
			/* data2_buf+0x1000〜 を使って最多data検索 */

		if( j == m2 - 1 )
			m1 = total - d ;	/* 1993 4/8 修正 */

		for( k=0 ; k<256 ; k++ ){
			DWORD( data2_buf+0x1000+k*4 ) = 0;	/* clear */
		}
		for( k=d ; k<d+m1 ; k++ ){
			data = BYTE( data1_buf + k );
			DWORD( data2_buf+0x1000+data*4 ) +=1;
		}
		n = 0; data = 0;
		for( k=0 ; k<256 ; k++ ){
			if( DWORD( data2_buf+0x1000+k*4 ) > n ){
				n = DWORD( data2_buf+0x1000+k*4 );
				data = k;
			}
		}
			/* 塗り潰し */
		for( k=d ; k<d+m1 ; k++ ){
			BYTE( make_buf+k ) = data;
			if(  cnv_cmp_data(data,BYTE(data1_buf+k),k,rate) == 0 )
			{
				BYTE( check_buf+k ) = 1;

//				temp = ( (k/(v02xd/2)) << 9 )
//				     + ( (k%(v02xd/2)) << 1 );
//				pokew( temp+(v02xd*2), 0x104, 0xffff );
					/* 処理印 */
			}
		}
		DWORD( data2_buf+i )
		 = data+(data << 8)+(data << 16)+(data << 24); i+=4;
							 /* data */
		DWORD( data2_buf+i ) = 1; i+=4;		/* 個数 */
		BYTE( data2_buf+i ) = 0xff; i+=1;	/* 間隔 */
		WORD( data2_buf+i ) = d; i+=2;
		BYTE( data2_buf+i ) = 0xff; i+=1;	/* 長さ */
		WORD( data2_buf+i ) = m1 / 4; i+=2;
	}

		/* checkのやり直し */
	for( k=0 ; k<total ; k++ ){
		a1 = BYTE( data1_buf+k ); a2 = BYTE( make_buf+k );
		if( cnv_cmp_data( a1,a2,k,rate ) == 0 ){
			BYTE( check_buf+k ) = 1;	/* 処理印 */
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0xffff );
		}
		else {
			BYTE( check_buf+k ) = 0;	/* 未処理 */
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0x0000 );
		}
	}
	for( k=1 ; k<total-1 ; k++ ){
		if( ( DWORD( check_buf+k-1 ) & 0x00ffffff ) == 0x000100
			||
		      BYTE( check_buf+k ) == 0
		){
			if( k < (v02xd * v02yd / 2) )
				WORD( new_ad+k*2 ) = WORD( new_ad_3+k*2 ) ;
			else
				BYTE( new_ad + k + v02xd*v02yd/2 )
				= BYTE( new_ad_3 + k + v02xd*v02yd/2 ) ;

			BYTE( check_buf+k ) = 0;
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0x0000 );
		}
	}
	v00_to_v02_conv_yc_to_cmpyc( data1_buf, new_ad );
				/* cmp. YCデータの計算し直し */
		/* stosb */
	i = cnv_stosb_0x1104(
	 i, 0, data2_buf, data1_buf, make_buf, check_buf );

		/* checkのやり直し */
	for( k=0 ; k<total ; k++ ){
		a1 = BYTE( data1_buf+k ); a2 = BYTE( make_buf+k );
		if( cnv_cmp_data( a1,a2,k,rate ) == 0 ){
			BYTE( check_buf+k ) = 1;	/* 処理印 */
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0xffff );
		}
		else {
			if( k < (v02xd * v02yd / 2) )
				WORD( new_ad+k*2 ) = WORD( new_ad_2+k*2 ) ;
			else
				BYTE( new_ad + k + v02xd*v02yd/2 )
				= BYTE( new_ad_2 + k + v02xd*v02yd/2 ) ;
					/* 処理解除部分のnew_adはnew_ad_2 */

			BYTE( check_buf+k ) = 0;	/* 未処理 */
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0x0000 );
		}
	}
	v00_to_v02_conv_yc_to_cmpyc( data1_buf, new_ad );
				/* cmp. YCデータの計算し直し */

		/* checkのやり直し */
	for( k=0 ; k<total ; k++ ){
		a1 = BYTE( data1_buf+k ); a2 = BYTE( make_buf+k );
		if( cnv_cmp_data( a1,a2,k,rate ) == 0 ){
			BYTE( check_buf+k ) = 1;	/* 処理印 */
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0xffff );
		}
	}
		/* 孤立した点は処理解除 */
	for( k=1 ; k<total-1 ; k++ ){
		if( ( DWORD( check_buf+k-1 ) & 0x00ffffff ) == 0x000100
			||
		      DWORD( check_buf+k-1 ) == 0x00010100
			||
		      BYTE( check_buf+k ) == 0
		){
			if( DWORD( check_buf+k-1 ) == 0x01010001 )
			{
			    if( k < (v02xd * v02yd / 2) )
				WORD( new_ad+k*2 ) = WORD( new_ad_3+k*2 ) ;
			    else
				BYTE( new_ad + k + v02xd*v02yd/2 )
				= BYTE( new_ad_3 + k + v02xd*v02yd/2 ) ;
					/* 処理解除部分のnew_adはnew_ad_3 */
			}
			else
			{
			    if( k < (v02xd * v02yd / 2) )
				WORD( new_ad+k*2 ) = WORD( new_ad_2+k*2 ) ;
			    else
				BYTE( new_ad + k + v02xd*v02yd/2 )
				= BYTE( new_ad_2 + k + v02xd*v02yd/2 ) ;
					/* 処理解除部分のnew_adはnew_ad_2 */
			}

			BYTE( check_buf+k ) = 0;
//			j = ( (k/(v02xd/2)) << 9 ) + ( (k%(v02xd/2)) << 1 );
//			pokew( j+(v02xd*2), 0x104, 0x0000 );
		}
	}
	v00_to_v02_conv_yc_to_cmpyc( data1_buf, new_ad );
					/* cmp. YCデータの計算し直し */
		/* movsb */
	i = cnv_movsb_0x2104(
	 i, 0, data2_buf, data1_buf, make_buf, check_buf );

	for( k=0 ; k<( v02xd * v02yd * 5 / 4 ) ; k++ )
	{
		BYTE( old_ad + k ) = BYTE( new_ad + k ) ;
	}			/* 次の処理のためにnew_adをold_adに */

	return i;
}

	/* サブ */
/* Y領域,C領域考慮して比較(ただしC領域は単純な比較のみ) 0:same 1:def. */

cnv_cmp_data( a, b, n, rate )
int a, b, n, rate;
{
	if( a == b )return 0;
	return 1 ;
}

	/* Yデータ作成のための変数の型の宣言 */

typedef struct
{
	int		start ;		/* 開始位置 */
	int		step ;		/* データ幅 */
	int		n ;			/* data総数 */
	int		d ;			/* 2dataの間の差 */
	int		p ;			/* 次のデータとの差(d)の境界線 */
	int		min ;		/* 守備範囲最小値 */
	int		max ;		/* 守備範囲最大値 */
	int		offset1 ;	/* offset1 */
	int		offset2 ;	/* offset2 */
} YDATA ;

static YDATA	ytpara[20] ;

/*
  d = 2data間の差, 
  step = ステップ, 
  n = data総数,
  offset1 = dataの存在するoffset1,
  offset2 = dataの存在するoffset2,
  start = 開始位置adjust,
  なおstartはY領域(0〜63)のデータの始まりの位置
	例えば 2data間の差=5, ステップ=3, data総数=38, start=2ならばデータは
	(2,7), (5,10), (8,13), … ,(56,61) [これはoffset1に収納されるデータ]
	(7,2), (10,5), (13,8), … ,(61,56) [これはoffset2に収納されるデータ]
*/

static int ytotal ;
static char ycnvtable[4096] ;

/*
   ytableテーブル( ｢8bit符号｣を｢6bit+6bitのy｣に変換するもの ), 
   ycnvtableテーブル( ｢6bit+6bitのy｣を｢8bit符号｣に変換するもの )の作成
*/
v00_to_v02_conv_ytable_set( ytable, ycnvtable )
char *ytable, *ycnvtable ;
{
	int d0, d1, d2, g, i, j, n, out, temp ;

	ytpara[0].start = 0 ; ytpara[0].step = 1 ; ytpara[0].n = 64 ;
	ytpara[0].d = 0 ; ytpara[0].p = 1 ;
	ytpara[0].min = 0 ; ytpara[0].max = 63 ;

	ytpara[1].start = 0 ; ytpara[1].step = 3 ; ytpara[1].n = 42 ;
	ytpara[1].d = 1 ; ytpara[1].p = 2 ;
	ytpara[1].min = 0 ; ytpara[1].max = 63 ;

	ytpara[2].start = 1 ; ytpara[2].step = 3 ; ytpara[2].n = 42 ;
	ytpara[2].d = 2 ; ytpara[2].p = 3 ;
	ytpara[2].min = 0 ; ytpara[2].max = 63 ;

	ytpara[3].start = 1 ; ytpara[3].step = 3 ; ytpara[3].n = 40 ;
	ytpara[3].d = 5 ; ytpara[3].p = 7 ;
	ytpara[3].min = 0 ; ytpara[3].max = 63 ;

	ytpara[4].start = 2 ; ytpara[4].step = 3 ; ytpara[4].n = 36 ;
	ytpara[4].d = 10 ; ytpara[4].p = 12 ;
	ytpara[4].min = 0 ; ytpara[4].max = 63 ;

	ytpara[5].start = 3 ; ytpara[5].step = 3 ; ytpara[5].n = 32 ;
	ytpara[5].d = 15 ; ytpara[5].p = 63 ;
	ytpara[5].min = 0 ; ytpara[5].max = 63 ;

	ytotal = 6 ;


		/* ytableテーブルの作成 */
	n = 0 ;
	/* 0番目は例外処理 */
	ytpara[0].offset1 = n ; ytpara[0].offset2 = n ;
	for( i=0 ; i<ytpara[0].n ; i++ )
	{
		d1 = i ;
		d2 = d1 ;
		WORD( ytable + 2*n ) = d1 + (d2 << 8) ;
		n++ ;
	}

	for( j=1 ; j<ytotal ; j++ )
	{
		ytpara[j].offset1 = n ;
		for( i=0 ; i<(ytpara[j].n/2) ; i++ )
		{
			d1 = i*ytpara[j].step +ytpara[j].start ;
			d2 = d1 + ytpara[j].d ;
			if( d1 > 63 )d1 = 63 ;
			if( d2 > 63 )d2 = 63 ;
			WORD( ytable + 2*n ) = d1 + (d2 << 8) ;
			n++ ;
		}

		ytpara[j].offset2 = n ;
		for( i=0 ; i<(ytpara[j].n/2) ; i++ )
		{
			d2 = i*ytpara[j].step +ytpara[j].start ;
			d1 = d2 + ytpara[j].d ;
			if( d1 > 63 )d1 = 63 ;
			if( d2 > 63 )d2 = 63 ;
			WORD( ytable + 2*n ) = d1 + (d2 << 8) ;
			n++ ;
		}
	}

		/* ycnvtableテーブルの作成 */
	for( i=0 ; i<4096 ; i++ )
	{
		int ds, dmin, dmax ;

		d1 = i & 0x3f ;
		d2 = (i >> 6) & 0x3f ;

		if( d1 > d2 )
		{
			ds = d1 - d2 ;
			dmin = d2 ;
			dmax = d1 ;
		}
		else
		{
			ds = d2 - d1 ;
			dmin = d1 ;
			dmax = d2 ;
		}

		if( ds == 0 )
		{
			v00_to_v02_conv_ytable_set_sub(d1,d2, 0, &d0,&g,&out) ;
		}

		if( ds == 1 )
		{
			if( (dmin % 3) == 0 )
			{
				v00_to_v02_conv_ytable_set_sub(d1,d2, 1, &d0,&g,&out) ;
				if( out )
					v00_to_v02_conv_ytable_set_sub(d1,d2, 0, &d0,&g,&out) ;
			}
			if( (dmin % 3) == 1 )
				v00_to_v02_conv_ytable_set_sub(d1,d2, 0, &d0,&g,&out) ;
			if( (dmin % 3) == 2 )
				v00_to_v02_conv_ytable_set_sub(d1,d2, 0, &d0,&g,&out) ;
		}

		if( ds == 2 )
		{
			if( (dmin % 3) == 0 )
			{
				v00_to_v02_conv_ytable_set_sub(d1,d2, 1, &d0,&g,&out) ;
				if( out )
					v00_to_v02_conv_ytable_set_sub(d1,d2, 0, &d0,&g,&out) ;
			}
			if( (dmin % 3) == 1 )
				v00_to_v02_conv_ytable_set_sub(d1,d2, 2, &d0,&g,&out) ;
			if( (dmin % 3) == 2 )
			{
				v00_to_v02_conv_ytable_set_sub(d1,d2, 1, &d0,&g,&out) ;
				if( out )
					v00_to_v02_conv_ytable_set_sub(d1,d2, 0, &d0,&g,&out) ;
			}
		}

		if( ds == 3 )
		{
			if( (dmin % 3) == 0 )
				v00_to_v02_conv_ytable_set_sub(d1,d2, 2, &d0,&g,&out) ;
			if( (dmin % 3) == 1 )
				v00_to_v02_conv_ytable_set_sub(d1,d2, 2, &d0,&g,&out) ;
			if( (dmin % 3) == 2 )
				v00_to_v02_conv_ytable_set_sub(d1,d2, 1, &d0,&g,&out) ;
		}

		if( ds == 4 )
		{
			if( (dmin % 3) == 0 )
				v00_to_v02_conv_ytable_set_sub(d1,d2, 2, &d0,&g,&out) ;
			if( (dmin % 3) == 1 )
				v00_to_v02_conv_ytable_set_sub(d1,d2, 3, &d0,&g,&out) ;
			if( (dmin % 3) == 2 )
				v00_to_v02_conv_ytable_set_sub(d1,d2, 3, &d0,&g,&out) ;
		}

		if( ds >= 5 )
		{
			for( j=3 ; j<ytotal ; j++ )
			{
			    temp = v00_to_v02_conv_ytable_set_sub(d1,d2, j, &d0,&g,&out);
		    	if( temp )
		    	{
		    		if( out )
		    		{
		    			for( j=j ; j>=4 ; j-- )
		    			{
		    				v00_to_v02_conv_ytable_set_sub(d1,d2, j,
															&d0,&g,&out) ;
							if( out == 0 )
								break ;
						}
					}
					break ;
				}
		    }
		}

		BYTE( ycnvtable + i ) = d0 ;
	}

	return 0 ;
}

	/* サブのサブ */
/*
	入力 int d1, d2; 輝度データ
	     int j;      順番(圧縮輝度データのグループ番号)
	     int *p;     輝度圧縮データ戻り値のポインター
	     int *g;     逆断層の定量用のポインター
	     int *out;	 0:守備範囲内 1:守備班以外

	出力 p           輝度圧縮データ戻り値
	     g			 逆断層の量
	     out;		 0:守備範囲内 1:守備班以外
	     return      0:番外
		     		 1:2つのデータの差がヒット

	(注意:番外の意味は指定された圧縮輝度データのグループ番号に当該なし)
*/
v00_to_v02_conv_ytable_set_sub( d1, d2, j, p, g, out )
int d1, d2, j, *p, *g, *out ;
{
	int d0, d3, ret ;
	int t1, t2, add, o ;

	ret = 0 ;
	add = 0 ;	/* 逆断層の足し算用 */
	d3 = d2 - d1;
	o = 0 ;

	/* 0番目は例外処理 */
	if( j == 0 )
	{
		if( (d3 >= -ytpara[0].p) && (d3 <= ytpara[0].p) )
			ret = 1 ;

		d0 = ( d1 + d2 + 1 )/2 ;

		if( d0 < 0 )
		{
			d0 = 0 ;
		}
		if( d0 > ytpara[0].n-1 )
		{
			d0 = ytpara[0].n-1 ;
		}
		d0 = d0 + ytpara[0].offset1 ;
	}
	else
	{
		if( (d3 >= -ytpara[j].p) && (d3 <= ytpara[j].p) )
			ret = 1 ;

		if( d3 >= 0 )
		{
			d0 = (d1+d2 - ytpara[j].d - ytpara[j].start*2 + ytpara[j].step)
						/( ytpara[j].step*2 ) ;

			if( d0 <  0 )
			{
				d0 = 0 ;
			}
			if( d0 > ytpara[j].n/2-1 )
			{
				d0 = ytpara[j].n/2-1 ;
			}

			if( (d1 < ytpara[j].min) || (d2 > ytpara[j].max) )
				o = 1 ;

				/* 逆断層のチェック */
			t1 = d0*ytpara[j].step + ytpara[j].start ;
			t2 = d0*ytpara[j].step + ytpara[j].start + ytpara[j].d ;
			if( t1 < d1 )
			{
				add = add + ( d1 - t1 ) ;
			}
			if( t2 > d2 )
			{
				add = add + ( t2 - d2 ) ;
			}

			/* 仕上げの時には必ず次の動作を削除しなけば危険!!!! */
//			if( v00_to_v02_conv_ytable_set_sub_ck( d1, d2, j, d0 ) )
//					EGB_init(guiEgbPtr, 1536) ;

			d0 = d0 + ytpara[j].offset1 ;
		}
		else
		{
			d0 = (d1+d2 - ytpara[j].d - ytpara[j].start*2 + ytpara[j].step)
						/( ytpara[j].step*2 ) ;

			if( d0 <  0 )
			{
				d0 = 0 ;
			}
			if( d0 > ytpara[j].n/2-1 )
			{
				d0 = ytpara[j].n/2-1 ;
			}

			if( (d2 < ytpara[j].min) || (d1 > ytpara[j].max) )
				o = 1 ;

				/* 逆断層のチェック */
			t1 = d0*ytpara[j].step + ytpara[j].start ;
			t2 = d0*ytpara[j].step + ytpara[j].start + ytpara[j].d ;
			if( t1 < d2 )
			{
				add = add + ( d2 - t1 ) ;
			}
			if( t2 > d1 )
			{
				add = add + ( t2 - d1 ) ;
			}

			/* 仕上げの時には必ず次の動作を削除しなけば危険!!!! */
//			if( v00_to_v02_conv_ytable_set_sub_ck( d1, d2, j, d0 ) )
//					EGB_init(guiEgbPtr, 1536) ;

			d0 = d0 + ytpara[j].offset2 ;
		}
	}

	*p = d0 ;
	*g = add ;
	*out = o ;
	return ret ;
}

/*
上の関数の結果が本当に最適値かどうかチェックするもの(最終的には無くてもよい)
	return値が0なら最適値, 0以外なら最適値に疑問あり
*/

/*
v00_to_v02_conv_ytable_set_sub_ck( d1, d2, j, d0 )
int d1, d2, j, d0 ;
{
	int t1, t2, x1, x2, add0, add1, i, ret ;

	add0 = 0 ;	ret = 0 ;

	if( d1 < d2 )
	{
		x1 = d1 ; x2 = d2 ;
	}
	else
	{
		x1 = d2 ; x2 = d1 ;
	}

	t1 = d0*ytpara[j].step + ytpara[j].start ;
	t2 = d0*ytpara[j].step + ytpara[j].start + ytpara[j].d ;

	if( t1 < x1 )
	{
		add0 = add0 + ( x1 - t1 ) ;
	}
	else
	{
		add0 = add0 + ( t1 - x1 ) ;
	}

	if( t2 > x2 )
	{
		add0 = add0 + ( t2 - x2 ) ;
	}
	else
	{
		add0 = add0 + ( x2 - t2 ) ;
	}

	for( i=0 ; i<ytpara[j].n/2 ; i++ )
	{
		add1 = 0 ;

		t1 = i*ytpara[j].step + ytpara[j].start ;
		t2 = i*ytpara[j].step + ytpara[j].start + ytpara[j].d ;

		if( t1 < x1 )
		{
			add1 = add1 + ( x1 - t1 ) ;
		}
		else
		{
			add1 = add1 + ( t1 - x1 ) ;
		}

		if( t2 > x2 )
		{
			add1 = add1 + ( t2 - x2 ) ;
		}
		else
		{
			add1 = add1 + ( x2 - t2 ) ;
		}

		if( add1 < add0 )
		{
			ret = 1 ;
			break ;
		}
	}

	return ret ;

}
*/

	/* サブ */
v00_to_v02_conv_yc_to_cmpyc( data1_buf, buf )
char *data1_buf, *buf;
{
	int d1, d2, n, i ;
	int total, g ;
	
	total = v02xd * v02yd / 2 ;
	n = 0;
	for( i=0 ; i<total ; i++ ){
		d1 = BYTE( buf + i*2 );
		d2 = BYTE( buf + i*2 + 1 );
		BYTE( data1_buf + n ) = BYTE( ycnvtable + d1 + (d2 << 6) ) ;
		n++;
	}
	for( i=0 ; i<( total >> 1 ) ; i++ ){
		BYTE( data1_buf + n ) = WORD( buf + (total << 1) + i );
		n++;
	}
	return 0;
}

	/* サブ */
v00_to_v02_conv_brg_to_yc( buf2, buf1, yc_buf )
char *buf2, *buf1, *yc_buf;
{
	int d0, d1, n, i, j, temp ;
	int xhf, yhf, total ;
	int b, r, g ;
	int d[4], yc, k;

	xhf = v02xd >> 1 ;
	yhf = v02yd >> 1 ;
	total = v02xd * v02yd ;
	n = 0;
	for( i=0 ; i<total ; i++ ){
		d0 = WORD( buf1 + i*2 ) & 0x7fff ;
		d1 = ( ( WORD( yc_buf + d0*2 ) & 0x3f00 ) >> 8 );
		BYTE( buf2 + n ) = d1;
		n++;
	}

	for( j=0 ; j<yhf ; j++ ){
		for( i=0 ; i<xhf ; i++ ){
			temp = j*v02xd*4 + i*4;

			d[0] = WORD( buf1 + temp ) & 0x7fff ;
			d[1] = WORD( buf1 + temp + 2 ) & 0x7fff ;
			d[2] = WORD( buf1 + temp + v02xd*2 ) & 0x7fff ;
			d[3] = WORD( buf1 + temp + v02xd*2+2 ) & 0x7fff ;

			b = 0; r =0; g = 0;
			for( k=0 ; k<4 ; k++ ){
				b += ( d[k] & 0x1f );
				r += ( (d[k] >> 5) & 0x1f );
				g += ( (d[k] >> 10) & 0x1f );
			}
			b = (b + 2) >> 2;
			r = (r + 2) >> 2;
			g = (g + 2) >> 2;
			yc =  WORD( yc_buf + (b + (r << 5) + (g << 10) )*2 );

			BYTE( buf2 + n ) = yc & 0xff ;
			n++;
		}
	}
	return 0;
}

v00_to_v02_conv_brg_to_y( buf2, buf1, yc_buf )
char *buf2, *buf1, *yc_buf;
{
	int d0, d1, i ;
	int total ;

	total = v02xd * v02yd ;
	for( i=0 ; i<total ; i++ )
	{
		d0 = WORD( buf1 + i*2 ) & 0x7fff ;
		d1 = ( ( WORD( yc_buf + d0*2 ) & 0x3f00 ) >> 8 );
		BYTE( buf2 + i ) = d1;
	}
	return 0;
}

v00_to_v02_conv_brg_to_c( buf2, buf1, yc_buf )
char *buf2, *buf1, *yc_buf;
{
	int d0, d1, n, i, j, temp ;
	int xhf, yhf ;
	int b, r, g ;
	int d[4], yc, k;

	xhf = v02xd >> 1 ;
	yhf = v02yd >> 1 ;
	n = v02xd * v02yd ;
	for( j=0 ; j<yhf ; j++ ){
		for( i=0 ; i<xhf ; i++ ){
			temp = j*v02xd*4 + i*4;

			d[0] = WORD( buf1 + temp ) & 0x7fff ;
			d[1] = WORD( buf1 + temp + 2 ) & 0x7fff ;
			d[2] = WORD( buf1 + temp + v02xd*2 ) & 0x7fff ;
			d[3] = WORD( buf1 + temp + v02xd*2+2 ) & 0x7fff ;

			b = 0; r =0; g = 0;
			for( k=0 ; k<4 ; k++ ){
				b += ( d[k] & 0x1f );
				r += ( (d[k] >> 5) & 0x1f );
				g += ( (d[k] >> 10) & 0x1f );
			}
			b = (b + 2) >> 2;
			r = (r + 2) >> 2;
			g = (g + 2) >> 2;
			yc =  WORD( yc_buf + (b + (r << 5) + (g << 10) )*2 );

			BYTE( buf2 + n ) = yc & 0xff ;
			n++;
		}
	}
	return 0;
}


/* メディアンフィルタ関係 */

v00_to_v02_time_rgb_median( 
			int rate, char *sub_ad, char *next_ad, char *new_ad, char *old_ad )
{
	int i, k, a[4], b[4], c[4], d, temp ;

	if( rate <= 0 )
	{
		int i ;

		for( i=0 ; i<( v02xd * v02yd ) ; i++ )
			WORD( sub_ad + i*2 ) = WORD( new_ad + i*2 ) ;
		return NOERR ;
	}

	for( k=0 ; k<( v02xd * v02yd ) ; k++ )
	{
		d = WORD( next_ad + k*2 ) ;
		a[0] = d & 0x1f ;
		a[1] = (d >> 5) & 0x1f ;
		a[2] = (d >> 10) & 0x1f ;
		d = WORD( new_ad + k*2 ) ;
		b[0] = d & 0x1f ;
		b[1] = (d >> 5) & 0x1f ;
		b[2] = (d >> 10) & 0x1f ;
		d = WORD( old_ad + k*2 ) ;
		c[0] = d & 0x1f ;
		c[1] = (d >> 5) & 0x1f ;
		c[2] = (d >> 10) & 0x1f ;

		for( i=0 ; i<3 ; i++ )
		{
			d = b[i] ;
			if( b[i] < a[i] )
			{
				temp = a[i] ;
				a[i] = b[i] ;
				b[i] = temp ;
			}
			if( c[i] < a[i] )
			{
				temp = a[i] ;
				a[i] = c[i] ;
				c[i] = temp ;
			}
			if( c[i] < b[i] )
			{
				temp = b[i] ;
				b[i] = c[i] ;
				c[i] = temp ;
			}
		}
		WORD( sub_ad + k*2 ) = b[0] + (b[1] << 5) + (b[2] << 10) ;
	}
	return NOERR ;
}

v00_to_v02_rgb_median( int rate, char *d_ad, char *s_ad )
{
	int a[10][4], x, y ;

	void read( int x, int y, int i )
	{
		int d ;

		if( x < 0 )x = 0 ;
		if( y < 0 )y = 0 ;
		if( x > v02xd-1 )x = v02xd-1 ;
		if( y > v02yd-1 )y = v02yd-1 ;

		d = WORD( s_ad + y*v02xd*2 + x*2 ) ;
		a[i][0] = d & 0x1f ;
		a[i][1] = (d >> 5) & 0x1f ;
		a[i][2] = (d >> 10) & 0x1f ;
	}

	if( rate <= 0 )
	{
		int i ;

		for( i=0 ; i<( v02xd * v02yd ) ; i++ )
			WORD( d_ad + i*2 ) = WORD( s_ad + i*2 ) ;
		return NOERR ;
	}

	if( rate == 1 )
	{
		for( y=0 ; y<v02yd ; y++ )
		{
	    	for( x=0 ; x<v02xd ; x++ )
		    {
				int i, j, k, temp ;

				read( x, y-1,   0 ) ;
				read( x,   y,   1 ) ;
				read( x, y-1,   2 ) ;

				for( k=0 ; k<3 ; k++ )
				{
					for( j=1 ; j<3 ; j++ )
					{
						for( i=j ; i<3 ; i++ )
						{
							if( a[j-1][k] > a[i][k] )
							{
								temp = a[i][k] ;
								a[i][k] = a[j-1][k] ;
								a[j-1][k] =temp ;
							}
						}
					}
				}

				WORD( d_ad + y*v02xd*2 + x*2 )
				= a[1][0] + (a[1][1] << 5) + (a[1][2] << 10) ;

			}
		}
		return NOERR ;
	}

	if( rate == 2 )
	{
		for( y=0 ; y<v02yd ; y++ )
		{
	    	for( x=0 ; x<v02xd ; x++ )
		    {
				int i, j, k, temp ;

				read( x,   y-1, 0 ) ;
				read( x-1, y,   1 ) ;
				read( x,   y,   2 ) ;
				read( x+1, y,   3 ) ;
				read( x,   y+1, 4 ) ;

				for( k=0 ; k<3 ; k++ )
				{
					for( j=1 ; j<4 ; j++ )
					{
						for( i=j ; i<5 ; i++ )
						{
							if( a[j-1][k] > a[i][k] )
							{
								temp = a[i][k] ;
								a[i][k] = a[j-1][k] ;
								a[j-1][k] =temp ;
							}
						}
					}
				}

				WORD( d_ad + y*v02xd*2 + x*2 )
				= a[2][0] + (a[2][1] << 5) + (a[2][2] << 10) ;

			}
		}
		return NOERR ;
	}

	if( rate > 2 )
	{
		for( y=0 ; y<v02yd ; y++ )
		{
	    	for( x=0 ; x<v02xd ; x++ )
		    {
				int i, j, k, temp ;

				read( x-1, y-1, 0 ) ;
				read( x,   y-1, 1 ) ;
				read( x+1, y-1, 2 ) ;
				read( x-1, y,   3 ) ;
				read( x,   y,   4 ) ;
				read( x+1, y,   5 ) ;
				read( x-1, y+1, 6 ) ;
				read( x,   y+1, 7 ) ;
				read( x+1, y+1, 8 ) ;

				for( k=0 ; k<3 ; k++ )
				{
					for( j=1 ; j<6 ; j++ )
					{
						for( i=j ; i<9 ; i++ )
						{
							if( a[j-1][k] > a[i][k] )
							{
								temp = a[i][k] ;
								a[i][k] = a[j-1][k] ;
								a[j-1][k] =temp ;
							}
						}
					}
				}

				WORD( d_ad + y*v02xd*2 + x*2 )
				= a[4][0] + (a[4][1] << 5) + (a[4][2] << 10) ;

			}
		}
		return NOERR ;
	}
}

v00_to_v02_rgb_back( int rate, char *soft_ad, char *org_ad )
{
	int a[4], b[4], x, y, d, i ;

	if( rate <= 0 )
	{
		for( i=0 ; i<( v02xd * v02yd ) ; i++ )
			WORD( soft_ad + i*2 ) = WORD( org_ad + i*2 ) ;
		return NOERR ;
	}

	for( y=0 ; y<v02yd ; y++ )
	{
	    for( x=0 ; x<v02xd ; x++ )
	    {
			d = WORD( soft_ad + y*v02xd*2 + x*2 ) ;
			a[0] = d & 0x1f ;
			a[1] = (d >> 5) & 0x1f ;
			a[2] = (d >> 10) & 0x1f ;

			d = WORD( org_ad + y*v02xd*2 + x*2 ) ;
			b[0] = d & 0x1f ;
			b[1] = (d >> 5) & 0x1f ;
			b[2] = (d >> 10) & 0x1f ;

			if( rate == 1 )
			{
				for( i=0 ; i<3 ; i++ )
				{
					d = a[i]-b[i] ;
					if( (d == 1) || (d == -1) )
						a[i] = a[i] ;
					else
						a[i] = b[i] ;
				}
			}
			else
			{
				for( i=0 ; i<3 ; i++ )
				{
					d = a[i]-b[i] ;
					if( (d == 1) || (d == -1) )
						a[i] = a[i] ;
					else if( d == 2 )
						a[i] = b[i]+1 ;
					else if( d == -2 )
						a[i] = b[i]-1 ;
					else
						a[i] = b[i] ;
				}
			}
			WORD( soft_ad + y*v02xd*2 + x*2 )
			= a[0] + (a[1] << 5) + (a[2] << 10) ;
		}
	}
	return NOERR ;
}


	/* メイン1 */

v00_to_v02( char *name1, char *name2, int flt, int soft, int color, 
												int mode, int packmode )
{
	int ret ;

	ret = v00_to_v02_conv( name1, name2, flt, soft, color, mode, 0, packmode );
	return ret ;
}

	/* テーブル参照 */

static char *v02table1 ;		/* yc to brg (32768) */
static char *v02table2 ;		/* y記号 to 00Y00Y (512) */
static char *brgtoyc ;			/* brg to yc (65536) */
static char *yytoy ;			/* YY to y記号 (4096) */

v00_to_v02_t( char *name1, char *name2, int flt, int soft, int color, 
										int mode, char *table, int packmode )
{
	int ret ;

	v02table1 = table ;
	v02table2 = v02table1 + 32768 ;
	brgtoyc   = v02table2 + 512   ;
	yytoy     = brgtoyc   + 65536 ;

	ret = v00_to_v02_conv( name1, name2, flt, soft, color, mode, 1, packmode );
	return ret ;
}

static int softSet[][7] = {
				{ 0,1,1, 1, 1,1,1 },	/* time median */
				{ 0,1,1, 1, 1,1,1 },	/* time median back rate */
				{ 0,0,1, 1, 1,1,1 },	/* 2d median */
				{ 0,0,1, 2, 2,2,2 },	/* 2d median back rate */
				{ 0,0,1, 1, 1,1,1 },	/* y median */
				{ 0,0,1, 1, 1,1,1 },	/* c median */
				{ 0,0,0, 0, 1,2,3 },	/* soft */
	} ;

	/* V00ファイル→V02ファイル(tbckはテーブル参照フラグ) */
	/* packmode 0:normal 1:瞬間圧縮 */
v00_to_v02_conv( char *name1, char *name2, int flt, int soft, int color, 
											int mode, int tbck, int packmode )
{
	FILE *fp2, *fp1, *fps;
	char names[100];
	int sw, cx, cy;
	int wait ;
	int i, j, size, sndflg, count, error, step, rate ;
	int scount0, scount1, cmpmode;
	int bind_point, bind, bind_size, total, file_point, frame;
	int d0, d1;

	char para2[64];
	char head[256];
	char v00_head[256];
	char bind_head[32];
	char *buffer;		/* buffer */
	char *snd_buf, *buf, *new_ad, *old_ad,
	     *brg_buf, *ytable, *yc_buf, *data1_buf,
	     *make_buf, *check_buf, *data2_buf;	/* 差分圧縮用 */
	int  snd_b, buf_b, new_b, old_b, brg_b, ytab_b, yc_b, data1_b,
	     make_b, check_b, data2_b ;	   /* 移動補償の計算でワークに使う */

	char *nextBuf ;	/* median filter 用 */
	char *newBuf ;
	char *oldBuf ;
	char *subBuf ;

	char *nextBuf2 ;
	char *newBuf2 ;
	char *oldBuf2 ;
	char *subBuf2 ;

	rate = 0 ; step = 2 ;	/* ラフレート, 動き補償 (固定) */

	if( ( fp1 = fopen( name1, "rb" ) ) == NULL )return 1;
	if( fread( v00_head, 256, 1, fp1 ) < 1 ){	/* ヘッダー */
		fclose(fp1);
		return 1;
	}
	if
	(
	    (DWORD( v00_head +  0 ) == 0x30304456)	/* VD00 */
	 && (DWORD( v00_head + 12 ) == 0x80008000)	/* dataの種類番号 */
	)
	{
		v02xd = DWORD( v00_head + 16 );	/* 横 */
		v02yd = DWORD( v00_head + 20 );	/* 縦 */
		wait = DWORD( v00_head + 24 );	/* wait */
	}
	else
	{
		v02xd = 128;			/* 横 */
		v02yd = 96;			/* 縦 */
		wait = 6;
		fseek( fp1, 0, SEEK_SET );
	}

	if( ( fp2 = fopen( name2, "wb" ) ) == NULL ){
		fclose(fp1);
		return 2;
	}

	snd_b = 8000 ;
	buf_b = (v02xd * v02yd * 2)*8 ;
	new_b = v02xd * v02yd * 5 / 4 * 4 ; /* 注意 4つ入らないとだめ!! */
	old_b = v02xd * v02yd * 5 / 4 ;
	brg_b = 32768 ;
	ytab_b = 512 ;
	yc_b = 65536 ;
	data1_b = v02xd * v02yd * 3 / 4 ;
	make_b =  v02xd * v02yd * 3 / 4 ;
	check_b = v02xd * v02yd * 3 / 4 ;
	data2_b = v02xd * v02yd * 4 + 0x1000 ;

	size = snd_b + buf_b + new_b + old_b
	+ brg_b + ytab_b + yc_b + data1_b + make_b + check_b + data2_b;
	if( (buffer = (char *)TL_malloc( size )) == NULL ){
		fclose(fp2);
		fclose(fp1);
		return 7;			/* out of memory */
	}
	snd_buf = buffer;
	buf     = snd_buf + snd_b; 	/* RGB org.(128*96*2) */
	new_ad  = buf + buf_b;		/* YC org.(15360) */
	old_ad  = new_ad + new_b;	/* YC 前frame(15360) */
	brg_buf = old_ad + old_b;
	ytable  = brg_buf + brg_b;
	yc_buf  = ytable + ytab_b;
	data1_buf = yc_buf + yc_b;
	make_buf = data1_buf + data1_b;
	check_buf = make_buf + make_b;
	data2_buf = check_buf + check_b;

	nextBuf = buf ;
	newBuf  = nextBuf + (v02xd * v02yd * 2) ;
	oldBuf  = newBuf  + (v02xd * v02yd * 2) ;
	subBuf  = oldBuf  + (v02xd * v02yd * 2) ;
	nextBuf2= subBuf  + (v02xd * v02yd * 2) ;
	newBuf2 = nextBuf2+ (v02xd * v02yd * 2) ;
	oldBuf2 = newBuf2 + (v02xd * v02yd * 2) ;
	subBuf2 = oldBuf2 + (v02xd * v02yd * 2) ;

	sndflg = 0;
	if( file_kakuchousi_set( names, name1, ".SND" ) )goto vd01;
	if( ( fps = fopen( names, "rb" ) ) == NULL )goto vd01;
	if( fread( buf, 32, 1, fps ) < 1 ){
		fclose(fps);
	}
	else sndflg = 1;			/* pcm ok */

	if( sndflg )	/* sound 先送り */
	{
		if( DWORD( buf+16 ) )
			fseek( fps, DWORD( buf+16 ), SEEK_CUR );
	}
		/* head */
vd01:	file_point = 0;
	for( i=0 ; i<64 ; i++ )DWORD( head + i*4 ) = 0;
	DWORD( head ) = 0x32304456;		/* ID = VD02 */
/*	DWORD( head + 4 ) total length	 */
/*	DWORD( head + 8 ) total frame	 */
	DWORD( head + 12 ) = 6;		/* dataの種類番号(bit/ピクセル) */
	DWORD( head + 16 ) = v02xd;		/* 横 */
	DWORD( head + 20 ) = v02yd;		/* 縦 */
	DWORD( head + 24 ) = wait;		/* wait */
	DWORD( head + 28 ) = 8;			/* バインダ内こま数 */
	DWORD( head + 32 ) = 8000*6/wait;	/* sound samp. freq */
	DWORD( head + 36 ) = 6400;		/* バインダ内snd data Byte数 */
	if( fwrite( head, 256, 1, fp2 ) < 1 ){	/* ヘッダー */
		error = 2;			/* 2 = error num. */
		goto vd10;
	}
	file_point += 256;
	total = 0;

/*	テーブルの設定1 */
	if( tbck )
	{
		for( j=0 ; j<32768 ; j += 4 )
			DWORD( brg_buf + j ) = DWORD( v02table1 + j ) ;
	}
	else
		v00_to_v02_conv_brg_set_buffer( brg_buf, color );
/* brg data set */

	if( fwrite( brg_buf, 16384*2, 1, fp2 ) < 1 ){	/* パレット1 */
		error = 2;			/* 2 = error num. */
		goto vd10;
	}
	file_point += 32768;
	total += 32768;

/*	テーブルの設定2 */
	if( tbck )
	{
		for( j=0 ; j<512 ; j += 4 )
			DWORD( ytable + j ) = DWORD( v02table2 + j ) ;
		for( j=0 ; j<4096 ; j += 4 )
			DWORD( ycnvtable + j ) = DWORD( yytoy + j ) ;
	}
	else
		v00_to_v02_conv_ytable_set( ytable, ycnvtable ) ;
/* ycnvtableとはy記号変換するためのtableでこのソース内のstatic char *変数 */

	if( fwrite( ytable, 512, 1, fp2 ) < 1 ){	/* パレット2 */
		error = 2;			/* 2 = error num. */
		goto vd10;
	}
	file_point += 512;
	total += 512;

/*	テーブルの設定3 */
	if( tbck )
	{
		for( j=0 ; j<65536 ; j += 4 )
			DWORD( yc_buf + j ) = DWORD( brgtoyc + j ) ;
	}
	else
		v00_to_v02_conv_yc_set_buffer( yc_buf, brg_buf, color );

/* yc data set */
	EGB_writePage(guiEgbPtr,1);
	EGB_color( guiEgbPtr, 1, 0 ) ;
	EGB_clearScreen( guiEgbPtr ) ;

	EGB_displayStart(guiEgbPtr,3,0,0);
	EGB_displayStart(guiEgbPtr,2,1,1);
	EGB_displayStart(guiEgbPtr,2,2,2);
	EGB_displayStart(guiEgbPtr,0,(320 - (v02xd))/2,(240 - (v02yd))/2);
	EGB_displayStart(guiEgbPtr,1,0,0);
	EGB_displayStart(guiEgbPtr,3,v02xd,v02yd);
	EGB_writePage(guiEgbPtr,0);
	bind = 0;	/* バインダーカウント */
	frame = 0;	/* frameカウント */
	scount0 = v02xd*v02yd*4+100;	/* 前の絵と同じポイントのカウンター */

	int frflg ;		/* フレームリードフラグ */
	if( fread( nextBuf,  v02xd * v02yd * 2, 1, fp1 )  )
		frflg = 1 ;
	else
		frflg = 0 ;

	if( packmode == 0 )	/* 瞬間圧縮でないなら */
	{
		v00_to_v02_rgb_median( softSet[2][soft], nextBuf2, nextBuf ) ;
		v00_to_v02_rgb_back( softSet[3][soft], nextBuf2, nextBuf ) ;
	}

	while( 1 ){
			/* bind_head */
		bind_point = file_point;
		for( i=0 ; i<8 ; i++ )DWORD( bind_head + i*4 ) = 0;
		DWORD( bind_head ) = bind;
			/* sound read */
		for( i=0 ; i<1600 ; i++ )DWORD( snd_buf + i*4 ) = 0x80808080;
		if( sndflg ){
			if( fread( snd_buf, 6400, 1, fps ) < 1 ){
				fclose(fps);
				sndflg = 0;
			}
		}
		bind_size = 0;		/* バインダー内data長カウント */
		count = 0;		/* バインダー内のframeカウント */

	vd03:
		MOS_rdpos(&sw,&cx,&cy);	/* sw == 3なら終了 */
		if( (frflg == 0) || ( sw == 3 ) )
		{
				/* 終了 */
		    if( count != 0 ){	/* bindの途中なら修正あり */
				/* bind head 修正 */
			fseek( fp2, bind_point, SEEK_SET );
			DWORD( bind_head + 4 ) = bind_size;
			if( fwrite( bind_head, 32, 1, fp2 ) < 1 ){
				error = 2;	/* 2 = can't save */
				break;
			}
				/* total data size 修正 */
			fseek( fp2, 0, SEEK_SET );
			DWORD( head+4 ) = total;		/* data size */
			DWORD( head+8 ) = frame;		/* bind */
			if( fwrite( head, 256, 1, fp2 ) < 1 ){	/* ヘッダー */
				error = 2;	/* 2 = error num. */
				break;
			}
		    }
		    error = 0;	/* 原画は全部読み切った(no error) */
		    break;		/* 終了 */
		}
		if( count == 0 ){
				/* bind head 登録 */
			if( fwrite( bind_head, 32, 1, fp2 ) < 1 ){
				error = 2;
				break;
			}
			file_point += 32;
			total += 32;
				/* sound 登録 */
			if( fwrite( snd_buf, 6400, 1, fp2 ) < 1 ){
				error = 2;	/* 2 = can't save */
				break;
			}
			file_point += 6400;
			total += 6400;
			bind_size += 6400;
		}

		for( i=0 ; i<(v02xd*v02yd*2) ; i += 2 )
			WORD( newBuf + i ) = WORD( nextBuf + i ) ;
		for( i=0 ; i<(v02xd*v02yd*2) ; i += 2 )
			WORD( newBuf2 + i ) = WORD( nextBuf2 + i ) ;

		if( fread( nextBuf, v02xd * v02yd * 2, 1, fp1 ) )
			frflg = 1 ;
		else
			frflg = 0 ;

		if( packmode == 0 )	/* 瞬間圧縮でないなら */
		{
			v00_to_v02_rgb_median( softSet[2][soft], nextBuf2, nextBuf ) ;
			v00_to_v02_rgb_back( softSet[3][soft], nextBuf2, nextBuf ) ;
		}

			/* buf(原画) -> buf(yc-data) */
		v00_to_v02_conv_brg_to_yc( new_ad, newBuf, yc_buf );
			/* 前の絵と同じ点をカウント */
		scount1 = 0;
		for( i=0 ; i<v02xd*v02yd ; i++ ){
		    d0 = BYTE( new_ad + i ) - BYTE( old_ad + i );
		    if( d0>-2 && d0<2 )scount1++;
		}
		if( scount1 < (scount0 >> 2) )cmpmode = 0;
		else cmpmode = 1;
		scount0 = scount1;
		if( frame == 0 )
			scount0 = 0;
			/* 圧縮 */
		if( packmode )	/* 瞬間圧縮 */
		{
			size = v00_to_v02_conv_cmp3(
			flt, rate, step, soft, mode, data2_buf,
			new_ad, old_ad, data1_buf, make_buf, check_buf
			);
		}
		else			/* normal */
		{
			if( cmpmode == 0 ){

				if( softSet[4][soft] )
					v00_to_v02_conv_brg_to_y( new_ad, newBuf2, yc_buf ) ;
				else
					v00_to_v02_conv_brg_to_y( new_ad, newBuf, yc_buf ) ;

				if( softSet[5][soft] )
					v00_to_v02_conv_brg_to_c( new_ad, newBuf2, yc_buf ) ;
				else
					v00_to_v02_conv_brg_to_c( new_ad, newBuf, yc_buf ) ;

				size = v00_to_v02_conv_cmp1(
				flt, rate, step, softSet[6][soft], mode, data2_buf,
				new_ad, old_ad, data1_buf, make_buf, check_buf
				);
			}
			else {

				v00_to_v02_time_rgb_median(
						softSet[0][soft], subBuf, nextBuf, newBuf, oldBuf);
				v00_to_v02_rgb_back( softSet[1][soft], subBuf, newBuf ) ;

				v00_to_v02_time_rgb_median(
						softSet[0][soft], subBuf2, nextBuf2, newBuf2, oldBuf2);
				v00_to_v02_rgb_back( softSet[1][soft], subBuf2, newBuf ) ;

				if( softSet[4][soft] )
					v00_to_v02_conv_brg_to_y( new_ad, subBuf2, yc_buf ) ;
				else
					v00_to_v02_conv_brg_to_y( new_ad, subBuf, yc_buf ) ;

				if( softSet[5][soft] )
					v00_to_v02_conv_brg_to_c( new_ad, subBuf2, yc_buf ) ;
				else
					v00_to_v02_conv_brg_to_c( new_ad, subBuf, yc_buf ) ;

				size = v00_to_v02_conv_cmp2(
				flt, rate, step, softSet[6][soft], mode, data2_buf,
				new_ad, old_ad, data1_buf, make_buf, check_buf
				);
			}

			for( i=0 ; i<v02xd*v02yd*2 ; i += 2 )
				WORD( oldBuf + i ) = WORD( newBuf + i ) ;
			for( i=0 ; i<v02xd*v02yd*2 ; i += 2 )
				WORD( oldBuf2 + i ) = WORD( newBuf2 + i ) ;

		}

			/* check */
		VSYNC_counter = 0;
		DWORD(para2+0) = (int)make_buf;
		DWORD(para2+4) = (int)brg_buf;
		DWORD(para2+8) = 100;
		DWORD(para2+12) = 1;
		DWORD(para2+16) = v02xd;
		DWORD(para2+20) = v02yd;
		vd_wide_01_2( para2 );

		v_cmp_frame_dsp( frame ) ;

		if( fwrite( data2_buf, size, 1, fp2 ) < 1 ){
			error = 2;	/* 2 = can't save */
			break;
		}
		file_point = file_point + size;
		total = total + size;
		bind_size = bind_size + size;
		count++;
		frame++;
		if( count < 8 )goto vd03;

		bind++;
			/* bind head 修正 */
		fseek( fp2, bind_point, SEEK_SET );
		DWORD( bind_head + 4 ) = bind_size;
		if( fwrite( bind_head, 32, 1, fp2 ) < 1 ){
			error = 2;	/* 2 = can't save */
			break;
		}
			/* total data size 修正 */
		fseek( fp2, 0, SEEK_SET );
		DWORD( head+4 ) = total;		/* data size */
		DWORD( head+8 ) = frame;		/* bind */
		if( fwrite( head, 256, 1, fp2 ) < 1 ){	/* ヘッダー */
			error = 2;	/* 2 = error num. */
			break;
		}

		/*
			 1bind終わるごとにしめる
			( 停電で止まってもそれまでのdataを残すため)
		*/

		fclose(fp2);
		if( ( fp2 = fopen( name2, "r+b" ) ) == NULL ){
			TL_free( buffer );
			fclose(fp1);
			if( sndflg )fclose( fps );
			return 2;
		}
		fseek( fp2, file_point, SEEK_SET );
	}
vd10:	TL_free( buffer );
	fclose(fp2);
	fclose(fp1);
	if( sndflg )fclose( fps );
	return error;
}

	/* メイン2 */

mvb_to_v02( char *name1, char *name2, int flt, int soft, int color, 
												int mode, int packmode )
{
	int ret ;

	ret = mvb_to_v02_conv( name1, name2, flt, soft, color, mode, 0, packmode );
	return ret ;
}

	/* テーブル参照 */

mvb_to_v02_t( char *name1, char *name2, int flt, int soft, int color, 
									int mode, char *table, int packmode )
{
	int ret ;

	v02table1 = table ;
	v02table2 = v02table1 + 32768 ;
	brgtoyc   = v02table2 + 512   ;
	yytoy     = brgtoyc   + 65536 ;

	ret = mvb_to_v02_conv( name1, name2, flt, soft, color, mode, 1, packmode );
	return ret ;
}

	/* mvbファイル→V02ファイル(tbckはテーブル参照フラグ) */
	/* packmode 0:normal 1:瞬間圧縮 */
mvb_to_v02_conv( name1, name2, flt, soft, color, mode, tbck, packmode )
char *name1, *name2 ;
int flt, soft, color, mode, tbck, packmode ;
{
	FILE *fp2, *fp1, *fps;
	char names[100];
	int sw, cx, cy;
	int wait, freq, sndpack ;
	int i, j, size, sndflg, count, error, step, rate ;
	int scount0, scount1, cmpmode;
	int bind_point, bind, bind_size, total, file_point, frame;
	int d0, d1;

	char para2[64];
	char head[256];
	char mvb_head[24];
	char bind_head[32];
	char *buffer;		/* buffer */
	char *snd_buf, *buf, *new_ad, *old_ad,
	     *brg_buf, *ytable, *yc_buf, *data1_buf,
	     *make_buf, *check_buf, *data2_buf;	/* 差分圧縮用 */
	int  snd_b, buf_b, new_b, old_b, brg_b, ytab_b, yc_b, data1_b,
	     make_b, check_b, data2_b ;	   /* 移動補償の計算でワークに使う */

	char *nextBuf ;	/* median filter 用 */
	char *newBuf ;
	char *oldBuf ;
	char *subBuf ;

	char *nextBuf2 ;
	char *newBuf2 ;
	char *oldBuf2 ;
	char *subBuf2 ;

	rate = 0 ; step = 2 ;	/* ラフレート, 動き補償 (固定) */

	if( ( fp1 = fopen( name1, "rb" ) ) == NULL )return 1;
	if( fread( mvb_head, 24, 1, fp1 ) < 1 ){	/* ヘッダー */
		fclose(fp1);
		return 1;
	}
	if( DWORD( mvb_head + 0 ) == 0x50414d42 )	/* BMAP */
	{
		v02xd = DWORD( mvb_head + 8 );	/* 横 */
		v02yd = DWORD( mvb_head + 12 );	/* 縦 */
		wait
		 = ( 600 +  DWORD( mvb_head + 20 )/2 )
		 / DWORD( mvb_head + 20 ) ; 		/* wait */
	}
	else
	{
		fclose(fp1);
		return 58;	/* データの形式が違うエラー */
	}
	if( DWORD( mvb_head + 4 ) != 0x00000014 )	/* headの大きさ */
	{
		fseek( fp1, DWORD( mvb_head + 4 ) + 4, SEEK_SET );
	}

	if( ( fp2 = fopen( name2, "wb" ) ) == NULL ){
		fclose(fp1);
		return 2;
	}

	snd_b = 19200;
	buf_b = (v02xd * v02yd * 2)*8 ;
	new_b = v02xd * v02yd * 5 / 4 * 4 ; /* 注意 4つ入らないとだめ!! */
	old_b = v02xd * v02yd * 5 / 4 ;
	brg_b = 32768 ;
	ytab_b = 512 ;
	yc_b = 65536 ;
	data1_b = v02xd * v02yd * 3 / 4 ;
	make_b =  v02xd * v02yd * 3 / 4 ;
	check_b = v02xd * v02yd * 3 / 4 ;
	data2_b = v02xd * v02yd * 4 + 0x1000 ;

	size = snd_b + buf_b + new_b + old_b
	+ brg_b + ytab_b + yc_b + data1_b + make_b + check_b + data2_b;
	if( (buffer = (char *)TL_malloc( size )) == NULL ){
		fclose(fp2);
		fclose(fp1);
		return 7;			/* out of memory */
	}
	snd_buf = buffer;
	buf     = snd_buf + snd_b; 	/* RGB org.(128*96*2) */
	new_ad  = buf + buf_b;		/* YC org.(15360) */
	old_ad  = new_ad + new_b;	/* YC 前frame(15360) */
	brg_buf = old_ad + old_b;
	ytable  = brg_buf + brg_b;
	yc_buf  = ytable + ytab_b;
	data1_buf = yc_buf + yc_b;
	make_buf = data1_buf + data1_b;
	check_buf = make_buf + make_b;
	data2_buf = check_buf + check_b;

	nextBuf = buf ;
	newBuf  = nextBuf + (v02xd * v02yd * 2) ;
	oldBuf  = newBuf  + (v02xd * v02yd * 2) ;
	subBuf  = oldBuf  + (v02xd * v02yd * 2) ;
	nextBuf2= subBuf  + (v02xd * v02yd * 2) ;
	newBuf2 = nextBuf2+ (v02xd * v02yd * 2) ;
	oldBuf2 = newBuf2 + (v02xd * v02yd * 2) ;
	subBuf2 = oldBuf2 + (v02xd * v02yd * 2) ;

	sndflg = 0;
	if( file_kakuchousi_set( names, name1, ".SND" ) )goto vd01;
	if( ( fps = fopen( names, "rb" ) ) == NULL )goto vd01;
	if( fread( buf, 32, 1, fps ) < 1 ){
		fclose(fps);
	}
	else sndflg = 1;			/* pcm ok */

vd01:	if( sndflg )	/* sound 先送り */
	{
		if( DWORD( buf+16 ) )
			fseek( fps, DWORD( buf+16 ), SEEK_CUR );
	}

	freq = 9600 ;
	if( sndflg )
	{
		freq = ( ( WORD( buf+24 )*10 + 0x31 ) / 0x62 ) * 100 ;
	}
	if( freq < 0 )freq = 9600 ;
	if( freq > 19200 )freq = 19200 ;
	sndpack = freq * wait * 8 / 60 ;

		/* head */
	file_point = 0;
	for( i=0 ; i<64 ; i++ )DWORD( head + i*4 ) = 0;
	DWORD( head ) = 0x32304456;		/* ID = VD02 */
/*	DWORD( head + 4 ) total length	 */
/*	DWORD( head + 8 ) total frame	 */
	DWORD( head + 12 ) = 7;		/* dataの種類番号(bit/ピクセル) */
	DWORD( head + 16 ) = v02xd;		/* 横 */
	DWORD( head + 20 ) = v02yd;		/* 縦 */
	DWORD( head + 24 ) = wait;		/* wait */
	DWORD( head + 28 ) = 8;			/* バインダ内こま数 */
	DWORD( head + 32 ) = freq;		/* sound samp. freq */
	DWORD( head + 36 ) = sndpack;		/* バインダ内snd data Byte数 */

	if( fwrite( head, 256, 1, fp2 ) < 1 ){	/* ヘッダー */
		error = 2;			/* 2 = error num. */
		goto vd10;
	}
	file_point += 256;
	total = 0;

/*	テーブルの設定1 */
	if( tbck )
	{
		for( j=0 ; j<32768 ; j += 4 )
			DWORD( brg_buf + j ) = DWORD( v02table1 + j ) ;
	}
	else
		v00_to_v02_conv_brg_set_buffer( brg_buf, color );
/* brg data set */

	if( fwrite( brg_buf, 16384*2, 1, fp2 ) < 1 ){	/* パレット1 */
		error = 2;			/* 2 = error num. */
		goto vd10;
	}
	file_point += 32768;
	total += 32768;

/*	テーブルの設定2 */
	if( tbck )
	{
		for( j=0 ; j<512 ; j += 4 )
			DWORD( ytable + j ) = DWORD( v02table2 + j ) ;
		for( j=0 ; j<4096 ; j += 4 )
			DWORD( ycnvtable + j ) = DWORD( yytoy + j ) ;
	}
	else
		v00_to_v02_conv_ytable_set( ytable, ycnvtable ) ;
/* ycnvtableとはy記号変換するためのtableでこのソース内のstatic char *変数 */

	if( fwrite( ytable, 512, 1, fp2 ) < 1 ){	/* パレット2 */
		error = 2;			/* 2 = error num. */
		goto vd10;
	}
	file_point += 512;
	total += 512;

/*	テーブルの設定3 */
	if( tbck )
	{
		for( j=0 ; j<65536 ; j += 4 )
			DWORD( yc_buf + j ) = DWORD( brgtoyc + j ) ;
	}
	else
		v00_to_v02_conv_yc_set_buffer( yc_buf, brg_buf, color );

/* yc data set */
	EGB_writePage(guiEgbPtr,1);
	EGB_color( guiEgbPtr, 1, 0 ) ;
	EGB_clearScreen( guiEgbPtr ) ;

	EGB_displayStart(guiEgbPtr,3,0,0);
	EGB_displayStart(guiEgbPtr,2,1,1);
	EGB_displayStart(guiEgbPtr,2,2,2);
	EGB_displayStart(guiEgbPtr,0,(320 - (v02xd))/2,(240 - (v02yd))/2);
	EGB_displayStart(guiEgbPtr,1,0,0);
	EGB_displayStart(guiEgbPtr,3,v02xd,v02yd);
	EGB_writePage(guiEgbPtr,0);

	bind = 0;	/* バインダーカウント */
	frame = 0;	/* frameカウント */
	scount0 = v02xd*v02yd*4+100;	/* 前の絵と同じポイントのカウンター */

	int frflg ;		/* フレームリードフラグ */
	if( fread( nextBuf,  v02xd * v02yd * 2, 1, fp1 )  )
		frflg = 1 ;
	else
		frflg = 0 ;

	if( packmode == 0 )	/* 瞬間圧縮でないなら */
	{
		v00_to_v02_rgb_median( softSet[2][soft], nextBuf2, nextBuf ) ;
		v00_to_v02_rgb_back( softSet[3][soft], nextBuf2, nextBuf ) ;
	}

	while( 1 ){
			/* bind_head */
		bind_point = file_point;
		for( i=0 ; i<8 ; i++ )DWORD( bind_head + i*4 ) = 0;
		DWORD( bind_head ) = bind;
			/* sound read */
		for( i=0 ; i<sndpack ; i++ )BYTE( snd_buf + i ) = 0x80 ;
		if( sndflg ){
			if( fread( snd_buf, sndpack, 1, fps ) < 1 ){
				fclose(fps);
				sndflg = 0;
			}
		}
		bind_size = 0;		/* バインダー内data長カウント */
		count = 0;		/* バインダー内のframeカウント */

	vd03:
		MOS_rdpos(&sw,&cx,&cy);	/* sw == 3なら終了 */
		if( (frflg == 0) || ( sw == 3 ) )
		{
				/* 終了 */
		    if( count != 0 ){	/* bindの途中なら修正あり */
				/* bind head 修正 */
			fseek( fp2, bind_point, SEEK_SET );
			DWORD( bind_head + 4 ) = bind_size;
			if( fwrite( bind_head, 32, 1, fp2 ) < 1 ){
				error = 2;	/* 2 = can't save */
				break;
			}
				/* total data size 修正 */
			fseek( fp2, 0, SEEK_SET );
			DWORD( head+4 ) = total;		/* data size */
			DWORD( head+8 ) = frame;		/* bind */
			if( fwrite( head, 256, 1, fp2 ) < 1 ){	/* ヘッダー */
				error = 2;	/* 2 = error num. */
				break;
			}
		    }
		    error = 0;	/* 原画は全部読み切った(no error) */
		    break;		/* 終了 */
		}
		if( count == 0 ){
				/* bind head 登録 */
			if( fwrite( bind_head, 32, 1, fp2 ) < 1 ){
				error = 2;
				break;
			}
			file_point += 32;
			total += 32;
				/* sound 登録 */
			if( fwrite( snd_buf, sndpack, 1, fp2 ) < 1 ){
				error = 2;	/* 2 = can't save */
				break;
			}
			file_point += sndpack;
			total += sndpack;
			bind_size += sndpack;
		}

		for( i=0 ; i<(v02xd*v02yd*2) ; i += 2 )
			WORD( newBuf + i ) = WORD( nextBuf + i ) ;
		for( i=0 ; i<(v02xd*v02yd*2) ; i += 2 )
			WORD( newBuf2 + i ) = WORD( nextBuf2 + i ) ;

		if( fread( nextBuf, v02xd * v02yd * 2, 1, fp1 ) )
			frflg = 1 ;
		else
			frflg = 0 ;

		if( packmode == 0 )	/* 瞬間圧縮でないなら */
		{
			v00_to_v02_rgb_median( softSet[2][soft], nextBuf2, nextBuf ) ;
			v00_to_v02_rgb_back( softSet[3][soft], nextBuf2, nextBuf ) ;
		}

			/* buf(原画) -> buf(yc-data) */
		v00_to_v02_conv_brg_to_yc( new_ad, newBuf, yc_buf );
			/* 前の絵と同じ点をカウント */
		scount1 = 0;
		for( i=0 ; i<v02xd*v02yd ; i++ ){
		    d0 = BYTE( new_ad + i ) - BYTE( old_ad + i );
		    if( d0>-2 && d0<2 )scount1++;
		}
		if( scount1 < (scount0 >> 2) )cmpmode = 0;
		else cmpmode = 1;
		scount0 = scount1;
		if( frame == 0 )
			scount0 = 0;

			/* 圧縮 */
		if( packmode )	/* 瞬間圧縮 */
		{
			size = v00_to_v02_conv_cmp3(
			flt, rate, step, soft, mode, data2_buf,
			new_ad, old_ad, data1_buf, make_buf, check_buf
			);
		}
		else			/* normal */
		{
			if( cmpmode == 0 ){

				if( softSet[4][soft] )
					v00_to_v02_conv_brg_to_y( new_ad, newBuf2, yc_buf ) ;
				else
					v00_to_v02_conv_brg_to_y( new_ad, newBuf, yc_buf ) ;

				if( softSet[5][soft] )
					v00_to_v02_conv_brg_to_c( new_ad, newBuf2, yc_buf ) ;
				else
					v00_to_v02_conv_brg_to_c( new_ad, newBuf, yc_buf ) ;

				size = v00_to_v02_conv_cmp1(
				flt, rate, step, softSet[6][soft], mode, data2_buf,
				new_ad, old_ad, data1_buf, make_buf, check_buf
				);
			}
			else {

				v00_to_v02_time_rgb_median(
						softSet[0][soft], subBuf, nextBuf, newBuf, oldBuf);
				v00_to_v02_rgb_back( softSet[1][soft], subBuf, newBuf ) ;

				v00_to_v02_time_rgb_median(
						softSet[0][soft], subBuf2, nextBuf2, newBuf2, oldBuf2);
				v00_to_v02_rgb_back( softSet[1][soft], subBuf2, newBuf ) ;

				if( softSet[4][soft] )
					v00_to_v02_conv_brg_to_y( new_ad, subBuf2, yc_buf ) ;
				else
					v00_to_v02_conv_brg_to_y( new_ad, subBuf, yc_buf ) ;

				if( softSet[5][soft] )
					v00_to_v02_conv_brg_to_c( new_ad, subBuf2, yc_buf ) ;
				else
					v00_to_v02_conv_brg_to_c( new_ad, subBuf, yc_buf ) ;

				size = v00_to_v02_conv_cmp2(
				flt, rate, step, softSet[6][soft], mode, data2_buf,
				new_ad, old_ad, data1_buf, make_buf, check_buf
				);
			}

			for( i=0 ; i<v02xd*v02yd*2 ; i += 2 )
				WORD( oldBuf + i ) = WORD( newBuf + i ) ;
			for( i=0 ; i<v02xd*v02yd*2 ; i += 2 )
				WORD( oldBuf2 + i ) = WORD( newBuf2 + i ) ;

		}

			/* check */
		VSYNC_counter = 0;
		DWORD(para2+0) = (int)make_buf;
		DWORD(para2+4) = (int)brg_buf;
		DWORD(para2+8) = 100;
		DWORD(para2+12) = 1;
		DWORD(para2+16) = v02xd;
		DWORD(para2+20) = v02yd;
		vd_wide_01_2( para2 );

		v_cmp_frame_dsp( frame ) ;

		if( fwrite( data2_buf, size, 1, fp2 ) < 1 ){
			error = 2;	/* 2 = can't save */
			break;
		}
		file_point = file_point + size;
		total = total + size;
		bind_size = bind_size + size;
		count++;
		frame++;
		if( count < 8 )goto vd03;

		bind++;
			/* bind head 修正 */
		fseek( fp2, bind_point, SEEK_SET );
		DWORD( bind_head + 4 ) = bind_size;
		if( fwrite( bind_head, 32, 1, fp2 ) < 1 ){
			error = 2;	/* 2 = can't save */
			break;
		}
			/* total data size 修正 */
		fseek( fp2, 0, SEEK_SET );
		DWORD( head+4 ) = total;		/* data size */
		DWORD( head+8 ) = frame;		/* bind */
		if( fwrite( head, 256, 1, fp2 ) < 1 ){	/* ヘッダー */
			error = 2;	/* 2 = error num. */
			break;
		}

		/*
			 1bind終わるごとにしめる
			( 停電で止まってもそれまでのdataを残すため)
		*/

		fclose(fp2);
		if( ( fp2 = fopen( name2, "r+b" ) ) == NULL ){
			TL_free( buffer );
			fclose(fp1);
			if( sndflg )fclose( fps );
			return 2;
		}
		fseek( fp2, file_point, SEEK_SET );
	}
vd10:	TL_free( buffer );
	fclose(fp2);
	fclose(fp1);
	if( sndflg )fclose( fps );
	return error;
}


	/* メイン3 */

v02_to_v02( char *name1, char *name2, int flt, int soft, int color, int mode )
{
	int ret ;

	ret = v02_to_v02_conv( name1, name2, flt, soft, color, mode, 0 ) ;
	return ret ;
}

	/* テーブル参照 */

v02_to_v02_t
(
 char *name1, char *name2, int flt, int soft, int color, int mode, char *table
)
{
	int ret ;

	v02table1 = table ;
	v02table2 = v02table1 + 32768 ;
	brgtoyc   = v02table2 + 512   ;
	yytoy     = brgtoyc   + 65536 ;

	ret = v02_to_v02_conv( name1, name2, flt, soft, color, mode, 1 ) ;
	return ret ;
}

	/* v02ファイル→V02ファイル(tbckはテーブル参照フラグ) */
v02_to_v02_conv( name1, name2, flt, soft, color, mode, tbck )
char *name1, *name2 ;
int flt, soft, color, mode, tbck ;
{
	FILE *fp2, *fp1 ;
	int sw, cx, cy;
	int wait, freq, sndpack ;
	int i, j, size, count, error, step, rate ;
	int scount0, scount1, cmpmode;
	int bind_point, bind, bind_size, total, file_point, frame;
	int d0, d1;
	int ret, koma ;

	char para2[64];
	char head[256];
	char v02_head[256];
	char bind_head[32];
	char *buffer;		/* buffer */
	char *snd_buf, *buf, *new_ad, *old_ad,
	     *brg_buf, *ytable, *yc_buf, *data1_buf,
	     *make_buf, *check_buf, *data2_buf;	/* 差分圧縮用 */
	int  snd_b, buf_b, new_b, old_b, brg_b, ytab_b, yc_b, data1_b,
	     make_b, check_b, data2_b ;	   /* 移動補償の計算でワークに使う */

	char *nextBuf ;	/* median filter 用 */
	char *newBuf ;
	char *oldBuf ;
	char *subBuf ;

	char *nextBuf2 ;
	char *newBuf2 ;
	char *oldBuf2 ;
	char *subBuf2 ;

	rate = 0 ; step = 2 ;	/* ラフレート, 動き補償 (固定) */

	if( ( fp1 = fopen( name1, "rb" ) ) == NULL )return 1;
	if( fread( v02_head, 256, 1, fp1 ) < 1 ){	/* ヘッダー */
		fclose(fp1);
		return 1;
	}
	if( DWORD( v02_head + 0 ) == 0x32304456 )	/* VD02 */
	{
		v02xd = DWORD( v02_head + 16 );	/* 横 */
		v02yd = DWORD( v02_head + 20 );	/* 縦 */
		wait = DWORD( v02_head + 24 ) ; /* wait */
		koma = DWORD( v02_head + 28 ) ; /* bind koma */
		if( koma == 0 )koma = 8 ;
		freq = DWORD( v02_head + 32 ) ;
		sndpack = DWORD( v02_head + 36 ) ;
		if( sndpack == 0 )sndpack = 6400 ;
	}
	else
	{
		fclose(fp1);
		return 58;	/* データの形式が違うエラー */
	}
	fclose(fp1);
	ret = v02_1step_open( name1 ) ;
	if( ret )return ret ;

	if( ( fp2 = fopen( name2, "wb" ) ) == NULL )
	{
		v02_1step_close() ;
		return 2;
	}

	snd_b = sndpack;
	buf_b = (v02xd * v02yd * 2)*8 ;
	new_b = v02xd * v02yd * 5 / 4 * 4 ; /* 注意 4つ入らないとだめ!! */
	old_b = v02xd * v02yd * 5 / 4 ;
	brg_b = 32768 ;
	ytab_b = 512 ;
	yc_b = 65536 ;
	data1_b = v02xd * v02yd * 3 / 4 ;
	make_b =  v02xd * v02yd * 3 / 4 ;
	check_b = v02xd * v02yd * 3 / 4 ;
	data2_b = v02xd * v02yd * 4 + 0x1000 ;

	size = snd_b + buf_b + new_b + old_b
	+ brg_b + ytab_b + yc_b + data1_b + make_b + check_b + data2_b;
	if( (buffer = (char *)TL_malloc( size )) == NULL ){
		v02_1step_close() ;
		fclose(fp2);
		return 7;			/* out of memory */
	}
	snd_buf = buffer;
	buf     = snd_buf + snd_b; 	/* RGB org.(128*96*2) */
	new_ad  = buf + buf_b;		/* YC org.(15360) */
	old_ad  = new_ad + new_b;	/* YC 前frame(15360) */
	brg_buf = old_ad + old_b;
	ytable  = brg_buf + brg_b;
	yc_buf  = ytable + ytab_b;
	data1_buf = yc_buf + yc_b;
	make_buf = data1_buf + data1_b;
	check_buf = make_buf + make_b;
	data2_buf = check_buf + check_b;

	nextBuf = buf ;
	newBuf  = nextBuf + (v02xd * v02yd * 2) ;
	oldBuf  = newBuf  + (v02xd * v02yd * 2) ;
	subBuf  = oldBuf  + (v02xd * v02yd * 2) ;
	nextBuf2= subBuf  + (v02xd * v02yd * 2) ;
	newBuf2 = nextBuf2+ (v02xd * v02yd * 2) ;
	oldBuf2 = newBuf2 + (v02xd * v02yd * 2) ;
	subBuf2 = oldBuf2 + (v02xd * v02yd * 2) ;

		/* head */
	file_point = 0;
	for( i=0 ; i<64 ; i++ )DWORD( head + i*4 ) = 0;
	DWORD( head ) = 0x32304456;		/* ID = VD02 */
/*	DWORD( head + 4 ) total length	 */
/*	DWORD( head + 8 ) total frame	 */
	if( sndpack == 6400 )
		DWORD( head + 12 ) = 6;		/* dataの種類番号(bit/ピクセル) */
	else
		DWORD( head + 12 ) = 7;		/* dataの種類番号(bit/ピクセル) */
	DWORD( head + 16 ) = v02xd;		/* 横 */
	DWORD( head + 20 ) = v02yd;		/* 縦 */
	DWORD( head + 24 ) = wait;		/* wait */
	DWORD( head + 28 ) = koma;			/* バインダ内こま数 */
	DWORD( head + 32 ) = freq;		/* sound samp. freq */
	DWORD( head + 36 ) = sndpack;		/* バインダ内snd data Byte数 */

	if( fwrite( head, 256, 1, fp2 ) < 1 ){	/* ヘッダー */
		error = 2;			/* 2 = error num. */
		goto vd10;
	}
	file_point += 256;
	total = 0;

/*	テーブルの設定1 */
	if( tbck )
	{
		for( j=0 ; j<32768 ; j += 4 )
			DWORD( brg_buf + j ) = DWORD( v02table1 + j ) ;
	}
	else
		v00_to_v02_conv_brg_set_buffer( brg_buf, color );
/* brg data set */

	if( fwrite( brg_buf, 16384*2, 1, fp2 ) < 1 ){	/* パレット1 */
		error = 2;			/* 2 = error num. */
		goto vd10;
	}
	file_point += 32768;
	total += 32768;

/*	テーブルの設定2 */
	if( tbck )
	{
		for( j=0 ; j<512 ; j += 4 )
			DWORD( ytable + j ) = DWORD( v02table2 + j ) ;
		for( j=0 ; j<4096 ; j += 4 )
			DWORD( ycnvtable + j ) = DWORD( yytoy + j ) ;
	}
	else
		v00_to_v02_conv_ytable_set( ytable, ycnvtable ) ;
/* ycnvtableとはy記号変換するためのtableでこのソース内のstatic char *変数 */

	if( fwrite( ytable, 512, 1, fp2 ) < 1 ){	/* パレット2 */
		error = 2;			/* 2 = error num. */
		goto vd10;
	}
	file_point += 512;
	total += 512;

/*	テーブルの設定3 */
	if( tbck )
	{
		for( j=0 ; j<65536 ; j += 4 )
			DWORD( yc_buf + j ) = DWORD( brgtoyc + j ) ;
	}
	else
		v00_to_v02_conv_yc_set_buffer( yc_buf, brg_buf, color );

/* yc data set */
	EGB_writePage(guiEgbPtr,1);
	EGB_color( guiEgbPtr, 1, 0 ) ;
	EGB_clearScreen( guiEgbPtr ) ;

	EGB_displayStart(guiEgbPtr,3,0,0);
	EGB_displayStart(guiEgbPtr,2,1,1);
	EGB_displayStart(guiEgbPtr,2,2,2);
	EGB_displayStart(guiEgbPtr,0,(320 - (v02xd))/2,(240 - (v02yd))/2);
	EGB_displayStart(guiEgbPtr,1,0,0);
	EGB_displayStart(guiEgbPtr,3,v02xd,v02yd);
	EGB_writePage(guiEgbPtr,0);
	bind = 0;	/* バインダーカウント */
	frame = 0;	/* frameカウント */
	scount0 = v02xd*v02yd*4+100;	/* 前の絵と同じポイントのカウンター */

	int frflg ;		/* フレームリードフラグ */

	v02_1step_snd( snd_buf ) ;

	if( v02_1step_frame( nextBuf ) )
		frflg = 0 ;
	else
		frflg = 1 ;

	v00_to_v02_rgb_median( softSet[2][soft], nextBuf2, nextBuf ) ;
	v00_to_v02_rgb_back( softSet[3][soft], nextBuf2, nextBuf ) ;


	while( 1 ){
			/* bind_head */
		bind_point = file_point;
		for( i=0 ; i<8 ; i++ )DWORD( bind_head + i*4 ) = 0;
		DWORD( bind_head ) = bind;
			/* sound read */
//		v02_1step_snd( snd_buf ) ;	/* 1frame先を読むためここでは出来ない */
		bind_size = 0;		/* バインダー内data長カウント */
		count = 0;		/* バインダー内のframeカウント */

	vd03:
		MOS_rdpos(&sw,&cx,&cy);	/* sw == 3なら終了 */
		if( (frflg == 0) || ( sw == 3 ) )
		{
				/* 終了 */
		    if( count != 0 ){	/* bindの途中なら修正あり */
				/* bind head 修正 */
			fseek( fp2, bind_point, SEEK_SET );
			DWORD( bind_head + 4 ) = bind_size;
			if( fwrite( bind_head, 32, 1, fp2 ) < 1 ){
				error = 2;	/* 2 = can't save */
				break;
			}
				/* total data size 修正 */
			fseek( fp2, 0, SEEK_SET );
			DWORD( head+4 ) = total;		/* data size */
			DWORD( head+8 ) = frame;		/* bind */
			if( fwrite( head, 256, 1, fp2 ) < 1 ){	/* ヘッダー */
				error = 2;	/* 2 = error num. */
				break;
			}
		    }
		    error = 0;	/* 原画は全部読み切った(no error) */
		    break;		/* 終了 */
		}
		if( count == 0 )
		{
				/* bind head 登録 */
			if( fwrite( bind_head, 32, 1, fp2 ) < 1 ){
				error = 2;
				break;
			}
			file_point += 32;
			total += 32;
				/* sound 登録 */
			if( fwrite( snd_buf, sndpack, 1, fp2 ) < 1 ){
				error = 2;	/* 2 = can't save */
				break;
			}
			file_point += sndpack;
			total += sndpack;
			bind_size += sndpack;
		}

		for( i=0 ; i<(v02xd*v02yd*2) ; i += 2 )
			WORD( newBuf + i ) = WORD( nextBuf + i ) ;
		for( i=0 ; i<(v02xd*v02yd*2) ; i += 2 )
			WORD( newBuf2 + i ) = WORD( nextBuf2 + i ) ;

		if( ((frame+1) % koma) == 0 )	/* これを忘れると暴走 */
			v02_1step_snd( snd_buf ) ;

		if( v02_1step_frame( nextBuf ) )
			frflg = 0 ;
		else
			frflg = 1 ;

		v00_to_v02_rgb_median( softSet[2][soft], nextBuf2, nextBuf ) ;
		v00_to_v02_rgb_back( softSet[3][soft], nextBuf2, nextBuf ) ;

			/* buf(原画) -> buf(yc-data) */
		v00_to_v02_conv_brg_to_yc( new_ad, newBuf, yc_buf );
			/* 前の絵と同じ点をカウント */
		scount1 = 0;
		for( i=0 ; i<v02xd*v02yd ; i++ ){
		    d0 = BYTE( new_ad + i ) - BYTE( old_ad + i );
		    if( d0>-2 && d0<2 )scount1++;
		}
		if( scount1 < (scount0 >> 2) )cmpmode = 0;
		else cmpmode = 1;
		scount0 = scount1;
		if( frame == 0 )
			scount0 = 0;
			/* 圧縮 */
		if( cmpmode == 0 ){

			if( softSet[4][soft] )
				v00_to_v02_conv_brg_to_y( new_ad, newBuf2, yc_buf ) ;
			else
				v00_to_v02_conv_brg_to_y( new_ad, newBuf, yc_buf ) ;

			if( softSet[5][soft] )
				v00_to_v02_conv_brg_to_c( new_ad, newBuf2, yc_buf ) ;
			else
				v00_to_v02_conv_brg_to_c( new_ad, newBuf, yc_buf ) ;

			size = v00_to_v02_conv_cmp1(
			flt, rate, step, softSet[6][soft], mode, data2_buf,
			new_ad, old_ad, data1_buf, make_buf, check_buf
			);
		}
		else {

			v00_to_v02_time_rgb_median(
					softSet[0][soft], subBuf, nextBuf, newBuf, oldBuf);
			v00_to_v02_rgb_back( softSet[1][soft], subBuf, newBuf ) ;

			v00_to_v02_time_rgb_median(
					softSet[0][soft], subBuf2, nextBuf2, newBuf2, oldBuf2);
			v00_to_v02_rgb_back( softSet[1][soft], subBuf2, newBuf ) ;

			if( softSet[4][soft] )
				v00_to_v02_conv_brg_to_y( new_ad, subBuf2, yc_buf ) ;
			else
				v00_to_v02_conv_brg_to_y( new_ad, subBuf, yc_buf ) ;

			if( softSet[5][soft] )
				v00_to_v02_conv_brg_to_c( new_ad, subBuf2, yc_buf ) ;
			else
				v00_to_v02_conv_brg_to_c( new_ad, subBuf, yc_buf ) ;

			size = v00_to_v02_conv_cmp2(
			flt, rate, step, softSet[6][soft], mode, data2_buf,
			new_ad, old_ad, data1_buf, make_buf, check_buf
			);

		}

		for( i=0 ; i<v02xd*v02yd*2 ; i += 2 )
			WORD( oldBuf + i ) = WORD( newBuf + i ) ;
		for( i=0 ; i<v02xd*v02yd*2 ; i += 2 )
			WORD( oldBuf2 + i ) = WORD( newBuf2 + i ) ;

			/* check */
		VSYNC_counter = 0;
		DWORD(para2+0) = (int)make_buf;
		DWORD(para2+4) = (int)brg_buf;
		DWORD(para2+8) = 100;
		DWORD(para2+12) = 1;
		DWORD(para2+16) = v02xd;
		DWORD(para2+20) = v02yd;
		vd_wide_01_2( para2 );

		v_cmp_frame_dsp( frame ) ;

		if( fwrite( data2_buf, size, 1, fp2 ) < 1 ){
			error = 2;	/* 2 = can't save */
			break;
		}
		file_point = file_point + size;
		total = total + size;
		bind_size = bind_size + size;
		count++;
		frame++;
		if( count < koma )goto vd03;

		bind++;
			/* bind head 修正 */
		fseek( fp2, bind_point, SEEK_SET );
		DWORD( bind_head + 4 ) = bind_size;
		if( fwrite( bind_head, 32, 1, fp2 ) < 1 ){
			error = 2;	/* 2 = can't save */
			break;
		}
			/* total data size 修正 */
		fseek( fp2, 0, SEEK_SET );
		DWORD( head+4 ) = total;		/* data size */
		DWORD( head+8 ) = frame;		/* bind */
		if( fwrite( head, 256, 1, fp2 ) < 1 ){	/* ヘッダー */
			error = 2;	/* 2 = error num. */
			break;
		}

		/*
			 1bind終わるごとにしめる
			( 停電で止まってもそれまでのdataを残すため)
		*/

		fclose(fp2);
		if( ( fp2 = fopen( name2, "r+b" ) ) == NULL ){
			TL_free( buffer );
			v02_1step_close() ;
			return 2;
		}
		fseek( fp2, file_point, SEEK_SET );
	}
vd10:
	TL_free( buffer );
	fclose(fp2);
	v02_1step_close() ;
	return error;
}

/* v02 to v02 絵の取り出しサブルーチン */

static v02_1step_open( name )	/* open */
char *name ;
{
	char dummy[4] ;
	return v02_1step_read( name, 0, dummy ) ;
}

static v02_1step_snd( buf )		/* snd read */
char *buf ;
{
	char dummy[4] ;
	return v02_1step_read( dummy, 1, buf ) ;
}

static v02_1step_frame( buf )	/* frame read */
char *buf ;
{
	char dummy[4] ;
	return v02_1step_read( dummy, 2, buf ) ;
}

static v02_1step_close()		/* close */
{
	char dummy[4] ;
	return v02_1step_read( dummy, 3, dummy ) ;
}

	/*
		name: ファイル名
	    mode: 0:open, 1:sound read, 2:frame read, 3:end
		dpoint: data point
		dsize: data size
	*/

static v02_1step_read( name, mode, output )
char *name ;
char *output ;
int mode ;
{
	static	FILE *fpoint ;
	static	int frame ;
	static	int xd ;
	static	int yd ;
	static	int koma ;
	static	int sndpack ;
	static	int bsize ;		/* bind buffer の大きさの許容の最大値 */ 
	static	char *buf ;
	static	char *table ;
	static	char *bind ;
	static	char *wk ;
	static	char play_para[64] ;

	static	int fcount ;	/* frame counter */
	static	int bcount ;	/* bind内 frame counter */
	static	char *point ;	/* bind内 位置 */

	static	ret = -1 ;

	int wsize, size ;
	int i ;
	char head[256], bhead[32] ;
	char para[64] ;

	switch( mode )
	{
	case 0:
		goto mode0 ;
	case 1:
		goto mode1 ;
	case 2:
		goto mode2 ;
	case 3:
		goto mode3 ;
	}

mode0:	/* open */
	if( ( fpoint = fopen( name, "rb" ) ) == NULL )
	{
		ret = 1 ;
		return ret ;
	}
	if( fread( head, 256, 1, fpoint ) < 1 )	/* head read */
	{
		fclose( fpoint ) ;
		ret = 1 ;
		return ret ;
	}

	if( DWORD( head ) != 0x32304456		/* IDcheck (VD02) */
	 || ( DWORD( head + 12 ) != 6 &&  DWORD( head + 12 ) != 7)
	 || DWORD( head + 16 ) < 8			/* x */
	 || DWORD( head + 20 ) < 8			/* y */
	){
		fclose(fpoint);
		ret = 58 ;
		return ret ;
	}

	frame = DWORD( head + 8 );	/* total frame */
	xd = DWORD( head + 16 );			/* x */
	yd = DWORD( head + 20 );			/* y */
	koma = DWORD( head + 28 );
	if( koma == 0 )koma = 8 ;
	sndpack = DWORD( head + 36 ) ;
	if( sndpack == 0 )sndpack = 800*8 ;

	if( frame <= 0 )
	{
		fclose( fpoint ) ;
		ret = 58 ;
		return ret ;
	}

	wsize = ( xd * yd * 2 * 3 / 8 ) * 2 ;
	bsize = wsize * koma + sndpack ;

	if( (buf = (char *)TL_malloc( 32768+512+wsize+bsize+1000 )) == NULL )
	{			/* ctable + ytable + work size + bind size + クッション */
		fclose( fpoint ) ;
		ret = 7 ;
		return ret ;
	}
	table = buf ;						/* bufの割りつけ */
	wk    = table + 32768 + 512 ;
	bind  = wk + wsize ;

	if( fread( table, 32768+512, 1, fpoint ) < 1 )	/* table read */
	{
		fclose( fpoint ) ;
		TL_free( buf ) ;
		ret = 1 ;
		return ret ;
	}

	DWORD(play_para+0) = (int)bind + sndpack ;		/* 圧縮data */
	DWORD(play_para+4) = (int)wk ;					/* work */
	DWORD(play_para+8) = (int)table ;				/* table */
	DWORD(play_para+12) = 6 ;						/* wait */
	DWORD(play_para+16) = 0 ;						/* page */
	DWORD(play_para+20) = xd ;						/* 横 */
	DWORD(play_para+24) = yd ;						/* 縦 */
	DWORD(play_para+28) = xd * yd * 2 * 3 / 8 ;		/* ycdata length */
	DWORD(play_para+32) = 0;						/* 急ぎフラグ */
	VSYNC_counter = 0 ;
	vd_wide_02_init( 6, 0x40000, 0x40000 );				/* init */
	fcount = 0 ;
	ret = NOERR ;
	return ret ;

mode1:	/* sound read ( 1bind read ) */
	if( ret )
		return ret ;
	if( fread( bhead, 32, 1, fpoint ) < 1 )		/* bind read */
		goto ed01 ;
	size = DWORD( bhead + 4 ) ;
	if( ( size > bsize ) || ( size <= 0 ) )
		goto ed01 ;
	if( fread( bind, size, 1, fpoint ) < 1 )		/* bind read */
		goto ed01 ;
	for( i=0 ; i<sndpack ; i++ )
		BYTE( output + i ) = BYTE( bind + i ) ;
	point = bind + sndpack ;		/* 圧縮data */
	bcount = 0 ;			/* bind koma counter */
	ret = NOERR ;
	return ret ;
ed01:
	fclose( fpoint ) ;
	TL_free( buf ) ;
	ret = 58 ;
	return ret ;

mode2:	/* frame read */
	if( ret )
		return ret ;
	if( fcount >= frame )goto ed02 ;
	if( bcount >= koma )goto ed02 ;
	DWORD(play_para+0) = (int)point ;		/* 圧縮data */
	DWORD(play_para+32) = 1;	/* 急ぎフラグ  これを立てて表示を回避 */
	point = (char *)( vd_wide_02_c( play_para ) ) ;	/* main */

	v02_1step_read_sub( wk, table, output, xd, yd ) ;
	fcount++ ;
	bcount++ ;
	ret = NOERR ;
	return ret ;
ed02:
	fclose( fpoint ) ;
	TL_free( buf ) ;
	ret = 58 ;
	return ret ;

mode3: /* close */
	if( ret )
		return ret ;
	fclose( fpoint ) ;
	TL_free( buf ) ;
	ret = -1 ;
	return NOERR ;
}

static v02_1step_read_sub( char *in, char *table, char *out, int dx, int dy )
{
	int x, y ;

	for( y=0 ; y<dy ; y++ )
	{
		for( x=0 ; x<dx ; x++ )
		{
			int y0, c0, n ;

			n = y*dx + x ;
			y0 = BYTE( in + n/2 ) ;
			y0 = WORD( table + 32768 + y0*2 ) ;
			if( n % 2 )
				y0 = ( y0 >> 8 ) & 0x3f ;
			else
				y0 = y0 & 0x3f ;

			n = (y/2)*(dx/2) + (x/2) ;
			c0 = BYTE( in + dx*dy/2 + n ) ;

			WORD( out + y*dx*2 + x*2 ) = WORD( table + (y0*256 + c0)*2 ) ;
		}
	}

	return NOERR ;
}

/*
	v02 play
*/

int v02_short_stop_flg;

int v02_play_check()
{
	int sw;

/*	注意:sw 押されているとbit off 押されてないとbit on 	*/

	SND_joy_in_1( 0xff, &sw );		/* end check */
	sw = (sw ^ 0xff) & 0x30;
	if( sw == 0 )return 0;
c01:	SND_joy_in_1( 0xff, &sw );
	sw = (sw ^ 0xff) & 0x30;
	if( sw & 0x10 ){
		while( sw == 0x30 ){
			SND_joy_in_1( 0xff, &sw );
			sw = (sw ^ 0xff) & 0x30;
		}
		if( sw == 0x20 )goto c02;
		while( sw ){
			SND_joy_in_1( 0xff, &sw );
			sw = (sw ^ 0xff) & 0x30;
			if( sw == 0x30 )break;
		}
		return 1;
	}
c02:	if( sw )goto c01;
	v02_short_stop_flg = -1;
	return -1;
}

v02_play( char *name, int mode, int wait )
{
	int xd, yd, size, frame, error;
	char *buffer;

vd01:	v02_short_stop_flg = 0;
	error = V02_wide_disk_play_init( name, v02_play_check, &frame, &xd, &yd,
	&size );
	if( error )
		goto vd11;
	if( (buffer = (char *)TL_malloc( size )) == NULL )
	{
		error = 7;
		goto vd11;			/* out of memory */
	}

		/* mute */
	int s ;					/* こうするとpcmだけをonにできる */
	SND_get_elevol_mute( &s ) ;
	SND_elevol_mute( s | 0x01 ) ;

	SND_fm_timer_b_set( 0, 0 ) ;
	error = V02_wide_disk_play( buffer, name, 1, wait );
	SND_fm_timer_b_start() ;

vd10:	TL_free( buffer );
vd11:	SND_pcm_sound_delete( -1 );
	return error;
}

v02_graph_init( int md, int xd, int yd )
{
	int x[4], y[4] ;

	if( (md < 1) || (md > 4) )
		return 100;
	v02_graph_init_sub( md, 0,0,xd-1,yd-1, x, y ) ;

	EGB_displayPage(guiEgbPtr,0,1);
	EGB_writePage(guiEgbPtr,1);
	EGB_displayStart(guiEgbPtr,3,0,0);
	EGB_displayStart(guiEgbPtr,2,1,1);
	EGB_displayStart(guiEgbPtr,2,x[2],y[2]);
	EGB_displayStart(guiEgbPtr,0,x[0],y[0]);
	EGB_displayStart(guiEgbPtr,1,x[1],y[1]);
	EGB_displayStart(guiEgbPtr,3,x[3],y[3]);
	EGB_writePage(guiEgbPtr,0);
	EGB_displayPage(guiEgbPtr,0,3);

	return 0;
}

static v02_graph_init_sub
(
	int zoom, int x0, int y0, int x1, int y1, int *x, int *y
)
{
	int xd, yd ;

	xd = x1-x0+1 ;
	yd = y1-y0+1 ;

	if( 640/zoom >= xd )
	{
		x[0] = ( 640/zoom - xd )/2 ;
		x[1] = x0 ;
		x[2] = zoom ;
		x[3] = xd ;
	}
	else
	{
		x[0] = 0 ;
		x[1] = x0 - ( 640/zoom - xd )/2 ;
		x[2] = zoom ;
		x[3] = 640/zoom ;
	}

	if( 480/zoom >= yd )
	{
		y[0] = ( 480/zoom - yd )/2 ;
		y[1] = y0 ;
		y[2] = zoom ;
		y[3] = yd ;
	}
	else
	{
		y[0] = 0 ;
		y[1] = y0 - ( 480/zoom - yd )/2 ;
		y[2] = zoom ;
		y[3] = 480/zoom ;
	}

	return NOERR ;
}

