From 78e9be6e1d114ff819e78467a0e6de4a4aa77053 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Mon, 27 Sep 2021 11:38:23 -0400 Subject: [PATCH] Clean up CGRect and CGSize usage, remove some debug prints --- SilicaViewer/Document.swift | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/SilicaViewer/Document.swift b/SilicaViewer/Document.swift index 1225d22..2e64a52 100644 --- a/SilicaViewer/Document.swift +++ b/SilicaViewer/Document.swift @@ -41,6 +41,18 @@ struct SilicaDocument { var height: Int = 0 var layers: [SilicaLayer] = [] + + lazy var nsSize = { + return NSSize(width: width, height: height) + }() + + lazy var cgSize = { + return CGSize(width: width, height: height) + }() + + lazy var cgRect = { + return CGRect(origin: .zero, size: cgSize) + }() } func objectRefGetValue2(_ objectRef: CFTypeRef) -> UInt32 { @@ -136,8 +148,6 @@ class Document: NSDocument { if getDocumentClassName(dict: dict) == LayerClassName { var layer = SilicaLayer() - //dump(dict, indent: 5) - if let val = dict["name"] { let NameClassID = getClassID(id: val) let NameClass = objectsArray[NameClassID] as! NSString @@ -389,11 +399,10 @@ class Document: NSDocument { let ccgContext = CGContext(data: nil, width: info.width, height: info.height, bitsPerComponent: 8, bytesPerRow: info.width * 4, space: info.colorSpace, bitmapInfo: bitmapInfo.rawValue) ccgContext?.setFillColor(info.backgroundColor) - ccgContext?.fill(CGRect(origin: .zero, size: CGSize(width: info.width, height: info.height))) + ccgContext?.fill(info.cgRect) var masterImage = ccgContext?.makeImage() let context = CIContext() - let size = CGRect(x: 0, y: 0, width: info.width, height: info.height) var previousImage: CGImage? @@ -407,7 +416,7 @@ class Document: NSDocument { let maskContext = CGContext(data: nil, width: info.width, height: info.height, bitsPerComponent: 16, bytesPerRow: 0, space: grayColorSpace, bitmapInfo: maskBitmapInfo.rawValue) maskContext?.setFillColor(.white) - maskContext?.fill(CGRect(origin: .zero, size: CGSize(width: info.width, height: info.height))) + maskContext?.fill(info.cgRect) var kernel: CIBlendKernel? = .sourceOver if layer.data.blendMode == 1 { @@ -492,9 +501,7 @@ class Document: NSDocument { if layer.data.blendMode == 14 { kernel = .luminosity } - - Swift.print(layer.name + " is " + kernel!.name) - + if layer.mask != nil { for chunk in layer.mask!.chunks { let x = chunk.x @@ -557,10 +564,10 @@ class Document: NSDocument { let ciImage = CIImage(cgImage: (masterImage)!) let newCiImage = kernel!.apply(foreground: CIImage(cgImage: previousImage!), background: ciImage, colorSpace: info.colorSpace) - masterImage = context.createCGImage(newCiImage!, from: size, format: .RGBA8, colorSpace: info.colorSpace)! + masterImage = context.createCGImage(newCiImage!, from: info.cgRect, format: .RGBA8, colorSpace: info.colorSpace)! } - var image = NSImage(cgImage: masterImage!, size: NSSize(width: info.width, height: info.height)) + var image = NSImage(cgImage: masterImage!, size: info.nsSize) if info.orientation == 3 { image = image.imageRotatedByDegreess(degrees: 90)