How to use OpenJPH in DicomObjects for High Throughput JPEG 2000 (HTJ2K)
Contents
- Overview
- Dependencies & Requirements
- Building & Running the Project
- Project Structure
- How the Console Application works
Overview
This guide explains how to integrate OpenJPH with DicomObjects (.NET and .Core versions) using DicomCodecs. The provided sample projects demonstrates how to:
- Build OpenJPH and OpenJPHWrapper
- Implement custom HTJ2K codec
- Register custom HTJ2K codec with DicomObjects.NET and DicomObjects.Core version
- Compress DICOM Images to HTJ2K format and decompress HTJ2K compressed images in sample user code
The sample projects and pre-compiled dlls can be downloaded directly from the links below or from the sample projects list on the DicomObjects.NET page or the DicomObjects.Core page
DicomObjects.NET HTJ2K Custom Codec Source Code download
DicomObjects.NET HTJ2K Custom Codec Pre-compiled DLL download
DicomObjects.Core HTJ2K Custom Codec Source Code download
DicomObjects.Core HTJ2K Custom Codec Pre-compiled DLL download
Dependencies & Requirements
Before using the OpenJPH codec, ensure you have done the following:
- CMake - Install CMake as you will need to build OpenJPH from source.
- Visit OpenJPH Main Repository - https://github.com/aous72/OpenJPH (required for later steps)
- TIFF Library - Required for OpenJPH’s build configuration, we have provided tiff in the sample project or you can download it via the OpenJPH repository compiling section.
Build OpenJPH Using CMake
After downloading OpenJPH, you need to build it using CMake:
To compile OpenJPH, visit https://github.com/aous72/OpenJPH/blob/master/docs/compiling.md which will provide OS specific instructions on how to compile the project. In general, for windows you need to do the following:
- Open a command prompt and navigate to the downloaded sample project source directory.
- Create a build folder and cd to it - e.g. C:\…\HTJ2KOpenJPHCodec\build
- Run the following CMake command:
cmake .. -G "Visual Studio 17 2022" -A x64 -DCMAKE_PREFIX_PATH="tiff_library_path" // the root directory of your download tiff
This will generate the openjph.sln which you can open via VisualStudio. Once opened, you’ll need to manually add the ConsoleAppNET, MyHTJ2KCodec and OpenJPHWrapper projects to the solution.
Building & Running the Project
When building the solution for the first time, you may encounter LNK1104 errors because OpenJPHWrapper
depends on the openJPH
library, which hasn’t been compiled yet.
To avoid this, follow one of these methods:
1. (Recommended) Set project dependencies in Visual Studio:
- Right-click on the solution (
HTJ2KOpenJPHCodec.sln
) → Project Dependencies… - In the “Projects” dropdown, select
OpenJPHWrapper
. - Tick
openJPH
to ensure it is built first.
2. Manually follow the correct build order (steps below).
Build OpenJPH
- Set Active Configuration to
Debug
orRelease
(depending on your needs). - Build the project (
Ctrl + Shift + B
).
Build OpenJPHWrapper
- Set Active Configuration to match OpenJPH (
Debug
orRelease
). - Build the project.
Build ConsoleAppNET (or ConsoleAppCore)
- Ensure the configuration matches (
Debug
orRelease
). - Build the project (this will also automatically build
MyHTJ2KCodec
, as it is referenced).
Post-Build Event: Copying OpenJPH.dll to ConsoleApp
By default openJPH is outputed to it’s own output directory, you need to copy the openJPH.dll to the console app bin directory (OpenJPHWrapper.dll should automatically be outputted here), you can also add the following post-build event in OpenJPHWrapper to avoid this copying each time the wrapper is compiled:
Steps to Add the Post-Build Event
1. Right-click on openJPH
→ Properties.
2. Navigate to Configuration Properties → Build Events → Post-Build Event
3. In the Command Line field, add:
copy "$(TargetDir)*" "$(SolutionDir)..\ConsoleAppNET\bin\x64\Debug"
Project Structure
The sample project includes the following components:
Project Name | Description |
---|---|
ConsoleAppNET | Sample .NET/.Core Console App demonstrating how to register MyHTJ2KCodec to read and write HTJ2K |
MyHTJ2KCodec | Custom HTJ2K Codec, invoking OpenJPHWrapper to handle HTJ2K compression & decompression |
OpenJPHWrapper | C++ wrapper for OpenJPH, enabling support for clr or clr/core |
openJPH | Opensource OpenJPH library for JPEG 2000 processing |
How the sample Console Application Works
The ConsoleAppNET (or ConsoleAppCore) demonstrates the compression and decompression of DICOM images using OpenJPH.
Input & Output Directories
- The input directory is expected to contain DICOM images (
*.dcm
) that will be compressed. - The output directory is where the compressed and decompressed images will be saved.
- A “decompressed” subfolder will be created automatically inside the output directory when decompressing images.
- You will need to set these paths before running
Compression & Decompression Workflow
- The console app registers the OpenJPH codec with DicomObjects.
- If compression mode (compressTest = true) is enabled:
- DICOM images from the input directory are read and compressed into HTJ2K format (
*.htj2k.dcm
). - The compressed files are saved in the output directory.
- DICOM images from the input directory are read and compressed into HTJ2K format (
- If decompression mode (compressTest = false) is enabled:
- The compressed HTJ2K DICOM images from the output directory are decompressed.
- Decompressed files are stored in the “decompressed” subfolder inside the output directory.