绘制具有相同颜色背景的图像时的色移

时间:2013-03-20 22:03:26

标签: xcode macos rgb nsbitmapimagerep

我的预期是输出将是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;
}

0 个答案:

没有答案