Mercurial > hg > orthanc
changeset 6952:94af24a493de
allow DecodeLookupTable to decode a single frame
| author | Alain Mazy <am@orthanc.team> |
|---|---|
| date | Wed, 17 Jun 2026 16:03:55 +0200 |
| parents | 7f50469de86e |
| children | 9832a716c687 |
| files | NEWS OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp OrthancFramework/Sources/Images/ImageProcessing.cpp OrthancFramework/Sources/Images/ImageProcessing.h |
| diffstat | 4 files changed, 128 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/NEWS Thu Jun 11 10:47:07 2026 +0200 +++ b/NEWS Wed Jun 17 16:03:55 2026 +0200 @@ -59,6 +59,8 @@ https://orthanc.uclouvain.be/bugs/show_bug.cgi?id=258 Security issue reported by Jose Lopez Martinez (aka elpe_pinillo) from Deloitte. * Fixed various OOB read/write (TODO: finalize release notes) +* Improved preview and rendering of multiframe instances a with a Color Lookup Table; + notably, some cardiac US instances. * Lua: Fix the "ReceivedCStoreInstanceFilter" Lua callback whose return value was not taken into account. * Upgraded dependencies for static builds:
--- a/OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp Thu Jun 11 10:47:07 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp Wed Jun 17 16:03:55 2026 +0200 @@ -404,9 +404,11 @@ const DicomImageInformation& info, DcmDataset& dataset, const uint8_t* pixelData, - unsigned long pixelLength) + unsigned long pixelDataLength, + unsigned int numberOfFramesInPixelData, + unsigned int frameToDecode) { - LOG(INFO) << "Decoding a lookup table"; + // LOG(INFO) << "Decoding a lookup table"; OFString r, g, b; PixelFormat format; @@ -417,14 +419,19 @@ unsigned long gc = 0; unsigned long bc = 0; - if (pixelData == NULL && - !dataset.findAndGetUint8Array(DCM_PixelData, pixelData, &pixelLength).good()) + // note: info.GetNumberOfFrames() is not reliable when a single frame has been uncompressed but, in this case, we provide a pixelData and we must use numberOfFramesInPixelData + // If there are no pixelData, numberOfFramesInPixelData is set to 0 and we must use info.GetNumberOfFrames() + if (pixelData == NULL) { - THROW_WITH_FILE_AND_LINE_INFO(ErrorCode_NotImplemented); + numberOfFramesInPixelData = info.GetNumberOfFrames(); + if (!dataset.findAndGetUint8Array(DCM_PixelData, pixelData, &pixelDataLength).good()) + { + THROW_WITH_FILE_AND_LINE_INFO(ErrorCode_NotImplemented); + } } if (info.IsPlanar() || - info.GetNumberOfFrames() != 1 || + // info.GetNumberOfFrames() != 1 || !info.ExtractPixelFormat(format, false) || !dataset.findAndGetOFStringArray(DCM_BluePaletteColorLookupTableDescriptor, b).good() || !dataset.findAndGetOFStringArray(DCM_GreenPaletteColorLookupTableDescriptor, g).good() || @@ -443,6 +450,29 @@ throw OrthancException(ErrorCode_NotImplemented, "Inconsistent Palette Color Lookup Table"); } + const uint64_t width = target->GetWidth(); + const uint64_t height = target->GetHeight(); + + uint64_t frameSize = width * height * GetBytesPerPixel(target->GetFormat()); + + if (frameSize > MAX_IMAGE_FRAME_SIZE || + static_cast<uint64_t>(static_cast<size_t>(frameSize)) != frameSize) + { + std::ostringstream errorMessage; + errorMessage << "DecodeLookupTable: max decoded frame size overflow (" << frameSize << " vs. " << MAX_IMAGE_FRAME_SIZE << ")"; + throw OrthancException(ErrorCode_BadFileFormat, errorMessage.str()); + } + + uint64_t frameEnd = frameSize * (frameToDecode + 1); + + if (static_cast<uint64_t>(static_cast<size_t>(frameEnd)) != frameEnd) + { + std::ostringstream errorMessage; + errorMessage << "DecodeLookupTable: frameEnd larger than 4GB, which is too large for Orthanc running in 32bits (" << frameEnd << ")"; // can only happen on 32 bits + throw OrthancException(ErrorCode_BadFileFormat, errorMessage.str()); + } + + switch (format) { case PixelFormat_RGB24: @@ -472,10 +502,10 @@ throw OrthancException(ErrorCode_NotImplemented, std::string("Palette Color Lookup Table Descriptor invalid palette size: '") + r.c_str() + "'"); } - const uint64_t expectedSize = (static_cast<uint64_t>(target->GetWidth()) * - static_cast<uint64_t>(target->GetHeight())); + const uint64_t expectedFrameSourceSize = (static_cast<uint64_t>(target->GetWidth()) * + static_cast<uint64_t>(target->GetHeight())); - if (pixelLength != expectedSize) + if (pixelDataLength != (expectedFrameSourceSize * numberOfFramesInPixelData)) { DcmElement *elem = NULL; Uint16 bitsAllocated = 0; @@ -496,15 +526,13 @@ // seen in some Philips ClearVue 650 images (using 8 bits LUT) if (elem->getVR() != EVR_OW || bitsAllocated != 8 || - 2 * pixelLength != expectedSize) + 2 * pixelDataLength != (expectedFrameSourceSize * numberOfFramesInPixelData)) { throw OrthancException(ErrorCode_BadFileFormat, "Invalid size"); } } - const uint8_t* source = reinterpret_cast<const uint8_t*>(pixelData); - const unsigned int width = target->GetWidth(); - const unsigned int height = target->GetHeight(); + const uint8_t* source = reinterpret_cast<const uint8_t*>(pixelData + expectedFrameSourceSize * frameToDecode); for (unsigned int y = 0; y < height; y++) { @@ -530,20 +558,24 @@ case PixelFormat_RGB48: { - const uint64_t expectedSize = static_cast<uint64_t>(target->GetWidth()) * target->GetHeight() * 2; + const uint64_t expectedFrameSourceSize = static_cast<uint64_t>(target->GetWidth()) * target->GetHeight() * 2; + + if ((expectedFrameSourceSize * numberOfFramesInPixelData) != pixelDataLength) + { + throw OrthancException(ErrorCode_InternalError, std::string("Palette Color Lookup Table Descriptor: invalid image size")); + } if (r != "0\\0\\16" || rc != 65536 || gc != 65536 || - bc != 65536 || - pixelLength != expectedSize) + bc != 65536) { throw OrthancException(ErrorCode_NotImplemented, std::string("Palette Color Lookup Table Descriptor not supported: '") + r.c_str() + "'"); } - const uint16_t* source = reinterpret_cast<const uint16_t*>(pixelData); const unsigned int width = target->GetWidth(); const unsigned int height = target->GetHeight(); + const uint16_t* source = reinterpret_cast<const uint16_t*>(pixelData + expectedFrameSourceSize * frameToDecode); for (unsigned int y = 0; y < height; y++) { @@ -597,7 +629,7 @@ if (info.GetPhotometricInterpretation() == PhotometricInterpretation_Palette) { - return DecodeLookupTable(target, info, dataset, NULL, 0); + return DecodeLookupTable(target, info, dataset, NULL, 0, 0 /* we don't know the number of frames in the buffer */, frame); } @@ -809,7 +841,7 @@ return DecodeLookupTable(target, info, dataset, reinterpret_cast<const uint8_t*>(uncompressed.c_str()), - uncompressed.size()); + uncompressed.size(), 1 /* a single frame has been decoded */, 0 /* decode the first frame of this buffer*/); } else {
--- a/OrthancFramework/Sources/Images/ImageProcessing.cpp Thu Jun 11 10:47:07 2026 +0200 +++ b/OrthancFramework/Sources/Images/ImageProcessing.cpp Wed Jun 17 16:03:55 2026 +0200 @@ -166,11 +166,11 @@ } - template <typename TargetType> + template <typename TargetType, typename SourceType> static void ConvertColorToGrayscale(ImageAccessor& target, const ImageAccessor& source) { - assert(source.GetFormat() == PixelFormat_RGB24); + assert(source.GetFormat() == PixelFormat_RGB24 || source.GetFormat() == PixelFormat_RGB48); // WARNING - "::min()" should be replaced by "::lowest()" if // dealing with float or double (which is not the case so far) @@ -184,7 +184,7 @@ for (unsigned int y = 0; y < height; y++) { TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); - const uint8_t* s = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); + const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); for (unsigned int x = 0; x < width; x++, t++, s += 3) { @@ -724,21 +724,35 @@ if (target.GetFormat() == PixelFormat_Grayscale8 && source.GetFormat() == PixelFormat_RGB24) { - ConvertColorToGrayscale<uint8_t>(target, source); + ConvertColorToGrayscale<uint8_t, uint8_t>(target, source); return; } if (target.GetFormat() == PixelFormat_Grayscale16 && source.GetFormat() == PixelFormat_RGB24) { - ConvertColorToGrayscale<uint16_t>(target, source); + ConvertColorToGrayscale<uint16_t, uint8_t>(target, source); + return; + } + + if (target.GetFormat() == PixelFormat_Grayscale16 && + source.GetFormat() == PixelFormat_RGB48) + { + ConvertColorToGrayscale<uint16_t, uint16_t>(target, source); + return; + } + + if (target.GetFormat() == PixelFormat_Grayscale8 && + source.GetFormat() == PixelFormat_RGB48) + { + ConvertColorToGrayscale<uint8_t, uint16_t>(target, source); return; } if (target.GetFormat() == PixelFormat_SignedGrayscale16 && source.GetFormat() == PixelFormat_RGB24) { - ConvertColorToGrayscale<int16_t>(target, source); + ConvertColorToGrayscale<int16_t, uint8_t>(target, source); return; } @@ -1097,6 +1111,12 @@ Set(image, v, v, v, v); // Use the color version return; } + case PixelFormat_RGB48: + { + uint16_t v = static_cast<uint16_t>(value); + SetUint16(image, v, v, v, v); // Use the color version + return; + } default: THROW_WITH_FILE_AND_LINE_INFO(ErrorCode_NotImplemented); @@ -1171,6 +1191,49 @@ } } + + void ImageProcessing::SetUint16(ImageAccessor& image, + uint16_t red, + uint16_t green, + uint16_t blue, + uint16_t alpha) + { + uint16_t p[4]; + unsigned int size; + + switch (image.GetFormat()) + { + case PixelFormat_RGB48: + p[0] = red; + p[1] = green; + p[2] = blue; + size = 3; + break; + + default: + THROW_WITH_FILE_AND_LINE_INFO(ErrorCode_NotImplemented); + } + + const unsigned int width = image.GetWidth(); + const unsigned int height = image.GetHeight(); + + for (unsigned int y = 0; y < height; y++) + { + uint16_t* q = reinterpret_cast<uint16_t*>(image.GetRow(y)); + + for (unsigned int x = 0; x < width; x++) + { + for (unsigned int i = 0; i < size; i++) + { + q[i] = p[i]; + } + + q += size; + } + } + } + + void ImageProcessing::Set(ImageAccessor& image, uint8_t red, uint8_t green,
--- a/OrthancFramework/Sources/Images/ImageProcessing.h Thu Jun 11 10:47:07 2026 +0200 +++ b/OrthancFramework/Sources/Images/ImageProcessing.h Wed Jun 17 16:03:55 2026 +0200 @@ -101,6 +101,12 @@ uint8_t blue, uint8_t alpha); + static void SetUint16(ImageAccessor& image, + uint16_t red, + uint16_t green, + uint16_t blue, + uint16_t alpha); + static void Set(ImageAccessor& image, uint8_t red, uint8_t green,
