2022-08-11 09:10:07 +00:00
|
|
|
// Copyright (c) 2019-2022 David Vogel
|
|
|
|
//
|
|
|
|
// This software is released under the MIT License.
|
|
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2022-08-11 23:06:22 +00:00
|
|
|
type ImageTiles []ImageTile
|
|
|
|
|
2022-08-11 09:10:07 +00:00
|
|
|
// LoadImageTiles "loads" all images in the directory at the given path.
|
2022-08-11 23:06:22 +00:00
|
|
|
func LoadImageTiles(path string, scaleDivider int) (ImageTiles, error) {
|
2022-08-11 09:10:07 +00:00
|
|
|
if scaleDivider < 1 {
|
|
|
|
return nil, fmt.Errorf("invalid scale of %v", scaleDivider)
|
|
|
|
}
|
|
|
|
|
2022-08-11 23:06:22 +00:00
|
|
|
var imageTiles ImageTiles
|
2022-08-11 09:10:07 +00:00
|
|
|
|
|
|
|
files, err := filepath.Glob(filepath.Join(path, "*.png"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
imageTile, err := NewImageTile(file, scaleDivider)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageTiles = append(imageTiles, imageTile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageTiles, nil
|
|
|
|
}
|
2022-08-11 23:06:22 +00:00
|
|
|
|
|
|
|
// InvalidateAboveY invalidates all cached images that have no pixel at the given y coordinate or below.
|
|
|
|
func (it ImageTiles) InvalidateAboveY(y int) {
|
2022-08-12 09:39:55 +00:00
|
|
|
for i := range it {
|
|
|
|
tile := &it[i] // Need to copy a reference.
|
2022-08-11 23:06:22 +00:00
|
|
|
if tile.Bounds().Max.Y <= y {
|
|
|
|
tile.Invalidate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|