-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcreatingsymmetry.go
113 lines (100 loc) · 3.91 KB
/
creatingsymmetry.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
package creatingsymmetry
import (
"github.com/chadius/creatingsymmetry/entities/command"
"github.com/chadius/creatingsymmetry/entities/imageoutput"
"github.com/chadius/creatingsymmetry/entities/transformer"
"image"
"image/png"
"io"
"io/ioutil"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
//counterfeiter:generate . TransformerStrategy
// TransformerStrategy shapes the expected messages and the expected responses when running the rules.
type TransformerStrategy interface {
ApplyFormulaToTransformImage(inputImageDataByteStream, formulaDataByteStream, outputSettingsDataByteStream io.Reader, output io.Writer) error
}
type FileTransformer struct{}
func (f *FileTransformer) ApplyFormulaToTransformImage(inputImageDataByteStream, formulaDataByteStream, outputSettingsDataByteStream io.Reader, output io.Writer) error {
wallpaperCommand, wallpaperErr := readWallpaperCommand(formulaDataByteStream)
if wallpaperErr != nil {
return wallpaperErr
}
sourceImage, sourceImageErr := readSourceImage(inputImageDataByteStream)
if sourceImageErr != nil {
return sourceImageErr
}
outputSettings, outputSettingsErr := readOutputSettings(outputSettingsDataByteStream)
if outputSettingsErr != nil {
return outputSettingsErr
}
outputImage := transformImage(sourceImage, wallpaperCommand, outputSettings)
png.Encode(output, outputImage)
return nil
}
func readSourceImage(input io.Reader) (image.Image, error) {
colorSourceImage, _, err := image.Decode(input)
if err != nil {
return nil, err
}
return colorSourceImage, nil
}
func readWallpaperCommand(input io.Reader) (*command.CreateSymmetryPattern, error) {
createWallpaperYAML, err := ioutil.ReadAll(input)
if err != nil {
return nil, err
}
wallpaperCommand, err := command.NewCreateWallpaperCommandFromYAML(createWallpaperYAML)
if err != nil {
return nil, err
}
return wallpaperCommand, nil
}
func readOutputSettings(input io.Reader) (*command.OutputSettings, error) {
outputSettingsYAML, err := ioutil.ReadAll(input)
if err != nil {
return nil, err
}
outputSettings := command.NewOutputSettingsBuilder().WithYAML(outputSettingsYAML).Build()
return outputSettings, nil
}
func transformImage(sourceImage image.Image, wallpaperCommand *command.CreateSymmetryPattern, outputSettings *command.OutputSettings) *image.NRGBA {
coordinateThreshold := imageoutput.CoordinateFilterBuilder().
WithMinimumX(wallpaperCommand.CoordinateThreshold.XMin).
WithMaximumX(wallpaperCommand.CoordinateThreshold.XMax).
WithMinimumY(wallpaperCommand.CoordinateThreshold.YMin).
WithMaximumY(wallpaperCommand.CoordinateThreshold.YMax).
Build()
var eyedropper *imageoutput.RectangularEyedropper
if wallpaperCommand.Eyedropper != nil {
eyedropper = imageoutput.EyedropperBuilder().
WithLeftSide(wallpaperCommand.Eyedropper.LeftSide).
WithRightSide(wallpaperCommand.Eyedropper.RightSide).
WithTopSide(wallpaperCommand.Eyedropper.TopSide).
WithBottomSide(wallpaperCommand.Eyedropper.BottomSide).
WithImage(sourceImage).
Build()
} else {
eyedropper = imageoutput.EyedropperBuilder().
WithLeftSide(sourceImage.Bounds().Min.X).
WithRightSide(sourceImage.Bounds().Max.X).
WithTopSide(sourceImage.Bounds().Min.Y).
WithBottomSide(sourceImage.Bounds().Max.Y).
WithImage(sourceImage).
Build()
}
transformerEntity := transformer.FormulaTransformer{}
outputImage := transformerEntity.Transform(&transformer.Settings{
PatternViewportXMin: wallpaperCommand.PatternViewport.XMin,
PatternViewportXMax: wallpaperCommand.PatternViewport.XMax,
PatternViewportYMin: wallpaperCommand.PatternViewport.YMin,
PatternViewportYMax: wallpaperCommand.PatternViewport.YMax,
InputImage: sourceImage,
Formula: wallpaperCommand,
CoordinateThreshold: coordinateThreshold,
Eyedropper: eyedropper,
OutputWidth: outputSettings.OutputWidth(),
OutputHeight: outputSettings.OutputHeight(),
})
return outputImage
}