Skip to content

Commit

Permalink
image_data: use std::size_t + various cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
artemp committed Nov 20, 2014
1 parent 79bbe13 commit 4f3521a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 62 deletions.
80 changes: 31 additions & 49 deletions include/mapnik/image_data.hpp
Expand Up @@ -39,45 +39,27 @@ class image_data
public:
using pixel_type = T;

image_data(int width, int height)
: width_(static_cast<unsigned>(width)),
height_(static_cast<unsigned>(height)),
image_data(std::size_t width, std::size_t height, bool initialize = true)
: width_(width),
height_(height),
pData_((width!=0 && height!=0) ? static_cast<T*>(::operator new(sizeof(T) * width_ * height_)):0),
owns_data_(true)
{
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");
}
pData_ = (width!=0 && height!=0) ? static_cast<T*>(::operator new(sizeof(T) * width * height)):0;
if (pData_) std::fill(pData_, pData_ + width_ * height_, 0);
if (pData_ && initialize) std::fill(pData_, pData_ + width_ * height_, 0);
}

image_data(int width, int height, T * data)
: width_(static_cast<unsigned>(width)),
height_(static_cast<unsigned>(height)),
image_data(std::size_t width, std::size_t height, T * data)
: width_(width),
height_(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");
}
}
pData_(data) {}

image_data(image_data<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)
: width_(rhs.width_),
height_(rhs.height_),
pData_((rhs.width_!=0 && rhs.height_!=0) ?
static_cast<T*>(::operator new(sizeof(T) * rhs.width_ * rhs.height_)) : 0),
owns_data_(true)
{
if (pData_) std::copy(rhs.pData_, rhs.pData_ + rhs.width_* rhs.height_, pData_);
}
Expand Down Expand Up @@ -105,21 +87,21 @@ class image_data
std::swap(pData_, rhs.pData_);
}

inline T& operator() (unsigned i,unsigned j)
inline T& operator() (std::size_t i, std::size_t j)
{
assert(i<width_ && j<height_);
return pData_[j*width_+i];
return pData_[j * width_ + i];
}
inline const T& operator() (unsigned i,unsigned j) const
inline const T& operator() (std::size_t i,std::size_t j) const
{
assert(i<width_ && j<height_);
return pData_[j*width_+i];
assert(i < width_ && j < height_);
return pData_[j * width_ + i];
}
inline unsigned width() const
inline std::size_t width() const
{
return width_;
}
inline unsigned height() const
inline std::size_t height() const
{
return height_;
}
Expand Down Expand Up @@ -150,21 +132,21 @@ class image_data

inline const T* getRow(unsigned row) const
{
return pData_+row*width_;
return pData_ + row * width_;
}

inline T* getRow(unsigned row)
{
return pData_+row*width_;
return pData_ + row * width_;
}

inline void setRow(unsigned row, T const* buf, unsigned size)
inline void setRow(std::size_t row, T const* buf, std::size_t size)
{
assert(row<height_);
assert(size<=width_);
assert(row < height_);
assert(size <= width_);
std::copy(buf, buf + size, pData_ + row * width_);
}
inline void setRow(unsigned row, unsigned x0, unsigned x1, T const* buf)
inline void setRow(std::size_t row, std::size_t x0, std::size_t x1, T const* buf)
{
std::copy(buf, buf + (x1 - x0), pData_ + row * width_);
}
Expand All @@ -178,13 +160,13 @@ class image_data
}

private:
unsigned width_;
unsigned height_;
bool owns_data_;
std::size_t width_;
std::size_t height_;
T *pData_;
bool owns_data_;
};

using image_data_32 = image_data<unsigned>;
using image_data_32 = image_data<std::uint32_t>;
using image_data_8 = image_data<byte> ;
}

Expand Down
20 changes: 10 additions & 10 deletions src/warp.cpp
Expand Up @@ -58,16 +58,16 @@ void reproject_and_scale_raster(raster & target, raster const& source,
view_transform tt(target.data_.width(), target.data_.height(),
target.ext_, offset_x, offset_y);

unsigned mesh_nx = std::ceil(source.data_.width()/double(mesh_size) + 1);
unsigned mesh_ny = std::ceil(source.data_.height()/double(mesh_size) + 1);
std::size_t mesh_nx = std::ceil(source.data_.width()/double(mesh_size) + 1);
std::size_t mesh_ny = std::ceil(source.data_.height()/double(mesh_size) + 1);

image_data<double> xs(mesh_nx, mesh_ny);
image_data<double> ys(mesh_nx, mesh_ny);

// Precalculate reprojected mesh
for(unsigned j=0; j<mesh_ny; ++j)
for(std::size_t j = 0; j < mesh_ny; ++j)
{
for (unsigned i=0; i<mesh_nx; ++i)
for (std::size_t i=0; i<mesh_nx; ++i)
{
xs(i,j) = std::min(i*mesh_size,source.data_.width());
ys(i,j) = std::min(j*mesh_size,source.data_.height());
Expand Down Expand Up @@ -143,9 +143,9 @@ void reproject_and_scale_raster(raster & target, raster const& source,
}

// Project mesh cells into target interpolating raster inside each one
for(unsigned j=0; j<mesh_ny-1; ++j)
for(std::size_t j = 0; j < mesh_ny - 1; ++j)
{
for (unsigned i=0; i<mesh_nx-1; ++i)
for (std::size_t i = 0; i < mesh_nx - 1; ++i)
{
double polygon[8] = {xs(i,j), ys(i,j),
xs(i+1,j), ys(i+1,j),
Expand All @@ -162,10 +162,10 @@ void reproject_and_scale_raster(raster & target, raster const& source,
rasterizer.line_to_d(std::floor(polygon[4]), std::floor(polygon[5]));
rasterizer.line_to_d(std::floor(polygon[6]), std::floor(polygon[7]));

unsigned x0 = i * mesh_size;
unsigned y0 = j * mesh_size;
unsigned x1 = (i+1) * mesh_size;
unsigned y1 = (j+1) * mesh_size;
std::size_t x0 = i * mesh_size;
std::size_t y0 = j * mesh_size;
std::size_t x1 = (i+1) * mesh_size;
std::size_t y1 = (j+1) * mesh_size;
x1 = std::min(x1, source.data_.width());
y1 = std::min(y1, source.data_.height());
agg::trans_affine tr(polygon, x0, y0, x1, y1);
Expand Down
6 changes: 3 additions & 3 deletions src/webp_reader.cpp
Expand Up @@ -241,16 +241,16 @@ void webp_reader<T>::read(unsigned x0, unsigned y0,image_data_32& image)
config.options.use_cropping = 1;
config.options.crop_left = x0;
config.options.crop_top = y0;
config.options.crop_width = std::min(width_ - x0, image.width());
config.options.crop_height = std::min(height_ - y0, image.height());
config.options.crop_width = std::min(static_cast<std::size_t>(width_ - x0), image.width());
config.options.crop_height = std::min(static_cast<std::size_t>(height_ - y0), image.height());

if (WebPGetFeatures(buffer_->data(), buffer_->size(), &config.input) != VP8_STATUS_OK)
{
throw image_reader_exception("WEBP reader: WebPGetFeatures failed");
}

config.output.colorspace = MODE_RGBA;
config.output.u.RGBA.rgba = (uint8_t *)image.getBytes();
config.output.u.RGBA.rgba = reinterpret_cast<uint8_t *>(image.getBytes());
config.output.u.RGBA.stride = 4 * image.width();
config.output.u.RGBA.size = image.width() * image.height() * 4;
config.output.is_external_memory = 1;
Expand Down

0 comments on commit 4f3521a

Please sign in to comment.