From 5473c0b38bf97e84c7f418add7bbf2d9eb5288fb Mon Sep 17 00:00:00 2001 From: Bruno Date: Sat, 24 Apr 2021 13:14:03 +0200 Subject: [PATCH] Store image size in XML and discard loaded image if size does not match --- SessionCreator.cpp | 12 +++++++++++- SessionCreator.h | 3 ++- SessionVisitor.cpp | 9 +++++++++ Source.cpp | 5 ++++- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/SessionCreator.cpp b/SessionCreator.cpp index b2fe67c..6e54acc 100644 --- a/SessionCreator.cpp +++ b/SessionCreator.cpp @@ -552,6 +552,10 @@ FrameBufferImage *SessionLoader::XMLToImage(tinyxml2::XMLElement *xml) // if there is an Image mask stored XMLElement* imageNode = xml->FirstChildElement("Image"); if (imageNode) { + // get theoretical image size + int w = 0, h = 0; + imageNode->QueryIntAttribute("width", &w); + imageNode->QueryIntAttribute("height", &h); // if there is an internal array of data XMLElement* array = imageNode->FirstChildElement("array"); if (array) { @@ -566,6 +570,11 @@ FrameBufferImage *SessionLoader::XMLToImage(tinyxml2::XMLElement *xml) if (XMLElementDecodeArray(array, jpgimg.buffer, jpgimg.len) ) { // create and set the image from jpeg i = new FrameBufferImage(jpgimg); + // failed if wrong size + if ( (w>0 && h>0) && (i->width != w || i->height != h) ) { + delete i; + i = nullptr; + } } // free temporary buffer if (jpgimg.buffer) @@ -741,7 +750,8 @@ void SessionLoader::visit (Source& s) } xmlCurrent_ = sourceNode->FirstChildElement("Blending"); - if (xmlCurrent_) s.blendingShader()->accept(*this); + if (xmlCurrent_) + s.blendingShader()->accept(*this); xmlCurrent_ = sourceNode->FirstChildElement("Mask"); if (xmlCurrent_) { diff --git a/SessionCreator.h b/SessionCreator.h index 81154e8..5262a85 100644 --- a/SessionCreator.h +++ b/SessionCreator.h @@ -32,7 +32,7 @@ public: static bool isClipboard(const std::string &clipboard); static tinyxml2::XMLElement* firstSourceElement(const std::string &clipboard, tinyxml2::XMLDocument &xmlDoc); static void applyImageProcessing(const Source &s, const std::string &clipboard); - //TODO static void applyMask(const Source &s, std::string clipboard); + //TODO static void applyMask(const Source &s, const std::string &clipboard); // Elements of Scene void visit (Node& n) override; @@ -90,6 +90,7 @@ public: void load(const std::string& filename); static std::string info(const std::string& filename); +// static std::string thumbnail(const std::string& filename); }; #endif // SESSIONCREATOR_H diff --git a/SessionVisitor.cpp b/SessionVisitor.cpp index 70f8eb4..a9146c1 100644 --- a/SessionVisitor.cpp +++ b/SessionVisitor.cpp @@ -49,6 +49,13 @@ bool SessionVisitor::saveSession(const std::string& filename, Session *session) // source visitor (*iter)->accept(sv); + // get the thumbnail + FrameBufferImage *thumbnail = session->thumbnail(); + XMLElement *imageelement = SessionVisitor::ImageToXML(thumbnail, &xmlDoc); + if (imageelement) + sessionNode->InsertEndChild(imageelement); + delete thumbnail; + // 2. config of views XMLElement *views = xmlDoc.NewElement("Views"); xmlDoc.InsertEndChild(views); @@ -161,6 +168,8 @@ XMLElement *SessionVisitor::ImageToXML(FrameBufferImage *img, XMLDocument *doc) if (array) { // create an Image node to store the mask image imageelement = doc->NewElement("Image"); + imageelement->SetAttribute("width", img->width); + imageelement->SetAttribute("height", img->height); imageelement->InsertEndChild(array); } } diff --git a/Source.cpp b/Source.cpp index 5c2500b..4acf0e0 100644 --- a/Source.cpp +++ b/Source.cpp @@ -762,8 +762,11 @@ void Source::setMask(FrameBufferImage *img) // ask to update the source touch(); } - else + else { + // CANCEL mask if set to null mask_need_update_ = false; + maskshader_->mode = MaskShader::NONE; + } } bool Source::hasNode::operator()(const Source* elem) const