/*****************************************************************

		Ｇ−Ｐｅｎ３２ｋ専用部
							Copyright(C) 1991-1994 OKOME
*****************************************************************/

#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<EGB.h>
#include	<MOS.h>
#include	<Normlib.h>
#include	<okome.h>
#include	<GPen32k.h>
#include	<GPenSub.c>

void colmix(int mb, int s)	/*	色混ぜ屋	*/
{
	int i, b, r, g, b1,r1,g1, b2,r2,g2 ;
	char wm[CBX4*2];
	s+=4;
	if (s==4 || s==5)
		mcl[s] = mcl[mb];

	b1 =   mcl[4]         & 0x1f ;
	r1 = ( mcl[4] >>  5 ) & 0x1f ;
	g1 = ( mcl[4] >> 10 ) & 0x1f ;
	b2 =   mcl[5]         & 0x1f ;
	r2 = ( mcl[5] >>  5 ) & 0x1f ;
	g2 = ( mcl[5] >> 10 ) & 0x1f ;

	for (i = 0; i<CBX4; i++)
	{
		b = b1 + i * ( b2-b1 + sign( b2-b1 ) ) /CBX4 ;
		r = r1 + i * ( r2-r1 + sign( r2-r1 ) ) /CBX4 ;
		g = g1 + i * ( g2-g1 + sign( g2-g1 ) ) /CBX4 ;
		WORD(wm+i*2)=(g << 10) + (r << 5) + b;
	}
	egbputZ(CBX1,CBY1,CBX2,CBY2,CBX4,1,wm);
	boxf( CBLX1,CBLY1,CBLX2,CBLY2, mcl[4] );
	boxf( CBRX1,CBRY1,CBRX2,CBRY2, mcl[5] );
}

void wsize( int mb, int s )
{
	static int k=3;
	const int h = 2;
	int mx, my;
	wget();
	wpg(0);
	boxf( (NX1-h)*bi-1, (NY1-h)*bi-1, (NX2+h+1)*bi, (NY2+h+1)*bi, BCL );
	wkugiri(0);
	nx[k] = NX1;
	ny[k] = NY1;
	if (mb & 1)
		k = (k+1) % (5+s);
	else {
		if (--k<0)	k = 4+s;
	}
	wb = wallbl[k];
	NX4 = (8/bi) << k;
	NY4 = NX4;
	NX1 = nx[k];
	NY1 = ny[k];
	wpg(1);
	wput();
	wpg(0);
	if (NX4<WX4)	wkugiri(1);
	biboxbf( NX1, NY1, NX2, NY2, 0, 4, 4 );
	wpg(1);
	mbout(&mb, &mx, &my);
}

static int HSV_h, HSV_s, HSV_v;
static hrs=0;

/*	ＨＳＶパレット	*/
void HSVcolms(int j)
{
	int c;
	switch (j)
	{
	case 0:	c = HSV_h/6;	break;
	case 1:	c = HSV_s;		break;
	case 2:	c = HSV_v;		break;
	} 
	view(PALX*bi,PALY*bi,PALX*bi+255,479);
	boxf( PALX*bi      , PALY*bi + j   *PALB*bi + 1,
		  PALX*bi + 255, PALY*bi +(j+1)*PALB*bi - 1, 0 );
	line( PALX*bi + c+1, PALY*bi + j   *PALB*bi + 2,
		  PALX*bi + c+1, PALY*bi +(j+1)*PALB*bi - 1, 8);
	line( PALX*bi + c, PALY*bi + j   *PALB*bi + 1,
		  PALX*bi + c, PALY*bi +(j+1)*PALB*bi - 2, 15);
	view(0,0,639,479);
}

void HSVcolp(int h, int s, int v)		/*	ＨＳＶ表示	*/
{
	int i;
	char wm[1024];
	mcl[0] = (mcl[0] & 0x8000) | HSVto32k(h,s,v);
	mbclp();
	for (i=0; i<128; i++) {
		WORD(wm    +i*2)=HSVto32k(i*12,s,v);
		WORD(wm+256+i*2)=HSVto32k(h,i*2,v);
		WORD(wm+512+i*2)=HSVto32k(h,s,i*2);
	}
	egbputZ(PALX,PALY, PALX+127,PALY+PALB*3-1, 128,3, wm);
	wpg(0);
	for (i=0; i<3; i++)
		HSVcolms(i);
	wpg(1);
}

void HSVmc(int mb, int mx, int my)
{
	int y, c;
	y = ( my - PALY*bi ) / ( PALB*bi );
	umosv(PALX, PALY+PALB*y, PALX+127, PALY+PALB*(y+1)-1 );
	while (mb!=0)
	{
		MOS_rdpos( &mb, &mx, &my );
		c = mx - PALX*bi;
		switch(y)
		{
		case 0:	HSV_h = c*6;	break;
		case 1: HSV_s = c;	break;
		case 2: HSV_v = c;	break;
		}
		HSVcolp(HSV_h,HSV_s,HSV_v);
	}
	mosv(0, 0, 639, 479);
}

/*	カラー選択パレット	*/
void mcolms( int j )		/*	カラーバー  □表示	*/
{
	int c, k;
	if (hrs!=0)
		HSVcolms(j);
	else {
		k = ( j + 1 ) % 3 ;
		c = ( mcl[0] / (1 << (k*5))) & 0x1f;
		boxf( PALX*bi + 1  , PALY*bi + j   *PALB*bi + 1,
			  PALX*bi + 255, PALY*bi +(j+1)*PALB*bi - 1, 0 );
		boxb( PALX*bi + c * 8 + 2, PALY*bi + j   *PALB*bi + 2,
			  PALX*bi + c * 8 + 7, PALY*bi +(j+1)*PALB*bi - 1, 8 );
		boxb( PALX*bi + c * 8 + 1, PALY*bi + j   *PALB*bi + 1,
			  PALX*bi + c * 8 + 6, PALY*bi +(j+1)*PALB*bi - 2, 15 );
	}
}

void mcols( int c )			/*	カラーバー表示	*/
{
	int i, j;
	char wm[128];
	if (hrs!=0 && c != mcl[0]) {
		c32toHSV(c, &HSV_h,&HSV_s,&HSV_v);
		HSVcolp(HSV_h,HSV_s,HSV_v);
	} else {
		if ( c != mcl[0] ) {
			mcl[0] = c;
			mbclp();
			for ( c = 0; c <= 2; c++ ) {
				j = (( c + 1 ) % 3) * 5 ;
				for ( i = 0; i<32; i++)
					WORD(wm+i*2) = (i<<j) + (mcl[0] & (0x7fff - (0x1f<<j)));
				egbputZ(PALX    , PALY+(c+1)*PALB-2,
						PALX+127, PALY+(c+1)*PALB-1, 32, 1, wm);
			}
			wpg(0);
			for ( j=0; j<=2; j++)
				mcolms(j);
			wpg(1);
		}
	}
}

void mcolm( int mb, int mx, int my )		/*	RGB カラー選択バー	*/
{
	int i, y, c;
	if (hrs!=0)
		HSVmc(mb, mx, my);
	else {
		y = ( my - PALY*bi ) / ( PALB*bi );
		i = ( 1 << (((y + 1) % 3) * 5) );
		umosv(PALX, PALY+PALB*y, PALX+127, PALY+PALB*(y+1)-1 );
		while (mb!=0) {
			MOS_rdpos( &mb, &mx, &my );
			c = (mx - PALX*bi) / 8;
			mcols((mcl[0] & (0xffff - 0x1f * i)) + c * i);
		}
		mosv(0, 0, 639, 479);
	}
}

void hsrg(int i)
{
	int x, y;
	char wm[64];
	if (i!=-1) {
		if (i==0 || i==1)
			hrs=i;
		else
			hrs = 1-hrs;
	}
	wpg(0);
	biubox( PALX-12, PALY, PALX-4, PALY+PALB*3-1, 8, 15, 0 );
	if (hrs==0) {
		font12((PALX-11)*bi,PALY*bi+12,"Ｒ",15);
		font12((PALX-11)*bi,PALY*bi+26,"Ｇ",15);
		font12((PALX-11)*bi,PALY*bi+40,"Ｂ",15);
		wpg(1);
		for ( y = 0; y <= 2; y++ ) {
			i = ( y + 1 ) % 3 ;
			for ( x = 0; x < 32; x++ )
				WORD(wm+x*2) = x * (1 << (i * 5));
			egbputZ(PALX, PALY+y*PALB, PALX+127, PALY+(y+1)*PALB-1, 32, 1, wm);
		}
	} else {
		font12((PALX-11)*bi,PALY*bi+12,"Ｈ",15);
		font12((PALX-11)*bi,PALY*bi+26,"Ｓ",15);
		font12((PALX-11)*bi,PALY*bi+40,"Ｖ",15);
		wpg(1);
	}
	i = mcl[0];
	mcl[0] ^= 1;
	mcols(i);
	mbout(&i,&x,&y);
}

void svpp(int mx)
{
	char a[32];
	int mb, my;
	mosv(VX1,VY1,VX2,VY2);
	do {
		my = mx - VX1;
		if (my!=vr) {
			vr = my;
			wpg(0);
			boxf(VX1,VY1,VX2,VY2,8);
			line(VX1+vr,VY1,VX1+vr,VY2,15);
			uboxf(VX2+16,VY1-4,VX2+48,VY2+4,8,15,8);
			_itoa(vr,a,10);
			font8(VX2+42-8*strlen(a),VY2+3,a,15);
			wpg(1);
		}
		MOS_rdpos(&mb,&mx,&my);
	}	while (mb);
	mosv(0,0,639,479);
}

void ptnsave4pg( char *name, int sx, int sy )
{
	int x, y, p;
	FILE *fp;
	if ((fp = fopen( name, "wb")) == NULL )
		return;
	for (p=0; p<=3; p++ ) {
		for ( y=0; y<BY4/sy; y++ ) {
			for ( x=0; x<BX4/sx; x++ ) {
				egbget(BX1+x*sx,BY1+y*sy,BX1+sx-1+x*sx,BY1+sy-1+y*sy,b);
				fwrite(b, 1, sx*sy*2, fp);
			}
		}
		page(1);
	}
	page(-4);
	fclose(fp);
}

void ptnload4pg( char *name, int sx, int sy )
{
	int x, y, p;
	FILE *fp;
	if ((fp = fopen( name, "rb")) == NULL )
		return;
	for ( p=0; p<=3; p++ ) {
		for ( y=0; y<BY4/sy; y++ ) {
			for ( x=0; x<BX4/sx; x++ ) {
				if (fread(b, 1, sx*sy*2, fp))
					vput2(BX1+x*sx,BY1+y*sy,BX1+sx-1+x*sx,BY1+sy-1+y*sy,b);
			}
		}
		page(1);
	}
	page(-4);
	fclose(fp);
}

void rchc( void )
{
	int mb, mx, my;
	mcl[0]^= 0x8000;
	mbclp();
	mbout( &mb, &mx, &my );
}

int ptr16dsa(char *name)
{
	char cl[64];
	int i, j, x, y, cf, ca;
	FILE *fp;
	if ((fp = fopen( name, "wb")) == NULL )
		return (1);
	for (y=0; y<BY4/16; y++) {
		for (x=0; x<BX4/16; x++) {
			cf = 0;
			WORD(cl) = 0;
			egbget(BX1+x*16, BY1+y*16, BX1+15+x*16, BY1+15+y*16, b);
			EGB_writePage(work,0);
			view(0,0,1023,511);
			for (i=0; i<256; i++) {
				ca = 16;
				if (WORD(b+i*2) & 0x8000)
					ca = 0;
				else {
					for (j=1; j<=cf; j++) {
						if (WORD(cl+j*2)==WORD(b+i*2)) {
							ca = j;
							break;
						}
					}
					if (ca==16) {
						if (cf==15) {
							message2(1,"色数が１６色を超えています",1);
							break;
						} else {
							++cf;
							WORD(cl+cf*2) = WORD(b+i*2);
							ca = cf;
						}
					}
				}
				line(i,511,i,511,ca);
			}
			egbget(0,511,255,511,b);
			EGB_writePage(work,1);
			fwrite(cl,1,32,fp);
			fwrite(b,1,128,fp);
		}
	}
	fclose(fp);
	return (0);
}

int ptr16dlo(char *name)
{
	char cl[64];
	unsigned char *p;
	int i, x, y;
	FILE *fp;
	p = (unsigned char *)b+512;
	if ((fp = fopen( name, "rb")) == NULL )
		return (1);
	for (y=0; y<BY4/16; y++) {
		for (x=0; x<BX4/16; x++) {
			fread(cl,1,32,fp);
			fread(p,1,128,fp);
			WORD(cl) = 0x8000;
			for (i=0; i<256; i++)
				WORD(b+i*2) = WORD(cl+(((p[i/2] >> ((i&1)*4)) & 0xf)*2));
			egbput(BX1+x*16, BY1+y*16, BX1+15+x*16, BY1+15+y*16, b);
		}
	}
	fclose(fp);
	return(0);
}

int cdasave(char *nn)
{
	char l[256], s[256];
	char *p;
	unsigned short int b[256];
	int i, j, x, y;
	FILE *fp;
	if (( fp = fopen( nn, "wt" ) )==NULL)
		return (1);
	fputs("short int chr[] = {",fp);
	for (i = 0; i < 64; i++) {
		x = i % 8;
		y = (i / 8) % 8;
		egbget(BX1+x*16,BY1+y*16, BX1+x*16+15,BY1+y*16+15, (char *)b);
		fputs("\n\t/* No.",fp);
		fputs(_itoa(i,l,16),fp);
		fputs( " */", fp );
		for (j = 0; j<256; j++)	{
			if (j % 8 == 0)
				fputs( "\n\t0x", fp );
			else
				fputs( ", 0x", fp );
			strcpy( s, "000" );
			strcat( s, _itoa( b[j], l, 16 ) );
			p = s + strlen(s) - 4;
			fputs( p, fp );
			if (j % 8 == 7 && (i<63 || j!=255))
				fputs( ",", fp );
		}
	}
	fputs( "\n\t}\n", fp );
	fclose(fp);
	return (0);
}

/*
int cdaload()

int basdasa()

int basdalo()
*/
