-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
win_image.go
146 lines (111 loc) · 3.27 KB
/
win_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright 2023 Milan Suk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this db 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 main
import (
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
"golang.org/x/image/webp"
)
func InitImageGlobal() {
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
image.RegisterFormat("webp", "webp", webp.Decode, webp.DecodeConfig)
image.RegisterFormat("jpeg", "jpeg", jpeg.Decode, jpeg.DecodeConfig)
image.RegisterFormat("jpg", "jpeg", jpeg.Decode, jpeg.DecodeConfig)
image.RegisterFormat("gif", "gif", gif.Decode, gif.DecodeConfig)
image.RegisterFormat("tiff", "tiff", tiff.Decode, tiff.DecodeConfig)
image.RegisterFormat("bmp", "bmp", bmp.Decode, bmp.DecodeConfig)
}
type WinImage struct {
origSize OsV2
maxUseSize OsV2
path WinMedia
blobDbLoadTicks int64
blobHash OsHash
texture *WinTexture
lastDrawTick int64
}
func NewWinImage(path WinMedia, disk *Disk) (*WinImage, error) {
var img WinImage
img.path = path
img.blobDbLoadTicks = -1
blob, err := path.GetBlob(disk)
if err != nil {
return nil, fmt.Errorf("GetFileBlob() failed: %w", err)
}
err = img.SetBlob(blob)
if err != nil {
return nil, fmt.Errorf("SetBlob() failed: %w", err)
}
return &img, nil
}
func (img *WinImage) FreeTexture() error {
if img.texture != nil {
img.texture.Destroy()
}
img.texture = nil
return nil
}
func (img *WinImage) GetBytes() int {
if img.texture != nil {
sz := img.texture.size
return sz.X * sz.Y * 4
}
return 0
}
func (img *WinImage) Destroy() error {
return img.FreeTexture()
}
func (img *WinImage) SetBlob(blob []byte) error {
if len(blob) == 0 {
return nil //empty = no error
}
var err error
img.blobHash, err = InitOsHash(blob)
if err != nil {
return fmt.Errorf("InitOsHash() failed: %w", err)
}
img.texture, _, err = InitWinTextureFromBlob(blob)
if err != nil {
return fmt.Errorf("InitUiTextureFromBlob() failed: %w", err)
}
img.origSize = img.texture.size
return nil
}
func (img *WinImage) NeedDbChanged(blobDbLoadTicks int64) bool {
return img.path.IsDb() && img.blobDbLoadTicks != blobDbLoadTicks
}
func (img *WinImage) Maintenance(win *Win) (bool, error) {
//is used
if !img.maxUseSize.Is() && !OsIsTicksIn(img.lastDrawTick, 10000) {
// free un-used
if img.texture != nil && !OsIsTicksIn(img.lastDrawTick, 10000) {
img.FreeTexture()
}
return false, nil
}
img.maxUseSize = OsV2{0, 0} // reset
return true, nil
}
func (img *WinImage) Draw(coord OsV4, depth int, cd OsCd) error {
img.maxUseSize = coord.Size.Max(img.maxUseSize)
if img.texture != nil {
img.texture.DrawQuad(coord, depth, cd)
}
img.lastDrawTick = OsTicks()
return nil
}