Skip to content

Commit 5c3c31e

Browse files
author
Adrià Arrufat
committed
draw: fix alpha blending when line width > 1
1 parent 9c744b7 commit 5c3c31e

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/draw.zig

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,37 @@ pub fn drawLine(comptime T: type, image: Image(T), p1: Point2d, p2: Point2d, wid
7373
const y = @floor(dy);
7474
const x = @floor(dx);
7575
if (y >= 0 and y <= rows - 1) {
76-
c2.a = @intFromFloat((1 - (dy - y)) * max_alpha);
7776
var j = -half_width;
7877
while (j <= half_width) : (j += 1) {
7978
const py = y + j;
8079
const pos = as(usize, py) * image.cols + as(usize, x);
8180
if (py >= 0 and py < rows) {
81+
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
8282
if (j == -half_width or j == half_width) {
83-
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
83+
c2.a = @intFromFloat((1 - (dy - y)) * max_alpha);
8484
c1.blend(c2);
8585
image.data[pos] = Color.convert(T, c1);
8686
} else {
87-
image.data[pos] = color;
87+
c1.blend(c2);
88+
image.data[pos] = Color.convert(T, c1);
8889
}
8990
}
9091
}
9192
}
9293
if (y + 1 >= 0 and y + 1 <= rows - 1) {
93-
c2.a = @intFromFloat((dy - y) * max_alpha);
9494
var j = -half_width;
9595
while (j <= half_width) : (j += 1) {
9696
const py = y + 1 + j;
9797
if (py >= 0 and py < rows) {
9898
const pos = as(usize, py) * image.cols + as(usize, x);
99+
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
99100
if (j == -half_width or j == half_width) {
100-
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
101+
c2.a = @intFromFloat((dy - y) * max_alpha);
101102
c1.blend(c2);
102103
image.data[pos] = Color.convert(T, c1);
103104
} else {
104-
image.data[pos] = color;
105+
c1.blend(c2);
106+
image.data[pos] = Color.convert(T, c1);
105107
}
106108
}
107109
}
@@ -118,18 +120,19 @@ pub fn drawLine(comptime T: type, image: Image(T), p1: Point2d, p2: Point2d, wid
118120
const y = @floor(dy);
119121
const x = @floor(dx);
120122
if (x >= 0 and x <= cols - 1) {
121-
c2.a = @intFromFloat((1 - (dx - x)) * max_alpha);
122123
var j = -half_width;
123124
while (j <= half_width) : (j += 1) {
124125
const px = x + j;
125126
const pos = as(usize, y) * image.cols + as(usize, px);
126127
if (px >= 0 and px < cols) {
128+
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
127129
if (j == -half_width or j == half_width) {
128-
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
130+
c2.a = @intFromFloat((1 - (dx - x)) * max_alpha);
129131
c1.blend(c2);
130132
image.data[pos] = Color.convert(T, c1);
131133
} else {
132-
image.data[pos] = color;
134+
c1.blend(c2);
135+
image.data[pos] = Color.convert(T, c1);
133136
}
134137
}
135138
}
@@ -141,12 +144,13 @@ pub fn drawLine(comptime T: type, image: Image(T), p1: Point2d, p2: Point2d, wid
141144
const px = x + 1 + j;
142145
const pos = as(usize, y) * image.cols + as(usize, px);
143146
if (px >= 0 and px < cols) {
147+
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
144148
if (j == -half_width or j == half_width) {
145-
var c1: Rgba = Color.convert(Rgba, image.data[pos]);
146149
c1.blend(c2);
147150
image.data[pos] = Color.convert(T, c1);
148151
} else {
149-
image.data[pos] = color;
152+
c1.blend(c2);
153+
image.data[pos] = Color.convert(T, c1);
150154
}
151155
}
152156
}

0 commit comments

Comments
 (0)