Skip to content

Example DICOM image viewer on iOS based on Cornerstone.js

License

Notifications You must be signed in to change notification settings

thulefog/lapis-angularis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lapis-angularis: a Hybrid Native and Web DICOM Viewer for iOS example

Abstract

This is a skeleton iOS application that displays DICOM medical images using the cornerstone.js web framework embedded in a WKWebView using Swift, SwiftUI, Cornerstone.js JavaScript.

Background

As an observation - a simple example illustrating integration of Cornerstone.js into iOS application with Swift was not available when I was looking for one on the project website, so here is one. It is clear in last years, Cornerstone.js has evolved and improved over time and a reasonable ingredient for a mobile application vertical. I am well versed in bringing up image viewers including on iOS (Swift, Objective-C, C++) but have tracked Cornerstone.js over time.

NOTE: The name lapis angularis is in effect, a variation on the Latin translation for "cornerstone". This is a combination of lapis (stone) and angularis term to convey the corner/angle characteristic, so "stone corner".

User Interface

The user interface under construction is illustrated below.

DICOM Viewer
viewer

Usage

Loading DICOM Files

  1. Tap Load DICOM to open the file picker
  2. Select a .dcm file from Files, iCloud, or connected sources
  3. The image will render with default window/level

Interaction

Gesture Action
Single-finger drag Adjust window/level
Two-finger pinch Zoom in/out
Two-finger drag Pan image

Controls

  • WW/WL Sliders: Fine-tune window width and window level
  • Reset: Return to default viewport

Software Blueprint

  • Application and View System
ContentView  
>  DicomViewModel 
>> CornerstoneWebView (UIViewRepresentable + WKWebView + Coordinator)

NOTE: JavaScript Bridge between CornerstoneWebView and WKWebView

>>> Web Layer (WKWebView)

  ┌─────────────────────────────────────────────────────┐
  │                 cornerstone.html                    │
  │  • cornerstone-core (rendering)                     │
  │  • cornerstone-tools (pan/zoom/window-level)        │
  │  • cornerstone-wado-image-loader (DICOM parsing)    │
  │  • dicom-parser (tag extraction)                    │
  └─────────────────────────────────────────────────────┘

Project Structure

<root source code directory>/
├── DicomViewerApp.swift      # App entry point
├── ContentView.swift         # Main UI with file picker & controls
├── DicomViewModel.swift      # State management & WebView bridge
├── CornerstoneWebView.swift  # WKWebView wrapper for SwiftUI
├── CornerstoneHTML.swift     # Inline HTML/JS (fallback)
├── Info.plist                # App configuration & permissions
└── Resources/
    └── cornerstone.html      # Bundled HTML file (preferred)

Implementation Notes

HTML Resources

  1. Resources folder contains the actual HTML web pages
  2. As needed, add more pages as additional web page transitions needed based on Cornerstone.js usage.
  3. NOTE: For new files added, verify the file(s) appear in Build Phases → Copy Bundle Resources

Configuration Settingss: Info.plist

These are the key Info.plist entries in the Info.plist.

Key configurations:

  • NSAppTransportSecurity - allows CDN script loading
  • UTImportedTypeDeclarations - DICOM file type support
  • UISupportsDocumentBrowser - Files app integration

Clean Build & Run

The app requires network access to load cornerstone.js from CDN on first launch.

Customization

Using Bundled vs CDN Scripts

For offline support, download cornerstone.js libraries and bundle them:

// In cornerstone.html, replace CDN URLs with local paths:
<script src="cornerstone.min.js"></script>
<script src="cornerstoneTools.min.js"></script>
// etc.

Adding More Tools

In cornerstone.html, add additional cornerstone-tools:

const LengthTool = cornerstoneTools.LengthTool;
cornerstoneTools.addTool(LengthTool);
cornerstoneTools.setToolActive('Length', { mouseButtonMask: 1 });

Extracting DICOM Tags

The viewer already parses basic tags. Add more in updateOverlays():

const seriesDesc = getDicomTagValue(dataSet, 'x0008103e'); // Series Description
const sliceThickness = getDicomTagValue(dataSet, 'x00180050');

Multi-frame / Series Support

For multi-frame DICOM or series navigation, you'll need to:

  1. Parse the NumberOfFrames tag (x00280008)
  2. Create multiple imageIds
  3. Add navigation controls in Swift
  4. Call cornerstone.displayImage() with different frames

Dependencies

All loaded via CDN (unpkg.com):

  • cornerstone-core@2.6.1
  • cornerstone-tools@6.0.10
  • cornerstone-wado-image-loader@4.13.2
  • cornerstone-math@0.1.10
  • dicom-parser@1.8.21
  • hammerjs@2.0.8

Limitations

  • Single-frame images only (skeleton doesn't include series navigation)
  • Requires network for CDN scripts (unless bundled)
  • No DICOM networking (C-FIND, C-MOVE, DICOMweb)
  • Basic toolset (extend cornerstone-tools as needed)
  • Does not contain advanced features (3D MPR, volume rendering). That would require migration to Cornerstone3D:
// Cornerstone3D uses a different API
import { RenderingEngine, Enums } from '@cornerstonejs/core';

Software License

The source code is published under the MIT License as per below. See also the top level LICENSE text file.

NOTE: Cornerstone.js is also MIT licensed.

MIT License

Copyright (c) 2025-2026 John Matthew Weston

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Example DICOM image viewer on iOS based on Cornerstone.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published