-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathPixelPacker.java
More file actions
328 lines (282 loc) · 10.5 KB
/
PixelPacker.java
File metadata and controls
328 lines (282 loc) · 10.5 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
/*
* Copyright 2012 Samuel Taylor
*
* This file is part of darkFunction Editor
*
* darkFunction Editor is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* darkFunction Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with darkFunction Editor. If not, see <http://www.gnu.org/licenses/>.
*/
package dfEditor;
import java.awt.Rectangle;
//import java.util.ArrayList;
import java.util.Enumeration;
import java.awt.image.BufferedImage;
/**
*
* @author s4m20
*/
public class PixelPacker
{
private class PixelRectPair
{
public Rectangle rect;
public Rectangle padRect;
public int[] pixels;
public PixelRectPair(final int[] aPixels, final Rectangle aRect, int padding)
{
rect = aRect;
pixels = aPixels;
padRect = new Rectangle(rect);
padRect.x -= padding;
padRect.y -= padding;
padRect.width += padding * 2;
padRect.height += padding * 2;
}
}
private Rectangle applySpacing(final Rectangle aRect, final int aSpacing)
{
return new Rectangle(aRect.x - aSpacing, aRect.y - aSpacing, aRect.width + aSpacing, aRect.height + aSpacing);
}
public boolean packRects(final Rectangle aMasterRect, final Rectangle[] aSubRects, final int aSpacing)
{
boolean bSuccess = true;
Node rootNode = new Node();
rootNode.rect = aMasterRect;
for (int i=0; i<aSubRects.length; ++i)
{
Node node = rootNode.Insert(applySpacing(aSubRects[i], aSpacing));
if (node != null)
copyRect(applySpacing(node.rect, -aSpacing), aSubRects[i]); // don't want to swap pointers
else
bSuccess = false;
}
return bSuccess;
}
public BufferedImage packPixels(final BufferedImage aOrigImage, final Rectangle[] aRects, boolean bPowerOfTwo, int padding)
{
int[] origPixels = new int[aOrigImage.getWidth(null) * aOrigImage.getHeight(null)];
aOrigImage.getRGB( 0,
0,
aOrigImage.getWidth(),
aOrigImage.getHeight(),
origPixels,
0,
aOrigImage.getWidth());
//Dictionary<Rectangle, int[]> dict = new Hashtable<Rectangle, int[]>();
PixelRectPair[] pairs = new PixelRectPair[aRects.length];
// first create a seperate array of pixels for every sprite
for (int i=0; i<aRects.length; ++i)
{
Rectangle r = aRects[i];
int[] pixels = new int[r.width * r.height];
for (int y=0; y<r.height; ++y)
{
for (int x=0; x<r.width; ++x)
{
pixels[y * r.width + x] = origPixels[(y + r.y) * aOrigImage.getWidth() + (r.x + x)];
}
}
pairs[i] = new PixelRectPair(pixels, r, padding);
}
// TODO: sprites totally enclosed by other sprites should remain enclosed
// TODO: multiple images?
// sort by size
for (int i=0; i<pairs.length-1; ++i)
{
double area1 = pairs[i].rect.width * pairs[i].rect.height;
double area2 = pairs[i+1].rect.width * pairs[i+1].rect.height;
if (area1 < area2)
{
PixelRectPair temp = pairs[i];
pairs[i] = pairs[i+1];
pairs[i+1] = temp;
}
}
// do the packing here
// madly inefficient but doesn't seem to matter a flying fuck
// some seriously shoddy coding here actually.. if neccessary will come
// back to it. Bit drunk
boolean bFailed = true;
int sizeX = 1;
int sizeY = sizeX;
while (true)
{
bFailed = false;
Node rootNode = new Node();
rootNode.rect = new Rectangle(0, 0, sizeX, sizeY);
for (int i=0; i<pairs.length; ++i)
{
Node node = rootNode.Insert(pairs[i].padRect);
if (node == null)
{
// failed to insert
bFailed = true;
if (sizeX > sizeY)
{
if (bPowerOfTwo)
sizeY *= 2;
else
sizeY += 1;
}
else
{
if (bPowerOfTwo)
sizeX *= 2;
else
sizeX += 1;
}
break;
}
}
if (!bFailed)
break;
}
Node rootNode = new Node();
rootNode.rect = new Rectangle(0, 0, sizeX, sizeY);
for (int i=0; i<pairs.length; ++i)
{
Node node = rootNode.Insert(pairs[i].padRect);
copyRect(node.rect, pairs[i].padRect); // don't want to swap pointers
}
// create the new image!
// first work out the min size
// constrain as tight as poss
{
sizeX = sizeY = 0;
for (int i=0; i<pairs.length; ++i)
{
Rectangle r = pairs[i].padRect;
if (sizeX < r.x + r.width)
sizeX = r.x + r.width;
if (sizeY < r.y + r.height)
sizeY = r.y + r.height;
}
}
// shouldn't need to do this with power of two because the algo is
// supposed to give us correct dimensions, sometimes it is wrong though,
// so re-constrain it according to min size above
if (bPowerOfTwo)
{
int x = 1;
while(x < sizeX)
x *= 2;
sizeX = x;
int y = 1;
while(y < sizeY)
y *= 2;
sizeY = y;
}
if (sizeX * sizeY == 0)
return null;
// create image
int[] newImagePixels = new int[sizeX * sizeY];
// copy rects to new image
for (int i=0; i<pairs.length; ++i)
{
Rectangle pr = pairs[i].padRect;
Rectangle r = pairs[i].rect;
r.x = pr.x + padding;
r.y = pr.y + padding;
int[] pixels = pairs[i].pixels;
for (int y=0; y<r.height; ++y)
{
for (int x=0; x<r.width; ++x)
{
newImagePixels[(pr.y+y+padding) * sizeX + (pr.x+x+padding)] = pixels[y * r.width + x];
}
}
}
BufferedImage newImage = new BufferedImage(sizeX, sizeY, aOrigImage.getType());
newImage.setRGB(0, 0, sizeX, sizeY, newImagePixels, 0, sizeX);
return newImage;
}
// don't want to change the pointer
private void copyRect(final Rectangle aFrom, final Rectangle aTo)
{
aTo.x = aFrom.x;
aTo.y = aFrom.y;
aTo.width = aFrom.width;
aTo.height = aFrom.height;
}
private class Node
{
public Node[] children;
public Rectangle rect;
boolean bTaken;
Node()
{
children = null;
bTaken = false;
}
private Node Insert(final Rectangle aRect)
{
if (children != null) // not a leaf
{
// try inserting into first child
Node node = children[0].Insert(aRect);
if (node != null)
return node;
// no room, insert into second
return children[1].Insert(aRect);
}
else
{
if (bTaken)
return null;
if ( aRect.width > this.rect.width
|| aRect.height > this.rect.height)
{
// node is too small, rect can't fit inside
return null;
}
if ( aRect.width == this.rect.width
&& aRect.height == this.rect.height)
{
// fits perfectly
this.bTaken = true;
return this;
}
// otherwise split node and create kids
children = new Node[2];
children[0] = new Node();
children[1] = new Node();
// decide which way to split
int dw = this.rect.width - aRect.width;
int dh = this.rect.height - aRect.height;
if (dw > dh)
{
children[0].rect = new Rectangle( this.rect.x,
this.rect.y,
aRect.width,
this.rect.height);
children[1].rect = new Rectangle( this.rect.x + aRect.width,
this.rect.y,
this.rect.width - aRect.width,
this.rect.height);
}
else
{
children[0].rect = new Rectangle( this.rect.x,
this.rect.y,
this.rect.width,
aRect.height);
children[1].rect = new Rectangle( this.rect.x,
this.rect.y + aRect.height,
this.rect.width,
this.rect.height - aRect.height);
}
// insert into first child we created
return children[0].Insert(aRect);
}
}
}
}