ColorSync和vImage产生错误输出

时间:2014-10-07 21:30:01

标签: macos cocoa vimage

我试图捕捉显示内容并从显示器的色彩空间转换为sRGB。我试图使用vImage和低级ColorSync转换API来做到这一点,但输出有一个奇怪的像素块加倍。我的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);

1 个答案:

答案 0 :(得分:2)

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

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

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