2015-01-29 03:20:14 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2015-06-16 12:49:16 +02:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
2015-01-29 03:20:14 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// mapnik
|
2015-02-06 22:27:19 +01:00
|
|
|
#include <mapnik/image_copy.hpp>
|
2015-01-29 03:20:14 +01:00
|
|
|
#include <mapnik/image.hpp>
|
|
|
|
#include <mapnik/image_any.hpp>
|
2015-06-14 04:19:58 +02:00
|
|
|
#include <mapnik/safe_cast.hpp>
|
2015-01-29 03:20:14 +01:00
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename T0>
|
2015-02-06 22:27:19 +01:00
|
|
|
struct visitor_image_copy
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
using dst_type = typename T0::pixel_type;
|
2015-06-02 15:25:57 +02:00
|
|
|
|
2016-01-27 14:50:17 +01:00
|
|
|
T0 operator() (image_null const&) const
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Can not cast a null image");
|
|
|
|
}
|
|
|
|
|
|
|
|
T0 operator() (T0 const& src)
|
|
|
|
{
|
|
|
|
return T0(src);
|
|
|
|
}
|
2015-06-02 15:25:57 +02:00
|
|
|
|
2015-01-29 03:20:14 +01:00
|
|
|
template <typename T1>
|
2016-01-27 14:50:17 +01:00
|
|
|
T0 operator() (T1 const& src) const
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
2015-06-16 04:36:36 +02:00
|
|
|
T0 dst(safe_cast<int>(src.width()), safe_cast<int>(src.height()), false);
|
|
|
|
for (std::size_t y = 0; y < dst.height(); ++y)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
2015-06-16 04:36:36 +02:00
|
|
|
for (std::size_t x = 0; x < dst.width(); ++x)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
2015-06-14 04:19:58 +02:00
|
|
|
dst(x,y) = safe_cast<dst_type>(src(x,y));
|
2015-01-29 03:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return T0(std::move(dst));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T0>
|
2015-02-06 22:27:19 +01:00
|
|
|
struct visitor_image_copy_so
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
using dst_type = typename T0::pixel_type;
|
2015-06-02 15:25:57 +02:00
|
|
|
|
2015-02-06 22:27:19 +01:00
|
|
|
visitor_image_copy_so(double offset, double scaling)
|
2015-01-29 03:20:14 +01:00
|
|
|
: offset_(offset), scaling_(scaling) {}
|
|
|
|
|
2015-06-02 15:25:57 +02:00
|
|
|
T0 operator() (image_null const&)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Can not cast a null image");
|
|
|
|
}
|
|
|
|
|
2016-01-27 14:50:17 +01:00
|
|
|
T0 operator() (T0 const& src) const
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
2015-03-07 00:24:23 +01:00
|
|
|
if (offset_ == src.get_offset() && scaling_ == src.get_scaling())
|
|
|
|
{
|
|
|
|
return T0(src);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
T0 dst(src);
|
2015-06-02 15:25:57 +02:00
|
|
|
dst.set_scaling(scaling_);
|
|
|
|
dst.set_offset(offset_);
|
2015-03-07 00:24:23 +01:00
|
|
|
return T0(std::move(dst));
|
|
|
|
}
|
2015-01-29 03:20:14 +01:00
|
|
|
}
|
2015-06-02 15:25:57 +02:00
|
|
|
|
2015-01-29 03:20:14 +01:00
|
|
|
template <typename T1>
|
2016-01-27 14:50:17 +01:00
|
|
|
T0 operator() (T1 const& src) const
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
double src_offset = src.get_offset();
|
|
|
|
double src_scaling = src.get_scaling();
|
2015-06-16 04:36:36 +02:00
|
|
|
T0 dst(safe_cast<int>(src.width()), safe_cast<int>(src.height()), false);
|
2015-01-29 03:20:14 +01:00
|
|
|
dst.set_scaling(scaling_);
|
|
|
|
dst.set_offset(offset_);
|
2015-06-16 04:36:36 +02:00
|
|
|
for (std::size_t y = 0; y < dst.height(); ++y)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
2015-06-16 04:36:36 +02:00
|
|
|
for (std::size_t x = 0; x < dst.width(); ++x)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
2015-06-14 04:19:58 +02:00
|
|
|
double scaled_src_val = (safe_cast<double>(src(x,y)) * src_scaling) + src_offset;
|
2015-01-29 03:20:14 +01:00
|
|
|
double dst_val = (scaled_src_val - offset_) / scaling_;
|
2015-06-14 04:19:58 +02:00
|
|
|
dst(x,y) = safe_cast<dst_type>(dst_val);
|
2015-01-29 03:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return T0(std::move(dst));
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
double offset_;
|
|
|
|
double scaling_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end detail ns
|
|
|
|
|
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_any const& data, double offset, double scaling)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
return util::apply_visitor(detail::visitor_image_copy<T>(), data);
|
2015-01-29 03:20:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
return util::apply_visitor(detail::visitor_image_copy_so<T>(offset, scaling), data);
|
2015-01-29 03:20:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_rgba8 const& data, double offset, double scaling)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-01-29 03:20:14 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-01-29 03:20:14 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_gray8 const& data, double offset, double scaling)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-01-29 03:20:14 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-01-29 03:20:14 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 22:41:58 +01:00
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_gray8s const& data, double offset, double scaling)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 03:20:14 +01:00
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_gray16 const& data, double offset, double scaling)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-01-29 03:20:14 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-01-29 03:20:14 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 22:41:58 +01:00
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_gray16s const& data, double offset, double scaling)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_gray32 const& data, double offset, double scaling)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_gray32s const& data, double offset, double scaling)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-29 03:20:14 +01:00
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_gray32f const& data, double offset, double scaling)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-01-29 03:20:14 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-01-29 03:20:14 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-04 22:41:58 +01:00
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_gray64 const& data, double offset, double scaling)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_gray64s const& data, double offset, double scaling)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL T image_copy(image_gray64f const& data, double offset, double scaling)
|
2015-02-04 22:41:58 +01:00
|
|
|
{
|
|
|
|
if (offset == 0.0 && scaling == 1.0 && data.get_offset() == 0.0 && data.get_scaling() == 1.0)
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy<T> visit;
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-02-06 22:27:19 +01:00
|
|
|
detail::visitor_image_copy_so<T> visit(offset, scaling);
|
2015-02-04 22:41:58 +01:00
|
|
|
return visit(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-06 22:27:19 +01:00
|
|
|
MAPNIK_DECL image_any image_copy(image_any const& data, image_dtype type, double offset, double scaling)
|
2015-01-29 03:20:14 +01:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case image_dtype_rgba8:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_rgba8>(data, offset, scaling));
|
2015-01-29 03:20:14 +01:00
|
|
|
case image_dtype_gray8:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_gray8>(data, offset, scaling));
|
2015-02-04 22:41:58 +01:00
|
|
|
case image_dtype_gray8s:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_gray8s>(data, offset, scaling));
|
2015-01-29 03:20:14 +01:00
|
|
|
case image_dtype_gray16:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_gray16>(data, offset, scaling));
|
2015-02-04 22:41:58 +01:00
|
|
|
case image_dtype_gray16s:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_gray16s>(data, offset, scaling));
|
2015-02-04 22:41:58 +01:00
|
|
|
case image_dtype_gray32:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_gray32>(data, offset, scaling));
|
2015-02-04 22:41:58 +01:00
|
|
|
case image_dtype_gray32s:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_gray32s>(data, offset, scaling));
|
2015-01-29 03:20:14 +01:00
|
|
|
case image_dtype_gray32f:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_gray32f>(data, offset, scaling));
|
2015-02-04 22:41:58 +01:00
|
|
|
case image_dtype_gray64:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_gray64>(data, offset, scaling));
|
2015-02-04 22:41:58 +01:00
|
|
|
case image_dtype_gray64s:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_gray64s>(data, offset, scaling));
|
2015-02-04 22:41:58 +01:00
|
|
|
case image_dtype_gray64f:
|
2015-06-02 15:25:57 +02:00
|
|
|
return image_any(image_copy<image_gray64f>(data, offset, scaling));
|
2015-01-29 03:20:14 +01:00
|
|
|
case image_dtype_null:
|
|
|
|
throw std::runtime_error("Can not cast a null image");
|
2015-03-06 04:51:48 +01:00
|
|
|
case IMAGE_DTYPE_MAX:
|
|
|
|
default:
|
|
|
|
throw std::runtime_error("Can not cast unknown type");
|
|
|
|
|
2015-01-29 03:20:14 +01:00
|
|
|
}
|
|
|
|
throw std::runtime_error("Unknown image type passed");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end mapnik ns
|