Skip to content

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

C

save fids of color model in palette (#2792)

shun-iwasawa1b50d3cb2b45
diff --git a/toonz/sources/common/tvrender/tpalette.cpp b/toonz/sources/common/tvrender/tpalette.cpp
index e9fc009d1..a7319adcd 100644
--- a/toonz/sources/common/tvrender/tpalette.cpp
+++ b/toonz/sources/common/tvrender/tpalette.cpp
@@ -54,6 +54,58 @@ const TColorStyle::PickedPosition stringToPoint(const std::string &string) {
return {TPoint(x, y), frame};
}
+// convert refLevelFids to string for saving
+std::string fidsToString(const std::vector<TFrameId> &fids) {
+ std::string str;
+ QList<int> numList;
+
+ for (const auto fid : fids) {
+ int num = fid.getNumber();
+ if (numList.isEmpty() || num == numList.last() + 1) {
+ numList.push_back(num);
+ continue;
+ }
+ // print
+ if (numList.count() == 1)
+ str += std::to_string(numList[0]) + ",";
+ else if (numList.count() == 2)
+ str +=
+ std::to_string(numList[0]) + "," + std::to_string(numList[1]) + ",";
+ else
+ str += std::to_string(numList[0]) + "-" + std::to_string(numList.last()) +
+ ",";
+
+ numList.clear();
+ numList.push_back(num);
+ }
+ if (numList.count() == 1)
+ str += std::to_string(numList[0]);
+ else if (numList.count() == 2)
+ str += std::to_string(numList[0]) + "," + std::to_string(numList[1]);
+ else
+ str += std::to_string(numList[0]) + "-" + std::to_string(numList.last());
+ return str;
+}
+
+// convert loaded string to refLevelFids
+std::vector<TFrameId> strToFids(std::string fidsStr) {
+ std::vector<TFrameId> ret;
+ QString str = QString::fromStdString(fidsStr);
+ QStringList chunks = str.split(',', QString::SkipEmptyParts);
+ for (const auto &chunk : chunks) {
+ QStringList nums = chunk.split('-', QString::SkipEmptyParts);
+ assert(nums.count() > 0 && nums.count() <= 2);
+ if (nums.count() == 1)
+ ret.push_back(TFrameId(nums[0].toInt()));
+ else { // nums.count() == 2
+ assert(nums[0].toInt() < nums[1].toInt());
+ for (int i = nums[0].toInt(); i <= nums[1].toInt(); i++)
+ ret.push_back(TFrameId(i));
+ }
+ }
+ return ret;
+}
+
} // namespace
//===================================================================
@@ -258,7 +310,7 @@ int TPalette::getFirstUnpagedStyle() const {
//-------------------------------------------------------------------
/*! Adding style with new styleId. Even if there are deleted styles in the
* palette, the new style will be appended to the end of the list.
-*/
+ */
int TPalette::addStyle(TColorStyle *style) {
// limit the number of cleanup style to 7
if (isCleanupPalette() && getStyleInPagesCount() >= 8) return -1;
@@ -357,9 +409,9 @@ void TPalette::erasePage(int index) {
m_pages.erase(m_pages.begin() + index);
int i;
for (i = 0; i < getPageCount(); i++) m_pages[i]->m_index = i;
- for (i = 0; i < page->getStyleCount(); i++)
+ for (i = 0; i < page->getStyleCount(); i++)
m_styles[page->getStyleId(i)].first = 0;
- page->m_palette = 0;
+ page->m_palette = 0;
delete page;
}
@@ -582,9 +634,16 @@ void TPalette::saveData(TOStream &os) {
os.child("version") << 71 << 0; // Inserting the version tag at this level.
// This is necessary to support the tpl format
if (m_refImgPath !=
- TFilePath()) // since it performs *untagged* stream output
- os.child("refImgPath")
- << m_refImgPath; // (the palette is streamed directly).
+ TFilePath()) { // since it performs *untagged* stream output
+ if (m_areRefLevelFidsSpecified) {
+ std::map<std::string, std::string> attr;
+ attr["fids"] = fidsToString(m_refLevelFids);
+ os.openChild("refImgPath", attr);
+ } else
+ os.openChild("refImgPath");
+ os << m_refImgPath; // (the palette is streamed directly).
+ os.closeChild();
+ }
os.openChild("styles");
{
@@ -650,9 +709,9 @@ void TPalette::saveData(TOStream &os) {
attributes.clear();
attributes["frame"] = std::to_string(frame);
- /*os.openChild("keycolor", attributes); // Up to Toonz 7.0, animations saved
- os << cs->getMainColor(); // the main color only
- os.closeChild();*/ //
+ /*os.openChild("keycolor", attributes); // Up
+ to Toonz 7.0, animations saved os << cs->getMainColor(); // the main
+ color only os.closeChild();*/ //
os.openChild("keyframe", attributes);
{
@@ -749,9 +808,17 @@ void TPalette::loadData(TIStream &is) {
}
is.closeChild();
}
- } else if (tagName == "refImgPath")
+ } else if (tagName == "refImgPath") {
+ std::string fidsStr;
+ if (is.getTagParam("fids", fidsStr)) {
+ m_areRefLevelFidsSpecified = true;
+ m_refLevelFids = strToFids(fidsStr);
+ } else {
+ m_areRefLevelFidsSpecified = false;
+ m_refLevelFids.clear();
+ }
is >> m_refImgPath;
- else if (tagName == "animation") {
+ } else if (tagName == "animation") {
while (!is.eos()) {
if (!is.openChild(tagName) || tagName != "style")
throw TException("palette, expected tag <style>");
@@ -827,7 +894,7 @@ void TPalette::loadData(TIStream &is) {
/*! if the palette is copied from studio palette, this function will modify the
* original names.
-*/
+ */
void TPalette::assign(const TPalette *src, bool isFromStudioPalette) {
if (src == this) return;
int i;
@@ -885,7 +952,7 @@ void TPalette::assign(const TPalette *src, bool isFromStudioPalette) {
cit != src->m_styleAnimationTable.end(); ++cit) {
StyleAnimation animation = cit->second;
for (j = animation.begin(); j != animation.end(); j++)
- j->second = j->second->clone();
+ j->second = j->second->clone();
m_styleAnimationTable[cit->first] = cit->second;
}
m_globalName = src->getGlobalName();
@@ -898,7 +965,7 @@ void TPalette::assign(const TPalette *src, bool isFromStudioPalette) {
//-------------------------------------------------------------------
/*!if the palette is merged from studio palette, this function will modify the
* original names.
-*/
+ */
void TPalette::merge(const TPalette *src, bool isFromStudioPalette) {
std::map<int, int> table;
int i;
@@ -931,7 +998,7 @@ void TPalette::merge(const TPalette *src, bool isFromStudioPalette) {
const Page *srcPage = src->getPage(i);
std::wstring pageName = srcPage->getName();
if (pageName == L"colors" && src->getPaletteName() != L"")
- pageName = src->getPaletteName();
+ pageName = src->getPaletteName();
Page *dstPage = addPage(pageName); //;
for (int j = 0; j < srcPage->getStyleCount(); j++) {
int styleId = srcPage->getStyleId(j);
@@ -958,8 +1025,10 @@ void TPalette::setRefImg(const TImageP &img) {
//-------------------------------------------------------------------
-void TPalette::setRefLevelFids(const std::vector<TFrameId> fids) {
- m_refLevelFids = fids;
+void TPalette::setRefLevelFids(const std::vector<TFrameId> fids,
+ bool specified) {
+ m_refLevelFids = fids;
+ m_areRefLevelFidsSpecified = specified;
}
//-------------------------------------------------------------------
diff --git a/toonz/sources/include/tpalette.h b/toonz/sources/include/tpalette.h
index 0e776687f..971f79a6f 100644
--- a/toonz/sources/include/tpalette.h
+++ b/toonz/sources/include/tpalette.h
@@ -216,6 +216,8 @@ private:
int m_currentStyleId;
+ bool m_areRefLevelFidsSpecified = false;
+
public:
TPalette();
~TPalette();
@@ -318,10 +320,11 @@ between RGBA color components.
m_version = v;
} //!< Sets the palette's version number
- void setRefLevelFids(const std::vector<TFrameId> fids); //!< Associates the
- //! list of frames \e
- //! fids to this
- //! palette.
+ void setRefLevelFids(const std::vector<TFrameId> fids,
+ bool specified); //!< Associates the
+ //! list of frames \e
+ //! fids to this palette.
+ //! When specified == true fids were specified by user on loading.
std::vector<TFrameId> getRefLevelFids(); //!< Returns the list of frames
//! associated to this palette.
diff --git a/toonz/sources/toonz/colormodelviewer.cpp b/toonz/sources/toonz/colormodelviewer.cpp
index 39deb09d3..730f77ed8 100644
--- a/toonz/sources/toonz/colormodelviewer.cpp
+++ b/toonz/sources/toonz/colormodelviewer.cpp
@@ -519,7 +519,7 @@ void ColorModelViewer::loadCurrentFrame() {
std::vector<TFrameId> fids;
fids.push_back(fid);
- currentPalette->setRefLevelFids(fids);
+ currentPalette->setRefLevelFids(fids, false);
m_currentRefImgPath = xl->getPath();
diff --git a/toonz/sources/toonzlib/palettecmd.cpp b/toonz/sources/toonzlib/palettecmd.cpp
index 48e4693bc..9269f4782 100644
--- a/toonz/sources/toonzlib/palettecmd.cpp
+++ b/toonz/sources/toonzlib/palettecmd.cpp
@@ -718,7 +718,7 @@ public:
assert(page);
m_pageName = page->getName();
m_styles.resize(page->getStyleCount());
- for (int i = 0; i < page->getStyleCount(); i++)
+ for (int i = 0; i < page->getStyleCount(); i++)
m_styles[i] = page->getStyleId(i);
}
void undo() const override {
@@ -840,7 +840,7 @@ int loadRefImage(TPaletteHandle *paletteHandle,
}
}
}
- levelPalette->setRefLevelFids(fids);
+ levelPalette->setRefLevelFids(fids, !frames.empty());
const TLevel::Table *table = level->getTable();
@@ -1018,7 +1018,7 @@ void PaletteCmd::removeReferenceImage(TPaletteHandle *paletteHandle) {
levelPalette->setRefImgPath(TFilePath());
std::vector<TFrameId> fids;
- levelPalette->setRefLevelFids(fids);
+ levelPalette->setRefLevelFids(fids, false);
levelPalette->setDirtyFlag(true);
paletteHandle->notifyPaletteChanged();
@@ -1242,7 +1242,7 @@ public:
}
int getHistoryType() override { return HistoryType::Palette; }
};
-}
+} // namespace
void PaletteCmd::organizePaletteStyle(
TPaletteHandle *paletteHandle, int styleId,
@@ -1335,7 +1335,7 @@ TPixel32 pickColor(TRasterImageP ri, const TPoint &rasterPoint) {
return TPixel32::Transparent;
}
-}
+} // namespace
void PaletteCmd::pickColorByUsingPickedPosition(TPaletteHandle *paletteHandle,
TImageP img, int frame) {
diff --git a/toonz/sources/toonzlib/txshsimplelevel.cpp b/toonz/sources/toonzlib/txshsimplelevel.cpp
index 687647699..54cb44daf 100644
--- a/toonz/sources/toonzlib/txshsimplelevel.cpp
+++ b/toonz/sources/toonzlib/txshsimplelevel.cpp
@@ -104,8 +104,8 @@ bool checkCreatorString(const QString &creator) {
if (pos >= 0 && len >= 4) {
QString v;
if (len > 4) v = creator.mid(pos + 3, len - 4);
- bool ok = true;
- mask = v.toInt(&ok, 16);
+ bool ok = true;
+ mask = v.toInt(&ok, 16);
}
}
return (mask & compatibility.neededMask) == compatibility.neededMask &&
@@ -169,8 +169,8 @@ void getIndexesRangefromFids(TXshSimpleLevel *level,
std::set<TFrameId>::const_iterator it;
for (it = fids.begin(); it != fids.end(); ++it) {
- int index = level->guessIndex(*it);
- if (index > toIndex) toIndex = index;
+ int index = level->guessIndex(*it);
+ if (index > toIndex) toIndex = index;
if (index < fromIndex) fromIndex = index;
}
}
@@ -909,13 +909,13 @@ void TXshSimpleLevel::loadData(TIStream &is) {
if (is.getTagParam("dpix", v)) xdpi = std::stod(v);
if (is.getTagParam("dpiy", v)) ydpi = std::stod(v);
if (xdpi != 0 && ydpi != 0) dpiPolicy = LevelProperties::DP_CustomDpi;
- std::string dpiType = is.getTagAttribute("dpiType");
- if (dpiType == "image") dpiPolicy = LevelProperties::DP_ImageDpi;
- if (is.getTagParam("type", v) && v == "s") type = TZI_XSHLEVEL;
- if (is.getTagParam("subsampling", v)) subsampling = std::stoi(v);
- if (is.getTagParam("premultiply", v)) doPremultiply = std::stoi(v);
+ std::string dpiType = is.getTagAttribute("dpiType");
+ if (dpiType == "image") dpiPolicy = LevelProperties::DP_ImageDpi;
+ if (is.getTagParam("type", v) && v == "s") type = TZI_XSHLEVEL;
+ if (is.getTagParam("subsampling", v)) subsampling = std::stoi(v);
+ if (is.getTagParam("premultiply", v)) doPremultiply = std::stoi(v);
if (is.getTagParam("antialias", v)) antialiasSoftness = std::stoi(v);
- if (is.getTagParam("whiteTransp", v)) whiteTransp = std::stoi(v);
+ if (is.getTagParam("whiteTransp", v)) whiteTransp = std::stoi(v);
m_properties->setDpiPolicy(dpiPolicy);
m_properties->setDpi(TPointD(xdpi, ydpi));
@@ -1046,9 +1046,8 @@ static TFilePath getLevelPathAndSetNameWithPsdLevelName(
if (removeFileName) wLevelName = list[1].toStdWString();
TLevelSet *levelSet = xshLevel->getScene()->getLevelSet();
- if (levelSet &&
- levelSet->hasLevel(
- wLevelName)) // levelSet should be asserted instead
+ if (levelSet && levelSet->hasLevel(
+ wLevelName)) // levelSet should be asserted instead
levelSet->renameLevel(xshLevel, wLevelName);
xshLevel->setName(wLevelName);
@@ -1209,10 +1208,35 @@ void TXshSimpleLevel::load() {
if (img && getPalette()) {
img->setPalette(0);
getPalette()->setRefImg(img);
- std::vector<TFrameId> fids;
- for (TLevel::Iterator it = level->begin(); it != level->end(); ++it)
- fids.push_back(it->first);
- getPalette()->setRefLevelFids(fids);
+ std::vector<TFrameId> fids = getPalette()->getRefLevelFids();
+ // in case the fids are specified by user
+ if (fids.size() > 0) {
+ // check existence of each fid
+ auto itr = fids.begin();
+ while (itr != fids.end()) {
+ bool found = false;
+ for (TLevel::Iterator it = level->begin(); it != level->end();
+ ++it) {
+ if (itr->getNumber() == it->first.getNumber()) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) // remove the fid if it does not exist in the level
+ itr = fids.erase(itr);
+ else
+ itr++;
+ }
+ }
+ // in case the fids are not specified, or all specified fids are
+ // absent
+ if (fids.size() == 0) {
+ for (TLevel::Iterator it = level->begin(); it != level->end();
+ ++it)
+ fids.push_back(it->first);
+ getPalette()->setRefLevelFids(fids, false);
+ } else if (fids.size() != getPalette()->getRefLevelFids().size())
+ getPalette()->setRefLevelFids(fids, true);
}
}
}
@@ -1556,8 +1580,8 @@ void TXshSimpleLevel::saveSimpleLevel(const TFilePath &decodedFp,
std::vector<TFrameId> fids;
getFids(fids);
- bool isLevelModified = getProperties()->getDirtyFlag();
- bool isPaletteModified = false;
+ bool isLevelModified = getProperties()->getDirtyFlag();
+ bool isPaletteModified = false;
if (getPalette()) isPaletteModified = getPalette()->getDirtyFlag();
if (isLevelModified || isPaletteModified) {
@@ -1800,11 +1824,11 @@ void TXshSimpleLevel::saveSimpleLevel(const TFilePath &decodedFp,
std::string TXshSimpleLevel::getImageId(const TFrameId &fid,
int frameStatus) const {
if (frameStatus < 0) frameStatus = getFrameStatus(fid);
- std::string prefix = "L";
+ std::string prefix = "L";
if (frameStatus & CleanupPreview)
prefix = "P";
else if ((frameStatus & (Scanned | Cleanupped)) == Scanned)
- prefix = "S";
+ prefix = "S";
std::string imageId = m_idBase + "_" + prefix + fid.expand();
return imageId;
}
@@ -2230,13 +2254,14 @@ TFilePath TXshSimpleLevel::getExistingHookFile(
int f, fCount = hookFiles.size();
for (f = 0; f != fCount; ++f) {
- fPattern = locals::getPattern(hookFiles[f]);
+ fPattern = locals::getPattern(hookFiles[f]);
if (fPattern < p) p = fPattern, h = f;
}
assert(h >= 0);
- return (h < 0) ? TFilePath() : decodedLevelPath.getParentDir() +
- TFilePath(hookFiles[h].toStdWString());
+ return (h < 0) ? TFilePath()
+ : decodedLevelPath.getParentDir() +
+ TFilePath(hookFiles[h].toStdWString());
}
//-----------------------------------------------------------------------------