Commit 0b5cb827 authored by Davis King's avatar Davis King

Made the version of draw_line() that draws onto a regular image use alpha

blending for drawing diagonal lines.
parent 41309698
...@@ -68,6 +68,13 @@ namespace dlib ...@@ -68,6 +68,13 @@ namespace dlib
} }
else else
{ {
// This part is a little more complicated because we are going to perform alpha
// blending so the diagonal lines look nice.
const rectangle valid_area = get_rect(c);
rgb_alpha_pixel alpha_pixel;
assign_pixel(alpha_pixel, val);
const unsigned char max_alpha = alpha_pixel.alpha;
const long rise = (((long)y2) - ((long)y1)); const long rise = (((long)y2) - ((long)y1));
const long run = (((long)x2) - ((long)x1)); const long run = (((long)x2) - ((long)x1));
if (std::abs(rise) < std::abs(run)) if (std::abs(rise) < std::abs(run))
...@@ -80,13 +87,13 @@ namespace dlib ...@@ -80,13 +87,13 @@ namespace dlib
if (x1 > x2) if (x1 > x2)
{ {
first = x2; first = std::max(x2,valid_area.left());
last = x1; last = std::min(x1,valid_area.right());
} }
else else
{ {
first = x1; first = std::max(x1,valid_area.left());
last = x2; last = std::min(x2,valid_area.right());
} }
long y; long y;
...@@ -95,18 +102,23 @@ namespace dlib ...@@ -95,18 +102,23 @@ namespace dlib
const double y1f = y1; const double y1f = y1;
for (double i = first; i <= last; ++i) for (double i = first; i <= last; ++i)
{ {
y = static_cast<long>(slope*(i-x1f) + y1f); const double dy = slope*(i-x1f) + y1f;
x = static_cast<long>(i); const double dx = i;
if (y < 0 || y >= c.nr()) y = static_cast<long>(dy);
continue; x = static_cast<long>(dx);
if (x < 0 || x >= c.nc())
continue;
if (y >= valid_area.top() && y <= valid_area.bottom())
assign_pixel(c[y][x] , val); {
alpha_pixel.alpha = static_cast<unsigned char>((1.0-(dy-y))*max_alpha);
assign_pixel(c[y][x], alpha_pixel);
}
if (y+1 >= valid_area.top() && y+1 <= valid_area.bottom())
{
alpha_pixel.alpha = static_cast<unsigned char>((dy-y)*max_alpha);
assign_pixel(c[y+1][x], alpha_pixel);
}
} }
} }
else else
...@@ -119,33 +131,37 @@ namespace dlib ...@@ -119,33 +131,37 @@ namespace dlib
if (y1 > y2) if (y1 > y2)
{ {
first = y2; first = std::max(y2,valid_area.top());
last = y1; last = std::min(y1,valid_area.bottom());
} }
else else
{ {
first = y1; first = std::max(y1,valid_area.top());
last = y2; last = std::min(y2,valid_area.bottom());
} }
long x; long x;
long y; long y;
const double x1f = x1; const double x1f = x1;
const double y1f = y1; const double y1f = y1;
for (double i = first; i <= last; ++i) for (double i = first; i <= last; ++i)
{ {
x = static_cast<long>(slope*(i-y1f) + x1f); const double dx = slope*(i-y1f) + x1f;
y = static_cast<long>(i); const double dy = i;
if (x < 0 || x >= c.nc()) y = static_cast<long>(dy);
continue; x = static_cast<long>(dx);
if (y < 0 || y >= c.nr()) if (x >= valid_area.left() && x <= valid_area.right())
continue; {
alpha_pixel.alpha = static_cast<unsigned char>((1.0-(dx-x))*max_alpha);
assign_pixel(c[y][x] , val); assign_pixel(c[y][x], alpha_pixel);
}
if (x+1 >= valid_area.left() && x+1 <= valid_area.right())
{
alpha_pixel.alpha = static_cast<unsigned char>((dx-x)*max_alpha);
assign_pixel(c[y][x+1], alpha_pixel);
}
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment