微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Core Image ciContext.writeHEIFRepresentation() 在使用 CICrop 过滤器时生成带有对角线的图像我怎样才能防止这种情况?

如何解决Core Image ciContext.writeHEIFRepresentation() 在使用 CICrop 过滤器时生成带有对角线的图像我怎样才能防止这种情况?

我正在尝试生成 HEIF 输出文件,但这样做时会出现奇怪的对角线。其他输出格式都没有表现出这些奇怪的线条。见下图。问题似乎是裁剪,但错误显示在 HEIF 输出文件中。

我可以使用以下 Playground 代码复制此代码,您可以从此处下载 https://duncangroenewald.com/files/SampleApps/CoreImageHEIFExporterBug-2.zip

我有一个关于 Apple 的错误报告,他们提到了“子像素错误”——我不知道那是什么或者为什么这些只会用 heif 输出格式表现出来或者如何防止它们——任何想法或者只是这样做看起来像是 Apple 代码中的错误

编辑。需要明确的是,这不是操场问题,因为问题发生在 macOS 应用程序中。

我怀疑这在生成 HEIF 输出时一定是一个 CIContext 错误,因为它只与 heif 输出文件一起显示。但是,我对亚像素错误一无所知,也不知道为什么 Apple 认为它们应该与此问题相关。也许有更多 Core Image 知识的人可以提出一些可能的解决方案。

import Cocoa

let files = ["/Users/duncangroenewald/Development/Playgrounds/CoreImageHEIFExporterBug/DSC02022.ARW","/Users/duncangroenewald/Development/Playgrounds/CoreImageHEIFExporterBug/DSC02018.ARW"
]

let destFilePath = "/Users/duncangroenewald/Development/Playgrounds/CoreImageHEIFExporterBug"

let destUrl = URL(fileURLWithPath: destFilePath)

let exporter = Exporter()

var crops = [CGRect(x: 0,y: 0,width: 5310,height: 3212),CGRect(x: 0,width: 5311,width: 5312,width: 5313,height: 3212)]


for file in files {
    exporter.processRAWFile(file,destPath: destFilePath,crops: crops)
}

print("Done!")

import AppKit
import CoreImage
import CoreImage.CIFilterBuiltins
import oslog

public class Exporter {
    
    var formats: [String] = ["heif","png"]
    
    // Get the CIContext
    let ciContext = CIContext()
    
    var imageOrientation: Int = 0
    var imageNativeSize: CGSize = .zero

    var crops: [CGRect] = [.zero]

    public init() {
        
    }
    
    public func processRAWFile(_ path: String,destPath: String,crops: [CGRect]) {
        
        print("processRAWFile XXX")
        self.crops = crops
        
        let url = URL(fileURLWithPath: path)
        
        let filename = url.deletingPathExtension().lastPathComponent
        
        let destDirUrl = URL(fileURLWithPath: destPath)
        print("destDirUrl: \(destDirUrl)")
        
        for crop in crops {
        
            let cropName = "\(filename)_\(crop.width)x\(crop.height)_\(useDefaultRAW ? "default" : "")"
        
            for format in formats {
            let destUrl = destDirUrl.appendingPathComponent(cropName).appendingPathExtension(format)
            print("destFile: \(destUrl)")
        
                self.exportFile(url: url,destUrl: destUrl,crop: crop,format: format)
                
            }
          
        }
    }
    
    func exportFile(url: URL,destUrl: URL,crop: CGRect,format: String){
        print("exportFile \(url)")
        print("destFile: \(destUrl)")
        
        guard let rawFilter = CIFilter(imageURL: url,options: nil),let outputimage = rawFilter.outputimage else {
            print("Failed to load \(url.lastPathComponent) for exporting")
            return
        }
        
        let processImage = self.crop(outputimage,rect: crop)
        
        
        let options = [kCGImageDestinationLossyCompressionQuality as CIImageRepresentationoption: 1.0 as CGFloat]
        let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)!
        let imgFormat = CIFormat.RGBA16
        
        do {
            
            switch format {
            case "tiff":
                try ciContext.writeTIFFRepresentation(of: processImage,to: destUrl,format: imgFormat,colorSpace: colorSpace,options: options)
            case "jpeg":
                try ciContext.writeJPEGRepresentation(of: processImage,options: options)
            case "png":
                try ciContext.writePNGRepresentation(of: processImage,options: options)
            case "heif":
                try ciContext.writeHEIFRepresentation(of: processImage,options: options)
            default:
                try ciContext.writePNGRepresentation(of: processImage,options: options)
            }
            
        } catch {
            print("Error exporting \(format): \(error.localizedDescription)")
        }
        
    }
    var useDefaultRAW: Bool = true

    func crop(_ ciImage: CIImage,rect: CGRect)->CIImage {
        if let cropped = self.cropFilter(ciImage,rect: rect) {
            return cropped
        } else {
            print("Error cropping image !")
            return ciImage
        }
    }
  
    
    func cropFilter(_ input: CIImage,rect: CGRect) -> CIImage? {
        
        
        // Getting a dashed border !!
        guard let cropFilter = CIFilter(name: "CICrop") else {
            print("Error no CICrop filter found !")
            return input
        }
        let ciVect = CIVector(cgRect: rect)
        cropFilter.setValue(input,forKey: kCIInputimageKey)

        cropFilter.setValue(ciVect,forKey: "inputRectangle")

        return cropFilter.value(forKey: kCIoUtputimageKey) as? CIImage
        
    }
}

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。