pixelweaver/config.go

100 lines
2.8 KiB
Go

package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"strconv"
"strings"
"time"
)
type Config struct {
Method string `json:method`
OutputDir string `json:outputdir`
InputDir string `json:inputdir`
BaseImage string `json:baseimage`
Dimensions string `json:dimensions`
OutputWidth int `json:output_width`
OutputHeight int `json:output_height`
Grayscale bool
Sepia bool
Amount int
Quiet bool
Seed int64
}
// loads configuration values from json file
func (c *Config) LoadConfigFromFile(filePath string) {
file, _ := os.Open("conf.json")
defer file.Close()
decoder := json.NewDecoder(file)
configuration := Config{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(configuration.Method)
fmt.Println(configuration.OutputDir)
}
// Set output file dimensions using string symbol (<width>x<height>)
func (c *Config) SetOutputDimensions() {
split := strings.Split(c.Dimensions, "x")
width, _ := strconv.Atoi(split[0])
height, _ := strconv.Atoi(split[1])
c.OutputWidth = width
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", "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. <width>x<height>")
flag.BoolVar(&ConfigRegister.Grayscale, "grayscale", false, "Output image in shade of gray (around 50)")
flag.BoolVar(&ConfigRegister.Sepia, "sepia", false, "Output image with sepia tone")
flag.IntVar(&ConfigRegister.Amount, "amount", 2, "Number of images to blend together")
flag.Int64Var(&ConfigRegister.Seed, "seed", seed, "Number of images to blend together")
flag.BoolVar(&ConfigRegister.Quiet, "quiet", false, "No progress messages")
flag.Parse()
ConfigRegister.CheckPaths()
// set output's width and height
ConfigRegister.SetOutputDimensions()
}
// global register to acccess configuration values
var ConfigRegister Config