Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 131 lines (111 sloc) 4.452 kB
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
1 //
2 // TileOperation.m
3 // Tile Cutter
4 //
5 // Created by jeff on 10/30/10.
6 // Copyright 2010 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "TileOperation.h"
10 #import "NSImage-Tile.h"
11 #import "NSInvocation-MCUtilities.h"
0774566 Tweaked the operation queue functionality
jlamarche authored
12 #import "NSBitmapImageRep-Tile.h"
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
13
14 @implementation TileOperation
b54e258 Added ability to specify the output file type in the preferences
jlamarche authored
15 @synthesize delegate, imageRep, row, baseFilename, tileHeight, tileWidth, outputFormat;
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
16 #pragma mark -
17 - (void)informDelegateOfError:(NSString *)message
18 {
19
20 if ([delegate respondsToSelector:@selector(operation:didFailWithMessage:)])
21 {
22 NSInvocation *invocation = [NSInvocation invocationWithTarget:delegate
23 selector:@selector(operation:didFailWithMessage:)
24 retainArguments:YES, self, message];
25 [invocation invokeOnMainThreadWaitUntilDone:YES];
26 }
27 }
28 - (void)main
29 {
30 @try
31 {
32 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
33
b54e258 Added ability to specify the output file type in the preferences
jlamarche authored
34 NSString *extension = nil;
35 NSBitmapImageFileType fileType;
36
37 switch (outputFormat)
38 {
39 case TileCutterOutputPrefsJPEG:
40 extension = @"jpg";
41 fileType = NSJPEGFileType;
42 break;
43 case TileCutterOutputPrefsGIF:
44 extension = @"gif";
45 fileType = NSGIFFileType;
46 break;
47 case TileCutterOutputPrefsTIFF:
48 extension = @"tiff";
49 fileType = NSTIFFFileType;
50 break;
51 case TileCutterOutputPrefsBMP:
52 extension = @"bmp";
53 fileType = NSBMPFileType;
54 break;
55 case TileCutterOutputPrefsPNG:
56 extension = @"png";
57 fileType = NSPNGFileType;
58 break;
59 case TileCutterOutputPrefsJPEG2000:
60 extension = @"jpx";
61 fileType = NSJPEG2000FileType;
62 break;
63 default:
64 NSLog(@"Bad preference detected, assuming JPEG");
65 extension = @"jpg";
66 fileType = NSJPEGFileType;
67 break;
68 }
69
3e3a7e7 @psineur Fixed: crashes when imageWidth is divisible by tileWidth without rema…
psineur authored
70 int tileColCount = [imageRep columnsWithTileWidth:tileWidth];
71 for (int column = 0; column < tileColCount; column++)
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
72 {
0774566 Tweaked the operation queue functionality
jlamarche authored
73 NSImage *subImage = [imageRep subImageWithTileWidth:(float)tileWidth tileHeight:(float)tileHeight column:column row:row];
74
75 if (subImage == nil)
76 {
77 [self informDelegateOfError:NSLocalizedString(@"Error creating tile", @"")];
78 goto finish;
79 }
80
81 NSArray * representations = [subImage representations];
82
83 if ([self isCancelled])
84 goto finish;
85
86 NSData *bitmapData = [NSBitmapImageRep representationOfImageRepsInArray:representations
b54e258 Added ability to specify the output file type in the preferences
jlamarche authored
87 usingType:fileType properties:nil];
0774566 Tweaked the operation queue functionality
jlamarche authored
88
89 if (bitmapData == nil)
90 {
91 [self informDelegateOfError:NSLocalizedString(@"Error retrieving bitmap data from result", @"")];
92 goto finish;
93 }
94
95
96 if ([self isCancelled])
97 goto finish;
98
b54e258 Added ability to specify the output file type in the preferences
jlamarche authored
99 NSString *outPath = [NSString stringWithFormat:@"%@_%d_%d.%@", baseFilename, row, column, extension];
0774566 Tweaked the operation queue functionality
jlamarche authored
100 [bitmapData writeToFile:outPath atomically:YES];
101
102 if ([delegate respondsToSelector:@selector(operationDidFinishTile:)])
103 [delegate performSelectorOnMainThread:@selector(operationDidFinishTile:)
104 withObject:self
105 waitUntilDone:NO];
106
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
107 }
b54e258 Added ability to specify the output file type in the preferences
jlamarche authored
108
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
109 if ([delegate respondsToSelector:@selector(operationDidFinishSuccessfully:)])
110 [delegate performSelectorOnMainThread:@selector(operationDidFinishSuccessfully:)
111 withObject:self
112 waitUntilDone:NO];
113 finish:
114 [pool drain];
115 }
116 @catch (NSException * e)
117 {
118 NSLog(@"Exception: %@", e);
119 }
120 }
121
122 - (void)dealloc
123 {
124 delegate = nil;
0774566 Tweaked the operation queue functionality
jlamarche authored
125 [imageRep release], imageRep = nil;
1c5e66f Moved tile cutting into an NSOperation to leverage all computing core…
jlamarche authored
126 [baseFilename release], baseFilename = nil;
127
128 [super dealloc];
129 }
130 @end
Something went wrong with that request. Please try again.