| 1 |
package gift |
| 2 |
|
| 3 |
import ( |
| 4 |
"image" |
| 5 |
"image/draw" |
| 6 |
"math" |
| 7 |
"sync" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
func parallelize(procs int, start, stop int, fn func(start, stop int)) { |
| 12 |
var wg sync.WaitGroup |
| 13 |
splitRange(start, stop, procs, func(pstart, pstop int) { |
| 14 |
wg.Add(1) |
| 15 |
go func() { |
| 16 |
defer wg.Done() |
| 17 |
fn(pstart, pstop) |
| 18 |
}() |
| 19 |
}) |
| 20 |
wg.Wait() |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
func splitRange(start, stop, n int, fn func(pstart, pstop int)) { |
| 25 |
count := stop - start |
| 26 |
if count < 1 { |
| 27 |
return |
| 28 |
} |
| 29 |
|
| 30 |
if n < 1 { |
| 31 |
n = 1 |
| 32 |
} |
| 33 |
if n > count { |
| 34 |
n = count |
| 35 |
} |
| 36 |
|
| 37 |
div := count / n |
| 38 |
mod := count % n |
| 39 |
|
| 40 |
for i := 0; i < n; i++ { |
| 41 |
fn( |
| 42 |
start+i*div+minint(i, mod), |
| 43 |
start+(i+1)*div+minint(i+1, mod), |
| 44 |
) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func absf32(x float32) float32 { |
| 49 |
if x < 0 { |
| 50 |
return -x |
| 51 |
} |
| 52 |
return x |
| 53 |
} |
| 54 |
|
| 55 |
func minf32(x, y float32) float32 { |
| 56 |
if x < y { |
| 57 |
return x |
| 58 |
} |
| 59 |
return y |
| 60 |
} |
| 61 |
|
| 62 |
func maxf32(x, y float32) float32 { |
| 63 |
if x > y { |
| 64 |
return x |
| 65 |
} |
| 66 |
return y |
| 67 |
} |
| 68 |
|
| 69 |
func powf32(x, y float32) float32 { |
| 70 |
return float32(math.Pow(float64(x), float64(y))) |
| 71 |
} |
| 72 |
|
| 73 |
func logf32(x float32) float32 { |
| 74 |
return float32(math.Log(float64(x))) |
| 75 |
} |
| 76 |
|
| 77 |
func expf32(x float32) float32 { |
| 78 |
return float32(math.Exp(float64(x))) |
| 79 |
} |
| 80 |
|
| 81 |
func sincosf32(a float32) (float32, float32) { |
| 82 |
sin, cos := math.Sincos(math.Pi * float64(a) / 180) |
| 83 |
return float32(sin), float32(cos) |
| 84 |
} |
| 85 |
|
| 86 |
func floorf32(x float32) float32 { |
| 87 |
return float32(math.Floor(float64(x))) |
| 88 |
} |
| 89 |
|
| 90 |
func sqrtf32(x float32) float32 { |
| 91 |
return float32(math.Sqrt(float64(x))) |
| 92 |
} |
| 93 |
|
| 94 |
func minint(x, y int) int { |
| 95 |
if x < y { |
| 96 |
return x |
| 97 |
} |
| 98 |
return y |
| 99 |
} |
| 100 |
|
| 101 |
func maxint(x, y int) int { |
| 102 |
if x > y { |
| 103 |
return x |
| 104 |
} |
| 105 |
return y |
| 106 |
} |
| 107 |
|
| 108 |
func sort(data []float32) { |
| 109 |
n := len(data) |
| 110 |
|
| 111 |
if n < 2 { |
| 112 |
return |
| 113 |
} |
| 114 |
|
| 115 |
if n <= 20 { |
| 116 |
for i := 1; i < n; i++ { |
| 117 |
x := data[i] |
| 118 |
j := i - 1 |
| 119 |
for ; j >= 0 && data[j] > x; j-- { |
| 120 |
data[j+1] = data[j] |
| 121 |
} |
| 122 |
data[j+1] = x |
| 123 |
} |
| 124 |
return |
| 125 |
} |
| 126 |
|
| 127 |
i := 0 |
| 128 |
j := n - 1 |
| 129 |
x := data[n/2] |
| 130 |
for i <= j { |
| 131 |
for data[i] < x { |
| 132 |
i++ |
| 133 |
} |
| 134 |
for data[j] > x { |
| 135 |
j-- |
| 136 |
} |
| 137 |
if i <= j { |
| 138 |
data[i], data[j] = data[j], data[i] |
| 139 |
i++ |
| 140 |
j-- |
| 141 |
} |
| 142 |
} |
| 143 |
if j > 0 { |
| 144 |
sort(data[:j+1]) |
| 145 |
} |
| 146 |
if i < n-1 { |
| 147 |
sort(data[i:]) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
func createTempImage(r image.Rectangle) draw.Image { |
| 153 |
return image.NewNRGBA64(r) |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
func isOpaque(img image.Image) bool { |
| 158 |
type opaquer interface { |
| 159 |
Opaque() bool |
| 160 |
} |
| 161 |
if o, ok := img.(opaquer); ok { |
| 162 |
return o.Opaque() |
| 163 |
} |
| 164 |
return false |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
func genDisk(ksize int) []float32 { |
| 169 |
if ksize%2 == 0 { |
| 170 |
ksize-- |
| 171 |
} |
| 172 |
if ksize < 1 { |
| 173 |
return []float32{} |
| 174 |
} |
| 175 |
disk := make([]float32, ksize*ksize) |
| 176 |
kcenter := ksize / 2 |
| 177 |
for i := 0; i < ksize; i++ { |
| 178 |
for j := 0; j < ksize; j++ { |
| 179 |
x := kcenter - i |
| 180 |
y := kcenter - j |
| 181 |
r := math.Sqrt(float64(x*x + y*y)) |
| 182 |
if r <= float64(ksize/2) { |
| 183 |
disk[j*ksize+i] = 1 |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
return disk |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
func copyimage(dst draw.Image, src image.Image, options *Options) { |
| 192 |
if options == nil { |
| 193 |
options = &defaultOptions |
| 194 |
} |
| 195 |
|
| 196 |
srcb := src.Bounds() |
| 197 |
dstb := dst.Bounds() |
| 198 |
pixGetter := newPixelGetter(src) |
| 199 |
pixSetter := newPixelSetter(dst) |
| 200 |
|
| 201 |
parallelize(options.Workers, srcb.Min.Y, srcb.Max.Y, func(start, stop int) { |
| 202 |
for srcy := start; srcy < stop; srcy++ { |
| 203 |
for srcx := srcb.Min.X; srcx < srcb.Max.X; srcx++ { |
| 204 |
dstx := dstb.Min.X + srcx - srcb.Min.X |
| 205 |
dsty := dstb.Min.Y + srcy - srcb.Min.Y |
| 206 |
pixSetter.setPixel(dstx, dsty, pixGetter.getPixel(srcx, srcy)) |
| 207 |
} |
| 208 |
} |
| 209 |
}) |
| 210 |
} |
| 211 |
|
| 212 |
type copyimageFilter struct{} |
| 213 |
|
| 214 |
func (p *copyimageFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) { |
| 215 |
dstBounds = image.Rect(0, 0, srcBounds.Dx(), srcBounds.Dy()) |
| 216 |
return |
| 217 |
} |
| 218 |
|
| 219 |
func (p *copyimageFilter) Draw(dst draw.Image, src image.Image, options *Options) { |
| 220 |
copyimage(dst, src, options) |
| 221 |
} |