OpenToonz - An open-source full-featured 2D animation creation software
C
refactor(refer fill): use RAII to manage refImg
Abel You5932a2e5b397
diff --git a/toonz/sources/include/toonz/fill.h b/toonz/sources/include/toonz/fill.hindex ba50d1692..fb1ca4e68 100644--- a/toonz/sources/include/toonz/fill.h+++ b/toonz/sources/include/toonz/fill.h@@ -21,6 +21,7 @@#include "tpixelcm.h"#include "traster.h"#include "trastercm.h"+#include "tropcm.h"#include "preferences.h"#define DEF_REGION_WITH_PAINT \@@ -170,4 +171,22 @@ else fill ink and paint in rect.bool onlyUnfilled);};+class RefImageGuard {+ 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));+ }+};++#endifdiff --git a/toonz/sources/toonzlib/fill.cpp b/toonz/sources/toonzlib/fill.cppindex 9d1df06fa..54b84f0a3 100644--- a/toonz/sources/toonzlib/fill.cpp+++ b/toonz/sources/toonzlib/fill.cpp@@ -38,10 +38,10 @@ inline int adjustFillDepth(const int &fillDepth) {}inline int threshMatte(int matte, int fillDepth) {- if (fillDepth == 255)- return matte;- else- return (matte < fillDepth) ? 255 : matte;+ if (fillDepth == 255)+ return matte;+ else+ return (matte < fillDepth) ? 255 : matte;}inline TPoint nearestInkNotDiagonal(const TRasterCM32P &r, const TPoint &p) {@@ -72,8 +72,7 @@ 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, bool refImagePut = false,- int paintAtClickPos = 0) {+ bool prevailing = true, int paintAtClickPos = 0) {int tone, oldtone;TPixelCM32 *pix, *pix0, *limit, *tmp_limit;@@ -132,9 +131,9 @@ void fillRow(const TRasterCM32P &r, const TPoint &p, int &xa, int &xb,preVailingInk = pix->getInk();if (oldPrevailingInk > 0 && preVailingInk != oldPrevailingInk) break;oldPrevailingInk = preVailingInk;+ }}}- }xb = p.x + pix - pix0 - 1;@@ -190,9 +189,9 @@ void fillRow(const TRasterCM32P &r, const TPoint &p, int &xa, int &xb,preVailingInk = pix->getInk();if (oldPrevailingInk > 0 && preVailingInk != oldPrevailingInk) break;oldPrevailingInk = preVailingInk;+ }}}- }xa = p.x + pix - pix0 + 1;@@ -398,9 +397,6 @@ public:: m_xa(xa), m_xb(xb), m_y(y), m_dy(dy) {}};-//-------------------------------------------------------------------------------//-----------------------------------------------------------------------------bool isPixelInSegment(const std::vector<std::pair<int, int>> &segments, int x) {@@ -572,29 +568,28 @@ bool fill(const TRasterCM32P &r, const FillParameters ¶ms,paintAtClickedPos = pix0->getPaint();} elsereturn false;++ /*- If clicked at pure ink pixel, return -*/+ if (pix0->isPureInk()) return false;+ if (Ref.getPointer() != nullptr && Ref->pixels(y)[x].m == 255) return false;+/*- If the "paint only transparent areas" option is enabled and the area is* already colored, return* -*/if (params.m_emptyOnly && pix0->getPaint() != 0 && !params.m_shiftFill)return false;- if (pix0->isPureInk()) return false;-- bool refImagePut = Ref.getPointer();- if (refImagePut) {- if (*(Ref->pixels(p.y) + p.x) != TPixel32(0, 0, 0, 0)) return false;- // !!!! To Save Undo Memory saved refer pixel would be cleared- // in rasterFillUndo::undo !!!!- // if (saver) saver->save(Ref->getBounds());- TRop::putRefImage(r, Ref);- }+ // Put reference image, will be automatically removed by RAII+ /* To Save Undo Memory, saved refer pixel would be cleared */+ RefImageGuard refGuard(r, Ref);fillDepth = adjustFillDepth(fillDepth);std::stack<FillSeed> seeds;+ // Also do fillInk for autoPaint style InkfillRow(r, p, xa, xb, paint, params.m_palette, saver, params.m_prevailing,- refImagePut, paintAtClickedPos);+ paintAtClickedPos);seeds.push(FillSeed(xa, xb, y, 1));seeds.push(FillSeed(xa, xb, y, -1));@@ -608,12 +603,12 @@ bool fill(const TRasterCM32P &r, const FillParameters ¶ms,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)();+ 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)();while (pix <= limit) {oldtone = threshTone(*oldpix, fillDepth);tone = threshTone(*pix, fillDepth);@@ -624,7 +619,7 @@ bool fill(const TRasterCM32P &r, const FillParameters ¶ms,(pix->getPaint() != pix->getInk() ||pix->getPaint() == paintAtClickedPos)) {fillRow(r, TPoint(x, y), xc, xd, paint, params.m_palette, saver,- params.m_prevailing, refImagePut, paintAtClickedPos);+ params.m_prevailing, paintAtClickedPos);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)@@ -644,8 +639,6 @@ bool fill(const TRasterCM32P &r, const FillParameters ¶ms,}if (oldxd > 0) seeds.push(FillSeed(oldxc, oldxd, y, dy));}-- if (refImagePut) TRop::eraseRefInks(r);return true;}diff --git a/toonz/sources/toonzlib/fillutil.cpp b/toonz/sources/toonzlib/fillutil.cppindex 1074bfaa4..efaeff541 100644--- a/toonz/sources/toonzlib/fillutil.cpp+++ b/toonz/sources/toonzlib/fillutil.cpp@@ -190,8 +190,6 @@ bool AreaFiller::rectFill(const TRect &rect, int color, bool onlyUnfilled,m_ras->lock();TRect r = m_bounds * rect;- if (m_refRas) TRop::putRefImage(m_ras, m_refRas);-int dx = r.x1 - r.x0;int dy = (r.y1 - r.y0) * m_wrap;if (dx < 2 || dy < 2) // rect degenere(area contenuta nulla), skippo.@@ -205,6 +203,9 @@ bool AreaFiller::rectFill(const TRect &rect, int color, bool onlyUnfilled,TRasterCM32P ras = m_ras->extract(rect.x0, rect.y0, rect.x1, rect.y1);TRasterCM32P backupRas = ras->clone();+ // Put reference image, will be automatically removed by RAII+ RefImageGuard refGuard(m_ras, m_refRas);+// fill borders with maxPaintFillParameters params;params.m_prevailing = false;@@ -278,9 +279,8 @@ bool AreaFiller::rectFill(const TRect &rect, int color, bool onlyUnfilled,processPixel(*pix, *bak, true, color, onlyUnfilled, fillPaints, fillInks);}- if (m_refRas) TRop::eraseRefInks(m_ras);-m_ras->unlock();+return true;}@@ -352,8 +352,6 @@ bool AreaFiller::rectFastFill(const TRect &rect, int color) {void AreaFiller::strokeFill(const TRect &rect, TStroke *stroke, int color,bool onlyUnfilled, bool fillPaints, bool fillInks) {- m_ras->lock();- if (m_refRas) TRop::putRefImage(m_ras, m_refRas);TRect box = rect;TRect bbox = m_ras->getBounds();box *= bbox;@@ -362,6 +360,9 @@ void AreaFiller::strokeFill(const TRect &rect, TStroke *stroke, int color,TRasterCM32P ras = m_ras->extract(box);TRasterCM32P backupRas = ras->clone();+ // Put reference image, will be automatically removed by RAII+ RefImageGuard refGuard(m_ras, m_refRas);+// std::vector<std::pair<TPoint, int>> seeds;// computeSeeds(m_ras, stroke, seeds);fillOutsideOfStroke(ras, stroke, TPixelCM32::getMaxPaint() - 1);@@ -382,7 +383,6 @@ void AreaFiller::strokeFill(const TRect &rect, TStroke *stroke, int color,processPixel(*pix, *bak, false, color, onlyUnfilled, fillPaints,fillInks);}- if (m_refRas) TRop::eraseRefInks(m_ras);m_ras->unlock();}