//
//	24Bits/Pixel
//
//		Copyright (c) 2000-2001	Chihiro.SAKAMOTO (HyperWorks)
//
#include "StdAfx.h"
#include "Application.h"
#include "Image.h"
#include "File.h"
#include "Misc.h"
#include "dc.h"

#define MaxColorCircle 10
const int colorTbl[MaxColorCircle][3] = {
	{0, 0, 0},
	{255, 0, 0},
	{0, 254, 0},
	{255, 255, 0},
	{0, 240, 240},
	{255, 0, 255},
	{70, 133, 54},
	{0, 60, 210},
	{207, 109, 143},
	{0, 0, 255}
};

CImage::CImage(int width, int height)
{
	Create(width, height);
}

BOOL CImage::Create(int	width, int height)
{
	return CDib::Create(width, height, 24);
}

void CImage::Copy(const	CImage *image, const CRect &rect)
{
	int		len	= rect.Width() * 3;
	for	(int y=rect.top; y<rect.bottom;	y++) {
		memcpy(GetBits(rect.left, y), image->GetBits(rect.left,	y),	len);
	}
}

void CImage::MixImage(const	CImage *image, const CRect &rect, COLORREF trans_color)
{
	const unsigned char	trans_b	= GetBValue(trans_color);
	const unsigned char	trans_g	= GetGValue(trans_color);
	const unsigned char	trans_r	= GetRValue(trans_color);

	for	(int y=rect.top; y<rect.bottom;	y++) {
		byte_t *p =	(byte_t	*)GetBits(rect.left, y);
		const byte_t *q	= (byte_t *)image->GetBits(rect.left, y);
		for	(int x=rect.left; x<rect.right;	x++) {
			const byte_t b = *q++;
			const byte_t g = *q++;
			const byte_t r = *q++;

			if (b != trans_b ||	g != trans_g ||	r != trans_r) {
				p[0] = b;
				p[1] = g;
				p[2] = r;
			}
			p += 3;
		}
	}
}

void CImage::MixImage(const	CImage *image, const CRect &rect, CPoint point,	COLORREF trans_color)
{
	const unsigned char	trans_b	= GetBValue(trans_color);
	const unsigned char	trans_g	= GetGValue(trans_color);
	const unsigned char	trans_r	= GetRValue(trans_color);

	for	(int y=rect.top; y<rect.bottom;	y++) {
		byte_t *p =	(byte_t	*)GetBits(rect.left, y);
		const byte_t *q	= (byte_t *)image->GetBits(point.x,	point.y++);
		for	(int x=rect.left; x<rect.right;	x++) {
			const byte_t b = *q++;
			const byte_t g = *q++;
			const byte_t r = *q++;

			if (b != trans_b ||	g != trans_g ||	r != trans_r) {
				p[0] = b;
				p[1] = g;
				p[2] = r;
			}
			p += 3;
		}
	}
}

void CImage::DrawDottedLine(const POINT point1, const POINT point2, int &spilth, int colorIndex)
{
	int i;
	int b, g, r;
	b = colorTbl[colorIndex][0];
	g = colorTbl[colorIndex][1];
	r = colorTbl[colorIndex][2];
	byte_t *p =	(byte_t	*)GetBits(point1.x, point1.y);
	if(point1.y == point2.y)
	{
		if(point1.x < point2.x)
		{
			for(i=point1.x; i<point2.x; i++)
			{
				spilth++;
				if(spilth <= 4)
				{
					p[0] = b;
					p[1] = g;
					p[2] = r;
				}
				else if(spilth == 6)
				{
					spilth = 0;
				}
				p += 3;
			}
		}
		else
		{
			for(i=point1.x; i>point2.x; i--)
			{
				spilth++;
				if(spilth <= 4)
				{
					p[0] = b;
					p[1] = g;
					p[2] = r;
				}
				else if(spilth == 6)
				{
					spilth = 0;
				}
				p -= 3;
			}
		}
	}
	else
	{
		if(point1.y < point2.y)
		{
			for(i=point1.y; i<point2.y; i++)
			{
				spilth++;
				if(spilth <= 4)
				{
					p[0] = b;
					p[1] = g;
					p[2] = r;
				}
				else if(spilth == 6)
				{
					spilth = 0;
				}
				p -= bytes_per_line;
			}
		}
		else
		{
			for(i=point1.y; i>point2.y; i--)
			{
				spilth++;
				if(spilth <= 4)
				{
					p[0] = b;
					p[1] = g;
					p[2] = r;
				}
				else if(spilth == 6)
				{
					spilth = 0;
				}
				p += bytes_per_line;
			}
		}
	}
}
