-
Notifications
You must be signed in to change notification settings - Fork 9
/
lutcache.go
118 lines (110 loc) · 3.45 KB
/
lutcache.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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package palette
import (
"encoding/json"
"fmt"
"image"
"os"
"github.com/divVerent/aaaaxy/internal/flag"
"github.com/divVerent/aaaaxy/internal/log"
"github.com/divVerent/aaaaxy/internal/screenshot"
"github.com/divVerent/aaaaxy/internal/vfs"
)
var (
paletteMaxCycles = flag.Float64("palette_max_cycles", 640*360*256*4, "maximum number of cycles to spend on palette generation; only applies if there is no cached palette file in the game")
)
type lutMeta struct {
Size int `json:"size"`
PerRow int `json:"per_row"`
Width int `json:"width"`
}
func SaveCachedLUTs(w, h int, dir string) error {
for numLUTs := 1; numLUTs <= 2; numLUTs++ {
for name, p := range data {
name := fmt.Sprintf("%s/lut_%s_%d.png", dir, name, numLUTs)
metaName := name + ".json"
if _, err := os.Stat(name); err == nil {
if _, err := os.Stat(metaName); err == nil {
// File already there. Skip.
log.Infof("skipping %s (already there)", name)
continue
}
}
log.Infof("generating %s...", name)
bounds := image.Rectangle{
Min: image.Point{},
Max: image.Point{X: w, Y: h},
}
img, size, perRow, width := p.computeLUT(bounds, numLUTs, *paletteMaxCycles)
log.Infof("saving %s (colors=%d size=%d perRow=%d width=%d)...", name, p.size, size, perRow, width)
err := screenshot.Write(img, name)
if err != nil {
return err
}
meta := lutMeta{
Size: size,
PerRow: perRow,
Width: width,
}
f, err := vfs.OSCreate(vfs.WorkDir, metaName)
if err != nil {
return err
}
j := json.NewEncoder(f)
j.SetIndent("", "\t")
err = j.Encode(meta)
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
}
}
return nil
}
func (p *Palette) loadLUT(numLUTs int) (image.Image, int, int, int, error) {
name := fmt.Sprintf("lut_%s_%d.png", p.name, numLUTs)
data, err := vfs.Load("generated", name)
if err != nil {
return nil, 0, 0, 0, fmt.Errorf("could not open %v: %w", name, err)
}
defer data.Close()
img, _, err := image.Decode(data)
if err != nil {
return nil, 0, 0, 0, fmt.Errorf("could not decode %v: %w", name, err)
}
var meta lutMeta
metaName := name + ".json"
j, err := vfs.Load("generated", metaName)
if err != nil {
return nil, 0, 0, 0, fmt.Errorf("could not open %v: %w", metaName, err)
}
defer j.Close()
err = json.NewDecoder(j).Decode(&meta)
if err != nil {
return nil, 0, 0, 0, fmt.Errorf("could not decode palette LUT json config file %v: %w", metaName, err)
}
return img, meta.Size, meta.PerRow, meta.Width, nil
}
func (p *Palette) ToLUT(bounds image.Rectangle, numLUTs int) (image.Image, int, int, int) {
lut, lutSize, perRow, lutWidth, err := p.loadLUT(numLUTs)
if err != nil {
log.Warningf("cached palette data not found, generating at runtime: %v", err)
lut, lutSize, perRow, lutWidth = p.computeLUT(bounds, numLUTs, *paletteMaxCycles)
}
return lut, lutSize, perRow, lutWidth
}