Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
a09e28a
Add DICOM C-GET, C-MOVE and DICOMWeb support
ThalesMMS Sep 6, 2025
64ac93b
Add advanced DICOM image rendering with window/level and inversion
ThalesMMS Sep 7, 2025
a2256f1
References
ThalesMMS Sep 7, 2025
c3e2bc7
feat: add ROI measurement tooling and tests
ThalesMMS Sep 7, 2025
828bdfd
Merge pull request #2 from ThalesMMS/codex/move-roimeasurementservice…
ThalesMMS Sep 7, 2025
219602f
Add async DicomTool utilities
ThalesMMS Sep 7, 2025
0e4805d
Merge pull request #3 from ThalesMMS/codex/refactor-dicomtool-for-asy…
ThalesMMS Sep 7, 2025
5ed1312
Add window level calculator with modality presets and conversions
ThalesMMS Sep 7, 2025
69f1bda
Merge pull request #4 from ThalesMMS/codex/add-window-level-calculato…
ThalesMMS Sep 7, 2025
ed731ce
Add concurrent 16-bit image processing fallback
ThalesMMS Sep 7, 2025
ebee1ce
Merge pull request #5 from ThalesMMS/codex/extend-createimage16-for-l…
ThalesMMS Sep 7, 2025
8d379b7
feat: reuse drawing context and cache processed image
ThalesMMS Sep 7, 2025
99f64bf
Merge branch 'pr/dcm-swift_new' into codex/add-caching-and-context-re…
ThalesMMS Sep 7, 2025
6118e9e
Merge pull request #6 from ThalesMMS/codex/add-caching-and-context-re…
ThalesMMS Sep 7, 2025
dad0061
Optimize pixel processing and translate comments
ThalesMMS Sep 7, 2025
9355b8c
Merge pull request #8 from ThalesMMS/codex/optimize-image-processing-…
ThalesMMS Sep 7, 2025
a5c0d31
Add optimization review doc and refactor DicomTool
ThalesMMS Sep 7, 2025
8b4a8de
Refactor image pipeline and add Metal GPU acceleration
ThalesMMS Sep 9, 2025
7a44199
Fix C-FIND PDU assembly and text padding
ThalesMMS Sep 9, 2025
bd0f798
Merge pull request #9 from ThalesMMS/codex/corrigir-serializacao-do-p…
ThalesMMS Sep 9, 2025
13b7f03
Optimize Metal pipeline for parallel processing
ThalesMMS Sep 9, 2025
fb6ceb2
Merge pull request #10 from ThalesMMS/codex/optimize-metal-implementa…
ThalesMMS Sep 9, 2025
0160f6d
Improve DICOM networking and add test scripts
ThalesMMS Sep 9, 2025
f2d44f0
Fix dataset flags and group length for C-MOVE and C-GET RQ
ThalesMMS Sep 9, 2025
e7267ae
Merge pull request #11 from ThalesMMS/codex/verify-dicom-network-oper…
ThalesMMS Sep 9, 2025
eee535b
Reduce LUT allocations and streamline Metal windowing
ThalesMMS Sep 9, 2025
005c3a2
Merge branch 'pr/dcm-swift_new' into codex/optimize-metal-implementat…
ThalesMMS Sep 9, 2025
754ff37
Merge pull request #12 from ThalesMMS/codex/optimize-metal-implementa…
ThalesMMS Sep 9, 2025
7a1c44d
References
ThalesMMS Sep 9, 2025
88ba192
Use memory-mapped Data for input streams
ThalesMMS Sep 9, 2025
38696b3
Merge pull request #14 from ThalesMMS/codex/review-and-optimize-dcmde…
ThalesMMS Sep 9, 2025
417e549
Read directly into Data buffer when reading stream
ThalesMMS Sep 9, 2025
6d2d857
Merge pull request #15 from ThalesMMS/codex/review-and-optimize-dcmde…
ThalesMMS Sep 9, 2025
0dc529f
Add streaming API, vectorized windowing, and GPU buffer reuse
ThalesMMS Sep 9, 2025
cb281ad
Merge pull request #18 from ThalesMMS/codex/assess-project-capabiliti…
ThalesMMS Sep 9, 2025
9a66cf2
Refactor windowing with Swift concurrency
ThalesMMS Sep 9, 2025
aef90f9
feat(graphics): add Metal buffer cache and RGB support
ThalesMMS Sep 9, 2025
efa2854
Add benchmark for window level vDSP
ThalesMMS Sep 9, 2025
4d2da36
test: add pixel fragment streaming tests
ThalesMMS Sep 9, 2025
b058de6
Merge pull request #19 from ThalesMMS/codex/refactor-dcmswift-for-con…
ThalesMMS Sep 9, 2025
754fe7b
Merge branch 'pr/dcm-swift_new' into codex/implement-buffer-cache-for…
ThalesMMS Sep 9, 2025
67577b8
Merge pull request #20 from ThalesMMS/codex/implement-buffer-cache-fo…
ThalesMMS Sep 9, 2025
ebb119e
Merge branch 'pr/dcm-swift_new' into codex/optimize-graphics-routines…
ThalesMMS Sep 9, 2025
237ac9e
Merge pull request #21 from ThalesMMS/codex/optimize-graphics-routine…
ThalesMMS Sep 9, 2025
61d32ef
Merge branch 'pr/dcm-swift_new' into codex/support-streaming-pixel-da…
ThalesMMS Sep 9, 2025
5e13c93
Merge pull request #22 from ThalesMMS/codex/support-streaming-pixel-d…
ThalesMMS Sep 9, 2025
ca8ff23
Refactor window/level processing for concurrency and Metal
ThalesMMS Sep 9, 2025
16e7551
Pack C-FIND command and dataset in single P-DATA-TF PDU
ThalesMMS Sep 9, 2025
b6da267
Improve DICOM C-GET and C-STORE networking robustness
ThalesMMS Sep 9, 2025
8264ae4
Revise and expand README with detailed features and usage
ThalesMMS Sep 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
xcuserdata/
docs/
Tests/DcmSwiftTests/Resources/
__MACOSX/
__MACOSX/
/build
166 changes: 166 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
{
"configurations": [
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Debug DcmAnonymize",
"program": "${workspaceFolder:DcmSwift}/.build/debug/DcmAnonymize",
"preLaunchTask": "swift: Build Debug DcmAnonymize"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Release DcmAnonymize",
"program": "${workspaceFolder:DcmSwift}/.build/release/DcmAnonymize",
"preLaunchTask": "swift: Build Release DcmAnonymize"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Debug DcmPrint",
"program": "${workspaceFolder:DcmSwift}/.build/debug/DcmPrint",
"preLaunchTask": "swift: Build Debug DcmPrint"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Release DcmPrint",
"program": "${workspaceFolder:DcmSwift}/.build/release/DcmPrint",
"preLaunchTask": "swift: Build Release DcmPrint"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Debug DcmServer",
"program": "${workspaceFolder:DcmSwift}/.build/debug/DcmServer",
"preLaunchTask": "swift: Build Debug DcmServer"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Release DcmServer",
"program": "${workspaceFolder:DcmSwift}/.build/release/DcmServer",
"preLaunchTask": "swift: Build Release DcmServer"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Debug DcmEcho",
"program": "${workspaceFolder:DcmSwift}/.build/debug/DcmEcho",
"preLaunchTask": "swift: Build Debug DcmEcho"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Release DcmEcho",
"program": "${workspaceFolder:DcmSwift}/.build/release/DcmEcho",
"preLaunchTask": "swift: Build Release DcmEcho"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Debug DcmStore",
"program": "${workspaceFolder:DcmSwift}/.build/debug/DcmStore",
"preLaunchTask": "swift: Build Debug DcmStore"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Release DcmStore",
"program": "${workspaceFolder:DcmSwift}/.build/release/DcmStore",
"preLaunchTask": "swift: Build Release DcmStore"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Debug DcmSR",
"program": "${workspaceFolder:DcmSwift}/.build/debug/DcmSR",
"preLaunchTask": "swift: Build Debug DcmSR"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Release DcmSR",
"program": "${workspaceFolder:DcmSwift}/.build/release/DcmSR",
"preLaunchTask": "swift: Build Release DcmSR"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Debug DcmFind",
"program": "${workspaceFolder:DcmSwift}/.build/debug/DcmFind",
"preLaunchTask": "swift: Build Debug DcmFind"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Release DcmFind",
"program": "${workspaceFolder:DcmSwift}/.build/release/DcmFind",
"preLaunchTask": "swift: Build Release DcmFind"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Debug DcmGet",
"program": "${workspaceFolder:DcmSwift}/.build/debug/DcmGet",
"preLaunchTask": "swift: Build Debug DcmGet"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Release DcmGet",
"program": "${workspaceFolder:DcmSwift}/.build/release/DcmGet",
"preLaunchTask": "swift: Build Release DcmGet"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Debug DcmMove",
"program": "${workspaceFolder:DcmSwift}/.build/debug/DcmMove",
"preLaunchTask": "swift: Build Debug DcmMove"
},
{
"type": "swift",
"request": "launch",
"args": [],
"cwd": "${workspaceFolder:DcmSwift}",
"name": "Release DcmMove",
"program": "${workspaceFolder:DcmSwift}/.build/release/DcmMove",
"preLaunchTask": "swift: Build Release DcmMove"
}
]
}
65 changes: 65 additions & 0 deletions GEMINI.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Gemini Development Plan: DcmSwift Optimization

This document outlines the strategic plan for refactoring and optimizing the `DcmSwift` library. The goal is to significantly improve the image rendering performance and network reliability of its primary consumer, the `Isis DICOM Viewer`.

The plan is based on the detailed guidelines in `instructions.md`.

## Core Objectives

1. **Performance:** Achieve high-performance image rendering and scrolling by adopting a real-time pixel pipeline, inspired by the efficient architecture of the legacy project located in the `/Users/thales/GitHub/References/` directory.
2. **Reliability:** Correct critical bugs in the DIMSE networking protocols (C-FIND, C-GET, C-MOVE) to ensure stable and correct communication with remote PACS nodes.
3. **Separation of Concerns:** Solidify the architecture where `DcmSwift` handles all core DICOM logic (parsing, rendering, networking), while `Isis DICOM Viewer` remains focused on UI/UX.

---

## Task 1: Image Rendering Pipeline Optimization

**Objective:** Replace the current, inefficient image rendering workflow with a high-performance, buffer-based pipeline.

### Subtask 1.1: Implement High-Performance Image View

* **Action:** Re-implement `DcmSwift/Sources/DcmSwift/Graphics/DicomPixelView.swift`.
* **Strategy:** Adapt the logic from the reference file `/Users/thales/GitHub/References/DicomPixelView.swift`.
* Maintain a persistent buffer for raw pixel data (`pix16` or `pix8`) to eliminate redundant file reads.
* Implement `computeLookUpTable16` and `createImage16` for fast, on-the-fly conversion from 16-bit raw pixels to an 8-bit displayable buffer.
* Ensure the `updateWindowLevel` function only triggers a recalculation of the 8-bit buffer and a lightweight `CGImage` recreation, avoiding the expensive `UIImage` process.

### Subtask 1.2: Integrate New View into the `DcmSwift` Pipeline

* **Action:** Modify `DcmSwift/Sources/DcmSwift/Graphics/DicomImage.swift`.
* **Strategy:** The `image(forFrame:wwl:inverted:)` function will be rewritten. Instead of performing the rendering itself, it will:
1. Use a decoder (inspired by `/Users/thales/GitHub/References/DCMDecoder.swift`) to get the raw pixel buffer.
2. Pass this buffer to the new, optimized `DicomPixelView` (or a similar canvas object) which will handle the final W/L mapping and rendering.

### Subtask 1.3: Optimize the `Isis DICOM Viewer` Image Pipeline

* **Action:** Refactor `Isis DICOM Viewer/Isis DICOM Viewer/Data/Services/DcmSwiftImagePipeline.swift`.
* **Strategy:**
* The `framePixels` method will be optimized to read raw pixels from a DICOM file only once per series, caching them in memory.
* This cached raw pixel buffer will be passed directly to the new `DicomPixelCanvas` (which will be based on the new `DicomPixelView` logic), offloading all W/L and rendering calculations to the optimized component.

### Subtask 1.4: Implement High-Speed Thumbnail Generation

* **Action:** Integrate down-sampling logic into `Isis DICOM Viewer/Isis DICOM Viewer/Data/Services/DICOM/ThumbnailGenerator.swift`.
* **Strategy:** Port the `getDownsampledPixels16` logic from the reference `DCMDecoder.swift` to enable extremely fast, low-overhead thumbnail creation without full image decoding.

---

## Task 2: Network Protocol Correction

**Objective:** Fix critical bugs in the C-FIND, C-GET, and C-MOVE implementations to ensure reliable and compliant DICOM network communication.

### Subtask 2.1: Correct C-FIND Query Filtering

* **Action:** Modify `DcmSwift/Sources/DcmSwift/Networking/CFindSCU.swift` and `PDUEncoder.swift`.
* **Strategy:**
1. Correct the `request(association:channel:)` method in `CFindSCU` to ensure the `queryDataset` (containing search filters) is included in the same PDU as the C-FIND-RQ command.
2. Adjust the `PDUEncoder` to correctly serialize the `queryDataset` alongside the command dataset, preventing the server from ignoring the filters.

### Subtask 2.2: Stabilize C-GET/C-MOVE Data Reception

* **Action:** Improve `DicomAssociation.swift`, `PDUBytesDecoder.swift`, and `CGetSCU.swift`.
* **Strategy:**
1. Enhance `PDUBytesDecoder` to correctly reassemble fragmented PDU messages, especially those containing large pixel data payloads from C-STORE sub-operations.
2. Update `CGetSCU` to properly handle multiple incoming C-STORE-RQ data transfers, accumulating the pixel data into a temporary buffer until the complete file is received.
3. Audit and fortify the temporary C-STORE-SCP server logic initiated by `DicomClient.move()`. Ensure the server starts reliably and that the C-MOVE operation only completes after all C-STORE sub-operations have successfully finished.
129 changes: 129 additions & 0 deletions OPTIMIZATION_REVIEW.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# DcmSwift Optimization Integration Review

## Summary
After thoroughly reviewing the DcmSwift codebase and comparing it with the optimizations provided in the References folder, I've identified which optimizations have been successfully integrated and which are missing.

## Successfully Integrated Optimizations ✅

### 1. DicomTool.swift
**Status: Partially Integrated**
- ✅ Basic DICOM decoding functionality implemented
- ✅ Window/level calculation using `WindowLevelCalculator`
- ✅ Synchronous wrapper for async methods
- ✅ Direct pixel data extraction
- ❌ Missing: DcmSwiftService protocol/implementation from References
- ❌ Missing: DicomImageModel abstraction
- ❌ Missing: Comprehensive error handling enum
- ❌ Missing: RGB image support (createRGBImage method)
- ❌ Missing: HU conversion utility methods
- ❌ Missing: Distance calculation methods

### 2. DicomPixelView.swift
**Status: Core Optimizations Integrated**
- ✅ Context reuse optimization (shouldReuseContext)
- ✅ Cached image data (cachedImageData, cachedImageDataValid)
- ✅ Parallel processing for large images (>2M pixels)
- ✅ Loop unrolling for better performance
- ✅ Window/level change detection to avoid recomputation
- ✅ Optimized LUT generation with derived LUT support
- ❌ Missing: Metal GPU acceleration (stub implementation only)
- ❌ Missing: Performance metrics tracking
- ❌ Missing: Image presets system
- ❌ Missing: Memory usage estimation
- ❌ Missing: Comprehensive 24-bit RGB support
- ❌ Missing: Advanced Metal shader implementation

### 3. WindowLevelCalculator.swift
**Status: Basic Implementation**
- ✅ Core window/level calculation logic
- ✅ Modality-specific presets
- ✅ HU to pixel conversions
- ✅ DicomImageContext structure
- ❌ Missing: UI presentation methods from WindowLevelService
- ❌ Missing: Gesture-based adjustment methods
- ❌ Missing: Performance logging
- ❌ Missing: Full dynamic preset calculation
- ❌ Missing: MVVM-C migration methods

## Missing Optimizations ❌

### 1. GPU Acceleration (Metal)
The References/DicomPixelView.swift contains a complete Metal implementation with:
- Metal device setup
- Custom compute shader for window/level processing
- GPU buffer management
- Optimized thread group calculations

Current DcmSwift only has a stub returning `false` in `processPixelsGPU`.

### 2. Advanced Caching Strategy
References implementation has:
- lastWinMin/lastWinMax tracking
- Context dimension caching (lastContextWidth, lastContextHeight)
- Intelligent cache invalidation

### 3. Performance Monitoring
References implementation includes:
- CFAbsoluteTimeGetCurrent() timing measurements
- Performance logging with [PERF] tags
- Detailed metrics for each operation

### 4. RGB/Color Image Support
References has full 24-bit RGB image handling with:
- BGR to RGB conversion
- RGBA buffer creation
- Proper color space management

### 5. Memory Management Extensions
References includes:
- clearCache() method
- estimatedMemoryUsage() calculation
- Memory-efficient buffer handling

### 6. UI/UX Enhancements
References WindowLevelService includes:
- presentWindowLevelDialog()
- presentPresetSelector()
- Gesture-based adjustment with proper axis mapping

## Recommendations for Full Integration

### Priority 1: GPU Acceleration
Implement the Metal GPU acceleration from References/DicomPixelView.swift lines 606-723. This provides significant performance improvements for large medical images.

### Priority 2: Complete RGB Support
Add the missing RGB/24-bit image handling methods for full DICOM format support.

### Priority 3: Advanced Caching
Implement the comprehensive caching strategy to avoid unnecessary recomputations.

### Priority 4: Performance Metrics
Add performance monitoring to identify bottlenecks and optimize critical paths.

### Priority 5: UI Components
Consider adding the UI presentation methods for better user interaction with window/level controls.

## Performance Impact Assessment

Based on the optimizations present in References but missing in DcmSwift:

1. **GPU Processing**: Could provide 2-10x speedup for large images
2. **Advanced Caching**: Could reduce redundant calculations by 30-50%
3. **Parallel Processing**: Already implemented for images >2M pixels
4. **Loop Unrolling**: Already implemented, provides ~20% improvement

## Conclusion

DcmSwift has successfully integrated the core optimizations for:
- Basic window/level calculations
- Context reuse
- Parallel CPU processing
- Caching strategies

However, significant optimizations from the References folder are missing:
- GPU acceleration (Metal)
- Complete RGB support
- Advanced performance monitoring
- UI/UX enhancements

The most impactful missing optimization is the GPU acceleration, which could provide substantial performance improvements for medical image processing.
Loading