-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpacker.go
278 lines (230 loc) · 7.18 KB
/
packer.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
package packer
import (
"image"
"image/draw"
"sort"
"path"
)
type ImageData struct {
img image.Image
filename string
position image.Point
origBounds image.Rectangle
// extrudeBounds image.Rectangle
extrudeOffset image.Point
}
func NewImageData(img image.Image, filename string) ImageData {
return ImageData{
img: img,
filename: filename,
position: image.Point{},
origBounds: img.Bounds(),
}
}
func (i *ImageData) Area() int {
size := i.img.Bounds().Size()
return size.X * size.Y
}
func PrepareImageList(images []ImageData, extrude int) {
// Sort images by their filename, so that there is some sort of determinism on input of files
// TODO - this might be automatic, but I haven't tested it cross platform. Maybe check docs
sort.Slice(images, func(i, j int) bool {
return images[i].filename < images[j].filename
})
for i := range images {
images[i].img = ExtrudeImage(images[i].img, extrude)
// images[i].extrudeBounds = images[i].img.Bounds()
images[i].extrudeOffset = image.Point{extrude, extrude}
}
}
func RowWisePacker(images []ImageData, width, height int) []ImageData {
// 1. Sort by area
// 2. Pack as many as we can into a row, then go to the next row
// Sort by area
sort.Slice(images, func(i, j int) bool { return (images[i].Area()) > (images[j].Area()) })
targetBounds := image.Rect(0, 0, width, height) // placed image must fall inside the targetBounds
placed := make([]ImageData, 0)
currentPos := image.Point{0, 0}
nextRow := 0
// Place Greedily
for i := 0; i < len(images); i++ {
attemptRect := images[i].img.Bounds().Add(currentPos)
if !attemptRect.In(targetBounds) {
// If the attempt rectangle isn't fully inside the target rect, then fail this position
// So we go to the next row
currentPos.X = 0
currentPos.Y = nextRow
i-- // Try again
continue
}
// Track the rect which pushed the row down the furthest
if nextRow < attemptRect.Max.Y {
nextRow = attemptRect.Max.Y
}
// If we were successful in placing, then place it officially
images[i].position = currentPos
placed = append(placed, images[i])
currentPos.X = attemptRect.Max.X
}
return placed
}
func BasicScanlinePacker(images []ImageData, width, height int) []ImageData {
// 1. Sort rectangles based on order
// 2. loop through width,height rectangle and place them at first available position
// Sort by area
sort.Slice(images, func(i, j int) bool { return (images[i].Area()) > (images[j].Area()) })
targetBounds := image.Rect(0, 0, width, height) // placed image must fall inside the targetBounds
placed := make([]ImageData, 0)
// Place Greedily
for i := range images {
attempt:
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
// Check if we can place it here
attemptPos := image.Point{x,y}
attemptRect := images[i].img.Bounds().Add(attemptPos)
if !attemptRect.In(targetBounds) {
// If the attempt rectangle isn't fully inside the target rect, then fail this position
continue
}
success := true
for _,placedImg := range placed {
placedRect := placedImg.img.Bounds().Add(placedImg.position)
if attemptRect.Overlaps(placedRect) {
// If there is ever an overlap then break
// However, we can safely increment X to the point after the image
x = placedRect.Max.X
success = false
break
}
}
if !success { continue }
// If we were successful in placing, then place it officially
images[i].position = attemptPos
placed = append(placed, images[i])
break attempt
}
}
}
return placed
}
func Pack(imageName string, images []ImageData, width, height int) (image.Image, SerializedSpritesheet) {
data := SerializedSpritesheet{
ImageName: path.Base(imageName),
Frames: make(map[string]SerializedFrame),
Meta: make(map[string]interface{}),
}
data.Meta["protocol"] = "github.com/unitoftime/packer"
atlasBounds := image.Rect(0, 0, width, height)
atlas := image.NewNRGBA(atlasBounds)
currentBounds := image.Rectangle{}
// currentPos := image.Point{}
for _, imageData := range images {
// img := imageData.img
// origBounds := img.Bounds()
// Extrude image
// img = ExtrudeImage(img, extrude)
// extrudeBounds := img.Bounds()
// destOrigBounds := origBounds.Add(currentPos).Add(image.Point{extrude,extrude})
// destBounds := extrudeBounds.Add(currentPos)
// draw.Draw(atlas, destBounds, img, image.ZP, draw.Src)
// currentPos.X += extrudeBounds.Dx()
img := imageData.img
extrudeBounds := img.Bounds()
destOrigBounds := imageData.origBounds.Add(imageData.position).Add(imageData.extrudeOffset)
destBounds := extrudeBounds.Add(imageData.position)
draw.Draw(atlas, destBounds, img, image.ZP, draw.Src)
// currentPos.X += extrudeBounds.Dx()
currentBounds = currentBounds.Union(destBounds)
data.Frames[imageData.filename] = SerializedFrame{
Frame: SerializedRect{
X: float64(destOrigBounds.Min.X),
Y: float64(destOrigBounds.Min.Y),
W: float64(destOrigBounds.Dx()),
H: float64(destOrigBounds.Dy()),
},
Rotated: false, // TODO
Trimmed: false, // TODO
SpriteSourceSize: SerializedRect{
// TODO
},
SourceSize: SerializedDim{
// TODO
},
Pivot: SerializedPos{
// TODO
},
}
}
// TODO - shrink final atlas down if possible
return atlas, data
}
// TODO - this is inefficient, but might not matter that much. I think most people will only extrude once
func ExtrudeImage(img image.Image, extrude int) image.Image {
for i := 0; i < extrude; i++ {
img = ExtrudeImageOnce(img)
}
return img
}
// TODO - needs cleanup
func ExtrudeImageOnce(img image.Image) image.Image {
extrude := 1
bounds := img.Bounds()
newImg := image.NewNRGBA(image.Rect(0, 0, bounds.Dx() + (2 * extrude), bounds.Dy() + (2 * extrude)))
dstBounds := newImg.Bounds()
draw.Draw(newImg, bounds.Add(image.Point{extrude,extrude}), img, image.ZP, draw.Src)
// Outer Rows
ySrc := 0
yDst := 0
for xSrc := 0; xSrc < bounds.Dx(); xSrc++ {
xDst := xSrc+1
newImg.Set(xDst, yDst, img.At(xSrc, ySrc))
}
ySrc = bounds.Dy()-1
yDst = dstBounds.Dy()-1
for xSrc := 0; xSrc < bounds.Dx(); xSrc++ {
xDst := xSrc+1
newImg.Set(xDst, yDst, img.At(xSrc, ySrc))
}
// Corners
newImg.Set(dstBounds.Min.X, dstBounds.Min.Y, img.At(bounds.Min.X, bounds.Min.Y))
newImg.Set(dstBounds.Max.X-1, dstBounds.Min.Y, img.At(bounds.Max.X-1, bounds.Min.Y))
newImg.Set(dstBounds.Min.X, dstBounds.Max.Y-1, img.At(bounds.Min.X, bounds.Max.Y-1))
newImg.Set(dstBounds.Max.X-1, dstBounds.Max.Y-1, img.At(bounds.Max.X-1, bounds.Max.Y-1))
// Outer Columns
xSrc := 0
xDst := 0
for ySrc := 0; ySrc < bounds.Dy(); ySrc++ {
yDst := ySrc+1
newImg.Set(xDst, yDst, img.At(xSrc, ySrc))
}
xSrc = bounds.Dx()-1
xDst = dstBounds.Dx()-1
for ySrc := 0; ySrc < bounds.Dy(); ySrc++ {
yDst := ySrc+1
newImg.Set(xDst, yDst, img.At(xSrc, ySrc))
}
return newImg
}
type SerializedRect struct {
X,Y,W,H float64
}
type SerializedPos struct {
X,Y float64
}
type SerializedDim struct {
W,H float64
}
type SerializedFrame struct {
Frame SerializedRect
Rotated bool
Trimmed bool
SpriteSourceSize SerializedRect
SourceSize SerializedDim
Pivot SerializedPos
}
type SerializedSpritesheet struct {
ImageName string
Frames map[string]SerializedFrame
Meta map[string]interface{}
}