comparison Plugins/Engine/OrthancPlugins.cpp @ 3928:4cdc875510d1 transcoding

ServerContext::DecodeDicomFrame()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 14 May 2020 13:31:05 +0200
parents 2910b0d30fe0
children 7dc5e7e0045d
comparison
equal deleted inserted replaced
3926:2910b0d30fe0 3928:4cdc875510d1
60 #include "../../Core/Logging.h" 60 #include "../../Core/Logging.h"
61 #include "../../Core/MetricsRegistry.h" 61 #include "../../Core/MetricsRegistry.h"
62 #include "../../Core/OrthancException.h" 62 #include "../../Core/OrthancException.h"
63 #include "../../Core/SerializationToolbox.h" 63 #include "../../Core/SerializationToolbox.h"
64 #include "../../Core/Toolbox.h" 64 #include "../../Core/Toolbox.h"
65 #include "../../OrthancServer/DefaultDicomImageDecoder.h"
66 #include "../../OrthancServer/OrthancConfiguration.h" 65 #include "../../OrthancServer/OrthancConfiguration.h"
67 #include "../../OrthancServer/OrthancFindRequestHandler.h" 66 #include "../../OrthancServer/OrthancFindRequestHandler.h"
68 #include "../../OrthancServer/Search/HierarchicalMatcher.h" 67 #include "../../OrthancServer/Search/HierarchicalMatcher.h"
69 #include "../../OrthancServer/ServerContext.h" 68 #include "../../OrthancServer/ServerContext.h"
70 #include "../../OrthancServer/ServerToolbox.h" 69 #include "../../OrthancServer/ServerToolbox.h"
2755 static OrthancPluginImage* ReturnImage(std::unique_ptr<ImageAccessor>& image) 2754 static OrthancPluginImage* ReturnImage(std::unique_ptr<ImageAccessor>& image)
2756 { 2755 {
2757 // Images returned to plugins are assumed to be writeable. If the 2756 // Images returned to plugins are assumed to be writeable. If the
2758 // input image is read-only, we return a copy so that it can be modified. 2757 // input image is read-only, we return a copy so that it can be modified.
2759 2758
2759 if (image.get() == NULL)
2760 {
2761 throw OrthancException(ErrorCode_NullPointer);
2762 }
2763
2760 if (image->IsReadOnly()) 2764 if (image->IsReadOnly())
2761 { 2765 {
2762 std::unique_ptr<Image> copy(new Image(image->GetFormat(), image->GetWidth(), image->GetHeight(), false)); 2766 std::unique_ptr<Image> copy(new Image(image->GetFormat(), image->GetWidth(), image->GetHeight(), false));
2763 ImageProcessing::Copy(*copy, *image); 2767 ImageProcessing::Copy(*copy, *image);
2764 image.reset(NULL); 2768 image.reset(NULL);
2808 return; 2812 return;
2809 } 2813 }
2810 2814
2811 case _OrthancPluginService_GetInstanceDecodedFrame: 2815 case _OrthancPluginService_GetInstanceDecodedFrame:
2812 { 2816 {
2817 if (p.targetImage == NULL)
2818 {
2819 throw OrthancException(ErrorCode_NullPointer);
2820 }
2821
2813 std::unique_ptr<ImageAccessor> decoded; 2822 std::unique_ptr<ImageAccessor> decoded;
2814 if (p.targetImage == NULL) 2823 {
2815 { 2824 PImpl::ServerContextLock lock(*pimpl_);
2816 throw OrthancException(ErrorCode_NullPointer); 2825 decoded.reset(lock.GetContext().DecodeDicomFrame(instance, p.frameIndex));
2817 } 2826 }
2818 else if (HasCustomImageDecoder()) 2827
2819 {
2820 // TODO - This call could be speeded up the future, if a
2821 // "decoding context" gets introduced in the decoder plugins
2822 decoded.reset(Decode(instance.GetBufferData(), instance.GetBufferSize(), p.frameIndex));
2823 }
2824 else
2825 {
2826 decoded.reset(DicomImageDecoder::Decode(instance.GetParsedDicomFile(), p.frameIndex));
2827 }
2828
2829 *(p.targetImage) = ReturnImage(decoded); 2828 *(p.targetImage) = ReturnImage(decoded);
2830 return; 2829 return;
2831 } 2830 }
2832 2831
2833 case _OrthancPluginService_SerializeDicomInstance: 2832 case _OrthancPluginService_SerializeDicomInstance:
2857 instance.GetParsedDicomFile().DatasetToJson( 2856 instance.GetParsedDicomFile().DatasetToJson(
2858 json, Plugins::Convert(p.format), 2857 json, Plugins::Convert(p.format),
2859 static_cast<DicomToJsonFlags>(p.flags), p.maxStringLength); 2858 static_cast<DicomToJsonFlags>(p.flags), p.maxStringLength);
2860 2859
2861 Json::FastWriter writer; 2860 Json::FastWriter writer;
2862 *p.targetStringToFree = CopyString(writer.write(json)); 2861 *p.targetStringToFree = CopyString(writer.write(json));
2863
2864 return; 2862 return;
2865 } 2863 }
2866 2864
2867 case _OrthancPluginService_GetInstanceDicomWebJson: 2865 case _OrthancPluginService_GetInstanceDicomWebJson:
2868 case _OrthancPluginService_GetInstanceDicomWebXml: 2866 case _OrthancPluginService_GetInstanceDicomWebXml:
4823 boost::shared_lock<boost::shared_mutex> lock(pimpl_->decodeImageCallbackMutex_); 4821 boost::shared_lock<boost::shared_mutex> lock(pimpl_->decodeImageCallbackMutex_);
4824 return !pimpl_->decodeImageCallbacks_.empty(); 4822 return !pimpl_->decodeImageCallbacks_.empty();
4825 } 4823 }
4826 4824
4827 4825
4828 ImageAccessor* OrthancPlugins::DecodeUnsafe(const void* dicom, 4826 ImageAccessor* OrthancPlugins::Decode(const void* dicom,
4829 size_t size, 4827 size_t size,
4830 unsigned int frame) 4828 unsigned int frame)
4831 { 4829 {
4832 boost::shared_lock<boost::shared_mutex> lock(pimpl_->decodeImageCallbackMutex_); 4830 boost::shared_lock<boost::shared_mutex> lock(pimpl_->decodeImageCallbackMutex_);
4833 4831
4834 for (PImpl::DecodeImageCallbacks::const_iterator 4832 for (PImpl::DecodeImageCallbacks::const_iterator
4835 decoder = pimpl_->decodeImageCallbacks_.begin(); 4833 decoder = pimpl_->decodeImageCallbacks_.begin();
4842 return reinterpret_cast<ImageAccessor*>(pluginImage); 4840 return reinterpret_cast<ImageAccessor*>(pluginImage);
4843 } 4841 }
4844 } 4842 }
4845 4843
4846 return NULL; 4844 return NULL;
4847 }
4848
4849
4850 ImageAccessor* OrthancPlugins::Decode(const void* dicom,
4851 size_t size,
4852 unsigned int frame)
4853 {
4854 ImageAccessor* result = DecodeUnsafe(dicom, size, frame);
4855
4856 if (result != NULL)
4857 {
4858 return result;
4859 }
4860 else
4861 {
4862 LOG(INFO) << "The installed image decoding plugins cannot handle an image, fallback to the built-in decoder";
4863 DefaultDicomImageDecoder defaultDecoder;
4864 return defaultDecoder.Decode(dicom, size, frame);
4865 }
4866 } 4845 }
4867 4846
4868 4847
4869 bool OrthancPlugins::IsAllowed(HttpMethod method, 4848 bool OrthancPlugins::IsAllowed(HttpMethod method,
4870 const char* uri, 4849 const char* uri,