-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.go
129 lines (105 loc) · 2.77 KB
/
image.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
128
129
package main
import (
"image"
"image/color"
"image/draw"
"image/gif"
"image/png"
"os"
"path/filepath"
"github.com/anthonynsimon/bild/adjust"
)
func processPNG(inputFs FileSystem, inputPath string, outputPath string, imageData ImageData) error {
file, err := inputFs.Open(inputPath)
if err != nil {
return err
}
defer file.Close()
img, err := png.Decode(file)
if err != nil {
return err
}
img = adjust.Hue(img, imageData.Hue)
img = adjust.Saturation(img, imageData.Saturation)
outputFile, err := os.Create(outputPath)
if err != nil {
return err
}
defer outputFile.Close()
if err := saveToStorage(outputPath, imageData); err != nil {
return err
}
return png.Encode(outputFile, img)
}
func processGIF(inputFs FileSystem, inputPath string, outputPath string, imageData ImageData) error {
file, err := inputFs.Open(inputPath)
if err != nil {
return err
}
defer file.Close()
gifImage, err := gif.DecodeAll(file)
if err != nil {
return err
}
var bounds image.Rectangle
// Process each frame
for i, img := range gifImage.Image {
// Get bounds from the first frame
if i == 0 {
bounds = img.Rect
}
// Convert *image.Paletted to *image.RGBA
rgba := PalettedToRGBA(img, bounds)
// Apply adjustments
rgba = adjust.Hue(rgba, imageData.Hue)
rgba = adjust.Saturation(rgba, imageData.Saturation)
// Convert *image.RGBA to *image.Paletted
gifImage.Image[i] = RGBAToPaletted(rgba, img.Bounds())
}
outputFile, err := os.Create(outputPath)
if err != nil {
return err
}
defer outputFile.Close()
if err := saveToStorage(outputPath, imageData); err != nil {
return err
}
return gif.EncodeAll(outputFile, gifImage)
}
func saveToStorage(outputPath string, imageData ImageData) error {
spriteType, err := DirPathToSpriteType(filepath.Dir(outputPath))
if err != nil {
return err
}
i := ImageData{
fileName: filepath.Base(outputPath),
spriteType: *spriteType,
Hue: imageData.Hue,
Saturation: imageData.Saturation,
}
if err := storage.UpsertOne(i); err != nil {
return err
}
return nil
}
func PalettedToRGBA(paletted *image.Paletted, bounds image.Rectangle) *image.RGBA {
rgba := image.NewRGBA(bounds)
draw.Draw(rgba, bounds, paletted, image.Point{}, draw.Over)
return rgba
}
func RGBAToPaletted(rgba *image.RGBA, bounds image.Rectangle) *image.Paletted {
// Extract the unique colors from the RGBA image
palette := color.Palette{}
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
c := rgba.At(x, y)
if !sliceIncludes(palette, c) {
palette = append(palette, c)
}
}
}
// Create the Paletted image with the constructed palette
paletted := image.NewPaletted(bounds, palette)
draw.Draw(paletted, rgba.Bounds(), rgba, image.Point{}, draw.Over)
return paletted
}