-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiler.go
163 lines (139 loc) · 4.62 KB
/
tiler.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
// Package tiler converts a standard image.Image to a tiled representation.
// Tiles are read as 8x8 pixel images starting a the top-left of the image (0,0).
//
// This package is currently marked as 'internal' as it has very rough and
// untested code. Once this has been improved, and its API has been better
// defined, it may be moved to a public package.
package tiler
import (
"fmt"
"image"
"image/color"
)
// Tiled represents a tiled version of a the original image, consisting of unique 8x8 tiles.
type Tiled struct {
tileSize int // width & height of tile (usually 8x8 pixels)
width int // image width in pixels
height int // image height in pixels
rows int // image row count (in 8x8 tiles)
cols int // image column count (in 8x8 tiles)
tiles []Tile // a set of unique tiles making up the image
palette Palette // map of unique colours found in the image
}
// FromImage returns a new tile set from the given image data.
// The tile size is the width/height of a tile in pixels, and must in be multiples of 8px.
func FromImage(img image.Image, tileSize int) *Tiled {
if tileSize == 0 || tileSize%8 != 0 {
tileSize = 8
}
bg := Tiled{
tileSize: tileSize,
rows: img.Bounds().Dy() / tileSize,
cols: img.Bounds().Dx() / tileSize,
width: img.Bounds().Dx(),
height: img.Bounds().Dy(),
palette: make(map[string]color.Color),
}
tiles := convertToTiles(img, tileSize)
bg.generateUniqueTileList(tiles)
return &bg
}
func (b *Tiled) Width() int {
return b.width
}
func (b *Tiled) Height() int {
return b.height
}
// GetTile returns the tile for the given index number.
func (b *Tiled) GetTile(id int) (*Tile, error) {
if id >= b.TileCount() {
return nil, fmt.Errorf("background tile index out of range: %d", id)
}
return &b.tiles[id], nil
}
// TileCount is the total number of unique tiles in the background image.
func (b *Tiled) TileCount() int {
return len(b.tiles)
}
// ColourCount is the total number of unique colours in the image.
func (b *Tiled) ColourCount() int {
return len(b.palette)
}
// ToImage converts the tiled data to a new NRGBA image, with all tiles mapped
// back to their original positions.
func (b *Tiled) ToImage() (image.Image, error) {
if b == nil {
return nil, fmt.Errorf("no image data available to convert")
}
img := image.NewNRGBA(image.Rectangle{
Min: image.Point{X: 0, Y: 0},
Max: image.Point{X: b.Width(), Y: b.Height()},
})
for i := 0; i < b.TileCount(); i++ {
bgTile, _ := b.GetTile(i)
y := bgTile.RowPosInPixels()
x := bgTile.ColPosInPixels()
if err := b.drawTileAt(bgTile, img, y, x, OrientationNormal); err != nil {
return nil, err
}
for did := 0; did < bgTile.DuplicateCount(); did++ {
info, _ := bgTile.GetDuplicateInfo(did)
y = info.Row() * bgTile.tileSize
x = info.Col() * bgTile.tileSize
if err := b.drawTileAt(bgTile, img, y, x, info.Orientation()); err != nil {
return nil, err
}
}
}
return img, nil
}
// processes the tile list, recording all unique tiles, and adding duplicate
// info if the tile is already present.
func (b *Tiled) generateUniqueTileList(tiles []imageTile) {
for _, tile := range tiles {
b.addTile(&tile)
}
}
// add a tile to the current background tiles, either as a new unique tile
// or as a duplicate of an existing tile, when flipped in one of the supported
// vertical/horizontal orientations.
func (b *Tiled) addTile(tile *imageTile) {
// add as a duplicate if an existing tile match is found
for i := 0; i < len(b.tiles); i++ {
if b.addIfDuplicate(i, tile) {
return
}
}
// if not duplicate found, add as a new tile
b.addTileColoursToPalette(tile)
b.tiles = append(b.tiles, *NewWithOrientations(tile.posX, tile.posY, b.tileSize, tile.palette, tile.image))
}
// if a tile is a duplicate, add it to the duplicates list
func (b *Tiled) addIfDuplicate(tileID int, tile *imageTile) bool {
t := New(tile.posX, tile.posY, b.tileSize, tile.palette, tile.image)
if or, dupe := b.tiles[tileID].IsDuplicate(t); dupe {
b.tiles[tileID].AddDuplicateInfo(tile.posX, tile.posY, or)
return true
}
return false
}
// adds the tile palette to the global palette data
func (b *Tiled) addTileColoursToPalette(tile *imageTile) {
for k, v := range tile.palette {
if _, found := b.palette[k]; !found {
b.palette[k] = v
}
}
}
func (b *Tiled) drawTileAt(tile *Tile, img *image.NRGBA, pxOffsetY, pxOffsetX int, orientation Orientation) error {
for y := 0; y < tile.tileSize; y++ {
for x := 0; x < tile.tileSize; x++ {
colour, err := tile.OrientationAt(y, x, orientation)
if err != nil {
return fmt.Errorf("draw tile error: %w", err)
}
img.Set(pxOffsetX+x, pxOffsetY+y, colour)
}
}
return nil
}