Debian Codegraph @

resize.go

raw symbols SCIP
1 package gift
2
3 import (
4 "image"
5 "image/draw"
6 "math"
7 )
8
9 // Resampling is an interpolation algorithm used for image resizing.
10 type Resampling interface {
11 Support() float32
12 Kernel(float32) float32
13 }
14
15 func bcspline(x, b, c float32) float32 {
16 if x < 0 {
17 x = -x
18 }
19 if x < 1 {
20 return ((12-9*b-6*c)*x*x*x + (-18+12*b+6*c)*x*x + (6 - 2*b)) / 6
21 }
22 if x < 2 {
23 return ((-b-6*c)*x*x*x + (6*b+30*c)*x*x + (-12*b-48*c)*x + (8*b + 24*c)) / 6
24 }
25 return 0
26 }
27
28 func sinc(x float32) float32 {
29 if x == 0 {
30 return 1
31 }
32 return float32(math.Sin(math.Pi*float64(x)) / (math.Pi * float64(x)))
33 }
34
35 type resamp struct {
36 name string
37 support float32
38 kernel func(float32) float32
39 }
40
41 func (r resamp) String() string {
42 return r.name
43 }
44
45 func (r resamp) Support() float32 {
46 return r.support
47 }
48
49 func (r resamp) Kernel(x float32) float32 {
50 return r.kernel(x)
51 }
52
53 // NearestNeighborResampling is a nearest neighbor resampling filter.
54 var NearestNeighborResampling Resampling
55
56 // BoxResampling is a box resampling filter (average of surrounding pixels).
57 var BoxResampling Resampling
58
59 // LinearResampling is a bilinear resampling filter.
60 var LinearResampling Resampling
61
62 // CubicResampling is a bicubic resampling filter (Catmull-Rom).
63 var CubicResampling Resampling
64
65 // LanczosResampling is a Lanczos resampling filter (3 lobes).
66 var LanczosResampling Resampling
67
68 type resampWeight struct {
69 index int
70 weight float32
71 }
72
73 func prepareResampWeights(dstSize, srcSize int, resampling Resampling) [][]resampWeight {
74 delta := float32(srcSize) / float32(dstSize)
75 scale := delta
76 if scale < 1 {
77 scale = 1
78 }
79 radius := float32(math.Ceil(float64(scale * resampling.Support())))
80
81 result := make([][]resampWeight, dstSize)
82 tmp := make([]resampWeight, 0, dstSize*int(radius+2)*2)
83
84 for i := range dstSize {
85 center := (float32(i)+0.5)*delta - 0.5
86
87 left := max(int(math.Ceil(float64(center-radius))), 0)
88 right := min(int(math.Floor(float64(center+radius))), srcSize-1)
89
90 var sum float32
91 for j := left; j <= right; j++ {
92 weight := resampling.Kernel((float32(j) - center) / scale)
93 if weight == 0 {
94 continue
95 }
96 tmp = append(tmp, resampWeight{
97 index: j,
98 weight: weight,
99 })
100 sum += weight
101 }
102
103 for j := range tmp {
104 tmp[j].weight /= sum
105 }
106
107 result[i] = tmp
108 tmp = tmp[len(tmp):]
109 }
110
111 return result
112 }
113
114 func resizeLine(dst []pixel, src []pixel, weights [][]resampWeight) {
115 for i := range dst {
116 var r, g, b, a float32
117 for _, w := range weights[i] {
118 c := src[w.index]
119 wa := c.a * w.weight
120 r += c.r * wa
121 g += c.g * wa
122 b += c.b * wa
123 a += wa
124 }
125 if a != 0 {
126 r /= a
127 g /= a
128 b /= a
129 }
130 dst[i] = pixel{r, g, b, a}
131 }
132 }
133
134 func resizeHorizontal(dst draw.Image, src image.Image, w int, resampling Resampling, options *Options) {
135 srcb := src.Bounds()
136 dstb := dst.Bounds()
137
138 weights := prepareResampWeights(w, srcb.Dx(), resampling)
139
140 pixGetter := newPixelGetter(src)
141 pixSetter := newPixelSetter(dst)
142
143 parallelize(options.Workers, srcb.Min.Y, srcb.Max.Y, func(start, stop int) {
144 buf := getPixelBuf(srcb.Dx(), w)
145 defer putPixelBuf(buf)
146
147 for srcy := start; srcy < stop; srcy++ {
148 pixGetter.getPixelRow(srcy, &buf.src)
149 resizeLine(buf.dst, buf.src, weights)
150 pixSetter.setPixelRow(dstb.Min.Y+srcy-srcb.Min.Y, buf.dst)
151 }
152 })
153 }
154
155 func resizeVertical(dst draw.Image, src image.Image, h int, resampling Resampling, options *Options) {
156 srcb := src.Bounds()
157 dstb := dst.Bounds()
158
159 weights := prepareResampWeights(h, srcb.Dy(), resampling)
160
161 pixGetter := newPixelGetter(src)
162 pixSetter := newPixelSetter(dst)
163
164 parallelize(options.Workers, srcb.Min.X, srcb.Max.X, func(start, stop int) {
165 buf := getPixelBuf(srcb.Dy(), h)
166 defer putPixelBuf(buf)
167
168 for srcx := start; srcx < stop; srcx++ {
169 pixGetter.getPixelColumn(srcx, &buf.src)
170 resizeLine(buf.dst, buf.src, weights)
171 pixSetter.setPixelColumn(dstb.Min.X+srcx-srcb.Min.X, buf.dst)
172 }
173 })
174 }
175
176 func resizeNearest(dst draw.Image, src image.Image, w, h int, options *Options) {
177 srcb := src.Bounds()
178 dstb := dst.Bounds()
179 dx := float64(srcb.Dx()) / float64(w)
180 dy := float64(srcb.Dy()) / float64(h)
181
182 pixGetter := newPixelGetter(src)
183 pixSetter := newPixelSetter(dst)
184
185 parallelize(options.Workers, dstb.Min.Y, dstb.Min.Y+h, func(start, stop int) {
186 for dsty := start; dsty < stop; dsty++ {
187 for dstx := dstb.Min.X; dstx < dstb.Min.X+w; dstx++ {
188 fx := math.Floor((float64(dstx-dstb.Min.X) + 0.5) * dx)
189 fy := math.Floor((float64(dsty-dstb.Min.Y) + 0.5) * dy)
190 srcx := srcb.Min.X + int(fx)
191 srcy := srcb.Min.Y + int(fy)
192 px := pixGetter.getPixel(srcx, srcy)
193 pixSetter.setPixel(dstx, dsty, px)
194 }
195 }
196 })
197 }
198
199 type resizeFilter struct {
200 width int
201 height int
202 resampling Resampling
203 }
204
205 func (p *resizeFilter) Bounds(srcBounds image.Rectangle) (dstBounds image.Rectangle) {
206 w, h := p.width, p.height
207 srcw, srch := srcBounds.Dx(), srcBounds.Dy()
208
209 if (w == 0 && h == 0) || w < 0 || h < 0 || srcw <= 0 || srch <= 0 {
210 dstBounds = image.Rect(0, 0, 0, 0)
211 } else if w == 0 {
212 fw := float64(h) * float64(srcw) / float64(srch)
213 dstw := int(math.Max(1, math.Floor(fw+0.5)))
214 dstBounds = image.Rect(0, 0, dstw, h)
215 } else if h == 0 {
216 fh := float64(w) * float64(srch) / float64(srcw)
217 dsth := int(math.Max(1, math.Floor(fh+0.5)))
218 dstBounds = image.Rect(0, 0, w, dsth)
219 } else {
220 dstBounds = image.Rect(0, 0, w, h)
221 }
222
223 return
224 }
225
226 func (p *resizeFilter) Draw(dst draw.Image, src image.Image, options *Options) {
227 if options == nil {
228 options = &defaultOptions
229 }
230
231 b := p.Bounds(src.Bounds())
232 w, h := b.Dx(), b.Dy()
233
234 if w <= 0 || h <= 0 {
235 return
236 }
237
238 if src.Bounds().Dx() == w && src.Bounds().Dy() == h {
239 copyimage(dst, src, options)
240 return
241 }
242
243 if p.resampling.Support() <= 0 {
244 resizeNearest(dst, src, w, h, options)
245 return
246 }
247
248 if src.Bounds().Dx() == w {
249 resizeVertical(dst, src, h, p.resampling, options)
250 return
251 }
252
253 if src.Bounds().Dy() == h {
254 resizeHorizontal(dst, src, w, p.resampling, options)
255 return
256 }
257
258 tmp := createTempImage(image.Rect(0, 0, w, src.Bounds().Dy()))
259 resizeHorizontal(tmp, src, w, p.resampling, options)
260 resizeVertical(dst, tmp, h, p.resampling, options)
261 }
262
263 // Resize creates a filter that resizes an image to the specified width and height using the specified resampling.
264 // If one of width or height is 0, the image aspect ratio is preserved.
265 // Supported resampling parameters: NearestNeighborResampling, BoxResampling, LinearResampling, CubicResampling, LanczosResampling.
266 //
267 // Example:
268 //
269 // // Resize the src image to width=300 preserving the aspect ratio.
270 // g := gift.New(
271 // gift.Resize(300, 0, gift.LanczosResampling),
272 // )
273 // dst := image.NewRGBA(g.Bounds(src.Bounds()))
274 // g.Draw(dst, src)
275 func Resize(width, height int, resampling Resampling) Filter {
276 return &resizeFilter{
277 width: width,
278 height: height,
279 resampling: resampling,
280 }
281 }
282
283 type resizeToFitFilter struct {
284 width int
285 height int
286 resampling Resampling
287 }
288
289 func (p *resizeToFitFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
290 w, h := p.width, p.height
291 srcw, srch := srcBounds.Dx(), srcBounds.Dy()
292
293 if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 {
294 return image.Rect(0, 0, 0, 0)
295 }
296
297 if srcw <= w && srch <= h {
298 return image.Rect(0, 0, srcw, srch)
299 }
300
301 wratio := float64(srcw) / float64(w)
302 hratio := float64(srch) / float64(h)
303
304 var dstw, dsth int
305 if wratio > hratio {
306 dstw = w
307 dsth = minint(int(float64(srch)/wratio+0.5), h)
308 } else {
309 dsth = h
310 dstw = minint(int(float64(srcw)/hratio+0.5), w)
311 }
312
313 return image.Rect(0, 0, dstw, dsth)
314 }
315
316 func (p *resizeToFitFilter) Draw(dst draw.Image, src image.Image, options *Options) {
317 b := p.Bounds(src.Bounds())
318 Resize(b.Dx(), b.Dy(), p.resampling).Draw(dst, src, options)
319 }
320
321 // ResizeToFit creates a filter that resizes an image to fit within the specified dimensions while preserving the aspect ratio.
322 // Supported resampling parameters: NearestNeighborResampling, BoxResampling, LinearResampling, CubicResampling, LanczosResampling.
323 func ResizeToFit(width, height int, resampling Resampling) Filter {
324 return &resizeToFitFilter{
325 width: width,
326 height: height,
327 resampling: resampling,
328 }
329 }
330
331 type resizeToFillFilter struct {
332 width int
333 height int
334 anchor Anchor
335 resampling Resampling
336 }
337
338 func (p *resizeToFillFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
339 w, h := p.width, p.height
340 srcw, srch := srcBounds.Dx(), srcBounds.Dy()
341
342 if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 {
343 return image.Rect(0, 0, 0, 0)
344 }
345
346 return image.Rect(0, 0, w, h)
347 }
348
349 func (p *resizeToFillFilter) Draw(dst draw.Image, src image.Image, options *Options) {
350 b := p.Bounds(src.Bounds())
351 w, h := b.Dx(), b.Dy()
352
353 if w <= 0 || h <= 0 {
354 return
355 }
356
357 srcw, srch := src.Bounds().Dx(), src.Bounds().Dy()
358
359 wratio := float64(srcw) / float64(w)
360 hratio := float64(srch) / float64(h)
361
362 var tmpw, tmph int
363 if wratio < hratio {
364 tmpw = w
365 tmph = maxint(int(float64(srch)/wratio+0.5), h)
366 } else {
367 tmph = h
368 tmpw = maxint(int(float64(srcw)/hratio+0.5), w)
369 }
370
371 tmp := createTempImage(image.Rect(0, 0, tmpw, tmph))
372 Resize(tmpw, tmph, p.resampling).Draw(tmp, src, options)
373 CropToSize(w, h, p.anchor).Draw(dst, tmp, options)
374 }
375
376 // ResizeToFill creates a filter that resizes an image to the smallest possible size that will cover the specified dimensions,
377 // then crops the resized image to the specified dimensions using the specified anchor point.
378 // Supported resampling parameters: NearestNeighborResampling, BoxResampling, LinearResampling, CubicResampling, LanczosResampling.
379 func ResizeToFill(width, height int, resampling Resampling, anchor Anchor) Filter {
380 return &resizeToFillFilter{
381 width: width,
382 height: height,
383 anchor: anchor,
384 resampling: resampling,
385 }
386 }
387
388 func init() {
389 // Nearest neighbor resampling filter.
390 NearestNeighborResampling = resamp{
391 name: "NearestNeighborResampling",
392 support: 0,
393 kernel: func(x float32) float32 {
394 return 0
395 },
396 }
397
398 // Box resampling filter.
399 BoxResampling = resamp{
400 name: "BoxResampling",
401 support: 0.5,
402 kernel: func(x float32) float32 {
403 if x < 0 {
404 x = -x
405 }
406 if x <= 0.5 {
407 return 1
408 }
409 return 0
410 },
411 }
412
413 // Linear resampling filter.
414 LinearResampling = resamp{
415 name: "LinearResampling",
416 support: 1,
417 kernel: func(x float32) float32 {
418 if x < 0 {
419 x = -x
420 }
421 if x < 1 {
422 return 1 - x
423 }
424 return 0
425 },
426 }
427
428 // Cubic resampling filter (Catmull-Rom).
429 CubicResampling = resamp{
430 name: "CubicResampling",
431 support: 2,
432 kernel: func(x float32) float32 {
433 if x < 0 {
434 x = -x
435 }
436 if x < 2 {
437 return bcspline(x, 0, 0.5)
438 }
439 return 0
440 },
441 }
442
443 // Lanczos resampling filter (3 lobes).
444 LanczosResampling = resamp{
445 name: "LanczosResampling",
446 support: 3,
447 kernel: func(x float32) float32 {
448 if x < 0 {
449 x = -x
450 }
451 if x < 3 {
452 return sinc(x) * sinc(x/3)
453 }
454 return 0
455 },
456 }
457 }