Skip to content

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

C

Optimize toonz raster fill performance

Abel Youb5e6f92810ab
diff --git a/toonz/sources/include/toonz/fill.h b/toonz/sources/include/toonz/fill.h
index fb1ca4e68..a83ad5e6d 100644
--- a/toonz/sources/include/toonz/fill.h
+++ b/toonz/sources/include/toonz/fill.h
@@ -11,7 +11,7 @@
#else
#define DVAPI DV_IMPORT_API
#define DVVAR DV_IMPORT_VAR
-#endif
+#endif
#include <set>
#include "ttilesaver.h"
@@ -42,6 +42,8 @@ public:
TPoint m_p;
TPalette *m_palette; // Whether to fill autoPaint Ink
bool m_prevailing;
+ bool m_defRegionWithPaint;
+ bool m_usePrevailingReferFill;
FillParameters()
: m_styleId(0)
@@ -53,7 +55,12 @@ public:
, m_p()
, m_shiftFill(false)
, m_palette(0)
- , m_prevailing(true) {}
+ , m_prevailing(true)
+ , m_defRegionWithPaint(true)
+ , m_usePrevailingReferFill(false) {
+ m_defRegionWithPaint = DEF_REGION_WITH_PAINT;
+ m_usePrevailingReferFill = USE_PREVAILING_REFER_FILL;
+ }
FillParameters(const FillParameters &params)
: m_styleId(params.m_styleId)
, m_fillType(params.m_fillType)
@@ -64,7 +71,9 @@ public:
, m_p(params.m_p)
, m_shiftFill(params.m_shiftFill)
, m_palette(params.m_palette)
- , m_prevailing(params.m_prevailing) {}
+ , m_prevailing(params.m_prevailing)
+ , m_defRegionWithPaint(params.m_defRegionWithPaint)
+ , m_usePrevailingReferFill(params.m_usePrevailingReferFill) {}
};
//=============================================================================
@@ -130,11 +139,11 @@ Fill \b rect in raster with \b color.
else if \b fillInks is false fill only paint delimited by ink;
else fill ink and paint in rect.
*/
- bool rectFill(const TRect &rect, int color, bool onlyUnfilled,
+ bool rectFill(const TRect &rect, int color, bool onlyUnfilled,
bool fillPaints, bool fillInks);
// Only for Fill Check
- bool rectFastFill(const TRect& rect, int color);
+ bool rectFastFill(const TRect &rect, int color);
/*!
Fill the raster region contained in spline \b s with \b color.
@@ -148,7 +157,8 @@ else fill ink and paint in region contained in spline.
private:
const void processPixel(TPixelCM32 &pix, const TPixelCM32 &bak, bool invert,
int color, bool onlyUnfilled, bool fillPaints,
- bool fillInks);
+ bool fillInks, bool defRegionWithPaint,
+ bool usePrevailingReferFill);
};
class DVAPI FullColorAreaFiller {
@@ -172,21 +182,20 @@ else fill ink and paint in rect.
};
class RefImageGuard {
- const TRasterCM32P& m_r;
- bool m_refPlaced;
+ const TRasterCM32P &m_r;
+ bool m_refPlaced;
public:
- RefImageGuard(const TRasterCM32P& raster, const TRaster32P& Ref)
- : m_r(raster), m_refPlaced(Ref.getPointer() != nullptr) {
- m_r->lock();
- if (m_refPlaced) TRop::putRefImage(const_cast<TRasterCM32P&>(m_r), Ref);
- }
-
- ~RefImageGuard() {
- m_r->unlock();
- if (m_refPlaced) TRop::eraseRefInks(const_cast<TRasterCM32P&>(m_r));
- }
+ RefImageGuard(const TRasterCM32P &raster, const TRaster32P &Ref)
+ : m_r(raster), m_refPlaced(Ref.getPointer() != nullptr) {
+ m_r->lock();
+ if (m_refPlaced) TRop::putRefImage(const_cast<TRasterCM32P &>(m_r), Ref);
+ }
+
+ ~RefImageGuard() {
+ m_r->unlock();
+ if (m_refPlaced) TRop::eraseRefInks(const_cast<TRasterCM32P &>(m_r));
+ }
};
-
#endif
diff --git a/toonz/sources/toonzlib/fill.cpp b/toonz/sources/toonzlib/fill.cpp
index 3db57b885..c9b3ce333 100644
--- a/toonz/sources/toonzlib/fill.cpp
+++ b/toonz/sources/toonzlib/fill.cpp
@@ -72,7 +72,9 @@ inline TPoint nearestInkNotDiagonal(const TRasterCM32P &r, const TPoint &p) {
void fillRow(const TRasterCM32P &r, const TPoint &p, int &xa, int &xb,
int paint, TPalette *palette, TTileSaverCM32 *saver,
- bool prevailing = true, int paintAtClickPos = 0) {
+ bool prevailing = true, int paintAtClickPos = 0,
+ bool defRegionWithPaint = true,
+ bool usePrevailingReferFill = false) {
int tone, oldtone;
TPixelCM32 *pix, *pix0, *limit, *tmp_limit;
@@ -87,8 +89,7 @@ void fillRow(const TRasterCM32P &r, const TPoint &p, int &xa, int &xb,
for (; pix <= limit; pix++) {
if (pix->getPaint() == paint) break;
tone = pix->getTone();
- if (tone == 0 ||
- (pix->getPaint() != paintAtClickPos && DEF_REGION_WITH_PAINT))
+ if (tone == 0 || (pix->getPaint() != paintAtClickPos && defRegionWithPaint))
break;
// prevent fill area from protruding behind the colored line
if (tone > oldtone) {
@@ -146,8 +147,7 @@ void fillRow(const TRasterCM32P &r, const TPoint &p, int &xa, int &xb,
for (pix--; pix >= limit; pix--) {
if (pix->getPaint() == paint) break;
tone = pix->getTone();
- if (tone == 0 ||
- pix->getPaint() != paintAtClickPos && DEF_REGION_WITH_PAINT)
+ if (tone == 0 || pix->getPaint() != paintAtClickPos && defRegionWithPaint)
break;
// prevent fill area from protruding behind the colored line
if (tone > oldtone) {
@@ -202,21 +202,21 @@ void fillRow(const TRasterCM32P &r, const TPoint &p, int &xa, int &xb,
TPixelCM32 *lastPix = pix;
for (n = 0; n < xb - xa + 1; n++, lastPix = pix, pix++) {
/*--- Check if out of range ---*/
- if (pix->getPaint() != paintAtClickPos && DEF_REGION_WITH_PAINT) continue;
+ if (pix->getPaint() != paintAtClickPos && defRegionWithPaint) continue;
/*--- Paint refer lines ---*/
- if (lastPix->getInk() != paint && !DEF_REGION_WITH_PAINT) {
+ if (lastPix->getInk() != paint && !defRegionWithPaint) {
if (pix->getInk() == TPixelCM32::getMaxInk() &&
lastPix->getInk() != TPixelCM32::getMaxInk() && pix->isPureInk()) {
pix->setInk(paint);
- if (USE_PREVAILING_REFER_FILL) pix->setPaint(paint);
+ if (usePrevailingReferFill) pix->setPaint(paint);
break;
} else if (pix->getInk() != TPixelCM32::getMaxInk() &&
lastPix->getInk() == TPixelCM32::getMaxInk() &&
lastPix->isPureInk()) {
lastPix->setInk(paint);
- if (USE_PREVAILING_REFER_FILL) lastPix->setPaint(paint);
- } else if (USE_PREVAILING_REFER_FILL &&
+ if (usePrevailingReferFill) lastPix->setPaint(paint);
+ } else if (usePrevailingReferFill &&
pix->getInk() == TPixelCM32::getMaxInk() && pix->isPureInk())
continue;
}
@@ -237,14 +237,14 @@ void fillRow(const TRasterCM32P &r, const TPoint &p, int &xa, int &xb,
}
/*--- Do the normal paint ---*/
- if (pix->getInk() == TPixelCM32::getMaxInk() &&
- !USE_PREVAILING_REFER_FILL && pix->isPureInk())
+ if (pix->getInk() == TPixelCM32::getMaxInk() && !usePrevailingReferFill &&
+ pix->isPureInk())
continue;
pix->setPaint(paint);
}
// Make sure the Surround ref Ink Pixels can be painted
- if (p.y > 0 && p.y < r->getLy() - 1 && !DEF_REGION_WITH_PAINT) {
+ if (p.y > 0 && p.y < r->getLy() - 1 && !defRegionWithPaint) {
pix = line + xa;
for (n = 0; n < xb - xa + 1; n++, pix++) {
if (pix->isPurePaint()) {
@@ -252,11 +252,11 @@ void fillRow(const TRasterCM32P &r, const TPoint &p, int &xa, int &xb,
TPixelCM32 *dnPix = pix + r->getWrap();
if (upPix->getInk() == TPixelCM32::getMaxInk()) {
upPix->setInk(paint);
- if (USE_PREVAILING_REFER_FILL) upPix->setPaint(paint);
+ if (usePrevailingReferFill) upPix->setPaint(paint);
}
if (dnPix->getInk() == TPixelCM32::getMaxInk()) {
dnPix->setInk(paint);
- if (USE_PREVAILING_REFER_FILL) dnPix->setPaint(paint);
+ if (usePrevailingReferFill) dnPix->setPaint(paint);
}
}
}
@@ -549,6 +549,8 @@ bool fill(const TRasterCM32P &r, const FillParameters &params,
int paint = params.m_styleId;
int fillDepth =
params.m_shiftFill ? params.m_maxFillDepth : params.m_minFillDepth;
+ bool defRegionWithPaint = params.m_defRegionWithPaint;
+ bool usePrevailingReferFill = params.m_usePrevailingReferFill;
/*-- getBounds returns the entire image --*/
TRect bbbox = r->getBounds();
@@ -590,37 +592,42 @@ bool fill(const TRasterCM32P &r, const FillParameters &params,
// Also do fillInk for autoPaint style Ink
fillRow(r, p, xa, xb, paint, params.m_palette, saver, params.m_prevailing,
- paintAtClickedPos);
+ paintAtClickedPos, defRegionWithPaint, usePrevailingReferFill);
seeds.push(FillSeed(xa, xb, y, 1));
seeds.push(FillSeed(xa, xb, y, -1));
+ int lasty = y;
+ int wrap = r->getWrap();
+ TPixelCM32 *line = r->pixels(y);
while (!seeds.empty()) {
FillSeed fs = seeds.top();
seeds.pop();
-
xa = fs.m_xa;
xb = fs.m_xb;
oldy = fs.m_y;
dy = fs.m_dy;
y = oldy + dy;
if (y > bbbox.y1 || y < bbbox.y0) continue;
- pix = pix0 = r->pixels(y) + xa;
- limit = r->pixels(y) + xb;
- oldpix = r->pixels(oldy) + xa;
- x = xa;
- oldxd = (std::numeric_limits<int>::min)();
- oldxc = (std::numeric_limits<int>::max)();
+ line += (y - lasty) * wrap;
+ pix = line + xa;
+ limit = line + xb;
+ oldpix = pix - dy * wrap;
+ x = xa;
+ oldxd = (std::numeric_limits<int>::min)();
+ oldxc = (std::numeric_limits<int>::max)();
+ lasty = y;
while (pix <= limit) {
oldtone = threshTone(*oldpix, fillDepth);
tone = threshTone(*pix, fillDepth);
// the last condition is added in order to prevent fill area from
// protruding behind the colored line
- if (pix->getPaint() != paint && tone <= oldtone && tone != 0 &&
- (pix->getPaint() == paintAtClickedPos || !DEF_REGION_WITH_PAINT) &&
- (pix->getPaint() != pix->getInk() ||
- pix->getPaint() == paintAtClickedPos)) {
+ int pixPaint = pix->getPaint();
+ if (pixPaint != paint && tone <= oldtone && tone != 0 &&
+ (pixPaint == paintAtClickedPos || !defRegionWithPaint) &&
+ (pixPaint != pix->getInk() || pixPaint == paintAtClickedPos)) {
fillRow(r, TPoint(x, y), xc, xd, paint, params.m_palette, saver,
- params.m_prevailing, paintAtClickedPos);
+ params.m_prevailing, paintAtClickedPos, defRegionWithPaint,
+ usePrevailingReferFill);
if (xc < xa) seeds.push(FillSeed(xc, xa - 1, y, -dy));
if (xd > xb) seeds.push(FillSeed(xb + 1, xd, y, -dy));
if (oldxd >= xc - 1)
diff --git a/toonz/sources/toonzlib/fillutil.cpp b/toonz/sources/toonzlib/fillutil.cpp
index 256478b0c..21270d5b4 100644
--- a/toonz/sources/toonzlib/fillutil.cpp
+++ b/toonz/sources/toonzlib/fillutil.cpp
@@ -295,6 +295,8 @@ bool AreaFiller::rectFill(const TRect &rect, int color, bool onlyUnfilled,
}
}
+ bool defRegionWithPaint = params.m_defRegionWithPaint;
+ bool usePrevailingReferFill = params.m_usePrevailingReferFill;
for (int y = 0; y < ly; ++y) {
TPixelCM32 *pix = ras->pixels(y);
TPixelCM32 *bak = backupRas->pixels(y);
@@ -304,7 +306,8 @@ bool AreaFiller::rectFill(const TRect &rect, int color, bool onlyUnfilled,
pix->setInk(color); // Paint all inks
else if (m_palette && m_palette->getStyle(pix->getInk())->getFlags() != 0)
inkFill(ras, TPoint(x, y), color, 0);
- processPixel(*pix, *bak, true, color, onlyUnfilled, fillPaints, fillInks);
+ processPixel(*pix, *bak, true, color, onlyUnfilled, fillPaints, fillInks,
+ defRegionWithPaint, usePrevailingReferFill);
}
}
@@ -380,7 +383,8 @@ bool AreaFiller::rectFastFill(const TRect &rect, int color) {
//-----------------------------------------------------------------------------
void AreaFiller::strokeFill(const TRect &rect, TStroke *stroke, int color,
- bool onlyUnfilled, bool fillPaints, bool fillInks) {
+ const bool onlyUnfilled, const bool fillPaints,
+ const bool fillInks) {
TRect box = rect;
TRect bbox = m_ras->getBounds();
box *= bbox;
@@ -407,29 +411,33 @@ void AreaFiller::strokeFill(const TRect &rect, TStroke *stroke, int color,
TPixelCM32::getMaxPaint(), m_palette, color, fillInks);
app.removeStroke(0);
+ bool defRegionWithPaint = DEF_REGION_WITH_PAINT;
+ bool usePrevailingReferFill = USE_PREVAILING_REFER_FILL;
int lx = box.getLx(), ly = box.getLy();
for (int y = 0; y < ly; ++y) {
TPixelCM32 *pix = ras->pixels(y);
TPixelCM32 *bak = backupRas->pixels(y);
for (int x = 0; x < lx; ++x, ++pix, ++bak) {
- processPixel(*pix, *bak, false, color, onlyUnfilled, fillPaints,
- fillInks);
+ processPixel(*pix, *bak, false, color, onlyUnfilled, fillPaints, fillInks,
+ defRegionWithPaint, usePrevailingReferFill);
}
}
m_ras->unlock();
}
-const void AreaFiller::processPixel(
- TPixelCM32 &pix, const TPixelCM32 &bak, bool invert, int color,
- bool onlyUnfilled, bool fillPaints,
- bool fillInks) { /*--- Process Area need to be painted */
+const void AreaFiller::processPixel(TPixelCM32 &pix, const TPixelCM32 &bak,
+ bool invert, int color, bool onlyUnfilled,
+ bool fillPaints, bool fillInks,
+ bool defRegionWithPaint,
+ bool usePrevailingReferFill) {
+ /*--- Process Area need to be painted */
if (invert ? pix.getPaint() != TPixelCM32::getMaxPaint()
: pix.getPaint() == TPixelCM32::getMaxPaint()) {
/*--- Process refer Lines ---*/
// paint refer lines
if (!invert && pix.getInk() == TPixelCM32::getMaxInk() - 1) // !!!!
pix.setInk(TPixelCM32::getMaxInk());
- if (pix.getInk() == TPixelCM32::getMaxInk() && !DEF_REGION_WITH_PAINT)
+ if (pix.getInk() == TPixelCM32::getMaxInk() && !defRegionWithPaint)
pix.setInk(color);
/*--- Process ALL PAINT ---*/
@@ -437,7 +445,7 @@ const void AreaFiller::processPixel(
// do not paint if not empty pixel
if (onlyUnfilled && bak.getPaint() != 0) pix.setPaint(bak.getPaint());
// do not paint under refer pure INK
- else if (!USE_PREVAILING_REFER_FILL &&
+ else if (!usePrevailingReferFill &&
pix.getInk() == TPixelCM32::getMaxInk() && pix.isPureInk())
pix.setPaint(bak.getPaint());
// restore paint under maxPaint