forked from jlamarche/Tile-Cutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTileOperation.m
129 lines (110 loc) · 4.31 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
//
// 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"
@implementation TileOperation
@synthesize delegate, imageRep, row, baseFilename, tileHeight, tileWidth, outputFormat;
#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;
}
for (int column = 0; column < [imageRep pixelsWide] / tileWidth + 1; column++)
{
NSImage *subImage = [imageRep subImageWithTileWidth:(float)tileWidth tileHeight:(float)tileHeight column:column row:row];
if (subImage == nil)
{
[self informDelegateOfError:NSLocalizedString(@"Error creating tile", @"")];
goto finish;
}
NSArray * representations = [subImage representations];
if ([self isCancelled])
goto finish;
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, extension];
[bitmapData writeToFile:outPath atomically:YES];
if ([delegate respondsToSelector:@selector(operationDidFinishTile:)])
[delegate performSelectorOnMainThread:@selector(operationDidFinishTile:)
withObject:self
waitUntilDone:NO];
}
if ([delegate respondsToSelector:@selector(operationDidFinishSuccessfully:)])
[delegate performSelectorOnMainThread:@selector(operationDidFinishSuccessfully:)
withObject:self
waitUntilDone:NO];
finish:
[pool drain];
}
@catch (NSException * e)
{
NSLog(@"Exception: %@", e);
}
}
- (void)dealloc
{
delegate = nil;
[imageRep release], imageRep = nil;
[baseFilename release], baseFilename = nil;
[super dealloc];
}
@end