forked from jlamarche/Tile-Cutter
-
Notifications
You must be signed in to change notification settings - Fork 4
/
TileCutterCore.m
188 lines (150 loc) · 5.2 KB
/
TileCutterCore.m
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
//
// TileCutterCore.m
// Tile Cutter
//
// Created by Stepan Generalov on 28.04.11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "TileCutterCore.h"
#import "NSImage-Tile.h"
@implementation TileCutterCore
@synthesize keepAllTiles, tileWidth, tileHeight, inputFilename,
outputBaseFilename, outputSuffix, operationsDelegate,
queue, allTilesInfo, imageInfo, outputFormat;
@synthesize rigidTiles;
@synthesize contentScaleFactor;
@synthesize POTTiles;
#pragma mark Public Methods
- (id) init
{
if ( (self == [super init]) )
{
self.queue = [[[NSOperationQueue alloc] init] autorelease];
[self.queue setMaxConcurrentOperationCount:1];
self.outputFormat = NSPNGFileType;
self.outputSuffix = @"";
self.keepAllTiles = NO;
self.rigidTiles = NO;
}
return self;
}
- (void) dealloc
{
self.queue = nil;
self.allTilesInfo = nil;
self.imageInfo = nil;
self.inputFilename = nil;
self.outputBaseFilename = nil;
self.outputSuffix = nil;
[super dealloc];
}
- (void) startSavingTiles
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSImage *image = [[[NSImage alloc] initWithContentsOfFile: self.inputFilename] autorelease];
progressCol = 0;
progressRow = 0;
tileRowCount = [image rowsWithTileHeight: self.tileHeight];
tileColCount = [image columnsWithTileWidth: self.tileWidth];
NSSize outputImageSizeForPlist = [image size];
outputImageSizeForPlist.width /= self.contentScaleFactor;
outputImageSizeForPlist.height /= self.contentScaleFactor;
self.imageInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[self.inputFilename lastPathComponent], @"Filename",
NSStringFromSize(outputImageSizeForPlist), @"Size", nil];
self.allTilesInfo = [NSMutableArray arrayWithCapacity: tileRowCount * tileColCount];
// One ImageRep for all TileOperation
NSBitmapImageRep *imageRep =
[[[NSBitmapImageRep alloc] initWithCGImage:[image CGImageForProposedRect:NULL context:NULL hints:nil]] autorelease];
for (int row = 0; row < tileRowCount; row++)
{
TileOperation *op = [[TileOperation alloc] init];
op.row = row;
op.tileWidth = self.tileWidth;
op.tileHeight = self.tileHeight;
op.imageRep = imageRep;
op.baseFilename = outputBaseFilename;
op.delegate = self;
op.outputFormat = self.outputFormat;
op.outputSuffix = self.outputSuffix;
op.skipTransparentTiles = (! self.keepAllTiles );
op.rigidTiles = self.rigidTiles;
op.POTTiles = self.POTTiles;
[queue addOperation:op];
[op release];
}
[pool drain];
}
- (void)operationDidFinishTile:(TileOperation *)op
{
progressCol++;
if (progressCol >= tileColCount)
{
progressCol = 0;
progressRow++;
}
if ([self.operationsDelegate respondsToSelector: _cmd])
[self.operationsDelegate performSelectorOnMainThread: _cmd
withObject: op
waitUntilDone: NO];
}
- (void) saveImageInfoDictionary
{
// Change coordinates & size of all tiles for contentScaleFactor
if (self.contentScaleFactor != 1.0f)
{
// Create new array, that will replace old self.allTilesInfo
NSMutableArray *newTilesInfoArray = [NSMutableArray arrayWithCapacity: [self.allTilesInfo count]];
for (NSDictionary *tileDict in self.allTilesInfo)
{
// Get Tile Rect
NSRect rect = NSRectFromString([tileDict objectForKey: @"Rect"]);
// Divide it by contentScaleFactor
rect.origin.x /= self.contentScaleFactor;
rect.origin.y /= self.contentScaleFactor;
rect.size.width /= self.contentScaleFactor;
rect.size.height /= self.contentScaleFactor;
// Create new tile info Dict with changed rect
NSDictionary *newTileDict = [NSDictionary dictionaryWithObjectsAndKeys:
[tileDict objectForKey:@"Name"], @"Name",
NSStringFromRect(rect), @"Rect",
nil];
// Add new tile info for new array
[newTilesInfoArray addObject:newTileDict ];
}
// Replace Old Tiles with New
self.allTilesInfo = newTilesInfoArray;
}
// Create Root Dictionary for a PLIST file
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
self.imageInfo, @"Source",
self.allTilesInfo, @"Tiles",
[NSNumber numberWithFloat:self.contentScaleFactor], @"ContentScaleFactor", nil];
// Be safe with outputSuffix
if (!self.outputSuffix)
self.outputSuffix = @"";
// Save Dict to File
[dict writeToFile:[NSString stringWithFormat:@"%@%@.plist", self.outputBaseFilename, self.outputSuffix] atomically:YES];
}
- (void)operationDidFinishSuccessfully:(TileOperation *)op
{
[(NSMutableArray *)self.allTilesInfo addObjectsFromArray: op.tilesInfo];
op.tilesInfo = nil;
// All Tiles Finished?
if (progressRow >= tileRowCount)
{
[self saveImageInfoDictionary];
}
if ([self.operationsDelegate respondsToSelector: _cmd])
[self.operationsDelegate performSelectorOnMainThread: _cmd
withObject: op
waitUntilDone: NO];
}
- (void)operation:(TileOperation *)op didFailWithMessage:(NSString *)message
{
if ([self.operationsDelegate respondsToSelector: _cmd])
[self.operationsDelegate performSelectorOnMainThread: _cmd
withObject: op
waitUntilDone: NO];
}
@end