macos - ColorSync 和 vImage 产生不良输出

标签 macos cocoa vimage

我正在 try catch 显示器的内容并将显示器的色彩空间转换为 sRGB。我正在尝试使用 vImage 和低级 ColorSync 转换 API 来执行此操作,但输出的像素 block 奇怪地加倍。我的 ColorSyncTransformRef 似乎是正确的,因为 ColorSyncTransformConvert() 有效,但使用 vImage 的 Accelerate 框架版本则不然。有谁以前见过这个并知道如何解决它?谢谢!

这是输出的样子,您可以看到奇怪的加倍:

enter image description here

这是我正在使用的代码:

    CGImageRef screenshot = CGDisplayCreateImage(CGMainDisplayID());
CFDataRef rawData = CGDataProviderCopyData(CGImageGetDataProvider(screenshot));
uint8_t *basePtr = (uint8_t *)CFDataGetBytePtr(rawData);

const void *keys[] = {kColorSyncProfile, kColorSyncRenderingIntent, kColorSyncTransformTag};

ColorSyncProfileRef srcProfile =  ColorSyncProfileCreateWithDisplayID(CGMainDisplayID());
ColorSyncProfileRef destProfile = ColorSyncProfileCreateWithName(kColorSyncSRGBProfile);

const void *srcVals[] = {srcProfile,  kColorSyncRenderingIntentPerceptual, kColorSyncTransformDeviceToPCS};
const void *dstVals[] = {destProfile,  kColorSyncRenderingIntentPerceptual, kColorSyncTransformPCSToDevice};

CFDictionaryRef srcDict = CFDictionaryCreate (
                                              NULL,
                                              (const void **)keys,
                                              (const void **)srcVals,
                                              3,
                                              &kCFTypeDictionaryKeyCallBacks,
                                              &kCFTypeDictionaryValueCallBacks);


CFDictionaryRef dstDict = CFDictionaryCreate (
                                              NULL,
                                              (const void **)keys,
                                              (const void **)dstVals,
                                              3,
                                              &kCFTypeDictionaryKeyCallBacks,
                                              &kCFTypeDictionaryValueCallBacks);

const void* arrayVals[] = {srcDict, dstDict, NULL};

CFArrayRef profileSequence = CFArrayCreate(NULL, (const void **)arrayVals, 2, &kCFTypeArrayCallBacks);

CFTypeRef codeFragment = NULL;

/* transform to be used for converting color */
ColorSyncTransformRef transform = ColorSyncTransformCreate(profileSequence, NULL);

/* get the code fragment specifying the full conversion */
codeFragment = ColorSyncTransformCopyProperty(transform, kColorSyncTransformFullConversionData, NULL);
if (transform) CFRelease (transform);

if (codeFragment) CFShow(codeFragment);

vImage_Error err;

vImage_CGImageFormat inFormat = {
    .bitsPerComponent = 8,
    .bitsPerPixel = 32,
    .colorSpace = CGColorSpaceCreateWithPlatformColorSpace(srcProfile),
    .bitmapInfo = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little,
}; // .version, .renderingIntent and .decode all initialized to 0 per C rules


vImage_CGImageFormat outFormat = {
    .bitsPerComponent = 8,
    .bitsPerPixel = 32,
    .colorSpace = CGColorSpaceCreateWithPlatformColorSpace(destProfile),
    .bitmapInfo = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little,
}; // .version, .renderingIntent and .decode all initialized to 0 per C rules

/* create a converter to do the image format conversion. */
vImageConverterRef converter = vImageConverter_CreateWithColorSyncCodeFragment(codeFragment, &inFormat, &outFormat, NULL, kvImagePrintDiagnosticsToConsole, &err );

NSAssert(err == kvImageNoError, @"Unable to setup the colorsync transform!");

/* Check to see if the converter will work in place. */
vImage_Error outOfPlace = vImageConverter_MustOperateOutOfPlace(converter, NULL, NULL, kvImageNoFlags);
NSAssert(!outOfPlace, @"We expect the image convert to work in place");

if (srcDict) CFRelease (srcDict);
if (dstDict) CFRelease (dstDict);

if (profileSequence) CFRelease (profileSequence);

// ColorSync converter time!
size_t width = CGImageGetWidth(screenshot);
size_t height = CGImageGetHeight(screenshot);

vImage_Buffer buf;
err = vImageBuffer_Init( &buf, height, width, 32, kvImageNoFlags );
NSAssert(err == kvImageNoError, @"Failed to init buffer");

memcpy(buf.data, basePtr, width * height * 4);

vImage_Buffer vimg_src = { buf.data, height, width, width * 4 };
vImage_Buffer vimg_dest = { buf.data, height, width, width * 4 };
vImage_Error colorSyncErr = vImageConvert_AnyToAny(converter, &vimg_src, &vimg_dest, NULL, kvImageNoFlags );
NSAssert(colorSyncErr == kvImageNoError, @"ColorSync conversion failed");


NSData *pngData = AOPngDataForBuffer(buf.data, (int)width, (int)height);
[pngData writeToFile:@"/tmp/out.png" atomically:YES];
self.imageView.image = [[NSImage alloc] initWithData:pngData];

free(buf.data);
CFRelease(rawData);

最佳答案

这似乎是操作系统中的一个错误(至少在 Mavericks 中)。

我下载了与您在此处找到的代码非常相似的示例代码:https://developer.apple.com/library/mac/samplecode/convertImage/Introduction/Intro.html

在我的机器(使用 Mavericks)上针对 PNG 屏幕截图运行该程序会产生相同类型的工件。

关于macos - ColorSync 和 vImage 产生不良输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26245657/

相关文章:

macos - 如何在 OS X Mavericks 上获取本地旧版本的 Safari?

Snow Leopard Xcode 中的 C++ 调试构建中断

macos - Mac上的Docker在更改ssl证书的代理后面

swift - 如何在 cocoa NSView 中设置背景图片

objective-c - 不可能创建没有可执行代码的框架吗?

cocoa - 如何为 NSHTTPURLResponse 获取正确的 Set-Cookie header ?

ios - Accelerate 的 vImage 与 vDSP

objective-c - 在 Cocoa 中添加一个新窗口

swift - 转换为 vImage 的 CMSampleBuffer 帧颜色错误

ios - 在 Swift ios 中使用 vImageBuffer_initWithCGImage 时出现 "fatal error: unexpectedly found nil while unwrapping an Optional value"