Saturday, September 24, 2022
HomeiOS Developmentios - Why does CGBitmapContextCreate count on double the variety of bytesPerRow?

ios – Why does CGBitmapContextCreate count on double the variety of bytesPerRow?


I am making an attempt to generate a pixel buffer utilizing a bitmap context on iOS (in ObjC). The abridged code (to take away null checks and many others) is beneath.

CGFloat width = 1;
CGFloat top = 1;

CVPixelBufferRef buffer;
CVReturn standing = CVPixelBufferCreate(kCFAllocatorDefault, 
                                      width, 
                                      top,  
                                      kCVPixelFormatType_OneComponent8,
                                      nil, 
                                      &buffer);

CVPixelBufferLockBaseAddress(buffer, 0);
void *information = CVPixelBufferGetBaseAddress(buffer);

CGColorSpaceRef house = CGColorSpaceCreateDeviceGray();

CGContextRef ctx = CGBitmapContextCreate(information, 
                                         width, 
                                         top, 
                                         8, 
                                         0, 
                                         house, 
                                         (CGBitmapInfo) kCGImageAlphaNoneSkipLast);

// ... draw into context

CVPixelBufferUnlockBaseAddress(buffer, 0);

That is making an attempt to create a bitmap context for a single pixel, the place each the enter and the output pixels are 8-bit grayscale.

I get the next output (I added the daring):

CGBitmapContextCreate: invalid information bytes/row: ought to be not less than 2 for 8 integer bits/part, 1 parts, kCGImageAlphaNoneSkipLast.

Why does it double the anticipated bytes per row? That is constant for the width / top combos I’ve tried, and ‘works’ if I halve the width parameter in CGBitmapContextCreate. Word additionally that if I go in a price for bytesPerRow then it nonetheless fails this verify and offers the identical output.

Am I lacking one thing apparent?

Edit: formatting.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular