From 9ce297bf9a2f38379ad1aaa64d37af377c2f1ed3 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Thu, 16 Sep 2021 18:28:56 -0400 Subject: [PATCH] Properly clear mask image, and load regular layer when a mask is present --- SilicaViewer/Document.swift | 71 ++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/SilicaViewer/Document.swift b/SilicaViewer/Document.swift index b4be7f9..165074e 100644 --- a/SilicaViewer/Document.swift +++ b/SilicaViewer/Document.swift @@ -339,7 +339,10 @@ class Document: NSDocument { let maskBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue).union(.byteOrder16Big) 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))) + if layer.mask != nil { for chunk in layer.mask!.chunks { let x = chunk.x @@ -357,44 +360,46 @@ class Document: NSDocument { maskContext?.draw(chunk.image.cgImage(forProposedRect: nil, context: NSGraphicsContext.current, hints: nil)!, in: rect) } } - } else { - for chunk in layer.data.chunks { - let x = chunk.x - var y = chunk.y - - let (width, height) = getTileSize(x: x, y: y) - - if y == rows { - y = 0 - } + } + + for chunk in layer.data.chunks { + let x = chunk.x + var y = chunk.y - let rect = NSRect(x: info.tileSize * x, y: info.height - (info.tileSize * y), width: width, height: height) - - var op = CGBlendMode.copy - if layer.data.blendMode == 12 { - op = .hardLight - } - - layerContext?.setAlpha(CGFloat(layer.data.opacity)) - layerContext?.setBlendMode(op) - - if !layer.data.hidden { - layerContext?.draw(chunk.image.cgImage(forProposedRect: nil, context: NSGraphicsContext.current, hints: nil)!, in: rect) - } + let (width, height) = getTileSize(x: x, y: y) + + if y == rows { + y = 0 + } + + let rect = NSRect(x: info.tileSize * x, y: info.height - (info.tileSize * y), width: width, height: height) + + var op = CGBlendMode.copy + if layer.data.blendMode == 12 { + op = .hardLight + } + + layerContext?.setAlpha(CGFloat(layer.data.opacity)) + layerContext?.setBlendMode(op) + + if !layer.data.hidden { + layerContext?.draw(chunk.image.cgImage(forProposedRect: nil, context: NSGraphicsContext.current, hints: nil)!, in: rect) } } - - var layerImage = (layerContext?.makeImage())! - if layer.mask != nil { - let maskImage = (maskContext?.makeImage())! - - layerImage = layerImage.masking(maskImage)! - } - + + let layerImage = (layerContext?.makeImage())! + ccgContext?.setAlpha(1.0) ccgContext?.setBlendMode(.sourceAtop) - ccgContext?.draw(layerImage, in: CGRect(x: 0, y: 0, width: info.width, height: info.height)) + if layer.mask != nil { + let maskImage = (maskContext?.makeImage())! + let newImage = layerImage.masking(maskImage)! + + ccgContext?.draw(newImage, in: CGRect(x: 0, y: 0, width: info.width, height: info.height)) + } else { + ccgContext?.draw(layerImage, in: CGRect(x: 0, y: 0, width: info.width, height: info.height)) + } } var image = NSImage(cgImage: (ccgContext?.makeImage())!, size: NSSize(width: info.width, height: info.height))