Bresenham Algorithm Accurate Rendering by Subpixel Addressing By
Bresenham Algorithm & Accurate Rendering by Subpixel Addressing By Olin Lathrop David Kirk, Doug Voorhies, Hewlett-Packard 2005. 6. 23 Jiwon Choo
Contents Introduction n Bresenham’s Algorithm n Dropouts and overlaps n Subpixel Addressing Algorithm n Remaining Errors n Conclusion n
Introduction n Subpixel addressing algorithm n for eliminating most pixel positioning errors such as dropouts and overlaps n uses on Edge Walk Stage To find adapted pixels for edges of polygon
Bresenham’s Algorithm On y = mx + c, let’s restrict m as 0<m≤ 1 Then next pixel is always (xk+1, yk) or (xk+1, yk+1) We can choose one between two pixels with the value of (d 2 – d 1)
Bresenham’s Algorithm C 1 = 2 dy C 2 = 2(dy - dx) P = 2 dy - dx While(1≤k≤dx) If(p < 0) xk+1 = yk+1 = P=P else xk+1 = yk+1 = P=P // decision value (d 2 – d 1) //Starting point is (x 1, y 1) // d 2 > d 1 xk+1 yk + C 1 // d 2 ≤ d 1 xk+1 yk +1 + C 2
Bresenham’s Algorithm n Advantage : Fast n n n It requires only integer arithmetic for setup calculations. Also has no multiplication or division in loop Disadvantage : Inaccurate n n It can produce pixel positioning errors up to one half of a pixel. It can occur dropouts and overlaps.
Dropouts and Overlaps n Dropout n n Some pixels are not written where they should be. Overlap n Some pixels are written more than once, even though they were inside only one polygon.
Dropouts and Overlaps n Problems Ragged color contours n Ragged intersection edge n Shading inaccuracies n Z-buffer poke-throughs n n Common Solution n Growing polygon
Subpixel Addressing Algorithm n Initial Values n n Maj 0 = round(Majs)+1/2 Min 0 = (Dmin/Dmaj)(Maj 0 -Majs)+Mins
Subpixel Addressing Algorithm n Slope fraction Algorithm n n n Imaj Trunc(Maj 0) Min 0 Loop n Imin Trunc(Min) n Draw(Imaj, Imin) n Imaj + 1 n Min + Dmin/Dmaj n Repeat #3 a (1 a) (2 a) (3 a) (4 a) (5 a) (6 a) (7 a)
Subpixel Addressing Algorithm n Slope fraction Algorithm with E (1) n n Imaj Trunc(Maj 0) Imin Trunc(Min 0) E frac(Min 0) Loop n Draw(Imaj, Imin) n Imaj + 1 n Imin + Trunc(Dmin/Dmaj) n E E+frac(Dmin/Dmaj) n if E ≥ 1 then n Imin +1 n E E-1 n Repeat from #4 b (1 b) (2 b) (3 b) (4 b) (5 b) (6 b) (7 b) (8 b) (9 b) (10 b) (11 b)
Subpixel Addressing Algorithm n Slope fraction Algorithm with E (2) n n Imaj Trunc(Maj 0) Imin Trunc(Min 0) E frac(Min 0) + frac(Dmin/Dmaj) Loop n Draw(Imaj, Imin) n Imaj + 1 n if E ≥ 1 n then n Imin + Trunc(Dmin/Dmaj) +1 n E E+ frac(Dmin/Dmaj) -1 n Else n Imin + Trunc(Dmin/Dmaj) n E E+ frac(Dmin/Dmaj) n Repeat from #4 c (1 c) (2 c) (3 c) (4 c) (5 c) (6 c) (7 c) (8 c) (9 c) (10 c) (11 c) (12 c) (13 c)
Subpixel Addressing Algorithm n Slope fraction Algorithm with E and constant values n n n n Imaj Trunc(Maj 0) Imin Trunc(Min 0) E frac(Min 0) + frac(Dmin/Dmaj) -1 DEA = frac(Dmin/Dmaj) DEB = frac(Dmin/Dmaj) -1 Astep = Trunc(Dmin/Dmaj) Bstep = Astep + 1 Loop n n n Draw(Imaj, Imin) Imaj + 1 if E ≥ 0 n n Else n n n then Imin + Bstep E E+ DEB (1 d) (2 d) (3 d) (4 d) (5 d) (6 d) (7 d) (8 d) (9 d) (10 d) (11 d) (12 d) (13 d) (14 d) Imin + Astep E E+ DEA Repeat from #8 d (15 d) (16 d) (17 d)
Subpixel Addressing Algorithm n Making all Integer setup for Bresenham’s algorithm n If vector is the first octant and starting point is on a pixel center(Min 0 = ½) then 1 d~3 d becomes E ½ + (Dmin/Dmaj) -1 n DEA = Dmin/Dmaj n DEB = (Dmin/Dmaj)-1 n n (1 e) (2 e) (3 e) All Integer setup E 2 ∙ Dmin -Dmaj n DEA = 2 ∙ Dmin n DEB = 2 (Dmin -Dmaj ) n (1 f) (2 f) (3 f)
Bresenham’s Algorithm C 1 = 2 dy C 2 = 2(dy - dx) P = 2 dy - dx While(1≤k≤dx) If(p < 0) xk+1 = yk+1 = P=P else xk+1 = yk+1 = P=P //DEA //DEB //E //Starting point is (x 1, y 1) // d 2 > d 1 xk+1 yk + C 1 // d 2 ≤ d 1 xk+1 yk +1 + C 2
Subpixel Addressing Algorithm n E, DEA, DEB must become integers for easy hardware execution n Apply scale factor to 3 d~5 d Scale factor – maximize the number of bits used in the integer registers in the hardware Also uses these characters of E, DEA, DEB < 0 < DEA n DEA = DEB+1 n DEB ≤ E ≤ DEA n
Subpixel Addressing Algorithm n n n n Imaj Trunc(Maj 0) Imin Trunc(Min 0) (1 g) (2 g) Astep = Trunc(Dmin/Dmaj) Bstep = Astep + 1 Loop n Draw(Imaj, Imin) n Imaj + 1 n if E ≥ 0 n then n Imin + Bstep n E E+ DEB n Else n Imin + Astep n E E+ DEA n Repeat from #8 g (6 g) (7 g) E round ((frac(Min 0) + frac(Dmin/Dmaj) -1)∙ 32767) DEA = round(frac(Dmin/Dmaj) ∙ 32767) DEB = frac(Dmin/Dmaj) -32767 (3 g) (4 g) (5 g) (8 g) (9 g) (10 g) (11 g) (12 g) (13 g) (14 g) (15 g) (16 g) (17 g)
Remaining Errors n Using round functions n n Do not cause dropout problem - DEA, DEB are depend on only the slope of the ideal line. For 16 bit E, this implies a resolutions about 1/16000 pixel
Remaining Errors n Errors at pixel Center
Conclusion n As we discussed above, with subpixel algorithm, we can solve the general inaccuracy problems of using Bresenham’s algorithm at it’s high speed.
- Slides: 20