CGBitmapContext gets the pixel value of Leopard vs. Snowleopard confusion

I am trying to draw specific colored rectangles in a CGBitmapContext and then compare the pixel values โ€‹โ€‹with the color that I painted (a kind of hit).

On Leopard, this works great, but on SnowLeopard, the pixel values โ€‹โ€‹I choose are different from the color values โ€‹โ€‹I draw - I think because of the confusion of colors and ignorance on my part.

The main steps that I take: -

  • create a raster context with the kCGColorSpaceGenericRGB color space
  • set the fillColorSpace context to the same color space kCGColorSpaceGenericRGB
  • set context fill color
  • to do
  • get bitmapContextData, iterate over pixel values, etc.

As an example, on Leopard, if I do: -

CGContextSetRGBFillColor(cntxt, 1.0, 0.0, 0.0, 1.0 ); // set pure red fill colour
CGContextFillRect(cntxt, cntxtBounds); // fill entire context

UInt8 red == 255, green == 0, blue == 0, alpha == 255

Snow Lepard, , UInt8 red == 243, == 31, == 6, alpha == 255 ( - . , - "", (1.0,0,0). , (1.0,1.0,1.0), (255,255,255) (0,0,0) (0,0,0)).

colorSpaces, . , .

UPDATE , , .

//create
NSUInteger arbitraryPixSize = 10;
size_t components = 4;
size_t bitsPerComponent = 8;
size_t bytesPerRow = (arbitraryPixSize * bitsPerComponent * components + 7)/8;
size_t dataLength = bytesPerRow * arbitraryPixSize;
UInt32 *bitmap = malloc( dataLength );
memset( bitmap, 0, dataLength );

CGColorSpaceRef colSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);

CGContextRef context = CGBitmapContextCreate (                                        bitmap, arbitraryPixSize, arbitraryPixSize,                                                 bitsPerComponent,bytesPerRow,                                           colSpace, kCGImageAlphaPremultipliedFirst );

CGContextSetFillColorSpace( context, colSpace );
CGContextSetStrokeColorSpace( context, colSpace );

// -- draw something
CGContextSetRGBFillColor( context, 1.0f, 0.0f, 0.0f, 1.0f );
CGContextFillRect( context, CGRectMake( 0, 0, arbitraryPixSize, arbitraryPixSize ) );

// test the first pixel
UInt8 *baseAddr = (UInt8 *)CGBitmapContextGetData(context);
UInt8 alpha = baseAddr[0];
UInt8 red = baseAddr[1];
UInt8 green = baseAddr[2];
UInt8 blue = baseAddr[3];

CGContextRelease(context);
CGColorSpaceRelease(colSpace);

Leopard โ†’ red == 255, green == 0, blue == 0, alpha == 255

โ†’ == 228, == 29, == 29, == 255

+3
1

CGContextSetRGBFillColor.

CGContextSetRGBFillColor DeviceRGB.

, RGB. , .

+3

Source: https://habr.com/ru/post/1720284/


All Articles