Cleanup image saving and loading in xml session

This commit is contained in:
Bruno
2021-04-22 23:37:39 +02:00
parent da64172848
commit 97e7e5f4a1
4 changed files with 68 additions and 44 deletions

View File

@@ -544,6 +544,40 @@ void SessionLoader::XMLToSourcecore( tinyxml2::XMLElement *xml, SourceCore &s)
} }
FrameBufferImage *SessionLoader::XMLToImage(tinyxml2::XMLElement *xml)
{
FrameBufferImage *i = nullptr;
if (xml != nullptr){
// if there is an Image mask stored
XMLElement* imageNode = xml->FirstChildElement("Image");
if (imageNode) {
// if there is an internal array of data
XMLElement* array = imageNode->FirstChildElement("array");
if (array) {
// create a temporary jpeg with size of the array
FrameBufferImage::jpegBuffer jpgimg;
array->QueryUnsignedAttribute("len", &jpgimg.len);
// ok, we got a size of data to load
if (jpgimg.len>0) {
// allocate jpeg buffer
jpgimg.buffer = (unsigned char*) malloc(jpgimg.len);
// actual decoding of array
if (XMLElementDecodeArray(array, jpgimg.buffer, jpgimg.len) ) {
// create and set the image from jpeg
i = new FrameBufferImage(jpgimg);
}
// free temporary buffer
if (jpgimg.buffer)
free(jpgimg.buffer);
}
}
}
}
return i;
}
void SessionLoader::visit(Node &n) void SessionLoader::visit(Node &n)
{ {
XMLToNode(xmlCurrent_, n); XMLToNode(xmlCurrent_, n);
@@ -713,29 +747,8 @@ void SessionLoader::visit (Source& s)
if (xmlCurrent_) { if (xmlCurrent_) {
// read the mask shader attributes // read the mask shader attributes
s.maskShader()->accept(*this); s.maskShader()->accept(*this);
// if there is an Image mask stored // set the mask from jpeg
XMLElement* imageNode = xmlCurrent_->FirstChildElement("Image"); s.setMask( SessionLoader::XMLToImage(xmlCurrent_) );
if (imageNode) {
// if there is an internal array of data
XMLElement* array = imageNode->FirstChildElement("array");
if (array) {
// create a temporary jpeg with size of the array
FrameBufferImage::jpegBuffer jpgimg;
array->QueryUnsignedAttribute("len", &jpgimg.len);
// ok, we got a size of data to load
if (jpgimg.len>0) {
// allocate jpeg buffer
jpgimg.buffer = (unsigned char*) malloc(jpgimg.len);
// actual decoding of array
if (XMLElementDecodeArray(array, jpgimg.buffer, jpgimg.len) )
// create and set the image from jpeg
s.setMask(new FrameBufferImage(jpgimg));
// free temporary buffer
if (jpgimg.buffer)
free(jpgimg.buffer);
}
}
}
} }
xmlCurrent_ = sourceNode->FirstChildElement("ImageProcessing"); xmlCurrent_ = sourceNode->FirstChildElement("ImageProcessing");

View File

@@ -8,7 +8,7 @@
#include "SourceList.h" #include "SourceList.h"
class Session; class Session;
class FrameBufferImage;
class SessionLoader : public Visitor { class SessionLoader : public Visitor {
@@ -59,7 +59,8 @@ public:
void visit (NetworkSource& s) override; void visit (NetworkSource& s) override;
static void XMLToNode(tinyxml2::XMLElement *xml, Node &n); static void XMLToNode(tinyxml2::XMLElement *xml, Node &n);
static void XMLToSourcecore( tinyxml2::XMLElement *xml, SourceCore &s); static void XMLToSourcecore(tinyxml2::XMLElement *xml, SourceCore &s);
static FrameBufferImage *XMLToImage(tinyxml2::XMLElement *xml);
protected: protected:
// result created session // result created session

View File

@@ -120,7 +120,7 @@ SessionVisitor::SessionVisitor(tinyxml2::XMLDocument *doc,
xmlDoc_ = doc; xmlDoc_ = doc;
} }
tinyxml2::XMLElement *SessionVisitor::NodeToXML(Node &n, tinyxml2::XMLDocument *doc) XMLElement *SessionVisitor::NodeToXML(Node &n, XMLDocument *doc)
{ {
XMLElement *newelement = doc->NewElement("Node"); XMLElement *newelement = doc->NewElement("Node");
newelement->SetAttribute("visible", n.visible_); newelement->SetAttribute("visible", n.visible_);
@@ -145,6 +145,29 @@ tinyxml2::XMLElement *SessionVisitor::NodeToXML(Node &n, tinyxml2::XMLDocument *
return newelement; return newelement;
} }
XMLElement *SessionVisitor::ImageToXML(FrameBufferImage *img, XMLDocument *doc)
{
XMLElement *imageelement = nullptr;
if (img != nullptr) {
// get the jpeg encoded buffer
FrameBufferImage::jpegBuffer jpgimg = img->getJpeg();
if (jpgimg.buffer != nullptr) {
// fill the xml array with jpeg buffer
XMLElement *array = XMLElementEncodeArray(doc, jpgimg.buffer, jpgimg.len);
// free the buffer
free(jpgimg.buffer);
// if we could create the array
if (array) {
// create an Image node to store the mask image
imageelement = doc->NewElement("Image");
imageelement->InsertEndChild(array);
}
}
}
return imageelement;
}
void SessionVisitor::visit(Node &n) void SessionVisitor::visit(Node &n)
{ {
XMLElement *newelement = NodeToXML(n, xmlDoc_); XMLElement *newelement = NodeToXML(n, xmlDoc_);
@@ -449,24 +472,9 @@ void SessionVisitor::visit (Source& s)
// if we are saving a pain mask // if we are saving a pain mask
if (s.maskShader()->mode == MaskShader::PAINT) { if (s.maskShader()->mode == MaskShader::PAINT) {
// get the mask previously stored // get the mask previously stored
FrameBufferImage *img = s.getMask(); XMLElement *imageelement = SessionVisitor::ImageToXML(s.getMask(), xmlDoc_);
if (img != nullptr) { if (imageelement)
// get the jpeg encoded buffer xmlCurrent_->InsertEndChild(imageelement);
FrameBufferImage::jpegBuffer jpgimg = img->getJpeg();
if (jpgimg.buffer != nullptr) {
// fill the xml array with jpeg buffer
XMLElement *array = XMLElementEncodeArray(xmlDoc_, jpgimg.buffer, jpgimg.len);
// free the buffer
free(jpgimg.buffer);
// if we could create the array
if (array) {
// create an Image node to store the mask image
XMLElement *imageelement = xmlDoc_->NewElement("Image");
imageelement->InsertEndChild(array);
xmlCurrent_->InsertEndChild(imageelement);
}
}
}
} }
xmlCurrent_ = xmlDoc_->NewElement( "ImageProcessing" ); xmlCurrent_ = xmlDoc_->NewElement( "ImageProcessing" );

View File

@@ -6,6 +6,7 @@
#include "SourceList.h" #include "SourceList.h"
class Session; class Session;
class FrameBufferImage;
class SessionVisitor : public Visitor { class SessionVisitor : public Visitor {
@@ -62,6 +63,7 @@ public:
protected: protected:
static tinyxml2::XMLElement *NodeToXML(Node &n, tinyxml2::XMLDocument *doc); static tinyxml2::XMLElement *NodeToXML(Node &n, tinyxml2::XMLDocument *doc);
static tinyxml2::XMLElement *ImageToXML(FrameBufferImage *img, tinyxml2::XMLDocument *doc);
}; };
#endif // XMLVISITOR_H #endif // XMLVISITOR_H