-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathSurfaceLoaders.go
360 lines (317 loc) · 11.1 KB
/
SurfaceLoaders.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package giu
import (
go_ctx "context"
"fmt"
"image"
"image/color"
"image/draw"
"io/fs"
"log"
"net/http"
"time"
)
// SurfaceLoader is an interface that defines a method to serve an RGBA image.
type SurfaceLoader interface {
// ServeRGBA serves an RGBA image.
//
// Returns:
// - *image.RGBA: The RGBA image.
// - error: An error if the image could not be served.
ServeRGBA() (*image.RGBA, error)
}
// SurfaceLoaderFunc is a function type that serves an RGBA image.
type SurfaceLoaderFunc func() (*image.RGBA, error)
// LoadSurfaceFunc loads a surface using a SurfaceLoaderFunc.
//
// Parameters:
// - fn: The SurfaceLoaderFunc to use for loading the surface.
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the surface could not be loaded.
func (i *ReflectiveBoundTexture) LoadSurfaceFunc(fn SurfaceLoaderFunc, commit bool) error {
img, err := fn()
if err != nil {
return err
}
return i.SetSurfaceFromRGBA(img, commit)
}
// LoadSurface loads a surface using a SurfaceLoader.
//
// Parameters:
// - loader: The SurfaceLoader to use for loading the surface.
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the surface could not be loaded.
func (i *ReflectiveBoundTexture) LoadSurface(loader SurfaceLoader, commit bool) error {
img, err := loader.ServeRGBA()
if err != nil {
return fmt.Errorf("in ReflectiveBoundTexture LoadSurface after loader.ServeRGBA: %w", err)
}
return i.SetSurfaceFromRGBA(img, commit)
}
// LoadSurface loads a surface asynchronously using a SurfaceLoader.
//
// Parameters:
// - loader: The SurfaceLoader to use for loading the surface.
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the surface could not be loaded.
func (s *StatefulReflectiveBoundTexture) LoadSurface(loader SurfaceLoader, commit bool) error {
return s.LoadSurfaceAsync(loader, commit)
}
var _ SurfaceLoader = &FileLoader{}
// FileLoader is a struct that implements the SurfaceLoader interface for loading images from a file.
type FileLoader struct {
path string
}
// ServeRGBA loads an RGBA image from the file specified by the path in fileLoader.
//
// Returns:
// - *image.RGBA: The loaded RGBA image.
// - error: An error if the image could not be loaded.
func (f *FileLoader) ServeRGBA() (*image.RGBA, error) {
img, err := LoadImage(f.path)
if err != nil {
return nil, err
}
return img, nil
}
// NewFileLoader creates a new SurfaceLoader that loads images from the specified file path.
//
// Parameters:
// - path: The path to the file to load the image from.
//
// Returns:
// - SurfaceLoader: A new SurfaceLoader for loading images from the specified file path.
func NewFileLoader(path string) *FileLoader {
return &FileLoader{
path: path,
}
}
// SetSurfaceFromFile loads an image from the specified file path and sets it as the surface of the ReflectiveBoundTexture.
//
// Parameters:
// - path: The path to the file to load the image from.
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the image could not be loaded or set as the surface.
func (i *ReflectiveBoundTexture) SetSurfaceFromFile(path string, commit bool) error {
return i.LoadSurface(NewFileLoader(path), commit)
}
// SetSurfaceFromFile loads an image from the specified file path and sets it as the surface of the StatefulReflectiveBoundTexture.
//
// Parameters:
// - path: The path to the file to load the image from.
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the image could not be loaded or set as the surface.
func (s *StatefulReflectiveBoundTexture) SetSurfaceFromFile(path string, commit bool) error {
return s.LoadSurface(NewFileLoader(path), commit)
}
var _ SurfaceLoader = &FsFileLoader{}
// FsFileLoader is a struct that implements the SurfaceLoader interface for loading images from a file and embedded fs.
type FsFileLoader struct {
file fs.File
}
// ServeRGBA loads an RGBA image from the file specified by the path in fileLoader.
//
// Returns:
// - *image.RGBA: The loaded RGBA image.
// - error: An error if the image could not be loaded.
func (f *FsFileLoader) ServeRGBA() (*image.RGBA, error) {
img, err := PNGToRgba(f.file)
if err != nil {
return nil, err
}
return img, nil
}
// NewFsFileLoader creates a new SurfaceLoader that loads images from the specified file interface.
//
// Parameters:
// - file: the file interface representing the file
//
// Returns:
// - SurfaceLoader: A new SurfaceLoader for loading images from the specified file path.
func NewFsFileLoader(file fs.File) *FsFileLoader {
return &FsFileLoader{
file: file,
}
}
// SetSurfaceFromFsFile loads an image from the specified file interface and sets it as the surface of the ReflectiveBoundTexture.
//
// Parameters:
// - file: the file interface representing the file
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the image could not be loaded or set as the surface.
func (i *ReflectiveBoundTexture) SetSurfaceFromFsFile(file fs.File, commit bool) error {
return i.LoadSurface(NewFsFileLoader(file), commit)
}
// SetSurfaceFromFsFile loads an image from the specified file interface and sets it as the surface of the StatefulReflectiveBoundTexture.
//
// Parameters:
// - file: the file interface representing the file
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the image could not be loaded or set as the surface.
func (s *StatefulReflectiveBoundTexture) SetSurfaceFromFsFile(file fs.File, commit bool) error {
return s.LoadSurface(NewFsFileLoader(file), commit)
}
var _ SurfaceLoader = &URLLoader{}
// URLLoader is a SurfaceLoader that loads images from a specified URL.
type URLLoader struct {
url string
timeout time.Duration
httpdir string
}
// ServeRGBA loads an image from the URL and returns it as an RGBA image.
//
// Returns:
// - *image.RGBA: The loaded RGBA image.
// - error: An error if the image could not be loaded.
func (u *URLLoader) ServeRGBA() (*image.RGBA, error) {
t := &http.Transport{}
t.RegisterProtocol("file", http.NewFileTransport(http.Dir(u.httpdir)))
client := &http.Client{
Transport: t,
Timeout: u.timeout,
}
req, err := http.NewRequestWithContext(go_ctx.Background(), http.MethodGet, u.url, http.NoBody)
if err != nil {
return nil, fmt.Errorf("urlLoader serveRGBA after http.NewRequestWithContext: %w", err)
}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("urlLoader serveRGBA after client.Do: %w", err)
}
defer func() {
err := resp.Body.Close()
if err != nil {
log.Println(err)
}
}()
img, _, err := image.Decode(resp.Body)
if err != nil {
return nil, fmt.Errorf("urlLoader serveRGBA after image.Decode: %w", err)
}
return ImageToRgba(img), nil
}
// NewURLLoader creates a new SurfaceLoader that loads images from the specified URL.
//
// Parameters:
// - url: The URL to load the image from.
// - httpdir: The root directory for file:// URLs.
// - timeout: The timeout duration for the HTTP request.
//
// Returns:
// - SurfaceLoader: A new SurfaceLoader for loading images from the specified URL.
func NewURLLoader(url, httpdir string, timeout time.Duration) *URLLoader {
return &URLLoader{
url: url,
timeout: timeout,
httpdir: httpdir,
}
}
// SetFSRoot sets the root directory for file:// URLs.
//
// Parameters:
// - root: The root directory to set.
func (i *ReflectiveBoundTexture) SetFSRoot(root string) {
i.fsroot = root
}
// GetFSRoot returns the root directory for file:// URLs.
//
// Returns:
// - string: The root directory.
func (i *ReflectiveBoundTexture) GetFSRoot() string {
return i.fsroot
}
// SetSurfaceFromURL loads an image from the specified URL and sets it as the surface of the ReflectiveBoundTexture.
//
// Parameters:
// - url: The URL to load the image from.
// - timeout: The timeout duration for the HTTP request.
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the image could not be loaded or set as the surface.
func (i *ReflectiveBoundTexture) SetSurfaceFromURL(url string, timeout time.Duration, commit bool) error {
return i.LoadSurface(NewURLLoader(url, i.fsroot, timeout), commit)
}
// SetSurfaceFromURL loads an image from the specified URL and sets it as the surface of the StatefulReflectiveBoundTexture.
//
// Parameters:
// - url: The URL to load the image from.
// - timeout: The timeout duration for the HTTP request.
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the image could not be loaded or set as the surface.
func (s *StatefulReflectiveBoundTexture) SetSurfaceFromURL(url string, timeout time.Duration, commit bool) error {
return s.LoadSurface(NewURLLoader(url, s.fsroot, timeout), commit)
}
var _ SurfaceLoader = &UniformLoader{}
// UniformLoader is a SurfaceLoader that creates a uniform color image.
type UniformLoader struct {
width, height int
color color.Color
}
// ServeRGBA creates a uniform color image and returns it as an RGBA image.
//
// Returns:
// - *image.RGBA: The created RGBA image.
// - error: An error if the image could not be created.
func (u *UniformLoader) ServeRGBA() (*image.RGBA, error) {
img := image.NewRGBA(image.Rect(0, 0, u.width, u.height))
draw.Draw(img, img.Bounds(), &image.Uniform{u.color}, image.Point{}, draw.Src)
return img, nil
}
// NewUniformLoader creates a new SurfaceLoader that creates a uniform color image.
//
// Parameters:
// - width: The width of the image.
// - height: The height of the image.
// - c: The color of the image.
//
// Returns:
// - SurfaceLoader: A new SurfaceLoader for creating a uniform color image.
func NewUniformLoader(width, height int, c color.Color) *UniformLoader {
return &UniformLoader{
width: width,
height: height,
color: c,
}
}
// SetSurfaceUniform creates a uniform color image and sets it as the surface of the ReflectiveBoundTexture.
//
// Parameters:
// - width: The width of the image.
// - height: The height of the image.
// - c: The color of the image.
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the image could not be created or set as the surface.
func (i *ReflectiveBoundTexture) SetSurfaceUniform(width, height int, c color.Color, commit bool) error {
return i.LoadSurface(NewUniformLoader(width, height, c), commit)
}
// SetSurfaceUniform creates a uniform color image and sets it as the surface of the StatefulReflectiveBoundTexture.
//
// Parameters:
// - width: The width of the image.
// - height: The height of the image.
// - c: The color of the image.
// - commit: A boolean flag indicating whether to commit the changes.
//
// Returns:
// - error: An error if the image could not be created or set as the surface.
func (s *StatefulReflectiveBoundTexture) SetSurfaceUniform(width, height int, c color.Color, commit bool) error {
return s.LoadSurface(NewUniformLoader(width, height, c), commit)
}