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,6 +11,7 @@ import (
"sync"
)
// job passed to goroutines. blend color from img1 and img2 at position (x, y)
type blendColorJob struct {
X int
Y int
@ -18,6 +19,7 @@ type blendColorJob struct {
Img2 image.Image
}
// new color after blend, to apply at position (x, y)
type blendColorResult struct {
X int
Y int
@ -98,7 +100,8 @@ func (p *blendWorkerPool) BlendImages(img1 image.Image, img2 image.Image) {
// encode the image
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()
fmt.Print("Encoding the image...")
jpeg.Encode(out, imgData, nil)
@ -117,19 +120,33 @@ func grayscale(pixel color.Color) color.Color {
}
}
// alpha blending RGBA pixels
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
func darken(fc uint8, bc uint8, fa uint8) float64 {
return math.Min(float64(fc), float64(bc))
}
// fucky fucky fun
// return float64((fc * fa) + (bc * (fa * 2)))
func lighten(fc uint8, bc uint8, fa uint8) float64 {
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

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
import (
"flag"
_ "image/jpeg"
_ "image/png"
)
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
imgs := getRandomImages(2)
img1 := loadImage(imgs[0])
img2 := loadImage(imgs[1])
// 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)
}