Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonzqt/rasterimagedata.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/strokesdata.h"
Toshihiro Shimizu 890ddd
#include "tpaletteutil.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcenterlinevectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/ttileset.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzimageutils.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/sceneproperties.h"
Toshihiro Shimizu 890ddd
#include "toonz/fullcolorpalette.h"
Toshihiro Shimizu 890ddd
Campbell Barton 40cabe
#include <memory></memory>
Campbell Barton 40cabe
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
TVectorImageP vectorize(const TImageP &source, const TRectD &rect,
Shinya Kitaoka 120a6e
                        const VectorizerConfiguration &config,
Shinya Kitaoka 120a6e
                        TAffine transform) {
Shinya Kitaoka 120a6e
  VectorizerCore vc;
Shinya Kitaoka 120a6e
  TVectorImageP vi =
Shinya Kitaoka 120a6e
      vc.vectorize(source.getPointer(), config, source->getPalette());
Shinya Kitaoka 120a6e
  assert(vi);
Shinya Kitaoka 120a6e
  vi->setPalette(source->getPalette());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double dpiX, dpiY;
Shinya Kitaoka 120a6e
  TToonzImageP ti(source);
Shinya Kitaoka 120a6e
  TRasterImageP ri(source);
Shinya Kitaoka 120a6e
  if (ti)
Shinya Kitaoka 120a6e
    ti->getDpi(dpiX, dpiY);
Shinya Kitaoka 120a6e
  else if (ri)
Shinya Kitaoka 120a6e
    ri->getDpi(dpiX, dpiY);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return vi;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TScale sc(dpiX / Stage::inch, dpiY / Stage::inch);
Shinya Kitaoka 120a6e
  TTranslation tr(rect.getP00());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)vi->getStrokeCount(); i++) {
Shinya Kitaoka 120a6e
    TStroke *stroke = vi->getStroke(i);
Shinya Kitaoka 120a6e
    stroke->transform((sc.inv() * tr) * transform, true);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return vi;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namspace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// RasterImageData
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
RasterImageData::RasterImageData()
Shinya Kitaoka 120a6e
    : m_dpiX(0), m_dpiY(0), m_transformation(), m_dim() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
RasterImageData::~RasterImageData() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// ToonzImageData
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzImageData::ToonzImageData()
Shinya Kitaoka 120a6e
    : RasterImageData(), m_copiedRaster(0), m_palette(new TPalette()) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ToonzImageData::ToonzImageData(const ToonzImageData &src)
Shinya Kitaoka 120a6e
    : m_copiedRaster(src.m_copiedRaster)
Shinya Kitaoka 120a6e
    , m_palette(src.m_palette)
Shinya Kitaoka 120a6e
    , m_usedStyles(src.m_usedStyles) {
Shinya Kitaoka 120a6e
  m_dpiX            = src.m_dpiX;
Shinya Kitaoka 120a6e
  m_dpiY            = src.m_dpiY;
Shinya Kitaoka 120a6e
  m_rects           = src.m_rects;
Shinya Kitaoka 120a6e
  m_strokes         = src.m_strokes;
Shinya Kitaoka 120a6e
  m_transformation  = src.m_transformation;
Shinya Kitaoka 120a6e
  m_originalStrokes = src.m_originalStrokes;
Shinya Kitaoka 120a6e
  m_dim             = src.m_dim;
Shinya Kitaoka 120a6e
  assert(m_palette);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
ToonzImageData::~ToonzImageData() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToonzImageData::setData(const TRasterP &copiedRaster,
Shinya Kitaoka 120a6e
                             const TPaletteP &palette, double dpiX, double dpiY,
Shinya Kitaoka 120a6e
                             const TDimension &dim,
Shinya Kitaoka 120a6e
                             const std::vector<trectd> &rects,</trectd>
Shinya Kitaoka 120a6e
                             const std::vector<tstroke> &strokes,</tstroke>
Shinya Kitaoka 120a6e
                             const std::vector<tstroke> &originalStrokes,</tstroke>
Shinya Kitaoka 120a6e
                             const TAffine &transformation) {
Shinya Kitaoka 120a6e
  m_copiedRaster    = copiedRaster;
Shinya Kitaoka 120a6e
  m_palette         = palette;
Shinya Kitaoka 120a6e
  m_dpiX            = dpiX;
Shinya Kitaoka 120a6e
  m_dpiY            = dpiY;
Shinya Kitaoka 120a6e
  m_rects           = rects;
Shinya Kitaoka 120a6e
  m_strokes         = strokes;
Shinya Kitaoka 120a6e
  m_transformation  = transformation;
Shinya Kitaoka 120a6e
  m_originalStrokes = originalStrokes;
Shinya Kitaoka 120a6e
  m_dim             = dim;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*-- 使用されているStyleの一覧を作る --*/
Shinya Kitaoka 120a6e
  TToonzImageP ti(m_copiedRaster, m_copiedRaster->getBounds());
Shinya Kitaoka 120a6e
  ToonzImageUtils::getUsedStyles(m_usedStyles, ti);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void ToonzImageData::getData(TRasterP &copiedRaster, double &dpiX, double &dpiY,
Shinya Kitaoka 120a6e
                             std::vector<trectd> &rects,</trectd>
Shinya Kitaoka 120a6e
                             std::vector<tstroke> &strokes,</tstroke>
Shinya Kitaoka 120a6e
                             std::vector<tstroke> &originalStrokes,</tstroke>
Shinya Kitaoka 120a6e
                             TAffine &transformation,
Shinya Kitaoka 120a6e
                             TPalette *targetPalette) const {
Shinya Kitaoka 120a6e
  if (!m_copiedRaster || (m_rects.empty() && m_strokes.empty())) return;
Shinya Kitaoka 120a6e
  copiedRaster = m_copiedRaster->clone();
Shinya Kitaoka 120a6e
  dpiX         = m_dpiX;
Shinya Kitaoka 120a6e
  dpiY         = m_dpiY;
Shinya Kitaoka 120a6e
  assert(m_palette);
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)m_rects.size(); i++) rects.push_back(m_rects[i]);
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)m_strokes.size(); i++) strokes.push_back(m_strokes[i]);
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)m_originalStrokes.size(); i++)
Shinya Kitaoka 120a6e
    originalStrokes.push_back(m_originalStrokes[i]);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  transformation     = m_transformation;
Shinya Kitaoka 120a6e
  TRasterCM32P cmRas = copiedRaster;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!targetPalette) targetPalette = new TPalette();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!cmRas) return;
Shinya Kitaoka 120a6e
  std::set<int> usedStyles(m_usedStyles);</int>
Shinya Kitaoka 120a6e
  TToonzImageP ti(cmRas, cmRas->getBounds());
Shinya Kitaoka 120a6e
  if (usedStyles.size() == 0) ToonzImageUtils::getUsedStyles(usedStyles, ti);
Shinya Kitaoka 120a6e
  std::map<int, int=""> indexTable;</int,>
Shinya Kitaoka 120a6e
  mergePalette(targetPalette, indexTable, m_palette, usedStyles);
Shinya Kitaoka 120a6e
  ToonzImageUtils::scrambleStyles(ti, indexTable);
Shinya Kitaoka 120a6e
  ti->setPalette(m_palette.getPointer());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StrokesData *ToonzImageData::toStrokesData(ToonzScene *scene) const {
Shinya Kitaoka 120a6e
  assert(scene);
Shinya Kitaoka 120a6e
  TRectD rect;
Shinya Kitaoka 120a6e
  if (!m_rects.empty())
Shinya Kitaoka 120a6e
    rect = m_rects[0];
Shinya Kitaoka 120a6e
  else if (!m_strokes.empty())
Shinya Kitaoka 120a6e
    rect = m_strokes[0].getBBox();
Shinya Kitaoka 120a6e
  unsigned int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < m_rects.size(); i++) rect += m_rects[i];
Shinya Kitaoka 120a6e
  for (i = 0; i < m_strokes.size(); i++) rect += m_strokes[i].getBBox();
Shinya Kitaoka 120a6e
  TToonzImageP image(m_copiedRaster, m_copiedRaster->getBounds());
Shinya Kitaoka 120a6e
  image->setPalette(m_palette.getPointer());
Shinya Kitaoka 120a6e
  image->setDpi(m_dpiX, m_dpiY);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const VectorizerParameters &vParams =
Shinya Kitaoka 120a6e
      *scene->getProperties()->getVectorizerParameters();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  CenterlineConfiguration cConf = vParams.getCenterlineConfiguration(0.0);
Shinya Kitaoka 120a6e
  NewOutlineConfiguration oConf = vParams.getOutlineConfiguration(0.0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const VectorizerConfiguration &config =
Shinya Kitaoka 120a6e
      vParams.m_isOutline ? static_cast<const &="" vectorizerconfiguration="">(oConf)</const>
Shinya Kitaoka 120a6e
                          : static_cast<const &="" vectorizerconfiguration="">(cConf);</const>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TVectorImageP vi = vectorize(image, rect, config, m_transformation);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  StrokesData *sd = new StrokesData();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  std::set<int> indexes;</int>
Shinya Kitaoka 120a6e
  for (i = 0; i < vi->getStrokeCount(); i++) indexes.insert(i);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  sd->setImage(vi, indexes);
Shinya Kitaoka 120a6e
  return sd;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int ToonzImageData::getMemorySize() const {
Shinya Kitaoka 120a6e
  int i, size = 0;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)m_strokes.size(); i++)
Shinya Kitaoka 120a6e
    size += m_strokes[i].getControlPointCount() * sizeof(TThickPoint) + 100;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)m_originalStrokes.size(); i++)
Shinya Kitaoka 120a6e
    size +=
Shinya Kitaoka 120a6e
        m_originalStrokes[i].getControlPointCount() * sizeof(TThickPoint) + 100;
Shinya Kitaoka 120a6e
  return size + sizeof(*(m_copiedRaster.getPointer())) +
Shinya Kitaoka 120a6e
         sizeof(*(m_palette.getPointer())) + sizeof(*this);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
// FullColorImageData
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FullColorImageData::FullColorImageData()
Shinya Kitaoka 120a6e
    : RasterImageData(), m_copiedRaster(0), m_palette(new TPalette()) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FullColorImageData::FullColorImageData(const FullColorImageData &src)
Shinya Kitaoka 120a6e
    : m_copiedRaster(src.m_copiedRaster), m_palette(src.m_palette) {
Shinya Kitaoka 120a6e
  m_dpiX            = src.m_dpiX;
Shinya Kitaoka 120a6e
  m_dpiY            = src.m_dpiY;
Shinya Kitaoka 120a6e
  m_rects           = src.m_rects;
Shinya Kitaoka 120a6e
  m_strokes         = src.m_strokes;
Shinya Kitaoka 120a6e
  m_transformation  = src.m_transformation;
Shinya Kitaoka 120a6e
  m_originalStrokes = src.m_originalStrokes;
Shinya Kitaoka 120a6e
  m_dim             = src.m_dim;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
FullColorImageData::~FullColorImageData() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorImageData::setData(const TRasterP &copiedRaster,
Shinya Kitaoka 120a6e
                                 const TPaletteP &palette, double dpiX,
Shinya Kitaoka 120a6e
                                 double dpiY, const TDimension &dim,
Shinya Kitaoka 120a6e
                                 const std::vector<trectd> &rects,</trectd>
Shinya Kitaoka 120a6e
                                 const std::vector<tstroke> &strokes,</tstroke>
Shinya Kitaoka 120a6e
                                 const std::vector<tstroke> &originalStrokes,</tstroke>
Shinya Kitaoka 120a6e
                                 const TAffine &transformation) {
Shinya Kitaoka 120a6e
  m_copiedRaster    = copiedRaster;
Shinya Kitaoka 120a6e
  m_palette         = palette;
Shinya Kitaoka 120a6e
  m_dpiX            = dpiX;
Shinya Kitaoka 120a6e
  m_dpiY            = dpiY;
Shinya Kitaoka 120a6e
  m_rects           = rects;
Shinya Kitaoka 120a6e
  m_strokes         = strokes;
Shinya Kitaoka 120a6e
  m_originalStrokes = originalStrokes;
Shinya Kitaoka 120a6e
  m_transformation  = transformation;
Shinya Kitaoka 120a6e
  m_dim             = dim;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullColorImageData::getData(TRasterP &copiedRaster, double &dpiX,
Shinya Kitaoka 120a6e
                                 double &dpiY, std::vector<trectd> &rects,</trectd>
Shinya Kitaoka 120a6e
                                 std::vector<tstroke> &strokes,</tstroke>
Shinya Kitaoka 120a6e
                                 std::vector<tstroke> &originalStrokes,</tstroke>
Shinya Kitaoka 120a6e
                                 TAffine &transformation,
Shinya Kitaoka 120a6e
                                 TPalette *targetPalette) const {
Shinya Kitaoka 120a6e
  if (!m_copiedRaster || (m_rects.empty() && m_strokes.empty())) return;
Shinya Kitaoka 120a6e
  copiedRaster = m_copiedRaster->clone();
Shinya Kitaoka 120a6e
  dpiX         = m_dpiX;
Shinya Kitaoka 120a6e
  dpiY         = m_dpiY;
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)m_rects.size(); i++) rects.push_back(m_rects[i]);
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)m_strokes.size(); i++) strokes.push_back(m_strokes[i]);
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)m_originalStrokes.size(); i++)
Shinya Kitaoka 120a6e
    originalStrokes.push_back(m_originalStrokes[i]);
Shinya Kitaoka 120a6e
  transformation = m_transformation;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRasterP ras = copiedRaster;
Shinya Kitaoka 120a6e
  if (!ras) return;
Shinya Kitaoka 120a6e
  if (!m_palette) return;
Shinya Kitaoka 120a6e
  if (!targetPalette) targetPalette = new TPalette();
Shinya Kitaoka 120a6e
  std::set<int> usedStyles;</int>
Shinya Kitaoka 120a6e
  TRasterImageP ri(ras);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < m_palette->getPageCount(); i++) {
Shinya Kitaoka 120a6e
    TPalette::Page *page = m_palette->getPage(i);
Shinya Kitaoka 120a6e
    int j;
Shinya Kitaoka 120a6e
    for (j = 0; j < page->getStyleCount(); j++)
Shinya Kitaoka 120a6e
      usedStyles.insert(page->getStyleId(j));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  std::map<int, int=""> indexTable;</int,>
Shinya Kitaoka 120a6e
  mergePalette(targetPalette, indexTable, m_palette, usedStyles);
Shinya Kitaoka 120a6e
  ri->setPalette(m_palette.getPointer());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
StrokesData *FullColorImageData::toStrokesData(ToonzScene *scene) const {
Shinya Kitaoka 120a6e
  assert(scene);
Shinya Kitaoka 120a6e
  TRectD rect;
Shinya Kitaoka 120a6e
  if (!m_rects.empty())
Shinya Kitaoka 120a6e
    rect = m_rects[0];
Shinya Kitaoka 120a6e
  else if (!m_strokes.empty())
Shinya Kitaoka 120a6e
    rect = m_strokes[0].getBBox();
Shinya Kitaoka 120a6e
  unsigned int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < m_rects.size(); i++) rect += m_rects[i];
Shinya Kitaoka 120a6e
  for (i = 0; i < m_strokes.size(); i++) rect += m_strokes[i].getBBox();
Shinya Kitaoka 120a6e
  TRasterImageP image(m_copiedRaster);
Shinya Kitaoka 120a6e
  image->setPalette(FullColorPalette::instance()->getPalette(scene));
Shinya Kitaoka 120a6e
  image->setDpi(m_dpiX, m_dpiY);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const VectorizerParameters *vParams =
Shinya Kitaoka 120a6e
      scene->getProperties()->getVectorizerParameters();
Shinya Kitaoka 120a6e
  assert(vParams);
Shinya Kitaoka 120a6e
Shinya Kitaoka 2a7129
  std::unique_ptr<vectorizerconfiguration> config(</vectorizerconfiguration>
Shinya Kitaoka 120a6e
      vParams->getCurrentConfiguration(0.0));
Shinya Kitaoka 120a6e
  TVectorImageP vi = vectorize(image, rect, *config, m_transformation);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  StrokesData *sd = new StrokesData();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::set<int> indexes;</int>
Shinya Kitaoka 120a6e
  for (i = 0; i < vi->getStrokeCount(); i++) indexes.insert(i);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  sd->setImage(vi, indexes);
Shinya Kitaoka 120a6e
  return sd;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int FullColorImageData::getMemorySize() const {
Shinya Kitaoka 120a6e
  int i, size = 0;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)m_strokes.size(); i++)
Shinya Kitaoka 120a6e
    size += m_strokes[i].getControlPointCount() * sizeof(TThickPoint) + 100;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)m_originalStrokes.size(); i++)
Shinya Kitaoka 120a6e
    size +=
Shinya Kitaoka 120a6e
        m_originalStrokes[i].getControlPointCount() * sizeof(TThickPoint) + 100;
Shinya Kitaoka 120a6e
  return size + sizeof(*(m_copiedRaster.getPointer())) +
Shinya Kitaoka 120a6e
         sizeof(*(m_palette.getPointer())) + sizeof(*this);
Toshihiro Shimizu 890ddd
}