-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset_output.go
56 lines (44 loc) · 1.29 KB
/
asset_output.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
package xcassets
import (
"fmt"
"image"
"image/png"
"os"
"path/filepath"
"github.com/nfnt/resize"
)
// AssetOutput represents the `Contents.json` file used in an image set.
type AssetOutput struct {
Images []AssetImage `json:"images"`
Info info `json:"info"`
Properties *AssetProperties `json:"properties,omitempty"`
inputs []assetInput
}
// WriteImages will write the file in `Images` to the specified path.
func (o *AssetOutput) WriteImages(path string) error {
loader := assetLoader{}
cache := map[string]image.Image{}
for _, input := range o.inputs {
img, ok := cache[loader.Key(input.Source)]
var err error = nil
if !ok {
img, err = loader.Load(input.Source)
if err != nil {
return fmt.Errorf("Failed to load image from source %v, error: %w", input.Source, err)
}
cache[loader.Key(input.Source)] = img
}
m := resize.Resize(uint(input.Width), uint(input.Height), img, resize.Lanczos3)
fileName := input.Filename + ".png"
dest := filepath.Join(path, fileName)
out, err := os.Create(dest)
if err != nil {
return fmt.Errorf("Failed to create image file, error: %w", err)
}
defer out.Close()
if err := png.Encode(out, m); err != nil {
return fmt.Errorf("Failed to write image file, error: %w", err)
}
}
return nil
}