Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit fec8f94951aa98192d0daa4c7fb292b3ed177b52 jlamarche committed Oct 8, 2010
@@ -0,0 +1,2 @@
+/* Localized versions of Info.plist keys */
+

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,16 @@
+//
+// NSImage-Tile.h
+// Crimson
+//
+// Created by jeff on 10/7/10.
+// Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+
+@interface NSImage(Tile)
+-(NSImage *)subImageWithTileWidth:(CGFloat)tileWidth tileHeight:(CGFloat)tileHeight column:(NSUInteger)column row:(NSUInteger)row;
+-(NSUInteger)columnsWithTileWidth:(CGFloat)tileWidth;
+-(NSUInteger)rowsWithTileHeight:(CGFloat)tileHeight;
+@end
@@ -0,0 +1,100 @@
+//
+// NSImage-Tile.m
+// Crimson
+//
+// Created by jeff on 10/7/10.
+// Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import "NSImage-Tile.h"
+
+
+@implementation NSImage(Tile)
+-(NSImage *)subImageWithTileWidth:(CGFloat)tileWidth tileHeight:(CGFloat)tileHeight column:(NSUInteger)column row:(NSUInteger)row
+{
+ if (column >= [self columnsWithTileWidth:tileWidth])
+ return nil;
+ if (row >= [self rowsWithTileHeight:tileHeight])
+ return nil;
+
+ NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithCGImage:[self CGImageForProposedRect:NULL context:NULL hints:nil]];
+ int width = [imageRep pixelsWide];
+ int height = [imageRep pixelsHigh];
+ int bpp = [imageRep bitsPerPixel] / 8;
+
+
+
+ int theRow = row * tileHeight;
+ int theCol = column * tileWidth;
+
+ int i,x,y;
+ unsigned char *p1, *p2;
+
+ int lastCol;
+ int outputWidth;
+ if (theCol + tileWidth > width)
+ {
+ lastCol = width;
+ outputWidth = width - theCol;
+ }
+ else
+ {
+ lastCol = theCol + tileWidth;
+ outputWidth = tileWidth;
+ }
+
+ int lastRow, outputHeight;
+ if (theRow + tileHeight > height)
+ {
+ lastRow = height;
+ outputHeight = height - theRow;
+ }
+ else
+ {
+ lastRow = theRow + tileHeight;
+ outputHeight = tileHeight;
+ }
+
+ NSImage *ret = [[NSImage alloc] initWithSize:NSMakeSize(outputWidth,outputHeight)];
+
+ NSMutableData *retData = [NSMutableData dataWithLength:bpp * tileWidth * tileHeight];
+
+ unsigned char *srcData = [imageRep bitmapData];
+ unsigned char *destData = [retData mutableBytes];
+
+
+ for (x = theCol; x < lastCol; x++)
+ {
+ for (y = theRow; y < lastRow; y++)
+ {
+ p1 = srcData + bpp * (y * width + x);
+ p2 = destData + bpp * ((y - theRow) * width + (x - theCol));
+ for (i = 0; i < bpp; i++)
+ p2[i] = p1[i];
+ }
+ }
+ NSBitmapImageRep *rep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:&destData
+ pixelsWide:tileWidth
+ pixelsHigh:tileHeight
+ bitsPerSample:[imageRep bitsPerSample]
+ samplesPerPixel:[imageRep samplesPerPixel]
+ hasAlpha:[imageRep hasAlpha]
+ isPlanar:[imageRep isPlanar]
+ colorSpaceName:[imageRep colorSpaceName]
+ bitmapFormat:[imageRep bitmapFormat]
+ bytesPerRow:[imageRep bytesPerRow]
+ bitsPerPixel:[imageRep bitsPerPixel]] autorelease];
+
+ [ret addRepresentation:rep];
+ return [ret autorelease];
+
+}
+-(NSUInteger)columnsWithTileWidth:(CGFloat)tileWidth
+{
+ return [self size].width / tileWidth;
+}
+-(NSUInteger)rowsWithTileHeight:(CGFloat)tileHeight
+{
+ return [self size].height / tileHeight;
+}
+@end
@@ -0,0 +1,17 @@
+//
+// NSInvocation-MCUtilities.h
+// Visioneer
+//
+// Created by jeff on 7/26/10.
+// Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface NSInvocation(MCUtilities)
+-(void)invokeOnMainThreadWaitUntilDone:(BOOL)wait;
++(NSInvocation*)invocationWithTarget:(id)target
+ selector:(SEL)aSelector
+ retainArguments:(BOOL)retainArguments, ...;
+@end
@@ -0,0 +1,47 @@
+//
+// NSInvocation-MCUtilities.m
+// Visioneer
+//
+// Created by jeff on 7/26/10.
+// Copyright 2010 __MyCompanyName__. All rights reserved.
+//
+
+#import "NSInvocation-MCUtilities.h"
+
+
+@implementation NSInvocation(MCUtilities)
+-(void)invokeOnMainThreadWaitUntilDone:(BOOL)wait
+{
+ [self performSelectorOnMainThread:@selector(invoke)
+ withObject:nil
+ waitUntilDone:wait];
+}
++(NSInvocation*)invocationWithTarget:(id)target
+ selector:(SEL)aSelector
+ retainArguments:(BOOL)retainArguments, ...
+{
+ va_list ap;
+ va_start(ap, retainArguments);
+ char* args = (char*)ap;
+ NSMethodSignature* signature = [target methodSignatureForSelector:aSelector];
+ NSInvocation* invocation = [NSInvocation invocationWithMethodSignature:signature];
+ if (retainArguments) {
+ [invocation retainArguments];
+ }
+ [invocation setTarget:target];
+ [invocation setSelector:aSelector];
+ for (int index = 2; index < [signature numberOfArguments]; index++) {
+ const char *type = [signature getArgumentTypeAtIndex:index];
+ NSUInteger size, align;
+ NSGetSizeAndAlignment(type, &size, &align);
+ NSUInteger mod = (NSUInteger)args % align;
+ if (mod != 0) {
+ args += (align - mod);
+ }
+ [invocation setArgument:args atIndex:index];
+ args += size;
+ }
+ va_end(ap);
+ return invocation;
+}
+@end
Oops, something went wrong.

0 comments on commit fec8f94

Please sign in to comment.