diff --git a/blend.go b/blend.go index d5594e3..517badf 100644 --- a/blend.go +++ b/blend.go @@ -11,13 +11,15 @@ import ( "sync" ) +// job passed to goroutines. blend color from img1 and img2 at position (x, y) type blendColorJob struct { - X int - Y int - Img1 image.Image - Img2 image.Image + X int + Y int + Img1 image.Image + 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 diff --git a/config.go b/config.go new file mode 100644 index 0000000..a4703fe --- /dev/null +++ b/config.go @@ -0,0 +1,8 @@ +package main + +type Config struct { + Method string + OutputDir string +} + +var ConfigRegister Config diff --git a/main.go b/main.go index 4fe122d..a1830a7 100644 --- a/main.go +++ b/main.go @@ -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) }