forked from cocos2d/cocos2d-objc
/
tiler.mm
277 lines (209 loc) · 5.94 KB
/
tiler.mm
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
/*
Tiler is a small command line utility that tiles an arbitrary image
into a PNG texture containing tiles and a TGA image containing a tilemap.
You can specify the tile size (must be a power of 2) with the -tilesize option.
To Compile:
gcc tiler.mm -o tiler -framework Cocoa -lstdc++
*/
#import <Cocoa/Cocoa.h>
void saveTilemap( unsigned char *tilemap, int tx, int ty, const char *output )
{
int x, y, index;
typedef struct t_tgaHeader {
unsigned char pad0;
unsigned char pad1;
unsigned char type;
unsigned char pad2[9];
short width;
short height;
unsigned char bits;
unsigned char pad3;
} tgaHeader;
tgaHeader header;
memset( &header, 0, sizeof( tgaHeader ) );
header.type = 2;
header.width = tx;
header.height = ty;
header.bits = 24;
unsigned long len = sizeof(tgaHeader) + (tx*ty*3);
unsigned char *buffer = (unsigned char *)malloc(len);
unsigned char *p = buffer;
memset(p, 0, len);
memcpy(p, &header, sizeof(tgaHeader));
p += sizeof(tgaHeader);
index = 0;
for( y = (ty - 1); y >= 0; y--)
{
for( x = 0; x < tx; x++ )
{
int i = (y*tx)+x;
p[(i*3)+2] = tilemap[index];
p[(i*3)] = tilemap[index] ? 0xff : 0;
index++;
}
}
NSData *data = [NSData dataWithBytesNoCopy:buffer length:len];
[data writeToFile:[NSString stringWithFormat:@"%s.tga", output] atomically:NO];
}
void saveTexture( NSMutableArray *tiles, const char *output, int tilesize )
{
int i, x, y;
// Write the texture
int stride = ((int)ceil(sqrt([tiles count]))) * tilesize;
int texsize = 1;
while( texsize < stride )
texsize <<= 1;
int itemsPerRow = texsize / tilesize;
NSImage *texture = [[NSImage alloc] initWithSize:NSMakeSize(texsize, texsize)];
[texture lockFocus];
NSRect imageRect = NSMakeRect(0, 0, texsize, texsize);
NSEraseRect( imageRect );
for( i = 0; i < [tiles count]; i++ )
{
x = (i % itemsPerRow) * tilesize;
y = texsize - ((i / itemsPerRow) * tilesize) - tilesize;
NSRect subRect = NSMakeRect(x, y, tilesize, tilesize);
[texture drawRepresentation:[tiles objectAtIndex:i] inRect:subRect];
}
[texture unlockFocus];
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[texture TIFFRepresentation]];
NSData *data = [rep representationUsingType:NSPNGFileType properties:NULL];
[data writeToFile:[NSString stringWithFormat:@"%s.png", output] atomically:NO];
}
void processFile( const char *input, const char *output, int tilesize )
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSImage *in = [[NSImage alloc] initWithContentsOfFile:[NSString stringWithUTF8String:input]];
if ( in == NULL )
{
printf( "Could not open image %s\n", input );
return;
}
NSSize inSize = [in size];
if ( ( (int)inSize.width % tilesize ) != 0 )
inSize.width += ( (int)inSize.width % tilesize );
if ( ( (int)inSize.height % tilesize ) != 0 )
inSize.height += ( (int)inSize.height % tilesize );
[in setSize:inSize];
int ty = ((int)inSize.height / tilesize);
int tx = ((int)inSize.width / tilesize);
NSMutableArray *tiles = [[NSMutableArray alloc] init];
unsigned char *tilemap = (unsigned char *)malloc(tx*ty);
memset( tilemap, 0, tx*ty );
NSImage *empty = [[NSImage alloc] initWithSize:NSMakeSize(tilesize, tilesize)];
[empty lockFocus];
[[NSColor clearColor] set];
NSRectFill(NSMakeRect(0, 0, tilesize, tilesize));
[empty unlockFocus];
NSBitmapImageRep *er = [NSBitmapImageRep imageRepWithData:[empty TIFFRepresentation]];
[tiles addObject:er];
NSBitmapImageRep *ir = [NSBitmapImageRep imageRepWithData:[in TIFFRepresentation]];
CGImageRef inputRef = [ir CGImage];
int y, x, i, j, k = 0;
for( y = 0; y < ty; y++ )
{
for( x = 0; x < tx; x++ )
{
int tileIndex = -1;
CGRect tileRect = CGRectMake(x*tilesize, y*tilesize, tilesize, tilesize);
CGImageRef tileRef = CGImageCreateWithImageInRect(inputRef, tileRect);
NSBitmapImageRep *tile = [[NSBitmapImageRep alloc] initWithCGImage:tileRef];
// see if it's opaque at all
BOOL opaque = NO;
for( j = 0; j < (tilesize * tilesize); j++ )
{
NSColor *color = [tile colorAtX:(j % tilesize) y:(j / tilesize)];
if ( [color alphaComponent] != 0 )
{
opaque = YES;
break;
}
}
if ( opaque )
{
for( i = 1; i < [tiles count]; i++ )
{
NSBitmapImageRep *t = [tiles objectAtIndex:i];
if ( [[t TIFFRepresentation] isEqualToData:[tile TIFFRepresentation]] )
{
tileIndex = i;
break;
}
}
}
else
tileIndex = 0;
if ( tileIndex < 0 )
{
tileIndex = [tiles count];
[tiles addObject:[tile retain]];
}
tilemap[k++] = tileIndex;
}
}
[in unlockFocus];
if ( [tiles count] >= 255 )
{
printf( "Could not reduce image to less than 255 tiles, please try a different tile size\n" );
}
else
{
saveTexture( tiles, output, tilesize );
saveTilemap( tilemap, tx, ty, output );
}
free( tilemap );
[pool release];
}
static int Usage( const char *name )
{
printf( "Tiler v1.0\n" );
printf( "Usage: %s [-tilesize {size}] input output\n", name);
printf( "Default tile size if not specified is 16 pixels\n");
printf( "The tile size must be a power of 2 value\n");
return 0;
}
int main (int argc, const char * argv[])
{
NSApplicationLoad();
int tilesize = 16, i;
const char *input = nil;
const char *output = nil;
if ( argc < 3 )
return Usage( argv[0] );
for( i = 1; i < argc; i++ )
{
if ( strcmp( argv[i], "-tilesize" ) == 0 )
{
i++;
if ( i < argc )
{
tilesize = atoi( argv[i] );
}
}
else if ( input == nil )
{
input = argv[i];
}
else
{
output = argv[i];
break;
}
}
if ( input == nil || output == nil )
return Usage( argv[0] );
// make sure the tile size is a power of 2 value
if ( tilesize == 0 )
return Usage( argv[0] );
int numones = 0, ts = tilesize;
while( ts )
{
if ( ts & 1 )
numones++;
ts >>= 1;
}
if ( numones > 1 )
return Usage( argv[0] );
processFile( input, output, tilesize );
return 0;
}