variable number of images to blend (shitcode)
This commit is contained in:
parent
bd7c32b526
commit
13923ff12f
37
blend.go
37
blend.go
@ -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...")
|
log.Println("Blending the images...")
|
||||||
|
|
||||||
// dimensions := getMaxDimensions(img1, img2)
|
p.InitWorkerPool()
|
||||||
|
|
||||||
// output image, ready to receive pixel values
|
// output image, ready to receive pixel values
|
||||||
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
|
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
|
||||||
outImg := image.NewRGBA(dimensions)
|
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 ?
|
// TODO: use a worker pool for those operations ?
|
||||||
// resize image
|
// resize image
|
||||||
img1Resized := resize(img1)
|
img1Resized := resize(img1)
|
||||||
img2Resized := resize(img2)
|
img2Resized := resize(img2)
|
||||||
|
|
||||||
p.RunWorkers(outImg)
|
p.RunWorkers(out)
|
||||||
|
|
||||||
p.SendBlendJobs(dimensions, img1Resized, img2Resized)
|
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
|
// second waitgroup to ensure the encoding doesn't start before the goroutines are done
|
||||||
p.ConsumerWG.Wait()
|
p.ConsumerWG.Wait()
|
||||||
encodeImage(outImg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// encode the image
|
// encode the image
|
||||||
|
@ -17,6 +17,7 @@ type Config struct {
|
|||||||
OutputWidth int `json:output_width`
|
OutputWidth int `json:output_width`
|
||||||
OutputHeight int `json:output_height`
|
OutputHeight int `json:output_height`
|
||||||
Grayscale bool
|
Grayscale bool
|
||||||
|
Amount int
|
||||||
}
|
}
|
||||||
|
|
||||||
// loads configuration values from json file
|
// loads configuration values from json file
|
||||||
|
17
main.go
17
main.go
@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"image"
|
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
)
|
)
|
||||||
@ -13,27 +12,17 @@ func main() {
|
|||||||
flag.StringVar(&ConfigRegister.OutputDir, "output", "./", "Output directory")
|
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.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.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.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()
|
flag.Parse()
|
||||||
|
|
||||||
// set output's width and height
|
// set output's width and height
|
||||||
ConfigRegister.SetOutputDimensions()
|
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
|
// pool of workers unionizing, ready to blend a new picture using the power of friendship
|
||||||
var pool blendWorkerPool
|
var pool blendWorkerPool
|
||||||
pool.InitWorkerPool()
|
|
||||||
|
|
||||||
// main blending routine
|
// main blending routine
|
||||||
pool.BlendImages(img1, img2)
|
pool.BlendImagesMain()
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,21 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"image"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMain(t *testing.T) {
|
func TestMain(t *testing.T) {
|
||||||
img1 := loadImage("assets/moutons.jpg")
|
img1 := loadImage("assets/moutons.jpg")
|
||||||
img2 := loadImage("assets/lavande.jpg")
|
img2 := loadImage("assets/lavande.jpg")
|
||||||
|
dimensions := dimensionsToRectangle(800, 600)
|
||||||
|
outImg := image.NewRGBA(dimensions)
|
||||||
|
|
||||||
var pool blendWorkerPool
|
var pool blendWorkerPool
|
||||||
|
|
||||||
pool.InitWorkerPool()
|
pool.InitWorkerPool()
|
||||||
|
|
||||||
pool.BlendImages(img1, img2)
|
pool.BlendImages(img1, img2, outImg)
|
||||||
|
|
||||||
|
encodeImage(outImg)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user