Debian Codegraph @

gift.go

raw symbols SCIP
1 /*
2 Package gift provides a set of useful image processing filters.
3
4 Basic usage:
5
6 // 1. Create a new filter list and add some filters.
7 g := gift.New(
8 gift.Resize(800, 0, gift.LanczosResampling),
9 gift.UnsharpMask(1, 1, 0),
10 )
11
12 // 2. Create a new image of the corresponding size.
13 // dst is a new target image, src is the original image.
14 dst := image.NewRGBA(g.Bounds(src.Bounds()))
15
16 // 3. Use the Draw func to apply the filters to src and store the result in dst.
17 g.Draw(dst, src)
18 */
19 package gift
20
21 import (
22 "image"
23 "image/draw"
24 "runtime"
25 )
26
27 // Filter is an image processing filter.
28 type Filter interface {
29 // Draw applies the filter to the src image and outputs the result to the dst image.
30 Draw(dst draw.Image, src image.Image, options *Options)
31 // Bounds calculates the appropriate bounds of an image after applying the filter.
32 Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle)
33 }
34
35 // Options is the parameters passed to image processing filters.
36 type Options struct {
37 Workers int
38 }
39
40 func (o *Options) init() {
41 if o.Workers < 1 {
42 // Values from benchmarking on a MacBook M1 with 10 logical CPUs.
43 o.Workers = min(6, runtime.NumCPU())
44 }
45 }
46
47 var defaultOptions = Options{}
48
49 func init() {
50 defaultOptions.init()
51 }
52
53 // GIFT is a list of image processing filters.
54 type GIFT struct {
55 filters []Filter
56 options Options
57 }
58
59 // New creates a new filter list and initializes it with the given slice of filters.
60 func New(filters ...Filter) *GIFT {
61 return &GIFT{
62 filters: filters,
63 options: defaultOptions,
64 }
65 }
66
67 // NewWithOptions creates a new filter list with the given options and initializes it with the given slice of filters.
68 func NewWithOptions(options Options, filters ...Filter) *GIFT {
69 options.init()
70 return &GIFT{
71 filters: filters,
72 options: options,
73 }
74 }
75
76 // Bounds calculates the appropriate bounds for the result image after applying all the added filters.
77 // Parameter srcBounds is the bounds of the source image.
78 //
79 // Example:
80 //
81 // src := image.NewRGBA(image.Rect(0, 0, 100, 200))
82 // g := gift.New(gift.Rotate90())
83 //
84 // // calculate image bounds after applying rotation and create a new image of that size.
85 // dst := image.NewRGBA(g.Bounds(src.Bounds())) // dst bounds: (0, 0, 200, 100)
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 // Draw applies all the added filters to the src image and outputs the result to the dst image.
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 // Operator is an image composition operator.
125 type Operator int
126
127 // Composition operators.
128 const (
129 CopyOperator Operator = iota
130 OverOperator
131 )
132
133 // DrawAt applies all the added filters to the src image and outputs the result to the dst image
134 // at the specified position pt using the specified composition operator op.
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 }