Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_rasterizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_image.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_level.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcamera.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
#include "tvectorrenderdata.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzimageutils.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace TScriptBinding
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Rasterizer::Rasterizer()
Toshihiro Shimizu 890ddd
	: m_colorMapped(false), m_xres(720), m_yres(576), m_dpi(72), m_antialiasing(true)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Rasterizer::~Rasterizer()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Rasterizer::toString()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return "Rasterizer";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Rasterizer::ctor(QScriptContext *context, QScriptEngine *engine)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return create(engine, new Rasterizer());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TToonzImageP vectorToToonzRaster(const TVectorImageP &vi, const TDimension &size, const TAffine &aff, const TPointD &dpi)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
  TScale sc(dpi.x/Stage::inch, dpi.y/Stage::inch);
Toshihiro Shimizu 890ddd
  TRectD bbox = sc*vi->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
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TToonzImageP ti = ToonzImageUtils::vectorToToonzImage(
Toshihiro Shimizu 890ddd
		vi, aff, vi->getPalette(), TPointD(0, 0), size, 0, true);
Toshihiro Shimizu 890ddd
	ti->setPalette(vi->getPalette()); // e' necessario?
Toshihiro Shimizu 890ddd
	return ti;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TImageP renderVectorImage(TOfflineGL *glContext, const TVectorRenderData &rd, const TPointD &dpi, const TImageP &img, const TPixel32 &color)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	glContext->clear(color);
Toshihiro Shimizu 890ddd
	glContext->draw(img, rd);
Toshihiro Shimizu 890ddd
	TRasterImageP rimg(glContext->getRaster());
Toshihiro Shimizu 890ddd
	rimg->setDpi(dpi.x, dpi.y);
Toshihiro Shimizu 890ddd
	return rimg;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void setFrame(QScriptEngine *engine, QScriptValue &level, const TFrameId &fid, const TImageP &drawing)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QScriptValueList args;
Toshihiro Shimizu 890ddd
	args << QString::fromStdString(fid.expand()) << Wrapper::create(engine, new Image(drawing.getPointer()));
Toshihiro Shimizu 890ddd
	level.property("setFrame").call(level, args);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Q_INVOKABLE QScriptValue Rasterizer::rasterize(QScriptValue arg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Image *img = qscriptvalue_cast(arg);
Toshihiro Shimizu 890ddd
	Level *level = qscriptvalue_cast<level *="">(arg);</level>
Toshihiro Shimizu 890ddd
	TPalette *palette = 0;
Toshihiro Shimizu 890ddd
	if (img) {
Toshihiro Shimizu 890ddd
		if (img->getType() != "Vector")
Toshihiro Shimizu 890ddd
			return context()->throwError(tr("Expected a vector image: %1").arg(arg.toString()));
Toshihiro Shimizu 890ddd
		palette = !!img->getImg() ? img->getImg()->getPalette() : 0;
Toshihiro Shimizu 890ddd
	} else if (level) {
Toshihiro Shimizu 890ddd
		if (level->getType() != "Vector")
Toshihiro Shimizu 890ddd
			return context()->throwError(tr("Expected a vector level: %1").arg(arg.toString()));
Toshihiro Shimizu 890ddd
		palette = level->getSimpleLevel() ? level->getSimpleLevel()->getPalette() : 0;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("Argument must be a vector level or image : ").arg(arg.toString()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!palette) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("%1 has no palette").arg(arg.toString()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDimension res(m_xres, m_yres);
Toshihiro Shimizu 890ddd
	TCamera camera;
Toshihiro Shimizu 890ddd
	camera.setRes(res);
Toshihiro Shimizu 890ddd
	camera.setSize(TDimensionD(m_xres / m_dpi, m_yres / m_dpi));
Toshihiro Shimizu 890ddd
	TPointD dpi = camera.getDpi();
Toshihiro Shimizu 890ddd
	TAffine aff = camera.getStageToCameraRef();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	QScriptValue result;
Toshihiro Shimizu 890ddd
	int n = 0;
Toshihiro Shimizu 890ddd
	TXshSimpleLevel *sl = 0;
Toshihiro Shimizu 890ddd
	if (level) {
Toshihiro Shimizu 890ddd
		result = create(engine(), new Level());
Toshihiro Shimizu 890ddd
		n = level->getFrameCount();
Toshihiro Shimizu 890ddd
		sl = level->getSimpleLevel();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_colorMapped) {
Toshihiro Shimizu 890ddd
		// vector -> toonz image
Toshihiro Shimizu 890ddd
		if (img) {
Toshihiro Shimizu 890ddd
			TImageP outImg = vectorToToonzRaster(img->getImg(), res, aff, dpi);
Toshihiro Shimizu 890ddd
			result = create(engine(), new Image(outImg));
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			for (int i = 0; i < n; i++) {
Toshihiro Shimizu 890ddd
				TFrameId fid = sl->index2fid(i);
Toshihiro Shimizu 890ddd
				TImageP drawing = sl->getFrame(fid, false);
Toshihiro Shimizu 890ddd
				TImageP outImg = vectorToToonzRaster(drawing, res, aff, dpi);
Toshihiro Shimizu 890ddd
				setFrame(engine(), result, fid, outImg);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		// vector -> full color
Toshihiro Shimizu 890ddd
		TPixel32 bgColor = TPixel32::White;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TOfflineGL *glContext = new TOfflineGL(res);
Toshihiro Shimizu 890ddd
		glContext->makeCurrent();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TVectorRenderData rd(TVectorRenderData::ProductionSettings(),
Toshihiro Shimizu 890ddd
							 aff, TRect(), palette);
Toshihiro Shimizu 890ddd
		rd.m_antiAliasing = m_antialiasing;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (img) {
Toshihiro Shimizu 890ddd
			TImageP outImg = renderVectorImage(glContext, rd, dpi, img->getImg(), bgColor);
Toshihiro Shimizu 890ddd
			result = create(engine(), new Image(outImg));
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			for (int i = 0; i < n; i++) {
Toshihiro Shimizu 890ddd
				TFrameId fid = sl->index2fid(i);
Toshihiro Shimizu 890ddd
				TImageP drawing = sl->getFrame(fid, false);
Toshihiro Shimizu 890ddd
				glContext->clear(TPixel32::White);
Toshihiro Shimizu 890ddd
				TImageP outImg = renderVectorImage(glContext, rd, dpi, drawing, bgColor);
Toshihiro Shimizu 890ddd
				setFrame(engine(), result, fid, outImg);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		delete glContext;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return result;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool Rasterizer::getColorMapped() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_colorMapped;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Rasterizer::setColorMapped(bool v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_colorMapped = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int Rasterizer::getXRes() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_xres;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Rasterizer::setXRes(int v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_xres = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int Rasterizer::getYRes() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_yres;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Rasterizer::setYRes(int v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_yres = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double Rasterizer::getDpi() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_dpi;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void Rasterizer::setDpi(double v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_dpi = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace TScriptBinding