私が期待したのは、出力が画像の同じ背景色(赤ではない)の1440x900背景のこのhttp://i45.tinypic.com/344ehb5.png画像になることでした。これは起こりますが、わずかなシフトがあります。背景色を本来よりも明るくしたり暗くしたりする色。それは色空間と関係がありますが、どうやってそれを扱うのか理解できません。
NSURL *myImage;
NSString *tempDir;
tempDir = @"/Users/Maxime/Documents/";
myImage = [NSURL fileURLWithPath: [tempDir stringByAppendingString:@"344ehb5.png"]];
NSLog(@"soundFile: %@", myImage);
NSImage *image = [[NSImage alloc] initWithContentsOfURL:myImage];
NSArray *upper = [self getRGBAsFromImage:image atX:5 andY:5 count:1];
NSColor *color = [upper lastObject];
NSRect imgRect = NSMakeRect(0.0, 0.0, 1400.0, 900.0);
NSSize imgSize = imgRect.size;
NSBitmapImageRep *offscreenRep = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:imgSize.width
pixelsHigh:imgSize.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:NSAlphaFirstBitmapFormat
bytesPerRow:0
bitsPerPixel:0] autorelease];
// set offscreen context
NSGraphicsContext *g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
//Set color of drawing, and fill the rectangle, so we can see it's boundaries.
[color setFill];
NSRectFill(imgRect);
//add image
NSRect shot = NSMakeRect(500.0, 300.0, 400.0, 300.0);
[image drawInRect:shot fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[NSGraphicsContext restoreGraphicsState];
// create an NSImage and add the rep to it
NSImage *img = [[[NSImage alloc] initWithSize:imgSize] autorelease];
[img addRepresentation:offscreenRep];
NSData *imagef = [[img TIFFRepresentation] retain];
[imagef writeToFile: @"/Users/Maxime/Desktop/test.png" atomically: NO];
私はこのコードを使用してピクセルデータを取得しています:
- (NSArray*)getRGBAsFromImage:(NSImage*)image atX:(int)xx andY:(int)yy count:(int)count
{
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
// create the image somehow, load from file, draw into it...
CGImageSourceRef source;
source = CGImageSourceCreateWithData((CFDataRef)[image TIFFRepresentation], NULL);
// First get the image into your data buffer
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
for (int ii = 0 ; ii < count ; ++ii)
{
CGFloat red = (rawData[byteIndex] * 1.0) / 255.0;
CGFloat green = (rawData[byteIndex + 1] * 1.0) / 255.0;
CGFloat blue = (rawData[byteIndex + 2] * 1.0) / 255.0;
CGFloat alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
byteIndex += 4;
NSColor *acolor = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:alpha];
[result addObject:acolor];
}
free(rawData);
return result;
}