Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_centerline_vectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_level.h"
Toshihiro Shimizu 890ddd
#include <qscriptengine></qscriptengine>
Toshihiro Shimizu 890ddd
#include "toonz/tcenterlinevectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
#include "toonz/Naa2TlvConverter.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace TScriptBinding {
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
CenterlineVectorizer::CenterlineVectorizer() {
Shinya Kitaoka 120a6e
  m_parameters = new CenterlineConfiguration();
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
CenterlineVectorizer::~CenterlineVectorizer() { delete m_parameters; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
QScriptValue CenterlineVectorizer::toString() {
Shinya Kitaoka 120a6e
  return "Centerline Vectorizer";
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
QScriptValue CenterlineVectorizer::ctor(QScriptContext *context,
Shinya Kitaoka 120a6e
                                        QScriptEngine *engine) {
Shinya Kitaoka 120a6e
  return create(engine, new CenterlineVectorizer());
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
QScriptValue CenterlineVectorizer::vectorizeImage(const TImageP &src,
Shinya Kitaoka 120a6e
                                                  TPalette *palette) {
Shinya Kitaoka 120a6e
  VectorizerCore vc;
Shinya Kitaoka 120a6e
  TAffine dpiAff;
Shinya Kitaoka 120a6e
  double factor = Stage::inch;
Shinya Kitaoka 120a6e
  double dpix = factor / 72, dpiy = factor / 72;
Shinya Kitaoka 120a6e
  TPointD center;
Shinya Kitaoka 120a6e
  if (TRasterImageP ri = src) {
Shinya Kitaoka 120a6e
    ri->getDpi(dpix, dpiy);
Shinya Kitaoka 120a6e
    center = ri->getRaster()->getCenterD();
Shinya Kitaoka 120a6e
  } else if (TToonzImageP ti = src) {
Shinya Kitaoka 120a6e
    ti->getDpi(dpix, dpiy);
Shinya Kitaoka 120a6e
    center = ti->getRaster()->getCenterD();
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    return context()->throwError(QObject::tr("Vectorization failed"));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (dpix != 0.0 && dpiy != 0.0) dpiAff = TScale(factor / dpix, factor / dpiy);
Shinya Kitaoka 120a6e
  factor                                 = norm(dpiAff * TPointD(1, 0));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_parameters->m_affine     = dpiAff * TTranslation(-center);
Shinya Kitaoka 120a6e
  m_parameters->m_thickScale = factor;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  palette->addRef();  // if there are no other references the vectorize() method
Shinya Kitaoka 120a6e
                      // below can destroy the palette
Shinya Kitaoka 120a6e
                      // BEFORE assigning it to the vector image
Shinya Kitaoka 120a6e
  TVectorImageP vi = vc.vectorize(src, *m_parameters, palette);
Shinya Kitaoka 120a6e
  vi->setPalette(palette);
Shinya Kitaoka 120a6e
  palette->release();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return engine()->newQObject(new Image(vi), QScriptEngine::AutoOwnership);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue CenterlineVectorizer::vectorize(QScriptValue arg) {
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
  TPalette *palette = 0;
Shinya Kitaoka 120a6e
  if (level) {
Shinya Kitaoka 120a6e
    type = level->getType();
Shinya Kitaoka 120a6e
    if (type != "Raster" && type != "ToonzRaster")
Shinya Kitaoka 120a6e
      return context()->throwError(tr("Can't vectorize a %1 level").arg(type));
Shinya Kitaoka 120a6e
    if (level->getFrameCount() <= 0)
Shinya Kitaoka 120a6e
      return context()->throwError(
Shinya Kitaoka 120a6e
          tr("Can't vectorize a level with no frames"));
Shinya Kitaoka 120a6e
    palette = level->getSimpleLevel()->getPalette();
Shinya Kitaoka 120a6e
  } else if (img) {
Shinya Kitaoka 120a6e
    type = img->getType();
Shinya Kitaoka 120a6e
    if (type != "Raster" && type != "ToonzRaster")
Shinya Kitaoka 120a6e
      return context()->throwError(tr("Can't vectorize a %1 image").arg(type));
Shinya Kitaoka 120a6e
    if (TToonzImageP ti = img->getImg()) {
Shinya Kitaoka 120a6e
      palette = ti->getPalette();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    return context()->throwError(
Shinya Kitaoka 120a6e
        tr("Bad argument (%1): should be an Image or a Level")
Shinya Kitaoka 120a6e
            .arg(arg.toString()));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (palette == 0) palette = new TPalette();
Shinya Kitaoka 120a6e
  if (img) {
Shinya Kitaoka 120a6e
    return vectorizeImage(img->getImg(), palette);
Shinya Kitaoka 120a6e
  } else if (level) {
Shinya Kitaoka 120a6e
    QScriptValue newLevel = create(engine(), new Level());
Shinya Kitaoka 120a6e
    QList<tframeid> fids;</tframeid>
Shinya Kitaoka 120a6e
    level->getFrameIds(fids);
Shinya Kitaoka 120a6e
    foreach (TFrameId fid, fids) {
Shinya Kitaoka 120a6e
      TImageP srcImg = level->getImg(fid);
Shinya Kitaoka 120a6e
      if (srcImg && (srcImg->getType() == TImage::RASTER ||
Shinya Kitaoka 120a6e
                     srcImg->getType() == TImage::TOONZ_RASTER)) {
Shinya Kitaoka 120a6e
        QScriptValue newFrame = vectorizeImage(srcImg, palette);
Shinya Kitaoka 120a6e
        if (newFrame.isError()) {
Shinya Kitaoka 120a6e
          return newFrame;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        QScriptValueList args;
Shinya Kitaoka 120a6e
        args << QString::fromStdString(fid.expand()) << newFrame;
Shinya Kitaoka 120a6e
        newLevel.property("setFrame").call(newLevel, args);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    return newLevel;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    // should never happen
Shinya Kitaoka 120a6e
    return QScriptValue();
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int CenterlineVectorizer::getThreshold() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_threshold / 25;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CenterlineVectorizer::setThreshold(int v) {
Shinya Kitaoka 120a6e
  m_parameters->m_threshold = v * 25;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int CenterlineVectorizer::getAccuracy() const {
Shinya Kitaoka 120a6e
  return 10 - m_parameters->m_penalty;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CenterlineVectorizer::setAccuracy(int v) {
Shinya Kitaoka 120a6e
  m_parameters->m_penalty = 10 - v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int CenterlineVectorizer::getDespeckling() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_despeckling / 2;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CenterlineVectorizer::setDespeckling(int v) {
Shinya Kitaoka 120a6e
  m_parameters->m_despeckling = v * 2;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double CenterlineVectorizer::getMaxThickness() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_maxThickness * 2.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CenterlineVectorizer::setMaxThickness(double v) {
Shinya Kitaoka 120a6e
  m_parameters->m_maxThickness = v / 2.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double CenterlineVectorizer::getThicknessCalibration() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_thicknessRatio;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CenterlineVectorizer::setThicknessCalibration(double v) {
Shinya Kitaoka 120a6e
  m_parameters->m_thicknessRatio = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool CenterlineVectorizer::getPreservePaintedAreas() const {
Shinya Kitaoka 120a6e
  return !m_parameters->m_leaveUnpainted;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CenterlineVectorizer::setPreservePaintedAreas(bool v) {
Shinya Kitaoka 120a6e
  m_parameters->m_leaveUnpainted = !v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool CenterlineVectorizer::getAddBorder() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_makeFrame;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void CenterlineVectorizer::setAddBorder(bool v) {
Shinya Kitaoka 120a6e
  m_parameters->m_makeFrame = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool CenterlineVectorizer::getEir() const { return m_parameters->m_naaSource; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void CenterlineVectorizer::setEir(bool v) { m_parameters->m_naaSource = v; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
}  // namespace TScriptBinding