1
Fork 0

Properly clear mask image, and load regular layer when a mask is present

This commit is contained in:
Joshua Goins 2021-09-16 18:28:56 -04:00
parent af6890dca8
commit 9ce297bf9a

View file

@ -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))