variable number of images to blend (shitcode)

This commit is contained in:
Ali Gator 2022-05-01 20:52:03 +02:00
parent bd7c32b526
commit 13923ff12f
4 changed files with 43 additions and 19 deletions

View File

@ -82,20 +82,50 @@ func (p *blendWorkerPool) SetImageWorker(newImage *image.RGBA) {
}
}
func (p *blendWorkerPool) BlendImages(img1 image.Image, img2 image.Image) {
func (p *blendWorkerPool) BlendImagesMain() {
log.Println("Blending the images...")
// dimensions := getMaxDimensions(img1, img2)
p.InitWorkerPool()
// output image, ready to receive pixel values
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
outImg := image.NewRGBA(dimensions)
for i := 1; i < ConfigRegister.Amount; i++ {
// get two random images and load them
var img1 image.Image
if ConfigRegister.BaseImage != "" && i == 1 {
img1 = loadImage(ConfigRegister.BaseImage)
} else {
if i != 1 {
img1 = loadImage("output.jpg")
} else {
img1 = loadImage(getRandomImage())
}
}
img2 := loadImage(getRandomImage())
// pool of workers unionizing, ready to blend a new picture using the power of friendship
var pool blendWorkerPool
pool.InitWorkerPool()
// main blending routine
pool.BlendImages(img1, img2, outImg)
encodeImage(outImg)
}
}
func (p *blendWorkerPool) BlendImages(img1 image.Image, img2 image.Image, out *image.RGBA) {
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
// TODO: use a worker pool for those operations ?
// resize image
img1Resized := resize(img1)
img2Resized := resize(img2)
p.RunWorkers(outImg)
p.RunWorkers(out)
p.SendBlendJobs(dimensions, img1Resized, img2Resized)
@ -105,7 +135,6 @@ func (p *blendWorkerPool) BlendImages(img1 image.Image, img2 image.Image) {
// second waitgroup to ensure the encoding doesn't start before the goroutines are done
p.ConsumerWG.Wait()
encodeImage(outImg)
}
// encode the image

View File

@ -17,6 +17,7 @@ type Config struct {
OutputWidth int `json:output_width`
OutputHeight int `json:output_height`
Grayscale bool
Amount int
}
// loads configuration values from json file

17
main.go
View File

@ -2,7 +2,6 @@ package main
import (
"flag"
"image"
_ "image/jpeg"
_ "image/png"
)
@ -13,27 +12,17 @@ func main() {
flag.StringVar(&ConfigRegister.OutputDir, "output", "./", "Output directory")
flag.StringVar(&ConfigRegister.InputDir, "input", "/home/gator/Photos", "Input directory. Where to look the images from")
flag.StringVar(&ConfigRegister.BaseImage, "base-img", "", "Path to the base image to work with. Random image if not set")
flag.StringVar(&ConfigRegister.Dimensions, "dimensions", "800x600", "Out image dimensions. <width>x<height>")
flag.StringVar(&ConfigRegister.Dimensions, "dimensions", "1280x1024", "Out image dimensions. <width>x<height>")
flag.BoolVar(&ConfigRegister.Grayscale, "grayscale", false, "Output image in shade of gray (around 50)")
flag.IntVar(&ConfigRegister.Amount, "amount", 2, "Number of images to blend together")
flag.Parse()
// set output's width and height
ConfigRegister.SetOutputDimensions()
// get two random images and load them
var img1 image.Image
if ConfigRegister.BaseImage != "" {
img1 = loadImage(ConfigRegister.BaseImage)
} else {
img1 = loadImage(getRandomImage())
}
img2 := loadImage(getRandomImage())
// pool of workers unionizing, ready to blend a new picture using the power of friendship
var pool blendWorkerPool
pool.InitWorkerPool()
// main blending routine
pool.BlendImages(img1, img2)
pool.BlendImagesMain()
}

View File

@ -1,16 +1,21 @@
package main
import (
"image"
"testing"
)
func TestMain(t *testing.T) {
img1 := loadImage("assets/moutons.jpg")
img2 := loadImage("assets/lavande.jpg")
dimensions := dimensionsToRectangle(800, 600)
outImg := image.NewRGBA(dimensions)
var pool blendWorkerPool
pool.InitWorkerPool()
pool.BlendImages(img1, img2)
pool.BlendImages(img1, img2, outImg)
encodeImage(outImg)
}