Skip to content

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

C

feat: auto detect painted Aliased Image and convert to TLV when importing

Abel Youc612b0c81ae7
diff --git a/toonz/sources/include/toonzqt/imageutils.h b/toonz/sources/include/toonzqt/imageutils.h
index 478d8e333..233d3b212 100644
--- a/toonz/sources/include/toonzqt/imageutils.h
+++ b/toonz/sources/include/toonzqt/imageutils.h
@@ -106,6 +106,8 @@ void DVAPI convert(
bool DVAPI isAAImage(TFilePath path);
+bool DVAPI isPaintedImage(TFilePath path);
+
void DVAPI convertNaa2Tlv(
const TFilePath &source, //!< Level path to convert from.
const TFilePath &dest, //!< Level path to convert to.
diff --git a/toonz/sources/toonz/convertpopup.cpp b/toonz/sources/toonz/convertpopup.cpp
index ecab5080a..77c226d12 100644
--- a/toonz/sources/toonz/convertpopup.cpp
+++ b/toonz/sources/toonz/convertpopup.cpp
@@ -210,13 +210,14 @@ void ConvertPopup::Converter::convertLevel(
if (popup->m_fileFormat->currentText() == TlvExtension) {
// convert to TLV
if (!(m_parent->getTlvMode().compare(m_parent->TlvMode_PaintedFromNonAA))) {
- // no AA source (retas)
+ // no AA source (retas painted)
TPaletteP palette = popup->readUserProvidedPalette();
ImageUtils::convertNaa2Tlv(sourceFileFullPath, dstFileFullPath, from, to,
m_parent->m_notifier, palette.getPointer(),
m_parent->m_removeUnusedStyles->isChecked(),
m_parent->m_dpiFld->getValue());
} else {
+ // other 3 cases
convertLevelWithConvert2Tlv(sourceFileFullPath);
}
} else if (popup->m_fileFormat->currentText() == OldLevelToTlvExtension) {
diff --git a/toonz/sources/toonz/iocommand.cpp b/toonz/sources/toonz/iocommand.cpp
index 6ca036dc8..408ba7be4 100644
--- a/toonz/sources/toonz/iocommand.cpp
+++ b/toonz/sources/toonz/iocommand.cpp
@@ -2890,10 +2890,17 @@ void IoCmd::convertNAARaster2TLV(
}
IoCmd::ConvertingPopup convertingPopup(TApp::instance()->getMainWindow(),
path);
- /*convertingPopup.show();
- ImageUtils::convertNaa2Tlv(path, dstPath, from, to,
- convertingPopup.getNotifier(), 0, true, dpi); convertingPopup.hide(); path
- = convertingPopup.getResultPath();*/
+ if (ImageUtils::isPaintedImage(first)) {
+ convertingPopup.setMaximum(to - from + 1);
+ convertingPopup.show();
+ ImageUtils::convertNaa2Tlv(path, dstPath, from, to,
+ convertingPopup.getNotifier(), 0, true, dpi);
+ convertingPopup.hide();
+ if (!convertingPopup.wasCanceled())
+ rd = LoadResourceArguments::ResourceData(dstPath);
+ return;
+ }
+
Convert2Tlv converter(path, TFilePath(), dstPath.getParentDir(),
QString::fromStdWString(dstPath.getWideName()),
from, to, false, TFilePath(), 0, 0, 50, true, true,
diff --git a/toonz/sources/toonzqt/imageutils.cpp b/toonz/sources/toonzqt/imageutils.cpp
index dce67b65b..a199562f7 100644
--- a/toonz/sources/toonzqt/imageutils.cpp
+++ b/toonz/sources/toonzqt/imageutils.cpp
@@ -699,6 +699,111 @@ bool isAAImage(TFilePath path) {
return ratio > value;
}
+bool isPaintedImage(TFilePath path) {
+ QImage img(path.getQString());
+ if (img.isNull()) return false;
+ const uint16_t width = img.width();
+ const uint16_t height = img.height();
+ const QRgb *imgData = reinterpret_cast<const QRgb *>(img.bits());
+ const uint32_t totalPixels = width * height;
+
+ std::unordered_map<uint32_t, uint32_t> colorCount;
+
+ for (int y = 0; y < height; ++y) {
+ for (int x = 0; x < width; ++x) {
+ QRgb color = imgData[y * width + x];
+ colorCount[color]++;
+ }
+ }
+
+ uint32_t maxCount = 0;
+ QRgb backgroundColor = 0;
+ for (const auto &pair : colorCount) {
+ if (pair.second > maxCount) {
+ maxCount = pair.second;
+ backgroundColor = pair.first;
+ }
+ }
+
+ std::vector<uint8_t> visited(totalPixels, 0);
+ std::vector<uint32_t> stack;
+ stack.reserve(totalPixels / 4);
+
+ uint32_t outerBackgroundCount = 0;
+
+ auto tryPush = [&](uint16_t x, uint16_t y) {
+ uint32_t idx = y * width + x;
+ if (!visited[idx] && imgData[idx] == backgroundColor) {
+ visited[idx] = 1;
+ outerBackgroundCount++;
+ stack.push_back((static_cast<uint32_t>(y) << 16) | x);
+ }
+ };
+
+ for (uint16_t x = 0; x < width; ++x) {
+ tryPush(x, 0);
+ tryPush(x, height - 1);
+ }
+ for (uint16_t y = 1; y < height - 1; ++y) {
+ tryPush(0, y);
+ tryPush(width - 1, y);
+ }
+
+ // DFS
+ while (!stack.empty()) {
+ uint32_t packed = stack.back();
+ stack.pop_back();
+
+ uint16_t y = packed >> 16;
+ uint16_t x = packed & 0xFFFF;
+ uint32_t idx = y * width + x;
+
+ if (y > 0) {
+ uint32_t nidx = idx - width;
+ if (!visited[nidx] && imgData[nidx] == backgroundColor) {
+ visited[nidx] = 1;
+ outerBackgroundCount++;
+ stack.push_back((static_cast<uint32_t>(y - 1) << 16) | x);
+ }
+ }
+ if (y < height - 1) {
+ uint32_t nidx = idx + width;
+ if (!visited[nidx] && imgData[nidx] == backgroundColor) {
+ visited[nidx] = 1;
+ outerBackgroundCount++;
+ stack.push_back((static_cast<uint32_t>(y + 1) << 16) | x);
+ }
+ }
+ if (x > 0) {
+ uint32_t nidx = idx - 1;
+ if (!visited[nidx] && imgData[nidx] == backgroundColor) {
+ visited[nidx] = 1;
+ outerBackgroundCount++;
+ stack.push_back((static_cast<uint32_t>(y) << 16) | (x - 1));
+ }
+ }
+ if (x < width - 1) {
+ uint32_t nidx = idx + 1;
+ if (!visited[nidx] && imgData[nidx] == backgroundColor) {
+ visited[nidx] = 1;
+ outerBackgroundCount++;
+ stack.push_back((static_cast<uint32_t>(y) << 16) | (x + 1));
+ }
+ }
+ }
+
+ uint32_t objectColorCount = totalPixels - maxCount;
+ uint32_t innerBackgroundCount =
+ maxCount - outerBackgroundCount;
+
+ if (objectColorCount == 0) return 0.0;
+
+ double ratio =
+ double(objectColorCount) / (innerBackgroundCount + objectColorCount);
+
+ return ratio > 0.3;
+}
+
void convertNaa2Tlv(const TFilePath &source, const TFilePath &dest,
const TFrameId &from, const TFrameId &to,
FrameTaskNotifier *frameNotifier, TPalette *palette,
@@ -764,7 +869,7 @@ void convertNaa2Tlv(const TFilePath &source, const TFilePath &dest,
} catch (...) {
}
- frameNotifier->notifyFrameCompleted(100 * (f + 1) / frames.size());
+ frameNotifier->notifyFrameCompleted(f+1);
}
if (removeUnusedStyles) converter.removeUnusedStyles(usedStyleIds);