首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >产生错误输出的ColorSync和vImage

产生错误输出的ColorSync和vImage
EN

Stack Overflow用户
提问于 2014-10-08 05:30:01
回答 1查看 488关注 0票数 0

我正在尝试捕获显示器的内容,并将显示器的色彩空间转换为sRGB。我尝试使用vImage和低级的ColorSync transform API来实现这一点,但是输出的像素块出现了奇怪的翻倍现象。当ColorSyncTransformConvert()工作时,我的ColorSyncTransformRef似乎是正确的,但是使用vImage的Accelerate框架版本不是这样。有没有人以前见过这种情况并知道如何修复它?谢谢!

下面是输出的样子,你可以看到奇怪的加倍:

下面是我使用的代码:

代码语言:javascript
复制
    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);
EN

回答 1

Stack Overflow用户

发布于 2014-10-08 08:03:37

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

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26245657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档