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)
{
XMLToNode(xmlCurrent_, n);
@@ -713,29 +747,8 @@ void SessionLoader::visit (Source& s)
if (xmlCurrent_) {
// read the mask shader attributes
s.maskShader()->accept(*this);
// if there is an Image mask stored
XMLElement* imageNode = xmlCurrent_->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
s.setMask(new FrameBufferImage(jpgimg));
// free temporary buffer
if (jpgimg.buffer)
free(jpgimg.buffer);
}
}
}
// set the mask from jpeg
s.setMask( SessionLoader::XMLToImage(xmlCurrent_) );
}
xmlCurrent_ = sourceNode->FirstChildElement("ImageProcessing");

View File

@@ -8,7 +8,7 @@
#include "SourceList.h"
class Session;
class FrameBufferImage;
class SessionLoader : public Visitor {
@@ -59,7 +59,8 @@ public:
void visit (NetworkSource& s) override;
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:
// result created session

View File

@@ -120,7 +120,7 @@ SessionVisitor::SessionVisitor(tinyxml2::XMLDocument *doc,
xmlDoc_ = doc;
}
tinyxml2::XMLElement *SessionVisitor::NodeToXML(Node &n, tinyxml2::XMLDocument *doc)
XMLElement *SessionVisitor::NodeToXML(Node &n, XMLDocument *doc)
{
XMLElement *newelement = doc->NewElement("Node");
newelement->SetAttribute("visible", n.visible_);
@@ -145,6 +145,29 @@ tinyxml2::XMLElement *SessionVisitor::NodeToXML(Node &n, tinyxml2::XMLDocument *
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)
{
XMLElement *newelement = NodeToXML(n, xmlDoc_);
@@ -449,24 +472,9 @@ void SessionVisitor::visit (Source& s)
// if we are saving a pain mask
if (s.maskShader()->mode == MaskShader::PAINT) {
// get the mask previously stored
FrameBufferImage *img = s.getMask();
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(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);
}
}
}
XMLElement *imageelement = SessionVisitor::ImageToXML(s.getMask(), xmlDoc_);
if (imageelement)
xmlCurrent_->InsertEndChild(imageelement);
}
xmlCurrent_ = xmlDoc_->NewElement( "ImageProcessing" );

View File

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