Skip to content

Instantly share code, notes, and snippets.

@kybernetyk
Created March 16, 2013 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kybernetyk/5176888 to your computer and use it in GitHub Desktop.
Save kybernetyk/5176888 to your computer and use it in GitHub Desktop.
NSBitmapImageRep* imageRep = [[NSBitmapImageRep alloc] initWithData:[img TIFFRepresentation]];
int x = [imageRep size].width - 300;
int y = 170;
float avg = 0.0;
float totalcnt = 0;
for (int _y = y; _y < y + 120; _y++) {
for (int _x = x; _x < x + 270; _x++) {
NSColor* color = [imageRep colorAtX: (_x) y: ([imageRep size].height - _y)];
NSColor *rgbColor = [color colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
if (!rgbColor)
continue;
float avgGray = ([rgbColor redComponent] + [rgbColor greenComponent] + [rgbColor blueComponent]) / 3.0;
avg += avgGray;
totalcnt += 1.0;
[imageRep setColor: [NSColor redColor] atX: _x y: ([imageRep size].height - _y)];
}
}
// [[imageRep TIFFRepresentation] writeToFile: [tagPath stringByAppendingPathComponent: @"ficken.tiff"] atomically: NO];
float avgGray = avg/totalcnt;
NSColor *color = (avgGray >= 0.65) ? [NSColor blackColor] : [NSColor whiteColor];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment