raster_floating.go 5.4 KB

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