/*
	ARTemis (Graphic Editor for FM-TOWNS)
	(c) MATSUUCHI Ryosuke 1992,1993

	cmdview.c
*/


#include <stdio.h>
#include <msdos.cf>
#include <memory.h>

#include "ge.h"
#include "dispman.h"
#include "imageman.h"

#if 0



#define	MAXLINBYTES		8192	/* １ライン分のデータの最大バイト数 */
#define	VRAMSEL	0x104


#define	_GetNewDW	3
#define	_Shift4bit	2
#define	_Store4bit	1
#define	_Shift8bit	4
#define	_Shift12bit	5
#define	_Shift16bit	6
#define	_Store8bit	7
#define	_Store12bit	8
#define	_Shift20bit	9
#define	_Shift24bit	10


static void makeViewCmd(char *cmdbuf, int dstlen, int srclen)
// 1ライン分の圧縮のコマンドリストを作成する
{
	char  *cp;  int  predw_x, x;
	if (dstlen <= 1)
	{
		cmdbuf[0] = _GetNewDW;
		cmdbuf[1] = _Store4bit;
		cmdbuf[2] = 0;
		return;
	}
	cp = cmdbuf;
	predw_x = -8;
	for (x=0; x<dstlen; x++)
	{
		int sx;
		sx = ((srclen-1) * x + (dstlen-1)/2) / (dstlen-1);
		while ((predw_x/8) < (sx/8))
		{
			*cp++ = _GetNewDW;
			predw_x = (predw_x+8) - (predw_x+8)%8;
		}
		while (predw_x < sx)
		{
			if ((predw_x % 8)==7)
			{
				*cp++ = _GetNewDW;
			}
			else
			{
				if (cmdbuf<cp)
				{
					switch (*(cp-1)) {
					case _Shift4bit:
						*(cp-1) = _Shift8bit;
						break;
					case _Shift8bit:
						*(cp-1) = _Shift12bit;
						break;
					case _Shift12bit:
						*(cp-1) = _Shift16bit;
						break;
					case _Shift16bit:
						*(cp-1) = _Shift20bit;
						break;
					case _Shift20bit:
						*(cp-1) = _Shift24bit;
						break;
					default:
						*cp++ = _Shift4bit;
					}
				}
				else
					*cp++ = _Shift4bit;
			}
			predw_x++;
		}
		if ((cmdbuf < cp)  &&  (x % 8 < 7)  &&  *(cp-1) == _Store4bit)
			*(cp-1) = _Store8bit;
		else if ((cmdbuf < cp)  &&  (x % 8 < 7)  &&  *(cp-1) == _Store8bit)
			*(cp-1) = _Store12bit;
		else
			*cp++ = _Store4bit;
		if ((predw_x % 8) == 7 && x<dstlen-1)
			*cp++ = _GetNewDW;
		predw_x++;
	}
	*cp = 0;
	return;
}


void commandView()
{
	eraseLattice_all();
	page_edit();
	gdsploc(0,0);
	if (scrmode != 10 && (editXlen > 640 || editYlen > 480))
	{
		gscrzoom(1,1);
		int xrate,yrate;
		xrate = (editXlen<<10)/640;
		yrate = (editYlen<<10)/480;
		if (xrate < yrate)
		{
			char cmd[8192];
			makeViewCmd(cmd, 640, editXlen);
#if 0
			for (int y=0; y<480; y++)
			{
				char linebuf[MAXLINBYTES],dspbuf[640/2];
				memcpy( linebuf, ebuf_adrs(0,((editYlen-1)*y) / (480-1)),
						_min(MAXLINBYTES,editXlen/2));
				// grboxfill(0,y,640,1,15,DrawNORMAL);
				viewscr_sub(VRAMSEL,0x40000+(1024/2)*y,linebuf,cmd);
			}
#else
			for (int yy=0; yy<480; yy+=20)
			{
				char outbuf[8192];
				memset(outbuf,0,8192);
				for (int y=0; y<20; y++)
				{
					char linebuf[MAXLINBYTES];
					memcpy( linebuf,
					        ebuf_adrs(0, ((editYlen-1) * (yy+y)) / (480-1)),
							_min(MAXLINBYTES, editXlen/2));
					viewscr_down(outbuf+320*y, linebuf, cmd);
				}
				ARTgputblk(0,yy,640,20,outbuf);
			}
#endif
			page_menu();
			int msx,msy;
			msx = ms.x;
			msy = ms.y;
			for (;;)
			{
				ms_get(&ms);
				ms.x = msx;
				ms.y = msy;
				mous_setpos(&ms,ms.x,ms.y);
				if (ms.btn1==OFFON || ms.btn2==OFFON || key_read()!=0)
					break;
			}
			page_edit();
			int vadrs = 0x40000;	// ページ１のVRAMアドレス
			int x_cnt = (scrmode == 10 ? v_editXlen*2 : v_editXlen/2);
			int Bystep = (scrmode == 10 ? editXlen*2 : editXlen/2);
			xymemcpy(VRAMSEL,vadrs,getds(),(int)ebuf_adrs(v_editX,v_editY),
					 x_cnt, v_editYlen, x_cnt, Bystep);
		}
	}
	else if (scrmode == 10 && (320 < editXlen && editXlen <= 640) &&
		     (240 < editYlen && editYlen <= 480))
	{
		ebuf_store();
		gscrzoom(2,2);
		grboxfill(0,0,320,240,0,DrawNORMAL);
		//
		{
			int linsiz = ebuf_getlinesize();
			int x,y; char *p;
			for (y=0; y<editYlen/2; y++)
			{
				char glinbuf[512];
				p = ebuf_adrs(0,y*2);
				for (x=0; x<editXlen/2; x++,p+=4)
				{
					int r,g,b,c1,c2,c3,c4;
					c1 = *(short*)p;          c2 = *(short*)(p+2);
					c3 = *(short*)(p+linsiz); c4 = *(short*)(p+linsiz+2);
					r = (getR(c1) + getR(c2) + getR(c3) + getR(c4) + 2) / 4;
					g = (getG(c1) + getG(c2) + getG(c3) + getG(c4) + 2) / 4;
					b = (getB(c1) + getB(c2) + getB(c3) + getB(c4) + 2) / 4;
					*(short*)(glinbuf+x*2) = g*1024+r*32+b;
				}
				grp_putblk((320-editXlen/2)/2,y,256,1,glinbuf,DrawNORMAL);
			}
		}
		//
		page_menu();
		int msx,msy;
		msx = ms.x;
		msy = ms.y;
		for (;;)
		{
			ms_get(&ms);
			ms.x = msx;
			ms.y = msy;
			mous_setpos(&ms,ms.x,ms.y);
			if (ms.btn1==OFFON || ms.btn2==OFFON || key_read()!=0)
				break;
		}
		page_edit();
		ebuf_disp();
	}
	else if (scrmode == 10 && (editXlen > 320 || editYlen > 240))
	{
		ebuf_store();
		gscrzoom(2,2);
		int xr,yr,xlen,ylen;
		xr = (editXlen*65536)/320;
		yr = (editYlen*65536)/240;
		if (xr > yr)
			xlen = 320,  ylen = (320*editYlen) / editXlen;
		else
			ylen = 240,  xlen = (240*editXlen) / editYlen;
		grboxfill(0,0,320,240,0,DrawNORMAL);
		zoomcopy(0,0,editXlen-1,editYlen-1,0,0,xlen-1,ylen-1,NO);
		page_menu();
		int msx,msy;
		msx = ms.x;
		msy = ms.y;
		for (;;)
		{
			ms_get(&ms);
			ms.x = msx;
			ms.y = msy;
			mous_setpos(&ms,ms.x,ms.y);
			if (ms.btn1==OFFON || ms.btn2==OFFON || key_read()!=0)
				break;
		}
		page_edit();
		ebuf_disp();
	}
	else
	{
		if (scrmode == 10)
			gscrzoom(2,2);
		else
			gscrzoom(1,1);

		page_menu();
		int msx,msy;
		msx = ms.x;
		msy = ms.y;
		for (;;)
		{
			ms_get(&ms);
			ms.x = msx;
			ms.y = msy;
			mous_setpos(&ms,ms.x,ms.y);
			if (ms.btn1==OFFON || ms.btn2==OFFON || key_read()!=0)
				break;
		}
		page_edit();
	}
	gscrzoom(zoomX,zoomY);
	gdsploc(dispX,dispY);
	page_menu();
	drawLattice_all();
}


/* end of cmdview.c */
#else

void commandView()
{
	int zr = DMimage_getzoomrate();
	bool lat1disp,lat2disp;
	int dispx,dispy;
	DMimage_getlatticeswitch(&lat1disp,&lat2disp);
	DMimage_getdispxy(&dispx,&dispy);
	DMimage_setlatticeswitch(NO,NO);
	DMimage_setdispxy(0,0);
	DMimage_setzoomrate(minzoomrate);
	int msx,msy;
	msx = ms.x, msy = ms.y;
	mous_setpos(&ms,0,0);
	for (;;)
	{
		for (;;)
		{
			ms_get(&ms);
			if (ms.dx!=0||ms.dy!=0||ms.btn1!=OFF||ms.btn2!=OFF||key_chk()!=0)
				break;
		}
		DMimage_setdispxy(ms.x,ms.y);
		int tx,ty;
		DMimage_getdispxy(&tx,&ty);
		mous_setpos(&ms,tx,ty);
		if (ms.btn1==OFFON || ms.btn2==OFFON || key_read()!=0)
			break;
	}
	mous_setpos(&ms,msx,msy);
	DMimage_setzoomrate(zr);
	DMimage_setdispxy(dispx,dispy);
	DMimage_setlatticeswitch(lat1disp,lat2disp);
}

#endif
