2021-09-14 12:24:08 -04:00
|
|
|
import Cocoa
|
|
|
|
import ZIPFoundation
|
|
|
|
import CoreFoundation
|
2021-09-21 04:27:20 -04:00
|
|
|
import Accelerate
|
2021-09-14 12:24:08 -04:00
|
|
|
|
|
|
|
struct SilicaChunk {
|
|
|
|
var x: Int = 0
|
|
|
|
var y: Int = 0
|
2021-09-29 13:02:32 -04:00
|
|
|
var image: CGImage?
|
2021-09-14 12:24:08 -04:00
|
|
|
}
|
|
|
|
|
2021-09-16 18:13:57 -04:00
|
|
|
struct SilicaLayerData {
|
|
|
|
var blendMode: Int = 0
|
2021-09-21 05:36:11 -04:00
|
|
|
var extendedBlend: Int = 0
|
2021-09-15 14:10:07 -04:00
|
|
|
var chunks: [SilicaChunk] = []
|
2021-09-16 18:13:57 -04:00
|
|
|
var opacity: Double = 1.0
|
|
|
|
var hidden: Bool = false
|
|
|
|
}
|
|
|
|
|
|
|
|
struct SilicaLayer {
|
2021-09-27 11:31:19 -04:00
|
|
|
var name: String = ""
|
2021-09-16 18:13:57 -04:00
|
|
|
var data: SilicaLayerData = SilicaLayerData()
|
|
|
|
var mask: SilicaLayerData?
|
2021-09-21 04:27:20 -04:00
|
|
|
var clipped: Bool = false
|
2021-09-14 12:24:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SilicaDocument {
|
|
|
|
var trackedTime: Int = 0
|
|
|
|
var tileSize: Int = 0
|
|
|
|
var orientation: Int = 0
|
|
|
|
var flippedHorizontally: Bool = false
|
|
|
|
var flippedVertically: Bool = false
|
2021-09-20 14:13:58 -04:00
|
|
|
var name: String = ""
|
|
|
|
var authorName: String = ""
|
|
|
|
var strokeCount: Int = 0
|
2021-09-14 12:24:08 -04:00
|
|
|
|
2021-09-27 11:31:19 -04:00
|
|
|
var backgroundColor: CGColor = .white
|
|
|
|
var colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
|
|
|
|
|
2021-09-14 12:24:08 -04:00
|
|
|
var width: Int = 0
|
|
|
|
var height: Int = 0
|
|
|
|
|
|
|
|
var layers: [SilicaLayer] = []
|
2021-09-27 11:38:23 -04:00
|
|
|
|
2021-09-29 17:54:04 -04:00
|
|
|
var videoFrame: (Int, Int) = (0, 0)
|
2021-09-29 17:27:13 -04:00
|
|
|
|
2021-09-27 11:38:23 -04:00
|
|
|
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)
|
|
|
|
}()
|
2021-09-14 12:24:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func objectRefGetValue2(_ objectRef: CFTypeRef) -> UInt32 {
|
|
|
|
let val = unsafeBitCast(objectRef, to: UInt64.self)
|
|
|
|
|
|
|
|
return valueForKeyedArchiverUID(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
class Document: NSDocument {
|
|
|
|
var data: Data? // oh no...
|
|
|
|
|
|
|
|
var dict: NSDictionary?
|
|
|
|
|
|
|
|
var info = SilicaDocument()
|
|
|
|
|
|
|
|
var rows: Int = 0
|
|
|
|
var columns: Int = 0
|
|
|
|
|
|
|
|
var remainderWidth: Int = 0
|
|
|
|
var remainderHeight: Int = 0
|
|
|
|
|
|
|
|
override init() {
|
|
|
|
super.init()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func makeWindowControllers() {
|
|
|
|
let storyboard = NSStoryboard(name: NSStoryboard.Name("Main"), bundle: nil)
|
|
|
|
let windowController = storyboard.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("Document Window Controller")) as! NSWindowController
|
|
|
|
self.addWindowController(windowController)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Pass in an object from the $object array, which always contains a $class key.
|
|
|
|
*/
|
|
|
|
func getDocumentClassName(dict: NSDictionary) -> String? {
|
|
|
|
let objectsArray = self.dict?["$objects"] as! NSArray
|
|
|
|
|
|
|
|
if let value = dict["$class"] {
|
|
|
|
let classObjectId = objectRefGetValue2(value as CFTypeRef)
|
|
|
|
let classObject = objectsArray[Int(classObjectId)] as! NSDictionary
|
|
|
|
|
|
|
|
return classObject["$classname"] as? String
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Returns the correct tile size, taking into account the remainder between tile size and image size.
|
|
|
|
*/
|
2021-09-29 13:02:32 -04:00
|
|
|
func getTileSize(_ x: Int, _ y: Int) -> (Int, Int) {
|
2021-09-14 12:24:08 -04:00
|
|
|
var width: Int = info.tileSize
|
|
|
|
var height: Int = info.tileSize
|
|
|
|
|
|
|
|
if((x + 1) == columns) {
|
|
|
|
width = info.tileSize - remainderWidth
|
|
|
|
}
|
|
|
|
|
|
|
|
if(y == rows) {
|
|
|
|
height = info.tileSize - remainderHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
return (width, height)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Converts a CFKeyedArchiveUID from a NSKeyedArchive to a Int for indexing an array or dictionary.
|
|
|
|
*/
|
|
|
|
func getClassID(id: Any?) -> Int {
|
|
|
|
return Int(objectRefGetValue2(id! as CFTypeRef))
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Parses the chunk filename, ex. 1~1 to integer coordinates.
|
|
|
|
*/
|
|
|
|
func parseChunkFilename(filename: String) -> (Int, Int)? {
|
|
|
|
let pathURL = URL(fileURLWithPath: filename)
|
|
|
|
let pathComponents = pathURL.lastPathComponent.replacingOccurrences(of: ".chunk", with: "").components(separatedBy: "~")
|
|
|
|
|
|
|
|
let x = Int(pathComponents[0])
|
|
|
|
let y = Int(pathComponents[1])
|
|
|
|
|
|
|
|
if x != nil && y != nil {
|
|
|
|
return (x!, y! + 1)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 11:48:16 -04:00
|
|
|
func getChunkRect(_ chunk: SilicaChunk) -> NSRect {
|
|
|
|
let x = chunk.x
|
|
|
|
var y = chunk.y
|
|
|
|
|
2021-09-29 13:02:32 -04:00
|
|
|
let (width, height) = getTileSize(x, y)
|
2021-09-27 11:48:16 -04:00
|
|
|
|
|
|
|
if y == rows {
|
|
|
|
y = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSRect(x: info.tileSize * x, y: info.height - (info.tileSize * y), width: width, height: height)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: convert to switch/case
|
|
|
|
func getBlendKernel(_ layer: SilicaLayer) -> CIBlendKernel? {
|
|
|
|
if layer.data.blendMode == 1 {
|
|
|
|
return .multiply
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 10 {
|
|
|
|
return .colorBurn
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 19 {
|
|
|
|
return .darken
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 8 {
|
|
|
|
return .linearBurn
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 4 {
|
|
|
|
return .lighten
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 2 {
|
|
|
|
return .screen
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 13 {
|
|
|
|
return .hardLight
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 9 {
|
|
|
|
return .colorDodge
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 3 {
|
|
|
|
return .subtract
|
|
|
|
}
|
|
|
|
|
|
|
|
if layer.data.blendMode == 0 && layer.data.blendMode != layer.data.extendedBlend {
|
|
|
|
if layer.data.extendedBlend == 25 {
|
|
|
|
return .darkerColor
|
|
|
|
}
|
|
|
|
if layer.data.extendedBlend == 24 {
|
|
|
|
return .lighterColor
|
|
|
|
}
|
|
|
|
if layer.data.extendedBlend == 21 {
|
|
|
|
return .vividLight
|
|
|
|
}
|
|
|
|
if layer.data.extendedBlend == 22 {
|
|
|
|
return .linearLight
|
|
|
|
}
|
|
|
|
if layer.data.extendedBlend == 23 {
|
|
|
|
return .pinLight
|
|
|
|
}
|
|
|
|
if layer.data.extendedBlend == 20 {
|
|
|
|
return .hardMix
|
|
|
|
}
|
|
|
|
if layer.data.extendedBlend == 26 {
|
|
|
|
return .divide
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if layer.data.blendMode == 11 {
|
|
|
|
return .overlay
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 17 {
|
|
|
|
return .softLight
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 12 {
|
|
|
|
return .hardLight
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 6 {
|
|
|
|
return .difference
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 5 {
|
|
|
|
return .exclusion
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 7 {
|
|
|
|
return .componentAdd
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 15 {
|
|
|
|
return .hue
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 16 {
|
|
|
|
return .saturation
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 13 {
|
|
|
|
return .color
|
|
|
|
}
|
|
|
|
if layer.data.blendMode == 14 {
|
|
|
|
return .luminosity
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:30:57 -04:00
|
|
|
return .sourceOver
|
2021-09-27 11:48:16 -04:00
|
|
|
}
|
|
|
|
|
2021-09-16 18:13:57 -04:00
|
|
|
func parseSilicaLayer(archive: Archive, dict: NSDictionary, isMask: Bool) -> SilicaLayer? {
|
2021-09-14 12:24:08 -04:00
|
|
|
let objectsArray = self.dict?["$objects"] as! NSArray
|
|
|
|
|
|
|
|
if getDocumentClassName(dict: dict) == LayerClassName {
|
|
|
|
var layer = SilicaLayer()
|
2021-09-27 11:31:19 -04:00
|
|
|
|
|
|
|
if let val = dict["name"] {
|
|
|
|
let NameClassID = getClassID(id: val)
|
|
|
|
let NameClass = objectsArray[NameClassID] as! NSString
|
|
|
|
|
|
|
|
layer.name = NameClass as String
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:24:08 -04:00
|
|
|
let UUIDKey = dict["UUID"]
|
|
|
|
let UUIDClassID = getClassID(id: UUIDKey)
|
|
|
|
let UUIDClass = objectsArray[UUIDClassID] as! NSString
|
|
|
|
|
2021-09-16 18:13:57 -04:00
|
|
|
let maskKey = dict["mask"]
|
|
|
|
let maskClassID = getClassID(id: maskKey)
|
|
|
|
let maskClass = objectsArray[maskClassID]
|
|
|
|
|
|
|
|
layer.data.blendMode = (dict["blend"] as? NSNumber)!.intValue
|
2021-09-21 05:36:11 -04:00
|
|
|
layer.data.extendedBlend = (dict["extendedBlend"] as? NSNumber)!.intValue
|
2021-09-16 18:13:57 -04:00
|
|
|
layer.data.opacity = (dict["opacity"] as? NSNumber)!.doubleValue
|
|
|
|
layer.data.hidden = (dict["hidden"] as? Bool)!
|
2021-09-21 04:27:20 -04:00
|
|
|
layer.clipped = (dict["clipped"] as? Bool)!
|
2021-09-21 04:41:05 -04:00
|
|
|
|
2021-09-16 18:13:57 -04:00
|
|
|
if maskClassID != 0 {
|
|
|
|
layer.mask = parseSilicaLayer(archive: archive, dict: maskClass as! NSDictionary, isMask: true)?.data
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:24:08 -04:00
|
|
|
var chunkPaths: [String] = []
|
|
|
|
|
|
|
|
archive.forEach { (entry: Entry) in
|
|
|
|
if entry.path.contains(String(UUIDClass)) {
|
|
|
|
chunkPaths.append(entry.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 18:13:57 -04:00
|
|
|
layer.data.chunks = Array(repeating: SilicaChunk(), count: chunkPaths.count)
|
2021-09-14 12:24:08 -04:00
|
|
|
|
|
|
|
let dispatchGroup = DispatchGroup()
|
|
|
|
let queue = DispatchQueue(label: "imageWork")
|
|
|
|
|
|
|
|
DispatchQueue.concurrentPerform(iterations: chunkPaths.count) { (i: Int) in
|
2021-09-29 13:02:32 -04:00
|
|
|
guard let threadArchive = Archive(data: self.data!, accessMode: .read) else {
|
2021-09-14 12:24:08 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let threadEntry = threadArchive[chunkPaths[i]]
|
|
|
|
|
|
|
|
guard let (x, y) = parseChunkFilename(filename: threadEntry!.path) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-29 13:02:32 -04:00
|
|
|
let (width, height) = getTileSize(x, y)
|
2021-09-16 18:13:57 -04:00
|
|
|
|
2021-09-27 11:48:16 -04:00
|
|
|
let numChannels = isMask ? 1 : 4
|
2021-09-16 18:13:57 -04:00
|
|
|
let byteSize = width * height * numChannels
|
2021-09-14 12:24:08 -04:00
|
|
|
|
|
|
|
let uncompressedMemory = UnsafeMutablePointer<UInt8>.allocate(capacity: byteSize)
|
|
|
|
|
|
|
|
guard let lzoData = readData(archive: threadArchive, entry: threadEntry!) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lzoData.withUnsafeBytes({ (bytes: UnsafeRawBufferPointer) -> Void in
|
|
|
|
var len = lzo_uint(byteSize)
|
|
|
|
|
|
|
|
lzo1x_decompress_safe(bytes.baseAddress!.assumingMemoryBound(to: uint8.self), lzo_uint(lzoData.count), uncompressedMemory, &len, nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
let imageData = Data(bytes: uncompressedMemory, count: byteSize)
|
|
|
|
|
|
|
|
let render: CGColorRenderingIntent = .defaultIntent
|
2021-09-27 11:48:16 -04:00
|
|
|
let rgbColorSpace = isMask ? CGColorSpaceCreateDeviceGray() : info.colorSpace
|
2021-09-16 18:13:57 -04:00
|
|
|
|
2021-09-29 16:30:57 -04:00
|
|
|
let bitmapInfo = CGBitmapInfo(rawValue: (isMask ? CGImageAlphaInfo.none : CGImageAlphaInfo.premultipliedLast).rawValue).union(.byteOrder32Big)
|
2021-09-14 12:24:08 -04:00
|
|
|
let providerRef: CGDataProvider? = CGDataProvider(data: imageData as CFData)
|
|
|
|
|
2021-09-16 18:13:57 -04:00
|
|
|
guard let cgimage = CGImage(width: width, height: height, bitsPerComponent: 8, bitsPerPixel: 8 * numChannels, bytesPerRow: width * numChannels, space: rgbColorSpace, bitmapInfo: bitmapInfo, provider: providerRef!, decode: nil, shouldInterpolate: false, intent: render) else {
|
2021-09-14 12:24:08 -04:00
|
|
|
return
|
|
|
|
}
|
2021-09-29 13:02:32 -04:00
|
|
|
|
2021-09-16 18:41:54 -04:00
|
|
|
queue.async(group: dispatchGroup) {
|
2021-09-29 13:02:32 -04:00
|
|
|
layer.data.chunks[i].image = cgimage
|
2021-09-16 18:13:57 -04:00
|
|
|
layer.data.chunks[i].x = x
|
|
|
|
layer.data.chunks[i].y = y
|
2021-09-14 12:24:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatchGroup.wait()
|
|
|
|
|
2021-09-16 18:13:57 -04:00
|
|
|
return layer
|
2021-09-14 12:24:08 -04:00
|
|
|
}
|
2021-09-16 18:13:57 -04:00
|
|
|
|
|
|
|
return nil
|
2021-09-14 12:24:08 -04:00
|
|
|
}
|
|
|
|
|
2021-09-29 17:27:13 -04:00
|
|
|
func parsePairString(_ str: String) -> (Int, Int)? {
|
|
|
|
let sizeComponents = str.replacingOccurrences(of: "{", with: "").replacingOccurrences(of: "}", with: "").components(separatedBy: ", ")
|
|
|
|
|
|
|
|
if sizeComponents.count == 2 {
|
|
|
|
let width = Int(sizeComponents[0])
|
|
|
|
let height = Int(sizeComponents[1])
|
|
|
|
|
|
|
|
return (width!, height!)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:24:08 -04:00
|
|
|
func parseSilicaDocument(archive: Archive, dict: NSDictionary) {
|
|
|
|
let objectsArray = self.dict?["$objects"] as! NSArray
|
|
|
|
|
|
|
|
if getDocumentClassName(dict: dict) == DocumentClassName {
|
|
|
|
info.trackedTime = (dict[TrackedTimeKey] as! NSNumber).intValue
|
|
|
|
info.tileSize = (dict[TileSizeKey] as! NSNumber).intValue
|
|
|
|
info.orientation = (dict[OrientationKey] as! NSNumber).intValue
|
|
|
|
info.flippedHorizontally = (dict[FlippedHorizontallyKey] as! NSNumber).boolValue
|
|
|
|
info.flippedVertically = (dict[FlippedVerticallyKey] as! NSNumber).boolValue
|
2021-09-29 17:27:13 -04:00
|
|
|
|
|
|
|
let videoResolutionClassKey = dict["SilicaDocumentVideoSegmentInfoKey"]
|
|
|
|
let videoResolutionClassID = getClassID(id: videoResolutionClassKey)
|
|
|
|
let videoResolution = objectsArray[videoResolutionClassID] as! NSDictionary
|
|
|
|
|
|
|
|
let frameSizeClassKey = videoResolution["frameSize"]
|
|
|
|
let frameSizeClassID = getClassID(id: frameSizeClassKey)
|
|
|
|
let frameSize = objectsArray[frameSizeClassID] as! String
|
2021-09-29 17:54:04 -04:00
|
|
|
|
|
|
|
info.videoFrame = parsePairString(frameSize)!
|
2021-09-20 14:13:58 -04:00
|
|
|
|
2021-09-27 11:31:19 -04:00
|
|
|
let colorProfileClassKey = dict["colorProfile"]
|
|
|
|
let colorProfileClassID = getClassID(id: colorProfileClassKey)
|
|
|
|
let colorProfile = objectsArray[colorProfileClassID] as! NSDictionary
|
|
|
|
|
|
|
|
let colorProfileNameClassKey = colorProfile["SiColorProfileArchiveICCNameKey"]
|
|
|
|
let colorProfileNameClassID = getClassID(id: colorProfileNameClassKey)
|
|
|
|
let colorProfileName = objectsArray[colorProfileNameClassID] as! NSString
|
|
|
|
|
|
|
|
// we only support the basic "Display P3" color space... does Procreate actually store the ICC data??
|
|
|
|
if colorProfileName == "Display P3" {
|
|
|
|
info.colorSpace = CGColorSpace(name: CGColorSpace.displayP3)!
|
|
|
|
}
|
|
|
|
|
|
|
|
let backgroundClassKey = dict["backgroundColor"]
|
|
|
|
let backgroundClassID = getClassID(id: backgroundClassKey)
|
|
|
|
let background = objectsArray[backgroundClassID] as! NSData
|
|
|
|
|
|
|
|
var backgroundArray: [Float] = [0.0, 0.0, 0.0, 0.0]
|
|
|
|
|
|
|
|
background.getBytes(&backgroundArray, length: 16)
|
|
|
|
let backgroundCgArray: [CGFloat] = [CGFloat(backgroundArray[0]), CGFloat(backgroundArray[1]), CGFloat(backgroundArray[2]), CGFloat(backgroundArray[3])]
|
|
|
|
|
|
|
|
info.backgroundColor = CGColor(colorSpace: info.colorSpace, components: backgroundCgArray)!
|
|
|
|
|
2021-09-20 14:13:58 -04:00
|
|
|
let strokeClassKey = dict[StrokeCountKey]
|
|
|
|
let strokeClassID = getClassID(id: strokeClassKey)
|
|
|
|
let strokeCount = objectsArray[strokeClassID] as! NSNumber
|
|
|
|
|
2021-09-21 04:27:20 -04:00
|
|
|
info.strokeCount = Int(truncating: strokeCount)
|
2021-09-20 14:13:58 -04:00
|
|
|
|
|
|
|
let nameClassKey = dict[NameKey]
|
|
|
|
let nameClassID = getClassID(id: nameClassKey)
|
|
|
|
let nameString = objectsArray[nameClassID] as! String
|
|
|
|
|
|
|
|
info.name = nameString
|
|
|
|
|
|
|
|
let authorClassKey = dict[AuthorNameKey]
|
|
|
|
let authorClassID = getClassID(id: authorClassKey)
|
|
|
|
let authorString = objectsArray[authorClassID] as! String
|
|
|
|
|
|
|
|
if authorString != "$null" {
|
|
|
|
info.authorName = authorString
|
|
|
|
}
|
2021-09-14 12:24:08 -04:00
|
|
|
|
|
|
|
let sizeClassKey = dict[SizeKey]
|
|
|
|
let sizeClassID = getClassID(id: sizeClassKey)
|
|
|
|
let sizeString = objectsArray[sizeClassID] as! String
|
|
|
|
|
2021-09-29 17:54:04 -04:00
|
|
|
let (width, height) = parsePairString(sizeString)!
|
|
|
|
info.width = width
|
|
|
|
info.height = height
|
2021-09-14 12:24:08 -04:00
|
|
|
|
|
|
|
columns = Int(ceil(Float(info.width) / Float(info.tileSize)))
|
2021-09-16 18:41:54 -04:00
|
|
|
rows = Int(ceil(Float(info.height) / Float(info.tileSize))) + 1 // TODO: lol why
|
2021-09-14 12:24:08 -04:00
|
|
|
|
|
|
|
if info.width % info.tileSize != 0 {
|
|
|
|
remainderWidth = (columns * info.tileSize) - info.width
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.height % info.tileSize != 0 {
|
|
|
|
remainderHeight = (rows * info.tileSize) - info.height
|
|
|
|
}
|
|
|
|
|
|
|
|
let layersClassKey = dict[LayersKey]
|
|
|
|
let layersClassID = getClassID(id: layersClassKey)
|
|
|
|
let layersClass = objectsArray[layersClassID] as! NSDictionary
|
|
|
|
|
|
|
|
let array = layersClass["NS.objects"] as! NSArray
|
|
|
|
|
2021-09-27 11:31:19 -04:00
|
|
|
//dump(dict, indent: 5)
|
|
|
|
|
2021-09-14 12:24:08 -04:00
|
|
|
for object in array {
|
|
|
|
let layerClassID = getClassID(id: object)
|
|
|
|
let layerClass = objectsArray[layerClassID] as! NSDictionary
|
|
|
|
|
2021-09-16 18:13:57 -04:00
|
|
|
guard let layer = parseSilicaLayer(archive: archive, dict: layerClass, isMask: false) else { return }
|
|
|
|
info.layers.append(layer)
|
2021-09-14 12:24:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseDocument(archive: Archive, dict: NSDictionary) {
|
|
|
|
// double check if this archive is really correct
|
|
|
|
if let value = dict["$version"] {
|
|
|
|
if (value as! Int) != NSKeyedArchiveVersion {
|
|
|
|
Swift.print("This is not a valid document!")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.dict = dict
|
|
|
|
|
|
|
|
let objectsArray = dict["$objects"] as! NSArray
|
|
|
|
|
|
|
|
// let's read the $top class, which is always going to be SilicaDocument type.
|
|
|
|
let topObject = dict["$top"] as! NSDictionary
|
|
|
|
let topClassID = objectRefGetValue2(topObject["root"] as CFTypeRef)
|
|
|
|
let topObjectClass = objectsArray[Int(topClassID)] as! NSDictionary
|
|
|
|
|
|
|
|
parseSilicaDocument(archive: archive, dict: topObjectClass)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override func read(from data: Data, ofType typeName: String) throws {
|
|
|
|
self.data = data
|
|
|
|
|
2021-09-15 14:10:07 -04:00
|
|
|
guard let archive = Archive(data: data, accessMode: Archive.AccessMode.read) else {
|
2021-09-14 12:24:08 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let documentEntry = archive[DocumentArchivePath] else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let documentData = readData(archive: archive, entry: documentEntry) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var plistFormat = PropertyListSerialization.PropertyListFormat.binary
|
|
|
|
guard let propertyList = try? PropertyListSerialization.propertyList(from: documentData, options: [], format: &plistFormat) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
parseDocument(archive: archive, dict: propertyList as! NSDictionary)
|
|
|
|
}
|
|
|
|
|
2021-09-21 04:41:05 -04:00
|
|
|
func makeComposite() -> NSImage? {
|
2021-09-16 18:13:57 -04:00
|
|
|
// create the final composite output image
|
|
|
|
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue).union(.byteOrder32Big)
|
|
|
|
|
2021-09-27 11:31:19 -04:00
|
|
|
let ccgContext = CGContext(data: nil, width: info.width, height: info.height, bitsPerComponent: 8, bytesPerRow: info.width * 4, space: info.colorSpace, bitmapInfo: bitmapInfo.rawValue)
|
2021-09-14 12:24:08 -04:00
|
|
|
|
2021-09-27 11:31:19 -04:00
|
|
|
ccgContext?.setFillColor(info.backgroundColor)
|
2021-09-27 11:38:23 -04:00
|
|
|
ccgContext?.fill(info.cgRect)
|
2021-09-27 11:31:19 -04:00
|
|
|
|
|
|
|
let context = CIContext()
|
2021-09-29 13:02:32 -04:00
|
|
|
var masterImage = CIImage(cgImage: (ccgContext?.makeImage())!)
|
|
|
|
|
|
|
|
var previousImage: CGImage? = nil
|
2021-09-21 04:27:20 -04:00
|
|
|
|
2021-09-27 11:31:19 -04:00
|
|
|
for layer in info.layers.reversed() {
|
2021-09-29 13:02:32 -04:00
|
|
|
if !layer.data.hidden {
|
|
|
|
// start by creating a new layer composite image, needed for image masking
|
|
|
|
let layerContext = CGContext(data: nil, width: info.width, height: info.height, bitsPerComponent: 8, bytesPerRow: info.width * 4, space: info.colorSpace, bitmapInfo: bitmapInfo.rawValue)
|
|
|
|
|
|
|
|
layerContext?.clear(info.cgRect)
|
|
|
|
|
|
|
|
var maskContext: CGContext?
|
|
|
|
|
|
|
|
let kernel = getBlendKernel(layer)
|
2021-09-29 16:30:57 -04:00
|
|
|
|
2021-09-29 13:02:32 -04:00
|
|
|
if layer.mask != nil {
|
|
|
|
let grayColorSpace = CGColorSpaceCreateDeviceGray()
|
|
|
|
let maskBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue).union(.byteOrder16Big)
|
|
|
|
|
|
|
|
maskContext = CGContext(data: nil, width: info.width, height: info.height, bitsPerComponent: 16, bytesPerRow: 0, space: grayColorSpace, bitmapInfo: maskBitmapInfo.rawValue)
|
|
|
|
|
|
|
|
maskContext?.setFillColor(.white)
|
|
|
|
maskContext?.fill(info.cgRect)
|
2021-09-16 18:28:56 -04:00
|
|
|
|
2021-09-29 13:02:32 -04:00
|
|
|
for chunk in layer.mask!.chunks {
|
|
|
|
maskContext?.draw(chunk.image!, in: getChunkRect(chunk))
|
2021-09-16 18:13:57 -04:00
|
|
|
}
|
2021-09-14 12:24:08 -04:00
|
|
|
}
|
2021-09-16 18:28:56 -04:00
|
|
|
|
2021-09-29 13:02:32 -04:00
|
|
|
for chunk in layer.data.chunks {
|
|
|
|
layerContext?.setAlpha(CGFloat(layer.data.opacity))
|
|
|
|
layerContext?.setBlendMode(.normal)
|
|
|
|
|
|
|
|
if !layer.data.hidden {
|
|
|
|
layerContext?.draw(chunk.image!, in: getChunkRect(chunk))
|
|
|
|
}
|
2021-09-16 18:13:57 -04:00
|
|
|
}
|
2021-09-16 18:28:56 -04:00
|
|
|
|
2021-09-29 13:02:32 -04:00
|
|
|
let layerImage = layerContext?.makeImage()
|
2021-09-21 04:41:05 -04:00
|
|
|
|
2021-09-29 13:02:32 -04:00
|
|
|
if layer.clipped && previousImage != nil {
|
|
|
|
let result = previousImage!.toGrayscale()
|
|
|
|
let newImage = layerImage!.masking(result!)
|
2021-09-27 12:23:18 -04:00
|
|
|
|
2021-09-29 13:02:32 -04:00
|
|
|
previousImage = newImage
|
|
|
|
} else if layer.mask != nil && maskContext != nil {
|
|
|
|
let maskImage = (maskContext?.makeImage())!
|
|
|
|
let newImage = layerImage!.masking(maskImage)!
|
|
|
|
|
|
|
|
previousImage = newImage
|
|
|
|
} else {
|
|
|
|
previousImage = layerImage
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply image
|
|
|
|
masterImage = kernel!.apply(foreground: CIImage(cgImage: previousImage!), background: masterImage, colorSpace: info.colorSpace)!
|
|
|
|
}
|
2021-09-14 12:24:08 -04:00
|
|
|
}
|
|
|
|
|
2021-09-29 13:02:32 -04:00
|
|
|
let cgImage = context.createCGImage(masterImage, from: info.cgRect, format: .RGBA8, colorSpace: info.colorSpace)!
|
|
|
|
var image = NSImage(cgImage: cgImage, size: info.nsSize)
|
2021-09-14 12:24:08 -04:00
|
|
|
|
|
|
|
if info.orientation == 3 {
|
|
|
|
image = image.imageRotatedByDegreess(degrees: 90)
|
|
|
|
} else if info.orientation == 4 {
|
|
|
|
image = image.imageRotatedByDegreess(degrees: -90)
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.flippedHorizontally && (info.orientation == 1 || info.orientation == 2) {
|
|
|
|
image = image.flipHorizontally()
|
|
|
|
} else if info.flippedHorizontally && (info.orientation == 3 || info.orientation == 4) {
|
|
|
|
image = image.flipVertically()
|
|
|
|
} else if info.flippedVertically && (info.orientation == 1 || info.orientation == 2) {
|
|
|
|
image = image.flipVertically()
|
|
|
|
} else if !info.flippedVertically && (info.orientation == 3 || info.orientation == 4) {
|
|
|
|
image = image.flipHorizontally()
|
|
|
|
}
|
|
|
|
|
|
|
|
return image
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeThumbnail() -> NSImage? {
|
|
|
|
guard let archive = Archive(data: data!, accessMode: Archive.AccessMode.read) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let entry = archive[ThumbnailPath] else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let thumbnailData = readData(archive: archive, entry: entry) else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSImage(data: thumbnailData)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public extension NSImage {
|
|
|
|
func imageRotatedByDegreess(degrees:CGFloat) -> NSImage {
|
|
|
|
var imageBounds = NSMakeRect(0.0, 0.0, size.width, size.height)
|
|
|
|
|
|
|
|
let pathBounds = NSBezierPath(rect: imageBounds)
|
|
|
|
var transform = NSAffineTransform()
|
|
|
|
transform.rotate(byDegrees: degrees)
|
|
|
|
pathBounds.transform(using: transform as AffineTransform)
|
|
|
|
|
|
|
|
let rotatedBounds:NSRect = NSMakeRect(NSZeroPoint.x, NSZeroPoint.y, pathBounds.bounds.size.width, pathBounds.bounds.size.height )
|
|
|
|
let rotatedImage = NSImage(size: rotatedBounds.size)
|
|
|
|
|
|
|
|
imageBounds.origin.x = NSMidX(rotatedBounds) - (NSWidth(imageBounds) / 2)
|
|
|
|
imageBounds.origin.y = NSMidY(rotatedBounds) - (NSHeight(imageBounds) / 2)
|
|
|
|
|
|
|
|
transform = NSAffineTransform()
|
|
|
|
transform.translateX(by: +(NSWidth(rotatedBounds) / 2 ), yBy: +(NSHeight(rotatedBounds) / 2))
|
|
|
|
transform.rotate(byDegrees: degrees)
|
|
|
|
transform.translateX(by: -(NSWidth(rotatedBounds) / 2 ), yBy: -(NSHeight(rotatedBounds) / 2))
|
|
|
|
|
|
|
|
rotatedImage.lockFocus()
|
|
|
|
transform.concat()
|
|
|
|
|
|
|
|
self.draw(in: imageBounds, from: .zero, operation: .copy, fraction: 1.0)
|
|
|
|
|
|
|
|
rotatedImage.unlockFocus()
|
|
|
|
|
|
|
|
return rotatedImage
|
|
|
|
}
|
|
|
|
|
|
|
|
func flipHorizontally() -> NSImage {
|
|
|
|
let flipedImage = NSImage(size: size)
|
|
|
|
flipedImage.lockFocus()
|
|
|
|
|
|
|
|
let transform = NSAffineTransform()
|
|
|
|
transform.translateX(by: size.width, yBy: 0.0)
|
|
|
|
transform.scaleX(by: -1.0, yBy: 1.0)
|
|
|
|
transform.concat()
|
|
|
|
|
|
|
|
let rect = NSMakeRect(0, 0, size.width, size.height)
|
|
|
|
self.draw(at: .zero, from: rect, operation: .sourceOver, fraction: 1.0)
|
|
|
|
|
|
|
|
flipedImage.unlockFocus()
|
|
|
|
|
|
|
|
return flipedImage
|
|
|
|
}
|
|
|
|
|
|
|
|
func flipVertically() -> NSImage {
|
|
|
|
let flipedImage = NSImage(size: size)
|
|
|
|
flipedImage.lockFocus()
|
|
|
|
|
|
|
|
let transform = NSAffineTransform()
|
|
|
|
transform.translateX(by: 0.0, yBy: size.height)
|
|
|
|
transform.scaleX(by: 1.0, yBy: -1.0)
|
|
|
|
transform.concat()
|
|
|
|
|
|
|
|
let rect = NSMakeRect(0, 0, size.width, size.height)
|
|
|
|
self.draw(at: .zero, from: rect, operation: .sourceOver, fraction: 1.0)
|
|
|
|
|
|
|
|
flipedImage.unlockFocus()
|
|
|
|
|
|
|
|
return flipedImage
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 04:41:05 -04:00
|
|
|
|
|
|
|
public extension CGImage {
|
|
|
|
func toGrayscale() -> CGImage? {
|
2021-09-27 12:23:18 -04:00
|
|
|
let ciImage = CIImage(cgImage: self)
|
|
|
|
|
|
|
|
let filter = CIFilter(name: "CIColorControls")
|
|
|
|
filter?.setValue(ciImage, forKey: kCIInputImageKey)
|
|
|
|
filter?.setValue(5.0, forKey: kCIInputBrightnessKey)
|
|
|
|
filter?.setValue(0.0, forKey: kCIInputSaturationKey)
|
|
|
|
filter?.setValue(1.1, forKey: kCIInputContrastKey)
|
|
|
|
|
|
|
|
guard let intermediateImage = filter?.outputImage else {
|
2021-09-21 04:41:05 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 12:23:18 -04:00
|
|
|
guard let image = CIContext().createCGImage(intermediateImage, from: CGRect(origin: .zero, size: CGSize(width: self.width, height: self.height))) else {
|
|
|
|
return nil
|
2021-09-21 04:41:05 -04:00
|
|
|
}
|
|
|
|
|
2021-09-27 12:23:18 -04:00
|
|
|
let grayColorSpace = CGColorSpaceCreateDeviceGray()
|
|
|
|
let maskBitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue).union(.byteOrder16Big)
|
2021-09-21 04:41:05 -04:00
|
|
|
|
2021-09-27 12:23:18 -04:00
|
|
|
let maskContext = CGContext(data: nil, width: self.width, height: self.height, bitsPerComponent: 16, bytesPerRow: 0, space: grayColorSpace, bitmapInfo: maskBitmapInfo.rawValue)
|
|
|
|
|
|
|
|
maskContext?.setFillColor(.black)
|
|
|
|
maskContext?.fill(CGRect(origin: .zero, size: CGSize(width: self.width, height: self.height)))
|
|
|
|
|
|
|
|
maskContext?.draw(image, in: CGRect(origin: .zero, size: CGSize(width: self.width, height: self.height)))
|
|
|
|
|
|
|
|
return maskContext?.makeImage()
|
2021-09-21 04:41:05 -04:00
|
|
|
}
|
|
|
|
}
|