Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzLib includes
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/preferences.h"
Toshihiro Shimizu 890ddd
#include "toonz/sceneproperties.h"
Toshihiro Shimizu 890ddd
#include "toutputproperties.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzBase includes
Toshihiro Shimizu 890ddd
#include "tcli.h"
Toshihiro Shimizu 890ddd
#include "tenv.h"
Toshihiro Shimizu 890ddd
#include "tpluginmanager.h"
Toshihiro Shimizu 890ddd
#include "trasterfx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tstream.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "tiio_std.h"
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
#include "tnzimage.h"
Toshihiro Shimizu 890ddd
#include "tlevel.h"
Toshihiro Shimizu 890ddd
#include "tlevel_io.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "tropcm.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "tvectorrenderdata.h"
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
using namespace TCli;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
typedef ArgumentT<tfilepath> FilePathArgument;</tfilepath>
Toshihiro Shimizu 890ddd
typedef QualifierT<tfilepath> FilePathQualifier;</tfilepath>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define RENDER_LICENSE_NOT_FOUND 888
Toshihiro Shimizu 890ddd
shun_iwasawa 4dbfbc
const char *applicationVersion = "1.1";
Shinya Kitaoka 120a6e
const char *applicationName    = "OpenToonz";
Shinya Kitaoka 120a6e
const char *rootVarName        = "TOONZROOT";
Shinya Kitaoka 120a6e
const char *systemVarPrefix    = "TOONZ";
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void doesExist(const TFilePath &fp) {
Shinya Kitaoka 120a6e
  string msg;
Shinya Kitaoka 120a6e
  TFilePath path =
Shinya Kitaoka 120a6e
      fp.getParentDir() + (fp.getName() + "." + fp.getDottedType());
Shinya Kitaoka 120a6e
  if (TSystem::doesExistFileOrLevel(fp) ||
Shinya Kitaoka 120a6e
      TSystem::doesExistFileOrLevel(path)) {
Shinya Kitaoka 120a6e
    msg = "File " + fp.getLevelName() + " already exists:";
Shinya Kitaoka 120a6e
    cout << endl << msg << endl;
Shinya Kitaoka 120a6e
    char answer = ' ';
Shinya Kitaoka 120a6e
    while (answer != 'Y' && answer != 'N' && answer != 'y' && answer != 'n') {
Shinya Kitaoka 120a6e
      msg = "do you want to replace it? [Y/N] ";
Shinya Kitaoka 120a6e
      cout << msg;
Shinya Kitaoka 120a6e
      cin >> answer;
Shinya Kitaoka 120a6e
      if (answer == 'N' || answer == 'n') {
Shinya Kitaoka 120a6e
        msg = "Conversion aborted.";
Shinya Kitaoka 120a6e
        cout << endl << msg << endl;
Shinya Kitaoka 120a6e
        exit(1);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// Ritorna un vettore contenete i soli TFrameId corrispondenti al range inserito
Shinya Kitaoka 120a6e
// dall'utente
Shinya Kitaoka 120a6e
vector<tframeid> getFrameIds(const RangeQualifier &range,</tframeid>
Shinya Kitaoka 120a6e
                             const TLevelP &level) {
Shinya Kitaoka 120a6e
  string msg;
Shinya Kitaoka 120a6e
  TFrameId r0, r1, lastFrame;
Shinya Kitaoka 120a6e
  TLevel::Iterator begin = level->begin();
Shinya Kitaoka 120a6e
  TLevel::Iterator end   = level->end();
Shinya Kitaoka 120a6e
  end--;
Shinya Kitaoka 120a6e
  lastFrame = end->first;
Shinya Kitaoka 120a6e
  vector<tframeid> frames;</tframeid>
Shinya Kitaoka 120a6e
  if (range.isSelected()) {
Shinya Kitaoka 120a6e
    r0 = TFrameId(range.getFrom());
Shinya Kitaoka 120a6e
    r1 = TFrameId(range.getTo());
Shinya Kitaoka 120a6e
    if (r0 > r1) {
Shinya Kitaoka 120a6e
      TFrameId app = r0;
Shinya Kitaoka 120a6e
      r0           = r1;
Shinya Kitaoka 120a6e
      r1           = app;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    r0 = begin->first;
Shinya Kitaoka 120a6e
    r1 = end->first;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  // cerco il primo TFrameId
Shinya Kitaoka 120a6e
  TLevel::Iterator it = begin;
Shinya Kitaoka 120a6e
  if (r0 <= end->first)
Shinya Kitaoka 120a6e
    while (it->first < r0) ++it;
Shinya Kitaoka 120a6e
  while (it != level->end() && r1 >= it->first) {
Shinya Kitaoka 120a6e
    // Riempio il vettore fino all'ultimo TFrameId che mi serve
Shinya Kitaoka 120a6e
    frames.push_back(it->first);
Shinya Kitaoka 120a6e
    ++it;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return frames;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void convertFromCM(const TLevelReaderP &lr, const TPaletteP &plt,
Shinya Kitaoka 120a6e
                   const TLevelWriterP &lw, const vector<tframeid> &frames,</tframeid>
Shinya Kitaoka 120a6e
                   const TAffine &aff,
Shinya Kitaoka 120a6e
                   const TRop::ResampleFilterType &resType) {
Shinya Kitaoka 120a6e
  TDimension dim(
Shinya Kitaoka 120a6e
      0,
Shinya Kitaoka 120a6e
      0);  // Serve per controllare che non ci siano frame di diverse dimensioni
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)frames.size(); i++) {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TImageReaderP ir = lr->getFrameReader(frames[i]);
Shinya Kitaoka 120a6e
      TImageP img      = ir->load();
Shinya Kitaoka 120a6e
      TToonzImageP toonzImage(img);
Shinya Kitaoka 120a6e
      double xdpi, ydpi;
Shinya Kitaoka 120a6e
      toonzImage->getDpi(xdpi, ydpi);
Shinya Kitaoka 120a6e
      assert(toonzImage);
Shinya Kitaoka 120a6e
      if (toonzImage) {
Shinya Kitaoka 120a6e
        TRasterCM32P rasCMImage = toonzImage->getRaster();
Shinya Kitaoka 120a6e
        if (i == 0)
Shinya Kitaoka 120a6e
          dim = rasCMImage->getSize();
Shinya Kitaoka 120a6e
        else if (dim != rasCMImage->getSize()) {
Shinya Kitaoka 120a6e
          // dimensioni diverse dei frame
Shinya Kitaoka 120a6e
          string msg = "Cannot continue to convert: not valid level!";
Shinya Kitaoka 120a6e
          cout << msg << endl;
Shinya Kitaoka 120a6e
          exit(1);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        TRaster32P ras(
Shinya Kitaoka 120a6e
            convert(aff * convert(rasCMImage->getBounds())).getSize());
Shinya Kitaoka 120a6e
        if (!aff.isIdentity())
Shinya Kitaoka 120a6e
          TRop::resample(ras, rasCMImage, plt, aff, resType);
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          TRop::convert(ras, rasCMImage, plt);
Shinya Kitaoka 120a6e
        TRasterImageP rasImage(ras);
Shinya Kitaoka 120a6e
        rasImage->setDpi(xdpi, ydpi);
Shinya Kitaoka 120a6e
        TImageWriterP iw = lw->getFrameWriter(frames[i]);
Shinya Kitaoka 120a6e
        iw->save(rasImage);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      string msg = "Frame " + std::to_string(frames[i].getNumber()) +
Shinya Kitaoka 120a6e
                   ": conversion failed!";
Shinya Kitaoka 120a6e
      cout << msg << endl;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void convertFromVI(const TLevelReaderP &lr, const TPaletteP &plt,
Shinya Kitaoka 120a6e
                   const TLevelWriterP &lw, const vector<tframeid> &frames,</tframeid>
Shinya Kitaoka 120a6e
                   const TRop::ResampleFilterType &resType, int width) {
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  vector<tvectorimagep> images;</tvectorimagep>
Shinya Kitaoka 120a6e
  TRectD maxBbox;
Shinya Kitaoka 120a6e
  TAffine aff;
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)frames.size();
Shinya Kitaoka 120a6e
       i++) {  // trovo la bbox che possa contenere tutte le immagini
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TImageReaderP ir  = lr->getFrameReader(frames[i]);
Shinya Kitaoka 120a6e
      TVectorImageP img = ir->load();
Shinya Kitaoka 120a6e
      images.push_back(img);
Shinya Kitaoka 120a6e
      maxBbox += img->getBBox();
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      string msg = "Frame " + std::to_string(frames[i].getNumber()) +
Shinya Kitaoka 120a6e
                   ": conversion failed!";
Shinya Kitaoka 120a6e
      cout << msg << endl;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  maxBbox = maxBbox.enlarge(2);
Shinya Kitaoka 120a6e
  if (width)  // calcolo l'affine
Shinya Kitaoka 120a6e
    aff   = TScale((double)width / maxBbox.getLx());
Shinya Kitaoka 120a6e
  maxBbox = aff * maxBbox;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < (int)images.size(); i++) {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TVectorImageP vectorImage = images[i];
Shinya Kitaoka 120a6e
      assert(vectorImage);
Shinya Kitaoka 120a6e
      if (vectorImage) {
Shinya Kitaoka 120a6e
        // faccio il render dell'immagine
Shinya Kitaoka 120a6e
        vectorImage->transform(aff, true);
Shinya Kitaoka 120a6e
        const TVectorRenderData rd(TTranslation(-maxBbox.getP00()), TRect(),
Shinya Kitaoka 120a6e
                                   plt.getPointer(), 0, true, true);
Shinya Kitaoka 120a6e
        TOfflineGL *glContext = new TOfflineGL(convert(maxBbox).getSize());
Shinya Kitaoka 120a6e
        glContext->clear(TPixel32::Transparent);
Shinya Kitaoka 120a6e
        glContext->draw(vectorImage, rd);
Shinya Kitaoka 120a6e
        TRaster32P rasImage = (glContext->getRaster());
Shinya Kitaoka 120a6e
        TImageWriterP iw    = lw->getFrameWriter(frames[i]);
Shinya Kitaoka 120a6e
        iw->save(TRasterImageP(rasImage));
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      string msg = "Frame " + frames[i].expand() + ": conversion failed!";
Shinya Kitaoka 120a6e
      cout << msg << endl;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void convertFromFullRaster(const TLevelReaderP &lr, const TLevelWriterP &lw,
Shinya Kitaoka 120a6e
                           const vector<tframeid> &frames, const TAffine &aff,</tframeid>
Shinya Kitaoka 120a6e
                           const TRop::ResampleFilterType &resType) {
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)frames.size(); i++) {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TImageReaderP ir  = lr->getFrameReader(frames[i]);
Shinya Kitaoka 120a6e
      TRasterImageP img = ir->load();
Shinya Kitaoka 120a6e
      TRaster32P raster(convert(aff * img->getBBox()).getSize());
Shinya Kitaoka 120a6e
      if (!aff.isIdentity())
Shinya Kitaoka 120a6e
        TRop::resample(raster, img->getRaster(), aff, resType);
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        if ((TRaster32P)img->getRaster())
Shinya Kitaoka 120a6e
          raster = img->getRaster();
Shinya Kitaoka 120a6e
        else
Shinya Kitaoka 120a6e
          TRop::convert(raster, img->getRaster());
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      TImageWriterP iw = lw->getFrameWriter(frames[i]);
Shinya Kitaoka 120a6e
      iw->save(TRasterImageP(raster));
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      string msg = "Frame " + frames[i].expand() + ": conversion failed!";
Shinya Kitaoka 120a6e
      cout << msg << endl;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void convertFromFullRasterToCm(const TLevelReaderP &lr, const TLevelWriterP &lw,
Shinya Kitaoka 120a6e
                               const vector<tframeid> &frames,</tframeid>
Shinya Kitaoka 120a6e
                               const TAffine &aff,
Shinya Kitaoka 120a6e
                               const TRop::ResampleFilterType &resType) {
Shinya Kitaoka 120a6e
  TPalette *plt = new TPalette();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)frames.size(); i++) {
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TImageReaderP ir  = lr->getFrameReader(frames[i]);
Shinya Kitaoka 120a6e
      TRasterImageP img = ir->load();
Shinya Kitaoka 120a6e
      double dpix, dpiy;
Shinya Kitaoka 120a6e
      img->getDpi(dpix, dpiy);
Shinya Kitaoka 120a6e
      if (dpix == 0 && dpiy == 0)
Shinya Kitaoka 120a6e
        dpix = dpiy = Preferences::instance()->getDefLevelDpi();
Shinya Kitaoka 120a6e
      TRasterCM32P raster(convert(aff * img->getBBox()).getSize());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (!aff.isIdentity()) {
Shinya Kitaoka 120a6e
        TRaster32P raux(raster->getSize());
Shinya Kitaoka 120a6e
        TRop::resample(raux, img->getRaster(), aff, resType);
Shinya Kitaoka 120a6e
        TRop::convert(raster, raux);
Shinya Kitaoka 120a6e
      } else
Shinya Kitaoka 120a6e
        TRop::convert(raster, img->getRaster());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TImageWriterP iw = lw->getFrameWriter(frames[i]);
Shinya Kitaoka 120a6e
      TToonzImageP outimg(raster, raster->getBounds());
Shinya Kitaoka 120a6e
      outimg->setDpi(dpix, dpiy);
Shinya Kitaoka 120a6e
      outimg->setPalette(plt);
Shinya Kitaoka 120a6e
      iw->save(outimg);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      string msg = "Frame " + frames[i].expand() + ": conversion failed!";
Shinya Kitaoka 120a6e
      cout << msg << endl;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TFilePath pltPath = lw->getFilePath().withNoFrame().withType("tpl");
Shinya Kitaoka 120a6e
  if (TSystem::touchParentDir(pltPath)) {
Shinya Kitaoka 120a6e
    if (TSystem::doesExistFileOrLevel(pltPath))
Shinya Kitaoka 120a6e
      TSystem::removeFileOrLevel(pltPath);
Shinya Kitaoka 120a6e
    TOStream os(pltPath);
Shinya Kitaoka 120a6e
    os << plt;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void convert(const TFilePath &source, const TFilePath &dest,
Shinya Kitaoka 120a6e
             const RangeQualifier &range, const IntQualifier &width,
Shinya Kitaoka 120a6e
             TPropertyGroup *prop,
Shinya Kitaoka 120a6e
             const TRenderSettings::ResampleQuality &resQuality) {
Shinya Kitaoka 120a6e
  string msg;
Shinya Kitaoka 120a6e
  // Carico le informazione del livello
Shinya Kitaoka 120a6e
  TLevelReaderP lr(source);
Shinya Kitaoka 120a6e
  TLevelP level = lr->loadInfo();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Trovo i TFrameId corrispondenti al range
Shinya Kitaoka 120a6e
  vector<tframeid> frames = getFrameIds(range, level);</tframeid>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  doesExist(dest);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  msg = "Level loaded";
Shinya Kitaoka 120a6e
  cout << msg << endl;
Shinya Kitaoka 120a6e
  msg = "Conversion in progress: wait please...";
Shinya Kitaoka 120a6e
  cout << msg << endl;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TAffine aff;
Shinya Kitaoka 120a6e
  if (width.isSelected()) {
Shinya Kitaoka 120a6e
    // calcolo un affine per fare la resample
Shinya Kitaoka 120a6e
    int imgLx = lr->getImageInfo()->m_lx;
Shinya Kitaoka 120a6e
    aff       = TScale((double)width / (double)imgLx);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // setto il FilterResempleType giusto
Shinya Kitaoka 120a6e
  TRop::ResampleFilterType resType;
Shinya Kitaoka 120a6e
  if (resQuality == TRenderSettings::StandardResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Triangle;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::ImprovedResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Hann2;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::HighResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Hamming3;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Triangle_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Triangle;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Mitchell_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Mitchell;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Cubic5_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Cubic5;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Cubic75_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Cubic75;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Cubic1_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Cubic1;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Hann2_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Hann2;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Hann3_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Hann3;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Hamming2_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Hamming2;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Hamming3_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Hamming3;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Lanczos2_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Lanczos2;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Lanczos3_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Lanczos3;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Gauss_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Gauss;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::ClosestPixel_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::ClosestPixel;
Shinya Kitaoka 120a6e
  else if (resQuality == TRenderSettings::Bilinear_FilterResampleQuality)
Shinya Kitaoka 120a6e
    resType = TRop::Bilinear;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  string ext = source.getType();
Shinya Kitaoka 120a6e
  TLevelWriterP lw(dest, prop);
Shinya Kitaoka 120a6e
  if (ext != "tlv" && ext != "pli") {
Shinya Kitaoka 120a6e
    if (dest.getType() == "tlv")
Shinya Kitaoka 120a6e
      convertFromFullRasterToCm(lr, lw, frames, aff, resType);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      convertFromFullRaster(lr, lw, frames, aff, resType);
Shinya Kitaoka 120a6e
  } else if (ext == "tlv")  // ToonzImage
Shinya Kitaoka 120a6e
    convertFromCM(lr, level->getPalette(), lw, frames, aff, resType);
Shinya Kitaoka 120a6e
  else if (ext == "pli")  // VectorImage
Shinya Kitaoka 120a6e
    convertFromVI(lr, level->getPalette(), lw, frames, resType,
Shinya Kitaoka 120a6e
                  width.getValue());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int main(int argc, char *argv[]) {
Shinya Kitaoka 120a6e
  TEnv::setApplication(applicationName, applicationVersion);
Shinya Kitaoka 120a6e
  TEnv::setRootVarName(rootVarName);
Shinya Kitaoka 120a6e
  TEnv::setSystemVarPrefix(systemVarPrefix);
Shinya Kitaoka 120a6e
  TFilePath fp = TEnv::getStuffDir();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  string msg;
Shinya Kitaoka 120a6e
  // Inizializzo i qualificatori
Shinya Kitaoka 120a6e
  TCli::FilePathArgument srcName("srcName", "Source file");
Shinya Kitaoka 120a6e
  TCli::FilePathArgument dstName("dstName", "Target file");
Shinya Kitaoka 120a6e
  FilePathQualifier tnzName("-s sceneName", "Scene file");
Shinya Kitaoka 120a6e
  RangeQualifier range;
Shinya Kitaoka 120a6e
  IntQualifier width("-w width", "Image width");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  Usage usage(argv[0]);
Shinya Kitaoka 120a6e
  usage.add(srcName + dstName + width + tnzName + range);
Shinya Kitaoka 120a6e
  if (!usage.parse(argc, argv)) exit(1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    Tiio::defineStd();
Shinya Kitaoka 120a6e
    // TPluginManager::instance()->loadStandardPlugins();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TSystem::hasMainLoop(false);
Shinya Kitaoka 120a6e
    TPropertyGroup *prop = 0;
Shinya Kitaoka 120a6e
    initImageIo();
Shinya Kitaoka 120a6e
    TRenderSettings::ResampleQuality resQuality =
Shinya Kitaoka 120a6e
        TRenderSettings::StandardResampleQuality;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TFilePath dstFilePath = dstName.getValue();
Shinya Kitaoka 120a6e
    TFilePath srcFilePath = srcName.getValue();
Shinya Kitaoka 120a6e
    if (!TSystem::doesExistFileOrLevel(srcFilePath)) {
Shinya Kitaoka 120a6e
      msg = srcFilePath.getLevelName() + " level doesn't exist.";
Shinya Kitaoka 120a6e
      cout << endl << msg << endl;
Shinya Kitaoka 120a6e
      exit(1);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    msg = "Loading " + srcFilePath.getLevelName();
Shinya Kitaoka 120a6e
    cout << endl << msg << endl;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    string ext = dstFilePath.getType();
Shinya Kitaoka 120a6e
    // controllo che ci sia un'estensione
Shinya Kitaoka 120a6e
    if (ext == "") {
Shinya Kitaoka 120a6e
      ext = ::to_string(dstFilePath);
Shinya Kitaoka 120a6e
      if (ext == "") {
Shinya Kitaoka 120a6e
        msg = "Invalid extension!";
Shinya Kitaoka 120a6e
        cout << msg << endl;
Shinya Kitaoka 120a6e
        exit(1);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (dstFilePath.getParentDir()
Shinya Kitaoka 120a6e
            .isEmpty())  // ho specificato solo l'estensione
Shinya Kitaoka 120a6e
      dstFilePath =
Shinya Kitaoka 120a6e
          srcFilePath.getParentDir() + (srcFilePath.getName() + "." + ext);
Shinya Kitaoka 120a6e
    if (tnzName.isSelected()) {
Shinya Kitaoka 120a6e
      // Devo prendermi i settaggi degli "output setting" dalla scena!
Shinya Kitaoka 120a6e
      TFilePath tnzFilePath = tnzName.getValue();
Shinya Kitaoka 120a6e
      if (tnzFilePath.getType() != "tnz") {
Shinya Kitaoka 120a6e
        msg = "Invalid scene file: conversion terminated!";
Shinya Kitaoka 120a6e
        cout << msg << endl;
Shinya Kitaoka 120a6e
        exit(1);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (!TSystem::doesExistFileOrLevel(tnzFilePath)) return false;
Shinya Kitaoka 120a6e
      ToonzScene *scene = new ToonzScene();
Shinya Kitaoka 120a6e
      try {
Shinya Kitaoka 120a6e
        scene->loadTnzFile(tnzFilePath);
Shinya Kitaoka 120a6e
      } catch (...) {
Shinya Kitaoka 120a6e
        string msg;
Shinya Kitaoka 120a6e
        msg = "There were problems loading the scene " +
Shinya Kitaoka 120a6e
              ::to_string(srcFilePath) + ".\n Some files may be missing.";
Shinya Kitaoka 120a6e
        cout << msg << endl;
Shinya Kitaoka 120a6e
        // return false;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (scene) {
Shinya Kitaoka 120a6e
        resQuality = scene->getProperties()
Shinya Kitaoka 120a6e
                         ->getOutputProperties()
Shinya Kitaoka 120a6e
                         ->getRenderSettings()
Shinya Kitaoka 120a6e
                         .m_quality;
Shinya Kitaoka 120a6e
        prop = scene->getProperties()
Shinya Kitaoka 120a6e
                   ->getOutputProperties()
Shinya Kitaoka 120a6e
                   ->getFileFormatProperties(ext);
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        msg = "Invalid scene file: conversion terminated!";
Shinya Kitaoka 120a6e
        cout << msg << endl;
Shinya Kitaoka 120a6e
        exit(1);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (ext != "3gp" && ext != "pli") {
Shinya Kitaoka 120a6e
      // assert(ext!="3gp" && ext!="pli" && ext!="tlv");
Shinya Kitaoka 120a6e
      convert(srcFilePath, dstFilePath, range, width, prop, resQuality);
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      msg = "Cannot convert to ." + ext + " format.";
Shinya Kitaoka 120a6e
      cout << msg << endl;
Shinya Kitaoka 120a6e
      exit(1);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } catch (TException &e) {
Shinya Kitaoka 120a6e
    msg = "Untrapped exception: " + ::to_string(e.getMessage());
Shinya Kitaoka 120a6e
    cout << msg << endl;
Shinya Kitaoka 120a6e
    return -1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  msg = "Conversion terminated!";
Shinya Kitaoka 120a6e
  cout << endl << msg << endl;
Shinya Kitaoka 120a6e
  return 0;
Toshihiro Shimizu 890ddd
}