2011-12-09 59 views
2

你有一个Objective C实现相当于ImageMagick的命令:阿尔法遮掩提取工艺Objective C的实现

convert -alpha Extract -type optimize -strip -quality 60 +dither Source.png Alpha.jpg 

我无法找到任何解决办法现在。 我正在寻找一个AlphaExtractor片断,将来自PNG提取的α和将其保存到JPG灰度

掩模使用代码段创建的:

CGImageRef createMaskWithImage(CGImageRef image) 
{ 
    int maskWidth    = CGImageGetWidth(image); 
    int maskHeight    = CGImageGetHeight(image); 
    // round bytesPerRow to the nearest 16 bytes, for performance's sake 
    int bytesPerRow    = (maskWidth + 15) & 0xfffffff0; 
    int bufferSize    = bytesPerRow * maskHeight; 

    // we use CFData instead of malloc(), because the memory has to stick around 
    // for the lifetime of the mask. if we used malloc(), we'd have to 
    // tell the CGDataProvider how to dispose of the memory when done. using 
    // CFData is just easier and cleaner. 

    CFMutableDataRef dataBuffer = CFDataCreateMutable(kCFAllocatorDefault, 0); 
    CFDataSetLength(dataBuffer, bufferSize); 

    // the data will be 8 bits per pixel, no alpha 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray);//CGColorSpaceCreateDeviceGray(); 
    CGContextRef ctx   = CGBitmapContextCreate(CFDataGetMutableBytePtr(dataBuffer), 
                 maskWidth, maskHeight, 
                 8, bytesPerRow, colorSpace, kCGImageAlphaNone); 
    // drawing into this context will draw into the dataBuffer. 
    CGContextDrawImage(ctx, CGRectMake(0, 0, maskWidth, maskHeight), image); 
    CGContextRelease(ctx); 

    // now make a mask from the data. 
    CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(dataBuffer); 
    CGImageRef mask     = CGImageMaskCreate(maskWidth, maskHeight, 8, 8, bytesPerRow, 
                 dataProvider, NULL, FALSE); 

    CGDataProviderRelease(dataProvider); 
    CGColorSpaceRelease(colorSpace); 
    CFRelease(dataBuffer); 

    return mask; 
} 

并保存:

回答

0

我看到两个问题:

CGContextDrawImage(ctx, CGRectMake(0, 0, maskWidth, maskHeight), image); 

不提取alpha,它只是将图像合成到黑色背景上。 如果图像是透明的黑色,那么全黑图像将是预期的输出。

和:

CGImageRef mask = CGImageMaskCreate(maskWidth, maskHeight, 8, 8, bytesPerRow, 
            dataProvider, NULL, FALSE); 

你治疗这个面具创建像一个真正的形象。如果您有

CGImageRef mask = CGImageCreate(maskWidth, maskHeight, 8, 8, bytesPerRow, colorSpace, 0, 
           dataProvider, NULL, FALSE, kCGRenderingIntentDefault); 

替换此行,然后你会得到你的图像的灰度版本(见问题1)

+0

我该如何解决问题1? – bpds

+0

我不太确定,我会尽量在稍后玩一下,除非有人知道更多的知识跳进去。 – cobbal

+0

我找到了解决方案 – bpds

1

一个非常快速的一个肮脏的工作方案:

假设我们有一个32字节的原始数据(如果不是代码需要适应)

1-我们通过+4步遍历字节并更改r,g,b分量。

CGImageRef ref=CGImageCreateCopy([_imageView image]); 
NSData *data  = (NSData *)CGDataProviderCopyData(CGImageGetDataProvider(ref)); 
char *bytes  = (char *)[data bytes]; 

int i; 
for(i= 0; i < [data length]; i += 4) 
{ 
    int r = i; 
    int g = i+1; 
    int b = i+2; 
    int a = i+3; 

    bytes[r] = 0; 
    bytes[g] = 0; 
    bytes[b] = 0; 
    bytes[a] = bytes[a]; 
} 

2-我们创建具有 “修改后的数据” 的新的RGBA(32位)的图像参考:

size_t width     = CGImageGetWidth(ref); 
size_t height     = CGImageGetHeight(ref); 
size_t bitsPerComponent   = CGImageGetBitsPerComponent(ref); 
size_t bitsPerPixel    = CGImageGetBitsPerPixel(ref); 
size_t bytesPerRow    = CGImageGetBytesPerRow(ref); 

CGColorSpaceRef colorspace  = CGColorSpaceCreateDeviceRGB(); 
CGBitmapInfo bitmapInfo   = CGImageGetBitmapInfo(ref); 
CGDataProviderRef provider  = CGDataProviderCreateWithData(NULL,bytes, [data length], NULL); 

CGImageRef newImageRef = CGImageCreate (
             width, 
             height, 
             bitsPerComponent, 
             bitsPerPixel, 
             bytesPerRow, 
             colorspace, 
             bitmapInfo, 
             provider, 
             NULL, 
             false, 
             kCGRenderingIntentDefault 
             ); 

3-我们保存此新32字节的图像参照的JPEG文件。 生成的JPG将可用作掩码。

我们可以通过创建8Bit上下文并仅编写“alpha组件”来实现更简洁的方式。