2017-02-23 7 views
0

このコードはありますが、明るさではなく各ピクセルのRGBカラー値を返すようにします。現時点では、すべてのピクセルの明るさをコマンドラインに出力します。代わりに、これを変更して各ピクセルのRGB値を出力します。私はかなりObjective-Cに新しいので、いくつかの助けと説明は非常に高く評価されるだろう:)。結果を変更してObjective CのRGBピクセル値を出力する方法

An example of what I have so far. This image shows the brightness of each pixel

// 1. Get pixels of image 
    CGImageRef inputCGImage = [image CGImage]; 
    NSUInteger width = CGImageGetWidth(inputCGImage); 
    NSUInteger height = CGImageGetHeight(inputCGImage); 

    NSUInteger bytesPerPixel = 4; 
    NSUInteger bytesPerRow = bytesPerPixel * width; 
    NSUInteger bitsPerComponent = 8; 

    UInt32 * pixels; 
    pixels = (UInt32 *) calloc(height * width, sizeof(UInt32)); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 
               bitsPerComponent, bytesPerRow, colorSpace, 
               kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big); 

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), inputCGImage); 

    CGColorSpaceRelease(colorSpace); 
    CGContextRelease(context); 

#define Mask8(x) ((x) & 0xFF) 
#define R(x) (Mask8(x)) 
#define G(x) (Mask8(x >> 8)) 
#define B(x) (Mask8(x >> 16)) 

    // 2. Iterate and log! 
    NSLog(@"Brightness of image:"); 
    UInt32 * currentPixel = pixels; 
    for (NSUInteger j = 0; j < height; j++) { 
    for (NSUInteger i = 0; i < width; i++) { 
     UInt32 color = *currentPixel; 

     printf("%3.0f ", (R(color)+G(color)+B(color))/3.0); 
     currentPixel++; 
    } 
    printf("\n"); 
    } 

    free(pixels); 

答えて

0

ただ、この行を変更:

printf("(r=%3.0f, g=%3.0f, b=%3.0f) ", R(color), G(color), B(color)); 
+0

printf("%3.0f ", (R(color)+G(color)+B(color))/3.0); 

をありがとうございました!! –

関連する問題