Skip to content

Commit 93a3e93

Browse files
Fix for decoded color photometric interpretation
1 parent d3232f9 commit 93a3e93

5 files changed

Lines changed: 35 additions & 11 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ project(
88

99
set(DCMTKHTJ2K_VERSION_MAJOR 1)
1010
set(DCMTKHTJ2K_VERSION_MINOR 0)
11-
set(DCMTKHTJ2K_VERSION_BUILD 4)
11+
set(DCMTKHTJ2K_VERSION_BUILD 5)
1212
set(DCMTKHTJ2K_VERSION
1313
"${DCMTKHTJ2K_VERSION_MAJOR}.${DCMTKHTJ2K_VERSION_MINOR}.${DCMTKHTJ2K_VERSION_BUILD}"
1414
)

build.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
3737
-DCMAKE_INSTALL_PREFIX=$OTS_DEV_SPACE/dcmtk/$BUILD_TYPE \
3838
-DCMAKE_POLICY_DEFAULT_CMP0091=NEW \
3939
-DCMAKE_MSVC_RUNTIME_LIBRARY='MultiThreaded$<$<CONFIG:Debug>:Debug>'
40-
cmake --build . --parallel $(nproc) --config $BUILD_TYPE
40+
cmake --build . --parallel $(nproc 2>/dev/null || sysctl -n hw.logicalcpu) --config $BUILD_TYPE
4141
cmake --install . --config $BUILD_TYPE
4242

4343
cd $OTS_DEV_SPACE
@@ -54,7 +54,7 @@ cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
5454
-DCMAKE_INSTALL_PREFIX=$OTS_DEV_SPACE/openjph/$BUILD_TYPE \
5555
-DCMAKE_POLICY_DEFAULT_CMP0091=NEW \
5656
-DCMAKE_MSVC_RUNTIME_LIBRARY='MultiThreaded$<$<CONFIG:Debug>:Debug>'
57-
cmake --build . --parallel $(nproc) --config $BUILD_TYPE
57+
cmake --build . --parallel $(nproc 2>/dev/null || sysctl -n hw.logicalcpu) --config $BUILD_TYPE
5858
cmake --install . --config $BUILD_TYPE
5959

6060
mkdir -p $BUILD_DIR
@@ -70,5 +70,5 @@ cmake .. -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
7070
-DCMAKE_INSTALL_PREFIX=$BUILD_DIR_LIB \
7171
-DCMAKE_POLICY_DEFAULT_CMP0091=NEW \
7272
-DCMAKE_MSVC_RUNTIME_LIBRARY='MultiThreaded$<$<CONFIG:Debug>:Debug>'
73-
cmake --build . --parallel $(nproc) --config $BUILD_TYPE
73+
cmake --build . --parallel $(nproc 2>/dev/null || sysctl -n hw.logicalcpu) --config $BUILD_TYPE
7474
cmake --install . --config $BUILD_TYPE

libsrc/djcodecd.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,12 @@ OFCondition HtJ2kDecoderBase::decodeFrame(
389389
codestream.read_headers(&mem_file);
390390

391391
ojph::param_siz siz = codestream.access_siz();
392+
ojph::param_cod cod = codestream.access_cod();
392393
int num_comps = siz.get_num_components();
393394
int width = siz.get_recon_width(0);
394395
int height = siz.get_recon_height(0);
396+
bool usingColorTransform =
397+
(num_comps == 3) && cod.is_using_color_transform();
395398

396399
if (width != imageColumns)
397400
result = EC_HTJ2KImageDataMismatch;
@@ -447,6 +450,11 @@ OFCondition HtJ2kDecoderBase::decodeFrame(
447450
}
448451
}
449452

453+
// Update photometric interpretation
454+
if (usingColorTransform) {
455+
dataset->putAndInsertString(DCM_PhotometricInterpretation, "RGB");
456+
}
457+
450458
// Clean up
451459
for (int c = 0; c < num_comps; c++) {
452460
delete[] comps_data[c];
@@ -522,6 +530,12 @@ OFCondition HtJ2kDecoderBase::determineDecompressedColorModel(
522530
// retrieve color model from given dataset
523531
result = dataset->findAndGetOFString(DCM_PhotometricInterpretation,
524532
decompressedColorModel);
533+
// YBR_ICT (lossy) and YBR_RCT (lossless) are converted to RGB by
534+
// OpenJPH's inverse color transform during decompression.
535+
if (result.good() && (decompressedColorModel == "YBR_ICT" ||
536+
decompressedColorModel == "YBR_RCT")) {
537+
decompressedColorModel = "RGB";
538+
}
525539
}
526540
return result;
527541
}

libsrc/djcodece.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ OFCondition HtJ2kEncoderBase::losslessRawEncode(
530530
if (compressedSize > 0) compressionRatio = uncompressedSize / compressedSize;
531531

532532
// update photometric interpretation for color images
533-
if (result.good() && samplesPerPixel > 1) {
533+
if (result.good() && photometricInterpretation == "RGB") {
534534
result = dataset->putAndInsertString(
535535
DCM_PhotometricInterpretation,
536536
djrp->useLosslessProcess() ? "YBR_RCT" : "YBR_ICT");
@@ -562,7 +562,8 @@ OFCondition HtJ2kEncoderBase::compressRawFrame(
562562
ojph::codestream codestream;
563563
ojph::mem_outfile destinationBuffer;
564564

565-
bool colorTransform = (samplesPerPixel > 1);
565+
// Apply color transform only for RGB input
566+
bool colorTransform = (photometricInterpretation == "RGB");
566567
codestream.set_planar(colorTransform == false);
567568
codestream.set_tilepart_divisions(true, false);
568569
codestream.request_tlm_marker(true);
@@ -813,7 +814,7 @@ OFCondition HtJ2kEncoderBase::RenderedEncode(
813814
if (result.good())
814815
result = dataset->putAndInsertUint16(DCM_HighBit, bitsPerSample - 1);
815816
// update photometric interpretation for color images
816-
if (result.good() && samplesPerPixel > 1) {
817+
if (result.good() && photometricInterpretation == "RGB") {
817818
result = dataset->putAndInsertString(
818819
DCM_PhotometricInterpretation,
819820
djrp->useLosslessProcess() ? "YBR_RCT" : "YBR_ICT");
@@ -980,7 +981,8 @@ OFCondition HtJ2kEncoderBase::compressRenderedFrame(
980981
ojph::codestream codestream;
981982
ojph::mem_outfile destinationBuffer;
982983

983-
bool colorTransform = (samplesPerPixel > 1);
984+
// Apply color transform only for RGB input
985+
bool colorTransform = (photometricInterpretation == "RGB");
984986
codestream.set_planar(colorTransform == false);
985987
codestream.set_tilepart_divisions(true, false);
986988
codestream.request_tlm_marker(true);

tests/test.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,18 +394,26 @@ TEST(CodecTest, BasicColorCompressDecompressLossless) {
394394
DcmDataset *readDataset = readFile.getDataset();
395395
const E_TransferSyntax readXfer = readDataset->getOriginalXfer();
396396
ASSERT_EQ(readXfer, htj2kLossless);
397+
398+
// Verify photometric interpretation is updated to YBR_RCT
399+
OFString photometricInterpretation;
400+
ASSERT_TRUE(readDataset
401+
->findAndGetOFString(DCM_PhotometricInterpretation,
402+
photometricInterpretation)
403+
.good());
404+
ASSERT_EQ(photometricInterpretation, "YBR_RCT");
405+
397406
ASSERT_TRUE(
398407
readDataset->chooseRepresentation(EXS_LittleEndianExplicit, nullptr)
399408
.good());
400409
ASSERT_TRUE(readDataset->canWriteXfer(EXS_LittleEndianExplicit));
401410

402-
// Verify photometric interpretation is updated to YBR_RCT
403-
OFString photometricInterpretation;
411+
// Verify photometric interpretation is updated to RGB
404412
ASSERT_TRUE(readDataset
405413
->findAndGetOFString(DCM_PhotometricInterpretation,
406414
photometricInterpretation)
407415
.good());
408-
ASSERT_EQ(photometricInterpretation, "YBR_RCT");
416+
ASSERT_EQ(photometricInterpretation, "RGB");
409417

410418
Uint8 const *decoded = nullptr;
411419
unsigned long decodedCount = 0;

0 commit comments

Comments
 (0)