Swift 3 Color Space macOS不是IOS [英] Swift 3 Colour Space macOS not IOS

查看:95
本文介绍了Swift 3 Color Space macOS不是IOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将RGB图像转换为其灰度颜色空间?我可以找到很多针对iOS的代码,但找不到针对macOS的代码.而且Apple的文档全都以客观的C ....

    let width = image.size.width
    let height = image.size.height
    let imageRect = NSMakeRect(0, 0, width, height);
    let colorSpace = CGColorSpaceCreateDeviceGray();


    let bits = image.representations.first as! NSBitmapImageRep;
    bitmap!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: nil)


    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue);
    let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue);
    context.draw(image.cgImage!, in : imageRect);// and this line is wrong obviously..

这是我到目前为止所能得到的.只是从互联网上复制和粘贴..但我不知道如何进一步...

解决方案

我找到了一种有趣的方法.

如何从nsimage快速创建灰度图像?/a>

使用COCOA和NSImage

更改NSImage的色彩空间:第二个回复

我的代码:

func saveImage(image:NSImage, destination:URL) throws{
    let rep = greyScale(image);
    var data = rep.representation(using: NSJPEGFileType, properties: [:]);
    try data?.write(to: destination);

}

// rgb2gray
func greyScale(image: NSImage) -> NSBitmapImageRep{
    let w = image.size.width
    let h = image.size.height
    let imageRect : NSRect! = NSMakeRect(0,0, w, h);
    let colourSpace : ColourSpace! = CGColorSpaceCreateDeviceGray();
    let context : CGContext! = CGContext(data: nil, width: Int(w), 
                                        height: Int(h), bitsPerComponent: 8, 
                                        bytesPerRow: 0, space: colorSpace, 
                                        bitmapInfo: CGImageAlphaInfo.none.rawValue);        

    context.draw(nsImageToCGImage(image: image), in: imageRect);
    let greyImage : CGImage! = context.makeImage();

    return NSBitmapImageRep(cgImage: greyImage);
}


func nsImageToCGImage(image: NSImage) -> CGImage{
    if let imageData = image.tiffRepresentation as NSData! {
        let imageSource : CGImageSource! = CGImageSourceCreateWithData(imageData, 
                                        nil);
        let image = CGImageSourceCreateImageAtIndex(imageSource, 0, nil);
        return image;
    }
    return nil;
}

我仍在试图理解其背后的原理.

How can I convert an RGB image into its grayscaled colour space? I can find a lot of code for iOS but non for macOS.. And the Apple's documentations are all in objective C....

    let width = image.size.width
    let height = image.size.height
    let imageRect = NSMakeRect(0, 0, width, height);
    let colorSpace = CGColorSpaceCreateDeviceGray();


    let bits = image.representations.first as! NSBitmapImageRep;
    bitmap!.representationUsingType(NSBitmapImageFileType.NSPNGFileType, properties: nil)


    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue);
    let context = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue);
    context.draw(image.cgImage!, in : imageRect);// and this line is wrong obviously..

This is what I have got so far..just copy and pasting from the internet.. but I have no idea on how to go further...

解决方案

I have found an interesting way to do this.. My code are simply copied from the three sources below.

how to create grayscale image from nsimage in swift?

Greyscale Image using COCOA and NSImage

Changing the Color Space of NSImage: The second reply

My Code:

func saveImage(image:NSImage, destination:URL) throws{
    let rep = greyScale(image);
    var data = rep.representation(using: NSJPEGFileType, properties: [:]);
    try data?.write(to: destination);

}

// rgb2gray
func greyScale(image: NSImage) -> NSBitmapImageRep{
    let w = image.size.width
    let h = image.size.height
    let imageRect : NSRect! = NSMakeRect(0,0, w, h);
    let colourSpace : ColourSpace! = CGColorSpaceCreateDeviceGray();
    let context : CGContext! = CGContext(data: nil, width: Int(w), 
                                        height: Int(h), bitsPerComponent: 8, 
                                        bytesPerRow: 0, space: colorSpace, 
                                        bitmapInfo: CGImageAlphaInfo.none.rawValue);        

    context.draw(nsImageToCGImage(image: image), in: imageRect);
    let greyImage : CGImage! = context.makeImage();

    return NSBitmapImageRep(cgImage: greyImage);
}


func nsImageToCGImage(image: NSImage) -> CGImage{
    if let imageData = image.tiffRepresentation as NSData! {
        let imageSource : CGImageSource! = CGImageSourceCreateWithData(imageData, 
                                        nil);
        let image = CGImageSourceCreateImageAtIndex(imageSource, 0, nil);
        return image;
    }
    return nil;
}

I am still trying to understand the principle behind.

这篇关于Swift 3 Color Space macOS不是IOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆