Skip to content

Commit

Permalink
fix: Convert MBMs to TIFF files (#166)
Browse files Browse the repository at this point in the history
TIFFs support multiple-images which makes them a simpler conversion
target.
  • Loading branch information
jbmorley authored Dec 15, 2024
1 parent 0ea6d14 commit 8cbbc0a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Reconnect/Model/FileReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Foundation

import ReconnectCore

enum FileReference {
enum FileReference: Equatable {

case local(URL)
case remote(FileServer.DirectoryEntry)
Expand Down
12 changes: 5 additions & 7 deletions Reconnect/Model/TransfersModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,19 @@ class TransfersModel {
// Convert known types.
// N.B. This would be better implemented as a user-configurable and extensible pipeline, but this is a
// reasonable point to hook an initial implementation.
var urls: [URL] = [destinationURL]
var url: URL = destinationURL
if convertFiles {
if directoryEntry.fileType == .mbm || directoryEntry.pathExtension.lowercased() == "mbm" {
urls = try PsiLuaEnv().convertMultiBitmap(at: destinationURL, removeSource: true)
url = try PsiLuaEnv().convertMultiBitmap(at: destinationURL, removeSource: true)
}
}

// Get the file details.
let details = try urls.map { url in
let size = try FileManager.default.attributesOfItem(atPath: url.path)[.size] as! UInt64
return Transfer.FileDetails(url: url, size: size)
}
let size = try FileManager.default.attributesOfItem(atPath: url.path)[.size] as! UInt64
let details = Transfer.FileDetails(url: url, size: size)

// Mark the transfer as complete.
transfer.setStatus(.complete(details.first))
transfer.setStatus(.complete(details))
}
transfers.append(download)
try await download.run()
Expand Down
22 changes: 8 additions & 14 deletions ReconnectCore/Sources/ReconnectCore/Extensions/PsiLuaEnv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,21 @@ import OpoLua

extension PsiLuaEnv {

public func convertMultiBitmap(at url: URL, removeSource: Bool = false) throws -> [URL] {
public func convertMultiBitmap(at url: URL, removeSource: Bool = false) throws -> URL {
let directoryURL = (url as NSURL).deletingLastPathComponent!
let basename = (url.lastPathComponent as NSString).deletingPathExtension
let bitmaps = PsiLuaEnv().getMbmBitmaps(path: url.path) ?? []
let urls = try bitmaps.enumerated().map { index, bitmap in
let identifier = if index < 1 {
basename
} else {
"\(basename) \(index)"
}
let conversionURL = directoryURL
.appendingPathComponent(identifier)
.appendingPathExtension("png")
let image = CGImage.from(bitmap: bitmap)
try CGImageWritePNG(image, to: conversionURL)
return conversionURL
let images = bitmaps.map { bitmap in
return CGImage.from(bitmap: bitmap)
}
let conversionURL = directoryURL
.appendingPathComponent(basename)
.appendingPathExtension("tiff")
try CGImageWriteTIFF(destinationURL: conversionURL, images: images)
if removeSource {
try FileManager.default.removeItem(at: url)
}
return urls
return conversionURL
}

}
10 changes: 6 additions & 4 deletions ReconnectCore/Sources/ReconnectCore/Utilities/Graphics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import Foundation
import ImageIO
import UniformTypeIdentifiers

public func CGImageWritePNG(_ image: CGImage, to destinationURL: URL) throws {
public func CGImageWriteTIFF(destinationURL: URL, images: [CGImage]) throws {
guard let destination = CGImageDestinationCreateWithURL(destinationURL as CFURL,
UTType.png.identifier as CFString,
1,
UTType.tiff.identifier as CFString,
images.count,
nil) else {
throw ReconnectError.imageSaveError
}
CGImageDestinationAddImage(destination, image, nil)
for image in images {
CGImageDestinationAddImage(destination, image, nil)
}
guard CGImageDestinationFinalize(destination) else {
throw ReconnectError.imageSaveError
}
Expand Down
2 changes: 1 addition & 1 deletion dependencies/interact

0 comments on commit 8cbbc0a

Please sign in to comment.