gdal - read single band as image_data_float32

(scaling/compositing FIXME)
This commit is contained in:
artemp 2014-11-28 12:51:23 +01:00
parent bf9c99e3d3
commit 6fec43bf9e
4 changed files with 75 additions and 48 deletions

View file

@ -70,15 +70,15 @@ MAPNIK_DECL void scale_image_agg(Image & target,
double y_off_f,
double filter_factor);
extern template MAPNIK_DECL void scale_image_agg<mapnik::image_data_32>(
mapnik::image_data_32 & target,
mapnik::image_data_32 const& source,
scaling_method_e scaling_method,
double image_ratio_x,
double image_ratio_y,
double x_off_f,
double y_off_f,
double filter_radius);
//extern template MAPNIK_DECL void scale_image_agg<mapnik::image_data_32>(
// mapnik::image_data_32 & target,
// mapnik::image_data_32 const& source,
// scaling_method_e scaling_method,
// double image_ratio_x,
// double image_ratio_y,
// double x_off_f,
// double y_off_f,
// double filter_radius);
}
#endif // MAPNIK_IMAGE_SCALING_HPP

View file

@ -100,6 +100,10 @@ void render_raster_symbolizer(raster_symbolizer const &sym,
{
composite(util::get<image_data_32>(target.data_), comp_op, opacity, start_x, start_y);
}
else
{
std::cerr << "#1 source->data float32" << std::endl;
}
}
else
{
@ -115,6 +119,10 @@ void render_raster_symbolizer(raster_symbolizer const &sym,
{
composite(util::get<image_data_32>(source->data_), comp_op, opacity, start_x, start_y);
}
else
{
std::cerr << "#2 source->data float32" << std::endl;
}
}
else
{
@ -123,15 +131,31 @@ void render_raster_symbolizer(raster_symbolizer const &sym,
image_data_32 data(raster_width, raster_height);
raster target(target_ext, data, source->get_filter_factor());
scale_image_agg<image_data_32>(util::get<image_data_32>(target.data_),
util::get<image_data_32>(source->data_),
scaling_method,
image_ratio_x,
image_ratio_y,
0.0,
0.0,
source->get_filter_factor());
util::get<image_data_32>(source->data_),
scaling_method,
image_ratio_x,
image_ratio_y,
0.0,
0.0,
source->get_filter_factor());
composite(util::get<image_data_32>(target.data_), comp_op, opacity, start_x, start_y);
}
else if (source->data_.is<image_data_float32>())
{
std::cerr << "#3 source->data float32" << std::endl;
image_data_float32 data(raster_width, raster_height);
raster target(target_ext, data, source->get_filter_factor());
//scale_image_agg<image_data_float32>(util::get<image_data_float32>(target.data_),
// util::get<image_data_float32>(source->data_),
// scaling_method,
// image_ratio_x,
// image_ratio_y,
// 0.0,
// 0.0,
// source->get_filter_factor());
// composite is no-op
}
}
}
}

View file

@ -199,35 +199,35 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
if (im_width > 0 && im_height > 0)
{
mapnik::image_data_32 data(im_width, im_height);
mapnik::raster_ptr raster = std::make_shared<mapnik::raster>(intersect, data, filter_factor);
feature->set_raster(raster);
mapnik::image_data_32 & image = mapnik::util::get<mapnik::image_data_32>(raster->data_);
image.set(0xffffffff);
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Image Size=(" << im_width << "," << im_height << ")";
MAPNIK_LOG_DEBUG(gdal) << "gdal_featureset: Reading band=" << band_;
if (band_ > 0) // we are querying a single band
{
mapnik::image_data_float32 image(im_width, im_height);
image.set(std::numeric_limits<float>::max());
if (band_ > nbands_)
{
std::ostringstream s;
s << "GDAL Plugin: " << band_ << " is an invalid band, dataset only has " << nbands_ << "bands";
throw datasource_exception(s.str());
}
float* imageData = reinterpret_cast<float*>(image.getBytes());
GDALRasterBand * band = dataset_.GetRasterBand(band_);
raster_nodata = band->GetNoDataValue(&raster_has_nodata);
raster_io_error = band->RasterIO(GF_Read, x_off, y_off, width, height,
imageData, image.width(), image.height(),
image.getData(), image.width(), image.height(),
GDT_Float32, 0, 0);
if (raster_io_error == CE_Failure) {
throw datasource_exception(CPLGetLastErrorMsg());
}
mapnik::raster_ptr raster = std::make_shared<mapnik::raster>(intersect, image, filter_factor);
feature->set_raster(raster);
}
else // working with all bands
{
mapnik::image_data_32 image(im_width, im_height);
image.set(std::numeric_limits<std::uint32_t>::max());
for (int i = 0; i < nbands_; ++i)
{
GDALRasterBand * band = dataset_.GetRasterBand(i + 1);
@ -364,7 +364,8 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
raster_io_error = grey->RasterIO(GF_Read, x_off, y_off, width, height,
imageData, image.width(), image.height(),
GDT_Float32, 0, 0);
if (raster_io_error == CE_Failure) {
if (raster_io_error == CE_Failure)
{
throw datasource_exception(CPLGetLastErrorMsg());
}
int len = image.width() * image.height();
@ -380,19 +381,26 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
}
}
}
raster_io_error = grey->RasterIO(GF_Read, x_off, y_off, width, height, image.getBytes() + 0,
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
if (raster_io_error == CE_Failure) {
if (raster_io_error == CE_Failure)
{
throw datasource_exception(CPLGetLastErrorMsg());
}
raster_io_error = grey->RasterIO(GF_Read,x_off, y_off, width, height, image.getBytes() + 1,
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
if (raster_io_error == CE_Failure) {
if (raster_io_error == CE_Failure)
{
throw datasource_exception(CPLGetLastErrorMsg());
}
raster_io_error = grey->RasterIO(GF_Read,x_off, y_off, width, height, image.getBytes() + 2,
image.width(), image.height(), GDT_Byte, 4, 4 * image.width());
if (raster_io_error == CE_Failure) {
if (raster_io_error == CE_Failure)
{
throw datasource_exception(CPLGetLastErrorMsg());
}
@ -436,15 +444,11 @@ feature_ptr gdal_featureset::get_feature(mapnik::query const& q)
MAPNIK_LOG_WARN(gdal) << "warning: nodata value (" << raster_nodata << ") used to set transparency instead of alpha band";
}
}
}
// set nodata value to be used in raster colorizer
if (nodata_value_)
{
raster->set_nodata(*nodata_value_);
}
else
{
raster->set_nodata(raster_nodata);
mapnik::raster_ptr raster = std::make_shared<mapnik::raster>(intersect, image, filter_factor);
// set nodata value to be used in raster colorizer
if (nodata_value_) raster->set_nodata(*nodata_value_);
else raster->set_nodata(raster_nodata);
feature->set_raster(raster);
}
// report actual/original source nodata in feature attributes
if (raster_has_nodata)

View file

@ -94,15 +94,14 @@ boost::optional<std::string> scaling_method_to_string(scaling_method_e scaling_m
return mode;
}
template <typename Image>
void scale_image_agg(Image & target,
Image const& source,
scaling_method_e scaling_method,
double image_ratio_x,
double image_ratio_y,
double x_off_f,
double y_off_f,
double filter_factor)
template <> void scale_image_agg<image_data_32>(image_data_32 & target,
image_data_32 const& source,
scaling_method_e scaling_method,
double image_ratio_x,
double image_ratio_y,
double x_off_f,
double y_off_f,
double filter_factor)
{
// "the image filters should work namely in the premultiplied color space"
// http://old.nabble.com/Re:--AGG--Basic-image-transformations-p1110665.html
@ -118,13 +117,13 @@ void scale_image_agg(Image & target,
agg::image_filter_lut filter;
// initialize source AGG buffer
agg::rendering_buffer rbuf_src((unsigned char*)source.getBytes(), source.width(), source.height(), source.width() * 4);
agg::rendering_buffer rbuf_src(const_cast<unsigned char*>(source.getBytes()), source.width(), source.height(), source.width() * 4);
pixfmt_pre pixf_src(rbuf_src);
using img_src_type = agg::image_accessor_clone<pixfmt_pre>;
img_src_type img_src(pixf_src);
// initialize destination AGG buffer (with transparency)
agg::rendering_buffer rbuf_dst((unsigned char*)target.getBytes(), target.width(), target.height(), target.width() * 4);
agg::rendering_buffer rbuf_dst(target.getBytes(), target.width(), target.height(), target.width() * 4);
pixfmt_pre pixf_dst(rbuf_dst);
renderer_base_pre rb_dst_pre(pixf_dst);