Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzQt includes
Toshihiro Shimizu 890ddd
#include "toonzqt/menubarcommand.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/viewcommandids.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/imageutils.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/dvdialog.h"
Toshihiro Shimizu 890ddd
#include "toonzqt/gutil.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzLib includes
Toshihiro Shimizu 890ddd
#include "toonz/preferences.h"
Toshihiro Shimizu 890ddd
#include "toonz/namebuilder.h"
Toshihiro Shimizu 890ddd
#include "toonz/toonzscene.h"
Toshihiro Shimizu 890ddd
#include "toonz/tproject.h"
Toshihiro Shimizu 890ddd
#include "toonz/Naa2TlvConverter.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#include "avicodecrestrictions.h"
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzCore includes
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "timage_io.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 "tvectorrenderdata.h"
Toshihiro Shimizu 890ddd
#include "tofflinegl.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "timageinfo.h"
Toshihiro Shimizu 890ddd
#include "tfiletype.h"
Toshihiro Shimizu 890ddd
#include "tfilepath.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "trasterimage.h"
Toshihiro Shimizu 890ddd
#include "tvectorimage.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// TnzQt includes
Toshihiro Shimizu 890ddd
#include <qapplication></qapplication>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// boost includes
Toshihiro Shimizu 890ddd
#include <boost iterator="" transform_iterator.hpp=""></boost>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**********************************************************************************
Toshihiro Shimizu 890ddd
//    Local namespace  stuff
Toshihiro Shimizu 890ddd
//**********************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void addOverlappedRegions(TRegion *reg, std::vector<tfilledregioninf> ®Inf) {</tfilledregioninf>
Shinya Kitaoka 120a6e
  regInf.push_back(TFilledRegionInf(reg->getId(), reg->getStyle()));
Shinya Kitaoka 120a6e
  UINT regNum = reg->getSubregionCount();
Shinya Kitaoka 120a6e
  for (UINT i = 0; i < regNum; i++)
Shinya Kitaoka 120a6e
    addOverlappedRegions(reg->getSubregion(i), regInf);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void addRegionsInArea(TRegion *reg, std::vector<tfilledregioninf> ®s,</tfilledregioninf>
Shinya Kitaoka 120a6e
                      const TRectD &area) {
Shinya Kitaoka 120a6e
  if (area.contains(reg->getBBox()))
Shinya Kitaoka 120a6e
    regs.push_back(TFilledRegionInf(reg->getId(), reg->getStyle()));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (reg->getBBox().overlaps(area)) {
Shinya Kitaoka 120a6e
    UINT regNum = reg->getSubregionCount();
Shinya Kitaoka 120a6e
    for (UINT i = 0; i < regNum; i++)
Shinya Kitaoka 120a6e
      addRegionsInArea(reg->getSubregion(i), regs, area);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void getFrameIds(TFrameId from, TFrameId to, const TLevelP &level,
Shinya Kitaoka 120a6e
                 std::vector<tframeid> &frames) {</tframeid>
Shinya Kitaoka 120a6e
  struct locals {
Shinya Kitaoka 120a6e
    static inline TFrameId getFrame(const TLevel::Table::value_type &pair) {
Shinya Kitaoka 120a6e
      return pair.first;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  };  // locals
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (from.isEmptyFrame()) from = TFrameId(-(std::numeric_limits<int>::max)());</int>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (to.isEmptyFrame()) to = TFrameId((std::numeric_limits<int>::max)());</int>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (from > to) tswap(from, to);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  const TLevel::Table &table = *level->getTable();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TLevel::Table::const_iterator lBegin = table.lower_bound(from),
Shinya Kitaoka 120a6e
                                lEnd   = table.upper_bound(to);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(frames.empty());
Shinya Kitaoka 120a6e
  frames.insert(frames.end(),
Shinya Kitaoka 120a6e
                boost::make_transform_iterator(lBegin, locals::getFrame),
Shinya Kitaoka 120a6e
                boost::make_transform_iterator(lEnd, locals::getFrame));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**********************************************************************************
Toshihiro Shimizu 890ddd
//    ImageUtils namespace  stuff
Toshihiro Shimizu 890ddd
//**********************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace ImageUtils {
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// todo: spostare da qualche altra parte. rendere accessibile a tutti.
Shinya Kitaoka 120a6e
// utilizzare
Toshihiro Shimizu 890ddd
// anche nella libreria dovunque si usi direttamente "_files"
Campbell Barton ccd505
static TFilePath getResourceFolder(const TFilePath &scenePath) {
Shinya Kitaoka 120a6e
  return scenePath.getParentDir() + (scenePath.getName() + "_files");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Campbell Barton ccd505
static void copyScene(const TFilePath &dst, const TFilePath &src) {
Shinya Kitaoka 120a6e
  TSystem::copyFile(dst, src);
Shinya Kitaoka 120a6e
  if (TProjectManager::instance()->isTabModeEnabled())
Shinya Kitaoka 120a6e
    TSystem::copyDir(getResourceFolder(dst), getResourceFolder(src));
Shinya Kitaoka 120a6e
  TFilePath srcIcon = ToonzScene::getIconPath(src);
Shinya Kitaoka 120a6e
  if (TFileStatus(srcIcon).doesExist()) {
Shinya Kitaoka 120a6e
    TFilePath dstIcon = ToonzScene::getIconPath(dst);
Shinya Kitaoka 120a6e
    TSystem::copyFile(dstIcon, srcIcon);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TFilePath duplicate(const TFilePath &levelPath) {
Shinya Kitaoka 120a6e
  if (levelPath == TFilePath()) return TFilePath();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TSystem::doesExistFileOrLevel(levelPath)) {
Shinya Kitaoka 120a6e
    DVGui::warning(
Shinya Kitaoka 120a6e
        QObject::tr("It is not possible to find the %1 level.")
Shinya Kitaoka 120a6e
            .arg(QString::fromStdWString(levelPath.getWideString())));
Shinya Kitaoka 120a6e
    return TFilePath();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  NameBuilder *nameBuilder =
Shinya Kitaoka 120a6e
      NameBuilder::getBuilder(::to_wstring(levelPath.getName()));
Shinya Kitaoka 120a6e
  std::wstring levelNameOut;
Shinya Kitaoka 120a6e
  do
Shinya Kitaoka 120a6e
    levelNameOut = nameBuilder->getNext();
Shinya Kitaoka 120a6e
  while (TSystem::doesExistFileOrLevel(levelPath.withName(levelNameOut)));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TFilePath levelPathOut = levelPath.withName(levelNameOut);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    if (levelPath.getType() == "tnz")
Shinya Kitaoka 120a6e
      copyScene(levelPathOut, levelPath);
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      TSystem::copyFileOrLevel_throw(levelPathOut, levelPath);
Shinya Kitaoka 120a6e
      if (levelPath.getType() == "tlv") {
Shinya Kitaoka 120a6e
        TFilePath pltPath = levelPath.withType("tpl");
Shinya Kitaoka 120a6e
        if (TSystem::doesExistFileOrLevel(pltPath))
Shinya Kitaoka 120a6e
          TSystem::copyFileOrLevel_throw(levelPathOut.withType("tpl"), pltPath);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    QString msg =
Shinya Kitaoka 120a6e
        QObject::tr("There was an error copying %1").arg(toQString(levelPath));
Shinya Kitaoka 120a6e
    DVGui::error(msg);
Shinya Kitaoka 120a6e
    return TFilePath();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  //  QString msg = QString("Copied ") +
Shinya Kitaoka 120a6e
  //                QString::fromStdWString(levelPath.withoutParentDir().getWideString())
Shinya Kitaoka 120a6e
  //                +
Shinya Kitaoka 120a6e
  //                QString(" to " +
Shinya Kitaoka 120a6e
  //                QString::fromStdWString(levelPathOut.withoutParentDir().getWideString()));
Shinya Kitaoka 120a6e
  //  DVGui::info(msg);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return levelPathOut;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void premultiply(const TFilePath &levelPath) {
Shinya Kitaoka 120a6e
  if (levelPath == TFilePath()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (!TSystem::doesExistFileOrLevel(levelPath)) {
Shinya Kitaoka 120a6e
    DVGui::warning(
Shinya Kitaoka 120a6e
        QObject::tr("It is not possible to find the level %1")
Shinya Kitaoka 120a6e
            .arg(QString::fromStdWString(levelPath.getWideString())));
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TFileType::Type type = TFileType::getInfo(levelPath);
Shinya Kitaoka 120a6e
  if (type == TFileType::CMAPPED_LEVEL) {
Shinya Kitaoka 120a6e
    DVGui::warning(QObject::tr("Cannot premultiply the selected file."));
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (type == TFileType::VECTOR_LEVEL || type == TFileType::VECTOR_IMAGE) {
Shinya Kitaoka 120a6e
    DVGui::warning(QObject::tr("Cannot premultiply a vector-based level."));
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (type != TFileType::RASTER_LEVEL && type != TFileType::RASTER_IMAGE) {
Shinya Kitaoka 120a6e
    DVGui::info(QObject::tr("Cannot premultiply the selected file."));
Toshihiro Shimizu 890ddd
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    TLevelReaderP lr = TLevelReaderP(levelPath);
Shinya Kitaoka 120a6e
    if (!lr) return;
Shinya Kitaoka 120a6e
    TLevelP level = lr->loadInfo();
Shinya Kitaoka 120a6e
    if (!level || level->getFrameCount() == 0) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    bool isMovie = type == TFileType::RASTER_LEVEL;
Shinya Kitaoka 120a6e
    /*
Shinya Kitaoka 120a6e
if (!isMovie && lr->getImageInfo()->m_samplePerPixel!=4)
Shinya Kitaoka 120a6e
{
Shinya Kitaoka 120a6e
QMessageBox::information(0, QString("ERROR"), QString("Only rgbm images can be
Shinya Kitaoka 120a6e
premultiplied!"));
Shinya Kitaoka 120a6e
return;
Shinya Kitaoka 120a6e
}
Shinya Kitaoka 120a6e
*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TLevelWriterP lw;
Shinya Kitaoka 120a6e
    if (!isMovie) {
Shinya Kitaoka 120a6e
      lw = TLevelWriterP(levelPath);
Shinya Kitaoka 120a6e
      if (!lw) return;
Toshihiro Shimizu 890ddd
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
Shinya Kitaoka 120a6e
    qApp->processEvents();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    int count           = 0;
Shinya Kitaoka 120a6e
    TLevel::Iterator it = level->begin();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    for (; it != level->end(); ++it) {
Shinya Kitaoka 120a6e
      TImageReaderP ir = lr->getFrameReader(it->first);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TRasterImageP rimg = (TRasterImageP)ir->load();
Shinya Kitaoka 120a6e
      if (!rimg) continue;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TRop::premultiply(rimg->getRaster());
Shinya Kitaoka 120a6e
      ir = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (isMovie)
Shinya Kitaoka 120a6e
        level->setFrame(it->first, rimg);
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        TImageWriterP iw = lw->getFrameWriter(it->first);
Shinya Kitaoka 120a6e
        iw->save(levelPath.withFrame(it->first), rimg);
Shinya Kitaoka 120a6e
        iw = 0;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    lr = TLevelReaderP();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (isMovie) {
Shinya Kitaoka 120a6e
      TSystem::deleteFile(levelPath);
Shinya Kitaoka 120a6e
      lw = TLevelWriterP(levelPath);
Shinya Kitaoka 120a6e
      if (!lw) {
Shinya Kitaoka 120a6e
        QApplication::restoreOverrideCursor();
Shinya Kitaoka 120a6e
        return;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      lw->save(level);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  } catch (...) {
Shinya Kitaoka 120a6e
    DVGui::warning(QObject::tr("Cannot premultiply the selected file."));
Shinya Kitaoka 120a6e
    QApplication::restoreOverrideCursor();
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  QApplication::restoreOverrideCursor();
Shinya Kitaoka 120a6e
  QString msg = QObject::tr("Level %1 premultiplied.")
Shinya Kitaoka 120a6e
                    .arg(QString::fromStdString(levelPath.getLevelName()));
Shinya Kitaoka 120a6e
  DVGui::info(msg);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void getFillingInformationOverlappingArea(const TVectorImageP &vi,
Shinya Kitaoka 120a6e
                                          std::vector<tfilledregioninf> ®Inf,</tfilledregioninf>
Shinya Kitaoka 120a6e
                                          const TRectD &area1,
Shinya Kitaoka 120a6e
                                          const TRectD &area2) {
Shinya Kitaoka 120a6e
  if (!vi->isComputedRegionAlmostOnce()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  assert(area1 != TRectD() || area2 != TRectD());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  vi->findRegions();
Shinya Kitaoka 120a6e
  UINT regNum = vi->getRegionCount();
Shinya Kitaoka 120a6e
  TRegion *reg;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (UINT i = 0; i < regNum; i++) {
Shinya Kitaoka 120a6e
    reg = vi->getRegion(i);
Shinya Kitaoka 120a6e
    if (reg->getBBox().overlaps(area1) || reg->getBBox().overlaps(area2))
Shinya Kitaoka 120a6e
      addOverlappedRegions(reg, regInf);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void getFillingInformationInArea(const TVectorImageP &vi,
Shinya Kitaoka 120a6e
                                 std::vector<tfilledregioninf> ®s,</tfilledregioninf>
Shinya Kitaoka 120a6e
                                 const TRectD &area) {
Shinya Kitaoka 120a6e
  if (!vi->isComputedRegionAlmostOnce()) return;
Shinya Kitaoka 120a6e
  vi->findRegions();
Shinya Kitaoka 120a6e
  UINT regNum = vi->getRegionCount();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (UINT i = 0; i < regNum; i++)
Shinya Kitaoka 120a6e
    addRegionsInArea(vi->getRegion(i), regs, area);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void assignFillingInformation(TVectorImage &vi,
Shinya Kitaoka 120a6e
                              const std::vector<tfilledregioninf> ®s) {</tfilledregioninf>
Shinya Kitaoka 120a6e
  vi.findRegions();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  UINT r, rCount = UINT(regs.size());
Shinya Kitaoka 120a6e
  for (r = 0; r != rCount; ++r) {
Shinya Kitaoka 120a6e
    const TFilledRegionInf &rInfo = regs[r];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    if (TRegion *region = vi.getRegion(rInfo.m_regionId))
Shinya Kitaoka 120a6e
      region->setStyle(rInfo.m_styleId);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void getStrokeStyleInformationInArea(
Shinya Kitaoka 120a6e
    const TVectorImageP &vi, std::vector<std::pair<int, int="">> &strokesInfo,</std::pair<int,>
Shinya Kitaoka 120a6e
    const TRectD &area) {
Shinya Kitaoka 120a6e
  if (!vi->isComputedRegionAlmostOnce()) return;
Shinya Kitaoka 120a6e
  vi->findRegions();
Shinya Kitaoka 120a6e
  UINT regNum = vi->getStrokeCount();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (UINT i = 0; i < vi->getStrokeCount(); i++) {
Shinya Kitaoka 120a6e
    if (!vi->inCurrentGroup(i)) continue;
Shinya Kitaoka 120a6e
    if (area.contains(vi->getStroke(i)->getBBox()))
Shinya Kitaoka 120a6e
      strokesInfo.push_back(
Shinya Kitaoka 120a6e
          std::pair<int, int="">(i, vi->getStroke(i)->getStyle()));</int,>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
shun-iwasawa 27b0cf
static void convertFromCM(
shun-iwasawa 27b0cf
    const TLevelReaderP &lr, const TPaletteP &plt, const TLevelWriterP &lw,
shun-iwasawa 27b0cf
    const std::vector<tframeid> &frames, const TAffine &aff,</tframeid>
shun-iwasawa 27b0cf
    const TRop::ResampleFilterType &resType, FrameTaskNotifier *frameNotifier,
shun-iwasawa 27b0cf
    const TPixel &bgColor, bool removeDotBeforeFrameNumber = false) {
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)frames.size(); i++) {
Shinya Kitaoka 120a6e
    if (frameNotifier->abortTask()) break;
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
        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
        if (bgColor != TPixel::Transparent) TRop::addBackground(ras, bgColor);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        TRasterImageP rasImage(ras);
Shinya Kitaoka 120a6e
        rasImage->setDpi(xdpi, ydpi);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        /*---
Shinya Kitaoka 120a6e
                ConvertPopup
Shinya Kitaoka 120a6e
        での指定に合わせて、[レベル名].[フレーム番号].[拡張子]
Shinya Kitaoka 120a6e
                のうち、[レベル名]と[フレーム番号]の間のドットを消す。
Shinya Kitaoka 120a6e
                例:A.0001.tga → A0001.tga になる。
Shinya Kitaoka 120a6e
        ---*/
Shinya Kitaoka 120a6e
        if (removeDotBeforeFrameNumber) {
Shinya Kitaoka 120a6e
          TFilePath outPath = lw->getFilePath().withFrame(frames[i]);
Shinya Kitaoka 120a6e
          /*--- フレーム番号と拡張子の文字数の合計。大抵8 ---*/
Shinya Kitaoka 120a6e
          int index               = 4 + 1 + outPath.getType().length();
Shinya Kitaoka 120a6e
          std::wstring renamedStr = outPath.getWideString();
Shinya Kitaoka 120a6e
          if (renamedStr[renamedStr.length() - index - 1] == L'.')
Shinya Kitaoka 120a6e
            renamedStr = renamedStr.substr(0, renamedStr.length() - index - 1) +
Shinya Kitaoka 120a6e
                         renamedStr.substr(renamedStr.length() - index, index);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          const TFilePath fp(renamedStr);
Shinya Kitaoka 120a6e
          TImageWriterP writer(fp);
Shinya Kitaoka 120a6e
          writer->setProperties(lw->getProperties());
Shinya Kitaoka 120a6e
          writer->save(rasImage);
Shinya Kitaoka 120a6e
        } else {
Shinya Kitaoka 120a6e
          TImageWriterP iw = lw->getFrameWriter(frames[i]);
Shinya Kitaoka 120a6e
          iw->save(rasImage);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      // QString msg=QObject::tr("Frame %1 : conversion
Shinya Kitaoka 120a6e
      // failed!").arg(QString::number(i+1));
Shinya Kitaoka 120a6e
      //      DVGui::info(msg);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    /*--
Shinya Kitaoka 120a6e
     * これはプログレスバーを進めるものなので、動画番号ではなく、完了したフレームの枚数を投げる
Shinya Kitaoka 120a6e
     * --*/
Shinya Kitaoka 120a6e
    frameNotifier->notifyFrameCompleted(100 * (i + 1) / frames.size());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Campbell Barton ccd505
static void convertFromVI(const TLevelReaderP &lr, const TPaletteP &plt,
shun-iwasawa 27b0cf
                          const TLevelWriterP &lw,
shun-iwasawa 27b0cf
                          const std::vector<tframeid> &frames,</tframeid>
Campbell Barton ccd505
                          const TRop::ResampleFilterType &resType, int width,
Campbell Barton ccd505
                          FrameTaskNotifier *frameNotifier) {
Shinya Kitaoka 120a6e
  QString msg;
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  std::vector<tvectorimagep> images;</tvectorimagep>
Shinya Kitaoka 120a6e
  TRectD maxBbox;
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
      msg = QObject::tr("Frame %1 : conversion failed!")
Shinya Kitaoka 120a6e
                .arg(QString::fromStdString(frames[i].expand()));
Shinya Kitaoka 120a6e
      DVGui::info(msg);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  maxBbox = maxBbox.enlarge(2);
Shinya Kitaoka 120a6e
  TAffine aff;
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
    if (frameNotifier->abortTask()) break;
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(TFrameId(i + 1));
Shinya Kitaoka 120a6e
        iw->save(TRasterImageP(rasImage));
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      msg = QObject::tr("Frame %1 : conversion failed!")
Shinya Kitaoka 120a6e
                .arg(QString::fromStdString(frames[i].expand()));
Shinya Kitaoka 120a6e
      DVGui::info(msg);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    frameNotifier->notifyFrameCompleted(100 * (i + 1) / frames.size());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 27b0cf
static void convertFromFullRaster(
shun-iwasawa 27b0cf
    const TLevelReaderP &lr, const TLevelWriterP &lw,
shun-iwasawa 27b0cf
    const std::vector<tframeid> &_frames, const TAffine &aff,</tframeid>
shun-iwasawa 27b0cf
    const TRop::ResampleFilterType &resType, FrameTaskNotifier *frameNotifier,
shun-iwasawa 27b0cf
    const TPixel &bgColor, bool removeDotBeforeFrameNumber = false) {
Shinya Kitaoka 120a6e
  std::vector<tframeid> frames = _frames;</tframeid>
Shinya Kitaoka 120a6e
  if (frames.empty() &&
Shinya Kitaoka 120a6e
      lr->loadInfo()->getFrameCount() ==
Shinya Kitaoka 120a6e
          1)  // e' una immagine singola, non un livello
Shinya Kitaoka 120a6e
    frames.push_back(TFrameId());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)frames.size(); i++) {
Shinya Kitaoka 120a6e
    if (frameNotifier->abortTask()) break;
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TRasterImageP img;
Shinya Kitaoka 120a6e
      if (frames[i] == TFrameId())  // immagine singola, non livello
Shinya Kitaoka 120a6e
      {
Shinya Kitaoka 120a6e
        assert(frames.size() == 1);
Shinya Kitaoka 120a6e
        TImageP _img;
Shinya Kitaoka 120a6e
        if (!TImageReader::load(lr->getFilePath(), _img)) continue;
Shinya Kitaoka 120a6e
        img = _img;
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        TImageReaderP ir = lr->getFrameReader(frames[i]);
Shinya Kitaoka 120a6e
        img              = ir->load();
Shinya Kitaoka 120a6e
      }
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
      if (bgColor != TPixel::Transparent) TRop::addBackground(raster, bgColor);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      TRasterImageP newImg(raster);
Shinya Kitaoka 120a6e
      double xDpi, yDpi;
Shinya Kitaoka 120a6e
      img->getDpi(xDpi, yDpi);
Shinya Kitaoka 120a6e
      if (xDpi == 0 && yDpi == 0)
Shinya Kitaoka 120a6e
        xDpi = yDpi = Preferences::instance()->getDefLevelDpi();
Shinya Kitaoka 120a6e
      newImg->setDpi(xDpi, yDpi);
Shinya Kitaoka 120a6e
      if (frames[i] == TFrameId())
Shinya Kitaoka 120a6e
        TImageWriter::save(lw->getFilePath(), newImg);
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        /*---
Shinya Kitaoka 120a6e
ConvertPopup での指定に合わせて、[レベル名].[フレーム番号].[拡張子]
Shinya Kitaoka 120a6e
のうち、[レベル名]と[フレーム番号]の間のドットを消す。
Shinya Kitaoka 120a6e
例:A.0001.tga → A0001.tga になる。
Shinya Kitaoka 120a6e
---*/
Shinya Kitaoka 120a6e
        if (removeDotBeforeFrameNumber) {
Shinya Kitaoka 120a6e
          TFilePath outPath = lw->getFilePath().withFrame(frames[i]);
Shinya Kitaoka 120a6e
          /*--- フレーム番号と拡張子の文字数の合計。大抵8 ---*/
Shinya Kitaoka 120a6e
          int index               = 4 + 1 + outPath.getType().length();
Shinya Kitaoka 120a6e
          std::wstring renamedStr = outPath.getWideString();
Shinya Kitaoka 120a6e
          if (renamedStr[renamedStr.length() - index - 1] == L'.')
Shinya Kitaoka 120a6e
            renamedStr = renamedStr.substr(0, renamedStr.length() - index - 1) +
Shinya Kitaoka 120a6e
                         renamedStr.substr(renamedStr.length() - index, index);
Shinya Kitaoka 120a6e
          const TFilePath fp(renamedStr);
Shinya Kitaoka 120a6e
          TImageWriterP writer(fp);
Shinya Kitaoka 120a6e
          writer->setProperties(lw->getProperties());
Shinya Kitaoka 120a6e
          writer->save(newImg);
Shinya Kitaoka 120a6e
        } else {
Shinya Kitaoka 120a6e
          TImageWriterP iw = lw->getFrameWriter(frames[i]);
Shinya Kitaoka 120a6e
          iw->save(newImg);
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      // QString msg=QObject::tr("Frame %1 : conversion
Shinya Kitaoka 120a6e
      // failed!").arg(QString::number(i+1));
Shinya Kitaoka 120a6e
      // DVGui::info(msg);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    /*--
Shinya Kitaoka 120a6e
     * これはプログレスバーを進めるものなので、動画番号ではなく、完了したフレームの枚数を投げる
Shinya Kitaoka 120a6e
     * --*/
Shinya Kitaoka 120a6e
    frameNotifier->notifyFrameCompleted(100 * (i + 1) / frames.size());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Campbell Barton ccd505
static void convertFromVector(const TLevelReaderP &lr, const TLevelWriterP &lw,
Campbell Barton ccd505
                              const std::vector<tframeid> &_frames,</tframeid>
Campbell Barton ccd505
                              FrameTaskNotifier *frameNotifier) {
Shinya Kitaoka 120a6e
  std::vector<tframeid> frames = _frames;</tframeid>
Shinya Kitaoka 120a6e
  TLevelP lv                   = lr->loadInfo();
Shinya Kitaoka 120a6e
  if (frames.empty() &&
Shinya Kitaoka 120a6e
      lv->getFrameCount() == 1)  // e' una immagine singola, non un livello
Shinya Kitaoka 120a6e
    frames.push_back(TFrameId());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)frames.size(); i++) {
Shinya Kitaoka 120a6e
    if (frameNotifier->abortTask()) break;
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TVectorImageP img;
Shinya Kitaoka 120a6e
      if (frames[i] == TFrameId())  // immagine singola, non livello
Shinya Kitaoka 120a6e
      {
Shinya Kitaoka 120a6e
        assert(frames.size() == 1);
Shinya Kitaoka 120a6e
        TImageP _img;
Shinya Kitaoka 120a6e
        if (!TImageReader::load(lr->getFilePath(), _img)) continue;
Shinya Kitaoka 120a6e
        img = _img;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        TImageReaderP ir = lr->getFrameReader(frames[i]);
Shinya Kitaoka 120a6e
        img              = ir->load();
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      img->setPalette(lv->getPalette());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (frames[i] == TFrameId())
Shinya Kitaoka 120a6e
        TImageWriter::save(lw->getFilePath(), img);
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        TImageWriterP iw = lw->getFrameWriter(frames[i]);
Shinya Kitaoka 120a6e
        iw->save(img);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
      // QString msg=QObject::tr("Frame %1 : conversion
Shinya Kitaoka 120a6e
      // failed!").arg(QString::number(i+1));
Shinya Kitaoka 120a6e
      // DVGui::info(msg);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    frameNotifier->notifyFrameCompleted(100 * (i + 1) / frames.size());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void convert(const TFilePath &source, const TFilePath &dest,
Shinya Kitaoka 120a6e
             const TFrameId &from, const TFrameId &to, double framerate,
Shinya Kitaoka 120a6e
             TPropertyGroup *prop, FrameTaskNotifier *frameNotifier,
Shinya Kitaoka 120a6e
             const TPixel &bgColor, bool removeDotBeforeFrameNumber) {
Shinya Kitaoka 120a6e
  std::string dstExt = dest.getType(), srcExt = source.getType();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Load source level structure
Shinya Kitaoka 120a6e
  TLevelReaderP lr(source);
Shinya Kitaoka 120a6e
  TLevelP level = lr->loadInfo();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Shinya Kitaoka 120a6e
  if (dstExt == "avi") {
Shinya Kitaoka 120a6e
    TDimension res(0, 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    const TImageInfo *info = lr->getImageInfo(level->begin()->first);
Shinya Kitaoka 120a6e
    res.lx                 = info->m_lx;
Shinya Kitaoka 120a6e
    res.ly                 = info->m_ly;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    std::string codecName = prop->getProperty(0)->getValueAsString();
Shinya Kitaoka 120a6e
    if (!AviCodecRestrictions::canWriteMovie(::to_wstring(codecName), res)) {
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 3bfa54
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Get the frames available in level inside the [from, to] range
Shinya Kitaoka 120a6e
  std::vector<tframeid> frames;</tframeid>
Shinya Kitaoka 120a6e
  getFrameIds(from, to, level, frames);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Write the destination level
Shinya Kitaoka 120a6e
  TLevelWriterP lw(dest, prop);
Shinya Kitaoka 120a6e
  lw->setFrameRate(framerate);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (srcExt == "tlv")
Shinya Kitaoka 120a6e
    convertFromCM(lr, level->getPalette(), lw, frames, TAffine(),
Shinya Kitaoka 120a6e
                  TRop::Triangle, frameNotifier, bgColor,
Shinya Kitaoka 120a6e
                  removeDotBeforeFrameNumber);
Shinya Kitaoka 120a6e
  else if (srcExt == "pli")
Shinya Kitaoka 120a6e
    // assert(!"Conversion from pli files is currently diabled");
Shinya Kitaoka 120a6e
    convertFromVector(lr, lw, frames, frameNotifier);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    convertFromFullRaster(lr, lw, frames, TAffine(), TRop::Triangle,
Shinya Kitaoka 120a6e
                          frameNotifier, bgColor, removeDotBeforeFrameNumber);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void convertNaa2Tlv(const TFilePath &source, const TFilePath &dest,
Shinya Kitaoka 120a6e
                    const TFrameId &from, const TFrameId &to,
shun_iwasawa 1fa45a
                    FrameTaskNotifier *frameNotifier, TPalette *palette,
shun-iwasawa 1584bb
                    bool removeUnusedStyles, double dpi) {
Shinya Kitaoka 120a6e
  std::string dstExt = dest.getType(), srcExt = source.getType();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Load source level structure
Shinya Kitaoka 120a6e
  TLevelReaderP lr(source);
Shinya Kitaoka 120a6e
  TLevelP level = lr->loadInfo();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Get the frames available in level inside the [from, to] range
Shinya Kitaoka 120a6e
  std::vector<tframeid> frames;</tframeid>
Shinya Kitaoka 120a6e
  getFrameIds(from, to, level, frames);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (frames.empty()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Write the destination level
Shinya Kitaoka 120a6e
  TLevelWriterP lw;  // Initialized just before a sure write
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  Naa2TlvConverter converter;
Shinya Kitaoka 120a6e
  converter.setPalette(palette);
Toshihiro Shimizu 890ddd
shun_iwasawa 22ad9e
  QList<int> usedStyleIds({0});</int>
shun_iwasawa 22ad9e
Shinya Kitaoka 120a6e
  int f, fCount = int(frames.size());
Shinya Kitaoka 120a6e
  for (f = 0; f != fCount; ++f) {
Shinya Kitaoka 120a6e
    if (frameNotifier->abortTask()) break;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    try {
Shinya Kitaoka 120a6e
      TImageReaderP ir = lr->getFrameReader(frames[f]);
Shinya Kitaoka 120a6e
      TImageP img      = ir->load();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      if (!img) continue;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      TRasterImageP ri = img;
Shinya Kitaoka 120a6e
      if (!ri) continue;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      TRaster32P raster =
Shinya Kitaoka 120a6e
          ri->getRaster();  // Converts only 32-bit images, it seems...
Shinya Kitaoka 120a6e
      if (!raster)          //
Shinya Kitaoka 120a6e
        continue;           //
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      converter.process(raster);
Toshihiro Shimizu 890ddd
shun-iwasawa 1584bb
      if (TToonzImageP dstImg = converter.makeTlv(
shun-iwasawa 1584bb
              false, usedStyleIds, dpi))  // Opaque synthetic inks
Shinya Kitaoka 120a6e
      {
Shinya Kitaoka 120a6e
        if (converter.getPalette() == 0)
Shinya Kitaoka 120a6e
          converter.setPalette(dstImg->getPalette());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        if (!lw)  // Initialize output file only on a sure write
Shinya Kitaoka 120a6e
          lw = TLevelWriterP(dest);  //
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        TImageWriterP iw = lw->getFrameWriter(frames[f]);
Shinya Kitaoka 120a6e
        iw->save(dstImg);
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        DVGui::warning(QObject::tr(
Shinya Kitaoka 120a6e
            "The source image seems not suitable for this kind of conversion"));
Shinya Kitaoka 120a6e
        frameNotifier->notifyError();
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } catch (...) {
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    frameNotifier->notifyFrameCompleted(100 * (f + 1) / frames.size());
Shinya Kitaoka 120a6e
  }
shun_iwasawa 22ad9e
shun_iwasawa 22ad9e
  if (removeUnusedStyles) converter.removeUnusedStyles(usedStyleIds);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define ZOOMLEVELS 13
Toshihiro Shimizu 890ddd
#define NOZOOMINDEX 6
Shinya Kitaoka 120a6e
double ZoomFactors[ZOOMLEVELS] = {
Shinya Kitaoka 120a6e
    0.015625, 0.03125, 0.0625, 0.125, 0.25, 0.5, 1, 2, 4, 8, 16, 32, 64};
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
double getQuantizedZoomFactor(double zf, bool forward) {
Shinya Kitaoka 120a6e
  if (forward && (zf > ZoomFactors[ZOOMLEVELS - 1] ||
Shinya Kitaoka 120a6e
                  areAlmostEqual(zf, ZoomFactors[ZOOMLEVELS - 1], 1e-5)))
Shinya Kitaoka 120a6e
    return zf;
Shinya Kitaoka 120a6e
  else if (!forward &&
Shinya Kitaoka 120a6e
           (zf < ZoomFactors[0] || areAlmostEqual(zf, ZoomFactors[0], 1e-5)))
Shinya Kitaoka 120a6e
    return zf;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert((!forward && zf > ZoomFactors[0]) ||
Shinya Kitaoka 120a6e
         (forward && zf < ZoomFactors[ZOOMLEVELS - 1]));
Shinya Kitaoka 120a6e
  int i = 0;
Shinya Kitaoka 120a6e
  for (i = 0; i <= ZOOMLEVELS - 1; i++)
Shinya Kitaoka 120a6e
    if (areAlmostEqual(zf, ZoomFactors[i], 1e-5)) zf = ZoomFactors[i];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (forward && zf < ZoomFactors[0])
Shinya Kitaoka 120a6e
    return ZoomFactors[0];
Shinya Kitaoka 120a6e
  else if (!forward && zf > ZoomFactors[ZOOMLEVELS - 1])
Shinya Kitaoka 120a6e
    return ZoomFactors[ZOOMLEVELS - 1];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < ZOOMLEVELS - 1; i++)
Shinya Kitaoka 120a6e
    if (ZoomFactors[i + 1] - zf >= 0 && zf - ZoomFactors[i] >= 0) {
Shinya Kitaoka 120a6e
      if (forward && ZoomFactors[i + 1] == zf)
Shinya Kitaoka 120a6e
        return ZoomFactors[i + 2];
Shinya Kitaoka 120a6e
      else if (!forward && ZoomFactors[i] == zf)
Shinya Kitaoka 120a6e
        return ZoomFactors[i - 1];
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        return forward ? ZoomFactors[i + 1] : ZoomFactors[i];
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  return ZoomFactors[NOZOOMINDEX];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace ImageUtils
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
void getViewerShortcuts(int &zoomIn, int &zoomOut, int &zoomReset, int &zoomFit,
Shinya Kitaoka 120a6e
                        int &showHideFullScreen, int &actualPixelSize) {
Shinya Kitaoka 120a6e
  CommandManager *cManager = CommandManager::instance();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  zoomIn = cManager->getKeyFromShortcut(cManager->getShortcutFromId(V_ZoomIn));
Shinya Kitaoka 120a6e
  zoomOut =
Shinya Kitaoka 120a6e
      cManager->getKeyFromShortcut(cManager->getShortcutFromId(V_ZoomOut));
Shinya Kitaoka 120a6e
  zoomReset =
Shinya Kitaoka 120a6e
      cManager->getKeyFromShortcut(cManager->getShortcutFromId(V_ZoomReset));
Shinya Kitaoka 120a6e
  zoomFit =
Shinya Kitaoka 120a6e
      cManager->getKeyFromShortcut(cManager->getShortcutFromId(V_ZoomFit));
Shinya Kitaoka 120a6e
  showHideFullScreen = cManager->getKeyFromShortcut(
Shinya Kitaoka 120a6e
      cManager->getShortcutFromId(V_ShowHideFullScreen));
Shinya Kitaoka 120a6e
  actualPixelSize = cManager->getKeyFromShortcut(
Shinya Kitaoka 120a6e
      cManager->getShortcutFromId(V_ActualPixelSize));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace ImageUtils {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
ShortcutZoomer::ShortcutZoomer(QWidget *zoomingWidget)
Shinya Kitaoka 120a6e
    : m_widget(zoomingWidget) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//--------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool ShortcutZoomer::exec(QKeyEvent *event) {
Shinya Kitaoka 120a6e
  int zoomInKey, zoomOutKey, zoomResetKey, zoomFitKey, showHideFullScreenKey,
Shinya Kitaoka 120a6e
      actualPixelSize;
Shinya Kitaoka 120a6e
  getViewerShortcuts(zoomInKey, zoomOutKey, zoomResetKey, zoomFitKey,
Shinya Kitaoka 120a6e
                     showHideFullScreenKey, actualPixelSize);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int key = event->key();
Shinya Kitaoka 120a6e
  if (key == Qt::Key_Control || key == Qt::Key_Shift || key == Qt::Key_Alt)
Shinya Kitaoka 120a6e
    return false;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  key =
Shinya Kitaoka 120a6e
      key |
Shinya Kitaoka 120a6e
      event->modifiers() & (~0xf0000000);  // Ignore if the key is a numpad key
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return (key == showHideFullScreenKey)
Shinya Kitaoka 120a6e
             ? toggleFullScreen()
Shinya Kitaoka 120a6e
             : (key == Qt::Key_Escape)
Shinya Kitaoka 120a6e
                   ? toggleFullScreen(true)
Shinya Kitaoka 120a6e
                   : (key == actualPixelSize)
Shinya Kitaoka 120a6e
                         ? setActualPixelSize()
Shinya Kitaoka 120a6e
                         : (key == zoomFitKey)
Shinya Kitaoka 120a6e
                               ? fit()
Shinya Kitaoka 120a6e
                               : (key == zoomInKey || key == zoomOutKey ||
Shinya Kitaoka 120a6e
                                  key == zoomResetKey)
Shinya Kitaoka 120a6e
                                     ? zoom(key == zoomInKey,
Shinya Kitaoka 120a6e
                                            key == zoomResetKey)
Shinya Kitaoka 120a6e
                                     : false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//*********************************************************************************************
Toshihiro Shimizu 890ddd
//    FullScreenWidget  implementation
Toshihiro Shimizu 890ddd
//*********************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
FullScreenWidget::FullScreenWidget(QWidget *parent) : QWidget(parent) {
Shinya Kitaoka 120a6e
  QHBoxLayout *layout = new QHBoxLayout(this);
Shinya Kitaoka 120a6e
  layout->setMargin(0);
Shinya Kitaoka 120a6e
  layout->setSpacing(0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  setLayout(layout);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void FullScreenWidget::setWidget(QWidget *widget) {
Shinya Kitaoka 120a6e
  QLayout *layout = this->layout();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  delete layout->takeAt(0);
Shinya Kitaoka 120a6e
  if ((m_widget = widget)) layout->addWidget(m_widget);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool FullScreenWidget::toggleFullScreen(bool quit) {
Shinya Kitaoka 120a6e
  if (windowState() & Qt::WindowFullScreen) {
Shinya Kitaoka 120a6e
    hide();
Shinya Kitaoka 120a6e
    setWindowFlags(windowFlags() & ~(Qt::Window | Qt::WindowStaysOnTopHint));
Shinya Kitaoka 120a6e
    showNormal();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    setContentsMargins(0, 0, 0, 0);  // ...
Shinya Kitaoka 120a6e
    m_widget->setFocus();
Shinya Kitaoka 120a6e
    return true;
Shinya Kitaoka 120a6e
  } else if (!quit) {
Shinya Kitaoka 120a6e
    setContentsMargins(0, 0, 1, 1);  // QTBUG #7556
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    setWindowFlags(windowFlags() | Qt::Window | Qt::WindowStaysOnTopHint);
Shinya Kitaoka 120a6e
    showFullScreen();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    return true;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // imageutils