Swift 3 Colour Space macOS not IOS

喜欢而已 提交于 2019-12-13 17:31:26

问题


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...


回答1:


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.




回答2:


You can try CIFilter. The annoyance is that you have to convert back and forth between NSImage and CIImage:

import Cocoa
import CoreImage

let url = Bundle.main.url(forResource: "image", withExtension: "jpg")!
let image = CIImage(contentsOf: url)!

let bwFilter = CIFilter(name: "CIColorControls", withInputParameters: ["inputImage": image, "inputSaturation": 0.0])!

if let ciImage = bwFilter.outputImage {
    let rep = NSCIImageRep(ciImage: ciImage)
    let nsImage = NSImage(size: rep.size)
    nsImage.addRepresentation(rep)
    // nsImage is now your black-and-white image
}


来源:https://stackoverflow.com/questions/42036448/swift-3-colour-space-macos-not-ios

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!