serialization of an dynamic array to read/write a image in xml

Hello I like to store and load images from a xml file. My problem is now the dynamic array which keeps the image data. I do not want to copy it into a container and I do not want to go in a for loop over every pixel. Any suggestions? Thanks Max #include <fstream> #include <boost/archive/xml_iarchive.hpp> #include <boost/archive/xml_oarchive.hpp> #include <boost/serialization/nvp.hpp> class Image { public: Image() : mWidth(0) , mHeight(0) , mpData(NULL) { } Image(int width, int height, char *pData) : mWidth(width) , mHeight(height) , mpData(pData) { } ~Image() { } template<class archive> void serialize(archive& ar, const unsigned int version) { int i; using boost::serialization::make_nvp; ar & make_nvp("width", mWidth); ar & make_nvp("height", mHeight); // ar & make_nvp("data", mpData, mWidth*mHeight); I know this can not work but --> is there something like this //** How can I serialize the serialization array without making a copy to a container **// } void save(const std::string& rFileName) { std::ofstream ofs(rFileName.c_str()); assert(ofs.good()); boost::archive::xml_oarchive xml(ofs); xml << boost::serialization::make_nvp("Image", this); } void load(const std::string& rFileName) { std::ifstream ifs(rFileName.c_str()); assert(ifs.good()); boost::archive::xml_iarchive xml(ifs); xml >> boost::serialization::make_nvp("Image", *this); } private: int mWidth; int mHeight; char *mpData; }; int main ( int argc, char** argv ) { int h = 640; int w = 480; char *p = new char[w*h]; Image img(w,h,p); img.save("/tmp/img.xml"); img.load("/tmp/img.xml"); delete p; return 0; }

Am Tuesday 22 September 2009 15:23:39 schrieb Markus Bader:
Hello
I like to store and load images from a xml file. My problem is now the dynamic array which keeps the image data. I do not want to copy it into a container and I do not want to go in a for loop over every pixel. Any suggestions?
archives must implement load_binary/save_binary functions you can use. you have to split your serialize function into load/save functions and can call load_binary/save_binary from there. see http://www.boost.org/doc/libs/1_40_0/libs/serialization/doc/archives.html#sa... I guess a XML archives translate the binary data to encoded text, but that's just a wild guess, I've never tried it.

Stefan Strasser wrote:
I guess a XML archives translate the binary data to encoded text, but that's just a wild guess, I've never tried it.
I've done it...and stopped doing it shortly thereafter due to how slow the deserialization process was compared to non-text formats.

Markus Bader wrote:
Hello
I like to store and load images from a xml file. My problem is now the dynamic array which keeps the image data. I do not want to copy it into a container and I do not want to go in a for loop over every pixel. Any suggestions? ar & make_nvp("data", make_array(mpData, mWidth*mHeight)) ?

Damien R wrote:
Markus Bader wrote:
Hello
I like to store and load images from a xml file. My problem is now the dynamic array which keeps the image data. I do not want to copy it into a container and I do not want to go in a for loop over every pixel. Any suggestions? ar & make_nvp("data", make_array(mpData, mWidth*mHeight)) ?
also you could try ar & make_nvp("data", binary_object(sizeof(mpData), & mpData)) the cost of xml archiving is going to dominate even if you were to use the default serialization method whch loops over every pixel. XML just has a lot of extra characters and requires parsing - it has to be much slower then a binary format. Robert Ramey

Robert Ramey wrote:
also you could try
ar & make_nvp("data", binary_object(sizeof(mpData), & mpData)) This code do not work for dynamic allocation of mpData. data_type * mpData = new data_type[MAX]; sizeof(mpData) = sizeof(data_type *);
but it will be fine if you have static allocation : data_type mpData[MAX]; sizeof(mpData) = MAX * sizeof(data_type);

Thanks all of you for your ideas and suggestions I finally realized the serialize(archive& ar, const unsigned) funktion using ar & make_nvp("data", make_binary_object(mpData, mWidth*mHeight); but I have to say that the memory behind mpData bust be allocated first on reading an image. Finaly I endet up with template<class archive> void serialize(archive& ar, const unsigned int version) { using boost::serialization::make_nvp; using boost::serialization::make_binary_object; if (archive::is_saving::value) { ar & make_nvp("width", mWidth); ar & make_nvp("height", mHeight); ar & make_nvp("data", make_binary_object(mpData, mWidth*mHeight); } else if (archive::is_loading::value) int w, h;{ ar & make_nvp("width", w); ar & make_nvp("height", h); 2009/9/25 Damien R <damien.rg@gmail.com>:
Robert Ramey wrote:
also you could try
ar & make_nvp("data", binary_object(sizeof(mpData), & mpData))
This code do not work for dynamic allocation of mpData. data_type * mpData = new data_type[MAX]; sizeof(mpData) = sizeof(data_type *);
but it will be fine if you have static allocation : data_type mpData[MAX]; sizeof(mpData) = MAX * sizeof(data_type);
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Dipl.-Ing. Markus Bader ACIN | AUTOMATION & CONTROL INSTITUTE INSTITUT FÜR AUTOMATISIERUNGS- & REGELUNGSTECHNIK TECHNISCHE UNIVERSITÄT WIEN Gußhausstraße 27-29 | 376. 1040 Wien DVR-Nummer: 0005886 Tel. +43 (0)1 58801 - 37664 Skype: markus.bader.at.work Fax. +43 (0)1 58801 - 37697 bader@acin.tuwien.ac.at | www.acin.tuwien.ac.at

Solved Thanks all of you for your ideas and suggestions I finally realized the serialize(archive& ar, const unsigned) funktion using ar & make_nvp("data", make_binary_object(mpData, mWidth*mHeight); but I have to say that the memory behind mpData bust be allocated first on reading an image. Finaly I endet up with template<class archive> void serialize(archive& ar, const unsigned int version) { using boost::serialization::make_nvp; using boost::serialization::make_binary_object; if (archive::is_saving::value) { ar & make_nvp("width", mWidth); ar & make_nvp("height", mHeight); } else if (archive::is_loading::value) int w, h;{ ar & make_nvp("width", w); ar & make_nvp("height", h); if ((w != mWidth) || (h != mHeight) { w = mWidth; h = mHeight; if(mpData != NULL) delete mpData; mpData = new char[w*h]; } ar & make_nvp("data", make_binary_object(mpData, mWidth*mHeight); } Thanks again Markus 2009/9/25 Damien R <damien.rg@gmail.com>:
Robert Ramey wrote:
also you could try
ar & make_nvp("data", binary_object(sizeof(mpData), & mpData))
This code do not work for dynamic allocation of mpData. data_type * mpData = new data_type[MAX]; sizeof(mpData) = sizeof(data_type *);
but it will be fine if you have static allocation : data_type mpData[MAX]; sizeof(mpData) = MAX * sizeof(data_type);
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- Dipl.-Ing. Markus Bader ACIN | AUTOMATION & CONTROL INSTITUTE INSTITUT FÜR AUTOMATISIERUNGS- & REGELUNGSTECHNIK TECHNISCHE UNIVERSITÄT WIEN Gußhausstraße 27-29 | 376. 1040 Wien DVR-Nummer: 0005886 Tel. +43 (0)1 58801 - 37664 Skype: markus.bader.at.work Fax. +43 (0)1 58801 - 37697 bader@acin.tuwien.ac.at | www.acin.tuwien.ac.at
participants (5)
-
Damien R
-
Kenny Riddile
-
Markus Bader
-
Robert Ramey
-
Stefan Strasser