2014-10-07 6 views
0

디스플레이의 내용을 캡처하고 디스플레이의 색상 공간을 sRGB로 변환하려고합니다. 저는 vImage와 저수준의 ColorSync를 사용하여 API를 변환하려고합니다. 그러나 출력에는 픽셀 블록이 이상하게 두 배가됩니다. 내 ColorSyncTransformRef는 ColorSyncTransformConvert()가 작동하는 것처럼 보이지만 vImage를 사용하는 Accelerate 프레임 워크 버전은 그렇지 않습니다. 누구든지 전에 이것을 보았고 그것을 고치는 법을 알고 있습니까? 감사!잘못된 출력을 생성하는 ColorSync 및 vImage

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); 

답변

2

이 버그가 나타납니다

여기 enter image description here

내가 사용하고 코드입니다 : 여기

출력이 어떻게 생겼는지, 당신은 이상한 배가을 볼 수있다 OS에서 (적어도 매버릭스에서는). 내 컴퓨터에 (매버릭스)가 실행 https://developer.apple.com/library/mac/samplecode/convertImage/Introduction/Intro.html

PNG 유물의 같은 종류를 생산하는 스크린 샷에 대하여 :

내가 여기 당신의 코드와 매우 유사하다 샘플 코드를 다운로드했습니다.