Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_outline_vectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_level.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcenterlinevectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace TScriptBinding {
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
OutlineVectorizer::OutlineVectorizer() {
Shinya Kitaoka 120a6e
  m_parameters = new NewOutlineConfiguration();
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
OutlineVectorizer::~OutlineVectorizer() { delete m_parameters; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
QScriptValue OutlineVectorizer::toString() { return "Outline Vectorizer"; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
QScriptValue OutlineVectorizer::ctor(QScriptContext *context,
Shinya Kitaoka 120a6e
                                     QScriptEngine *engine) {
Shinya Kitaoka 120a6e
  return create(engine, new OutlineVectorizer());
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
shun-iwasawa 27b0cf
static QScriptValue vectorizeImage(QScriptContext *context,
shun-iwasawa 27b0cf
                                   QScriptEngine *engine, const TImageP &src,
shun-iwasawa 27b0cf
                                   TPalette *palette,
Campbell Barton b3bd84
                                   NewOutlineConfiguration *parameters) {
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
  parameters->m_affine     = dpiAff * TTranslation(-center);
Shinya Kitaoka 120a6e
  parameters->m_thickScale = factor;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TVectorImageP vi = vc.vectorize(src, *parameters, palette);
Shinya Kitaoka 120a6e
  vi->setPalette(palette);
Shinya Kitaoka 120a6e
  return engine->newQObject(new Image(vi), QScriptEngine::AutoOwnership);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QScriptValue OutlineVectorizer::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(context(), engine(), img->getImg(), palette,
Shinya Kitaoka 120a6e
                          m_parameters);
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 =
Shinya Kitaoka 120a6e
            vectorizeImage(context(), engine(), srcImg, palette, m_parameters);
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 OutlineVectorizer::getAccuracy() const {
Shinya Kitaoka 120a6e
  return (5.0 - m_parameters->m_mergeTol) * 2.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void OutlineVectorizer::setAccuracy(int v) {
Shinya Kitaoka 120a6e
  m_parameters->m_mergeTol = 5.0 - v * 0.5;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int OutlineVectorizer::getDespeckling() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_despeckling;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void OutlineVectorizer::setDespeckling(int v) {
Shinya Kitaoka 120a6e
  m_parameters->m_despeckling = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool OutlineVectorizer::getPreservePaintedAreas() const {
Shinya Kitaoka 120a6e
  return !m_parameters->m_leaveUnpainted;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void OutlineVectorizer::setPreservePaintedAreas(bool v) {
Shinya Kitaoka 120a6e
  m_parameters->m_leaveUnpainted = !v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double OutlineVectorizer::getCornerAdherence() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_adherenceTol * 100.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void OutlineVectorizer::setCornerAdherence(double v) {
Shinya Kitaoka 120a6e
  m_parameters->m_adherenceTol = 0.01 * v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double OutlineVectorizer::getCornerAngle() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_angleTol * 180.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void OutlineVectorizer::setCornerAngle(double v) {
Shinya Kitaoka 120a6e
  m_parameters->m_angleTol = v / 180.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double OutlineVectorizer::getCornerCurveRadius() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_relativeTol * 100;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void OutlineVectorizer::setCornerCurveRadius(double v) {
Shinya Kitaoka 120a6e
  m_parameters->m_relativeTol = 0.01 * v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int OutlineVectorizer::getMaxColors() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_maxColors;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void OutlineVectorizer::setMaxColors(int v) { m_parameters->m_maxColors = v; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
QString OutlineVectorizer::getTransparentColor() const {
Shinya Kitaoka 120a6e
  TPixel32 c = m_parameters->m_transparentColor;
Shinya Kitaoka 120a6e
  QColor color(c.r, c.g, c.b, c.m);
Shinya Kitaoka 120a6e
  return color.name();
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void OutlineVectorizer::setTransparentColor(const QString &colorName) {
Shinya Kitaoka 120a6e
  QColor color;
Shinya Kitaoka 120a6e
  color.setNamedColor(colorName);
Shinya Kitaoka 120a6e
  if (color.isValid()) {
Shinya Kitaoka 120a6e
    m_parameters->m_transparentColor =
Shinya Kitaoka 120a6e
        TPixel32(color.red(), color.green(), color.blue(), color.alpha());
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    context()->throwError(tr("Invalid color : ").arg(colorName));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int OutlineVectorizer::getToneThreshold() const {
Shinya Kitaoka 120a6e
  return m_parameters->m_toneTol;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void OutlineVectorizer::setToneThreshold(int v) { m_parameters->m_toneTol = v; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
}  // namespace TScriptBinding