Moved set_color_to_alpha and set_grayscale_to_alpha to image_util.

Ref # 2633
This commit is contained in:
Blake Thompson 2015-01-15 20:26:20 -06:00
parent 3b1c99ff1b
commit 33ccc12355
5 changed files with 143 additions and 53 deletions

View file

@ -36,6 +36,7 @@
// mapnik
#include <mapnik/graphics.hpp>
#include <mapnik/color.hpp>
#include <mapnik/palette.hpp>
#include <mapnik/image.hpp>
#include <mapnik/image_util.hpp>
@ -188,6 +189,16 @@ std::shared_ptr<image_32> frombuffer(PyObject * obj)
throw mapnik::image_reader_exception("Failed to load image from buffer" );
}
void set_grayscale_to_alpha(image_32 & im)
{
mapnik::set_grayscale_to_alpha(im.data());
}
void set_color_to_alpha(image_32 & im, mapnik::color const& c)
{
mapnik::set_color_to_alpha(im.data(), c);
}
void set_alpha(image_32 & im, float opacity)
{
mapnik::set_alpha(im.data(), opacity);
@ -308,8 +319,8 @@ void export_image()
.add_property("background",make_function
(&image_32::get_background,return_value_policy<copy_const_reference>()),
&image_32::set_background, "The background color of the image.")
.def("set_grayscale_to_alpha",&image_32::set_grayscale_to_alpha, "Set the grayscale values to the alpha channel of the Image")
.def("set_color_to_alpha",&image_32::set_color_to_alpha, "Set a given color to the alpha channel of the Image")
.def("set_grayscale_to_alpha",&set_grayscale_to_alpha, "Set the grayscale values to the alpha channel of the Image")
.def("set_color_to_alpha",&set_color_to_alpha, "Set a given color to the alpha channel of the Image")
.def("set_alpha",&set_alpha, "Set the overall alpha channel of the Image")
.def("composite",&composite,
( arg("self"),

View file

@ -74,10 +74,6 @@ public:
void set_background(const color& c);
void set_grayscale_to_alpha();
void set_color_to_alpha(color const& c);
inline const image_data_rgba8& data() const
{
return data_;

View file

@ -127,10 +127,10 @@ template <typename T>
MAPNIK_DECL void set_alpha (T & image, float opacity);
template <typename T>
MAPNIK_DECL void set_grayscale_to_alpha (T const& image);
MAPNIK_DECL void set_grayscale_to_alpha (T & image);
template <typename T>
MAPNIK_DECL void set_color_to_alpha (T const& image, color const& c);
MAPNIK_DECL void set_color_to_alpha (T & image, color const& c);
inline bool is_png(std::string const& filename)
{

View file

@ -53,45 +53,6 @@ image_32::image_32(image_data_rgba8 && data)
image_32::~image_32() {}
void image_32::set_grayscale_to_alpha()
{
for (unsigned int y = 0; y < data_.height(); ++y)
{
unsigned int* row_from = data_.getRow(y);
for (unsigned int x = 0; x < data_.width(); ++x)
{
unsigned rgba = row_from[x];
unsigned r = rgba & 0xff;
unsigned g = (rgba >> 8 ) & 0xff;
unsigned b = (rgba >> 16) & 0xff;
// magic numbers for grayscale
unsigned a = static_cast<unsigned>(std::ceil((r * .3) + (g * .59) + (b * .11)));
row_from[x] = (a << 24)| (255 << 16) | (255 << 8) | (255) ;
}
}
}
void image_32::set_color_to_alpha(const color& c)
{
for (unsigned y = 0; y < data_.height(); ++y)
{
unsigned int* row_from = data_.getRow(y);
for (unsigned x = 0; x < data_.width(); ++x)
{
unsigned rgba = row_from[x];
unsigned r = rgba & 0xff;
unsigned g = (rgba >> 8 ) & 0xff;
unsigned b = (rgba >> 16) & 0xff;
if (r == c.red() && g == c.green() && b == c.blue())
{
row_from[x] = 0;
}
}
}
}
void image_32::set_background(const color& c)
{
background_=c;

View file

@ -625,12 +625,6 @@ void visitor_set_alpha::operator()<image_data_rgba8> (image_data_rgba8 & data)
}
}
template <>
void visitor_set_alpha::operator()<image_data_null> (image_data_null &)
{
throw std::runtime_error("Can not set alpha for null image");
}
} // end detail ns
template<>
@ -659,4 +653,132 @@ MAPNIK_DECL void set_alpha<image_data_rgba8> (image_data_rgba8 & data, float opa
}
}
namespace detail {
struct visitor_set_grayscale_to_alpha
{
template <typename T>
void operator() (T & data)
{
throw std::runtime_error("Error: set_grayscale_to_alpha with " + std::string(typeid(data).name()) + " is not supported");
}
};
template <>
void visitor_set_grayscale_to_alpha::operator()<image_data_rgba8> (image_data_rgba8 & data)
{
using pixel_type = typename image_data_rgba8::pixel_type;
for (unsigned int y = 0; y < data.height(); ++y)
{
pixel_type* row_from = data.getRow(y);
for (unsigned int x = 0; x < data.width(); ++x)
{
pixel_type rgba = row_from[x];
pixel_type r = rgba & 0xff;
pixel_type g = (rgba >> 8 ) & 0xff;
pixel_type b = (rgba >> 16) & 0xff;
// magic numbers for grayscale
pixel_type a = static_cast<pixel_type>(std::ceil((r * .3) + (g * .59) + (b * .11)));
row_from[x] = (a << 24)| (255 << 16) | (255 << 8) | (255) ;
}
}
}
} // end detail ns
template<>
MAPNIK_DECL void set_grayscale_to_alpha<image_data_any> (image_data_any & data)
{
// Prior to calling the data must not be premultiplied
bool remultiply = mapnik::demultiply_alpha(data);
util::apply_visitor(detail::visitor_set_grayscale_to_alpha(), data);
if (remultiply)
{
mapnik::premultiply_alpha(data);
}
}
// TEMPORARY can be removed once image_data_any is only way it is being passed.
template<>
MAPNIK_DECL void set_grayscale_to_alpha<image_data_rgba8> (image_data_rgba8 & data)
{
// Prior to calling the data must not be premultiplied
bool remultiply = mapnik::demultiply_alpha(data);
detail::visitor_set_grayscale_to_alpha visit;
visit(data);
if (remultiply)
{
mapnik::premultiply_alpha(data);
}
}
namespace detail {
struct visitor_set_color_to_alpha
{
visitor_set_color_to_alpha(color const& c)
: c_(c) {}
template <typename T>
void operator() (T & data)
{
throw std::runtime_error("Error: set_color_to_alpha with " + std::string(typeid(data).name()) + " is not supported");
}
private:
color const& c_;
};
template <>
void visitor_set_color_to_alpha::operator()<image_data_rgba8> (image_data_rgba8 & data)
{
using pixel_type = typename image_data_rgba8::pixel_type;
for (unsigned y = 0; y < data.height(); ++y)
{
pixel_type* row_from = data.getRow(y);
for (unsigned x = 0; x < data.width(); ++x)
{
pixel_type rgba = row_from[x];
pixel_type r = rgba & 0xff;
pixel_type g = (rgba >> 8 ) & 0xff;
pixel_type b = (rgba >> 16) & 0xff;
if (r == c_.red() && g == c_.green() && b == c_.blue())
{
row_from[x] = 0;
}
}
}
}
} // end detail ns
template<>
MAPNIK_DECL void set_color_to_alpha<image_data_any> (image_data_any & data, color const& c)
{
// Prior to calling the data must not be premultiplied
bool remultiply = mapnik::demultiply_alpha(data);
util::apply_visitor(detail::visitor_set_color_to_alpha(c), data);
if (remultiply)
{
mapnik::premultiply_alpha(data);
}
}
// TEMPORARY can be removed once image_data_any is only way it is being passed.
template<>
MAPNIK_DECL void set_color_to_alpha<image_data_rgba8> (image_data_rgba8 & data, color const& c)
{
// Prior to calling the data must not be premultiplied
bool remultiply = mapnik::demultiply_alpha(data);
detail::visitor_set_color_to_alpha visit(c);
visit(data);
if (remultiply)
{
mapnik::premultiply_alpha(data);
}
}
} // end ns