4

我正在尝试用白色而不是黑色绘制标准 NSImage 。以下适用于在当前 NSGraphicsContext 中以黑色绘制图像:

NSImage* image = [NSImage imageNamed:NSImageNameEnterFullScreenTemplate];
[image drawInRect:r fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

我希望 NSCompositeXOR 能做到这一点,但没有。我需要走复杂的 [CIFilter filterWithName:@"CIColorInvert"] 路径吗?我觉得我一定错过了一些简单的东西。

4

3 回答 3

6

Core Image 路线将是最可靠的。它实际上并不是很复杂,我在下面发布了一个示例。如果您知道您的图像都不会被翻转,那么您可以删除转换代码。需要注意的主要事情是从NSImage到的转换在CIImage性能方面可能会很昂贵,因此您应该确保CIImage尽可能缓存并且不要在每次绘图操作期间重新创建它。

CIImage* ciImage = [[CIImage alloc] initWithData:[yourImage TIFFRepresentation]];
if ([yourImage isFlipped])
{
    CGRect cgRect    = [ciImage extent];
    CGAffineTransform transform;
    transform = CGAffineTransformMakeTranslation(0.0,cgRect.size.height);
    transform = CGAffineTransformScale(transform, 1.0, -1.0);
    ciImage   = [ciImage imageByApplyingTransform:transform];
}
CIFilter* filter = [CIFilter filterWithName:@"CIColorInvert"];
[filter setDefaults];
[filter setValue:ciImage forKey:@"inputImage"];
CIImage* output = [filter valueForKey:@"outputImage"];
[output drawAtPoint:NSZeroPoint fromRect:NSRectFromCGRect([output extent]) operation:NSCompositeSourceOver fraction:1.0];

注意:释放/保留内存管理留作练习,上面的代码假设垃圾回收。

如果要以任意大小渲染图像,可以执行以下操作:

NSSize imageSize = NSMakeSize(1024,768); //or whatever size you want
[yourImage setSize:imageSize];
[yourImage lockFocus];
NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, imageSize.width, imageSize.height)];
[yourImage unlockFocus];
CIImage* image = [CIImage imageWithData:[bitmap TIFFRepresentation]];
于 2010-01-27T06:54:18.807 回答
1

这是使用 Swift 5.1 的解决方案,在某种程度上基于上述解决方案。请注意,我没有缓存图像,因此它可能不是最有效的,因为我的主要用例是根据当前配色方案是浅色还是深色来翻转工具栏按钮中的小单色图像。

import os
import AppKit
import Foundation

public extension NSImage {

    func inverted() -> NSImage {
        guard let cgImage = self.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
            os_log(.error, "Could not create CGImage from NSImage")
            return self
        }

        let ciImage = CIImage(cgImage: cgImage)
        guard let filter = CIFilter(name: "CIColorInvert") else {
            os_log(.error, "Could not create CIColorInvert filter")
            return self
        }

        filter.setValue(ciImage, forKey: kCIInputImageKey)
        guard let outputImage = filter.outputImage else {
            os_log(.error, "Could not obtain output CIImage from filter")
            return self
        }

        guard let outputCgImage = outputImage.toCGImage() else {
            os_log(.error, "Could not create CGImage from CIImage")
            return self
        }

        return NSImage(cgImage: outputCgImage, size: self.size)
    }
}

fileprivate extension CIImage {
    func toCGImage() -> CGImage? {
        let context = CIContext(options: nil)
        if let cgImage = context.createCGImage(self, from: self.extent) {
            return cgImage
        }
        return nil
    }
}
于 2020-03-04T23:43:08.543 回答
0

请注意:我发现 CIColorInvert 过滤器并不总是可靠的。例如,如果您想反转在 Photoshop 中反转的图像,CIFilter 将生成更亮的图像。据我了解,这是因为 CIFilter 的 gamma 值(gamma 为 1)和来自其他来源的图像的差异。

当我在寻找改变 CIFilter 的 gamma 值的方法时,我发现 CIContext 中存在一个错误:将其 gamma 值从默认值 1 更改会产生不可预知的结果。

无论如何,还有另一种反转 NSImage 的解决方案,它总是产生正确的结果 - 通过反转 NSBitmapImageRep 的像素。

我从 etutorials.org ( http://bit.ly/Y6GpLn ) 重新发布代码:

// srcImageRep is the NSBitmapImageRep of the source image
int n = [srcImageRep bitsPerPixel] / 8;           // Bytes per pixel
int w = [srcImageRep pixelsWide];
int h = [srcImageRep pixelsHigh];
int rowBytes = [srcImageRep bytesPerRow];
int i;

NSImage *destImage = [[NSImage alloc] initWithSize:NSMakeSize(w, h)];
NSBitmapImageRep *destImageRep = [[[NSBitmapImageRep alloc] 
      initWithBitmapDataPlanes:NULL
          pixelsWide:w
          pixelsHigh:h
          bitsPerSample:8
          samplesPerPixel:n
          hasAlpha:[srcImageRep hasAlpha] 
          isPlanar:NO
          colorSpaceName:[srcImageRep colorSpaceName]
          bytesPerRow:rowBytes 
          bitsPerPixel:NULL] autorelease];

unsigned char *srcData = [srcImageRep bitmapData];
unsigned char *destData = [destImageRep bitmapData];

for ( i = 0; i < rowBytes * h; i++ )
    *(destData + i) = 255 - *(srcData + i);

[destImage addRepresentation:destImageRep];
于 2013-04-07T18:18:35.840 回答