resize picture based on specified dimensions
This commit is contained in:
40
blend.go
40
blend.go
@ -85,12 +85,19 @@ func (p *blendWorkerPool) SetImageWorker(newImage *image.RGBA) {
|
||||
func (p *blendWorkerPool) BlendImages(img1 image.Image, img2 image.Image) {
|
||||
log.Println("Blending the images...")
|
||||
|
||||
dimensions := getDimensions(img1, img2)
|
||||
// dimensions := getMaxDimensions(img1, img2)
|
||||
// output image, ready to receive pixel values
|
||||
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
|
||||
outImg := image.NewRGBA(dimensions)
|
||||
|
||||
// TODO: use a worker pool for those operations ?
|
||||
// resize image
|
||||
img1Resized := resize(img1)
|
||||
img2Resized := resize(img2)
|
||||
|
||||
p.RunWorkers(outImg)
|
||||
|
||||
p.SendBlendJobs(dimensions, img1, img2)
|
||||
p.SendBlendJobs(dimensions, img1Resized, img2Resized)
|
||||
|
||||
// first waitgroup to wait for the results to be ready before closing the channel
|
||||
p.WorkerWG.Wait()
|
||||
@ -166,7 +173,7 @@ func blendColor(color1 color.Color, color2 color.Color) color.Color {
|
||||
}
|
||||
|
||||
// creates a new rectangle with the min height and width from both images
|
||||
func getDimensions(img1 image.Image, img2 image.Image) image.Rectangle {
|
||||
func getMaxDimensions(img1 image.Image, img2 image.Image) image.Rectangle {
|
||||
// get dimensions for both images
|
||||
size1 := img1.Bounds().Size()
|
||||
size2 := img2.Bounds().Size()
|
||||
@ -181,3 +188,30 @@ func getDimensions(img1 image.Image, img2 image.Image) image.Rectangle {
|
||||
|
||||
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}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user