#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 <tifflib.h>
#include "gratif.h"
//#include "ugoku.h"
#include "video.h"
#include "movrlf.h"

extern int tiffSaveCompMode ;	/*  tiff save mode 0:normal, 1:comp. */

int		setupSens ;		/* para */
int		setupCutoff ;
int		setupSoft ;
int		setupY ;
int		setupC ;
int		setupP ;
int		setupBibun ;
int		setupRay ;

static int xd, yd ;	/* 原画の大きさ */
static int tifNum ;

int tiffRelief()
{
	char para[64] ;
	char tiffname[100];
	int i, j, k ;
	int error ;

	error = NOERR;
	if( tifTotal == 0 )return BASEERR;
	if( tifOutPath[0] == 0 )return BASEERR;

	error = underSet() ;	/* underTypeMode = 1のときは窓を開ける */
	if( error )return UNDERERR ;

	if( underTypeMode != 1 )
	{
		r_panel_make( 0, 0, 639, 19 ) ;
		r_waku_clear2( 320, 240, 2 ) ;
		MG_mosDisp( 0 ) ;
	}

	tifNum = 0 ;
	for( k=0 ; k<tifTotal ; k++ )
	{
		for( i = 0 ; i<80 ; i++ )
		{
			tiffname[i] = tifPath[i];
			if( tiffname[i] == (char)0 )break;
		}
//		if( tiffname[i-1] != '\\' )tiffname[i++] = '\\';
		for( j=0 ; j<13 ; j++ ){
			tiffname[i++] = tifName[k][j];
		}
		if( loadtif( tiffname ) == NOERR )
		{
			xd = xtif ;			/* 読み込んだtifデータの大きさ */
			yd = ytif ;
			if( xd > 320 )xd = 320 ;
			if( yd > 240 )yd = 240 ;

			EGB_writePage(guiEgbPtr,1);
			DWORD( para + 0 ) = (unsigned int)orgBuf ;
			WORD( para + 4 ) = 0x14 ;
			WORD( para + 6 ) = 0 ;
			WORD( para + 8 ) = 0 ;
			WORD( para + 10 ) = 319 ;
			WORD( para + 12 ) = 239 ;
			EGB_getBlock(guiEgbPtr,para);
			EGB_writePage(guiEgbPtr,0);

			switch( effectMode )
			{
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
				mode0_4() ;
				break ;
			case 5:
				mode5() ;
				break ;
			case 6:
			case 7:
			case 8:
			case 9:
			case 10:
				mode6_10() ;
				break ;
			case 11:
				mode11() ;
				break ;
			}

			error = saveTiff() ;
			if( error == NOERR )tifNum++ ;
		}

		if( error )break ;
		if( check() )break ;
	}
	tifOutTotal = tifNum ;

	EGB_writePage(guiEgbPtr,0);
	EGB_paintMode(guiEgbPtr,0x22);
	EGB_writeMode(guiEgbPtr,0);

	MG_mosDisp( 1 ) ;
	r_waku_clear1( 320, 240, 2 ) ;
	r_panel_del() ;
	r_waku_del() ;

	return error ;
}

tiffPlay()
{
	char tiffname[100];
	int pt ;
	int i, k, temp ;

	temp = 0 ;
	if( tifOutPath[0] == 0 )return 1 ;

	EGB_writePage(guiEgbPtr,1);
	EGB_color( guiEgbPtr, 1, 0 ) ;
	EGB_clearScreen(guiEgbPtr);			/* clear */
	EGB_displayStart(guiEgbPtr,3,0,0);
	EGB_displayStart(guiEgbPtr,2,1,1);
	EGB_displayStart(guiEgbPtr,2,2,2);
	EGB_displayStart(guiEgbPtr,0,0,0);
	EGB_displayStart(guiEgbPtr,1,0,0);
	EGB_displayStart(guiEgbPtr,3,320,240);
	EGB_displayPage(guiEgbPtr,0,3);
	EGB_writePage(guiEgbPtr,0);			/* pageを元に戻さないと暴走 */

	r_panel_make( 0, 0, 639, 19 ) ;
	r_waku_clear2( 320, 240, 2 ) ;

	MG_mosDisp( 2 ) ;
	for( k=0 ; k<0x7fffffff ; k++ )
	{
		for( i = 0 ; i<80 ; i++ ){
			tiffname[i] = tifOutPath[i] ;
			if( tiffname[i] == 0 )break;
		}
		pt = i;
		itochar( k, 4, tiffname+pt );
		tiffname[pt+4] = '.';
		tiffname[pt+5] = 'T';
		tiffname[pt+6] = 'I';
		tiffname[pt+7] = 'F';
		tiffname[pt+8] = 0;
		if( loadtif( tiffname ) )
			break ;
		temp = playCheck() ;
		if( temp == -1 )break ;
	}
	EGB_writePage(guiEgbPtr,0);
	EGB_paintMode(guiEgbPtr,0x22);
	EGB_writeMode(guiEgbPtr,0);

	MG_mosDisp( 3 ) ;

	r_waku_clear1( 320, 240, 2 ) ;
	r_panel_del() ;
//	r_waku_del() ;

	return NOERR ;
}

/*
   画面を ファイル名 tifOutPath+ ???? + .tif で save
*/

static saveTiff()
{
	int i, pt ;
	char tiffname[100];

	for( i = 0 ; i<80 ; i++ ){
		tiffname[i] = tifOutPath[i] ;
		if( tiffname[i] == 0 )break;
	}
	if( i == 0 )return 2 ;

	pt = i;

	itochar( tifNum, 4, tiffname+pt );
	tiffname[pt+4] = '.';
	tiffname[pt+5] = 'T';
	tiffname[pt+6] = 'I';
	tiffname[pt+7] = 'F';
	tiffname[pt+8] = 0;

	return tif_save_sub( tiffname );
}

static tif_save_sub( fullname )
char *fullname;
{
	/*	workは COMP_WORK_SIZE + SAVEBUFSIZE + GETBUFSIZEバイト必要 */

	char *work ;
	int ret ;
	char para[64] ;

	EGB_writePage(guiEgbPtr,1);
	DWORD( para + 0 ) = (unsigned int)bbuf ;
	WORD( para + 4 ) = 0x14 ;
	WORD( para + 6 ) = 0 ;
	WORD( para + 8 ) = 0 ;
	WORD( para + 10 ) = xd - 1 ;
	WORD( para + 12 ) = yd - 1 ;
	EGB_getBlock(guiEgbPtr,para);
	EGB_writePage(guiEgbPtr,0);

	if( (work=(char *)TL_malloc(COMP_WORK_SIZE+SAVEBUFSIZE+GETBUFSIZE))
			 == NULL )
	{
		ret = OUT_OF_MEMORY ;
	}
	else
	{
		ret = tifSave32kAnd16m( work, fullname, bbuf,
							16, tiffSaveCompMode, xd, yd ) ;

		TL_free( work ) ;
	}

	return ret ;
}

static itochar(n,count,p)	/* n --> char p[count] */
int n,count;
char *p;
{
	int temp;
	char dsp[] = "0123456789ABCDEF";

	if( n < 0 ){
		*p++ = '-';
		*p++ = '-';
		*p++ = '-';
		*p = (char)0;
		return 0;
	}
	switch( count ){
		case 8: temp = n/10000000;
			if( temp > 15 )break;
			*p++ = dsp[ temp ];
			n = n % 10000000;
		case 7: temp = n/1000000;
			if( temp > 15 )break;
			*p++ = dsp[ temp ];
			n = n % 1000000;
		case 6: temp = n/100000;
			if( temp > 15 )break;
			*p++ = dsp[ temp ];
			n = n % 100000;
		case 5: temp = n/10000;
			if( temp > 15 )break;
			*p++ = dsp[ temp ];
			n = n % 10000;
		case 4: temp = n/1000;
			if( temp > 15 )break;
			*p++ = dsp[ temp ];
			n = n % 1000;
		case 3: temp = n/100;
			if( temp > 15 )break;
			*p++ = dsp[ temp ];
			n = n % 100;
		case 2: temp = n/10;
			if( temp > 15 )break;
			*p++ = dsp[ temp ];
			n = n % 10;
		case 1: temp = n;
			if( temp > 15 )break;
			*p++ = dsp[ temp ];
			*p = (char)0;
	}
	return 0;
}


int v00Relief()
{
	FILE *fp, *fps ;
	char para[64] ;
	char head[256] ;
	int i, temp ;
	int error ;
	int frame, x, y, wt ;
	int v00xd, v00yd, orgWait, size ;

	error = NOERR;
	if( v00Path[0] == 0 )return BASEERR;
	if( v00OutPath[0] == 0 )return BASEERR;

	temp = 0 ;
	for( i=0 ; i<100 ; i++ )
	{
		int a, b ;

		a = (unsigned int)v00Path[i] ; b = (unsigned int)v00OutPath[i] ;
		if( ( a > 0x60 ) && ( a < 0x7b ) )
			a &= 0xdf ;
		if( ( b > 0x60 ) && ( b < 0x7b ) )
			b &= 0xdf ;
		if( ( a == 0 ) && ( b == 0 ) )
			break ;
		if( a != b )
		{
			temp = 1 ;
			break ;
		}
	}
	if( (temp == 0) && (i > 0) )
		return 52 ;		/* ファイルの重複 */

	error = underSet() ;	/* underTypeMode = 1のときは窓を開ける */
	if( error )return UNDERERR ;
	if( underTypeMode == 1 )
	{
		r_waku_clear1( 320, 240, 2 ) ;
		r_panel_del() ;
		r_waku_del() ;
		MG_mosDisp( 0 ) ;
	}

	error = v00_head_read( v00Path, &frame, &x, &y, &wt ) ;
	if( error )
	{
		MG_mosDisp( 1 ) ;
		return error ;
	}
	if( frame < 0 )frame = 0x7fffffff ;
	v_waku_clear( x, y, 2 ) ;		/* 枠抜き */
	MG_mosDisp( 0 ) ;

	if( ( fp = fopen( v00Path, "rb" ) ) == NULL )
	{
		error = 1;
		goto v10 ;
	}

	if( fread( head, 256, 1, fp ) < 1 ){	/* ヘッダー */
		fclose(fp);
		error = 1;
		goto v10 ;
	}
	if
	(
	    (DWORD( head +  0 ) == 0x30304456)	/* VD00 */
	 && (DWORD( head + 12 ) == 0x80008000)	/* dataの種類番号 */
	)
	{
		v00xd = DWORD( head + 16 );	/* 横 */
		v00yd = DWORD( head + 20 );	/* 縦 */
		orgWait = DWORD( head + 24 );	/* オリジナルwait */
	}
	else
	{
		/* 古いタイプのv00ならヘッダを作り直す */
		v00xd = 128;			/* 横 */
		v00yd = 96;			/* 縦 */
		orgWait = 6;			/* 縦 */

		for( i=0 ; i<64 ; i++ )DWORD( head + i*4 ) = 0;
		DWORD( head ) = 0x30304456;		/* ID = VD00 */
		DWORD( head + 4 ) = frame * v00xd * v00yd * 2; /* total length */
		DWORD( head + 8 ) = frame;		/* total frame	 */
		DWORD( head + 12 ) = 0x80008000;	/* dataの種類番号 */
		DWORD( head + 16 ) = v00xd;		/* 横 */
		DWORD( head + 20 ) = v00yd;		/* 縦 */
		DWORD( head + 24 ) = orgWait;		/* wait */

		fseek( fp, 0, SEEK_SET );
	}
	if( v00xd * v00yd > 320*240 ){	/* 規格オーバー */
		fclose(fp);
		error = 58;
		goto v10 ;
	}

	if( ( fps = fopen( v00OutPath, "wb" ) ) == NULL ){
		fclose(fp);
		error = 2;
		goto v10 ;
	}
	if( fwrite( head, 256, 1, fps ) < 1 ){	/* ヘッダー */
		fclose(fps);
		fclose(fp);
		error = 2;
		goto v10 ;
	}

	EGB_writePage(guiEgbPtr,1);
	EGB_displayStart(guiEgbPtr,1,0,0);
	EGB_displayStart(guiEgbPtr,2,2,2);
	EGB_displayStart(guiEgbPtr,3,v00xd,v00yd);
	EGB_displayStart(guiEgbPtr,0,(320-v00xd)/2,(240-v00yd)/2);
	EGB_displayPage(guiEgbPtr,0,3);

	size = v00xd*v00yd*2;		/* 先送り */
	xd = v00xd ;			/* 原画の大きさ */
	yd = v00yd ;

	for( i=0 ; i<frame ; i++ )
	{
		if( fread( orgBuf, size, 1, fp ) < 1 )break ;
		DWORD(para+0) = (unsigned int)orgBuf;
		WORD(para+4) = 0x14;
		WORD(para+6) = 0;
		WORD(para+8) = 0;
		WORD(para+10) = 0 + v00xd - 1;
		WORD(para+12) = 0 + v00yd - 1;
		EGB_putBlock( guiEgbPtr, 0, para );

		DWORD( para + 0 ) = (unsigned int)orgBuf ;
		WORD( para + 4 ) = 0x14 ;
		WORD( para + 6 ) = 0 ;
		WORD( para + 8 ) = 0 ;
		WORD( para + 10 ) = 319 ;
		WORD( para + 12 ) = 239 ;
		EGB_getBlock(guiEgbPtr,para);

		switch( effectMode )
		{
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
			mode0_4() ;
			break ;
		case 5:
			mode5() ;
			break ;
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
			mode6_10() ;
			break ;
		case 11:
			mode11() ;
			break ;
		}

		DWORD(para+0) = (unsigned int)orgBuf;
		WORD(para+4) = 0x14;
		WORD(para+6) = 0;
		WORD(para+8) = 0;
		WORD(para+10) = 0 + v00xd - 1;
		WORD(para+12) = 0 + v00yd - 1;
		EGB_getBlock(guiEgbPtr,para);
		if( fwrite( orgBuf, size, 1, fps ) < 1 )
		{
			error = 2 ;
			break ;
		}
		if( check() )
		{
			i++ ;
			break ;
		}
	}

		/* total data size 修正 */
	fseek( fps, 0, SEEK_SET );
	DWORD( head + 4 ) = i * v00xd * v00yd * 2; /* total length */
	DWORD( head + 8 ) = i;			/* total frame	 */
	if( fwrite( head, 256, 1, fps ) < 1 ){	/* ヘッダー */
		fclose(fps);
		fclose(fp);
		error = 2;
		goto v10 ;
	}

	fclose(fps);
	fclose(fp);

	error = sndTrans( v00Path, v00OutPath ) ;

v10:
	EGB_writePage(guiEgbPtr,0);
	EGB_paintMode(guiEgbPtr,0x22);
	EGB_writeMode(guiEgbPtr,0);

	MG_mosDisp( 1 ) ;
	r_waku_clear1( 320, 240, 2 ) ;
	r_panel_del() ;
	r_waku_del() ;

	return error ;
}

int mvbRelief()
{
	FILE *fp, *fps ;
	char para[64] ;
	char head[256] ;
	int i, temp ;
	int error ;
	int frame, x, y, wt ;
	int mvbxd, mvbyd, size, hlen ;

	error = NOERR;
	if( mvbPath[0] == 0 )return BASEERR;
	if( mvbOutPath[0] == 0 )return BASEERR;

	temp = 0 ;
	for( i=0 ; i<100 ; i++ )
	{
		int a, b ;

		a = (unsigned int)mvbPath[i] ; b = (unsigned int)mvbOutPath[i] ;
		if( ( a > 0x60 ) && ( a < 0x7b ) )
			a &= 0xdf ;
		if( ( b > 0x60 ) && ( b < 0x7b ) )
			b &= 0xdf ;
		if( ( a == 0 ) && ( b == 0 ) )
			break ;
		if( a != b )
		{
			temp = 1 ;
			break ;
		}
	}
	if( (temp == 0) && (i > 0) )
		return 52 ;		/* ファイルの重複 */

	error = underSet() ;	/* underTypeMode = 1のときは窓を開ける */
	if( error )return UNDERERR ;
	if( underTypeMode == 1 )
	{
		r_waku_clear1( 320, 240, 2 ) ;
		r_panel_del() ;
		r_waku_del() ;
		MG_mosDisp( 0 ) ;
	}

	error = mvb_head_read( mvbPath, &frame, &x, &y, &wt ) ;
	if( error )
	{
		MG_mosDisp( 1 ) ;
		return error ;
	}
	v_waku_clear( x, y, 2 ) ;		/* 枠抜き */
	MG_mosDisp( 0 ) ;

	if( ( fp = fopen( mvbPath, "rb" ) ) == NULL )
	{
		error = 1;
		goto v10 ;
	}

	if( fread( head, 24, 1, fp ) < 1 ){	/* ヘッダー */
		fclose(fp);
		error = 1;
		goto v10 ;
	}
	mvbxd = DWORD( head + 8 );	/* 横 */
	mvbyd = DWORD( head + 12 );	/* 縦 */
	frame = DWORD( head + 16 );	/* frame */
	hlen = DWORD( head + 4 ) ;
	if( hlen > 252 )
	{
		hlen = 20 ;
		DWORD( head + 4 ) = hlen ;
	}
	fseek( fp, 0, SEEK_SET );
	if( fread( head, hlen+4, 1, fp ) < 1 ){	/* ヘッダー */
		fclose(fp);
		error = 1;
		goto v10 ;
	}
	if( mvbxd * mvbyd > 320*240 ){	/* 規格オーバー */
		fclose(fp);
		error = 58;
		goto v10 ;
	}

	if( ( fps = fopen( mvbOutPath, "wb" ) ) == NULL )
	{
		fclose(fp);
		error = 2;
		goto v10 ;
	}

	if( fwrite( head, hlen+4, 1, fps ) < 1 ){	/* ヘッダー */
		fclose(fps);
		fclose(fp);
		error = 2;
		goto v10 ;
	}

	EGB_writePage(guiEgbPtr,1);
	EGB_displayStart(guiEgbPtr,1,0,0);
	EGB_displayStart(guiEgbPtr,2,2,2);
	EGB_displayStart(guiEgbPtr,3,mvbxd,mvbyd);
	EGB_displayStart(guiEgbPtr,0,(320-mvbxd)/2,(240-mvbyd)/2);
	EGB_displayPage(guiEgbPtr,0,3);

	size = mvbxd*mvbyd*2;
	xd = mvbxd ;			/* 原画の大きさ */
	yd = mvbyd ;

	for( i=0 ; i<frame ; i++ )
	{
		if( fread( orgBuf, size, 1, fp ) < 1 )break ;
		DWORD(para+0) = (unsigned int)orgBuf;
		WORD(para+4) = 0x14;
		WORD(para+6) = 0;
		WORD(para+8) = 0;
		WORD(para+10) = 0 + mvbxd - 1;
		WORD(para+12) = 0 + mvbyd - 1;
		EGB_putBlock( guiEgbPtr, 0, para );

		DWORD( para + 0 ) = (unsigned int)orgBuf ;
		WORD( para + 4 ) = 0x14 ;
		WORD( para + 6 ) = 0 ;
		WORD( para + 8 ) = 0 ;
		WORD( para + 10 ) = 319 ;
		WORD( para + 12 ) = 239 ;
		EGB_getBlock(guiEgbPtr,para);

		switch( effectMode )
		{
		case 0:
		case 1:
		case 2:
		case 3:
		case 4:
			mode0_4() ;
			break ;
		case 5:
			mode5() ;
			break ;
		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
			mode6_10() ;
			break ;
		case 11:
			mode11() ;
			break ;
		}

		DWORD(para+0) = (unsigned int)orgBuf;
		WORD(para+4) = 0x14;
		WORD(para+6) = 0;
		WORD(para+8) = 0;
		WORD(para+10) = 0 + mvbxd - 1;
		WORD(para+12) = 0 + mvbyd - 1;
		EGB_getBlock(guiEgbPtr,para);
		if( fwrite( orgBuf, size, 1, fps ) < 1 )
		{
			error = 2 ;
			break ;
		}
		if( check() )
		{
			i++ ;
			break ;
		}
	}

		/* total data size 修正 */
	fseek( fps, 0, SEEK_SET );
	DWORD( head + 16 ) = i ;	/* frame */
	if( fwrite( head, 24, 1, fps ) < 1 ){	/* ヘッダー */
		fclose(fps);
		fclose(fp);
		error = 2;
		goto v10 ;
	}

	fclose(fps);
	fclose(fp);

	error = sndTrans( mvbPath, mvbOutPath ) ;

v10:
	EGB_writePage(guiEgbPtr,0);
	EGB_paintMode(guiEgbPtr,0x22);
	EGB_writeMode(guiEgbPtr,0);

	MG_mosDisp( 1 ) ;
	r_waku_clear1( 320, 240, 2 ) ;
	r_panel_del() ;
	r_waku_del() ;

	return error ;
}

static sndTrans( char *name1, char *name2 )
{
	FILE *fp, *fps;
	char path1[100], path2[100], head[32] ;
	int size, n ;

	if( file_kakuchousi_set( path1, name1, ".SND" ) ) ;
	if( ( fp = fopen( path1, "rb" ) ) == NULL )
		return NOERR;
	if( fread( head, 32, 1, fp ) < 1 )
	{
		fclose( fp ) ;
		return 1 ;
	}
	size = DWORD( head+12 ) ;

	if( file_kakuchousi_set( path2, name2, ".SND" ) ) ;
	if( ( fps = fopen( path2, "wb" ) ) == NULL )
	{
		fclose( fp ) ;
		return 2 ;
	}

	if( fwrite( head, 32, 1, fps ) < 1 )
	{
		fclose( fps ) ;
		fclose( fp ) ;
		return 2 ;
	}

	n = size ;

	while( 1 )
	{
		if( n > 153600 )
		{
			if( fread( orgBuf, 153600, 1, fp ) < 1 )
			{
				fclose( fps ) ;
				fclose( fp ) ;
				return 1 ;
			}
			if( fwrite( orgBuf, 153600, 1, fps ) < 1 )
			{
				fclose( fps ) ;
				fclose( fp ) ;
				return 2 ;
			}
			n -= 153600 ;
		}
		else
		{
			if( fread( orgBuf, n, 1, fp ) < 1 )
			{
				fclose( fps ) ;
				fclose( fp ) ;
				return 1 ;
			}
			if( fwrite( orgBuf, n, 1, fps ) < 1 )
			{
				fclose( fps ) ;
				fclose( fp ) ;
				return 2 ;
			}
			n = 0 ;
			break ;
		}
	}

	fclose( fps ) ;
	fclose( fp ) ;
	return NOERR ;
}


/* マウス左右ボタンが押されていれば終了するためのチェック */

static check()
{
	int sw;

/*	注意:sw 押されているとbit off 押されてないとbit on 	*/

	SND_joy_in_1( 0xff, &sw );		/* end check */
	sw = (sw ^ 0xff) & 0x30;
	if( sw != 0x30 )return 0;
	else return 1;
}

static underSet()
{
	int error ;
	int i, y, u, v ;

	error = NOERR ;

	xd = 320 ; yd = 240 ;	/* とりあえず作業エリアはフル画面に設定 */

	MG_mosDisp( 0 ) ;
	EGB_writePage(guiEgbPtr,1);
	EGB_color( guiEgbPtr, 1, 0 ) ;
	EGB_clearScreen(guiEgbPtr);			/* clear */
	EGB_displayStart(guiEgbPtr,3,0,0);
	EGB_displayStart(guiEgbPtr,2,1,1);
	EGB_displayStart(guiEgbPtr,2,2,2);
	EGB_displayStart(guiEgbPtr,0,0,0);
	EGB_displayStart(guiEgbPtr,1,0,0);
	EGB_displayStart(guiEgbPtr,3,320,240);
	EGB_displayPage(guiEgbPtr,0,3);
	EGB_writePage(guiEgbPtr,0);			/* pageを元に戻さないと暴走 */

	/* 下地の設定 */

	rgbToYcbcr( (underColor[0] << 3) + 4,
				(underColor[1] << 3) + 4,
				(underColor[2] << 3) + 4,
				&y, &u, &v, 255 ) ;
	for( i=0 ; i<76800 ; i++ )	/* とにかく色を塗る */
		BYTE( transBuf3 + i ) = y ;
	for( i=0 ; i<76800 ; i++ )
		BYTE( transBuf3 + 76800 + i ) = u ;
	for( i=0 ; i<76800 ; i++ )
		BYTE( transBuf3 + 153600 + i ) = v ;

	if( underTypeMode == 1 )
	{
		char para[16] ;

		error = loadtif( underTifPath ) ;

		if( error == NOERR )
		{
			EGB_writePage(guiEgbPtr,1);

			DWORD( para + 0 ) = (unsigned int)underBuf ;
			WORD( para + 4 ) = 0x14 ;
			WORD( para + 6 ) = 0 ;
			WORD( para + 8 ) = 0 ;
			WORD( para + 10 ) = 319 ;
			WORD( para + 12 ) = 239 ;
			EGB_getBlock(guiEgbPtr,para);

			EGB_writePage(guiEgbPtr,0);

			r_panel_make( 0, 0, 639, 19 ) ;
			r_waku_clear2( 320, 240, 2 ) ;
			MG_mosDisp( 0 ) ;

			rgbToYuvTrans( underBuf, transBuf3 ) ;
		}
		else
			return error ;
	}

	if( underTypeMode == 2 )
		return NOERR ;
	switch( effectMode )
	{
	case 6:
		transY( transBuf3, transBuf2 ) ;
		relief( setupSens, setupCutoff, transBuf2, transBuf3 ) ;
		soft( setupSoft, transBuf3, transBuf2+76800 ) ;
		transY( transBuf2+76800, transBuf3 ) ;
		break ;
	case 7:
		transY( transBuf3, transBuf2 ) ;
		lineBlack( setupSens, setupCutoff, transBuf2, transBuf3 ) ;
		soft( setupSoft, transBuf3, transBuf2+76800 ) ;
		transY( transBuf2+76800, transBuf3 ) ;
		break ;
	case 8:
		transY( transBuf3, transBuf2 ) ;
		lineWhite( setupSens, setupCutoff, transBuf2, transBuf3 ) ;
		soft( setupSoft, transBuf3, transBuf2+76800 ) ;
		transY( transBuf2+76800, transBuf3 ) ;
		break ;
	case 9:
		transY( transBuf3, transBuf2 ) ;
		lineBlack2( setupSens, setupCutoff, setupP, transBuf2, transBuf3 ) ;
		soft( setupSoft, transBuf3, transBuf2+76800 ) ;
		transY( transBuf2+76800, transBuf3 ) ;
		break ;
	case 10:
		transY( transBuf3, transBuf2 ) ;
		lineWhite2( setupSens, setupCutoff, setupP, transBuf2, transBuf3 ) ;
		soft( setupSoft, transBuf3, transBuf2+76800 ) ;
		transY( transBuf2+76800, transBuf3 ) ;
		break ;
	case 11:
		transY( transBuf3, transBuf2 ) ;
		normalAdd( setupP, transBuf2, transBuf3 ) ;
		soft( setupSoft, transBuf3, transBuf2+76800 ) ;
		transY( transBuf2+76800, transBuf3 ) ;
		break ;
	}

	return error ;
}

static mode0_4()
{
	int a[] = { 256,   0,   0 } ;
	int b[] = { 256,   0,   0 } ;
	int c[] = { 256, 256, 256 } ;

	b[0] = 256 * setupY / 100 ;
	c[0] = 256 * ( 100 - setupY ) / 100 ;
	a[1] = 256 * setupC / 100 ;
	a[2] = a[1] ;

	rgbToYuvTrans( orgBuf, transBuf1 ) ;

	if( underTypeMode == 2 )
		transYcbcr( transBuf1, transBuf3 ) ;

	transY( transBuf1, transBuf2 ) ;

	switch( effectMode )
	{
	case 0:
		relief( setupSens, setupCutoff, transBuf2, transBuf1 ) ;
		break ;
	case 1:
		lineBlack( setupSens, setupCutoff, transBuf2, transBuf1 ) ;
		break ;
	case 2:
		lineWhite( setupSens, setupCutoff, transBuf2, transBuf1 ) ;
		break ;
	case 3:
		lineBlack2( setupSens, setupCutoff, setupP, transBuf2, transBuf1 ) ;
		break ;
	case 4:
		lineWhite2( setupSens, setupCutoff, setupP,	transBuf2, transBuf1 ) ;
		break ;
	}

	soft( setupSoft, transBuf1, transBuf2+76800 ) ;

	transY( transBuf2+76800, transBuf1 ) ;

	vramTrans( a, b, c, transBuf1, transBuf2, transBuf3 ) ;

	return NOERR ;
}

static mode5()
{
	int a[] = { 256,   0,   0 } ;
	int b[] = {   0,   0,   0 } ;
	int c[] = { 256, 256, 256 } ;

	a[0] = 256 * setupY / 100 ;
	a[1] = 256 * setupC / 100 ;
	a[2] = a[1] ;

	rgbToYuvTrans( orgBuf, transBuf1 ) ;

	if( underTypeMode == 2 )
		transYcbcr( transBuf1, transBuf3 ) ;

	transY( transBuf1, transBuf2 ) ;

	normalAdd( setupP, transBuf2, transBuf1 ) ;

	soft( setupSoft, transBuf1, transBuf2+76800 ) ;

	transY( transBuf2+76800, transBuf1 ) ;

	vramTrans( a, b, c, transBuf1, transBuf2, transBuf3 ) ;

	return NOERR ;
}

static mode6_10()
{
	int a[] = { 256,   0,   0 } ;
	int b[] = { 256,   0,   0 } ;
	int c[] = { 256, 256, 256 } ;

	b[0] = 256 * setupY / 100 ;
	c[0] = 256 * ( 100 - setupY ) / 100 ;
	a[1] = 256 * setupC / 100 ;
	a[2] = a[1] ;

	rgbToYuvTrans( orgBuf, transBuf1 ) ;

	if( underTypeMode == 2 )
	{
		transYcbcr( transBuf1, transBuf3 ) ;
		switch( effectMode )
		{
		case 6:
			transY( transBuf3, transBuf2 ) ;
			relief( setupSens, setupCutoff, transBuf2, transBuf3 ) ;
			soft( setupSoft, transBuf3, transBuf2+76800 ) ;
			transY( transBuf2+76800, transBuf3 ) ;
			break ;
		case 7:
			transY( transBuf3, transBuf2 ) ;
			lineBlack( setupSens, setupCutoff, transBuf2, transBuf3 ) ;
			soft( setupSoft, transBuf3, transBuf2+76800 ) ;
			transY( transBuf2+76800, transBuf3 ) ;
			break ;
		case 8:
			transY( transBuf3, transBuf2 ) ;
			lineWhite( setupSens, setupCutoff, transBuf2, transBuf3 ) ;
			soft( setupSoft, transBuf3, transBuf2+76800 ) ;
			transY( transBuf2+76800, transBuf3 ) ;
			break ;
		case 9:
			transY( transBuf3, transBuf2 ) ;
			lineBlack2( setupSens, setupCutoff, setupP, transBuf2, transBuf3 );
			soft( setupSoft, transBuf3, transBuf2+76800 ) ;
			transY( transBuf2+76800, transBuf3 ) ;
			break ;
		case 10:
			transY( transBuf3, transBuf2 ) ;
			lineWhite2( setupSens, setupCutoff, setupP, transBuf2, transBuf3 );
			soft( setupSoft, transBuf3, transBuf2+76800 ) ;
			transY( transBuf2+76800, transBuf3 ) ;
			break ;
		}
	}

	vramTrans( a, b, c, transBuf3, transBuf2, transBuf1 ) ;

	return NOERR ;
}

static mode11()
{
	int a[] = { 256,   0,   0 } ;
	int b[] = {   0,   0,   0 } ;
	int c[] = { 256, 256, 256 } ;

	a[0] = 256 * setupY / 100 ;
	a[1] = 256 * setupC / 100 ;
	a[2] = a[1] ;

	rgbToYuvTrans( orgBuf, transBuf1 ) ;

	if( underTypeMode == 2 )
	{
		transYcbcr( transBuf1, transBuf3 ) ;
		transY( transBuf3, transBuf2 ) ;
		normalAdd( setupP, transBuf2, transBuf3 ) ;
		soft( setupSoft, transBuf3, transBuf2+76800 ) ;
		transY( transBuf2+76800, transBuf3 ) ;
	}

	vramTrans( a, b, c, transBuf3, transBuf2, transBuf1 ) ;

	return NOERR ;
}


static relief( int mul, int del, char *buf1, char *buf2 )
{
	int x, y, d ;

	for( y=0 ; y<yd ; y++ )
	{
		for( x=0 ; x<xd ; x++ )
		{
			switch( setupBibun )
			{

			case 0:
				switch( setupRay )
				{
				case 0:
					d = readY( x+1, y, buf1 ) - readY( x, y, buf1 )
					  + readY( x, y+1, buf1 ) - readY( x, y, buf1 ) ;
					break ;
				case 1:
					d = - readY( x+1, y, buf1 ) + readY( x, y, buf1 )
					  + readY( x, y+1, buf1 ) - readY( x, y, buf1 ) ;
					break ;
				case 2:
					d = readY( x+1, y, buf1 ) - readY( x, y, buf1 )
					  - readY( x, y+1, buf1 ) + readY( x, y, buf1 ) ;
					break ;
				case 3:
					d = - readY( x+1, y, buf1 ) + readY( x, y, buf1 )
					  - readY( x, y+1, buf1 ) + readY( x, y, buf1 ) ;
					break ;
				}
				if( d >= 0 )
				{
					d = d * mul / 10 - del ;
					if( d < 0 )d = 0 ;
				}
				else
				{
					d = d * mul / 10 + del ;
					if( d > 0 )d = 0 ;
				}
				break ;

			case 1:
				switch( setupRay )
				{
				case 0:
					d = wideBibunX( x, y, buf1 ) + wideBibunY( x, y, buf1 ) ;
					break ;
				case 1:
					d = - wideBibunX( x, y, buf1 ) + wideBibunY( x, y, buf1 ) ;
					break ;
				case 2:
					d = wideBibunX( x, y, buf1 ) - wideBibunY( x, y, buf1 ) ;
					break ;
				case 3:
					d = - wideBibunX( x, y, buf1 ) - wideBibunY( x, y, buf1 ) ;
					break ;
				}
				if( d >= 0 )
				{
					d = ( (d * mul / 10) >> 2 ) - del ;
					if( d < 0 )d = 0 ;
				}
				else
				{
					d = ( (d * mul / 10) >> 2 ) + del ;
					if( d > 0 )d = 0 ;
				}
				break ;

			}

			d = ( d >> 1 ) + 128 ;
			if( d < 0 )d = 0 ;
			if( d > 255 )d = 255 ;
			writeY( x, y, d, buf2 ) ;
		}
	}

	return NOERR ;
}

static lineWhite( int mul, int del, char *buf1, char *buf2 )
{
	int x, y, d, d2 ;

	for( y=0 ; y<yd ; y++ )
	{
		for( x=0 ; x<xd ; x++ )
		{
			switch( setupBibun )
			{

			case 0:
				d  = readY( x+1, y, buf1 ) - readY( x, y, buf1 ) ;
				d2 = readY( x, y+1, buf1 ) - readY( x, y, buf1 ) ;
				if( d < 0 )d = -d ;
				if( d2 < 0 )d2 = -d2 ;
				d += d2 ;
				d = d * mul / 10 - del ;
				if( d < 0 )d = 0 ;
				break ;

			case 1:
				d  = wideBibunX( x, y, buf1 ) ;
				d2 = wideBibunY( x, y, buf1 ) ;
				if( d < 0 )d = -d ;
				if( d2 < 0 )d2 = -d2 ;
				d += d2 ;
				d = ( (d * mul / 10) >> 2 ) - del ;
				if( d < 0 )d = 0 ;
				break ;

			}
			d = ( d >> 1 ) + 128 ;
			if( d < 0 )d = 0 ;
			if( d > 255 )d = 255 ;
			writeY( x, y, d, buf2 ) ;
		}
	}

	return NOERR ;
}

static lineBlack( int mul, int del, char *buf1, char *buf2 )
{
	int x, y, d, d2 ;

	for( y=0 ; y<yd ; y++ )
	{
		for( x=0 ; x<xd ; x++ )
		{
			switch( setupBibun )
			{

			case 0:
				d  = readY( x+1, y, buf1 ) - readY( x, y, buf1 ) ;
				d2 = readY( x, y+1, buf1 ) - readY( x, y, buf1 ) ;
				if( d < 0 )d = -d ;
				if( d2 < 0 )d2 = -d2 ;
				d += d2 ;
				d = d * mul / 10 - del ;
				if( d < 0 )d = 0 ;
				break ;

			case 1:
				d  = wideBibunX( x, y, buf1 ) ;
				d2 = wideBibunY( x, y, buf1 ) ;
				if( d < 0 )d = -d ;
				if( d2 < 0 )d2 = -d2 ;
				d += d2 ;
				d = ( (d * mul / 10) >> 2 ) - del ;
				if( d < 0 )d = 0 ;
				break ;

			}
			d = - ( d >> 1 ) + 128 ;
			if( d < 0 )d = 0 ;
			if( d > 255 )d = 255 ;
			writeY( x, y, d, buf2 ) ;
		}
	}

	return NOERR ;
}

static lineWhite2( int mul, int del, int dt, char *buf1, char *buf2 )
{
	int x, y, d, d2 ;

	for( y=0 ; y<yd ; y++ )
	{
		for( x=0 ; x<xd ; x++ )
		{
			switch( setupBibun )
			{

			case 0:
				d  = readY( x+1, y, buf1 ) - readY( x, y, buf1 ) ;
				d2 = readY( x, y+1, buf1 ) - readY( x, y, buf1 ) ;
				if( d < 0 )d = -d ;
				if( d2 < 0 )d2 = -d2 ;
				d += d2 ;
				d = d * mul / 10 - del ;
				if( d < 0 )d = 0 ;
				break ;

			case 1:
				d  = wideBibunX( x, y, buf1 ) ;
				d2 = wideBibunY( x, y, buf1 ) ;
				if( d < 0 )d = -d ;
				if( d2 < 0 )d2 = -d2 ;
				d += d2 ;
				d = ( (d * mul / 10) >> 2 ) - del ;
				if( d < 0 )d = 0 ;
				break ;

			}
			if( d <= 0 )d = 0 ;
			else d = dt ;
			d = ( d >> 1 ) + 128 ;
			if( d < 0 )d = 0 ;
			if( d > 255 )d = 255 ;
			writeY( x, y, d, buf2 ) ;
		}
	}

	return NOERR ;
}

static lineBlack2( int mul, int del, int dt, char *buf1, char *buf2 )
{
	int x, y, d, d2 ;

	for( y=0 ; y<yd ; y++ )
	{
		for( x=0 ; x<xd ; x++ )
		{
			switch( setupBibun )
			{

			case 0:
				d  = readY( x+1, y, buf1 ) - readY( x, y, buf1 ) ;
				d2 = readY( x, y+1, buf1 ) - readY( x, y, buf1 ) ;
				if( d < 0 )d = -d ;
				if( d2 < 0 )d2 = -d2 ;
				d += d2 ;
				d = d * mul / 10 - del ;
				if( d < 0 )d = 0 ;
				break ;

			case 1:
				d  = wideBibunX( x, y, buf1 ) ;
				d2 = wideBibunY( x, y, buf1 ) ;
				if( d < 0 )d = -d ;
				if( d2 < 0 )d2 = -d2 ;
				d += d2 ;
				d = ( (d * mul / 10) >> 2 ) - del ;
				if( d < 0 )d = 0 ;
				break ;

			}
			if( d <= 0 )d = 0 ;
			else d = dt ;
			d = - ( d >> 1 ) + 128 ;
			if( d < 0 )d = 0 ;
			if( d > 255 )d = 255 ;
			writeY( x, y, d, buf2 ) ;
		}
	}

	return NOERR ;
}

static wideBibunX( int x, int y, char *buf )
{
	return readY( x+1, y-1, buf )
	   + 2*readY( x+1, y,   buf )
		 + readY( x+1, y+1, buf )
		 - readY( x-1, y-1, buf )
	   - 2*readY( x-1, y,   buf )
		 - readY( x-1, y+1, buf ) ;
}

static wideBibunY( int x, int y, char *buf )
{
	return readY( x-1, y+1, buf )
	   + 2*readY( x,   y+1, buf )
		 + readY( x+1, y+1, buf )
		 - readY( x-1, y-1, buf )
	   - 2*readY( x,   y-1, buf )
		 - readY( x+1, y-1, buf ) ;
}

static normalAdd( int del, char *buf1, char *buf2 )
{
	int x, y, d ;

	for( y=0 ; y<yd ; y++ )
	{
		for( x=0 ; x<xd ; x++ )
		{
			d = readY( x, y, buf1 ) ;
			d = ( (d - del) >> 1 ) + 128 ;
			if( d < 0 )d = 0 ;
			if( d > 255 )d = 255 ;
			writeY( x, y, d, buf2 ) ;
		}
	}

	return NOERR ;
}


static rgbToYuvTrans( char *buf1, char *buf2 )
{
	int i, d, a[3] ;

	for( i=0 ; i<76800 ; i++ )
	{
		d = WORD( buf1 + (i << 1) ) ;
		rgbToYcbcr( 
			(( d >> 2 ) & 0xf8) + 4,
			(( d >> 7 ) & 0xf8) + 4,
			(( d << 3 ) & 0xf8) + 4,
			&(a[0]), &(a[1]), &(a[2]), 255 ) ;
		BYTE( buf2 + i ) = a[0] ;
		BYTE( buf2 + 76800 + i ) = a[1] ;
		BYTE( buf2 + 153600 + i ) = a[2] ;
	}
	return NOERR ;
}

static vramTrans( int a0[], int a1[], int a2[], 
				char *buf0, char *buf1, char *buf2 )
{
	int a[3], b[3], c[3] ;
	int x, y ;
	int y0, u, v ;

	for( y=0 ; y<yd ; y++ )
	{
		for( x=0 ; x<xd ; x++ )
		{
			read( x, y, a, buf0 ) ;
			read( x, y, b, buf1 ) ;
			read( x, y, c, buf2 ) ;

			a[0] = ((a[0] - 128) << 1) ;

			y0 = ((a0[0] * a[0]) >> 8)
			   + ((a1[0] * b[0]) >> 8)
			   + ((a2[0] * c[0]) >> 8) ;

			u  = ( ( a0[1] * (a[1]-128) ) >> 8 )
			   + ( ( a1[1] * (b[1]-128) ) >> 8 )
			   + ( ( a2[1] * (c[1]-128) ) >> 8 ) + 128 ;

			v  = ( ( a0[2] * (a[2]-128) ) >> 8 )
			   + ( ( a1[2] * (b[2]-128) ) >> 8 )
			   + ( ( a2[2] * (c[2]-128) ) >> 8 ) + 128 ;

			ycbcrToRgb( y0, u, v, &(c[1]), &(c[2]), &(c[0]), 255 ) ;
			writeVram( x, y, c ) ;
		}
	}

	return NOERR ;
}

static transYcbcr( char *buf1, char *buf2 )
{
	int i ;

	for( i=0 ; i<230400 ; i += 4 )
	{
		DWORD( buf2 + i ) = DWORD( buf1 + i ) ;
	}

	return NOERR ;
}

static transY( char *buf1, char *buf2 )
{
	int i ;

	for( i=0 ; i<76800 ; i += 4 )
	{
		DWORD( buf2 + i ) = DWORD( buf1 + i ) ;
	}

	return NOERR ;
}

static soft( int rate, char *buf1, char *buf2 )
{
	int x, y, r, s, d ;

	r = rate ;
	s = 100 - r ;

	for( y=0 ; y<yd ; y++ )
	{
		for( x=0 ; x<xd ; x++ )
		{
			d = readY( x-1, y-1, buf1 )
			  + readY( x,   y-1, buf1 )
			  + readY( x+1, y-1, buf1 )
			  + readY( x-1, y  , buf1 )
			  + readY( x+1, y  , buf1 )
			  + readY( x-1, y+1, buf1 )
			  + readY( x,   y+1, buf1 )
			  + readY( x+1, y+1, buf1 ) ;

			d = ( r*( d >> 3 ) + s*readY( x, y, buf1 ) ) / 100 ;
			writeY( x, y, d, buf2 ) ;
		}
	}
	return NOERR ;
}

static yuvToRgbTrans( char *buf1, char *buf2 )
{
	int i, r, g, b ;

	for( i=0 ; i<76800 ; i++ )
	{
		ycbcrToRgb( BYTE(buf1+i), BYTE(buf1+76800+i), BYTE(buf1+153600+i),
					&r, &g, &b, 255 ) ;
		WORD( buf2 + (i << 2) )
		= ((b >> 3) & 0x1f) + ((r << 2) & 0x3e0) + ((g << 7) & 0x7c00) ;
	}
	return NOERR ;
}



/* buf領域からの読み取り関数( yuv ) */
static read( int x, int y, int a[], char *buf )
{
	if( x < 0 )x = 0 ;
	if( y < 0 )y = 0 ;
	if( x > (xd-1) )x = (xd-1) ;
	if( y > (yd-1) )y = (yd-1) ;

	a[0] = BYTE( buf + y*320 + x ) ;
	a[1] = BYTE( buf + 76800 + y*320 + x ) ;
	a[2] = BYTE( buf + 153600 + y*320 + x ) ;

	return 0 ;
}

/* transBuf1領域からのYdataのみの読み込み関数( y ) */
static readY( int x, int y, char *buf )
{
	if( x < 0 )x = 0 ;
	if( y < 0 )y = 0 ;
	if( x > (xd-1) )x = (xd-1) ;
	if( y > (yd-1) )y = (yd-1) ;

	return BYTE( buf + y*320 + x ) ;
}

/* transBuf1領域へのYdataのみの書き込み関数( y ) */
static writeY( int x, int y, int a, char *buf )
{
	if( x < 0 )x = 0 ;
	if( y < 0 )y = 0 ;
	if( x > (xd-1) )x = (xd-1) ;
	if( y > (yd-1) )y = (yd-1) ;

	BYTE( buf + y*320 + x ) = a ;
	return 0 ;
}

/* vram領域に書き込み関数 */
static writeVram( int x, int y, int a[] )
{
	int d ;

	if( x < 0 )x = 0 ;
	if( y < 0 )y = 0 ;
	if( x > (xd-1) )x = (xd-1) ;
	if( y > (yd-1) )y = (yd-1) ;

	/* offset = 0x40000 --> page1 */

	d=((a[0] >> 3) & 0x1f ) + ((a[1] << 2) & 0x3e0 ) + ((a[2] << 7) & 0x7c00);
	pokew( y*1024 + x*2 + 0x40000, 0x104, d );

	return 0 ;
}


/*
rgbToYcbcr( int r0, int g0, int b0, int *y0, int *u0, int *v0, int max )
{
	double r, g, b ;
	double y, cb, cr ;

	r = r0 ;
	g = g0 ;
	b = b0 ;
	r = r/max ;
	g = g/max ;
	b = b/max ;

	y  =  .299*r + .587*g + .114*b ;
	cb = -.1686*r - .3311*g + .4997*b ;
	cr =  .4998*r -.4185*g -.0813*b ;

	*y0 = 219*y + 16 ;
	*u0 = 224*cb + 128 ;
	*v0 = 224*cr + 128 ;

	if( *y0 < 0 )*y0 = 0 ;
	if( *y0 > 255 )*y0 = 255 ;
	if( *u0 < 0 )*u0 = 0 ;
	if( *u0 > 255 )*u0 = 255 ;
	if( *v0 < 0 )*v0 = 0 ;
	if( *v0 > 255 )*v0 = 255 ;

	return NOERR ;
}
*/

rgbToYcbcr( int r0, int g0, int b0, int *y0, int *u0, int *v0, int max )
{
	int y, cb, cr ;

	y  =  19595*r0 + 38470*g0 + 7471*b0 ;
	cb = -11049*r0 - 21699*g0 + 32748*b0 ;
	cr =  32755*r0 -27427*g0 -5328*b0 ;

	*y0 = ( ((y/max)*219) >> 16 ) + 16 ;
	*u0 = ( ((cb/max)*224) >> 16 ) + 128 ;
	*v0 = ( ((cr/max)*224) >> 16 ) + 128 ;

	if( *y0 < 0 )*y0 = 0 ;
	if( *y0 > 255 )*y0 = 255 ;
	if( *u0 < 0 )*u0 = 0 ;
	if( *u0 > 255 )*u0 = 255 ;
	if( *v0 < 0 )*v0 = 0 ;
	if( *v0 > 255 )*v0 = 255 ;

	return NOERR ;
}

/*
static ycbcrToRgb( int y0, int u0, int v0, int *r0, int *g0, int *b0, int max )
{
	double r, g, b ;
	double y, cb, cr ;

	y = y0 - 16 ;
	y = y/219 ;
	cb = u0 - 128 ;
	cb = cb/224 ;
	cr = v0 - 128 ;
	cr = cr/224 ;

	r = y + .00005973490918*cb + 1.402581175*cr ;
	g = y - .3443692095*cb - .7144070603*cr ;
	b = y + 1.773042678*cb - .0001300611143*cr ;

	*r0 = r * max ;
	*g0 = g * max ;
	*b0 = b * max ;

	if( *r0 < 0 )*r0 = 0 ;
	if( *r0 > max )*r0 = max ;
	if( *g0 < 0 )*g0 = 0 ;
	if( *g0 > max )*g0 = max ;
	if( *b0 < 0 )*b0 = 0 ;
	if( *b0 > max )*b0 = max ;

	return NOERR ;
}
*/

static ycbcrToRgb( int y0, int u0, int v0, int *r0, int *g0, int *b0, int max )
{
	y0 = (( y0 - 16 ) * max )/219 ;
	u0 = (( u0 -128 ) * max )/224 ;
	v0 = (( v0 -128 ) * max )/224 ;

	*r0 = ( 65536*y0 + 4*u0 + 91920*v0 ) >> 16 ;
	*g0 = ( 65536*y0 - 22569*u0 - 46819*v0 ) >> 16 ;
	*b0 = ( 65536*y0 + 116198*u0 - 9*v0 ) >> 16 ;

	if( *r0 < 0 )*r0 = 0 ;
	if( *r0 > max )*r0 = max ;
	if( *g0 < 0 )*g0 = 0 ;
	if( *g0 > max )*g0 = max ;
	if( *b0 < 0 )*b0 = 0 ;
	if( *b0 > max )*b0 = max ;

	return NOERR ;
}

