Skip to content

OpenToonz - An open-source full-featured 2D animation creation software

C

convert old level files to tlv (#2126)

shun-iwasawaacfe755bb9b0
diff --git a/toonz/sources/include/toonzqt/imageutils.h b/toonz/sources/include/toonzqt/imageutils.h
index e7fab9228..863e0880c 100644
--- a/toonz/sources/include/toonzqt/imageutils.h
+++ b/toonz/sources/include/toonzqt/imageutils.h
@@ -116,6 +116,16 @@ void DVAPI convertNaa2Tlv(
bool removeUnusedStyles = false,
double dpi = 0.0); //! Remove unused styles from input palette.
+// convert old levels (tzp / tzu) to tlv
+void DVAPI convertOldLevel2Tlv(
+ const TFilePath &source, //!< Level path to convert from.
+ const TFilePath &dest, //!< Level path to convert to.
+ const TFrameId &from, //!< First source frame to convert.
+ const TFrameId &to, //!< Last source frame to convert.
+ FrameTaskNotifier
+ *frameNotifier //!< Observer class for frame success notifications.
+ );
+
double DVAPI getQuantizedZoomFactor(double zf, bool forward);
void DVAPI getFillingInformationOverlappingArea(
diff --git a/toonz/sources/toonz/convertpopup.cpp b/toonz/sources/toonz/convertpopup.cpp
index 8417cb71e..7a3cf6492 100644
--- a/toonz/sources/toonz/convertpopup.cpp
+++ b/toonz/sources/toonz/convertpopup.cpp
@@ -75,6 +75,7 @@ QMap<std::string, TPropertyGroup *> ConvertPopup::m_formatProperties;
// const QString CreateNewPalette(QObject::tr("Create new palette"));
const QString TlvExtension("tlv");
+const QString OldLevelToTlvExtension("Old Level to TLV");
/*
const QString TlvMode_Unpainted(QObject::tr("Unpainted tlv"));
const QString TlvMode_PaintedFromTwoImages(QObject::tr("Painted tlv from two
@@ -213,6 +214,10 @@ void ConvertPopup::Converter::convertLevel(
} else {
convertLevelWithConvert2Tlv(sourceFileFullPath);
}
+ } else if (popup->m_fileFormat->currentText() == OldLevelToTlvExtension) {
+ // convert old levels (tzp/tzu) to tlv
+ ImageUtils::convertOldLevel2Tlv(sourceFileFullPath, dstFileFullPath, from,
+ to, m_parent->m_notifier);
} else {
// convert to full-color
TPixel32 bgColor = m_parent->m_bgColorField->getColor();
@@ -727,12 +732,13 @@ void ConvertPopup::onTlvModeSelected(const QString &tlvMode) {
void ConvertPopup::onFormatSelected(const QString &format) {
onFormatChanged(format);
- bool isTlv = format == TlvExtension;
- bool isPli = format == "svg";
+ bool isTlv = format == TlvExtension;
+ bool isOldLevel2Tlv = format == OldLevelToTlvExtension;
+ bool isPli = format == "svg";
- m_formatOptions->setVisible(!isTlv);
- m_bgColorField->setVisible(!isTlv && !isPli);
- m_bgColorLabel->setVisible(!isTlv && !isPli);
+ m_formatOptions->setVisible(!isTlv && !isOldLevel2Tlv);
+ m_bgColorField->setVisible(!isTlv && !isPli && !isOldLevel2Tlv);
+ m_bgColorLabel->setVisible(!isTlv && !isPli && !isOldLevel2Tlv);
m_tlvFrame->setVisible(isTlv);
// m_svgFrame->setVisible(isPli);
@@ -755,14 +761,27 @@ void ConvertPopup::setFiles(const std::vector<TFilePath> &fps) {
m_okBtn->setEnabled(true);
m_fileFormat->setEnabled(true);
m_fileFormat->removeItem(m_fileFormat->findText("svg"));
+ m_fileFormat->removeItem(m_fileFormat->findText(OldLevelToTlvExtension));
+ // true if the ALL files are full color raster
bool areFullcolor = true;
- bool areVector = false;
+
+ // true if AT LEAST ONE file is vector. Since the popup will never open if the
+ // file selection contains both raster and vector file types, we can assume
+ // ALL files are vector if areVector == true.
+ bool areVector = false;
+ // For the same logic as the above, true if all files are old levels
+ // (tzp/tzu).
+ bool areOldLevel = false;
+
for (int i = 0; i < (int)fps.size(); i++) {
TFileType::Type type = TFileType::getInfo(fps[i]);
if (!TFileType::isFullColor(type)) {
- areFullcolor = false;
- if (TFileType::isVector(type)) areVector = true;
+ areFullcolor = false;
+ if (TFileType::isVector(type))
+ areVector = true;
+ else if (fps[i].getType() == "tzp" || fps[i].getType() == "tzu")
+ areOldLevel = true;
break;
}
}
@@ -779,9 +798,13 @@ void ConvertPopup::setFiles(const std::vector<TFilePath> &fps) {
m_fileFormat->setCurrentIndex(m_fileFormat->findText("svg"));
m_fileFormat->setEnabled(false);
onFormatSelected("svg");
- } else
-
- {
+ } else if (areOldLevel) {
+ if (tlvIndex < 0) m_fileFormat->addItem(OldLevelToTlvExtension);
+ m_fileFormat->setCurrentIndex(
+ m_fileFormat->findText(OldLevelToTlvExtension));
+ m_fileFormat->setEnabled(false);
+ onFormatSelected(OldLevelToTlvExtension);
+ } else {
if (tlvIndex >= 0) {
int index = m_fileFormat->currentIndex();
m_fileFormat->removeItem(tlvIndex);
@@ -1014,7 +1037,10 @@ TFilePath ConvertPopup::getDestinationFilePath(
// Build the output level name
const QString &fldName = m_fileNameFld->text();
- const std::string &ext = m_fileFormat->currentText().toStdString();
+ const std::string &ext =
+ (m_fileFormat->currentText() == OldLevelToTlvExtension)
+ ? "tlv"
+ : m_fileFormat->currentText().toStdString();
const std::wstring &name =
fldName.isEmpty() ? sourceFilePath.getWideName() : fldName.toStdWString();
@@ -1243,7 +1269,8 @@ void ConvertPopup::onOptionsClicked() {
//-------------------------------------------------------------------
void ConvertPopup::onFormatChanged(const QString &ext) {
- if (ext == QString("avi") || ext == QString("tzp") || ext == TlvExtension) {
+ if (ext == QString("avi") || ext == QString("tzp") || ext == TlvExtension ||
+ ext == OldLevelToTlvExtension) {
m_removeDotBeforeFrameNumber->setChecked(false);
m_removeDotBeforeFrameNumber->setEnabled(false);
} else {
diff --git a/toonz/sources/toonz/filebrowser.cpp b/toonz/sources/toonz/filebrowser.cpp
index a14fe8aa9..4d2c576a6 100644
--- a/toonz/sources/toonz/filebrowser.cpp
+++ b/toonz/sources/toonz/filebrowser.cpp
@@ -1086,6 +1086,10 @@ TSystem::renameFile(newFolder, folder);
//-----------------------------------------------------------------------------
QMenu *FileBrowser::getContextMenu(QWidget *parent, int index) {
+ auto isOldLevelType = [](TFilePath &path) -> bool {
+ return path.getType() == "tzp" || path.getType() == "tzu";
+ };
+
bool ret = true;
// TODO: spostare in questa classe anche la definizione delle azioni?
@@ -1180,12 +1184,17 @@ QMenu *FileBrowser::getContextMenu(QWidget *parent, int index) {
if (i == files.size()) {
std::string type = files[0].getType();
for (j = 0; j < files.size(); j++)
- if (files[j].getType() == "tzp" || files[j].getType() == "tzu") break;
+ if (isOldLevelType(files[j])) break;
if (j == files.size()) menu->addAction(cm->getAction(MI_ViewFile));
- for (j = 0; j < files.size(); j++)
+
+ for (j = 0; j < files.size(); j++) {
if ((files[0].getType() == "pli" && files[j].getType() != "pli") ||
(files[0].getType() != "pli" && files[j].getType() == "pli"))
break;
+ else if ((isOldLevelType(files[0]) && !isOldLevelType(files[j])) ||
+ (!isOldLevelType(files[0]) && isOldLevelType(files[j])))
+ break;
+ }
if (j == files.size()) {
menu->addAction(cm->getAction(MI_ConvertFiles));
// iwsw commented out temporarily
diff --git a/toonz/sources/toonzqt/imageutils.cpp b/toonz/sources/toonzqt/imageutils.cpp
index bb19e906b..91cb97b9f 100644
--- a/toonz/sources/toonzqt/imageutils.cpp
+++ b/toonz/sources/toonzqt/imageutils.cpp
@@ -13,6 +13,7 @@
#include "toonz/toonzscene.h"
#include "toonz/tproject.h"
#include "toonz/Naa2TlvConverter.h"
+#include "toonz/toonzimageutils.h"
#ifdef _WIN32
#include "avicodecrestrictions.h"
@@ -704,6 +705,63 @@ void convertNaa2Tlv(const TFilePath &source, const TFilePath &dest,
//=============================================================================
+void convertOldLevel2Tlv(const TFilePath &source, const TFilePath &dest,
+ const TFrameId &from, const TFrameId &to,
+ FrameTaskNotifier *frameNotifier) {
+ TFilePath destPltPath = TFilePath(dest.getParentDir().getWideString() +
+ L"\\" + dest.getWideName() + L".tpl");
+ if (TSystem::doesExistFileOrLevel(destPltPath))
+ TSystem::removeFileOrLevel(destPltPath);
+
+ TLevelWriterP lw(dest);
+ lw->setIconSize(Preferences::instance()->getIconSize());
+ TPaletteP palette =
+ ToonzImageUtils::loadTzPalette(source.withType("plt").withNoFrame());
+ TLevelReaderP lr(source);
+ if (!lr) {
+ DVGui::warning(QObject::tr(
+ "The source image seems not suitable for this kind of conversion"));
+ frameNotifier->notifyError();
+ return;
+ }
+ TLevelP inLevel = lr->loadInfo();
+ if (!inLevel || inLevel->getFrameCount() == 0) {
+ DVGui::warning(QObject::tr(
+ "The source image seems not suitable for this kind of conversion"));
+ frameNotifier->notifyError();
+ return;
+ }
+ // Get the frames available in level inside the [from, to] range
+ std::vector<TFrameId> frames;
+ getFrameIds(from, to, inLevel, frames);
+ if (frames.empty()) return;
+
+ TLevelP outLevel;
+ outLevel->setPalette(palette.getPointer());
+ try {
+ int f, fCount = int(frames.size());
+ for (f = 0; f != fCount; ++f) {
+ if (frameNotifier->abortTask()) break;
+ TToonzImageP img = lr->getFrameReader(frames[f])->load();
+ if (!img) continue;
+ img->setPalette(palette.getPointer());
+ lw->getFrameWriter(frames[f])->save(img);
+
+ frameNotifier->notifyFrameCompleted(100 * (f + 1) / frames.size());
+ }
+ } catch (TException &e) {
+ QString msg = QString::fromStdWString(e.getMessage());
+ DVGui::warning(msg);
+ lw = TLevelWriterP();
+ if (TSystem::doesExistFileOrLevel(dest)) TSystem::removeFileOrLevel(dest);
+ frameNotifier->notifyError();
+ return;
+ }
+ lw = TLevelWriterP();
+}
+
+//=============================================================================
+
#define ZOOMLEVELS 13
#define NOZOOMINDEX 6
double ZoomFactors[ZOOMLEVELS] = {