allow external image data - refs #2002

Conflicts:
	include/mapnik/image_data.hpp
	tests/cpp_tests/image_io_test.cpp
This commit is contained in:
Dane Springmeyer 2014-06-11 16:58:58 -07:00
parent 7cc87c2196
commit cc874364b2
2 changed files with 34 additions and 4 deletions

View file

@ -39,9 +39,10 @@ class ImageData
public:
typedef T pixel_type;
ImageData(int width,int height)
ImageData(int width, int height)
: width_(static_cast<unsigned>(width)),
height_(static_cast<unsigned>(height))
height_(static_cast<unsigned>(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<unsigned>(width)),
height_(static_cast<unsigned>(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<T> const& rhs)
:width_(rhs.width_),
height_(rhs.height_),
owns_data_(true),
pData_((rhs.width_!=0 && rhs.height_!=0)?
static_cast<T*>(::operator new(sizeof(T)*rhs.width_*rhs.height_)) :0)
{
@ -159,15 +177,19 @@ public:
}
inline ~ImageData()
{
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<unsigned> image_data_32;

View file

@ -21,6 +21,14 @@ int main(int argc, char** argv)
boost::optional<std::string> 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)