diff --git a/c++/perspective/src/generator.cpp b/c++/perspective/src/generator.cpp index 83bf8aa..4e0f23a 100644 --- a/c++/perspective/src/generator.cpp +++ b/c++/perspective/src/generator.cpp @@ -348,7 +348,7 @@ Generator::generate(GenSurface &target, const GenSurface &source) const { void -Generator::generate(Surface &target, const Pair2 &bounds) const { +Generator::generate(Surface &target, const Pair2 &bounds, bool checks) const { if (target.empty()) return; const int w = target.width(); @@ -365,6 +365,9 @@ Generator::generate(Surface &target, const Pair2 &bounds) const { d.x /= w; d.y /= h; const Vector2 precision(fabs(d.x/2.0), fabs(d.x/2.0)); + const Vector2 precision_inv( + 1/std::max(precision.x, real_precision), + 1/std::max(precision.y, real_precision) ); const Real precision_max = std::max(precision.x, precision.y); Color gray(0.5, 0.5, 0.5, 0.5); @@ -419,15 +422,29 @@ Generator::generate(Surface &target, const Pair2 &bounds) const { ULongInt rr = (ULongInt(round(p.y*one)) - offset.y)/step; assert(rr < gen_surface.height()); + const Real ay = checks ? checks_value(p.y, precision_inv.y) : 1; for(int c = 0; c < target.width(); ++c, ++pixel, p.x += d.x) { ULongInt cc = (ULongInt(round(p.x*one)) - offset.x)/step; assert(cc < gen_surface.width()); + const Real ax = checks ? checks_value(p.x, precision_inv.x) : 1; + const Real a = (ax*ay + 1)*0.5; + const Vector4 &v = gen_surface[rr][cc]; - *pixel = Color( flip_clamp((v.x + 1)*0.5), - flip_clamp((v.y + 1)*0.5), - flip_clamp((v.z + 1)*0.5), - flip_clamp((v.w + 1)*0.5) )*alpha + gray; + const Color color = Color( flip_clamp((v.x + 1)*0.5), + flip_clamp((v.y + 1)*0.5), + flip_clamp((v.z + 1)*0.5), + flip_clamp((v.w + 1)*0.5) )*alpha + gray; + if (a + real_precision >= 1) { + *pixel = color; + } else { + const Color back_color = Color(1 - color.r, 1 - color.g, 1 - color.b, 1 - color.a); + if (a - real_precision <= 0) { + *pixel = back_color; + } else { + *pixel = color*a + back_color*(1 - a); + } + } } } } diff --git a/c++/perspective/src/generator.h b/c++/perspective/src/generator.h index b79519a..6718ac5 100644 --- a/c++/perspective/src/generator.h +++ b/c++/perspective/src/generator.h @@ -114,7 +114,7 @@ public: Vector4 generate(const ULongIntVector2 &coord, bool shrink_cache = true) const; Vector4 generate(const Vector2 &coord, const Vector2 &precision) const; - void generate(Surface &target, const Pair2 &bounds) const; + void generate(Surface &target, const Pair2 &bounds, bool checks = false) const; void generate(GenSurface &target) const; void generate(GenSurface &target, const GenSurface &source) const; diff --git a/c++/perspective/src/mainwindow.cpp b/c++/perspective/src/mainwindow.cpp index 85632e0..d40780d 100644 --- a/c++/perspective/src/mainwindow.cpp +++ b/c++/perspective/src/mainwindow.cpp @@ -10,7 +10,8 @@ #include "mainwindow.h" -const bool checks = true; +const bool checks = false; +const bool checks_on_generator = true; const bool paint_outside_bounds = true; @@ -114,7 +115,7 @@ MainWindow::update_src_surface() { } else { DataSurface surface(w, h); if (checks) generator.generate_checks(surface, rect); - else generator.generate(surface, rect); + else generator.generate(surface, rect, checks_on_generator); src_surface = surface.to_cairo_surface(); } @@ -205,7 +206,7 @@ MainWindow::update_dst_surface() { for(Perspective::LayerList::const_iterator i = layers.begin(); i != layers.end(); ++i) { DataSurface layer_surface(i->src_size.x, i->src_size.y); if (checks) generator.generate_checks(layer_surface, i->src_bounds); - else generator.generate(layer_surface, i->src_bounds); + else generator.generate(layer_surface, i->src_bounds, checks_on_generator); layer_surface.mult_alpha(); Perspective::add_premulted(*i, layer_surface, surface); }