command line argument and a config register

This commit is contained in:
Ali Gator 2022-04-26 13:51:49 +02:00
parent 2360f3062b
commit 09b64a7fad
3 changed files with 49 additions and 16 deletions

View File

@ -11,13 +11,15 @@ import (
"sync" "sync"
) )
// job passed to goroutines. blend color from img1 and img2 at position (x, y)
type blendColorJob struct { type blendColorJob struct {
X int X int
Y int Y int
Img1 image.Image Img1 image.Image
Img2 image.Image Img2 image.Image
} }
// new color after blend, to apply at position (x, y)
type blendColorResult struct { type blendColorResult struct {
X int X int
Y int Y int
@ -98,7 +100,8 @@ func (p *blendWorkerPool) BlendImages(img1 image.Image, img2 image.Image) {
// encode the image // encode the image
func encodeImage(imgData *image.RGBA) { func encodeImage(imgData *image.RGBA) {
out, _ := os.Create("output.jpg") outputFile := fmt.Sprintf("%s/%s", ConfigRegister.OutputDir, "output.jpg")
out, _ := os.Create(outputFile)
defer out.Close() defer out.Close()
fmt.Print("Encoding the image...") fmt.Print("Encoding the image...")
jpeg.Encode(out, imgData, nil) jpeg.Encode(out, imgData, nil)
@ -117,19 +120,33 @@ func grayscale(pixel color.Color) color.Color {
} }
} }
// alpha blending RGBA pixels func darken(fc uint8, bc uint8, fa uint8) float64 {
func blend(fc uint8, bc uint8, fa uint8, ba uint8) float64 {
// fc : foreground color
// bc : background color
// fa : foreground alpha
// ba : background alpha
// all values are alpha-premultiplied
// darken only
return math.Min(float64(fc), float64(bc)) return math.Min(float64(fc), float64(bc))
}
// fucky fucky fun func lighten(fc uint8, bc uint8, fa uint8) float64 {
// return float64((fc * fa) + (bc * (fa * 2))) return math.Max(float64(fc), float64(bc))
}
// produce absolute garbage
func fuckyfun(fc uint8, bc uint8, fa uint8) float64 {
return float64((fc * fa) + (bc * (fa * 2)))
}
// get the blending method from the config register
func blend(fc uint8, bc uint8, fa uint8, ba uint8) float64 {
var newValue float64
switch ConfigRegister.Method {
case "darken":
newValue = darken(fc, bc, fa)
case "ligten":
newValue = lighten(fc, bc, fa)
case "fuckyfun":
newValue = fuckyfun(fc, bc, fa)
default:
newValue = darken(fc, bc, fa)
}
return newValue
} }
// blend two RGBA colors/pixels and returns a new one // blend two RGBA colors/pixels and returns a new one

8
config.go Normal file
View File

@ -0,0 +1,8 @@
package main
type Config struct {
Method string
OutputDir string
}
var ConfigRegister Config

View File

@ -1,18 +1,26 @@
package main package main
import ( import (
"flag"
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
) )
func main() { func main() {
// command line arguments
flag.StringVar(&ConfigRegister.Method, "blending", "darken", "Blending methods : darken, lighten, fuckyfun")
flag.StringVar(&ConfigRegister.OutputDir, "output", "./", "Output directory")
flag.Parse()
// get two random images and load them // get two random images and load them
imgs := getRandomImages(2) imgs := getRandomImages(2)
img1 := loadImage(imgs[0]) img1 := loadImage(imgs[0])
img2 := loadImage(imgs[1]) img2 := loadImage(imgs[1])
// pool of workers unionizing, ready to blend a new picture using the power of friendship
var pool blendWorkerPool var pool blendWorkerPool
pool.InitWorkerPool() pool.InitWorkerPool()
// main blending routine
pool.BlendImages(img1, img2) pool.BlendImages(img1, img2)
} }