Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_toonz_raster_converter.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_level.h"
Toshihiro Shimizu 890ddd
#include "tropcm.h"
Toshihiro Shimizu 890ddd
#include "convert2tlv.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace TScriptBinding
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzRasterConverter::ToonzRasterConverter()
Toshihiro Shimizu 890ddd
	: m_flatSource(false)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzRasterConverter::~ToonzRasterConverter()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue ToonzRasterConverter::ctor(QScriptContext *context, QScriptEngine *engine)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return engine->newQObject(new ToonzRasterConverter(), QScriptEngine::AutoOwnership);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue ToonzRasterConverter::toString()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return "ToonzRasterConverter";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue ToonzRasterConverter::convert(QScriptContext *context, QScriptEngine *engine)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (context->argumentCount() != 1)
Toshihiro Shimizu 890ddd
		return context->throwError("Expected one argument (a raster Level or a raster Image)");
Toshihiro Shimizu 890ddd
	QScriptValue arg = context->argument(0);
Toshihiro Shimizu 890ddd
	Level *level = qscriptvalue_cast<level *="">(arg);</level>
Toshihiro Shimizu 890ddd
	Image *img = qscriptvalue_cast(arg);
Toshihiro Shimizu 890ddd
	QString type;
Toshihiro Shimizu 890ddd
	if (level) {
Toshihiro Shimizu 890ddd
		type = level->getType();
Toshihiro Shimizu 890ddd
		if (type != "Raster")
Toshihiro Shimizu 890ddd
			return context->throwError(tr("Can't convert a %1 level").arg(type));
Toshihiro Shimizu 890ddd
		if (level->getFrameCount() <= 0)
Toshihiro Shimizu 890ddd
			return context->throwError(tr("Can't convert a level with no frames"));
Toshihiro Shimizu 890ddd
	} else if (img) {
Toshihiro Shimizu 890ddd
		type = img->getType();
Toshihiro Shimizu 890ddd
		if (type != "Raster")
Toshihiro Shimizu 890ddd
			return context->throwError(tr("Can't convert a %1 image").arg(type));
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		return context->throwError(tr("Bad argument (%1): should be a raster Level or a raster Image").arg(arg.toString()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	RasterToToonzRasterConverter converter;
Toshihiro Shimizu 890ddd
	if (img) {
Toshihiro Shimizu 890ddd
		TRasterImageP ri = img->getImg();
Toshihiro Shimizu 890ddd
		TToonzImageP ti = converter.convert(ri);
Toshihiro Shimizu 890ddd
		ti->setPalette(converter.getPalette().getPointer());
Toshihiro Shimizu 890ddd
		return create(engine, new Image(ti));
Toshihiro Shimizu 890ddd
	} else
Toshihiro Shimizu 890ddd
		return QScriptValue();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace TScriptBinding