Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "trastercm.h"
Toshihiro Shimizu 890ddd
#include "toonz/fill.h"
Toshihiro Shimizu 890ddd
#include "tregion.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "toonz/ttileset.h"
Toshihiro Shimizu 890ddd
#include "toonz/ttilesaver.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzimageutils.h"
Toshihiro Shimizu 890ddd
#include "skeletonlut.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <stack></stack>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace SkeletonLut;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{ // Utility Function
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void computeSeeds(const TRasterCM32P &r, TStroke *stroke,
Shinya Kitaoka 3bfa54
				  std::vector<std::pair<tpoint, int="">> &seeds)</std::pair<tpoint,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int length = (int)stroke->getLength();
Toshihiro Shimizu 890ddd
	TRect bbox = r->getBounds();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPoint oldP;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < length; i++) {
Toshihiro Shimizu 890ddd
		TPoint p = convert(stroke->getPointAtLength(i));
Toshihiro Shimizu 890ddd
		if (p == oldP || !bbox.contains(p))
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		seeds.push_back(std::pair<tpoint, int="">(p, (r->pixels(p.y) + p.x)->getPaint()));</tpoint,>
Toshihiro Shimizu 890ddd
		oldP = p;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void fillArea(const TRasterCM32P &ras, TRegion *r, int colorId,
Toshihiro Shimizu 890ddd
			  bool onlyUnfilled, bool fillPaints, bool fillInks)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRect bbox = convert(r->getBBox());
Toshihiro Shimizu 890ddd
	bbox *= ras->getBounds();
Toshihiro Shimizu 890ddd
	ras->lock();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = bbox.y0; i <= bbox.y1; i++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *line = ras->pixels(i);
Shinya Kitaoka 3bfa54
		std::vector<double> intersections;</double>
Toshihiro Shimizu 890ddd
		r->computeScanlineIntersections(i, intersections);
Toshihiro Shimizu 890ddd
		assert(!(intersections.size() & 0x1));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (UINT j = 0; j < intersections.size(); j += 2) {
Toshihiro Shimizu 890ddd
			if (intersections[j] == intersections[j + 1])
Toshihiro Shimizu 890ddd
				continue;
Shinya Kitaoka 12c444
			int from = std::max(tfloor(intersections[j]), bbox.x0);
Shinya Kitaoka 12c444
			int to = std::min(tceil(intersections[j + 1]), bbox.x1);
Toshihiro Shimizu 890ddd
			TPixelCM32 *pix = line + from;
Toshihiro Shimizu 890ddd
			for (int k = from; k < to; k++, pix++) {
Toshihiro Shimizu 890ddd
				if (fillPaints && (!onlyUnfilled || pix->getPaint() == 0))
Toshihiro Shimizu 890ddd
					pix->setPaint(colorId);
Toshihiro Shimizu 890ddd
				if (fillInks)
Toshihiro Shimizu 890ddd
					pix->setInk(colorId);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void restoreColors(const TRasterCM32P &r, const std::vector<std::pair<tpoint, int="">> &seeds)</std::pair<tpoint,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FillParameters params;
Toshihiro Shimizu 890ddd
	for (UINT i = 0; i < seeds.size(); i++) {
Toshihiro Shimizu 890ddd
		params.m_p = seeds[i].first;
Toshihiro Shimizu 890ddd
		params.m_styleId = seeds[i].second;
Toshihiro Shimizu 890ddd
		fill(r, params);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Return true if all \b pixels in \b rect are pure paint; otherwise return false.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
bool areRectPixelsPurePaint(TPixelCM32 *pixels, TRect rect, int wrap)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int dx = rect.x1 - rect.x0;
Toshihiro Shimizu 890ddd
	TPixelCM32 *pix = pixels + rect.y0 * wrap + rect.x0;
Toshihiro Shimizu 890ddd
	int x, y;
Toshihiro Shimizu 890ddd
	for (y = rect.y0; y <= rect.y1; y++, pix += wrap - dx - 1)
Toshihiro Shimizu 890ddd
		for (x = rect.x0; x <= rect.x1; x++, pix++)
Toshihiro Shimizu 890ddd
			if (!pix->isPurePaint())
Toshihiro Shimizu 890ddd
				return false;
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  Return true if all \b pixels in \b rect are transparent; otherwise return false.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
bool areRectPixelsTransparent(TPixel32 *pixels, TRect rect, int wrap)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int dx = rect.x1 - rect.x0;
Toshihiro Shimizu 890ddd
	TPixel32 *pix = pixels + rect.y0 * wrap + rect.x0;
Toshihiro Shimizu 890ddd
	int x, y;
Toshihiro Shimizu 890ddd
	for (y = rect.y0; y <= rect.y1; y++, pix += wrap - dx - 1)
Toshihiro Shimizu 890ddd
		for (x = rect.x0; x <= rect.x1; x++, pix++)
Toshihiro Shimizu 890ddd
			if (pix->m <= 0)
Toshihiro Shimizu 890ddd
				return false;
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// AreaFiller
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
AreaFiller::AreaFiller(const TRasterCM32P &ras)
Toshihiro Shimizu 890ddd
	: m_ras(ras), m_bounds(ras->getBounds()), m_pixels(ras->pixels()), m_wrap(ras->getWrap()), m_color(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_ras->lock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
AreaFiller::~AreaFiller()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
// questa funzione viene chiamata dopo il fill rect delle aree, e colora gli inchiostri di tipo "autoink"
Toshihiro Shimizu 890ddd
// che confinano con le aree appena fillate con il rect. rbefore e' il rect del raster prima del rectfill.
Toshihiro Shimizu 890ddd
void fillautoInks(TRasterCM32P &rin, TRect &rect, const TRasterCM32P &rbefore, TPalette *plt)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(plt);
Toshihiro Shimizu 890ddd
	TRasterCM32P r = rin->extract(rect);
Toshihiro Shimizu 890ddd
	assert(r->getSize() == rbefore->getSize());
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < r->getLy(); i++) {
Toshihiro Shimizu 890ddd
		TPixelCM32 *pix = r->pixels(i);
Toshihiro Shimizu 890ddd
		TPixelCM32 *pixb = rbefore->pixels(i);
Toshihiro Shimizu 890ddd
		for (j = 0; j < r->getLx(); j++, pix++, pixb++) {
Toshihiro Shimizu 890ddd
			int paint = pix->getPaint();
Toshihiro Shimizu 890ddd
			int tone = pix->getTone();
Toshihiro Shimizu 890ddd
			int ink = pix->getInk();
Toshihiro Shimizu 890ddd
			if (paint != pixb->getPaint() && tone > 0 && tone < 255 && ink != paint && plt->getStyle(ink)->getFlags() != 0)
Toshihiro Shimizu 890ddd
				inkFill(rin, TPoint(j, i) + rect.getP00(), paint, 0, NULL, &rect);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void AreaFiller::rectFill(const TRect &rect, int color,
Toshihiro Shimizu 890ddd
						  bool onlyUnfilled, bool fillPaints, bool fillInks)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//Viene trattato il caso fillInks
Toshihiro Shimizu 890ddd
	/*- FillInkのみの場合 -*/
Toshihiro Shimizu 890ddd
	if (!fillPaints) {
Toshihiro Shimizu 890ddd
		assert(fillInks);
Toshihiro Shimizu 890ddd
		assert(m_ras->getBounds().contains(rect));
Toshihiro Shimizu 890ddd
		for (int y = rect.y0; y <= rect.y1; y++) {
Toshihiro Shimizu 890ddd
			TPixelCM32 *pix = m_ras->pixels(y) + rect.x0;
Toshihiro Shimizu 890ddd
			for (int x = rect.x0; x <= rect.x1; x++, pix++)
Toshihiro Shimizu 890ddd
				pix->setInk(color);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRect r = m_bounds * rect;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int dx = r.x1 - r.x0;
Toshihiro Shimizu 890ddd
	int dy = (r.y1 - r.y0) * m_wrap;
Toshihiro Shimizu 890ddd
	if (dx < 2 || dy < 2) //rect degenere(area contenuta nulla), skippo.
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::vector<int> frameSeed(2 * (r.getLx() + r.getLy() - 2));</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int x, y, count1, count2;
Toshihiro Shimizu 890ddd
	/*- ptrをRect範囲のスタート地点に移動 -*/
Toshihiro Shimizu 890ddd
	Pixel *ptr = m_pixels + r.y0 * m_wrap + r.x0;
Toshihiro Shimizu 890ddd
	count1 = 0;
Toshihiro Shimizu 890ddd
	count2 = r.y1 - r.y0 + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Se il rettangolo non contiene il bordo del raster e se tutti i pixels
Toshihiro Shimizu 890ddd
	//contenuti nel rettangolo sono pure paint non deve fare nulla!
Toshihiro Shimizu 890ddd
	if (!rect.contains(m_bounds) &&
Toshihiro Shimizu 890ddd
		areRectPixelsPurePaint(m_pixels, r, m_wrap))
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Viene riempito frameSeed con tutti i paint delle varie aree del rettangolo di contorno.
Toshihiro Shimizu 890ddd
	//Viene verificato se i pixels del rettangolo sono tutti pure paint.
Toshihiro Shimizu 890ddd
	/*- 輪郭のPaintのIDをframeseed内に格納 -*/
Toshihiro Shimizu 890ddd
	for (y = r.y0; y <= r.y1; y++, ptr += m_wrap, count1++, count2++) {
Toshihiro Shimizu 890ddd
		if (r.x0 > 0)
Toshihiro Shimizu 890ddd
			frameSeed[count1] = ptr->getPaint();
Toshihiro Shimizu 890ddd
		if (r.x1 < m_ras->getLx() - 1)
Toshihiro Shimizu 890ddd
			frameSeed[count2] = (ptr + dx)->getPaint();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	ptr = m_pixels + r.y0 * m_wrap + r.x0 + 1;
Toshihiro Shimizu 890ddd
	count1 = count2;
Toshihiro Shimizu 890ddd
	count2 = count1 + r.x1 - r.x0 - 1;
Toshihiro Shimizu 890ddd
	for (x = r.x0 + 1; x < r.x1; x++, ptr++, count1++, count2++) {
Toshihiro Shimizu 890ddd
		if (r.y0 > 0)
Toshihiro Shimizu 890ddd
			frameSeed[count1] = ptr->getPaint();
Toshihiro Shimizu 890ddd
		if (r.y1 < m_ras->getLy() - 1)
Toshihiro Shimizu 890ddd
			frameSeed[count2] = (ptr + dy)->getPaint();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	assert(count2 == 2 * (r.getLx() + r.getLy() - 2));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Viene fillato l'interno e il bordo del rettangolo rect con color
Toshihiro Shimizu 890ddd
	Pixel *pix = m_pixels + r.y0 * m_wrap + r.x0;
Toshihiro Shimizu 890ddd
	if (onlyUnfilled)
Toshihiro Shimizu 890ddd
		for (y = r.y0; y <= r.y1; y++, pix += m_wrap - dx - 1) {
Toshihiro Shimizu 890ddd
			for (x = r.x0; x <= r.x1; x++, pix++) {
Toshihiro Shimizu 890ddd
				if (pix->getPaint() == 0) // BackgroundStyle
Toshihiro Shimizu 890ddd
					pix->setPaint(color);
Toshihiro Shimizu 890ddd
				if (fillInks)
Toshihiro Shimizu 890ddd
					pix->setInk(color);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		for (y = r.y0; y <= r.y1; y++, pix += m_wrap - dx - 1) {
Toshihiro Shimizu 890ddd
			for (x = r.x0; x <= r.x1; x++, pix++) {
Toshihiro Shimizu 890ddd
				pix->setPaint(color);
Toshihiro Shimizu 890ddd
				if (fillInks)
Toshihiro Shimizu 890ddd
					pix->setInk(color);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Vengono fillati i pixel del rettangolo con i paint (mantenuti in frameSeed) che
Toshihiro Shimizu 890ddd
	//c'erano prima di fillare l'intero rettangolo, in questo modo si riportano
Toshihiro Shimizu 890ddd
	//al colore originale le aree che non sono chiuse e non dovevano essere fillate.
Toshihiro Shimizu 890ddd
	count1 = 0;
Toshihiro Shimizu 890ddd
	FillParameters params;
Toshihiro Shimizu 890ddd
	if (r.x0 > 0)
Toshihiro Shimizu 890ddd
		for (y = r.y0; y <= r.y1; y++) {
Toshihiro Shimizu 890ddd
			params.m_p = TPoint(r.x0, y);
Toshihiro Shimizu 890ddd
			params.m_styleId = frameSeed[count1++];
Toshihiro Shimizu 890ddd
			fill(m_ras, params);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		count1 += r.y1 - r.y0 + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (r.x1 < m_ras->getLx() - 1)
Toshihiro Shimizu 890ddd
		for (y = r.y0; y <= r.y1; y++) {
Toshihiro Shimizu 890ddd
			params.m_p = TPoint(r.x1, y);
Toshihiro Shimizu 890ddd
			params.m_styleId = frameSeed[count1++];
Toshihiro Shimizu 890ddd
			fill(m_ras, params);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		count1 += r.y1 - r.y0 + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (r.y0 > 0)
Toshihiro Shimizu 890ddd
		for (x = r.x0 + 1; x < r.x1; x++) {
Toshihiro Shimizu 890ddd
			params.m_p = TPoint(x, r.y0);
Toshihiro Shimizu 890ddd
			params.m_styleId = frameSeed[count1++];
Toshihiro Shimizu 890ddd
			fill(m_ras, params);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		count1 += r.x1 - r.x0 - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (r.y1 < m_ras->getLy() - 1)
Toshihiro Shimizu 890ddd
		for (x = r.x0 + 1; x < r.x1; x++) {
Toshihiro Shimizu 890ddd
			params.m_p = TPoint(x, r.y1);
Toshihiro Shimizu 890ddd
			params.m_styleId = frameSeed[count1++];
Toshihiro Shimizu 890ddd
			fill(m_ras, params);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void AreaFiller::strokeFill(TStroke *stroke, int colorId,
Toshihiro Shimizu 890ddd
							bool onlyUnfilled, bool fillPaints, bool fillInks)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	stroke->transform(TTranslation(convert(m_ras->getCenter())));
Toshihiro Shimizu 890ddd
	m_ras->lock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::vector<std::pair<tpoint, int="">> seeds;</std::pair<tpoint,>
Toshihiro Shimizu 890ddd
	computeSeeds(m_ras, stroke, seeds);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TVectorImage app;
Toshihiro Shimizu 890ddd
	app.addStroke(stroke);
Toshihiro Shimizu 890ddd
	app.findRegions();
Toshihiro Shimizu 890ddd
	for (UINT i = 0; i < app.getRegionCount(); i++)
Toshihiro Shimizu 890ddd
		fillArea(m_ras, app.getRegion(i), colorId, onlyUnfilled, fillPaints, fillInks);
Toshihiro Shimizu 890ddd
	app.removeStroke(0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	stroke->transform(TTranslation(convert(-m_ras->getCenter())));
Toshihiro Shimizu 890ddd
	restoreColors(m_ras, seeds);
Toshihiro Shimizu 890ddd
	m_ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// FullColorAreaFiller
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FullColorAreaFiller::FullColorAreaFiller(const TRaster32P &ras)
Toshihiro Shimizu 890ddd
	: m_ras(ras), m_bounds(ras->getBounds()), m_pixels(ras->pixels()), m_wrap(ras->getWrap()), m_color(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_ras->lock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FullColorAreaFiller::~FullColorAreaFiller()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void FullColorAreaFiller::rectFill(const TRect &rect, const FillParameters ¶ms, bool onlyUnfilled)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRect bbox = m_ras->getBounds();
Toshihiro Shimizu 890ddd
	TRect r = rect * bbox;
Toshihiro Shimizu 890ddd
	if (r.isEmpty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRaster32P workRas = m_ras->extract(r);
Toshihiro Shimizu 890ddd
	TRaster32P copy = workRas->clone();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixel32 color = params.m_palette->getStyle(params.m_styleId)->getMainColor();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Fillo tutto il quadaratino con color
Toshihiro Shimizu 890ddd
	int x, y;
Toshihiro Shimizu 890ddd
	for (y = 0; y < workRas->getLy(); y++) {
Toshihiro Shimizu 890ddd
		TPixel32 *line = workRas->pixels(y);
Toshihiro Shimizu 890ddd
		for (x = 0; x < workRas->getLx(); x++)
Toshihiro Shimizu 890ddd
			*(line + x) = overPix(color, workRas->pixels(y)[x]);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	FillParameters paramsApp = params;
Toshihiro Shimizu 890ddd
	TPixel32 refColor;
Toshihiro Shimizu 890ddd
	for (y = 0; y < workRas->getLy(); y++) {
Toshihiro Shimizu 890ddd
		paramsApp.m_p = TPoint(0, y);
Toshihiro Shimizu 890ddd
		if (y == 0 || refColor != workRas->pixels(paramsApp.m_p.y)[paramsApp.m_p.x]) {
Toshihiro Shimizu 890ddd
			fill(workRas, copy, paramsApp);
Toshihiro Shimizu 890ddd
			refColor = workRas->pixels(paramsApp.m_p.y)[paramsApp.m_p.x];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (y = 0; y < workRas->getLy(); y++) {
Toshihiro Shimizu 890ddd
		paramsApp.m_p = TPoint(workRas->getLx() - 1, y);
Toshihiro Shimizu 890ddd
		if (y == 0 || refColor != workRas->pixels(paramsApp.m_p.y)[paramsApp.m_p.x]) {
Toshihiro Shimizu 890ddd
			fill(workRas, copy, paramsApp);
Toshihiro Shimizu 890ddd
			refColor = workRas->pixels(paramsApp.m_p.y)[paramsApp.m_p.x];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (x = 0; x < workRas->getLx(); x++) {
Toshihiro Shimizu 890ddd
		paramsApp.m_p = TPoint(x, 0);
Toshihiro Shimizu 890ddd
		if (x == 0 || refColor != workRas->pixels(paramsApp.m_p.y)[paramsApp.m_p.x]) {
Toshihiro Shimizu 890ddd
			fill(workRas, copy, paramsApp);
Toshihiro Shimizu 890ddd
			refColor = workRas->pixels(paramsApp.m_p.y)[paramsApp.m_p.x];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	for (x = 0; x < workRas->getLx(); x++) {
Toshihiro Shimizu 890ddd
		paramsApp.m_p = TPoint(x, workRas->getLy() - 1);
Toshihiro Shimizu 890ddd
		if (x == 0 || refColor != workRas->pixels(paramsApp.m_p.y)[paramsApp.m_p.x]) {
Toshihiro Shimizu 890ddd
			fill(workRas, copy, paramsApp);
Toshihiro Shimizu 890ddd
			refColor = workRas->pixels(paramsApp.m_p.y)[paramsApp.m_p.x];
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
// InkSegmenter
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const int damInk = 3;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define GROW_FACTOR 2.51
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class InkSegmenter
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int m_lx, m_ly, m_wrap;
Toshihiro Shimizu 890ddd
	int m_displaceVector[8];
Toshihiro Shimizu 890ddd
	TPixelCM32 *m_buf;
Toshihiro Shimizu 890ddd
	TRect m_bBox;
Toshihiro Shimizu 890ddd
	TRasterCM32P m_r;
Toshihiro Shimizu 890ddd
	TTileSaverCM32 *m_saver;
Toshihiro Shimizu 890ddd
	float m_growFactor;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	InkSegmenter(const TRasterCM32P &r, float growFactor, TTileSaverCM32 *saver)
Toshihiro Shimizu 890ddd
		: m_r(r), m_lx(r->getLx()), m_ly(r->getLy()), m_wrap(r->getWrap()), m_buf((TPixelCM32 *)r->getRawData()), m_bBox(r->getBounds()), m_saver(saver), m_growFactor(growFactor)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		m_displaceVector[0] = -m_wrap - 1;
Toshihiro Shimizu 890ddd
		m_displaceVector[1] = -m_wrap;
Toshihiro Shimizu 890ddd
		m_displaceVector[2] = -m_wrap + 1;
Toshihiro Shimizu 890ddd
		m_displaceVector[3] = -1;
Toshihiro Shimizu 890ddd
		m_displaceVector[4] = +1;
Toshihiro Shimizu 890ddd
		m_displaceVector[5] = m_wrap - 1;
Toshihiro Shimizu 890ddd
		m_displaceVector[6] = m_wrap;
Toshihiro Shimizu 890ddd
		m_displaceVector[7] = m_wrap + 1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool compute(const TPoint &pin, int ink, bool isSelective)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPixelCM32 *pix;
Toshihiro Shimizu 890ddd
		int distance;
Toshihiro Shimizu 890ddd
		TPixelCM32 *master;
Toshihiro Shimizu 890ddd
		TPoint mp, sp;
Toshihiro Shimizu 890ddd
		TPixelCM32 *slave;
Toshihiro Shimizu 890ddd
		TPixelCM32 *d11, *d12, *d21, *d22;
Toshihiro Shimizu 890ddd
		TPoint d1p1, d1p2, d2p1, d2p2;
Toshihiro Shimizu 890ddd
		TPoint p = pin;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!m_bBox.contains(p))
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if ((m_buf + p.y * m_wrap + p.x)->isPurePaint() && ((p = nearestInk(p, 2)) == TPoint(-1, -1)))
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		pix = m_buf + p.y * m_wrap + p.x;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*-- 同じインクの場合はreturn --*/
Toshihiro Shimizu 890ddd
		if (pix->getInk() == ink)
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!ConnectionTable[neighboursCode(pix, p)]) {
Toshihiro Shimizu 890ddd
			master = slave = pix;
Toshihiro Shimizu 890ddd
			mp = sp = p;
Toshihiro Shimizu 890ddd
			distance = 0;
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			distance = findTwinPoints(pix, p, master, mp, slave, sp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (distance == -1)
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!findDam(master, mp, slave, sp, distance,
Toshihiro Shimizu 890ddd
					 d11, d1p1, d12, d1p2))
Toshihiro Shimizu 890ddd
			d11 = d12 = d21 = d22 = 0;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			findDamRev(master, mp, slave, sp, distance,
Toshihiro Shimizu 890ddd
					   d21, d2p1, d22, d2p2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//vector<pair<tpixelcm32*, int=""> > oldInks;</pair<tpixelcm32*,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		drawSegment(d1p1, d1p2, damInk, m_saver);
Toshihiro Shimizu 890ddd
		drawSegment(d2p1, d2p2, damInk, m_saver);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		inkSegmentFill(p, ink, isSelective, m_saver);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//UINT i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		drawSegment(d1p1, d1p2, ink, m_saver);
Toshihiro Shimizu 890ddd
		drawSegment(d2p1, d2p2, ink, m_saver);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*	for (i=0; i
Toshihiro Shimizu 890ddd
		(oldInks[i].first)->setInk(ink);*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	void drawSegment(const TPoint &p0, const TPoint &p1, int ink,
Toshihiro Shimizu 890ddd
					 /*vector<pair<tpixelcm32*, int=""> >& oldInks,*/ TTileSaverCM32 *saver);</pair<tpixelcm32*,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int findTwinPoints(TPixelCM32 *pix, const TPoint &p,
Toshihiro Shimizu 890ddd
					   TPixelCM32 *&master, TPoint &mp,
Toshihiro Shimizu 890ddd
					   TPixelCM32 *&slave, TPoint &sp);
Toshihiro Shimizu 890ddd
	int searchForNearestSlave(TPixelCM32 *pix1, TPixelCM32 *pix2,
Toshihiro Shimizu 890ddd
							  const TPoint &p1, TPoint &p2,
Toshihiro Shimizu 890ddd
							  TPixelCM32 *&slave, TPoint &sp);
Toshihiro Shimizu 890ddd
	int rearrangePoints(TPixelCM32 *&master, TPoint &mp,
Toshihiro Shimizu 890ddd
						TPixelCM32 *&slave, int s_prewalker,
Toshihiro Shimizu 890ddd
						TPoint &sp, int walk);
Toshihiro Shimizu 890ddd
	int rearrangePointsRev(TPixelCM32 *&master, TPoint &mp,
Toshihiro Shimizu 890ddd
						   TPixelCM32 *&slave, int s_prewalker,
Toshihiro Shimizu 890ddd
						   TPoint &sp, int walk);
Toshihiro Shimizu 890ddd
	int dragSlave(TPoint mp, TPixelCM32 *&slave, int &s_prewalker, TPoint &sp);
Toshihiro Shimizu 890ddd
	int dragSlaveRev(TPoint mp, TPixelCM32 *&slave, int &s_prewalker,
Toshihiro Shimizu 890ddd
					 TPoint &sp, TPixelCM32 *first_slave);
Toshihiro Shimizu 890ddd
	bool findDam(TPixelCM32 *master, TPoint mp,
Toshihiro Shimizu 890ddd
				 TPixelCM32 *slave, TPoint sp, int distance,
Toshihiro Shimizu 890ddd
				 TPixelCM32 *&d11, TPoint &d1p1,
Toshihiro Shimizu 890ddd
				 TPixelCM32 *&d12, TPoint &d1p2);
Toshihiro Shimizu 890ddd
	void findDamRev(TPixelCM32 *master, TPoint mp,
Toshihiro Shimizu 890ddd
					TPixelCM32 *slave, TPoint sp, int distance,
Toshihiro Shimizu 890ddd
					TPixelCM32 *&d11, TPoint &d1p1,
Toshihiro Shimizu 890ddd
					TPixelCM32 *&d12, TPoint &d1p2);
Toshihiro Shimizu 890ddd
	int nextPointIsGoodRev(TPoint mp, TPoint sp,
Toshihiro Shimizu 890ddd
						   TPixelCM32 *slave, int s_prewalker, int distance);
Toshihiro Shimizu 890ddd
	int nextPointIsGood(TPoint mp, TPoint sp,
Toshihiro Shimizu 890ddd
						TPixelCM32 *slave, int s_prewalker, int distance);
Toshihiro Shimizu 890ddd
	void inkSegmentFill(const TPoint &p, int ink, bool isSelective, TTileSaverCM32 *saver);
Toshihiro Shimizu 890ddd
	TPoint nearestInk(const TPoint &p, int ray);
Toshihiro Shimizu 890ddd
	inline int stepReversed(TPixelCM32 *walker, int prewalker, int &distance,
Toshihiro Shimizu 890ddd
							const TPoint &p1, TPoint &p2);
Toshihiro Shimizu 890ddd
	inline int stepForward(TPixelCM32 *walker, int prewalker, int &distance,
Toshihiro Shimizu 890ddd
						   const TPoint &p1, TPoint &p2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixelCM32 *ePix(TPixelCM32 *br) { return (br + 1); }
Toshihiro Shimizu 890ddd
	TPixelCM32 *wPix(TPixelCM32 *br) { return (br - 1); }
Toshihiro Shimizu 890ddd
	TPixelCM32 *nPix(TPixelCM32 *br) { return (br + m_wrap); }
Toshihiro Shimizu 890ddd
	TPixelCM32 *sPix(TPixelCM32 *br) { return (br - m_wrap); }
Toshihiro Shimizu 890ddd
	TPixelCM32 *swPix(TPixelCM32 *br) { return (br - m_wrap - 1); }
Toshihiro Shimizu 890ddd
	TPixelCM32 *nwPix(TPixelCM32 *br) { return (br + m_wrap - 1); }
Toshihiro Shimizu 890ddd
	TPixelCM32 *nePix(TPixelCM32 *br) { return (br + m_wrap + 1); }
Toshihiro Shimizu 890ddd
	TPixelCM32 *sePix(TPixelCM32 *br) { return (br - m_wrap + 1); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	UCHAR neighboursCode(TPixelCM32 *seed, const TPoint &p)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		//assert(p == TPoint((seed-m_buf)%m_wrap, (seed-m_buf)/m_wrap));
Toshihiro Shimizu 890ddd
		bool w = (p.x > 0), e = (p.x < m_lx - 1), s = (p.y > 0), n = (p.y < m_ly - 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		return (((s && w) ? ((!swPix(seed)->isPurePaint())) : 0) |
Toshihiro Shimizu 890ddd
				((s) ? ((!sPix(seed)->isPurePaint()) << 1) : 0) |
Toshihiro Shimizu 890ddd
				((s && e) ? ((!sePix(seed)->isPurePaint()) << 2) : 0) |
Toshihiro Shimizu 890ddd
				((w) ? ((!wPix(seed)->isPurePaint()) << 3) : 0) |
Toshihiro Shimizu 890ddd
				((e) ? ((!ePix(seed)->isPurePaint()) << 4) : 0) |
Toshihiro Shimizu 890ddd
				((n && w) ? ((!nwPix(seed)->isPurePaint()) << 5) : 0) |
Toshihiro Shimizu 890ddd
				((n) ? ((!nPix(seed)->isPurePaint()) << 6) : 0) |
Toshihiro Shimizu 890ddd
				((n && e) ? ((!nePix(seed)->isPurePaint()) << 7) : 0));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define DRAW_SEGMENT(a, b, da, db, istr1, istr2, block) \
Toshihiro Shimizu 890ddd
	{                                                   \
Toshihiro Shimizu 890ddd
		d = 2 * db - da;                                \
Toshihiro Shimizu 890ddd
		incr_1 = 2 * db;                                \
Toshihiro Shimizu 890ddd
		incr_2 = 2 * (db - da);                         \
Toshihiro Shimizu 890ddd
		while (a < da) {                                \
Toshihiro Shimizu 890ddd
			if (d <= 0) {                               \
Toshihiro Shimizu 890ddd
				d += incr_1;                            \
Toshihiro Shimizu 890ddd
				a++;                                    \
Toshihiro Shimizu 890ddd
				istr1;                                  \
Toshihiro Shimizu 890ddd
			} else {                                    \
Toshihiro Shimizu 890ddd
				d += incr_2;                            \
Toshihiro Shimizu 890ddd
				a++;                                    \
Toshihiro Shimizu 890ddd
				b++;                                    \
Toshihiro Shimizu 890ddd
				istr2;                                  \
Toshihiro Shimizu 890ddd
			}                                           \
Toshihiro Shimizu 890ddd
			block;                                      \
Toshihiro Shimizu 890ddd
		}                                               \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
#define SET_INK                                                              \
Toshihiro Shimizu 890ddd
	{                                                                        \
Toshihiro Shimizu 890ddd
		if (saver)                                                           \
Toshihiro Shimizu 890ddd
			saver->save(TPoint(x1 + x, y1 + y));                             \
Toshihiro Shimizu 890ddd
		/*if (buf->getInk()!=damInk)*/                                       \
Toshihiro Shimizu 890ddd
		/*  oldInks.push_back(pair<tpixelcm32*, int="">(buf, buf->getInk()));*/ \</tpixelcm32*,>
Toshihiro Shimizu 890ddd
		buf->setInk(ink);                                                    \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void InkSegmenter::drawSegment(const TPoint &p0, const TPoint &p1, int ink,
Toshihiro Shimizu 890ddd
							   /*vector<pair<tpixelcm32*, int=""> >& oldInks,*/ TTileSaverCM32 *saver)</pair<tpixelcm32*,>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int x, y, dx, dy, d, incr_1, incr_2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int x1 = p0.x;
Toshihiro Shimizu 890ddd
	int y1 = p0.y;
Toshihiro Shimizu 890ddd
	int x2 = p1.x;
Toshihiro Shimizu 890ddd
	int y2 = p1.y;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (x1 > x2) {
Toshihiro Shimizu 890ddd
		tswap(x1, x2);
Toshihiro Shimizu 890ddd
		tswap(y1, y2);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPixelCM32 *buf = m_r->pixels() + y1 * m_wrap + x1;
Toshihiro Shimizu 890ddd
	/*if (buf->getInk()!=damInk) 
Toshihiro Shimizu 890ddd
		oldInks.push_back(pair<tpixelcm32*, int="">(buf, buf->getInk()));</tpixelcm32*,>
Toshihiro Shimizu 890ddd
	if ((m_r->pixels() + y2*m_wrap + x2)->getInk()!=damInk) 
Toshihiro Shimizu 890ddd
		oldInks.push_back(pair<tpixelcm32*, int="">(m_r->pixels() + y2*m_wrap + x2, (m_r->pixels() + y2*m_wrap + x2)->getInk()));*/</tpixelcm32*,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (saver) {
Toshihiro Shimizu 890ddd
		saver->save(p0);
Toshihiro Shimizu 890ddd
		saver->save(p1);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	buf->setInk(ink);
Toshihiro Shimizu 890ddd
	(m_r->pixels() + y2 * m_wrap + x2)->setInk(ink);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	dx = x2 - x1;
Toshihiro Shimizu 890ddd
	dy = y2 - y1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x = y = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dy >= 0) {
Toshihiro Shimizu 890ddd
		if (dy <= dx)
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(x, y, dx, dy, (buf++), (buf += m_wrap + 1), SET_INK)
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(y, x, dy, dx, (buf += m_wrap), (buf += m_wrap + 1), SET_INK)
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		dy = -dy;
Toshihiro Shimizu 890ddd
		if (dy <= dx)
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(x, y, dx, dy, (buf++), (buf -= (m_wrap - 1)), SET_INK)
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(y, x, dy, dx, (buf -= m_wrap), (buf -= (m_wrap - 1)), SET_INK)
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void InkSegmenter::inkSegmentFill(const TPoint &p, int ink, bool isSelective, TTileSaverCM32 *saver)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int x = p.x, y = p.y;
Toshihiro Shimizu 890ddd
	int lx = m_r->getLx();
Toshihiro Shimizu 890ddd
	int ly = m_r->getLy();
Toshihiro Shimizu 890ddd
	TPixelCM32 *pixels = (TPixelCM32 *)m_r->getRawData();
Toshihiro Shimizu 890ddd
	TPixelCM32 *pix = pixels + p.y * m_wrap + x;
Toshihiro Shimizu 890ddd
	int oldInk;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (pix->isPurePaint() || pix->getInk() == ink)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (isSelective)
Toshihiro Shimizu 890ddd
		oldInk = pix->getInk();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::stack<tpoint> seeds;</tpoint>
Toshihiro Shimizu 890ddd
	seeds.push(p);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (!seeds.empty()) {
Toshihiro Shimizu 890ddd
		TPoint seed = seeds.top();
Toshihiro Shimizu 890ddd
		seeds.pop();
Toshihiro Shimizu 890ddd
		//if(!m_r->getBounds().contains(seed)) continue;
Toshihiro Shimizu 890ddd
		x = seed.x;
Toshihiro Shimizu 890ddd
		y = seed.y;
Toshihiro Shimizu 890ddd
		TPixelCM32 *pix = pixels + (y * m_wrap + x);
Toshihiro Shimizu 890ddd
		if (pix->isPurePaint() || pix->getInk() == ink || pix->getInk() == damInk || (isSelective && pix->getInk() != oldInk))
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (saver)
Toshihiro Shimizu 890ddd
			saver->save(seed);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		pix->setInk(ink);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (x > 0)
Toshihiro Shimizu 890ddd
			seeds.push(TPoint(x - 1, y));
Toshihiro Shimizu 890ddd
		if (y > 0)
Toshihiro Shimizu 890ddd
			seeds.push(TPoint(x, y - 1));
Toshihiro Shimizu 890ddd
		if (y < ly - 1)
Toshihiro Shimizu 890ddd
			seeds.push(TPoint(x, y + 1));
Toshihiro Shimizu 890ddd
		if (x < lx - 1)
Toshihiro Shimizu 890ddd
			seeds.push(TPoint(x + 1, y));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (x == lx - 1 || x == 0 || y == ly - 1 || y == 0)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (ePix(pix)->getInk() == damInk ||
Toshihiro Shimizu 890ddd
			wPix(pix)->getInk() == damInk ||
Toshihiro Shimizu 890ddd
			sPix(pix)->getInk() == damInk ||
Toshihiro Shimizu 890ddd
			nPix(pix)->getInk() == damInk ||
Toshihiro Shimizu 890ddd
			nePix(pix)->getInk() == damInk ||
Toshihiro Shimizu 890ddd
			sePix(pix)->getInk() == damInk ||
Toshihiro Shimizu 890ddd
			swPix(pix)->getInk() == damInk ||
Toshihiro Shimizu 890ddd
			nwPix(pix)->getInk() == damInk)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		seeds.push(TPoint(x - 1, y - 1));
Toshihiro Shimizu 890ddd
		seeds.push(TPoint(x - 1, y + 1));
Toshihiro Shimizu 890ddd
		seeds.push(TPoint(x + 1, y - 1));
Toshihiro Shimizu 890ddd
		seeds.push(TPoint(x + 1, y + 1));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPoint InkSegmenter::nearestInk(const TPoint &p, int ray)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 12c444
	for (j = std::max(p.y - ray, 0); j <= std::min(p.y + ray, m_ly - 1); j++)
Shinya Kitaoka 12c444
		for (i = std::max(p.x - ray, 0); i <= std::min(p.x + ray, m_lx - 1); i++)
Toshihiro Shimizu 890ddd
			if (!(m_buf + j * m_wrap + i)->isPurePaint())
Toshihiro Shimizu 890ddd
				return TPoint(i, j);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TPoint(-1, -1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int InkSegmenter::findTwinPoints(TPixelCM32 *pix, const TPoint &p,
Toshihiro Shimizu 890ddd
								 TPixelCM32 *&master, TPoint &mp,
Toshihiro Shimizu 890ddd
								 TPixelCM32 *&slave, TPoint &sp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPixelCM32 *row_p1, *col_p1, *row_p2, *col_p2;
Toshihiro Shimizu 890ddd
	int distance;
Toshihiro Shimizu 890ddd
	int row_x1, row_x2, col_y1, col_y2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	row_p1 = pix - 1;
Toshihiro Shimizu 890ddd
	row_x1 = p.x - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (row_x1 + 1 < m_lx && !(row_p1 + 1)->isPurePaint()) {
Toshihiro Shimizu 890ddd
		row_p1++;
Toshihiro Shimizu 890ddd
		row_x1++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	row_p2 = pix + 1;
Toshihiro Shimizu 890ddd
	row_x2 = p.x + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (row_x2 - 1 > 0 && !(row_p2 - 1)->isPurePaint()) {
Toshihiro Shimizu 890ddd
		row_p2--;
Toshihiro Shimizu 890ddd
		row_x2--;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	master = row_p1;
Toshihiro Shimizu 890ddd
	mp.x = row_x1;
Toshihiro Shimizu 890ddd
	mp.y = p.y;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	col_p1 = pix - m_wrap;
Toshihiro Shimizu 890ddd
	col_y1 = p.y - 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (col_y1 + 1 < m_ly && !(col_p1 + m_wrap)->isPurePaint()) {
Toshihiro Shimizu 890ddd
		col_p1 += m_wrap;
Toshihiro Shimizu 890ddd
		col_y1++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	col_p2 = pix + m_wrap;
Toshihiro Shimizu 890ddd
	col_y2 = p.y + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (col_y2 - 1 > 0 && !(col_p2 - 1)->isPurePaint()) {
Toshihiro Shimizu 890ddd
		col_p2 -= m_wrap;
Toshihiro Shimizu 890ddd
		col_y2--;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (row_x1 - row_x2 <= col_y1 - col_y2) {
Toshihiro Shimizu 890ddd
		master = row_p1;
Toshihiro Shimizu 890ddd
		mp = TPoint(row_x1, p.y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TPoint auxp(row_x2, p.y);
Toshihiro Shimizu 890ddd
		if ((distance = searchForNearestSlave(row_p1, row_p2, mp, auxp, slave, sp)) != 0)
Toshihiro Shimizu 890ddd
			return distance;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		master = col_p1;
Toshihiro Shimizu 890ddd
		mp = TPoint(p.x, col_y1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		auxp = TPoint(p.x, col_y2);
Toshihiro Shimizu 890ddd
		if ((distance = searchForNearestSlave(col_p1, col_p2, mp, auxp, slave, sp)) == 0 /*&& !is_connecting(p1)*/)
Toshihiro Shimizu 890ddd
			return -1;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		master = col_p1;
Toshihiro Shimizu 890ddd
		mp = TPoint(p.x, col_y1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TPoint auxp(p.x, col_y2);
Toshihiro Shimizu 890ddd
		if ((distance = searchForNearestSlave(col_p1, col_p2, mp, auxp, slave, sp)) != 0)
Toshihiro Shimizu 890ddd
			return distance;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		master = row_p1;
Toshihiro Shimizu 890ddd
		mp = TPoint(row_x1, p.y);
Toshihiro Shimizu 890ddd
		auxp = TPoint(row_x2, p.y);
Toshihiro Shimizu 890ddd
		if ((distance = searchForNearestSlave(row_p1, row_p2, mp, auxp, slave, sp)) == 0 /*&& !is_connecting(p1)*/)
Toshihiro Shimizu 890ddd
			return -1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return distance;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline void newP(int next, TPoint &p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	switch (next) {
Toshihiro Shimizu 890ddd
	case 0:
Shinya Kitaoka d4642c
	case 3:
Shinya Kitaoka d4642c
	case 5:
Shinya Kitaoka d4642c
		p.x -= 1;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 2:
Shinya Kitaoka d4642c
	case 4:
Shinya Kitaoka d4642c
	case 7:
Shinya Kitaoka d4642c
		p.x += 1;
Shinya Kitaoka d4642c
		break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	switch (next) {
Toshihiro Shimizu 890ddd
	case 0:
Shinya Kitaoka d4642c
	case 1:
Shinya Kitaoka d4642c
	case 2:
Shinya Kitaoka d4642c
		p.y -= 1;
Shinya Kitaoka d4642c
		break;
Shinya Kitaoka d4642c
	case 5:
Shinya Kitaoka d4642c
	case 6:
Shinya Kitaoka d4642c
	case 7:
Shinya Kitaoka d4642c
		p.y += 1;
Shinya Kitaoka d4642c
		break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline int InkSegmenter::stepReversed(TPixelCM32 *walker, int prewalker, int &distance,
Toshihiro Shimizu 890ddd
									  const TPoint &p1, TPoint &p2)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int next = NextPointTableRev[(neighboursCode(walker, p2) << 3) | prewalker];
Toshihiro Shimizu 890ddd
	newP(next, p2);
Toshihiro Shimizu 890ddd
	distance = norm2(p1 - p2);
Toshihiro Shimizu 890ddd
	return next;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline int InkSegmenter::stepForward(TPixelCM32 *walker, int prewalker, int &distance,
Toshihiro Shimizu 890ddd
									 const TPoint &p1, TPoint &p2)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int next = NextPointTable[(neighboursCode(walker, p2) << 3) | prewalker];
Toshihiro Shimizu 890ddd
	newP(next, p2);
Toshihiro Shimizu 890ddd
	distance = norm2(p1 - p2);
Toshihiro Shimizu 890ddd
	return next;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int InkSegmenter::searchForNearestSlave(TPixelCM32 *pix1, TPixelCM32 *pix2,
Toshihiro Shimizu 890ddd
										const TPoint &p1, TPoint &p2,
Toshihiro Shimizu 890ddd
										TPixelCM32 *&slave, TPoint &sp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int curr_distance, new_distance;
Toshihiro Shimizu 890ddd
	UCHAR prewalker, next;
Toshihiro Shimizu 890ddd
	TPixelCM32 *walker;
Toshihiro Shimizu 890ddd
	TPoint currp2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	currp2 = p2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	curr_distance = norm2(p1 - p2);
Toshihiro Shimizu 890ddd
	walker = pix2;
Toshihiro Shimizu 890ddd
	slave = pix2;
Toshihiro Shimizu 890ddd
	sp = p2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	prewalker = FirstPreseedTable[neighboursCode(walker, p2)];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	next = stepForward(walker, prewalker, new_distance, p1, p2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (curr_distance != 0 && new_distance < curr_distance) {
Toshihiro Shimizu 890ddd
		while (p2.x > 0 && p2.x < m_lx - 1 && p2.y > 0 && p2.y < m_ly - 1 &&
Toshihiro Shimizu 890ddd
			   new_distance < curr_distance && new_distance != 0) {
Toshihiro Shimizu 890ddd
			curr_distance = new_distance;
Toshihiro Shimizu 890ddd
			sp.x = p2.x;
Toshihiro Shimizu 890ddd
			sp.y = p2.y;
Toshihiro Shimizu 890ddd
			walker = walker + m_displaceVector[next];
Toshihiro Shimizu 890ddd
			slave = walker;
Toshihiro Shimizu 890ddd
			prewalker = (~next) & 0x7;
Toshihiro Shimizu 890ddd
			next = stepForward(walker, prewalker, new_distance, p1, p2);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (new_distance != 0)
Toshihiro Shimizu 890ddd
			return curr_distance;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	curr_distance = norm2(p1 - p2);
Toshihiro Shimizu 890ddd
	walker = pix2;
Toshihiro Shimizu 890ddd
	p2 = currp2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	UCHAR code = neighboursCode(walker, p2);
Toshihiro Shimizu 890ddd
	next = FirstPreseedTable[code];
Toshihiro Shimizu 890ddd
	next = NextPointTable[(code << 3) | next];
Toshihiro Shimizu 890ddd
	prewalker = next;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	next = stepReversed(walker, prewalker, new_distance, p1, p2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (p2.x > 0 && p2.x < m_lx - 1 && p2.y > 0 && p2.y < m_ly - 1 &&
Toshihiro Shimizu 890ddd
		curr_distance != 0 && new_distance < curr_distance) {
Toshihiro Shimizu 890ddd
		while (new_distance < curr_distance && new_distance > 0) {
Toshihiro Shimizu 890ddd
			curr_distance = new_distance;
Toshihiro Shimizu 890ddd
			sp = p2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			walker = walker + m_displaceVector[next];
Toshihiro Shimizu 890ddd
			slave = walker;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			prewalker = (~next) & 0x7;
Toshihiro Shimizu 890ddd
			next = stepReversed(walker, prewalker, new_distance, p1, p2);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		if (new_distance != 0)
Toshihiro Shimizu 890ddd
			return curr_distance;
Toshihiro Shimizu 890ddd
	} else if (new_distance != 0)
Toshihiro Shimizu 890ddd
		return curr_distance;
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int InkSegmenter::rearrangePoints(TPixelCM32 *&master, TPoint &mp,
Toshihiro Shimizu 890ddd
								  TPixelCM32 *&slave, int s_prewalker,
Toshihiro Shimizu 890ddd
								  TPoint &sp, int walk)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int s_next;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (walk-- && sp.x > 0 && sp.x < m_lx - 1 && sp.y > 0 && sp.y < m_ly - 1) {
Toshihiro Shimizu 890ddd
		s_next = NextPointTableRev[((neighboursCode(slave, sp)) << 3) | s_prewalker];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		newP(s_next, sp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		slave = slave + m_displaceVector[s_next];
Toshihiro Shimizu 890ddd
		s_prewalker = (~s_next) & 0x7;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int InkSegmenter::rearrangePointsRev(TPixelCM32 *&master, TPoint &mp,
Toshihiro Shimizu 890ddd
									 TPixelCM32 *&slave, int s_prewalker,
Toshihiro Shimizu 890ddd
									 TPoint &sp, int walk)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int s_next;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (walk-- && sp.x > 0 && sp.x < m_lx - 1 && sp.y > 0 && sp.y < m_ly - 1) {
Toshihiro Shimizu 890ddd
		s_next = NextPointTable[((neighboursCode(slave, sp)) << 3) | s_prewalker];
Toshihiro Shimizu 890ddd
		//s_next = NEXT_POINT_24(*slave, s_prewalker);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		newP(s_next, sp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		slave = slave + m_displaceVector[s_next];
Toshihiro Shimizu 890ddd
		s_prewalker = (~s_next) & 0x7;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int InkSegmenter::dragSlave(TPoint mp, TPixelCM32 *&slave, int &s_prewalker, TPoint &sp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int distance, s_next, new_distance;
Toshihiro Shimizu 890ddd
	int ret = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	distance = norm2(mp - sp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	s_next = stepForward(slave, s_prewalker, new_distance, mp, sp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (sp.x > 0 && sp.x < m_lx - 1 && sp.y > 0 && sp.y < m_ly - 1 &&
Toshihiro Shimizu 890ddd
		   (new_distance < distance ||
Toshihiro Shimizu 890ddd
			nextPointIsGood(mp, sp, slave + m_displaceVector[s_next], (~s_next) & 0x7, distance))) {
Toshihiro Shimizu 890ddd
		if (!ret)
Toshihiro Shimizu 890ddd
			ret = 1;
Toshihiro Shimizu 890ddd
		distance = new_distance;
Toshihiro Shimizu 890ddd
		slave = slave + m_displaceVector[s_next];
Toshihiro Shimizu 890ddd
		s_prewalker = (~s_next) & 0x7;
Toshihiro Shimizu 890ddd
		s_next = stepForward(slave, s_prewalker, new_distance, mp, sp);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	newP(((~s_next) & 0x7), sp);
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int InkSegmenter::dragSlaveRev(TPoint mp, TPixelCM32 *&slave, int &s_prewalker,
Toshihiro Shimizu 890ddd
							   TPoint &sp, TPixelCM32 *first_slave)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int distance, new_distance, s_next;
Toshihiro Shimizu 890ddd
	int ret = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	distance = norm2(mp - sp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	s_next = stepReversed(slave, s_prewalker, new_distance, mp, sp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (sp.x > 0 && sp.x < m_lx - 1 && sp.y > 0 && sp.y < m_ly - 1 &&
Toshihiro Shimizu 890ddd
		   (new_distance < distance ||
Toshihiro Shimizu 890ddd
			nextPointIsGoodRev(mp, sp, slave + m_displaceVector[s_next], (~s_next) & 0x7, distance))) {
Toshihiro Shimizu 890ddd
		if (!ret)
Toshihiro Shimizu 890ddd
			ret = 1;
Toshihiro Shimizu 890ddd
		distance = new_distance;
Toshihiro Shimizu 890ddd
		slave = slave + m_displaceVector[s_next];
Toshihiro Shimizu 890ddd
		if (slave == first_slave)
Toshihiro Shimizu 890ddd
			return -1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		s_prewalker = (~s_next) & 0x7;
Toshihiro Shimizu 890ddd
		s_next = stepReversed(slave, s_prewalker, new_distance, mp, sp);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	newP(((~s_next) & 0x7), sp);
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool InkSegmenter::findDam(TPixelCM32 *master, TPoint mp,
Toshihiro Shimizu 890ddd
						   TPixelCM32 *slave, TPoint sp, int distance,
Toshihiro Shimizu 890ddd
						   TPixelCM32 *&d11, TPoint &d1p1,
Toshihiro Shimizu 890ddd
						   TPixelCM32 *&d12, TPoint &d1p2)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int ref_distance, m_prewalker, s_prewalker, m_next, next, ret;
Toshihiro Shimizu 890ddd
	unsigned int walkalone = 0;
Toshihiro Shimizu 890ddd
	TPixelCM32 *first_slave, *first_master;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	first_slave = slave;
Toshihiro Shimizu 890ddd
	first_master = master;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ref_distance = tround(m_growFactor * ((float)distance + 1));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_prewalker = FirstPreseedTable[neighboursCode(master, mp)];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!ConnectionTable[neighboursCode(master, mp)])
Toshihiro Shimizu 890ddd
		s_prewalker = FirstPreseedTableRev[neighboursCode(slave, sp)];
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		UCHAR code = neighboursCode(slave, sp);
Toshihiro Shimizu 890ddd
		next = FirstPreseedTable[code];
Toshihiro Shimizu 890ddd
		next = NextPointTable[(code << 3) | next];
Toshihiro Shimizu 890ddd
		s_prewalker = next;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (mp.x > 0 && mp.x < m_lx - 1 && mp.y > 0 && mp.y < m_ly - 1 &&
Toshihiro Shimizu 890ddd
		   distance < ref_distance &&
Toshihiro Shimizu 890ddd
		   !(((m_next = NextPointTable[((neighboursCode(master, mp)) << 3) | m_prewalker]) == s_prewalker) &&
Toshihiro Shimizu 890ddd
			 master == slave)) {
Toshihiro Shimizu 890ddd
		newP(m_next, mp);
Toshihiro Shimizu 890ddd
		master = master + m_displaceVector[m_next];
Toshihiro Shimizu 890ddd
		m_prewalker = (~m_next) & 0x7;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ret = dragSlaveRev(mp, slave, s_prewalker, sp, first_slave);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (ret == -1)
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (ret == 0)
Toshihiro Shimizu 890ddd
			walkalone++;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			walkalone = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (master == first_master)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		distance = norm2(mp - sp);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (walkalone > 0)
Toshihiro Shimizu 890ddd
		rearrangePoints(master, mp, slave, s_prewalker, sp, walkalone);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	d11 = master;
Toshihiro Shimizu 890ddd
	d1p1 = mp;
Toshihiro Shimizu 890ddd
	d12 = slave;
Toshihiro Shimizu 890ddd
	d1p2 = sp;
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void InkSegmenter::findDamRev(TPixelCM32 *master, TPoint mp,
Toshihiro Shimizu 890ddd
							  TPixelCM32 *slave, TPoint sp, int distance,
Toshihiro Shimizu 890ddd
							  TPixelCM32 *&d11, TPoint &d1p1,
Toshihiro Shimizu 890ddd
							  TPixelCM32 *&d12, TPoint &d1p2)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int ref_distance, m_prewalker, s_prewalker, m_next, next;
Toshihiro Shimizu 890ddd
	unsigned int walkalone = 0;
Toshihiro Shimizu 890ddd
	TPixelCM32 *first_master;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	first_master = master;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	ref_distance = tround(GROW_FACTOR * ((float)distance + 1));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_prewalker = FirstPreseedTableRev[neighboursCode(master, mp)];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!ConnectionTable[neighboursCode(master, mp)]) {
Toshihiro Shimizu 890ddd
		UCHAR code = neighboursCode(slave, sp);
Toshihiro Shimizu 890ddd
		next = FirstPreseedTableRev[code];
Toshihiro Shimizu 890ddd
		next = NextPointTableRev[(code << 3) | next];
Toshihiro Shimizu 890ddd
		s_prewalker = next;
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		s_prewalker = FirstPreseedTable[neighboursCode(slave, sp)];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (mp.x > 0 && mp.x < m_lx - 1 && mp.y > 0 && mp.y < m_ly - 1 &&
Toshihiro Shimizu 890ddd
		   distance < ref_distance &&
Toshihiro Shimizu 890ddd
		   !(((m_next = NextPointTableRev[((neighboursCode(master, mp)) << 3) | m_prewalker]) == s_prewalker) &&
Toshihiro Shimizu 890ddd
			 master == slave)) {
Toshihiro Shimizu 890ddd
		newP(m_next, mp);
Toshihiro Shimizu 890ddd
		master = master + m_displaceVector[m_next];
Toshihiro Shimizu 890ddd
		m_prewalker = (~m_next) & 0x7;
Toshihiro Shimizu 890ddd
		if (!dragSlave(mp, slave, s_prewalker, sp))
Toshihiro Shimizu 890ddd
			walkalone++;
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			walkalone = 0;
Toshihiro Shimizu 890ddd
		if (master == first_master)
Toshihiro Shimizu 890ddd
			break;
Toshihiro Shimizu 890ddd
		distance = norm2(mp - sp);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (walkalone > 0)
Toshihiro Shimizu 890ddd
		rearrangePointsRev(master, mp, slave, s_prewalker, sp, walkalone);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	d11 = master;
Toshihiro Shimizu 890ddd
	d1p1 = mp;
Toshihiro Shimizu 890ddd
	d12 = slave;
Toshihiro Shimizu 890ddd
	d1p2 = sp;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int InkSegmenter::nextPointIsGood(TPoint mp, TPoint sp,
Toshihiro Shimizu 890ddd
								  TPixelCM32 *slave, int s_prewalker, int distance)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int s_next;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	s_next = NextPointTable[((neighboursCode(slave, sp)) << 3) | s_prewalker];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	newP(s_next, sp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (norm2(mp - sp) <= distance);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int InkSegmenter::nextPointIsGoodRev(TPoint mp, TPoint sp,
Toshihiro Shimizu 890ddd
									 TPixelCM32 *slave, int s_prewalker, int distance)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int s_next;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	s_next = NextPointTableRev[((neighboursCode(slave, sp)) << 3) | s_prewalker];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	newP(s_next, sp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (norm2(mp - sp) <= distance);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool inkSegment(const TRasterCM32P &r, const TPoint &p, int ink, float growFactor, bool isSelective, TTileSaverCM32 *saver)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	r->lock();
Toshihiro Shimizu 890ddd
	InkSegmenter is(r, growFactor, saver);
Toshihiro Shimizu 890ddd
	bool ret = is.compute(p, ink, isSelective);
Toshihiro Shimizu 890ddd
	r->unlock();
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}