From cc874364b25f7f7a3b199694acc006240068da10 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Wed, 11 Jun 2014 16:58:58 -0700 Subject: [PATCH] allow external image data - refs #2002 Conflicts: include/mapnik/image_data.hpp tests/cpp_tests/image_io_test.cpp --- include/mapnik/image_data.hpp | 30 ++++++++++++++++++++++++++---- tests/cpp_tests/image_io_test.cpp | 8 ++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/include/mapnik/image_data.hpp b/include/mapnik/image_data.hpp index b46fa4b7c..d8df2f423 100644 --- a/include/mapnik/image_data.hpp +++ b/include/mapnik/image_data.hpp @@ -39,9 +39,10 @@ class ImageData public: typedef T pixel_type; - ImageData(int width,int height) + ImageData(int width, int height) : width_(static_cast(width)), - height_(static_cast(height)) + height_(static_cast(height)), + owns_data_(true) { if (width < 0) { @@ -55,9 +56,26 @@ public: if (pData_) std::memset(pData_,0,sizeof(T)*width_*height_); } + ImageData(int width, int height, T * data) + : width_(static_cast(width)), + height_(static_cast(height)), + owns_data_(false), + pData_(data) + { + if (width < 0) + { + throw std::runtime_error("negative width not allowed for image_data"); + } + if (height < 0) + { + throw std::runtime_error("negative height not allowed for image_data"); + } + } + ImageData(ImageData const& rhs) :width_(rhs.width_), height_(rhs.height_), + owns_data_(true), pData_((rhs.width_!=0 && rhs.height_!=0)? static_cast(::operator new(sizeof(T)*rhs.width_*rhs.height_)) :0) { @@ -160,14 +178,18 @@ public: inline ~ImageData() { - ::operator delete(pData_),pData_=0; + if (owns_data_) + { + ::operator delete(pData_),pData_=0; + } } private: unsigned width_; unsigned height_; + bool owns_data_; T *pData_; - ImageData& operator=(const ImageData&); + ImageData& operator=(ImageData const&); }; typedef ImageData image_data_32; diff --git a/tests/cpp_tests/image_io_test.cpp b/tests/cpp_tests/image_io_test.cpp index bdc4f8967..aae80ede0 100644 --- a/tests/cpp_tests/image_io_test.cpp +++ b/tests/cpp_tests/image_io_test.cpp @@ -21,6 +21,14 @@ int main(int argc, char** argv) boost::optional type; try { + mapnik::image_data_32 im(256,256); + unsigned char* bytes = im.getBytes(); + mapnik::image_data_32 * im_ptr = new mapnik::image_data_32(256,256,(unsigned int *)bytes); + unsigned char* same_bytes = im_ptr->getBytes(); + BOOST_TEST(bytes == same_bytes); + delete im_ptr; + BOOST_TEST(bytes == same_bytes); + BOOST_TEST(set_working_dir(args)); #if defined(HAVE_JPEG)