1 #import
2
3 int main(int argc, const char * argv[])
4 {
5 @autoreleasepool{
6 ?//handle incorrect arguments
7 if (argc < 2) {
8 NSLog(@"TileCutter arguments: inputfile");
9 return 0;
10 }
11
12 //input file
13 NSString *inputFile = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
14
15 //tile size
16 CGFloat tileSize = 256; //output path
17 NSString *outputPath = [inputFile stringByDeletingPathExtension];
18
19 //load image
20 NSImage *image = [[NSImage alloc] initWithContentsOfFile:inputFile];
21 NSSize size = [image size];
22 NSArray *representations = [image representations];
23 if ([representations count]){
24 NSBitmapImageRep *representation = representations[0];
25 size.width = [representation pixelsWide];
26 size.height = [representation pixelsHigh];
27 }
28 NSRect rect = NSMakeRect(0.0, 0.0, size.width, size.height);
29 CGImageRef imageRef = [image CGImageForProposedRect:&rect context:NULL hints:nil];
30
31 //calculate rows and columns
32 NSInteger rows = ceil(size.height / tileSize);
33 NSInteger cols = ceil(size.width / tileSize);
34
35 //generate tiles
36 for (int y = 0; y < rows; ++y) {
37 for (int x = 0; x < cols; ++x) {
38 //extract tile image
39 CGRect tileRect = CGRectMake(x*tileSize, y*tileSize, tileSize, tileSize);
40 CGImageRef tileImage = CGImageCreateWithImageInRect(imageRef, tileRect);
41
42 //convert to jpeg data
43 NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:tileImage];
44 NSData *data = [imageRep representationUsingType: NSJPEGFileType properties:nil];
45 CGImageRelease(tileImage);
46
47 //save file
48 NSString *path = [outputPath stringByAppendingFormat: @"_%02i_%02i.jpg", x, y];
49 [data writeToFile:path atomically:NO];
50 }
51 }
52 }
53 return 0;
54 }