color.cpp - port premultiply/demultiply and remove agg dependency

This commit is contained in:
artemp 2016-02-22 14:39:31 +01:00
parent 3e8ee9a559
commit 2c931a238f

View file

@ -24,8 +24,6 @@
#include <mapnik/color.hpp> #include <mapnik/color.hpp>
#include <mapnik/color_factory.hpp> #include <mapnik/color_factory.hpp>
#include <mapnik/config_error.hpp> #include <mapnik/config_error.hpp>
// agg
#include "agg_color_rgba.h"
#pragma GCC diagnostic push #pragma GCC diagnostic push
#include <mapnik/warning_ignore.hpp> #include <mapnik/warning_ignore.hpp>
@ -88,14 +86,25 @@ std::string color::to_hex_string() const
return str; return str;
} }
namespace {
static std::uint8_t multiply(std::uint8_t c, std::uint8_t a)
{
std::uint32_t t = c * a + 128;
return std::uint8_t(((t >> 8) + t) >> 8);
}
}
bool color::premultiply() bool color::premultiply()
{ {
if (premultiplied_) return false; if (premultiplied_) return false;
agg::rgba8 pre_c = agg::rgba8(red_,green_,blue_,alpha_); if (alpha_ != 255)
pre_c.premultiply(); {
red_ = pre_c.r; red_ = multiply(red_, alpha_);
green_ = pre_c.g; green_ = multiply(green_, alpha_);
blue_ = pre_c.b; blue_ = multiply(blue_, alpha_);
}
premultiplied_ = true; premultiplied_ = true;
return true; return true;
} }
@ -103,13 +112,23 @@ bool color::premultiply()
bool color::demultiply() bool color::demultiply()
{ {
if (!premultiplied_) return false; if (!premultiplied_) return false;
agg::rgba8 pre_c = agg::rgba8(red_,green_,blue_,alpha_); if (alpha_ < 255)
pre_c.demultiply(); {
red_ = pre_c.r; if (alpha_ == 0)
green_ = pre_c.g; {
blue_ = pre_c.b; red_ = green_ = blue_ = 0;
}
else
{
std::uint32_t r = (std::uint32_t(red_) * 255) / alpha_;
std::uint32_t g = (std::uint32_t(green_) * 255) / alpha_;
std::uint32_t b = (std::uint32_t(blue_) * 255) / alpha_;
red_ = (r > 255) ? 255 : r;
green_ = (g > 255) ? 255 : g;
blue_ = (b > 255) ? 255 : b;
}
}
premultiplied_ = false; premultiplied_ = false;
return true; return true;
} }
} }