/*
	ARTemis (Graphic Editor for FM-TOWNS)
	(c) MATSUUCHI Ryosuke 1992,1993

	effect.c
	
	画像にさまざまな特殊効果を加える処理
*/

#include <stdio.h>

#include "ge.h"
#include "dispman.h"
#include "imageman.h"

static void do_accent(x1,y1,x2,y2)
int	x1,y1,x2,y2;
{
	int a,b;	// 変数a,b : アクセント処理のパラメータ(5ビット固定小数点)
	a = 96;
	b = (a-32)/2;
	if (x1 > x2)	swap(x1,x2);
	if (y1 > y2)	swap(y1,y2);
	int x,y;
	for (y=y1; y<=y2; y++) {
		for (x=x1; x<=x2; x++) {
			int c,rr,gg,bb,rrr,ggg,bbb;
			c = EIMpoint(x,y);
			bb = c & 31;  c >>= 5;
			rr = c & 31;  c >>= 5;
			gg = c & 31;
			rrr = ( a*rr-b*gg-b*bb) >> 5;
			ggg = (-b*rr+a*gg-b*bb) >> 5;
			bbb = (-b*rr-b*gg+a*bb) >> 5;
			if (rrr < 0)		rrr = 0;
			else if (rrr > 31)	rrr = 31;
			if (ggg < 0)		ggg = 0;
			else if (ggg > 31)	ggg = 31;
			if (bbb < 0)		bbb = 0;
			else if (bbb > 31)	bbb = 31;
			EIMpset(x,y,(ggg<<10)+(rrr<<5)+bbb,DrawNORMAL);
		}
	}
}

void commandAccent()
{
	int step;
	int x1,y1,x2,y2;
	void drawcsr(int msx,int msy)
	{
		page_menu();
		int tx,ty;
		tx = DMimage_getx(msx);
		ty = DMimage_gety(msy);
		if (step==1) {
			EIMboxline(x1,y1,tx,ty,white,DrawXOR);
		}
	}
	step=0;
	for(;;) {
		int prex,prey;
		DMdispcsr(ms.x,ms.y);
		drawcsr((prex=ms.x),(prey=ms.y));
		do {
			ms_get(&ms);
		} while (ms.dx==0 && ms.dy==0 && ms.btn1==OFF && ms.btn2==OFF && key_chk()==0);
		DMerasecsr();
		drawcsr(prex,prey);		// 消去
		scrollForCsr(1,1);
		if (ms.btn1==OFFON) {
			if (step==0) {			// 始点指定
				step=1;
				x1=DMimage_getx(ms.x);
				y1=DMimage_gety(ms.y);
			} else if (step==1) {		// 終点指定
				x2=DMimage_getx(ms.x);
				y2=DMimage_gety(ms.y);
				page_edit();
				do_accent(x1,y1,x2,y2);
				page_menu();
				step=0;
			}
		}
		if (ms.btn2==OFFON) {
			if (step==0)
				break;
			else if (step==1)
				step=0;
		}
	}
}

/* end of effect.c */
