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