简体   繁体   English

如何更改结果以在目标C中打印RGB像素值

[英]How to change the result to print the RGB pixel value in Objective C

I have this code and I want it to return the RGB colour value of each pixel, not the brightness. 我有这段代码,我希望它返回每个像素的RGB颜色值,而不是亮度。 At the moment it prints the brightness of every pixel onto the command line and I want that to change this to print the RGB value of each pixel instead. 目前,它将每个像素的亮度打印到命令行上,我希望更改它以打印每个像素的RGB值。 Im fairly new to Objective-C so some help and explanation would be greatly appreciated :). 我对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);

Just change this line: 只需更改此行:

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM