-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimages.go
More file actions
613 lines (568 loc) · 14.6 KB
/
Copy pathimages.go
File metadata and controls
613 lines (568 loc) · 14.6 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package main
import (
"encoding/csv"
"fmt"
"image"
"image/png"
"log"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/hajimehoshi/ebiten/v2"
"gothoom/climg"
)
// imageCache lazily loads images from the CL_Images archive. If an image is not
// present, nil is cached to avoid repeated lookups.
const maxColors = 30
type imageKey struct {
id uint16
frame uint16
}
type sheetKey struct {
id uint16
forceTransparent bool
colorsLen uint8
colors [maxColors]byte
}
type mobileKey struct {
id uint16
state uint8
colorsLen uint8
colors [maxColors]byte
}
type mobileBlendKey struct {
from mobileKey
to mobileKey
step uint8
total uint8
}
type pictBlendKey struct {
id uint16
from uint16
to uint16
step uint8
total uint8
}
type scaledImageKey struct {
imageKey
scale uint8
}
type scaledMobileKey struct {
mobileKey
scale uint8
}
var (
// imageCache holds cropped animation frames keyed by picture ID and
// frame index.
imageCache = make(map[imageKey]*ebiten.Image)
// sheetCache holds the full sprite sheet for a picture ID and optional
// custom color palette. The key combines the picture ID with the custom
// color bytes so tinted versions are cached separately.
sheetCache = make(map[sheetKey]*ebiten.Image)
// mobileCache caches individual mobile frames keyed by picture ID,
// state, and color overrides.
mobileCache = make(map[mobileKey]*ebiten.Image)
// mobileBlendCache stores pre-rendered blended mobile frames.
mobileBlendCache = make(map[mobileBlendKey]*ebiten.Image)
// pictBlendCache stores pre-rendered blended picture frames.
pictBlendCache = make(map[pictBlendKey]*ebiten.Image)
// scaledImageCache stores pixel-art upscaled world picture frames.
scaledImageCache = make(map[scaledImageKey]*ebiten.Image)
// scaledMobileCache stores pixel-art upscaled mobile frames.
scaledMobileCache = make(map[scaledMobileKey]*ebiten.Image)
imageMu sync.Mutex
clImages *climg.CLImages
dumpImgOnce sync.Once
dumpImgMu sync.Mutex
dumpedImgIDs = make(map[uint16]struct{})
imgMetaWriter *csv.Writer
)
func makeSheetKey(id uint16, colors []byte, forceTransparent bool) sheetKey {
var k sheetKey
k.id = id
k.forceTransparent = forceTransparent
if len(colors) > 0 {
l := len(colors)
if l > maxColors {
l = maxColors
}
k.colorsLen = uint8(l)
copy(k.colors[:], colors[:l])
}
return k
}
func makeImageKey(id uint16, frame int) imageKey {
return imageKey{id: id, frame: uint16(frame)}
}
func makeMobileKey(id uint16, state uint8, colors []byte) mobileKey {
var k mobileKey
k.id = id
k.state = state
if len(colors) > 0 {
l := len(colors)
if l > maxColors {
l = maxColors
}
k.colorsLen = uint8(l)
copy(k.colors[:], colors[:l])
}
return k
}
// loadSheet retrieves the full sprite sheet for the specified picture ID.
// The forceTransparent flag forces palette index 0 to be fully transparent
// regardless of the pictDef flags. Mobile sprites require this behavior
// since the original client always treats index 0 as transparent for them.
func loadSheet(id uint16, colors []byte, forceTransparent bool) *ebiten.Image {
if id == 0xffff {
return nil
}
key := makeSheetKey(id, colors, forceTransparent)
imageMu.Lock()
if img, ok := sheetCache[key]; ok {
imageMu.Unlock()
return img
}
imageMu.Unlock()
if clImages != nil {
var t0 time.Time
if measureLoads {
t0 = time.Now()
}
if img := clImages.Get(uint32(id), colors, forceTransparent); img != nil {
statImageLoaded(id)
if imgDump && colors == nil && !forceTransparent {
dumpImageSheet(id, img)
}
imageMu.Lock()
sheetCache[key] = img
imageMu.Unlock()
if measureLoads {
frames := clImages.NumFrames(uint32(id))
if frames <= 0 {
frames = 1
}
innerW := img.Bounds().Dx() - 2
innerH := img.Bounds().Dy() - 2
h := 0
if frames > 0 {
h = innerH / frames
}
dtms := float64(time.Since(t0).Nanoseconds()) / 1e6
log.Printf("measure: sprite id=%d frames=%d size=%dx%d load=%.2fms frame=%d", id, frames, innerW, h, dtms, frameCounter)
}
return img
}
log.Printf("missing image %d", id)
} else {
log.Printf("CL_Images not loaded when requesting image %d", id)
}
return nil
}
func dumpImageSheet(id uint16, sheet *ebiten.Image) {
if isWASM {
return
}
dumpImgOnce.Do(func() {
os.MkdirAll(filepath.Join("dump", "img"), 0755)
if f, err := os.Create(filepath.Join("dump", "img", "metadata.csv")); err == nil {
imgMetaWriter = csv.NewWriter(f)
imgMetaWriter.Write([]string{"id", "width", "height", "frames", "flags", "name"})
}
})
dumpImgMu.Lock()
if _, ok := dumpedImgIDs[id]; ok {
dumpImgMu.Unlock()
return
}
dumpedImgIDs[id] = struct{}{}
dumpImgMu.Unlock()
frames := 1
if clImages != nil {
frames = clImages.NumFrames(uint32(id))
}
if frames <= 0 {
frames = 1
}
innerHeight := sheet.Bounds().Dy() - 2
innerWidth := sheet.Bounds().Dx() - 2
h := innerHeight / frames
for f := 0; f < frames; f++ {
y := 1 + f*h
frameImg := sheet.SubImage(image.Rect(1, y, 1+innerWidth, y+h)).(*ebiten.Image)
fn := filepath.Join("dump", "img", fmt.Sprintf("%d_%d.png", id, f))
if file, err := os.Create(fn); err == nil {
png.Encode(file, frameImg)
file.Close()
}
}
width, height := innerWidth, h
var flags uint32
var name string
if clImages != nil {
if it, ok := clImages.Item(uint32(id)); ok {
flags = it.Flags
name = it.Name
}
}
if imgMetaWriter != nil {
imgMetaWriter.Write([]string{
strconv.Itoa(int(id)),
strconv.Itoa(width),
strconv.Itoa(height),
strconv.Itoa(frames),
strconv.FormatUint(uint64(flags), 10),
name,
})
imgMetaWriter.Flush()
}
}
// loadImage retrieves the first frame for the specified picture ID. Images are
// cached after the first load to avoid reopening files each frame.
func loadImage(id uint16) *ebiten.Image {
return loadImageFrame(id, 0)
}
// loadImageFrame retrieves a specific animation frame for the specified picture
// ID. Frames are cached individually after the first load.
func loadImageFrame(id uint16, frame int) *ebiten.Image {
origKey := makeImageKey(id, frame)
imageMu.Lock()
if img, ok := imageCache[origKey]; ok {
imageMu.Unlock()
return img
}
imageMu.Unlock()
sheet := loadSheet(id, nil, false)
if sheet == nil {
imageMu.Lock()
imageCache[origKey] = nil
imageMu.Unlock()
return nil
}
frames := 1
if clImages != nil {
frames = clImages.NumFrames(uint32(id))
}
if frames <= 0 {
frames = 1
}
frame = frame % frames
innerHeight := sheet.Bounds().Dy() - 2
innerWidth := sheet.Bounds().Dx() - 2
h := innerHeight / frames
imageMu.Lock()
for f := 0; f < frames; f++ {
k := makeImageKey(id, f)
if _, ok := imageCache[k]; !ok {
y := 1 + f*h
imageCache[k] = sheet.SubImage(image.Rect(1, y, 1+innerWidth, y+h)).(*ebiten.Image)
}
}
img := imageCache[makeImageKey(id, frame)]
imageMu.Unlock()
return img
}
// loadMobileFrame retrieves a cropped frame from a mobile sprite sheet based on
// the state value provided by the server. The optional colors slice allows
// caller-supplied palette overrides to be cached separately.
func loadMobileFrame(id uint16, state uint8, colors []byte) *ebiten.Image {
baseKey := makeMobileKey(id, 0, colors)
key := baseKey
key.state = state
imageMu.Lock()
if img, ok := mobileCache[key]; ok {
imageMu.Unlock()
return img
}
imageMu.Unlock()
sheet := loadSheet(id, colors, true)
if sheet == nil {
imageMu.Lock()
mobileCache[key] = nil
imageMu.Unlock()
return nil
}
innerSize := (sheet.Bounds().Dx() - 2) / 16
x := 1 + int(state&0x0F)*innerSize
y := 1 + int(state>>4)*innerSize
if x+innerSize > sheet.Bounds().Dx()-1 || y+innerSize > sheet.Bounds().Dy()-1 {
imageMu.Lock()
mobileCache[key] = nil
imageMu.Unlock()
return nil
}
imageMu.Lock()
for yy := 0; yy < 16; yy++ {
for xx := 0; xx < 16; xx++ {
k := baseKey
k.state = uint8(yy<<4 | xx)
if _, ok := mobileCache[k]; !ok {
sx := 1 + xx*innerSize
sy := 1 + yy*innerSize
if sx+innerSize <= sheet.Bounds().Dx()-1 && sy+innerSize <= sheet.Bounds().Dy()-1 {
mobileCache[k] = sheet.SubImage(image.Rect(sx, sy, sx+innerSize, sy+innerSize)).(*ebiten.Image)
} else {
mobileCache[k] = nil
}
}
}
}
img := mobileCache[key]
imageMu.Unlock()
return img
}
func ebitenImageToRGBA(img *ebiten.Image) *image.RGBA {
b := img.Bounds()
w, h := b.Dx(), b.Dy()
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
if w == 0 || h == 0 {
return rgba
}
img.ReadPixels(rgba.Pix)
return rgba
}
func upscaleSpriteImage(img *ebiten.Image, factor int) *ebiten.Image {
if factor <= 1 || img == nil {
return img
}
src := ebitenImageToRGBA(img)
var scaled *image.RGBA
switch factor {
case 2:
scaled = scale2xRGBA(src)
case 3:
scaled = scale3xRGBA(src)
case 4:
scaled = scale4xRGBA(src)
default:
return img
}
return newImageFromImage(scaled)
}
func getScaledPictureFrame(id uint16, frame int, img *ebiten.Image) *ebiten.Image {
factor := spriteUpscaleFactor()
if factor <= 1 || img == nil || !gs.SpriteUpscaleFilter {
return img
}
key := scaledImageKey{imageKey: makeImageKey(id, frame), scale: uint8(factor)}
imageMu.Lock()
if cached, ok := scaledImageCache[key]; ok {
imageMu.Unlock()
return cached
}
imageMu.Unlock()
scaled := upscaleSpriteImage(img, factor)
imageMu.Lock()
scaledImageCache[key] = scaled
imageMu.Unlock()
return scaled
}
func getScaledMobileFrame(key mobileKey, img *ebiten.Image) *ebiten.Image {
factor := spriteUpscaleFactor()
if factor <= 1 || img == nil || !gs.SpriteUpscaleFilter {
return img
}
sKey := scaledMobileKey{mobileKey: key, scale: uint8(factor)}
imageMu.Lock()
if cached, ok := scaledMobileCache[sKey]; ok {
imageMu.Unlock()
return cached
}
imageMu.Unlock()
scaled := upscaleSpriteImage(img, factor)
imageMu.Lock()
scaledMobileCache[sKey] = scaled
imageMu.Unlock()
return scaled
}
// mobileSize returns the dimension of a single mobile frame for the given
// image ID. If the image cannot be loaded, 0 is returned.
func mobileSize(id uint16) int {
sheet := loadSheet(id, nil, true)
if sheet == nil {
return 0
}
return (sheet.Bounds().Dx() - 2) / 16
}
func mobileBlendFrame(from, to mobileKey, prevImg, img *ebiten.Image, step, total int) *ebiten.Image {
if prevImg == nil || img == nil {
return nil
}
k := mobileBlendKey{from: from, to: to, step: uint8(step), total: uint8(total)}
imageMu.Lock()
if b, ok := mobileBlendCache[k]; ok {
imageMu.Unlock()
return b
}
imageMu.Unlock()
size := img.Bounds().Dx()
if s := prevImg.Bounds().Dx(); s > size {
size = s
}
blended := newImage(size, size)
alpha := float32(step) / float32(total)
offPrev := (size - prevImg.Bounds().Dx()) / 2
op1 := acquireDrawOpts()
op1.ColorScale.Reset()
op1.ColorScale.ScaleAlpha(1 - alpha)
op1.Blend = ebiten.BlendCopy
op1.GeoM.Reset()
op1.GeoM.Translate(float64(offPrev), float64(offPrev))
blended.DrawImage(prevImg, op1)
op1.ColorScale.Reset()
op1.GeoM.Reset()
op1.Filter = 0
op1.DisableMipmaps = false
op1.Blend = ebiten.BlendSourceOver
releaseDrawOpts(op1)
offCur := (size - img.Bounds().Dx()) / 2
op2 := acquireDrawOpts()
op2.ColorScale.Reset()
op2.ColorScale.ScaleAlpha(alpha)
op2.Blend = ebiten.BlendLighter
op2.GeoM.Reset()
op2.GeoM.Translate(float64(offCur), float64(offCur))
blended.DrawImage(img, op2)
op2.ColorScale.Reset()
op2.GeoM.Reset()
op2.Filter = 0
op2.DisableMipmaps = false
op2.Blend = ebiten.BlendSourceOver
releaseDrawOpts(op2)
imageMu.Lock()
mobileBlendCache[k] = blended
imageMu.Unlock()
return blended
}
func pictBlendFrame(id uint16, fromFrame, toFrame int, prevImg, img *ebiten.Image, step, total int) *ebiten.Image {
if prevImg == nil || img == nil {
return nil
}
k := pictBlendKey{id: id, from: uint16(fromFrame), to: uint16(toFrame), step: uint8(step), total: uint8(total)}
imageMu.Lock()
if b, ok := pictBlendCache[k]; ok {
imageMu.Unlock()
return b
}
imageMu.Unlock()
w1, h1 := prevImg.Bounds().Dx(), prevImg.Bounds().Dy()
w2, h2 := img.Bounds().Dx(), img.Bounds().Dy()
w := w1
if w2 > w {
w = w2
}
h := h1
if h2 > h {
h = h2
}
blended := newImage(w, h)
alpha := float32(step) / float32(total)
offPrevX := (w - w1) / 2
offPrevY := (h - h1) / 2
op1 := acquireDrawOpts()
op1.ColorScale.Reset()
op1.ColorScale.ScaleAlpha(1 - alpha)
op1.Blend = ebiten.BlendCopy
op1.GeoM.Reset()
op1.GeoM.Translate(float64(offPrevX), float64(offPrevY))
blended.DrawImage(prevImg, op1)
op1.ColorScale.Reset()
op1.GeoM.Reset()
op1.Filter = 0
op1.DisableMipmaps = false
op1.Blend = ebiten.BlendSourceOver
releaseDrawOpts(op1)
offCurX := (w - w2) / 2
offCurY := (h - h2) / 2
op2 := acquireDrawOpts()
op2.ColorScale.Reset()
op2.ColorScale.ScaleAlpha(alpha)
op2.Blend = ebiten.BlendLighter
op2.GeoM.Reset()
op2.GeoM.Translate(float64(offCurX), float64(offCurY))
blended.DrawImage(img, op2)
op2.ColorScale.Reset()
op2.GeoM.Reset()
op2.Filter = 0
op2.DisableMipmaps = false
op2.Blend = ebiten.BlendSourceOver
releaseDrawOpts(op2)
imageMu.Lock()
pictBlendCache[k] = blended
imageMu.Unlock()
return blended
}
type imageCacheStatsData struct {
sheetCount int
sheetBytes int
frameCount int
frameBytes int
scaledFrameCount int
scaledFrameBytes int
mobileCount int
mobileBytes int
scaledMobileCount int
scaledMobileBytes int
mobileBlendCount int
mobileBlendBytes int
pictBlendCount int
pictBlendBytes int
}
// imageCacheStats returns the counts and approximate memory usage in bytes for
// each of the image caches: sheets, cropped frames, scaled variants, and blends.
func imageCacheStats() imageCacheStatsData {
imageMu.Lock()
defer imageMu.Unlock()
var stats imageCacheStatsData
for _, img := range sheetCache {
if img != nil {
stats.sheetCount++
b := img.Bounds()
stats.sheetBytes += b.Dx() * b.Dy() * 4
}
}
for _, img := range imageCache {
if img != nil {
stats.frameCount++
b := img.Bounds()
stats.frameBytes += b.Dx() * b.Dy() * 4
}
}
for _, img := range scaledImageCache {
if img != nil {
stats.scaledFrameCount++
b := img.Bounds()
stats.scaledFrameBytes += b.Dx() * b.Dy() * 4
}
}
for _, img := range mobileCache {
if img != nil {
stats.mobileCount++
b := img.Bounds()
stats.mobileBytes += b.Dx() * b.Dy() * 4
}
}
for _, img := range scaledMobileCache {
if img != nil {
stats.scaledMobileCount++
b := img.Bounds()
stats.scaledMobileBytes += b.Dx() * b.Dy() * 4
}
}
for _, img := range mobileBlendCache {
if img != nil {
stats.mobileBlendCount++
b := img.Bounds()
stats.mobileBlendBytes += b.Dx() * b.Dy() * 4
}
}
for _, img := range pictBlendCache {
if img != nil {
stats.pictBlendCount++
b := img.Bounds()
stats.pictBlendBytes += b.Dx() * b.Dy() * 4
}
}
return stats
}