[DEBUG] try to corect color merge ...

This commit is contained in:
Edouard DUPIN 2015-12-01 23:11:51 +01:00
parent 7037a6980b
commit 3ebd82c71d
4 changed files with 36 additions and 17 deletions

View File

@ -266,13 +266,13 @@ void esvg::Base::parsePaintAttr(const std::shared_ptr<const exml::Element>& _ele
if (content.size()!=0) {
float opacity = parseLength(content);
opacity = std::avg(0.0f, opacity, 1.0f);
m_paint.fill.setA(opacity*0xFF);
m_paint.fill.setA(opacity);
}
content = _element->getAttribute("stroke-opacity");
if (content.size()!=0) {
float opacity = parseLength(content);
opacity = std::avg(0.0f, opacity, 1.0f);
m_paint.stroke.setA(opacity*0xFF);
m_paint.stroke.setA(opacity);
}
content = _element->getAttribute("fill-rule");
if (content.size()!=0) {
@ -346,12 +346,12 @@ void esvg::Base::parsePaintAttr(const std::shared_ptr<const exml::Element>& _ele
} else if (outputType == "fill-opacity") {
float opacity = parseLength(outputValue);
opacity = std::avg(0.0f, opacity, 1.0f);
m_paint.fill.setA(opacity*0xFF);
m_paint.fill.setA(opacity);
SVG_VERBOSE(" input : \"" << outputValue << "\" == > " << m_paint.fill);
} else if (outputType == "stroke-opacity") {
float opacity = parseLength(outputValue);
opacity = std::avg(0.0f, opacity, 1.0f);
m_paint.stroke.setA(opacity*0xFF);
m_paint.stroke.setA(opacity);
SVG_VERBOSE(" input : \"" << outputValue << "\" == > " << m_paint.stroke);
} else if (outputType == "fill-rule" ) {
if (outputValue == "nonzero" ) {

View File

@ -47,8 +47,8 @@ namespace esvg {
PaintState();
void clear();
public:
etk::Color<uint8_t,4> fill;
etk::Color<uint8_t,4> stroke;
etk::Color<float,4> fill;
etk::Color<float,4> stroke;
float strokeWidth;
bool flagEvenOdd; //!< Fill rules
enum esvg::cap lineCap;

View File

@ -36,11 +36,14 @@ esvg::Renderer::~Renderer() {
m_buffer.clear();
m_size = ivec2(0,0);
}
#if 0
etk::Color<float,4> esvg::Renderer::mergeColor(etk::Color<float,4> _base, const etk::Color<float,4>& _integration) {
if (_integration.a() == 0.0f) {
return _base;
}
if (_base.a() == 0.0f) {
return _integration;
}
etk::Color<float,4> tmpColor(_integration.r()*_integration.a(),
_integration.g()*_integration.a(),
_integration.b()*_integration.a(),
@ -52,7 +55,18 @@ etk::Color<float,4> esvg::Renderer::mergeColor(etk::Color<float,4> _base, const
_base.setA(tmpA);
return _base;
}
#else
etk::Color<float,4> esvg::Renderer::mergeColor(etk::Color<float,4> _base, const etk::Color<float,4>& _integration) {
etk::Color<float,4> result;
float a1 = _base.a();
float a2 = _integration.a();
result.setR(a1 * _base.r() + a2 * (1.0f - a1) * _integration.r());
result.setG(a1 * _base.g() + a2 * (1.0f - a1) * _integration.g());
result.setB(a1 * _base.b() + a2 * (1.0f - a1) * _integration.b());
result.setA(a1 + a2 * (1.0f - a1));
return result;
}
#endif
void esvg::Renderer::print(const esvg::render::Weight& _weightFill,
const etk::Color<float,4>& _colorFill,
const esvg::render::Weight& _weightStroke,
@ -77,7 +91,8 @@ void esvg::Renderer::print(const esvg::render::Weight& _weightFill,
float valueStroke = _weightStroke.get(pos);
if (valueStroke != 0.0f) {
// set a ratio of the merging value
etk::Color<float,4> tmpColor = _colorStroke * valueStroke * _opacity;
etk::Color<float,4> tmpColor = _colorStroke * valueStroke;
tmpColor.setA(tmpColor.a() * _opacity);
// integrate the value at the previous color
m_buffer[sizeX*yyy + xxx] = mergeColor(m_buffer[sizeX*yyy + xxx], tmpColor);
}
@ -97,7 +112,8 @@ void esvg::Renderer::print(const esvg::render::Weight& _weightFill,
float valueFill = _weightFill.get(pos);
if (valueFill != 0.0f) {
// set a ratio of the merging value
etk::Color<float,4> tmpColor = _colorFill * valueFill * _opacity;
etk::Color<float,4> tmpColor = _colorFill * valueFill;
tmpColor.setA(tmpColor.a() * _opacity);
// integrate the value at the previous color
m_buffer[sizeX*yyy + xxx] = mergeColor(m_buffer[sizeX*yyy + xxx], tmpColor);
}
@ -115,9 +131,12 @@ void esvg::Renderer::print(const esvg::render::Weight& _weightFill,
float valueFill = _weightFill.get(pos);
float valueStroke = _weightStroke.get(pos);
// calculate merge of stroke and fill value:
etk::Color<float,4> intermediateColorFill = _colorFill*valueFill;
etk::Color<float,4> intermediateColorStroke = _colorStroke*valueStroke;
etk::Color<float,4> intermediateColor = mergeColor(intermediateColorFill, intermediateColorStroke) * _opacity;
etk::Color<float,4> intermediateColorFill = _colorFill;
intermediateColorFill.setA(intermediateColorFill.a()*valueFill);
etk::Color<float,4> intermediateColorStroke = _colorStroke;
intermediateColorStroke.setA(intermediateColorStroke.a()*valueStroke);
etk::Color<float,4> intermediateColor = mergeColor(intermediateColorFill, intermediateColorStroke);
intermediateColor.setA(intermediateColor.a() * _opacity);
m_buffer[sizeX*yyy + xxx] = mergeColor(m_buffer[sizeX*yyy + xxx], intermediateColor);
}
}

View File

@ -49,7 +49,7 @@ TEST(TestRectangle, fill_and_stroke) {
TEST(TestRectangle, fill_and_stroke_blend) {
std::string data("<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
"<svg height='100' width='100'>"
" <rect x='12.5' y='12.5' width='75' height='50' stroke='#0F08' stroke-width='3' fill='#F008' />"
" <rect x='12.5' y='12.5' width='75' height='50' stroke='#0F0' stroke-opacity='0.5' stroke-width='3' fill='#F00' fill-opacity='0.5' />"
"</svg>");
esvg::Document doc;
doc.parse(data);
@ -60,12 +60,12 @@ TEST(TestRectangle, fill_and_stroke_blend) {
TEST(TestRectangle, fill_and_stroke_opacity) {
std::string data("<?xml version='1.0' encoding='UTF-8' standalone='no'?>"
"<svg height='100' width='100'>"
" <rect x='12.5' y='12.5' width='75' height='50' stroke='#0F0F' stroke-width='3' fill='#F00F' opacity='0.5' />"
" <rect x='12.5' y='12.5' width='75' height='50' stroke='#0F0' stroke-width='3' fill='#F00' opacity='0.5' />"
"</svg>");
esvg::Document doc;
doc.parse(data);
etk::FSNodeWriteAllData("TestRectangle_fill_and_stroke_blend.svg", data);
doc.generateAnImage(ivec2(100, 100), "TestRectangle_fill_and_stroke_blend.bmp", g_visualDebug);
etk::FSNodeWriteAllData("TestRectangle_fill_and_stroke_opacity.svg", data);
doc.generateAnImage(ivec2(100, 100), "TestRectangle_fill_and_stroke_opacity.bmp", g_visualDebug);
}
TEST(TestRectangle, corned_fill) {