开发者

How to figure out "Color of a specific pixel" in an image?

I have an image which is inside an Image View. I want to find the color of a specific pixel (say x=10开发者_如何学Go, y=20) inside the image. Working code would really help me out.


That depends very much on the type of image :)

If you have the byte data, and you know how it is arranged, for example PNG uses RGBA then you've got a leg up.

Liberal use of all the CGImageFunctions like CGImageGetBitsPerComponent and CGImageGetColorSpace will be your guide.

To get the actual byte data, CGImageDestinationCreateWithData Creates an image destination that writes to a Core Foundation mutable data object (NSMutableData* / CFMutableDataRef)

If all of this is gibberish, start with the Quart2D Programming Guide.


Here is very simple sceleton of class to work with image as bitmap. When you create this object with

ImageBitmap * imageBitmap = [[ImageBitmap alloc] initWithImage:myImageView.image  bitmapInfo:kCGImageAlphaNoneSkipLast];

You can access any pixel at (x, y) as

Byte * pixel = [imageBitmap pixelAtX:x Y:y];

RGB components at 0, 1, 2 byte so

Byte red = pixel[0]; etc.

you can both read or write pixels, for example set remove green component from pixel:

pixel[1] = 0;

if use kCGImageAlphaPremultipliedLast pixel[3] is alpha

//ImageBitmap.h
@interface ImageBitmap : NSObject {
  int height, width;
  void * contextData;
  CGContextRef context;
  CGBitmapInfo bitmapInfo;
}

@property(assign)       int height;
@property(assign)       int width;
@property(readonly)     CGContextRef    context;
@property(readonly)     void * contextData;

-(id)initWithSize:(CGSize)size bitmapInfo:(CGBitmapInfo)bmInfo;
-(id)initWithImage:(UIImage *)image  bitmapInfo:(CGBitmapInfo)bmInfo;
-(CGContextRef)createBitmapContextWithData:(void *)data;
- (UIImage *)imageFromContext;
-(Byte *)pixelAtX:(NSInteger)pixelX Y:(NSInteger)pixelY;

//ImageBitmap.m
#import "ImageBitmap.h"


@implementation ImageBitmap

@synthesize width, height;
@synthesize context, contextData;
-(id)initWithSize:(CGSize)size bitmapInfo:(CGBitmapInfo)bmInfo{
     if (self = [super init]) {
        height = size.height;
        width = size.width;
        contextData = malloc(width * height * 4);
        bitmapInfo = bmInfo;
        context = [self createBitmapContextWithData:contextData];
    }
    return self;
}

-(id)initWithImage:(UIImage *)image  bitmapInfo:(CGBitmapInfo)bmInfo{
    [self initWithSize:image.size bitmapInfo:bmInfo];
    CGContextDrawImage(context, CGRectMake(0, 0, width, height),image.CGImage);
    return self;
} 

-(CGContextRef)  createBitmapContextWithData:(void *)data{
    CGContextRef    ctx = NULL;
    int bitmapBytesPerRow   = (width * 4); 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    if (data == NULL){
        return NULL;
    }
    ctx = CGBitmapContextCreate (data,
                                    width,
                                    height,
                                    8,      // bits per component
                                    bitmapBytesPerRow,
                                    colorSpace,
                                    bitmapInfo); //kCGImageAlphaNoneSkipLast or kCGImageAlphaPremultipliedLast

    CGColorSpaceRelease( colorSpace );
    return ctx;
}

- (UIImage *)imageFromContext{
    CGImageRef cgImage = CGBitmapContextCreateImage(context);
    return [UIImage imageWithCGImage:cgImage];
}

-(Byte *)pixelAtX:(NSInteger)pixelX Y:(NSInteger)pixelY{
    return (Byte *)contextData + (width * pixelY + pixelX)*4;
}

@end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜