Dithering and Raster Graphics Dithering Dithering Methods (Digital
Total Page:16
File Type:pdf, Size:1020Kb
Dithering missing colors are simulated by 1st color mixing existing colors Dithering and Raster Graphics Special Aspects of Raster Images nd 2 color missing color Eduard Gröller, Thomas Theußl 2 /54 Dithering Methods Dithering in Printing Industry (Digital Halftoning) ● newspapers ■ threshold dithering black ink on light paper, rasterization of the ◆ ordered dither images enables also grey levels, equal point ◆ stochastic dither density everywhere, variable size ◆ dot diffusion ● color printing ◆ .... every primary color is rasterized separately, ■ error diffusion dithering different printing angles ensure unbiased results (Floyd-Steinberg) Eduard Gröller, Thomas Theußl 3 /54 Eduard Gröller, Thomas Theußl 4 /54 Threshold Dithering Constant Threshold Dithering every pixel is compared to a threshold t: sample image threshold values result p < t a 1 7 6 5 4.5 4.5 4.5 4.5 0 9 9 9 p > t b 1 6 5 4 4.5 4.5 4.5 4.5 0 9 9 0 t can be: 1 5 4 3 4.5 4.5 4.5 4.5 0 9 0 0 ● equal everywhere (e.g. (b–a)/2, 1 4 2 1 4.5 4.5 4.5 4.5 0 0 0 0 arbitrary value, mean value, median, ...) ● location dependent (values between 0 and 9) (defined locally or globally) corresponds to rounding Eduard Gröller, Thomas Theußl 5 /54 Eduard Gröller, Thomas Theußl 6 /54 Principle of Dithering Principle of Dithering (2) available values a, b a x b missing value x between a and b shall be simulated by mixing a-pixels and b-pixels x - a b - x b - a a x b to produce color value x there have to be: 100 * x – a % b-pixels b – a 100 * b – x % a-pixels b – a Eduard Gröller, Thomas Theußl 7 /54 Eduard Gröller, Thomas Theußl 8 /54 Dithering a Uniform Area Dithering a Uniform Area (2) for a uniform area regular application of This can be done by using a different threshold for every pixel (using the interval borders) this pattern will produce 1/8 5/8 1/8 5/8 1/8 5/8 this grey tone 0 1/4 1/2 3/4 1 7/8 3/8 7/8 3/8 7/8 ... interval borders 1/8 3/8 5/8 7/8 1/8 5/8 1/8 = "Threshold Matrix" all grey levels in this interval ... will be mapped to 1/4 Eduard Gröller, Thomas Theußl 9 /54 Eduard Gröller, Thomas Theußl 10 /54 Threshold Matrix Dither Matrix Example distances between interval borders are equal, dither matrix ➞ threshold matrix therefore it suffices to define the sequence of the pixel values in the matrix: 15 5 7 7 2 3 18 18 18 1/8 5/8 0 2 0 4 8 1 9 17 instead of only 18 18 18 5 6 1 11 13 3 7/8 3/8 3 1 18 18 18 i.e. for an nxn matrix: values ∈ [0,n2–1] 2k+1 2k+1 value k corresponds to threshold value: value k corresponds to threshold value: 2n2 2n2 Eduard Gröller, Thomas Theußl 11 /54 Eduard Gröller, Thomas Theußl 12 /54 Threshold Matrix Dithering Example Generation of Threshold Matrices (1) recursive method: 4 copies of smaller matrices sample image threshold values result 0 2 1 7 6 5 1.1 5.6 1.1 5.6 0 9 9 0 ... k= 3 1 4k+0 4k+2 1 6 5 4 7.9 3.4 7.9 3.4 0 9 0 9 0(1)n2-1 1 5 4 3 1.1 5.6 1.1 5.6 0 0 9 0 1 4 7.9 3.4 7.9 3.4 9 0 0 2 1 0 example: 0 8 2 10 4k+3 4k+1 12 4 14 6 (values between 0 and 9) 3 11 1 9 15 7 13 5 Eduard Gröller, Thomas Theußl 13 /54 Eduard Gröller, Thomas Theußl 14 /54 Generation of Threshold Matrices (2) Dithering between Grey Levels direct method: use of magic squares a x b threshold values have to lie between a and b: example 0 14 3 13 11 5 8 6 k a + 2k+1 ⋅ (b-a) 12 2 15 1 2n2 7 9 4 10 calculation is done separately for every pixel magic squares produce (not once for a dithering matrix) fewer diagonal stripes Eduard Gröller, Thomas Theußl 15 /54 Eduard Gröller, Thomas Theußl 16 /54 Grey Level Dithering Example Dot Diffusion Dithering 4 grey values are available: 0, 3, 6, 9 0 2 ordering of the threshold values generates larger dot areas dither matrix: 3 1 example: sample image threshold values result 1 7 6 5 0.4 7.9 3.4 4.9 3 6 6 6 11 16 12 8 4 5 15 17 13 3 0 1 1 6 5 4 2.6 3.4 5.6 4.1 0 6 3 3 10 14 9 7 2 6 1 5 4 3 0.4 4.9 3.4 1.9 3 6 6 3 1 4 2 1 2.6 4.1 2.6 1.1 0 3 0 0 simulates traditional printing techniques for high resolution devices Eduard Gröller, Thomas Theuß(l values between 0 and 9) 17 /54 Eduard Gröller, Thomas Theußl 18 /54 Stochastic Dithering? Forced Random Matrix Dithering improved "random" matrices very good results use of random numbers as threshold values method: insert threshold values one by one into matrix, always use the position farthest away from all ■ expectation value of total error = 0 ¡ previous points ¤ ¦ ■ no regular artificial patterns possible £ r p ¥ − ¢ repulsive force field: f(r) = e s unfortunately: very bad results! (due to bad distribution of random numbers) precalculate large threshold matrices: 300x300 very good results! Eduard Gröller, Thomas Theußl 19 /54 Eduard Gröller, Thomas Theußl 20 /54 Error Diffusion Dithering Simplest Error Diffusion Dithering (Floyd-Steinberg) pixel line ... correct value k1 k2 k3 k4 k5 ... the rounding error of every pixel rounded value r1 r2 r3 r4 r5 ... is propagated to neighbor pixels and compensated there r1 := round (k1) error1 := r1 – k1 r2 := round (k2 – error1) error := r – (k – error ) variations: which neighbor pixels 2 2 2 1 ... are affected? ri := round (ki – errori–1) errori := ri – (ki – errori–1) Eduard Gröller, Thomas Theußl 21 /54 Eduard Gröller, Thomas Theußl 22 /54 Error Diffusion Dithering Example Diffusion Direction Variations to gain better results, the error is distributed to example: the values 0, 3, 6, 9 are available several neighbors (with weights) sum of all weights = 1 k: 1 1 1 2 3 4 7 1 5 ... 1 x x 7 r: 0 3 0 3 3 3 6 3 3 ... often used 16 3 5 1 f: -1 1 0 1 1 0 -1 1 -1 ... 1 x 7 5 weighting x 48 3 5 7 5 3 distributions: 1 ri := round (ki - fi-1) fi := ri - ki + fi-1 x x 1 1 3 5 3 1 2 1 1 x 8 4 x 42 2 4 8 4 2 1 2 4 2 1 Eduard Gröller, Thomas Theußl 23 /54 Eduard Gröller, Thomas Theußl 24 /54 Error Diffusion Dithering Example Serpentine Method example: the values 0, 3, 6, 9 artificial stripes can be reduced drastically by are available 1 x x 1 processing the scanlines in serpentine order error distribution: 2 1 sample image error result instead of "normal" now in "serpentines" 1 7 6 5 -1 1.5 .75 1.37 0 9 6 6 1 6 5 4 1.5 1.5 -.87 -.75 3 6 3 3 1 5 4 3 -.25 -1.37 .87 .06 0 3 6 3 ... ... 1 4 2 1 -1.12 .75 -1.12 1.47 0 6 0 3 no additional memory necessary (values between 0 and 9) Eduard Gröller, Thomas Theußl 25 /54 Eduard Gröller, Thomas Theußl 26 /54 Raster Conversion Raster Conversion of Lines lines should appear straight converting primitives (lines etc.) into pixels lines should appear uniformly bright very frequent operations, therefore necessary: ■ efficiency lightness should be ■ possibility to implement in hardware independent of direction endpoints should be "exact" Eduard Gröller, Thomas Theußl 27 /54 Eduard Gröller, Thomas Theußl 28 /54 ➞ Symm. DDA for Line (x1,y1) (x2,y2) Simple DDA ∆ ∆ i i i-1 symmetric DDA produces lines of variable ( x, y) := ( (x2,y2) – (x1,y1) ) / 2 2 < length<2 breadth, but if ∆x and ∆y are chosen such that draw max(|∆x|, |∆y|) = 1 (rd(x1) / rd(y1)) only 1 pixel per unit in the longer direction ∆ ∆ (x1,y1) := (x1+ x,y1+ y) ∆ YES problem: requires a real division x2 < x1+ x draw NO (rd(x2) / rd(y2)) NO rd(x –∆x)° rd(x ) solution: Bresenham Algorithm 1 ∆ 1 ORrd(y1– y)° rd(y1) image equal to simple DDA, but no division YES STOP Eduard Gröller, Thomas Theußl 29 /54 Eduard Gröller, Thomas Theußl 30 /54 Comparision Symmetric - Simple DDA Raster Conversion of Lines (Bresenham's Line Algorithm) T D } dy H y -y < y -y S P S D S T D yD-yS > yT-yD T symmetric DDA simple DDA only for lines with angle 45º other lines by mirroring/rotating with 90º/180º... Eduard Gröller, Thomas Theußl 31 /54 Eduard Gröller, Thomas Theußl 32 /54 Some Bresenham Mathematics Bresenham's Line Algorithm (1) ⇔ ys:=y1; yD – yS < yT – yD yD – yH < 0 y – y > y – y ⇔ y – y > 0 d:=-0.5; {d=yD–yH} D S T D D H dy:=(y2-y1)/(x2-x1); FOR xs:=x1 TO x2 DO if y – y < 0 ¡ y , y do not change, BEGIN SetPixel(xs,ys); D H S T d:=d+dy; if y – y > 0 ¡ y := y +1, y := y +1 D H S S T T IF d>0 in every case y := y + dy THEN BEGIN ys:=ys+1; D D d:=d-1 {because yH:=yH+1} and (xS/yS) are drawn END END let d = yD – yH decision variable Eduard Gröller, Thomas Theußl 33 /54 Eduard Gröller, Thomas Theußl 34 /54 Bresenham's Line Algorithm (2) Raster Conversion of Circles (Bresenham's Circle Algorithm) ys:=y1; e:=-(x2-x1)div 2; {d*(x2-x1)} utilize threefold symmetry! de:=(y2-y1); {dy*(x2-x1)} FOR xs:=x1 TO x2 DO BEGIN SetPixel(xs,ys); e:=e+de; only one eighth has IF e>0 to be calculated THEN BEGIN ys:=ys+1; e:=e-(x2-x1) END END only integers! only addition, subtraction, shift! Eduard Gröller, Thomas Theußl 35 /54 Eduard Gröller, Thomas Theußl 36 /54 Bresenham's Circle Algorithm Bresenham's Circle Algorithm ⇔ P T yD–yS > yT–yD H inside the circle D 2 2 2 H that is if xH +yH < r 2 2 2 or if f(xH,yH) = xH +yH – r < 0 S ¢ ¢ ¢ f(xH,yH) < 0 ¢ T ¢ ¢ ¢ f(x ,y ) > 0 ¢ S criterion: H H ¡ yD-yS > yT-yD T yD-yS < yT-yD S d = "decision variable" 1 i.e.