-
-
Save codeswimmer/3416013 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <AppKit/AppKit.h> | |
#import <objc/runtime.h> | |
#import <objc/message.h> | |
@interface NSBitmapImageRep (Mapping) | |
- (void)map:(void (^)(NSUInteger[4]))aBlk; | |
@end | |
@implementation NSBitmapImageRep (Mapping) | |
- (void)map:(void (^)(NSUInteger[4]))aBlk | |
{ | |
NSUInteger px[4]; | |
for(int y = 0; y < self.size.height; ++y) { | |
for(int x = 0; x < self.size.width; ++x) { | |
[self getPixel:px atX:x y:y]; | |
aBlk(px); | |
[self setPixel:px atX:x y:y]; | |
} | |
} | |
} | |
@end | |
int main(int argc, char *argv[]) { | |
NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init]; | |
NSImage *img = [[NSImage alloc] initWithContentsOfFile:@"/Users/fyolnish/Dropbox/Desktop/test.png"]; | |
NSBitmapImageRep *bm = [img representations][0]; | |
[bm map:^(NSUInteger *px) { | |
if(!(bm.bitmapFormat & NSAlphaNonpremultipliedBitmapFormat)) { | |
double factor = 255.0 / px[3]; | |
px[0] *= factor; | |
px[1] *= factor; | |
px[2] *= factor; | |
} | |
px[0] = px[1] = px[2] = px[3] = 255 - (0.299*px[0] + 0.587*px[1] + 0.114*px[2]); | |
}]; | |
[[bm TIFFRepresentation] writeToFile:[@"~/Desktop/test.tiff" stringByExpandingTildeInPath] atomically:YES]; | |
[p release]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment