-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
image_filter.go
340 lines (311 loc) · 13.4 KB
/
image_filter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright (c) 2021-2024 by Richard A. Wilkes. All rights reserved.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, version 2.0. If a copy of the MPL was not distributed with
// this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, version 2.0.
package unison
import (
"runtime"
"github.com/richardwilkes/unison/enums/colorchannel"
"github.com/richardwilkes/unison/enums/tilemode"
"github.com/richardwilkes/unison/internal/skia"
)
// ImageFilter performs a transformation on the image before drawing it.
type ImageFilter struct {
filter skia.ImageFilter
}
func newImageFilter(filter skia.ImageFilter) *ImageFilter {
if filter == nil {
return nil
}
f := &ImageFilter{filter: filter}
runtime.SetFinalizer(f, func(obj *ImageFilter) {
ReleaseOnUIThread(func() {
skia.ImageFilterUnref(obj.filter)
})
})
return f
}
func (f *ImageFilter) filterOrNil() skia.ImageFilter {
if f == nil {
return nil
}
return f.filter
}
// NewArithmeticImageFilter returns a new arithmetic image filter. Each output pixel is the result of combining the
// corresponding background and foreground pixels using the 4 coefficients:
// k1 * foreground * background + k2 * foreground + k3 * background + k4
// Both background and foreground may be nil, in which case the source bitmap is used.
// If enforcePMColor is true, the RGB channels will clamped to the calculated alpha.
// cropRect may be nil.
func NewArithmeticImageFilter(k1, k2, k3, k4 float32, background, foreground *ImageFilter, enforcePMColor bool, cropRect *Rect) *ImageFilter {
var bg, fg skia.ImageFilter
if background != nil {
bg = background.filter
}
if foreground != nil {
fg = foreground.filter
}
return newImageFilter(skia.ImageFilterNewArithmetic(k1, k2, k3, k4, enforcePMColor, bg, fg, cropRect))
}
// NewBlurImageFilter returns a new blur image filter. input may be nil, in which case the source bitmap is used.
// cropRect may be nil.
func NewBlurImageFilter(sigmaX, sigmaY float32, tileMode tilemode.Enum, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewBlur(sigmaX, sigmaY, skia.TileMode(tileMode), in, cropRect))
}
// NewColorImageFilter returns a new color image filter. input may be nil, in which case the source bitmap is used.
// cropRect may be nil.
func NewColorImageFilter(colorFilter *ColorFilter, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewColorFilter(colorFilter.filter, in, cropRect))
}
// NewComposeImageFilter returns a new combining image filter.
func NewComposeImageFilter(outer, inner *ImageFilter) *ImageFilter {
return newImageFilter(skia.ImageFilterNewCompose(outer.filter, inner.filter))
}
// NewDisplacementImageFilter returns a new displacement image filter. displayment may be nil, in which case the source
// bitmap will be used. cropRect may be nil.
func NewDisplacementImageFilter(xChannelSelector, yChannelSelector colorchannel.Enum, scale float32, displacement, color *ImageFilter, cropRect *Rect) *ImageFilter {
var dis skia.ImageFilter
if displacement != nil {
dis = displacement.filter
}
return newImageFilter(skia.ImageFilterNewDisplacementMapEffect(skia.ColorChannel(xChannelSelector),
skia.ColorChannel(yChannelSelector), scale, dis, color.filter, cropRect))
}
// NewDropShadowImageFilter returns a new drop shadow image filter. input may be nil, in which case the source bitmap
// will be used. cropRect may be nil.
func NewDropShadowImageFilter(dx, dy, sigmaX, sigmaY float32, color Color, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewDropShadow(dx, dy, sigmaX, sigmaY, skia.Color(color), in, cropRect))
}
// NewDropShadowOnlyImageFilter returns a new drop shadow only image filter. input may be nil, in which case the source
// bitmap will be used. cropRect may be nil.
func NewDropShadowOnlyImageFilter(dx, dy, sigmaX, sigmaY float32, color Color, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewDropShadowOnly(dx, dy, sigmaX, sigmaY, skia.Color(color), in, cropRect))
}
// NewImageSourceImageFilter returns a new image source image filter. If canvas is not nil, a hardware-accellerated
// image will be used if possible.
func NewImageSourceImageFilter(canvas *Canvas, img *Image, srcRect, dstRect Rect, sampling *SamplingOptions) *ImageFilter {
var image skia.Image
ref := img.ref()
if canvas == nil {
image = ref.img
} else {
image = ref.contextImg(canvas.surface)
}
return newImageFilter(skia.ImageFilterNewImageSource(image, srcRect, dstRect,
sampling.skSamplingOptions()))
}
// NewImageSourceDefaultImageFilter returns a new image source image filter that uses the default quality and the full
// image size. If canvas is not nil, a hardware-accellerated image will be used if possible.
func NewImageSourceDefaultImageFilter(canvas *Canvas, img *Image, sampling *SamplingOptions) *ImageFilter {
var image skia.Image
ref := img.ref()
if canvas == nil {
image = ref.img
} else {
image = ref.contextImg(canvas.surface)
}
return newImageFilter(skia.ImageFilterNewImageSourceDefault(image, sampling.skSamplingOptions()))
}
// NewMagnifierImageFilter returns a new magnifier image filter. input may be nil, in which case the source bitmap will
// be used. cropRect may be nil.
func NewMagnifierImageFilter(lensBounds Rect, zoomAmount, inset float32, sampling *SamplingOptions, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewMagnifier(lensBounds, zoomAmount, inset, sampling.skSamplingOptions(), in, cropRect))
}
// NewMatrixConvolutionImageFilter returns a new matrix convolution image filter.
//
// width, height: The kernel size in pixels.
// kernel: The image processing kernel. Must contain width * height elements, in row order. If less than this, zeroes
// will be added to make up the difference.
// gain: A scale factor applied to each pixel after convolution. This can be used to normalize the kernel, if it does
// not already sum to 1.
// bias: A bias factor added to each pixel after convolution.
// offsetX, offsetY: An offset applied to each pixel coordinate before convolution. This can be used to center the
// kernel over the image (e.g., a 3x3 kernel should have an offset of {1, 1}).
// tileMode: How accesses outside the image are treated.
// convolveAlpha: If true, all channels are convolved. If false, only the RGB channels are convolved, and alpha is
// copied from the source image.
// input: The input image filter, if nil the source bitmap is used instead.
// cropRect: Rectangle to which the output processing will be limited. May be nil.
func NewMatrixConvolutionImageFilter(width, height int, kernel []float32, gain, bias float32, offsetX, offsetY int, tileMode tilemode.Enum, convolveAlpha bool, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
if len(kernel) < width*height {
k := make([]float32, width*height)
copy(k, kernel)
kernel = k
}
return newImageFilter(skia.ImageFilterNewMatrixConvolution(&skia.ISize{
Width: int32(width),
Height: int32(height),
}, kernel, gain, bias, &skia.IPoint{
X: int32(offsetX),
Y: int32(offsetY),
}, skia.TileMode(tileMode), convolveAlpha, in, cropRect))
}
// NewMatrixTransformImageFilter returns a new matrix transform image filter. input may be nil, in which case the source
// bitmap will be used.
func NewMatrixTransformImageFilter(matrix Matrix, sampling *SamplingOptions, input *ImageFilter) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewMatrixTransform(matrix, sampling.skSamplingOptions(), in))
}
// NewMergeImageFilter returns a new merge image filter. Each filter will draw their results in order with src-over
// blending. A nil filter will use the source bitmap instead. cropRect may be nil.
func NewMergeImageFilter(filters []*ImageFilter, cropRect *Rect) *ImageFilter {
ff := make([]skia.ImageFilter, len(filters))
for i, one := range filters {
if one != nil {
ff[i] = one.filter
}
}
return newImageFilter(skia.ImageFilterNewMerge(ff, cropRect))
}
// NewOffsetImageFilter returns a new offset image filter. input may be nil, in which case the source bitmap will be
// used. cropRect may be nil.
func NewOffsetImageFilter(dx, dy float32, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewOffset(dx, dy, in, cropRect))
}
// NewTileImageFilter returns a new tile image filter. input may be nil, in which case the source bitmap will be used.
func NewTileImageFilter(src, dst Rect, input *ImageFilter) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewTile(src, dst, in))
}
// NewDilateImageFilter returns a new dilate image filter. input may be nil, in which case the source bitmap will be
// used. cropRect may be nil.
func NewDilateImageFilter(radiusX, radiusY int, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewDilate(radiusX, radiusY, in, cropRect))
}
// NewErodeImageFilter returns a new erode image filter. input may be nil, in which case the source bitmap will be
// used. cropRect may be nil.
func NewErodeImageFilter(radiusX, radiusY int, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewErode(radiusX, radiusY, in, cropRect))
}
// NewDistantLitDiffuseImageFilter returns a new distant lit diffuse image filter. input may be nil, in which case the
// source bitmap will be used. cropRect may be nil.
func NewDistantLitDiffuseImageFilter(x, y, z, scale, reflectivity float32, color Color, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewDistantLitDiffuse(&skia.Point3{
X: x,
Y: y,
Z: z,
}, skia.Color(color), scale, reflectivity, in, cropRect))
}
// NewPointLitDiffuseImageFilter returns a new point lit diffuse image filter. input may be nil, in which case the
// source bitmap will be used. cropRect may be nil.
func NewPointLitDiffuseImageFilter(x, y, z, scale, reflectivity float32, color Color, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewPointLitDiffuse(&skia.Point3{
X: x,
Y: y,
Z: z,
}, skia.Color(color), scale, reflectivity, in, cropRect))
}
// NewSpotLitDiffuseImageFilter returns a new spot lit diffuse image filter. input may be nil, in which case the
// source bitmap will be used. cropRect may be nil.
func NewSpotLitDiffuseImageFilter(x, y, z, targetX, targetY, targetZ, specularExponent, cutoffAngle, scale, reflectivity float32, color Color, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewSpotLitDiffuse(&skia.Point3{
X: x,
Y: y,
Z: z,
}, &skia.Point3{
X: targetX,
Y: targetY,
Z: targetZ,
}, specularExponent, cutoffAngle, scale, reflectivity, skia.Color(color), in, cropRect))
}
// NewDistantLitSpecularImageFilter returns a new distant lit specular image filter. input may be nil, in which case the
// source bitmap will be used. cropRect may be nil.
func NewDistantLitSpecularImageFilter(x, y, z, scale, reflectivity, shine float32, color Color, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewDistantLitSpecular(&skia.Point3{
X: x,
Y: y,
Z: z,
}, skia.Color(color), scale, reflectivity, shine, in, cropRect))
}
// NewPointLitSpecularImageFilter returns a new point lit specular image filter. input may be nil, in which case the
// source bitmap will be used. cropRect may be nil.
func NewPointLitSpecularImageFilter(x, y, z, scale, reflectivity, shine float32, color Color, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewPointLitSpecular(&skia.Point3{
X: x,
Y: y,
Z: z,
}, skia.Color(color), scale, reflectivity, shine, in, cropRect))
}
// NewSpotLitSpecularImageFilter returns a new spot lit specular image filter. input may be nil, in which case the
// source bitmap will be used. cropRect may be nil.
func NewSpotLitSpecularImageFilter(x, y, z, targetX, targetY, targetZ, specularExponent, cutoffAngle, scale, reflectivity, shine float32, color Color, input *ImageFilter, cropRect *Rect) *ImageFilter {
var in skia.ImageFilter
if input != nil {
in = input.filter
}
return newImageFilter(skia.ImageFilterNewSpotLitSpecular(&skia.Point3{
X: x,
Y: y,
Z: z,
}, &skia.Point3{
X: targetX,
Y: targetY,
Z: targetZ,
}, specularExponent, cutoffAngle, scale, reflectivity, shine, skia.Color(color), in, cropRect))
}