diff --git a/.gitignore b/.gitignore index 0a70cd3..631b0e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.jpg -*.png \ No newline at end of file +*.png +output/ \ No newline at end of file diff --git a/blend.go b/blend.go index 2598be4..e85201f 100644 --- a/blend.go +++ b/blend.go @@ -8,6 +8,7 @@ import ( "log" "math" "os" + "path" ) // convert RGBA pixel to grayscale. BT.709 luminosity formula @@ -115,7 +116,7 @@ func blendColor(color1 color.Color, color2 color.Color) color.Color { // encode the image func encodeImage(imgData *image.RGBA) { - outputFile := fmt.Sprintf("%s/%s", ConfigRegister.OutputDir, "output.jpg") + outputFile := fmt.Sprintf("%s/%s", path.Clean(ConfigRegister.OutputDir), "output.jpg") out, _ := os.Create(outputFile) defer out.Close() log.Println("Encoding the new image...") diff --git a/config.go b/config.go index b99f2f7..4e54e04 100644 --- a/config.go +++ b/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "flag" "fmt" + "log" "os" "strconv" "strings" @@ -48,13 +49,35 @@ func (c *Config) SetOutputDimensions() { c.OutputHeight = height } +func (c *Config) CheckPaths() { + _, err := os.Stat(c.OutputDir) + + if err != nil { + log.Fatal("Output dir does not exist :", c.OutputDir) + } + + _, err = os.Stat(c.InputDir) + + if err != nil { + log.Fatal("Input dir does not exist :", c.InputDir) + } + + if (c.BaseImage != "") { + _, err = os.Stat(c.BaseImage) + + if err != nil { + log.Fatal("Base image does not exist :", c.BaseImage) + } + } +} + func initConfigRegister() { // default seed for the RNG seed := time.Now().UnixNano() // command line arguments flag.StringVar(&ConfigRegister.Method, "blending", "darken", "Blending methods : darken, lighten, average, fuckyfun") - flag.StringVar(&ConfigRegister.OutputDir, "output", "./", "Output directory") + flag.StringVar(&ConfigRegister.OutputDir, "output", "./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", "1280x1024", "Out image dimensions. x") @@ -66,6 +89,8 @@ func initConfigRegister() { flag.Parse() + ConfigRegister.CheckPaths() + // set output's width and height ConfigRegister.SetOutputDimensions() }