Expand DICOM decoding and Enhanced CT support
This commit is contained in:
1 parent
c7f9af8025
commit
6b95fbb1fc
9 files changed
+508
-96
No files matched your search
@@ -10,6 +10,7 @@ struct PatientWorkspace: View {
|
||||
@State private var editor: PatientEditor?
|
||||
@State private var importPatientID: PatientRecord.ID?
|
||||
@State private var showImporter = false
|
||||
@State private var isImporting = false
|
||||
@State private var errorMessage: String?
|
||||
|
||||
private var filteredPatients: [PatientRecord] {
|
||||
@@ -96,8 +97,16 @@ struct PatientWorkspace: View {
|
||||
patientID = id
|
||||
}
|
||||
}
|
||||
.fileImporter(isPresented: $showImporter, allowedContentTypes: [.data], allowsMultipleSelection: true,
|
||||
onCompletion: importImages)
|
||||
.fileImporter(isPresented: $showImporter, allowedContentTypes: [.data, .folder], allowsMultipleSelection: true,
|
||||
onCompletion: { result in
|
||||
guard let patient = importPatientID else { return }
|
||||
importPatientID = nil
|
||||
isImporting = true
|
||||
Task {
|
||||
defer { isImporting = false }
|
||||
await importImages(result, for: patient)
|
||||
}
|
||||
})
|
||||
.alert("Ошибка", isPresented: Binding(
|
||||
get: { errorMessage != nil },
|
||||
set: { if !$0 { errorMessage = nil } }
|
||||
@@ -123,6 +132,10 @@ struct PatientWorkspace: View {
|
||||
Label("Добавить снимки…", systemImage: "plus")
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
.disabled(isImporting)
|
||||
if isImporting {
|
||||
ProgressView().controlSize(.small).accessibilityLabel("Импорт снимков")
|
||||
}
|
||||
}
|
||||
.padding(18)
|
||||
Divider()
|
||||
@@ -146,7 +159,7 @@ struct PatientWorkspace: View {
|
||||
Divider()
|
||||
Group {
|
||||
if selected.dicomCount != nil {
|
||||
DICOMViewer(urls: library.dicomURLs(for: selected))
|
||||
DICOMViewer(urls: library.dicomURLs(for: selected), name: selected.name)
|
||||
} else {
|
||||
ImageViewer(image: selected, url: library.url(for: selected)) { message in
|
||||
errorMessage = message
|
||||
@@ -169,26 +182,27 @@ struct PatientWorkspace: View {
|
||||
showImporter = true
|
||||
}
|
||||
|
||||
private func importImages(_ result: Result<[URL], Error>) {
|
||||
guard let importPatientID else { return }
|
||||
self.importPatientID = nil
|
||||
private func importImages(_ result: Result<[URL], Error>, for importPatientID: UUID) async {
|
||||
do {
|
||||
var errors: [String] = []
|
||||
var firstImported: UUID?
|
||||
let urls = try result.get()
|
||||
let dicom = Set(urls.filter { url in
|
||||
let access = url.startAccessingSecurityScopedResource()
|
||||
defer { if access { url.stopAccessingSecurityScopedResource() } }
|
||||
return ["dcm", "dicom"].contains(url.pathExtension.lowercased()) || DICOMImage.recognizes(url)
|
||||
})
|
||||
if !dicom.isEmpty {
|
||||
let selection = try result.get()
|
||||
let accessed = selection.filter { $0.startAccessingSecurityScopedResource() }
|
||||
defer { accessed.forEach { $0.stopAccessingSecurityScopedResource() } }
|
||||
let files = await Task.detached(priority: .userInitiated) {
|
||||
ImageImportSelection(urls: selection)
|
||||
}.value
|
||||
errors.append(contentsOf: files.errors)
|
||||
|
||||
for series in files.series {
|
||||
do {
|
||||
firstImported = try library.importDICOM(from: urls.filter { dicom.contains($0) }, for: importPatientID).id
|
||||
let image = try await library.importDICOM(from: series, for: importPatientID)
|
||||
firstImported = firstImported ?? image.id
|
||||
} catch {
|
||||
errors.append(error.localizedDescription)
|
||||
errors.append("\(series[0].lastPathComponent) — \(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
for url in urls where !dicom.contains(url) {
|
||||
for url in files.images {
|
||||
do {
|
||||
let image = try library.importImage(from: url, for: importPatientID)
|
||||
firstImported = firstImported ?? image.id
|
||||
@@ -199,6 +213,9 @@ struct PatientWorkspace: View {
|
||||
if patientID == importPatientID, let firstImported {
|
||||
imageID = firstImported
|
||||
}
|
||||
if files.images.isEmpty, files.series.isEmpty {
|
||||
errors.append("В выбранных папках нет поддерживаемых снимков.")
|
||||
}
|
||||
if !errors.isEmpty { errorMessage = errors.joined(separator: "\n") }
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
@@ -221,6 +238,57 @@ struct PatientWorkspace: View {
|
||||
}
|
||||
}
|
||||
|
||||
private struct ImageImportSelection {
|
||||
let series: [[URL]]
|
||||
let images: [URL]
|
||||
let errors: [String]
|
||||
|
||||
init(urls: [URL]) {
|
||||
let keys: Set<URLResourceKey> = [.isDirectoryKey, .isRegularFileKey]
|
||||
var candidates: [(url: URL, direct: Bool)] = []
|
||||
var errors: [String] = []
|
||||
for url in urls {
|
||||
if url.lastPathComponent.uppercased() == "DICOMDIR" {
|
||||
do {
|
||||
candidates.append(contentsOf: try DICOMImage.directoryReferences(url).map { ($0, true) })
|
||||
} catch {
|
||||
errors.append("DICOMDIR — \(error.localizedDescription)")
|
||||
}
|
||||
} else if (try? url.resourceValues(forKeys: keys).isDirectory) == true {
|
||||
if let items = FileManager.default.enumerator(at: url, includingPropertiesForKeys: Array(keys),
|
||||
options: [.skipsHiddenFiles, .skipsPackageDescendants]) {
|
||||
for case let item as URL in items {
|
||||
if (try? item.resourceValues(forKeys: keys).isRegularFile) == true {
|
||||
candidates.append((item, false))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
candidates.append((url, true))
|
||||
}
|
||||
}
|
||||
|
||||
var groups: [String: [URL]] = [:]
|
||||
var groupOrder: [String] = []
|
||||
var images: [URL] = []
|
||||
var seen = Set<URL>()
|
||||
for (url, direct) in candidates where seen.insert(url.standardizedFileURL).inserted {
|
||||
if url.lastPathComponent.uppercased() == "DICOMDIR" { continue }
|
||||
let ext = url.pathExtension.lowercased()
|
||||
if ["dcm", "dicom"].contains(ext) || DICOMImage.recognizes(url) {
|
||||
let key = DICOMImage.stackSeriesIdentifier(url) ?? UUID().uuidString
|
||||
if groups[key] == nil { groupOrder.append(key) }
|
||||
groups[key, default: []].append(url)
|
||||
} else if direct || UTType(filenameExtension: ext)?.conforms(to: .image) == true {
|
||||
images.append(url)
|
||||
}
|
||||
}
|
||||
series = groupOrder.compactMap { groups[$0] }
|
||||
self.images = images
|
||||
self.errors = errors
|
||||
}
|
||||
}
|
||||
|
||||
private struct PatientEditor: Identifiable {
|
||||
let id = UUID()
|
||||
let patientID: UUID?
|
||||
|
||||
Reference in new issue
Block a user