从CGImage创建的CVPixelBuffer具有不同的属性



EDIT:我解决了这个问题,但为了重新表述我的问题——是否可以创建CVPixelBuffer来匹配CGImage?例如,我希望每个组件有16位,而不是8位。


正如标题所说。例如,当我NSLog我的缓冲区时,它每行有4544个字节。但当我NSLog实际的CGImage时,它有9000。这是日志:

buffer: <CVPixelBuffer 0x282b8b020 width=1125 height=2436 bytesPerRow=4544 pixelFormat=BGRA iosurface=0x0 attributes={    PixelFormatDescription =     {
BitsPerBlock = 32;
BitsPerComponent = 8;
BlackBlock = {length = 4, bytes = 0x000000ff};
CGBitmapContextCompatibility = 1;
CGBitmapInfo = 8196;
CGImageCompatibility = 1;
ComponentRange = FullRange;
ContainsAlpha = 1;
ContainsGrayscale = 0;
ContainsRGB = 1;
ContainsYCbCr = 0;
FillExtendedPixelsCallback = {length = 24, bytes = 0x00000000000000008c3c1da7010000000000000000000000};
IOSurfaceCoreAnimationCompatibility = 1;
IOSurfaceOpenGLESFBOCompatibility = 1;
IOSurfaceOpenGLESTextureCompatibility = 1;
OpenGLESCompatibility = 1;
PixelFormat = 1111970369;
};
} propagatedAttachments={
} nonPropagatedAttachments={
}>

和图像

image: <CGImage 0x1044625d0> (IP)   <<CGColorSpace 0x283e9ea60> (kCGColorSpaceICCBased; kCGColorSpaceModelRGB; Display P3)>
width = 1125, height = 2436, bpc = 16, bpp = 64, row bytes = 9000
kCGImageAlphaNoneSkipLast | kCGImageByteOrder16Little  | kCGImagePixelFormatPacked
is mask? No, has masking color? No, has soft mask? No, has matte? No, should interpolate? Yes

这是我使用的代码

CGImageRef cgImageRef = [image CGImage];
CGImageRetain(cgImageRef);
CVPixelBufferRef pixelBuffer = NULL;
size_t w = CGImageGetWidth(cgImageRef);
size_t h = CGImageGetHeight(cgImageRef);
CGDataProviderRef x = CGImageGetDataProvider(cgImageRef);
CGDataProviderRetain(x);
CFDataRef da = CGDataProviderCopyData(x);
CFIndex len = CFDataGetLength(da);
const uint8_t* src = CFDataGetBytePtr(da);
CVPixelBufferCreate(kCFAllocatorDefault,
w,
h,
kCVPixelFormatType_32BGRA,
nil,
&pixelBuffer);
CVPixelBufferLockBaseAddress( pixelBuffer, 0 );
void* base = CVPixelBufferGetBaseAddress(pixelBuffer);
SNLog(@"buffer: %@", pixelBuffer);
SNLog(@"image: %@", cgImageRef);

稍后,我将尝试遍历CGImage地址,并手动将字节复制到目标像素缓冲区中。但它崩溃了,我想这是因为我在某个时候试图从";在";实际图像。

我想我最初的问题并不完全清楚,或者我应该稍微改写一下。问题更像是——我想创建我的缓冲区来匹配图像——每个组件的字节/位数量相同。

我通过在缓冲区中循环并从图像中单独挑选我想要的字节来解决这个问题。基本上忽略每个像素的一些位。

最后,图像看起来仍然很好,我怀疑增加的比特数量是为了在功能更强的屏幕上获得更好的质量。虽然我不确定。

最新更新