# HG changeset patch # User Sebastien Jodogne # Date 1589386354 -7200 # Node ID dd112d2b83f0aba87552cf67ccc3b62fad0aedbc # Parent 395c59208f10ca167c26cccda61dd548f230bdf6 new SDK: OrthancPluginGetInstanceAdvancedJson(), OrthancPluginGetInstanceDicomWebJson() and OrthancPluginGetInstanceDicomWebXml() diff -r 395c59208f10 -r dd112d2b83f0 NEWS --- a/NEWS Wed May 13 16:43:09 2020 +0200 +++ b/NEWS Wed May 13 18:12:34 2020 +0200 @@ -21,15 +21,17 @@ ------- * New functions in the SDK: + - OrthancPluginCreateDicomInstance() - OrthancPluginEncodeDicomWebJson2() - OrthancPluginEncodeDicomWebXml2() - - OrthancPluginCreateDicomInstance() - OrthancPluginFreeDicomInstance() + - OrthancPluginGetInstanceDecodedFrame() + - OrthancPluginGetInstanceDicomWebJson() + - OrthancPluginGetInstanceDicomWebXml() - OrthancPluginGetInstanceFramesCount() - OrthancPluginGetInstanceRawFrame() - - OrthancPluginGetInstanceDecodedFrame() + - OrthancPluginSerializeDicomInstance() - OrthancPluginTranscodeDicomInstance() - - OrthancPluginSerializeDicomInstance() * "OrthancPluginDicomInstance" structure wrapped in "OrthancPluginCppWrapper.h" Maintenance diff -r 395c59208f10 -r dd112d2b83f0 Plugins/Engine/OrthancPlugins.cpp --- a/Plugins/Engine/OrthancPlugins.cpp Wed May 13 16:43:09 2020 +0200 +++ b/Plugins/Engine/OrthancPlugins.cpp Wed May 13 18:12:34 2020 +0200 @@ -368,17 +368,18 @@ } public: - DicomWebBinaryFormatter(const _OrthancPluginEncodeDicomWeb& parameters) : - oldCallback_(parameters.callback), + DicomWebBinaryFormatter(OrthancPluginDicomWebBinaryCallback callback) : + oldCallback_(callback), newCallback_(NULL), newPayload_(NULL) { } - DicomWebBinaryFormatter(const _OrthancPluginEncodeDicomWeb2& parameters) : + DicomWebBinaryFormatter(OrthancPluginDicomWebBinaryCallback2 callback, + void* payload) : oldCallback_(NULL), - newCallback_(parameters.callback), - newPayload_(parameters.payload) + newCallback_(callback), + newPayload_(payload) { } @@ -441,6 +442,39 @@ return currentMode_; } } + + void Apply(char** target, + bool isJson, + ParsedDicomFile& dicom) + { + DicomWebJsonVisitor visitor; + visitor.SetFormatter(*this); + + dicom.Apply(visitor); + + std::string s; + + if (isJson) + { + s = visitor.GetResult().toStyledString(); + } + else + { + visitor.FormatXml(s); + } + + *target = CopyString(s); + } + + + void Apply(char** target, + bool isJson, + const void* dicom, + size_t dicomSize) + { + ParsedDicomFile parsed(dicom, dicomSize); + Apply(target, isJson, parsed); + } }; } @@ -2790,8 +2824,7 @@ else if (hasDecoderPlugin) { // TODO - This call could be speeded up the future, if a - // "decoding context" gets introduced in the decoder plugins - + // "decoding context" gets introduced in the decoder plugins decoded.reset(Decode(instance.GetBufferData(), instance.GetBufferSize(), p.frameIndex)); } else @@ -2818,7 +2851,40 @@ CopyToMemoryBuffer(*p.targetBuffer, serialized); return; } + + case _OrthancPluginService_GetInstanceAdvancedJson: + { + if (p.targetStringToFree == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + Json::Value json; + instance.GetParsedDicomFile().DatasetToJson( + json, Plugins::Convert(p.format), + static_cast(p.flags), p.maxStringLength); + + Json::FastWriter writer; + *p.targetStringToFree = CopyString(writer.write(json)); + + return; + } + + case _OrthancPluginService_GetInstanceDicomWebJson: + case _OrthancPluginService_GetInstanceDicomWebXml: + { + if (p.targetStringToFree == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + + DicomWebBinaryFormatter formatter(p.dicomWebCallback, p.dicomWebPayload); + formatter.Apply(p.targetStringToFree, + (service == _OrthancPluginService_GetInstanceDicomWebJson), + instance.GetParsedDicomFile()); + return; + } + default: throw OrthancException(ErrorCode_InternalError); } @@ -3543,35 +3609,6 @@ } - static void FormatDicomWeb(char** target, - bool isJson, - DicomWebBinaryFormatter& formatter, - const void* dicom, - size_t dicomSize) - { - DicomWebJsonVisitor visitor; - visitor.SetFormatter(formatter); - - { - ParsedDicomFile parsed(dicom, dicomSize); - parsed.Apply(visitor); - } - - std::string s; - - if (isJson) - { - s = visitor.GetResult().toStyledString(); - } - else - { - visitor.FormatXml(s); - } - - *target = CopyString(s); - } - - bool OrthancPlugins::InvokeSafeService(SharedLibrary& plugin, _OrthancPluginService service, const void* parameters) @@ -3733,6 +3770,9 @@ case _OrthancPluginService_GetInstanceRawFrame: case _OrthancPluginService_GetInstanceDecodedFrame: case _OrthancPluginService_SerializeDicomInstance: + case _OrthancPluginService_GetInstanceAdvancedJson: + case _OrthancPluginService_GetInstanceDicomWebJson: + case _OrthancPluginService_GetInstanceDicomWebXml: AccessDicomInstance2(service, parameters); return true; @@ -4253,11 +4293,10 @@ const _OrthancPluginEncodeDicomWeb& p = *reinterpret_cast(parameters); - DicomWebBinaryFormatter formatter(p); - - FormatDicomWeb(p.target, - (service == _OrthancPluginService_EncodeDicomWebJson), - formatter, p.dicom, p.dicomSize); + DicomWebBinaryFormatter formatter(p.callback); + formatter.Apply(p.target, + (service == _OrthancPluginService_EncodeDicomWebJson), + p.dicom, p.dicomSize); return true; } @@ -4267,11 +4306,10 @@ const _OrthancPluginEncodeDicomWeb2& p = *reinterpret_cast(parameters); - DicomWebBinaryFormatter formatter(p); - - FormatDicomWeb(p.target, - (service == _OrthancPluginService_EncodeDicomWebJson2), - formatter, p.dicom, p.dicomSize); + DicomWebBinaryFormatter formatter(p.callback, p.payload); + formatter.Apply(p.target, + (service == _OrthancPluginService_EncodeDicomWebJson2), + p.dicom, p.dicomSize); return true; } diff -r 395c59208f10 -r dd112d2b83f0 Plugins/Include/orthanc/OrthancCPlugin.h --- a/Plugins/Include/orthanc/OrthancCPlugin.h Wed May 13 16:43:09 2020 +0200 +++ b/Plugins/Include/orthanc/OrthancCPlugin.h Wed May 13 18:12:34 2020 +0200 @@ -511,6 +511,9 @@ _OrthancPluginService_GetInstanceDecodedFrame = 4014, /* New in Orthanc 1.7.0 */ _OrthancPluginService_TranscodeDicomInstance = 4015, /* New in Orthanc 1.7.0 */ _OrthancPluginService_SerializeDicomInstance = 4016, /* New in Orthanc 1.7.0 */ + _OrthancPluginService_GetInstanceAdvancedJson = 4017, /* New in Orthanc 1.7.0 */ + _OrthancPluginService_GetInstanceDicomWebJson = 4018, /* New in Orthanc 1.7.0 */ + _OrthancPluginService_GetInstanceDicomWebXml = 4019, /* New in Orthanc 1.7.0 */ /* Services for plugins implementing a database back-end */ _OrthancPluginService_RegisterDatabaseBackend = 5000, @@ -7740,11 +7743,17 @@ typedef struct { - uint32_t* targetUint32; - OrthancPluginMemoryBuffer* targetBuffer; - OrthancPluginImage** targetImage; - const OrthancPluginDicomInstance* instance; - uint32_t frameIndex; + uint32_t* targetUint32; + OrthancPluginMemoryBuffer* targetBuffer; + OrthancPluginImage** targetImage; + char** targetStringToFree; + const OrthancPluginDicomInstance* instance; + uint32_t frameIndex; + OrthancPluginDicomToJsonFormat format; + OrthancPluginDicomToJsonFlags flags; + uint32_t maxStringLength; + OrthancPluginDicomWebBinaryCallback2 dicomWebCallback; + void* dicomWebPayload; } _OrthancPluginAccessDicomInstance2; ORTHANC_PLUGIN_INLINE uint32_t OrthancPluginGetInstanceFramesCount( @@ -7845,6 +7854,105 @@ return context->InvokeService(context, _OrthancPluginService_SerializeDicomInstance, ¶ms); } + /** + * @brief Format a DICOM memory buffer as a JSON string. + * + * This function takes as input a memory buffer containing a DICOM + * file, and outputs a JSON string representing the tags of this + * DICOM file. + * + * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). + * @param instance The DICOM instance of interest. + * @param format The output format. + * @param flags Flags governing the output. + * @param maxStringLength The maximum length of a field. Too long fields will + * be output as "null". The 0 value means no maximum length. + * @return The NULL value if the case of an error, or the JSON + * string. This string must be freed by OrthancPluginFreeString(). + * @ingroup Toolbox + * @see OrthancPluginDicomBufferToJson + **/ + ORTHANC_PLUGIN_INLINE char* OrthancPluginGetInstanceAdvancedJson( + OrthancPluginContext* context, + const OrthancPluginDicomInstance* instance, + OrthancPluginDicomToJsonFormat format, + OrthancPluginDicomToJsonFlags flags, + uint32_t maxStringLength) + { + char* result = NULL; + + _OrthancPluginAccessDicomInstance2 params; + memset(¶ms, 0, sizeof(params)); + params.targetStringToFree = &result; + params.instance = instance; + params.format = format; + params.flags = flags; + params.maxStringLength = maxStringLength; + + if (context->InvokeService(context, _OrthancPluginService_GetInstanceAdvancedJson, ¶ms) != OrthancPluginErrorCode_Success) + { + /* Error */ + return NULL; + } + else + { + return result; + } + } + + + ORTHANC_PLUGIN_INLINE char* OrthancPluginGetInstanceDicomWebJson( + OrthancPluginContext* context, + const OrthancPluginDicomInstance* instance, + OrthancPluginDicomWebBinaryCallback2 callback, + void* payload) + { + char* target = NULL; + + _OrthancPluginAccessDicomInstance2 params; + params.targetStringToFree = ⌖ + params.instance = instance; + params.dicomWebCallback = callback; + params.dicomWebPayload = payload; + + if (context->InvokeService(context, _OrthancPluginService_GetInstanceDicomWebJson, ¶ms) != OrthancPluginErrorCode_Success) + { + /* Error */ + return NULL; + } + else + { + return target; + } + } + + + ORTHANC_PLUGIN_INLINE char* OrthancPluginGetInstanceDicomWebXml( + OrthancPluginContext* context, + const OrthancPluginDicomInstance* instance, + OrthancPluginDicomWebBinaryCallback2 callback, + void* payload) + { + char* target = NULL; + + _OrthancPluginAccessDicomInstance2 params; + params.targetStringToFree = ⌖ + params.instance = instance; + params.dicomWebCallback = callback; + params.dicomWebPayload = payload; + + if (context->InvokeService(context, _OrthancPluginService_GetInstanceDicomWebXml, ¶ms) != OrthancPluginErrorCode_Success) + { + /* Error */ + return NULL; + } + else + { + return target; + } + } + + #ifdef __cplusplus } #endif