forked from jlamarche/Tile-Cutter
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTileOperation.m
199 lines (161 loc) · 5.63 KB
/
TileOperation.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
189
190
191
192
193
194
195
196
197
198
199
//
// TileOperation.m
// Tile Cutter
//
// Created by jeff on 10/30/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "TileOperation.h"
#import "NSImage-Tile.h"
#import "NSInvocation-MCUtilities.h"
#import "NSBitmapImageRep-Tile.h"
@interface NSBitmapImageRep (Extension)
- (BOOL) isAbsoluteTransparent;
@end
@implementation NSBitmapImageRep (Extension)
- (BOOL) isAbsoluteTransparent
{
NSSize s = [self size];
for (int i = 0; i < s.width; ++i)
{
for (int j = 0; j < s.height; ++j)
{
NSColor *col = [self colorAtX:i y:j];
if ([col alphaComponent] != 0.0f)
return NO;
}
}
return YES;
}
@end
@implementation TileOperation
@synthesize delegate, imageRep, row, baseFilename, tileHeight, tileWidth, outputFormat;
@synthesize tilesInfo;
@synthesize skipTransparentTiles;
@synthesize outputSuffix;
@synthesize rigidTiles, POTTiles;
#pragma mark -
- (void)informDelegateOfError:(NSString *)message
{
if ([delegate respondsToSelector:@selector(operation:didFailWithMessage:)])
{
NSInvocation *invocation = [NSInvocation invocationWithTarget:delegate
selector:@selector(operation:didFailWithMessage:)
retainArguments:YES, self, message];
[invocation invokeOnMainThreadWaitUntilDone:YES];
}
}
- (void)main
{
@try
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *extension = nil;
NSBitmapImageFileType fileType;
switch (outputFormat)
{
case TileCutterOutputPrefsJPEG:
extension = @"jpg";
fileType = NSJPEGFileType;
break;
case TileCutterOutputPrefsGIF:
extension = @"gif";
fileType = NSGIFFileType;
break;
case TileCutterOutputPrefsTIFF:
extension = @"tiff";
fileType = NSTIFFFileType;
break;
case TileCutterOutputPrefsBMP:
extension = @"bmp";
fileType = NSBMPFileType;
break;
case TileCutterOutputPrefsPNG:
extension = @"png";
fileType = NSPNGFileType;
break;
case TileCutterOutputPrefsJPEG2000:
extension = @"jpx";
fileType = NSJPEG2000FileType;
break;
default:
NSLog(@"Bad preference detected, assuming JPEG");
extension = @"jpg";
fileType = NSJPEGFileType;
break;
}
// Get Tile Count for this Operation
int tileColCount = [imageRep columnsWithTileWidth:tileWidth];
// Create tilesInfo Array for holding this Operation Tiles Info
self.tilesInfo = [NSMutableArray arrayWithCapacity: tileColCount];
// Safe Empty Suffix
if (!self.outputSuffix)
self.outputSuffix = @"";
for (int column = 0; column < tileColCount; column++)
{
NSImage *subImage = [imageRep subImageWithTileWidth:(float)tileWidth
tileHeight:(float)tileHeight
column:column
row:row
rigidSize:self.rigidTiles];
if (subImage == nil)
{
[self informDelegateOfError:NSLocalizedString(@"Error creating tile", @"")];
goto finish;
}
NSArray * representations = [subImage representations];
NSBitmapImageRep *subBitmapRep = nil;
if ( [representations count] )
subBitmapRep = [representations objectAtIndex: 0];
if ([self isCancelled])
goto finish;
// Analyze do we need this tile saved
BOOL curTileNeeded = ! (self.skipTransparentTiles && [subBitmapRep isAbsoluteTransparent]);
// save if yes
if ( curTileNeeded )
{
NSData *bitmapData = [NSBitmapImageRep representationOfImageRepsInArray:representations
usingType:fileType properties:nil];
if (bitmapData == nil)
{
[self informDelegateOfError:NSLocalizedString(@"Error retrieving bitmap data from result", @"")];
goto finish;
}
if ([self isCancelled])
goto finish;
NSString *outPath = [NSString stringWithFormat:@"%@_%d_%d%@.%@", baseFilename, row, column, self.outputSuffix, extension];
[bitmapData writeToFile:outPath atomically:YES];
// Add created Tile Info to tilesInfo array
NSRect tileRect = NSRectFromCGRect( CGRectMake(column * tileWidth,
row * tileHeight,
[subImage size].width,
[subImage size].height));
NSDictionary *tileInfoDict = [NSDictionary dictionaryWithObjectsAndKeys:
[outPath lastPathComponent], @"Name",
NSStringFromRect(tileRect), @"Rect",
nil];
[(NSMutableArray *)self.tilesInfo addObject: tileInfoDict ];
}
if ([delegate respondsToSelector:@selector(operationDidFinishTile:)])
[delegate operationDidFinishTile: self];
}
if ([delegate respondsToSelector:@selector(operationDidFinishSuccessfully:)])
[delegate operationDidFinishSuccessfully: self ];
finish:
[pool drain];
}
@catch (NSException * e)
{
NSLog(@"Exception: %@", e);
}
}
- (void)dealloc
{
self.outputSuffix = nil;
delegate = nil;
[imageRep release], imageRep = nil;
[baseFilename release], baseFilename = nil;
self.tilesInfo = nil;
[super dealloc];
}
@end