Skip to content

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

C

Add "Fill Holes" feature for Toonz Raster Level

Abel You3a42fd9da133
diff --git a/stuff/profiles/layouts/rooms/Default/menubar_template.xml b/stuff/profiles/layouts/rooms/Default/menubar_template.xml
index 168709309..ee3393532 100644
--- a/stuff/profiles/layouts/rooms/Default/menubar_template.xml
+++ b/stuff/profiles/layouts/rooms/Default/menubar_template.xml
@@ -132,6 +132,7 @@
<command>MI_LinesFade</command>
</menu>
<menu title="Optimize">
+ <command>MI_FillHoles</command>
<command>MI_RemoveEndpoints</command>
<command>MI_ConvertVectorToVector</command>
</menu>
diff --git a/toonz/sources/include/toonz/fill.h b/toonz/sources/include/toonz/fill.h
index cc4972c16..06f2b03a9 100644
--- a/toonz/sources/include/toonz/fill.h
+++ b/toonz/sources/include/toonz/fill.h
@@ -16,6 +16,14 @@ class TPalette;
#endif
#include <set>
+#include "ttilesaver.h"
+#include "timage.h"
+#include "tpalette.h"
+#include "tgeometry.h"
+#include "tpixelcm.h"
+#include "traster.h"
+#include "trastercm.h"
+#include "tpalette.h"
class FillParameters {
public:
@@ -84,6 +92,9 @@ void DVAPI fillautoInks(TRasterCM32P &r, TRect &rect,
void DVAPI fullColorFill(const TRaster32P &ras, const FillParameters &params,
TTileSaverFullColor *saver = 0);
+void DVAPI fillHoles(const TRasterCM32P &ras, const int size,
+ TTileSaverCM32 *saver = nullptr);
+
//=============================================================================
//! The class AreaFiller allows to fill a raster area, delimited by rect or
//! spline.
diff --git a/toonz/sources/toonz/CMakeLists.txt b/toonz/sources/toonz/CMakeLists.txt
index 8f6eb0da3..a75fbd1ce 100644
--- a/toonz/sources/toonz/CMakeLists.txt
+++ b/toonz/sources/toonz/CMakeLists.txt
@@ -10,6 +10,7 @@ set(MOC_HEADERS
batches.h
batchserversviewer.h
binarizepopup.h
+ fillholespopup.h
boardsettingspopup.h
brightnessandcontrastpopup.h
cachefxcommand.h
@@ -360,6 +361,7 @@ set(SOURCES
adjustlevelspopup.cpp
adjustthicknesspopup.cpp
binarizepopup.cpp
+ fillholespopup.cpp
brightnessandcontrastpopup.cpp
linesfadepopup.cpp
rendercommand.cpp
diff --git a/toonz/sources/toonz/fillholespopup.cpp b/toonz/sources/toonz/fillholespopup.cpp
new file mode 100644
index 000000000..0d0290f11
--- /dev/null
+++ b/toonz/sources/toonz/fillholespopup.cpp
@@ -0,0 +1,116 @@
+#include "fillholespopup.h"
+#include "toonz/fill.h"
+
+#include "toonzqt/dvdialog.h"
+#include "toonzqt/intfield.h"
+#include "toonzqt/menubarcommand.h"
+#include "menubarcommandids.h"
+#include "selectionutils.h"
+#include "toonz/txshlevel.h"
+#include "toonz/txshsimplelevel.h"
+#include "toonz/txshleveltypes.h"
+#include "ttoonzimage.h"
+#include "tools/toolutils.h"
+#include "tundo.h"
+#include "toonz/ttileset.h"
+#include "toonz/ttilesaver.h"
+#include "toonz/txsheethandle.h"
+
+#include <QPushButton>
+#include <QProgressBar>
+
+using namespace DVGui;
+using namespace ToolUtils;
+
+class TFillHolesUndo final : public TRasterUndo {
+ int m_size;
+
+public:
+ TFillHolesUndo(TTileSetCM32* tileSet, int size, TXshSimpleLevel* sl,
+ const TFrameId& fid)
+ : TRasterUndo(tileSet, sl, fid, false, false, 0), m_size(size) {};
+
+ void redo() const override {
+ TToonzImageP image = getImage();
+ if (!image) return;
+ TTool::Application* app = TTool::getApplication();
+ if (!app) return;
+ fillHoles(image->getRaster(), m_size);
+ app->getCurrentXsheet()->notifyXsheetChanged();
+ notifyImageChanged();
+ };
+ int getSize() const override {
+ return sizeof(*this) + TRasterUndo::getSize();
+ };
+ QString getToolName() override { return QString("Fill Holes"); };
+};
+
+FillHolesDialog::FillHolesDialog() : Dialog(0, true, true, "Fill Small Holes") {
+ setWindowTitle(tr("Fill Small Holes"));
+ setModal(false);
+ setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
+
+ beginVLayout();
+ m_size = new IntField(this);
+ m_size->setRange(1, 10);
+ m_size->setValue(3);
+ addWidget(tr("Size"), m_size);
+ endVLayout();
+
+ QPushButton* okBtn = new QPushButton(tr("Apply"), this);
+ okBtn->setDefault(true);
+ QPushButton* cancelBtn = new QPushButton(tr("Cancel"), this);
+ bool ret = connect(okBtn, SIGNAL(clicked()), this, SLOT(apply()));
+ ret = ret && connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
+ assert(ret);
+
+ addButtonBarWidget(okBtn, cancelBtn);
+};
+
+void FillHolesDialog::apply() {
+ std::set<TXshLevel*> levels;
+ SelectionUtils::getSelectedLevels(levels);
+ std::vector<std::pair<TXshSimpleLevel*, TFrameId>> Frames;
+ for (const auto& level : levels) {
+ if (level->getType() != TXshLevelType::TZP_XSHLEVEL) continue;
+ TXshSimpleLevel* sl = level->getSimpleLevel();
+ if (!sl) continue;
+ for (const TFrameId& fid : sl->getFids()) {
+ Frames.push_back(std::make_pair(sl, fid));
+ }
+ }
+ int size = Frames.size();
+ if (size == 0) {
+ DVGui::warning(tr("No Toonz Raster Level Selected"));
+ return;
+ } else {
+ m_progressDialog =
+ new ProgressDialog("Filling Holes...", QObject::tr("Cancel"), 0, size, this);
+ m_progressDialog->show();
+ }
+
+ int count = 0;
+ TUndoManager::manager()->beginBlock();
+ for (const auto& frame : Frames) {
+ TXshSimpleLevel* sl = frame.first;
+ TFrameId fid = frame.second;
+
+ TImageP img = sl->getFrame(fid, true);
+ TToonzImageP ti = TToonzImageP(img);
+ TRasterCM32P ras = ti->getRaster();
+ TTileSetCM32* tileSet = new TTileSetCM32(ras->getSize());
+ TTileSaverCM32* saver = new TTileSaverCM32(ras, tileSet);
+ if (m_progressDialog->wasCanceled()) break;
+ fillHoles(ras, m_size->getValue(), saver);
+ if (tileSet->getTileCount() != 0)
+ TUndoManager::manager()->add(
+ new TFillHolesUndo(tileSet, m_size->getValue(), sl, fid));
+ count++;
+ m_progressDialog->setValue(count);
+ }
+ TUndoManager::manager()->endBlock();
+ m_progressDialog->close();
+ Dialog::accept();
+}
+
+OpenPopupCommandHandler<FillHolesDialog> fillholesPopup(MI_FillHoles);
\ No newline at end of file
diff --git a/toonz/sources/toonz/fillholespopup.h b/toonz/sources/toonz/fillholespopup.h
new file mode 100644
index 000000000..a76e9c594
--- /dev/null
+++ b/toonz/sources/toonz/fillholespopup.h
@@ -0,0 +1,16 @@
+#include "toonzqt/dvdialog.h"
+#include "toonzqt/intfield.h"
+#include <QProgressBar>
+
+class FillHolesDialog final : public DVGui::Dialog {
+ Q_OBJECT
+
+ DVGui::IntField* m_size;
+ DVGui::ProgressDialog* m_progressDialog;
+
+public:
+ FillHolesDialog();
+
+protected slots:
+ void apply();
+};
\ No newline at end of file
diff --git a/toonz/sources/toonz/mainwindow.cpp b/toonz/sources/toonz/mainwindow.cpp
index 599d564eb..da59f85c0 100644
--- a/toonz/sources/toonz/mainwindow.cpp
+++ b/toonz/sources/toonz/mainwindow.cpp
@@ -1952,6 +1952,8 @@ void MainWindow::defineActions() {
QT_TRANSLATE_NOOP("MainWindow",
"Replace Vectors with Simplified Vectors"),
"");
+ createMenuLevelAction(MI_FillHoles, QT_TR_NOOP("&Fill Holes..."), "",
+ "Fill small holes in Toonz Raster Level");
createMenuLevelAction(MI_Tracking, QT_TR_NOOP("Tracking..."), "", "focus");
// Menu - Xsheet
diff --git a/toonz/sources/toonz/menubar.cpp b/toonz/sources/toonz/menubar.cpp
index eaf5178d1..891f62a75 100644
--- a/toonz/sources/toonz/menubar.cpp
+++ b/toonz/sources/toonz/menubar.cpp
@@ -1259,6 +1259,7 @@ QMenuBar *StackedMenuBar::createFullMenuBar() {
{
addMenuItem(optimizeMenu, MI_RemoveEndpoints);
addMenuItem(optimizeMenu, MI_ConvertVectorToVector);
+ addMenuItem(optimizeMenu, MI_FillHoles);
}
QMenu *convertMenu = levelMenu->addMenu(tr("Convert"));
{
diff --git a/toonz/sources/toonz/menubarcommandids.h b/toonz/sources/toonz/menubarcommandids.h
index c2f102313..0040e6ca1 100644
--- a/toonz/sources/toonz/menubarcommandids.h
+++ b/toonz/sources/toonz/menubarcommandids.h
@@ -107,6 +107,7 @@
#define MI_AdjustLevels "MI_AdjustLevels"
#define MI_AdjustThickness "MI_AdjustThickness"
#define MI_Binarize "MI_Binarize"
+#define MI_FillHoles "MI_FillHoles"
#define MI_LinesFade "MI_LinesFade"
#define MI_OpenXshView "MI_OpenXshView"
#define MI_OpenTimelineView "MI_OpenTimelineView"
diff --git a/toonz/sources/toonzlib/fill.cpp b/toonz/sources/toonzlib/fill.cpp
index 31401c7c1..f3d77f0d5 100644
--- a/toonz/sources/toonzlib/fill.cpp
+++ b/toonz/sources/toonzlib/fill.cpp
@@ -359,6 +359,92 @@ bool floodCheck(const TPixel32 &clickColor, const TPixel32 *targetPix,
return !doesStemFill(clickColor, targetPix, fillDepth2);
}
+bool scanTransparentRegion(TRasterCM32P ras, int x, int y,
+ std::vector<TPoint> &points, bool *visited,
+ const int maxSize) {
+ if (!ras) return false;
+ int lx = ras->getLx(), ly = ras->getLy();
+ if (x < 0 || y < 0 || x >= lx || y >= ly) return true;
+ if (visited[y * lx + x]) return true;
+
+ TPixelCM32 *pix = ras->pixels(y) + x;
+ if (pix->getPaint() != 0 || pix->isPureInk()) return true;
+ if (points.size() + 1 > maxSize) return false;
+
+ visited[y * lx + x] = true;
+ points.push_back(TPoint(x, y));
+
+ bool ret = scanTransparentRegion(ras, x + 1, y, points, visited, maxSize) &&
+ scanTransparentRegion(ras, x - 1, y, points, visited, maxSize) &&
+ scanTransparentRegion(ras, x, y + 1, points, visited, maxSize) &&
+ scanTransparentRegion(ras, x, y - 1, points, visited, maxSize);
+ ret = ret && points.size() <= maxSize;
+ if (!ret) visited[y * lx + x] = false;
+ return ret;
+}
+
+int getMostFrequentNeighborStyleId(TRasterCM32P ras,
+ const std::vector<TPoint> &points,
+ const bool *visited) {
+ if (!ras) return -1;
+
+ int lx = ras->getLx();
+ int ly = ras->getLy();
+
+ std::map<int, int> styleCount;
+
+ const std::vector<TPoint> directions = {TPoint(1, -1), TPoint(1, 1),
+ TPoint(-1, 1), TPoint(1, 1)};
+
+ for (const TPoint &p : points) {
+ for (const TPoint &d : directions) {
+ int nx = p.x + d.x;
+ int ny = p.y + d.y;
+ if (nx < 0 || ny < 0 || nx >= lx || ny >= ly) continue;
+ if (visited[ny * lx + nx]) continue;
+
+ TPixelCM32 *pix = ras->pixels(ny) + nx;
+ if (pix->isPureInk()) continue;
+ int styleId = pix->getPaint();
+ styleCount[styleId]++;
+ }
+ }
+
+ int maxStyleId = 0;
+ int maxCount = 0;
+ for (const auto &entry : styleCount) {
+ if (entry.second > maxCount) {
+ maxCount = entry.second;
+ maxStyleId = entry.first;
+ }
+ }
+ if (maxStyleId) return maxStyleId;
+
+ const std::vector<TPoint> directions2 = {TPoint(0, -1), TPoint(0, 1),
+ TPoint(-1, 0), TPoint(1, 0)};
+
+ for (const TPoint &p : points) {
+ for (const TPoint &d : directions2) {
+ int nx = p.x + d.x;
+ int ny = p.y + d.y;
+ if (nx < 0 || ny < 0 || nx >= lx || ny >= ly) continue;
+ if (visited[ny * lx + nx]) continue;
+
+ TPixelCM32 *pix = ras->pixels(ny) + nx;
+ int styleId = pix->getInk();
+ styleCount[styleId]++;
+ }
+ }
+
+ for (const auto &entry : styleCount) {
+ if (entry.second > maxCount) {
+ maxCount = entry.second;
+ maxStyleId = entry.first;
+ }
+ }
+ return maxStyleId;
+}
+
//-----------------------------------------------------------------------------
} // namespace
//-----------------------------------------------------------------------------
@@ -804,3 +890,43 @@ void fullColorFill(const TRaster32P &ras, const FillParameters &params,
}
}
}
+
+void fillHoles(const TRasterCM32P &ras, const int maxSize,
+ TTileSaverCM32 *saver) {
+ int lx = ras->getLx();
+ int ly = ras->getLy();
+ bool *visited = new bool[lx * ly]{false};
+
+ auto isEmpty = [&](int x, int y) -> bool {
+ TPixelCM32 *pix = ras->pixels(y) + x;
+ return !pix->getPaint() && !pix->isPureInk() && !visited[y * lx + x];
+ };
+
+ for (int y = 0; y < ly; y++) {
+ int emptyCount = 0;
+ for (int x = 0; x < lx; x++) {
+ if (isEmpty(x, y)) {
+ emptyCount++;
+ continue;
+ } else if (emptyCount && emptyCount <= maxSize) {
+ std::vector<TPoint> points;
+ int startX = x - emptyCount;
+ if (scanTransparentRegion(ras, startX, y, points, visited, maxSize)) {
+ int paint = getMostFrequentNeighborStyleId(ras, points, visited);
+ if (paint)
+ for (const TPoint &p : points) {
+ if (saver) saver->save(p);
+ if (paint == 1) {
+ ras->pixels(p.y)[p.x].setInk(paint);
+ ras->pixels(p.y)[p.x].setTone(0);
+ } else
+ ras->pixels(p.y)[p.x].setPaint(paint);
+ }
+ }
+ }
+ emptyCount = 0;
+ }
+ }
+
+ delete[] visited;
+}