Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_image_builder.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_image.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Shinya Kitaoka d4642c
#include <cmath></cmath>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace TScriptBinding
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Transform::Transform()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Transform::Transform(const TAffine &aff)
Toshihiro Shimizu 890ddd
	: m_affine(aff)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Transform::~Transform()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Transform::ctor(QScriptContext *context, QScriptEngine *engine)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return create(engine, new Transform());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Transform::toString()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_affine.isIdentity())
Toshihiro Shimizu 890ddd
		return tr("Identity");
Toshihiro Shimizu 890ddd
	else if (m_affine.isTranslation())
Toshihiro Shimizu 890ddd
		return tr("Translation(%1,%2)").arg(m_affine.a13).arg(m_affine.a23);
Toshihiro Shimizu 890ddd
	else {
Toshihiro Shimizu 890ddd
		QString translationPart = "";
Toshihiro Shimizu 890ddd
		if (m_affine.a13 != 0.0 || m_affine.a23 != 0.0)
Toshihiro Shimizu 890ddd
			translationPart = tr("Translation(%1,%2)").arg(m_affine.a13).arg(m_affine.a23);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (fabs(m_affine.det() - 1.0) < 1.0e-8) {
Toshihiro Shimizu 890ddd
			double phi = atan2(m_affine.a12, m_affine.a11) * 180.0 / M_PI;
Toshihiro Shimizu 890ddd
			phi = -(0.001 * floor(1000 * phi + 0.5));
Toshihiro Shimizu 890ddd
			QString rotationPart = tr("Rotation(%1)").arg(phi);
Toshihiro Shimizu 890ddd
			if (translationPart == "")
Toshihiro Shimizu 890ddd
				return rotationPart;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				return translationPart + "*" + rotationPart;
Toshihiro Shimizu 890ddd
		} else if (m_affine.a12 == 0.0 && m_affine.a21 == 0.0) {
Toshihiro Shimizu 890ddd
			QString scalePart;
Toshihiro Shimizu 890ddd
			if (m_affine.a11 == m_affine.a22)
Toshihiro Shimizu 890ddd
				scalePart = tr("Scale(%1%)").arg(m_affine.a11 * 100);
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				scalePart = tr("Scale(%1%, %2%)").arg(m_affine.a11 * 100).arg(m_affine.a22 * 100);
Toshihiro Shimizu 890ddd
			if (translationPart == "")
Toshihiro Shimizu 890ddd
				return scalePart;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				return translationPart + "*" + scalePart;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			return tr("Transform(%1, %2, %3;  %4, %5, %6)")
Toshihiro Shimizu 890ddd
				.arg(m_affine.a11)
Toshihiro Shimizu 890ddd
				.arg(m_affine.a12)
Toshihiro Shimizu 890ddd
				.arg(m_affine.a13)
Toshihiro Shimizu 890ddd
				.arg(m_affine.a21)
Toshihiro Shimizu 890ddd
				.arg(m_affine.a22)
Toshihiro Shimizu 890ddd
				.arg(m_affine.a23);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Transform::translate(double x, double y)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return create(engine(), new Transform(TTranslation(x, y) * m_affine));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Transform::rotate(double degrees)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return create(engine(), new Transform(TRotation(degrees) * m_affine));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Transform::scale(double s)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return create(engine(), new Transform(TScale(s) * m_affine));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue Transform::scale(double sx, double sy)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return create(engine(), new Transform(TScale(sx, sy) * m_affine));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ImageBuilder::ImageBuilder()
Toshihiro Shimizu 890ddd
	: m_width(0), m_height(0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ImageBuilder::~ImageBuilder()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue ImageBuilder::ctor(QScriptContext *context, QScriptEngine *engine)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	ImageBuilder *imageBuilder = 0;
Toshihiro Shimizu 890ddd
	if (context->argumentCount() == 2 || context->argumentCount() == 3) {
Toshihiro Shimizu 890ddd
		if (!context->argument(0).isNumber() || !context->argument(1).isNumber())
Toshihiro Shimizu 890ddd
			return context->throwError("Bad arguments: expected width,height[,type]");
Toshihiro Shimizu 890ddd
		int width = (int)(context->argument(0).toNumber());
Toshihiro Shimizu 890ddd
		int height = (int)(context->argument(1).toNumber());
Toshihiro Shimizu 890ddd
		if (width <= 0 || height <= 0)
Toshihiro Shimizu 890ddd
			return context->throwError("Bad size");
Toshihiro Shimizu 890ddd
		QString type;
Toshihiro Shimizu 890ddd
		if (context->argumentCount() == 3) {
Toshihiro Shimizu 890ddd
			if (context->argument(2).isString())
Toshihiro Shimizu 890ddd
				type = context->argument(2).toString();
Toshihiro Shimizu 890ddd
			if (type != "Raster" && type != "ToonzRaster")
Toshihiro Shimizu 890ddd
				return context->throwError(tr("Bad argument (%1): should be 'Raster' or ToonzRaster'").arg(context->argument(2).toString()));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		imageBuilder = new ImageBuilder();
Toshihiro Shimizu 890ddd
		imageBuilder->m_width = width;
Toshihiro Shimizu 890ddd
		imageBuilder->m_height = height;
Toshihiro Shimizu 890ddd
		if (type == "Raster")
Toshihiro Shimizu 890ddd
			imageBuilder->m_img = new TRasterImage(TRaster32P(width, height));
Toshihiro Shimizu 890ddd
		else if (type == "ToonzRaster") {
Toshihiro Shimizu 890ddd
			imageBuilder->m_img = new TToonzImage(TRasterCM32P(width, height), TRect(0, 0, width, height));
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (context->argumentCount() != 0)
Toshihiro Shimizu 890ddd
			return context->throwError("Bad argument count. expected: width,height[,type]");
Toshihiro Shimizu 890ddd
		imageBuilder = new ImageBuilder();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	QScriptValue obj = engine->newQObject(imageBuilder, QScriptEngine::AutoOwnership, QScriptEngine::ExcludeSuperClassContents | QScriptEngine::ExcludeSuperClassMethods);
Toshihiro Shimizu 890ddd
	return obj;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue ImageBuilder::toString()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QString type = "Empty";
Toshihiro Shimizu 890ddd
	if (m_img) {
Toshihiro Shimizu 890ddd
		if (m_img->getType() == TImage::RASTER)
Toshihiro Shimizu 890ddd
			type = "Raster";
Toshihiro Shimizu 890ddd
		else if (m_img->getType() == TImage::TOONZ_RASTER)
Toshihiro Shimizu 890ddd
			type = "ToonzRaster";
Toshihiro Shimizu 890ddd
		else if (m_img->getType() == TImage::VECTOR)
Toshihiro Shimizu 890ddd
			type = "Vector";
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			type = "Bad";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return tr("ImageBuilder(%1 image)").arg(type);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue ImageBuilder::getImage()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return create(engine(), new Image(m_img));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue ImageBuilder::fill(const QString &colorName)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QColor color;
Toshihiro Shimizu 890ddd
	QScriptValue err = checkColor(context(), colorName, color);
Toshihiro Shimizu 890ddd
	if (err.isError())
Toshihiro Shimizu 890ddd
		return err;
Toshihiro Shimizu 890ddd
	TPixel32 pix(color.red(), color.green(), color.blue(), color.alpha());
Toshihiro Shimizu 890ddd
	if (m_img) {
Toshihiro Shimizu 890ddd
		if (m_img->getType() != TImage::RASTER)
Toshihiro Shimizu 890ddd
			context()->throwError("Can't fill a non-'Raster' image");
Toshihiro Shimizu 890ddd
		TRaster32P ras = m_img->raster();
Toshihiro Shimizu 890ddd
		if (ras)
Toshihiro Shimizu 890ddd
			ras->fill(pix);
Toshihiro Shimizu 890ddd
	} else if (m_width > 0 && m_height > 0) {
Toshihiro Shimizu 890ddd
		TRaster32P ras(m_width, m_height);
Toshihiro Shimizu 890ddd
		ras->fill(pix);
Toshihiro Shimizu 890ddd
		m_img = TRasterImageP(ras);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return context()->thisObject();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ImageBuilder::clear()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_img = TImageP();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString ImageBuilder::add(const TImageP &img, const TAffine &aff)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (fabs(aff.det()) < 0.001)
Toshihiro Shimizu 890ddd
		return "";
Toshihiro Shimizu 890ddd
	if (m_img && (m_img->getType() != img->getType()))
Toshihiro Shimizu 890ddd
		return "Image type mismatch";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!m_img && img->getType() != TImage::VECTOR && m_width > 0 && m_height > 0) {
Toshihiro Shimizu 890ddd
		TRasterP ras = img->raster()->create(m_width, m_height);
Toshihiro Shimizu 890ddd
		if (img->getType() == TImage::RASTER)
Toshihiro Shimizu 890ddd
			m_img = TRasterImageP(ras);
Toshihiro Shimizu 890ddd
		else if (img->getType() == TImage::TOONZ_RASTER) {
Toshihiro Shimizu 890ddd
			m_img = TToonzImageP(ras, ras->getBounds());
Toshihiro Shimizu 890ddd
			m_img->setPalette(img->getPalette());
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			return "Bad image type";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!m_img && aff.isIdentity()) {
Toshihiro Shimizu 890ddd
		m_img = img->cloneImage();
Toshihiro Shimizu 890ddd
	} else if (img->getType() == TImage::VECTOR) {
Toshihiro Shimizu 890ddd
		// vector image
Toshihiro Shimizu 890ddd
		if (!m_img) {
Toshihiro Shimizu 890ddd
			// transform
Toshihiro Shimizu 890ddd
			TVectorImageP vi = img->cloneImage();
Toshihiro Shimizu 890ddd
			vi->transform(aff);
Toshihiro Shimizu 890ddd
			m_img = vi;
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			// merge
Toshihiro Shimizu 890ddd
			TVectorImageP up = img;
Toshihiro Shimizu 890ddd
			TVectorImageP dn = m_img;
Toshihiro Shimizu 890ddd
			dn->mergeImage(up, aff);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		if (img->getType() != TImage::TOONZ_RASTER && img->getType() != TImage::RASTER) {
Toshihiro Shimizu 890ddd
			// this should not ever happen
Toshihiro Shimizu 890ddd
			return "Bad image type";
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		TRasterP up = img->raster();
Toshihiro Shimizu 890ddd
		if (!m_img) {
Toshihiro Shimizu 890ddd
			// create an empty bg
Toshihiro Shimizu 890ddd
			TRasterP ras = up->create();
Toshihiro Shimizu 890ddd
			ras->clear();
Toshihiro Shimizu 890ddd
			if (img->getType() == TImage::TOONZ_RASTER) {
Toshihiro Shimizu 890ddd
				TRasterCM32P rasCm = ras;
Toshihiro Shimizu 890ddd
				m_img = TToonzImageP(rasCm, TRect(0, 0, ras->getLx() - 1, ras->getLy() - 1));
Toshihiro Shimizu 890ddd
				m_img->setPalette(img->getPalette());
Toshihiro Shimizu 890ddd
			} else {
Toshihiro Shimizu 890ddd
				m_img = TRasterImageP(ras);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		TRasterP dn = m_img->raster();
Toshihiro Shimizu 890ddd
		if (aff.isTranslation() && aff.a13 == floor(aff.a13) && aff.a23 == floor(aff.a23)) {
Toshihiro Shimizu 890ddd
			// just a integer coord translation
Toshihiro Shimizu 890ddd
			TPoint delta = -up->getCenter() + dn->getCenter() + TPoint((int)aff.a13, (int)aff.a23);
Toshihiro Shimizu 890ddd
			TRop::over(dn, up, delta);
Toshihiro Shimizu 890ddd
		} else {
Toshihiro Shimizu 890ddd
			TAffine aff1 = TTranslation(dn->getCenterD()) * aff * TTranslation(-up->getCenterD());
Toshihiro Shimizu 890ddd
			TRop::over(dn, up, aff1, TRop::Mitchell);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
  TImageP srcImg = img->getImg();
Toshihiro Shimizu 890ddd
  if(srcImg->getType()==TImage::RASTER || srcImg->getType()==TImage::TOONZ_RASTER)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    TRasterP in = srcImg->raster();
Toshihiro Shimizu 890ddd
    TRasterP out = in->create();
Toshihiro Shimizu 890ddd
    TPointD center = in->getCenterD();
Toshihiro Shimizu 890ddd
    TAffine aff1 = TTranslation(center) * aff * TTranslation(-center);
Toshihiro Shimizu 890ddd
    TRop::resample(out,in,aff1,TRop::Mitchell);
Toshihiro Shimizu 890ddd
    m_img = TRasterImageP(out);
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  else if(srcImg->getType()==TImage::VECTOR)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    TVectorImageP vi = srcImg->cloneImage();
Toshihiro Shimizu 890ddd
    vi->transform(aff);
Toshihiro Shimizu 890ddd
    m_img = vi;
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  else
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
    return context()->throwError(tr("Bad image type"));
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
  return context()->thisObject();
Toshihiro Shimizu 890ddd
  */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue ImageBuilder::add(QScriptValue imgArg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Image *simg = 0;
Toshihiro Shimizu 890ddd
	QScriptValue err = checkImage(context(), imgArg, simg);
Toshihiro Shimizu 890ddd
	if (err.isError())
Toshihiro Shimizu 890ddd
		return err;
Toshihiro Shimizu 890ddd
	QString errStr = add(simg->getImg(), TAffine());
Toshihiro Shimizu 890ddd
	if (errStr != "")
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("%1 : %2").arg(errStr).arg(imgArg.toString()));
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return context()->thisObject();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue ImageBuilder::add(QScriptValue imgArg, QScriptValue transformationArg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	Image *simg = 0;
Toshihiro Shimizu 890ddd
	QScriptValue err = checkImage(context(), imgArg, simg);
Toshihiro Shimizu 890ddd
	if (err.isError())
Toshihiro Shimizu 890ddd
		return err;
Toshihiro Shimizu 890ddd
	Transform *transformation = qscriptvalue_cast<transform *="">(transformationArg);</transform>
Toshihiro Shimizu 890ddd
	if (!transformation) {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("Bad argument (%1): should be a Transformation").arg(transformationArg.toString()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	TAffine aff = transformation->getAffine();
Toshihiro Shimizu 890ddd
	QString errStr = add(simg->getImg(), aff);
Toshihiro Shimizu 890ddd
	if (errStr != "")
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("%1 : %2").arg(errStr).arg(imgArg.toString()));
Toshihiro Shimizu 890ddd
	else
Toshihiro Shimizu 890ddd
		return context()->thisObject();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace TScriptBinding