summaryrefslogtreecommitdiff
path: root/autoimages.go
blob: 54a7a0ffc6225b45fb33fe2d9af836a4dee1f8f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright (C) 2019 Leo Tenenbaum

This file is part of AutoArt.

AutoArt is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

AutoArt is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with AutoArt.  If not, see <https://www.gnu.org/licenses/>.
*/

package main

import (
	"bufio"
	"fmt"
	"github.com/pommicket/autoart/autoart"
	"github.com/pommicket/autoart/autoutils"
	"image"
	"image/png"
	"math/rand"
	"os"
	"time"
)

// AutoImages client

func genImage(width int, height int, paletted bool, conf *autoart.Config, pconf *autoart.PaletteConfig, filename string) error {
	var img image.Image
	if paletted {
		img = autoart.GenerateImagePalette(width, height, *pconf)
	} else {
		img = autoart.GenerateImage(width, height, *conf)
	}
	file, err := os.Create(filename)
	if err != nil {
		return err
	}
	err = png.Encode(file, img)
	if err != nil {
		file.Close()
		return err
	}
	return file.Close()
}

func batchedImages(seed int64, width int, height int, paletted bool, conf *autoart.Config, pconf *autoart.PaletteConfig, number int64) error {
	// Create a directory for the images
	rand.Seed(seed)
	dir := fmt.Sprintf("autoimages%v", seed)
	err := os.MkdirAll(dir, 0700)
	if err != nil {
		return err
	}
	err = autoutils.RunInBatches(number, "Generating images...", func(n int64, errs chan<- error) {
		filename := fmt.Sprintf("%v/%09d.png", dir, n)
		errs <- genImage(width, height, paletted, conf, pconf, filename)
	})

	if err != nil {
		return err
	}
	fmt.Println("Done! Your images are in this directory:", dir)
	return nil
}

func autoImages(reader *bufio.Reader) error {
	prompt := `How many options do you want?
1. None - Just make an image
2. Some - Basic options
3. All  - Advanced options
Please enter 1, 2, or 3 (default: 1): `
	option, err := readInt64(reader, prompt, func(i int64) bool {
		return i >= 1 && i <= 3
	}, 1)
	if err != nil {
		return err
	}
	var conf autoart.Config
	var pconf autoart.PaletteConfig
	t := time.Now().UTC().UnixNano()
	if option == 1 {
		rand.Seed(t)
		fmt.Println("Generating image...")
		filename := fmt.Sprintf("autoimages%d.png", t)
		err = genImage(1920, 1080, false, &conf, &pconf, filename)
		if err != nil {
			// We're done!
			fmt.Println("Generated an image:", filename)
		}
		return err
	}
	// Basic options
	positive := func(i int64) bool { return i > 0 }
	width, err := readInt64(reader, "Width (default: 1920)? ", positive, 1920)
	if err != nil {
		return err
	}
	height, err := readInt64(reader, "Height (default: 1080)? ", positive, 1080)
	if err != nil {
		return err
	}
	number, err := readInt64(reader, "How many (default: 1)? ", positive, 1)
	if err != nil {
		return err
	}
	if option == 2 {
		return batchedImages(t, int(width), int(height), false, &conf, &pconf, number)
	}

	paletted, err := readBool(reader, "Should a palette be used (y/n, default: n)? ", false)
	if err != nil {
		return err
	}

	// Advanced options
	if paletted {
		err = readPaletteConf(reader, &pconf)
		if err != nil {
			return err
		}
	} else {
		err = readConf(reader, &conf)
		if err != nil {
			return err
		}
	}
	seed, err := readInt64(reader, "Random seed (default: current time)? ", func(i int64) bool {
		return true
	}, t)

	return batchedImages(seed, int(width), int(height), paletted, &conf, &pconf, number)
}