# c++ – Getting PixelData from CGImageRef returns wrong value

0
108 Im trying to get the Pixel-Values from a CGImageRef, but somehow it always returns the wrong values.
My Test-Image is a complete red picture, just so i can test it easier. Its this: https://i.imgur.com/ZfLtzsl.png

Im getting the CGImageRef with the following Code:

``````CGImageRef UICreateScreenImage();
``````

If i add Code to save the Picture to the Album, it saves correctly a complete red screen. So the ImageRef should be correct.

``````IOSurfaceRef ioSurfaceRef = (__bridge IOSurfaceRef)([UIWindow performSelector:@selector(createScreenIOSurface)]);
CGImageRef cgImageRef = UICreateCGImageFromIOSurface(ioSurfaceRef);

UIImage *uiImage = [UIImage imageWithCGImage:cgImageRef];
UIImageWriteToSavedPhotosAlbum(uiImage, nil, nil, nil);
``````

So, i found code to get the Pixel-Buffer from a ImageRef. Im using the following Code:

``````size_t width  = CGImageGetWidth(cgImageRef);
size_t height = CGImageGetHeight(cgImageRef);

size_t bpr = CGImageGetBytesPerRow(cgImageRef);
size_t bpp = CGImageGetBitsPerPixel(cgImageRef);
size_t bpc = CGImageGetBitsPerComponent(cgImageRef);
size_t bytes_per_pixel = bpp / bpc;
CGDataProviderRef provider = CGImageGetDataProvider(cgImageRef);
CFDataRef rawData = CGDataProviderCopyData(provider);
UInt8 * bytes = (UInt8 *) CFDataGetBytePtr(rawData);
``````

This gives me the width, the height, the buffer
Now, i simply loop through it and print (as test) the Pixels at [100, 100] and [100, 101], both should be red (FF0000FF)

``````struct SPixel {
int r = 0;
int g = 0;
int b = 0;
int a = 0;
};

for(size_t y = 0; y < height; y++) {
for(size_t x = 0; x < width; x++) {
const uint8_t* pixel = &bytes[y * bpr + x * bytes_per_pixel];
SPixel Pixel;
Pixel.a = pixel;
Pixel.r = pixel;
Pixel.g = pixel;
Pixel.b = pixel;

if(y == 100 && x == 100){
Log::Color("red", "Pixel[100, 100]: %d, %d, %d, %d", Pixel.r, Pixel.g, Pixel.b, Pixel.a);
}
if(y == 100 && x == 101){
Log::Color("red", "Pixel[100, 101]: %d, %d, %d, %d", Pixel.r, Pixel.g, Pixel.b, Pixel.a);
}
}
}
``````

And now, if i let that code run, and open my complete red screenshot on fullscreen, the Pixel that get printed are the following:

``````Pixel[100, 100]: 147, 56, 26, 255
Pixel[100, 101]: 20, 255, 255, 144
``````

They are complete off from Red, and are not even then same, even though they are just one pixel from each other.

What am doing wrong?
{"ft_lang_label":"__label__en","ft_lang_prob":0.5373615,"math_prob":0.9654111,"size":2288,"snap":"2023-40-2023-50","text_gpt3_token_len":685,"char_repetition_ratio":0.15367776,"word_repetition_ratio":0.041543026,"special_character_ratio":0.30944055,"punctuation_ratio":0.24400872,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97443336,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-10-02T18:57:34Z\",\"WARC-Record-ID\":\"<urn:uuid:f69fa41b-bbc0-4d28-aa30-d12c555a19f5>\",\"Content-Length\":\"240965\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:e6b223a1-abf3-488c-9ec8-fcfffb710888>\",\"WARC-Concurrent-To\":\"<urn:uuid:09711a14-0a82-4379-8460-0ef023faf32b>\",\"WARC-IP-Address\":\"154.62.106.86\",\"WARC-Target-URI\":\"https://channel969.com/c-getting-pixeldata-from-cgimageref-returns-wrong-value/\",\"WARC-Payload-Digest\":\"sha1:Q3LT572WFUN4PZJOEOQPNLKHGDXCJ47N\",\"WARC-Block-Digest\":\"sha1:PU3T3BXVGSKNEYOFQMV4KMOGGJLEPJGM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233511002.91_warc_CC-MAIN-20231002164819-20231002194819-00181.warc.gz\"}"}