From 0bbdf2b289efbe5ed3b1b790167d9d14adb0f602 Mon Sep 17 00:00:00 2001 From: Rodney Date: Aug 26 2019 09:11:11 +0000 Subject: Merge pull request #2614 from shun-iwasawa/vectorize_tlv_with_mypaint_styles Fix "Convert to Vectors" command for TLV with MyPaint Styles --- diff --git a/toonz/sources/toonz/vectorizerpopup.cpp b/toonz/sources/toonz/vectorizerpopup.cpp index 0b1335f..91b0b52 100644 --- a/toonz/sources/toonz/vectorizerpopup.cpp +++ b/toonz/sources/toonz/vectorizerpopup.cpp @@ -190,6 +190,17 @@ void getSelectedFids(std::vector &fids, TXshSimpleLevel *level, for (fst = fidsSet.begin(); fst != fsEnd; ++fst) fids.push_back(*fst); } +// Toonz Raster Level may have palette including MyPaint styles, +// which cannot be rendered in vector levels. +// In such case replace MyPaint styles by solid color styles. +void replaceMyPaintBrushStyles(TPalette *palette) { + for (int s = 0; s < palette->getStyleCount(); s++) { + TColorStyle *style = palette->getStyle(s); + if (style->getTagId() == 4001) // TMyPaintBrushStyle + palette->setStyle(s, style->getMainColor()); + } +} + } // namespace //***************************************************************************** @@ -288,10 +299,12 @@ void Vectorizer::setLevel(const TXshSimpleLevelP &level) { vl->setName(levelName); } - TPalette *palette = 0; - if (sl->getType() == TZP_XSHLEVEL) palette = sl->getPalette(); - - palette = palette ? palette->clone() : new TPalette; + TPalette *palette = 0; + if (sl->getType() == TZP_XSHLEVEL) { + palette = sl->getPalette()->clone(); + replaceMyPaintBrushStyles(palette); + } else + palette = new TPalette; palette->setPaletteName(vl->getName()); vl->setPalette(palette); diff --git a/toonz/sources/toonz/vectorizerswatch.cpp b/toonz/sources/toonz/vectorizerswatch.cpp index 2cfc4dd..dcdbe44 100644 --- a/toonz/sources/toonz/vectorizerswatch.cpp +++ b/toonz/sources/toonz/vectorizerswatch.cpp @@ -1,7 +1,6 @@ // TnzCore includes -#include "tpalette.h" #include "trasterimage.h" #include "ttoonzimage.h" #include "tvectorimage.h" @@ -108,6 +107,13 @@ struct VectorizationBuilder final : public TThread::Executor { QList m_listeners; + // Toonz Raster Level may have palette including MyPaint styles, + // which cannot be rendered in vector levels. + // In such case prepare an alternative palette in which MyPaint styles + // are converted to solid color styles. + TPaletteP m_substitutePalette; + void prepareSupstitutePaletteIfNeeded(int row, int col); + public: VectorizationBuilder() : m_row(-1), m_col(-1), m_image(), m_done(false), m_submitted(false) { @@ -159,7 +165,8 @@ bool VectorizationBuilder::update(TImageP &img) { if (row != m_row || col != m_col) { m_row = row, m_col = col; m_done = false; - addTask(new VectorizationSwatchTask(row, col)); + prepareSupstitutePaletteIfNeeded(row, col); + addTask(new VectorizationSwatchTask(row, col, m_substitutePalette)); computing = true; } @@ -170,8 +177,9 @@ bool VectorizationBuilder::update(TImageP &img) { //----------------------------------------------------------------------------- bool VectorizationBuilder::invalidate(TImageP &img) { - m_row = m_col = -1; - m_done = false; + m_row = m_col = -1; + m_done = false; + m_substitutePalette = TPaletteP(); return update(img); } @@ -190,12 +198,31 @@ void VectorizationBuilder::notifyDone(TImageP img) { } } +//----------------------------------------------------------------------------- + +void VectorizationBuilder::prepareSupstitutePaletteIfNeeded(int row, int col) { + // Retrieve the image to be vectorized + TToonzImageP ti(getXsheetImage(row, col)); + if (!ti) return; + m_substitutePalette = ti->getPalette()->clone(); + bool found = false; + for (int s = 0; s < m_substitutePalette->getStyleCount(); s++) { + TColorStyle *style = m_substitutePalette->getStyle(s); + if (style->getTagId() == 4001) { // TMyPaintBrushStyle + found = true; + m_substitutePalette->setStyle(s, style->getMainColor()); + } + } + if (!found) m_substitutePalette = TPaletteP(); +} + //***************************************************************************** // VectorizationSwatchTask implementation //***************************************************************************** -VectorizationSwatchTask::VectorizationSwatchTask(int row, int col) - : m_row(row), m_col(col) { +VectorizationSwatchTask::VectorizationSwatchTask(int row, int col, + TPaletteP substitutePalette) + : m_row(row), m_col(col), m_substitutePalette(substitutePalette) { // Establish connections to default slots; the started one must be blocking, // so that run() awaits until it has been performed. @@ -237,7 +264,7 @@ void VectorizationSwatchTask::run() { TPointD center, dpi; if (TToonzImageP ti = m_image) { - palette = ti->getPalette(); + palette = (m_substitutePalette) ? m_substitutePalette : ti->getPalette(); center = ti->getRaster()->getCenterD(); ti->getDpi(dpi.x, dpi.y); } else if (TRasterImageP ri = m_image) { diff --git a/toonz/sources/toonz/vectorizerswatch.h b/toonz/sources/toonz/vectorizerswatch.h index 6827d9d..ce5f870 100644 --- a/toonz/sources/toonz/vectorizerswatch.h +++ b/toonz/sources/toonz/vectorizerswatch.h @@ -3,6 +3,9 @@ #ifndef VECTORIZER_SWATCH_H #define VECTORIZER_SWATCH_H +// TnzCore includes +#include "tpalette.h" + // Toonz includes #include "tthread.h" #include "timage.h" @@ -95,8 +98,11 @@ class VectorizationSwatchTask final : public TThread::Runnable { std::unique_ptr m_config; + TPaletteP m_substitutePalette; + public: - VectorizationSwatchTask(int row, int col); + VectorizationSwatchTask(int row, int col, + TPaletteP substitutePalette = TPaletteP()); void run() override; void onStarted(TThread::RunnableP task) override;