Dithering missing colors are simulated by 1st color mixing existing colors Dithering and

Special Aspects of Raster Images

nd 2 color missing color

Eduard Gröller, Thomas Theußl 2 /54

Dithering Methods Dithering in 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 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- 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. dold = d = f(xp+1, yp– /2) , (only for 2nd octant) then dnew can be calculated as follows:

Eduard Gröller, Thomas Theußl 37 /54 Eduard Gröller, Thomas Theußl 38 /54

Bresenham's Circle Algorithm Bresenham's Circle Algorithm if dold < 0 then Hnew = Hold + (1,0) , PROCEDURE Circle (r:INTEGER); ie. VAR x,y: INTEGER; d: REAL; 1 2 1 2 2 BEGIN ... {initialize x,y,d} dnew = f(xp+2, yp– /2) = (xp+2) + (yp– /2) – r REPEAT Draw_8(x,y) dnew = dold + (2xp+3) IF d < 0 THEN d:=d+2*x+3 if dold > 0 then Hnew = Hold + (1,–1) , ELSE ie. BEGIN d:=d+2*(x-y)+5; 3 2 3 2 2 y:=y-1 dnew = f(xp+2, yp– /2) = (xp+2) + (yp– /2) – r END; x:=x+1; dnew = dold + (2xp – 2yp + 5) UNTIL y < x

Eduard Gröller, Thomas Theußl 39 /54 EduaErd NGröDller;, Thomas Theußl 40 /54

Bresenham's Circle Algorithm Bresenham's Circle Algorithm PROCEDURE Circle (r:INTEGER); initialization: restriction to integer radii VAR x,y: INTEGER; d: REAL; BEGIN x:=0; y:=r; d:=5/4 - r; x = 0 and y = r REPEAT Draw_8(x,y) 1 IF d < 0

H = (1,r- /2)

£ £ £ £ 2 1 2 THEN d:=d+2*x+3 d = f(H) = 1 + (r -r+ /4) - r ELSE 5 = /4 - r BEGIN d:=d+2*(x-y)+5; y:=y-1 END; x:=x+1; UNTIL y < x

Eduard Gröller, Thomas Theußl 41 /54 EduaErd NGröDller;, Thomas Theußl 42 /54 Raster Transformations Raster Scaling: Scaling Up

how to apply geometrical transformations to old raster images? resolution

■ translation: trivial new resolution ■ scaling: resampling necessary ■ shearing: by line conversion ■ rotation: partition in three shearings center point of pixel in new resolution defines its color = "resampling"

Eduard Gröller, Thomas Theußl 43 /54 Eduard Gröller, Thomas Theußl 44 /54

Raster Scaling: Scaling Down Shearing x-shearing α old 1 resolution (x y) ⋅ 1 0 = (x+αy y) α 1 new resolution y-shearing β (x y) ⋅ 1 = (x y+βx) center point of pixel in new resolution 0 1 β defines its color = "resampling" 1

Eduard Gröller, Thomas Theußl 45 /54 Eduard Gröller, Thomas Theußl 46 /54

Raster Shearing Raster Rotation 90°-rotation, 180°-rotation, etc. trivial principle of other rotations: subdivide rotation in a series of three shears

θ θ β cos sin = 1 0 . 1 . 1 0 -sin θ cos θ α 1 0 1 α 1 can be seen as the multiple application of a line raster conversion algorithm (e.g. Bresenham) where α = - tan (θ/2)

and β = sin θ

¡ ¡ ¡ ¡ no information lost

Eduard Gröller, Thomas Theußl 47 /54 Eduard Gröller, Thomas Theußl 48 /54 Rotation = 3 Shears Example: –45o Rotation tan (θ/2) = (1- cos θ) / sin θ 1 0 1 θ 1 0 . sin . = cos θ −1 1 0 1 cos θ −1 1 (θ/2) = 0.4142 sin θ sin θ –tan sin θ = 0.7071 1 θ 1 0 sin . = cos θ −1 θ c os θ −1 1 sin θ cos sin θ θ −1 θ 1+ cos sin cos θ sin θ θ −1+ 2θ − θ = cos cos cos cos θ -sin θ cos θ sin θ Eduard Gröller, Thomas Theußl 49 /54 Eduard Gröller, Thomas Theußl 50 /54

Example: –45o Rotation Example: –45o Rotation

(θ/2) = 0.4142 –tan (θ/2) = 0.4142 –tan θ = 0.7071 sin θ = 0.7071 sin

Eduard Gröller, Thomas Theußl 51 /54 Eduard Gröller, Thomas Theußl 52 /54

Distortion of Raster Images of Raster Images y y v u=0 inter- u=0 polation interpolation v=0 v=1 inter- u u x x polation (u,v) v u=1

u=1 we are looking for the (u,v)-coordinates (u,v) are calculated from (x,y) by of the center of pixel (x,y) interpolation from the corner coordinates Eduard Gröller, Thomas Theußl 53 /54 Eduard Gröller, Thomas Theußl 54 /54