pixelweaver/dimensions.go

51 lines
1.6 KiB
Go
Raw Permalink Normal View History

2022-05-04 09:31:21 +02:00
package main
import (
"image"
"math"
)
// creates a new rectangle with the min height and width from both images
func getMaxDimensions(img1 image.Image, img2 image.Image) image.Rectangle {
// get dimensions for both images
size1 := img1.Bounds().Size()
size2 := img2.Bounds().Size()
// final image sized from lowest width and height
width := int(math.Min(float64(size1.X), float64(size2.X)))
height := int(math.Min(float64(size1.Y), float64(size2.Y)))
// the dimensions, as Points, of the output image
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
return image.Rectangle{upLeft, lowRight}
}
// resizes image using dimensions from config register (nearest neighbour interpolation)
func resize(img image.Image) (resized *image.RGBA) {
imgSize := img.Bounds().Size()
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
xscale := float64(imgSize.X) / float64(dimensions.Max.X)
yscale := float64(imgSize.Y) / float64(dimensions.Max.Y)
// creates new rescaled image based on a given image dimensions
resized = image.NewRGBA(dimensions)
// get pixels from the original image
for x := 0; x < dimensions.Max.X; x++ {
for y := 0; y < dimensions.Max.Y; y++ {
xp := int(math.Floor(float64(x) * xscale))
yp := int(math.Floor(float64(y) * yscale))
pixel := img.At(xp, yp)
resized.Set(x, y, pixel)
}
}
return
}
// returns a Rectangle of dimensions <width>x<height>
func dimensionsToRectangle(width int, height int) image.Rectangle {
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
return image.Rectangle{upLeft, lowRight}
}