-
Notifications
You must be signed in to change notification settings - Fork 0
/
zoom.go
68 lines (53 loc) · 2.5 KB
/
zoom.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
package main
import (
"image"
"image/color"
"image/draw"
"github.com/bamiaux/rez"
)
func ResizeRGBA(img *image.RGBA, maxWidth, maxHeight int) *image.RGBA {
// Calculate the scaling factor for width and height
scaleWidth := float64(maxWidth) / float64(img.Bounds().Dx())
scaleHeight := float64(maxHeight) / float64(img.Bounds().Dy())
// Use the smaller scaling factor to ensure the entire image fits within the specified dimensions
scale := scaleWidth
if scaleHeight < scaleWidth {
scale = scaleHeight
}
// Resize the image using the calculated scaling factor
resizedImg := image.NewRGBA(image.Rect(0, 0, int(float64(img.Bounds().Dx())*scale), int(float64(img.Bounds().Dy())*scale)))
err := rez.Convert(resizedImg, img, rez.NewLanczosFilter(2))
if err != nil {
panic("Failed to resize img: "+err.Error())
}
// Create a new RGBA image with the resized dimensions
resizedRGBA := image.NewRGBA(image.Rect(0, 0, int(resizedImg.Bounds().Dx()), int(resizedImg.Bounds().Dy())))
// Fill with black
draw.Draw(resizedRGBA, resizedRGBA.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
// Copy the resized image onto the new RGBA image, with alpha blending
draw.Draw(resizedRGBA, resizedRGBA.Bounds(), resizedImg, resizedImg.Bounds().Min, draw.Over)
return resizedRGBA
}
// ZoomOutRGBA zooms out the given image.RGBA by a percentage while keeping the same aspect ratio.
// It fills the background with black.
func ZoomOutRGBA(img *image.RGBA, percentage float64) *image.RGBA {
// Calculate the dimensions after zooming out
newWidth := int(float64(img.Bounds().Dx()) * (1 - percentage/100))
newHeight := int(float64(img.Bounds().Dy()) * (1 - percentage/100))
// Resize the image using the calculated dimensions
resizedImg := image.NewRGBA(image.Rect(0, 0, newWidth, newHeight))
err := rez.Convert(resizedImg, img, rez.NewLanczosFilter(2))
if err != nil {
panic("Failed to resize img: "+err.Error())
}
// Create a new RGBA image with the original dimensions
resizedRGBA := image.NewRGBA(image.Rect(0, 0, img.Bounds().Dx(), img.Bounds().Dy()))
// Calculate the position to draw the resized image (centered)
x := (img.Bounds().Dx() - newWidth) / 2
y := (img.Bounds().Dy() - newHeight) / 2
// Fill the background with black
draw.Draw(resizedRGBA, resizedRGBA.Bounds(), &image.Uniform{color.Black}, image.ZP, draw.Src)
// Draw the resized image onto the new RGBA image
draw.Draw(resizedRGBA, image.Rect(x, y, x+newWidth, y+newHeight), resizedImg, resizedImg.Bounds().Min, draw.Over)
return resizedRGBA
}