Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/strokesdata.h"
Toshihiro Shimizu 890ddd
#include "tthreadmessage.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/rasterimagedata.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzimageutils.h"
Toshihiro Shimizu 890ddd
#include "toonz/trasterimageutils.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int findStroke(const TVectorImageP &img, TStroke *stroke, const TAffine &aff)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRectD strokeBBox = aff * stroke->getBBox();
Toshihiro Shimizu 890ddd
	int count = img->getStrokeCount();
Toshihiro Shimizu 890ddd
	for (int i = 0; i < count; i++) {
Toshihiro Shimizu 890ddd
		TStroke *s = img->getStroke(i);
Toshihiro Shimizu 890ddd
		TRectD bbox = s->getBBox();
Toshihiro Shimizu 890ddd
		if (tdistance2(bbox.getP00(), strokeBBox.getP00()) +
Toshihiro Shimizu 890ddd
				tdistance2(bbox.getP11(), strokeBBox.getP11()) >
Toshihiro Shimizu 890ddd
			0.001)
Toshihiro Shimizu 890ddd
			continue;
Toshihiro Shimizu 890ddd
		return i;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return -1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TAffine findOffset(const TVectorImageP &srcImg, const TVectorImageP &img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TAffine offset;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TVectorImageP tarImg = img;
Toshihiro Shimizu 890ddd
	if (!tarImg)
Toshihiro Shimizu 890ddd
		return offset;
Toshihiro Shimizu 890ddd
	if (tarImg->getStrokeCount() == 0 || srcImg->getStrokeCount() == 0)
Toshihiro Shimizu 890ddd
		return offset;
Toshihiro Shimizu 890ddd
	bool done = false;
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	while (!done)
Toshihiro Shimizu 890ddd
		for (i = 0; i < (int)srcImg->getStrokeCount(); i++) {
Toshihiro Shimizu 890ddd
			TStroke *stroke = srcImg->getStroke(i);
Toshihiro Shimizu 890ddd
			assert(stroke);
Toshihiro Shimizu 890ddd
			if (findStroke(tarImg, stroke, offset) >= 0) {
Toshihiro Shimizu 890ddd
				offset = offset * TTranslation(10, -10);
Toshihiro Shimizu 890ddd
				break;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			done = true;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	return offset;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TStroke getStrokeByRect(TRectD r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TStroke stroke;
Toshihiro Shimizu 890ddd
	if (r.isEmpty())
Toshihiro Shimizu 890ddd
		return stroke;
Toshihiro Shimizu 890ddd
	vector<tthickpoint> points;</tthickpoint>
Toshihiro Shimizu 890ddd
	points.push_back(r.getP00());
Toshihiro Shimizu 890ddd
	points.push_back((r.getP00() + r.getP01()) * 0.5);
Toshihiro Shimizu 890ddd
	points.push_back(r.getP01());
Toshihiro Shimizu 890ddd
	points.push_back((r.getP01() + r.getP11()) * 0.5);
Toshihiro Shimizu 890ddd
	points.push_back(r.getP11());
Toshihiro Shimizu 890ddd
	points.push_back((r.getP11() + r.getP10()) * 0.5);
Toshihiro Shimizu 890ddd
	points.push_back(r.getP10());
Toshihiro Shimizu 890ddd
	points.push_back((r.getP10() + r.getP00()) * 0.5);
Toshihiro Shimizu 890ddd
	points.push_back(r.getP00());
Toshihiro Shimizu 890ddd
	stroke.reshape(&(points[0]), points.size());
Toshihiro Shimizu 890ddd
	stroke.setSelfLoop(true);
Toshihiro Shimizu 890ddd
	return stroke;
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
// StrokesData
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StrokesData::setImage(TVectorImageP image, const std::set<int> &indices)</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!image)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	if (indices.empty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// indices e' un set; splitImage si aspetta un vector
Toshihiro Shimizu 890ddd
	vector<int> indicesV(indices.begin(), indices.end());</int>
Toshihiro Shimizu 890ddd
	QMutexLocker lock(image->getMutex());
Toshihiro Shimizu 890ddd
	m_image = image->splitImage(indicesV, false);
Toshihiro Shimizu 890ddd
	if (m_image->getPalette() == 0) {
Toshihiro Shimizu 890ddd
		// nel caso lo stroke sia un path (e quindi senza palette)
Toshihiro Shimizu 890ddd
		m_image->setPalette(new TPalette());
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void StrokesData::getImage(TVectorImageP image, std::set<int> &indices, bool insert) const</int>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_image)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TVectorImageP srcImg = m_image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QMutexLocker lock(image->getMutex());
Toshihiro Shimizu 890ddd
	if (insert) {
Toshihiro Shimizu 890ddd
		TAffine offset = findOffset(srcImg, image);
Toshihiro Shimizu 890ddd
		UINT oldImageSize = image->getStrokeCount();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		image->mergeImage(srcImg, offset, false);
Toshihiro Shimizu 890ddd
		UINT newImageSize = image->getStrokeCount();
Toshihiro Shimizu 890ddd
		indices.clear();
Toshihiro Shimizu 890ddd
		for (UINT sI = oldImageSize; sI < newImageSize; sI++)
Toshihiro Shimizu 890ddd
			indices.insert(sI);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		std::vector<int> indicesToInsert(indices.begin(), indices.end());</int>
Toshihiro Shimizu 890ddd
		if (indicesToInsert.empty())
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		image->insertImage(srcImg, indicesToInsert);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzImageData *StrokesData::toToonzImageData(const TToonzImageP &imageToPaste) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double dpix, dpiy;
Toshihiro Shimizu 890ddd
	imageToPaste->getDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
	assert(dpix != 0 && dpiy != 0);
Toshihiro Shimizu 890ddd
	TScale sc(dpix / Stage::inch, dpiy / Stage::inch);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRectD bbox = sc * m_image->getBBox();
Toshihiro Shimizu 890ddd
	bbox.x0 = tfloor(bbox.x0);
Toshihiro Shimizu 890ddd
	bbox.y0 = tfloor(bbox.y0);
Toshihiro Shimizu 890ddd
	bbox.x1 = tceil(bbox.x1);
Toshihiro Shimizu 890ddd
	bbox.y1 = tceil(bbox.y1);
Toshihiro Shimizu 890ddd
	TDimension size(bbox.getLx(), bbox.getLy());
Toshihiro Shimizu 890ddd
	TToonzImageP app = ToonzImageUtils::vectorToToonzImage(
Toshihiro Shimizu 890ddd
		m_image, sc, m_image->getPalette(), bbox.getP00(), size, 0, true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	vector<trectd> rects;</trectd>
Toshihiro Shimizu 890ddd
	vector<tstroke> strokes;</tstroke>
Toshihiro Shimizu 890ddd
	TStroke stroke = getStrokeByRect(bbox);
Toshihiro Shimizu 890ddd
	strokes.push_back(stroke);
Toshihiro Shimizu 890ddd
	ToonzImageData *data = new ToonzImageData();
Toshihiro Shimizu 890ddd
	data->setData(app->getRaster(), m_image->getPalette(), dpix, dpiy, TDimension(), rects, strokes, strokes, TAffine());
Toshihiro Shimizu 890ddd
	return data;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FullColorImageData *StrokesData::toFullColorImageData(const TRasterImageP &imageToPaste) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	double dpix, dpiy;
Toshihiro Shimizu 890ddd
	imageToPaste->getDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
	assert(dpix != 0 && dpiy != 0);
Toshihiro Shimizu 890ddd
	TScale sc(dpix / Stage::inch, dpiy / Stage::inch);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRectD bbox = sc * m_image->getBBox();
Toshihiro Shimizu 890ddd
	bbox.x0 = tfloor(bbox.x0);
Toshihiro Shimizu 890ddd
	bbox.y0 = tfloor(bbox.y0);
Toshihiro Shimizu 890ddd
	bbox.x1 = tceil(bbox.x1);
Toshihiro Shimizu 890ddd
	bbox.y1 = tceil(bbox.y1);
Toshihiro Shimizu 890ddd
	TDimension size(bbox.getLx(), bbox.getLy());
Toshihiro Shimizu 890ddd
	TRasterImageP app = TRasterImageUtils::vectorToFullColorImage(
Toshihiro Shimizu 890ddd
		m_image, sc, m_image->getPalette(), bbox.getP00(), size, 0, true);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	vector<trectd> rects;</trectd>
Toshihiro Shimizu 890ddd
	vector<tstroke> strokes;</tstroke>
Toshihiro Shimizu 890ddd
	TStroke stroke = getStrokeByRect(bbox);
Toshihiro Shimizu 890ddd
	strokes.push_back(stroke);
Toshihiro Shimizu 890ddd
	FullColorImageData *data = new FullColorImageData();
Toshihiro Shimizu 890ddd
	data->setData(app->getRaster(), m_image->getPalette(), dpix, dpiy, TDimension(), rects, strokes, strokes, TAffine());
Toshihiro Shimizu 890ddd
	return data;
Toshihiro Shimizu 890ddd
}