2017-02-23 12 views
0

У меня есть этот код, и я хочу, чтобы он возвращал значение цвета RGB для каждого пикселя, а не яркость. В настоящий момент он печатает яркость каждого пикселя в командной строке, и я хочу, чтобы это изменило его, чтобы вместо этого отобразить значение RGB для каждого пикселя. Im довольно новичок в Objective-C, поэтому нам будет очень полезно помочь и объяснить :).Как изменить результат для печати значения пикселя RGB в Objective C

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("%3.0f ", (R(color)+G(color)+B(color))/3.0); 

в

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

Спасибо так много !! –