/
mosaic_aspect.go
127 lines (110 loc) · 4.02 KB
/
mosaic_aspect.go
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
package cmd
import (
"strconv"
"strings"
"github.com/atongen/gosaic/controller"
"github.com/spf13/cobra"
)
var (
mosaicAspectName string
mosaicAspectFillType string
mosaicAspectCoverWidth int
mosaicAspectCoverHeight int
mosaicAspectPartialAspect string
mosaicAspectSize int
mosaicAspectMaxRepeats int
mosaicAspectThreashold float64
mosaicAspectOutfile string
mosaicAspectCoverOutfile string
mosaicAspectMacroOutfile string
mosaicAspectCleanup bool
mosaicAspectDestructive bool
)
func init() {
addLocalStrFlag(&mosaicAspectName, "name", "n", "", "Name of mosaic", MosaicAspectCmd)
addLocalStrFlag(&mosaicAspectFillType, "fill-type", "f", "random", "Mosaic fill to use, either 'random' or 'best'", MosaicAspectCmd)
addLocalIntFlag(&mosaicAspectCoverWidth, "width", "w", 0, "Pixel width of mosaic, 0 maintains aspect from image height", MosaicAspectCmd)
addLocalIntFlag(&mosaicAspectCoverHeight, "height", "", 0, "Pixel height of mosaic, 0 maintains aspect from width", MosaicAspectCmd)
addLocalStrFlag(&mosaicAspectPartialAspect, "aspect", "a", "", "Aspect of mosaic partials (CxR)", MosaicAspectCmd)
addLocalIntFlag(&mosaicAspectSize, "size", "s", 0, "Number of mosaic partials in smallest dimension, 0 auto-calculates", MosaicAspectCmd)
addLocalIntFlag(&mosaicAspectMaxRepeats, "max-repeats", "", -1, "Number of times an index image can be repeated, 0 is unlimited, -1 is the minimun number", MosaicAspectCmd)
addLocalFloatFlag(&mosaicAspectThreashold, "threashold", "t", -1.0, "How similar aspect ratios must be", MosaicAspectCmd)
addLocalStrFlag(&mosaicAspectOutfile, "out", "", "", "File to write final mosaic image", MosaicAspectCmd)
addLocalStrFlag(&mosaicAspectCoverOutfile, "cover-out", "", "", "File to write cover partial pattern image", MosaicAspectCmd)
addLocalStrFlag(&mosaicAspectMacroOutfile, "macro-out", "", "", "File to write resized macro image", MosaicAspectCmd)
addLocalBoolFlag(&mosaicAspectCleanup, "cleanup", "", false, "Delete mosaic metadata after completion", MosaicAspectCmd)
addLocalBoolFlag(&mosaicAspectDestructive, "destructive", "d", false, "Delete mosaic metadata during creation", MosaicAspectCmd)
MosaicCmd.AddCommand(MosaicAspectCmd)
}
var MosaicAspectCmd = &cobra.Command{
Use: "aspect PATH",
Short: "Create an aspect mosaic from image at PATH",
Long: "Create an aspect mosaic from image at PATH",
Run: func(c *cobra.Command, args []string) {
if len(args) != 1 {
Env.Fatalln("Mosaic path is required")
}
if args[0] == "" {
Env.Fatalln("Mosaic path is required")
}
if mosaicAspectCoverWidth < 0 {
Env.Fatalln("width must be greater than zero")
}
if mosaicAspectCoverHeight < 0 {
Env.Fatalln("height must be greater than zero")
}
var (
aw, ah int
err error
)
if mosaicAspectPartialAspect == "" {
aw = 0
ah = 0
} else {
aspectStrings := strings.Split(mosaicAspectPartialAspect, "x")
if len(aspectStrings) != 2 {
Env.Fatalln("aspect format must be CxR")
}
aw, err = strconv.Atoi(aspectStrings[0])
if err != nil {
Env.Fatalf("Error converting aspect columns: %s\n", err.Error())
}
if aw < 0 {
Env.Fatalln("aspect columns must be greater than zero")
}
ah, err = strconv.Atoi(aspectStrings[1])
if err != nil {
Env.Fatalf("Error converting aspect rows: %s\n", err.Error())
}
if ah < 0 {
Env.Fatalln("aspect rows must be greater than zero")
}
}
if mosaicAspectFillType != "best" && mosaicAspectFillType != "random" {
Env.Fatalln("Invalid fill-type")
}
err = Env.Init()
if err != nil {
Env.Fatalf("Unable to initialize environment: %s\n", err.Error())
}
defer Env.Close()
controller.MosaicAspect(
Env,
args[0],
mosaicAspectName,
mosaicAspectFillType,
mosaicAspectCoverWidth,
mosaicAspectCoverHeight,
aw,
ah,
mosaicAspectSize,
mosaicAspectMaxRepeats,
mosaicAspectThreashold,
mosaicAspectCoverOutfile,
mosaicAspectMacroOutfile,
mosaicAspectOutfile,
mosaicAspectCleanup,
mosaicAspectDestructive,
)
},
}