| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package gift |
| 20 |
|
| 21 |
import ( |
| 22 |
"image" |
| 23 |
"image/draw" |
| 24 |
"runtime" |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
type Filter interface { |
| 29 |
|
| 30 |
Draw(dst draw.Image, src image.Image, options *Options) |
| 31 |
|
| 32 |
Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
type Options struct { |
| 37 |
Workers int |
| 38 |
} |
| 39 |
|
| 40 |
func (o *Options) init() { |
| 41 |
if o.Workers < 1 { |
| 42 |
|
| 43 |
o.Workers = min(6, runtime.NumCPU()) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
var defaultOptions = Options{} |
| 48 |
|
| 49 |
func init() { |
| 50 |
defaultOptions.init() |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
type GIFT struct { |
| 55 |
filters []Filter |
| 56 |
options Options |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
func New(filters ...Filter) *GIFT { |
| 61 |
return &GIFT{ |
| 62 |
filters: filters, |
| 63 |
options: defaultOptions, |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
func NewWithOptions(options Options, filters ...Filter) *GIFT { |
| 69 |
options.init() |
| 70 |
return &GIFT{ |
| 71 |
filters: filters, |
| 72 |
options: options, |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
func (g *GIFT) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) { |
| 87 |
b := srcBounds |
| 88 |
for _, f := range g.filters { |
| 89 |
b = f.Bounds(b) |
| 90 |
} |
| 91 |
dstBounds = b |
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func (g *GIFT) Draw(dst draw.Image, src image.Image) error { |
| 97 |
if len(g.filters) == 0 { |
| 98 |
copyimage(dst, src, &g.options) |
| 99 |
return nil |
| 100 |
} |
| 101 |
|
| 102 |
first, last := 0, len(g.filters)-1 |
| 103 |
var tmpIn image.Image |
| 104 |
var tmpOut draw.Image |
| 105 |
|
| 106 |
for i, f := range g.filters { |
| 107 |
if i == first { |
| 108 |
tmpIn = src |
| 109 |
} else { |
| 110 |
tmpIn = tmpOut |
| 111 |
} |
| 112 |
|
| 113 |
if i == last { |
| 114 |
tmpOut = dst |
| 115 |
} else { |
| 116 |
tmpOut = createTempImage(f.Bounds(tmpIn.Bounds())) |
| 117 |
} |
| 118 |
|
| 119 |
f.Draw(tmpOut, tmpIn, &g.options) |
| 120 |
} |
| 121 |
return nil |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
type Operator int |
| 126 |
|
| 127 |
|
| 128 |
const ( |
| 129 |
CopyOperator Operator = iota |
| 130 |
OverOperator |
| 131 |
) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
func (g *GIFT) DrawAt(dst draw.Image, src image.Image, pt image.Point, op Operator) error { |
| 136 |
switch op { |
| 137 |
case OverOperator: |
| 138 |
tb := g.Bounds(src.Bounds()) |
| 139 |
tb = tb.Sub(tb.Min).Add(pt) |
| 140 |
tmp := createTempImage(tb) |
| 141 |
if err := g.Draw(tmp, src); err != nil { |
| 142 |
return err |
| 143 |
} |
| 144 |
pixGetterDst := newPixelGetter(dst) |
| 145 |
pixGetterTmp := newPixelGetter(tmp) |
| 146 |
pixSetterDst := newPixelSetter(dst) |
| 147 |
ib := tb.Intersect(dst.Bounds()) |
| 148 |
parallelize(g.options.Workers, ib.Min.Y, ib.Max.Y, func(start, stop int) { |
| 149 |
for y := start; y < stop; y++ { |
| 150 |
for x := ib.Min.X; x < ib.Max.X; x++ { |
| 151 |
px0 := pixGetterDst.getPixel(x, y) |
| 152 |
px1 := pixGetterTmp.getPixel(x, y) |
| 153 |
c1 := px1.a |
| 154 |
c0 := (1 - c1) * px0.a |
| 155 |
cs := c0 + c1 |
| 156 |
c0 /= cs |
| 157 |
c1 /= cs |
| 158 |
r := px0.r*c0 + px1.r*c1 |
| 159 |
g := px0.g*c0 + px1.g*c1 |
| 160 |
b := px0.b*c0 + px1.b*c1 |
| 161 |
a := px0.a + px1.a*(1-px0.a) |
| 162 |
pixSetterDst.setPixel(x, y, pixel{r, g, b, a}) |
| 163 |
} |
| 164 |
} |
| 165 |
}) |
| 166 |
|
| 167 |
default: |
| 168 |
if pt.Eq(dst.Bounds().Min) { |
| 169 |
return g.Draw(dst, src) |
| 170 |
} |
| 171 |
if subimg, ok := getSubImage(dst, pt); ok { |
| 172 |
return g.Draw(subimg, src) |
| 173 |
} |
| 174 |
tb := g.Bounds(src.Bounds()) |
| 175 |
tb = tb.Sub(tb.Min).Add(pt) |
| 176 |
tmp := createTempImage(tb) |
| 177 |
if err := g.Draw(tmp, src); err != nil { |
| 178 |
return err |
| 179 |
} |
| 180 |
pixGetter := newPixelGetter(tmp) |
| 181 |
pixSetter := newPixelSetter(dst) |
| 182 |
ib := tb.Intersect(dst.Bounds()) |
| 183 |
parallelize(g.options.Workers, ib.Min.Y, ib.Max.Y, func(start, stop int) { |
| 184 |
for y := start; y < stop; y++ { |
| 185 |
for x := ib.Min.X; x < ib.Max.X; x++ { |
| 186 |
pixSetter.setPixel(x, y, pixGetter.getPixel(x, y)) |
| 187 |
} |
| 188 |
} |
| 189 |
}) |
| 190 |
} |
| 191 |
return nil |
| 192 |
} |
| 193 |
|
| 194 |
func getSubImage(img draw.Image, pt image.Point) (draw.Image, bool) { |
| 195 |
if !pt.In(img.Bounds()) { |
| 196 |
return nil, false |
| 197 |
} |
| 198 |
switch img := img.(type) { |
| 199 |
case *image.Gray: |
| 200 |
return img.SubImage(image.Rectangle{pt, img.Bounds().Max}).(draw.Image), true |
| 201 |
case *image.Gray16: |
| 202 |
return img.SubImage(image.Rectangle{pt, img.Bounds().Max}).(draw.Image), true |
| 203 |
case *image.RGBA: |
| 204 |
return img.SubImage(image.Rectangle{pt, img.Bounds().Max}).(draw.Image), true |
| 205 |
case *image.RGBA64: |
| 206 |
return img.SubImage(image.Rectangle{pt, img.Bounds().Max}).(draw.Image), true |
| 207 |
case *image.NRGBA: |
| 208 |
return img.SubImage(image.Rectangle{pt, img.Bounds().Max}).(draw.Image), true |
| 209 |
case *image.NRGBA64: |
| 210 |
return img.SubImage(image.Rectangle{pt, img.Bounds().Max}).(draw.Image), true |
| 211 |
default: |
| 212 |
return nil, false |
| 213 |
} |
| 214 |
} |