ios - 对于IOS,如何使用vImage将ARGB图像转换为灰度图像

标签 ios vimage

我是IOS APP开发的大一,遇到一个问题:“如何使用vImage将ARGB图像转换为灰度图像”。实际上,我已经通过其他方法实现了这种转换处理。但是,我发现“vImage”可以通过“加速框架引用”完成这项工作。
我试过这种方法,但没有任何转换结果。在这里,我附上代码,你能帮我吗?

-(UIImage *)rgbTograyByvImage
{
    const size_t width = self.size.width * self.scale;
    const size_t height = self.size.height * self.scale;
    const size_t bytesPerRow = width * 4;
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGContextRef bmContext= CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, space, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(space);
    if (!bmContext) {
        return nil;
    }
    CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = width, .size.height = height}, self.CGImage);
    UInt32 *data = (UInt32 *)CGBitmapContextGetData(bmContext);
    if (!data) {
        CGContextRelease(bmContext);
        return nil;
    }
    int Aphal = 0;
    int Red = 1;
    int Green = 2;
    int Blue = 3;
    UInt8 *aphalSpace = malloc(sizeof(UInt8) * width * height);
    UInt8 *redSpace = malloc(sizeof(UInt8) * width * height);
    UInt8 *greenSpace = malloc(sizeof(UInt8) * width * height);
    UInt8 *blueSpace = malloc(sizeof(UInt8) * width * height);

    for (int y=0; y<height; y++) {
        for (int x=0; x<width; x++) {
            UInt8 *argbPixel = (UInt8 *) (&data[y*width+x]); 
            aphalSpace[y*width+x] = argbPixel[Aphal];
            redSpace[y*width+x] = argbPixel[Red];
            greenSpace[y*width+x] = argbPixel[Green];
            blueSpace[y*width+x] = argbPixel[Blue];
        }
    }

    vImage_Buffer argbImageBuffer = {(UInt8*)data, height, width, bytesPerRow};
    vImage_Buffer aImageBuffer = {aphalSpace, height, width, width};
    vImage_Buffer rImageBuffer = {redSpace, height, width, width};
    vImage_Buffer gImageBuffer = {greenSpace, height, width, width};
    vImage_Buffer bImageBuffer = {blueSpace, height, width, width};
    vImage_Error error;
    error = vImageConvert_ARGB8888toPlanar8(&argbImageBuffer, &aImageBuffer, &rImageBuffer, &gImageBuffer, &bImageBuffer, kvImageNoFlags);
    if (error != kvImageNoError) {
        NSLog(@"%s, vImage error %zd", __PRETTY_FUNCTION__, error);
    }
    CGImageRef grayImage = CGBitmapContextCreateImage(bmContext);
    UIImage *gray = [UIImage imageWithCGImage:grayImage];

    CGContextRelease(bmContext);
    CGImageRelease(grayImage);
    free(aphalSpace);
    free(redSpace);
    free(greenSpace);
    free(blueSpace);

    return gray;
}

最佳答案

你想要 vImageMatrixMultiply_ARGB8888ToPlanar8,iOS 9 的新功能。

关于ios - 对于IOS,如何使用vImage将ARGB图像转换为灰度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31241427/

相关文章:

ios - 可以从共享点 url 运行 WKWebView 吗?

ios - Xcode:如何使用多个 "Display Name"值,每个配置都不同?

ios - swift 3 : Handle Errors in URLSession Delegates

iOS Accelerate Framework vImage - 性能改进?

macos - 由于调用可疑的调试函数,CGContextDrawImage 的性能不佳

ios - 如何从 CGColorSpace 获取 Unmanaged<CGColorSpace> ?

ios - 如何使用 vImageMatrixMultiply 将 YUV 平面数据转换为 32 bpp、8bpc RGBX?

ios - 将 UITapGestureReconizer 添加到 UIImage

ios - 创建一个圆形颜色选择器

ios - 如何编译vImage浮雕效果示例代码?