Concurrently process reading chunk data
This commit is contained in:
parent
338cec2076
commit
2134cbc6a8
1 changed files with 51 additions and 34 deletions
|
@ -19,6 +19,8 @@ func objectRefGetValue(_ objectRef : CFTypeRef) -> UInt32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
class Document: NSDocument {
|
class Document: NSDocument {
|
||||||
|
var data: Data? // oh no...
|
||||||
|
|
||||||
var dict: NSDictionary?
|
var dict: NSDictionary?
|
||||||
|
|
||||||
let DocumentClassName = "SilicaDocument"
|
let DocumentClassName = "SilicaDocument"
|
||||||
|
@ -64,45 +66,58 @@ class Document: NSDocument {
|
||||||
if getDocumentClassName(dict: dict) == LayerClassName {
|
if getDocumentClassName(dict: dict) == LayerClassName {
|
||||||
var layer = SilicaLayer()
|
var layer = SilicaLayer()
|
||||||
|
|
||||||
dump(dict, maxDepth: 2)
|
|
||||||
|
|
||||||
let UUIDKey = dict["UUID"]
|
let UUIDKey = dict["UUID"]
|
||||||
let UUIDClassID = objectRefGetValue(UUIDKey as CFTypeRef)
|
let UUIDClassID = objectRefGetValue(UUIDKey as CFTypeRef)
|
||||||
let UUIDClass = objectsArray[Int(UUIDClassID)] as! NSString
|
let UUIDClass = objectsArray[Int(UUIDClassID)] as! NSString
|
||||||
|
|
||||||
|
var chunkPaths: [Entry] = []
|
||||||
|
|
||||||
archive.forEach { (entry: Entry) in
|
archive.forEach { (entry: Entry) in
|
||||||
if entry.path.contains(String(UUIDClass)) {
|
if entry.path.contains(String(UUIDClass)) {
|
||||||
var lzo_data = Data()
|
chunkPaths.append(entry)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
do {
|
layer.chunks = Array(repeating: NSImage(), count: chunkPaths.count)
|
||||||
try archive.extract(entry, consumer: { (d) in
|
|
||||||
lzo_data.append(d)
|
|
||||||
})
|
|
||||||
} catch {
|
|
||||||
Swift.print("Extracting entry from archive failed with error:\(error)")
|
|
||||||
}
|
|
||||||
|
|
||||||
let uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: info.tileSize * info.tileSize * 4)
|
DispatchQueue.concurrentPerform(iterations: chunkPaths.count) { (i: Int) in
|
||||||
|
let entry = chunkPaths[i]
|
||||||
|
|
||||||
lzo_data.withUnsafeBytes({ (bytes: UnsafeRawBufferPointer) -> Void in
|
guard let archive = Archive(data: self.data!, accessMode: Archive.AccessMode.read) else {
|
||||||
var len = lzo_uint(info.tileSize * info.tileSize * 4)
|
return
|
||||||
|
}
|
||||||
|
|
||||||
lzo1x_decompress_safe(bytes.baseAddress!.assumingMemoryBound(to: uint8.self), lzo_uint(lzo_data.count), uint8Pointer, &len, nil)
|
var lzo_data = Data()
|
||||||
|
|
||||||
|
do {
|
||||||
|
try archive.extract(entry, consumer: { (d) in
|
||||||
|
lzo_data.append(d)
|
||||||
})
|
})
|
||||||
|
} catch {
|
||||||
|
Swift.print("Extracting entry from archive failed with error:\(error)")
|
||||||
|
}
|
||||||
|
|
||||||
let image_data = Data(bytes: uint8Pointer, count: info.tileSize * info.tileSize * 4)
|
let uint8Pointer = UnsafeMutablePointer<UInt8>.allocate(capacity: info.tileSize * info.tileSize * 4)
|
||||||
|
|
||||||
let render: CGColorRenderingIntent = CGColorRenderingIntent.defaultIntent
|
lzo_data.withUnsafeBytes({ (bytes: UnsafeRawBufferPointer) -> Void in
|
||||||
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
|
var len = lzo_uint(info.tileSize * info.tileSize * 4)
|
||||||
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue)
|
|
||||||
.union(.byteOrder32Little)
|
|
||||||
let providerRef: CGDataProvider? = CGDataProvider(data: image_data as CFData)
|
|
||||||
|
|
||||||
let cgimage: CGImage? = CGImage(width: info.tileSize, height: info.tileSize, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: info.tileSize * 4, space: rgbColorSpace, bitmapInfo: bitmapInfo, provider: providerRef!, decode: nil, shouldInterpolate: true, intent: render)
|
lzo1x_decompress_safe(bytes.baseAddress!.assumingMemoryBound(to: uint8.self), lzo_uint(lzo_data.count), uint8Pointer, &len, nil)
|
||||||
if cgimage != nil {
|
})
|
||||||
let image = NSImage(cgImage: cgimage!, size: NSZeroSize)
|
|
||||||
layer.chunks.append(image)
|
let image_data = Data(bytes: uint8Pointer, count: info.tileSize * info.tileSize * 4)
|
||||||
}
|
|
||||||
|
let render: CGColorRenderingIntent = CGColorRenderingIntent.defaultIntent
|
||||||
|
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
|
||||||
|
let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedFirst.rawValue)
|
||||||
|
.union(.byteOrder32Little)
|
||||||
|
let providerRef: CGDataProvider? = CGDataProvider(data: image_data as CFData)
|
||||||
|
|
||||||
|
let cgimage: CGImage? = CGImage(width: info.tileSize, height: info.tileSize, bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: info.tileSize * 4, space: rgbColorSpace, bitmapInfo: bitmapInfo, provider: providerRef!, decode: nil, shouldInterpolate: true, intent: render)
|
||||||
|
if cgimage != nil {
|
||||||
|
let image = NSImage(cgImage: cgimage!, size: NSZeroSize)
|
||||||
|
|
||||||
|
layer.chunks[i] = image
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,6 +168,8 @@ class Document: NSDocument {
|
||||||
}
|
}
|
||||||
|
|
||||||
override func read(from data: Data, ofType typeName: String) throws {
|
override func read(from data: Data, ofType typeName: String) throws {
|
||||||
|
self.data = data
|
||||||
|
|
||||||
guard let archive = Archive(data: data, accessMode: Archive.AccessMode.read) else {
|
guard let archive = Archive(data: data, accessMode: Archive.AccessMode.read) else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue