git clone https://git.lucas.co/go_mono.git
vector/raster_floating.go (5.2K)
1 // Copyright 2016 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package vector
6
7 // This file contains a floating point math implementation of the vector
8 // graphics rasterizer.
9
10 import (
11 "math"
12 )
13
14 func floatingFloor(x float32) int32 { return int32(math.Floor(float64(x))) }
15 func floatingCeil(x float32) int32 { return int32(math.Ceil(float64(x))) }
16
17 func (z *Rasterizer) floatingLineTo(bx, by float32) {
18 ax, ay := z.penX, z.penY
19 z.penX, z.penY = bx, by
20 dir := float32(1)
21 if ay > by {
22 dir, ax, ay, bx, by = -1, bx, by, ax, ay
23 }
24 // Horizontal line segments yield no change in coverage. Almost horizontal
25 // segments would yield some change, in ideal math, but the computation
26 // further below, involving 1 / (by - ay), is unstable in floating point
27 // math, so we treat the segment as if it was perfectly horizontal.
28 if by-ay <= 0.000001 {
29 return
30 }
31 dxdy := (bx - ax) / (by - ay)
32
33 x := ax
34 y := floatingFloor(ay)
35 yMax := floatingCeil(by)
36 if yMax > int32(z.size.Y) {
37 yMax = int32(z.size.Y)
38 }
39 width := int32(z.size.X)
40
41 for ; y < yMax; y++ {
42 dy := min(float32(y+1), by) - max(float32(y), ay)
43
44 // The "float32" in expressions like "float32(foo*bar)" here and below
45 // look redundant, since foo and bar already have type float32, but are
46 // explicit in order to disable the compiler's Fused Multiply Add (FMA)
47 // instruction selection, which can improve performance but can result
48 // in different rounding errors in floating point computations.
49 //
50 // This package aims to have bit-exact identical results across all
51 // GOARCHes, and across pure Go code and assembly, so it disables FMA.
52 //
53 // See the discussion at
54 // https://groups.google.com/d/topic/golang-dev/Sti0bl2xUXQ/discussion
55 xNext := x + float32(dy*dxdy)
56 if y < 0 {
57 x = xNext
58 continue
59 }
60 buf := z.bufF32[y*width:]
61 d := float32(dy * dir)
62 x0, x1 := x, xNext
63 if x > xNext {
64 x0, x1 = x1, x0
65 }
66 x0i := floatingFloor(x0)
67 x0Floor := float32(x0i)
68 x1i := floatingCeil(x1)
69 x1Ceil := float32(x1i)
70
71 if x1i <= x0i+1 {
72 xmf := float32(0.5*(x+xNext)) - x0Floor
73 if i := clamp(x0i+0, width); i < uint(len(buf)) {
74 buf[i] += d - float32(d*xmf)
75 }
76 if i := clamp(x0i+1, width); i < uint(len(buf)) {
77 buf[i] += float32(d * xmf)
78 }
79 } else {
80 s := 1 / (x1 - x0)
81 x0f := x0 - x0Floor
82 oneMinusX0f := 1 - x0f
83 a0 := float32(0.5 * s * oneMinusX0f * oneMinusX0f)
84 x1f := x1 - x1Ceil + 1
85 am := float32(0.5 * s * x1f * x1f)
86
87 if i := clamp(x0i, width); i < uint(len(buf)) {
88 buf[i] += float32(d * a0)
89 }
90
91 if x1i == x0i+2 {
92 if i := clamp(x0i+1, width); i < uint(len(buf)) {
93 buf[i] += float32(d * (1 - a0 - am))
94 }
95 } else {
96 a1 := float32(s * (1.5 - x0f))
97 if i := clamp(x0i+1, width); i < uint(len(buf)) {
98 buf[i] += float32(d * (a1 - a0))
99 }
100 dTimesS := float32(d * s)
101 for xi := x0i + 2; xi < x1i-1; xi++ {
102 if i := clamp(xi, width); i < uint(len(buf)) {
103 buf[i] += dTimesS
104 }
105 }
106 a2 := a1 + float32(s*float32(x1i-x0i-3))
107 if i := clamp(x1i-1, width); i < uint(len(buf)) {
108 buf[i] += float32(d * (1 - a2 - am))
109 }
110 }
111
112 if i := clamp(x1i, width); i < uint(len(buf)) {
113 buf[i] += float32(d * am)
114 }
115 }
116
117 x = xNext
118 }
119 }
120
121 const (
122 // almost256 scales a floating point value in the range [0, 1] to a uint8
123 // value in the range [0x00, 0xff].
124 //
125 // 255 is too small. Floating point math accumulates rounding errors, so a
126 // fully covered src value that would in ideal math be float32(1) might be
127 // float32(1-ε), and uint8(255 * (1-ε)) would be 0xfe instead of 0xff. The
128 // uint8 conversion rounds to zero, not to nearest.
129 //
130 // 256 is too big. If we multiplied by 256, below, then a fully covered src
131 // value of float32(1) would translate to uint8(256 * 1), which can be 0x00
132 // instead of the maximal value 0xff.
133 //
134 // math.Float32bits(almost256) is 0x437fffff.
135 almost256 = 255.99998
136
137 // almost65536 scales a floating point value in the range [0, 1] to a
138 // uint16 value in the range [0x0000, 0xffff].
139 //
140 // math.Float32bits(almost65536) is 0x477fffff.
141 almost65536 = almost256 * 256
142 )
143
144 func floatingAccumulateOpOver(dst []uint8, src []float32) {
145 // Sanity check that len(dst) >= len(src).
146 if len(dst) < len(src) {
147 return
148 }
149
150 acc := float32(0)
151 for i, v := range src {
152 acc += v
153 a := acc
154 if a < 0 {
155 a = -a
156 }
157 if a > 1 {
158 a = 1
159 }
160 // This algorithm comes from the standard library's image/draw package.
161 dstA := uint32(dst[i]) * 0x101
162 maskA := uint32(almost65536 * a)
163 outA := dstA*(0xffff-maskA)/0xffff + maskA
164 dst[i] = uint8(outA >> 8)
165 }
166 }
167
168 func floatingAccumulateOpSrc(dst []uint8, src []float32) {
169 // Sanity check that len(dst) >= len(src).
170 if len(dst) < len(src) {
171 return
172 }
173
174 acc := float32(0)
175 for i, v := range src {
176 acc += v
177 a := acc
178 if a < 0 {
179 a = -a
180 }
181 if a > 1 {
182 a = 1
183 }
184 dst[i] = uint8(almost256 * a)
185 }
186 }
187
188 func floatingAccumulateMask(dst []uint32, src []float32) {
189 // Sanity check that len(dst) >= len(src).
190 if len(dst) < len(src) {
191 return
192 }
193
194 acc := float32(0)
195 for i, v := range src {
196 acc += v
197 a := acc
198 if a < 0 {
199 a = -a
200 }
201 if a > 1 {
202 a = 1
203 }
204 dst[i] = uint32(almost65536 * a)
205 }
206 }