Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_rasterizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_image.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_level.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcamera.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
#include "tvectorrenderdata.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzimageutils.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace TScriptBinding {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Rasterizer::Rasterizer()
Shinya Kitaoka 120a6e
    : m_colorMapped(false)
Shinya Kitaoka 120a6e
    , m_xres(720)
Shinya Kitaoka 120a6e
    , m_yres(576)
Shinya Kitaoka 120a6e
    , m_dpi(72)
Shinya Kitaoka 120a6e
    , m_antialiasing(true) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
Rasterizer::~Rasterizer() {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
QScriptValue Rasterizer::toString() { return "Rasterizer"; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
QScriptValue Rasterizer::ctor(QScriptContext *context, QScriptEngine *engine) {
Shinya Kitaoka 120a6e
  return create(engine, new Rasterizer());
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
TToonzImageP vectorToToonzRaster(const TVectorImageP &vi,
Shinya Kitaoka 120a6e
                                 const TDimension &size, const TAffine &aff,
Shinya Kitaoka 120a6e
                                 const TPointD &dpi) {
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
TScale sc(dpi.x/Stage::inch, dpi.y/Stage::inch);
Shinya Kitaoka 120a6e
TRectD bbox = sc*vi->getBBox();
Shinya Kitaoka 120a6e
bbox.x0 = tfloor(bbox.x0);
Shinya Kitaoka 120a6e
bbox.y0 = tfloor(bbox.y0);
Shinya Kitaoka 120a6e
bbox.x1 = tceil(bbox.x1);
Shinya Kitaoka 120a6e
bbox.y1 = tceil(bbox.y1);
Shinya Kitaoka 120a6e
TDimension size(bbox.getLx(), bbox.getLy());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TToonzImageP ti = ToonzImageUtils::vectorToToonzImage(
Shinya Kitaoka 120a6e
      vi, aff, vi->getPalette(), TPointD(0, 0), size, 0, true);
Shinya Kitaoka 120a6e
  ti->setPalette(vi->getPalette());  // e' necessario?
Shinya Kitaoka 120a6e
  return ti;
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
TImageP renderVectorImage(TOfflineGL *glContext, const TVectorRenderData &rd,
Shinya Kitaoka 120a6e
                          const TPointD &dpi, const TImageP &img,
Shinya Kitaoka 120a6e
                          const TPixel32 &color) {
Shinya Kitaoka 120a6e
  glContext->clear(color);
Shinya Kitaoka 120a6e
  glContext->draw(img, rd);
Shinya Kitaoka 120a6e
  TRasterImageP rimg(glContext->getRaster());
Shinya Kitaoka 120a6e
  rimg->setDpi(dpi.x, dpi.y);
Shinya Kitaoka 120a6e
  return rimg;
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void setFrame(QScriptEngine *engine, QScriptValue &level, const TFrameId &fid,
Shinya Kitaoka 120a6e
              const TImageP &drawing) {
Shinya Kitaoka 120a6e
  QScriptValueList args;
Shinya Kitaoka 120a6e
  args << QString::fromStdString(fid.expand())
Shinya Kitaoka 120a6e
       << Wrapper::create(engine, new Image(drawing.getPointer()));
Shinya Kitaoka 120a6e
  level.property("setFrame").call(level, args);
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
Q_INVOKABLE QScriptValue Rasterizer::rasterize(QScriptValue arg) {
Shinya Kitaoka 120a6e
  Image *img        = qscriptvalue_cast(arg);
Shinya Kitaoka 120a6e
  Level *level      = qscriptvalue_cast<level *="">(arg);</level>
Shinya Kitaoka 120a6e
  TPalette *palette = 0;
Shinya Kitaoka 120a6e
  if (img) {
Shinya Kitaoka 120a6e
    if (img->getType() != "Vector")
Shinya Kitaoka 120a6e
      return context()->throwError(
Shinya Kitaoka 120a6e
          tr("Expected a vector image: %1").arg(arg.toString()));
Shinya Kitaoka 120a6e
    palette = !!img->getImg() ? img->getImg()->getPalette() : 0;
Shinya Kitaoka 120a6e
  } else if (level) {
Shinya Kitaoka 120a6e
    if (level->getType() != "Vector")
Shinya Kitaoka 120a6e
      return context()->throwError(
Shinya Kitaoka 120a6e
          tr("Expected a vector level: %1").arg(arg.toString()));
Shinya Kitaoka 120a6e
    palette =
Shinya Kitaoka 120a6e
        level->getSimpleLevel() ? level->getSimpleLevel()->getPalette() : 0;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    return context()->throwError(
Shinya Kitaoka 120a6e
        tr("Argument must be a vector level or image : ").arg(arg.toString()));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (!palette) {
Shinya Kitaoka 120a6e
    return context()->throwError(tr("%1 has no palette").arg(arg.toString()));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TDimension res(m_xres, m_yres);
Shinya Kitaoka 120a6e
  TCamera camera;
Shinya Kitaoka 120a6e
  camera.setRes(res);
Shinya Kitaoka 120a6e
  camera.setSize(TDimensionD(m_xres / m_dpi, m_yres / m_dpi));
Shinya Kitaoka 120a6e
  TPointD dpi = camera.getDpi();
Shinya Kitaoka 120a6e
  TAffine aff = camera.getStageToCameraRef();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QScriptValue result;
Shinya Kitaoka 120a6e
  int n               = 0;
Shinya Kitaoka 120a6e
  TXshSimpleLevel *sl = 0;
Shinya Kitaoka 120a6e
  if (level) {
Shinya Kitaoka 120a6e
    result = create(engine(), new Level());
Shinya Kitaoka 120a6e
    n      = level->getFrameCount();
Shinya Kitaoka 120a6e
    sl     = level->getSimpleLevel();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (m_colorMapped) {
Shinya Kitaoka 120a6e
    // vector -> toonz image
Shinya Kitaoka 120a6e
    if (img) {
Shinya Kitaoka 120a6e
      TImageP outImg = vectorToToonzRaster(img->getImg(), res, aff, dpi);
Shinya Kitaoka 120a6e
      result         = create(engine(), new Image(outImg));
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      for (int i = 0; i < n; i++) {
Shinya Kitaoka 120a6e
        TFrameId fid    = sl->index2fid(i);
Shinya Kitaoka 120a6e
        TImageP drawing = sl->getFrame(fid, false);
Shinya Kitaoka 120a6e
        TImageP outImg  = vectorToToonzRaster(drawing, res, aff, dpi);
Shinya Kitaoka 120a6e
        setFrame(engine(), result, fid, outImg);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    // vector -> full color
Shinya Kitaoka 120a6e
    TPixel32 bgColor = TPixel32::White;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TOfflineGL *glContext = new TOfflineGL(res);
Shinya Kitaoka 120a6e
    glContext->makeCurrent();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TVectorRenderData rd(TVectorRenderData::ProductionSettings(), aff, TRect(),
Shinya Kitaoka 120a6e
                         palette);
Shinya Kitaoka 120a6e
    rd.m_antiAliasing = m_antialiasing;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (img) {
Shinya Kitaoka 120a6e
      TImageP outImg =
Shinya Kitaoka 120a6e
          renderVectorImage(glContext, rd, dpi, img->getImg(), bgColor);
Shinya Kitaoka 120a6e
      result = create(engine(), new Image(outImg));
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      for (int i = 0; i < n; i++) {
Shinya Kitaoka 120a6e
        TFrameId fid    = sl->index2fid(i);
Shinya Kitaoka 120a6e
        TImageP drawing = sl->getFrame(fid, false);
Shinya Kitaoka 120a6e
        glContext->clear(TPixel32::White);
Shinya Kitaoka 120a6e
        TImageP outImg =
Shinya Kitaoka 120a6e
            renderVectorImage(glContext, rd, dpi, drawing, bgColor);
Shinya Kitaoka 120a6e
        setFrame(engine(), result, fid, outImg);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    delete glContext;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return result;
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
bool Rasterizer::getColorMapped() const { return m_colorMapped; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void Rasterizer::setColorMapped(bool v) { m_colorMapped = v; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
int Rasterizer::getXRes() const { return m_xres; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void Rasterizer::setXRes(int v) { m_xres = v; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
int Rasterizer::getYRes() const { return m_yres; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void Rasterizer::setYRes(int v) { m_yres = v; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
double Rasterizer::getDpi() const { return m_dpi; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void Rasterizer::setDpi(double v) { m_dpi = v; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
}  // namespace TScriptBinding