# HG changeset patch # User Sebastien Jodogne # Date 1448383592 -3600 # Node ID 580951a3358376a146f0a4c7d3c9ff57bf7a27bc # Parent 2d8191b135674fc6c18a7040f844790c6e312a58 OrthancPluginDicomFromJson diff -r 2d8191b13567 -r 580951a33583 Plugins/Engine/OrthancPlugins.cpp --- a/Plugins/Engine/OrthancPlugins.cpp Tue Nov 24 17:24:37 2015 +0100 +++ b/Plugins/Engine/OrthancPlugins.cpp Tue Nov 24 17:46:32 2015 +0100 @@ -414,6 +414,7 @@ OrthancPlugins::OrthancPlugins() { + /* Sanity check of the compiler */ if (sizeof(int32_t) != sizeof(OrthancPluginErrorCode) || sizeof(int32_t) != sizeof(OrthancPluginHttpMethod) || sizeof(int32_t) != sizeof(_OrthancPluginService) || @@ -427,6 +428,7 @@ sizeof(int32_t) != sizeof(OrthancPluginValueRepresentation) || sizeof(int32_t) != sizeof(OrthancPluginDicomToJsonFlags) || sizeof(int32_t) != sizeof(OrthancPluginDicomToJsonFormat) || + sizeof(int32_t) != sizeof(OrthancPluginDicomFromJsonFlags) || sizeof(int32_t) != sizeof(_OrthancPluginDatabaseAnswerType) || sizeof(int32_t) != sizeof(OrthancPluginIdentifierConstraint) || sizeof(int32_t) != sizeof(OrthancPluginInstanceOrigin) || @@ -435,9 +437,11 @@ static_cast(OrthancPluginDicomToJsonFlags_IncludeUnknownTags) != static_cast(DicomToJsonFlags_IncludeUnknownTags) || static_cast(OrthancPluginDicomToJsonFlags_IncludePixelData) != static_cast(DicomToJsonFlags_IncludePixelData) || static_cast(OrthancPluginDicomToJsonFlags_ConvertBinaryToNull) != static_cast(DicomToJsonFlags_ConvertBinaryToNull) || - static_cast(OrthancPluginDicomToJsonFlags_ConvertBinaryToAscii) != static_cast(DicomToJsonFlags_ConvertBinaryToAscii)) + static_cast(OrthancPluginDicomToJsonFlags_ConvertBinaryToAscii) != static_cast(DicomToJsonFlags_ConvertBinaryToAscii) || + static_cast(OrthancPluginDicomFromJsonFlags_DecodeDataUriScheme) != static_cast(DicomFromJsonFlags_DecodeDataUriScheme) || + static_cast(OrthancPluginDicomFromJsonFlags_GenerateIdentifiers) != static_cast(DicomFromJsonFlags_GenerateIdentifiers)) + { - /* Sanity check of the compiler */ throw OrthancException(ErrorCode_Plugin); } @@ -1413,6 +1417,31 @@ } + void OrthancPlugins::ApplyDicomFromJson(_OrthancPluginService service, + const void* parameters) + { + const _OrthancPluginDicomFromJson& p = + *reinterpret_cast(parameters); + + Json::Value json; + Json::Reader reader; + if (!reader.parse(p.json, json)) + { + throw OrthancException(ErrorCode_BadJson); + } + + std::string dicom; + + { + std::auto_ptr file + (ParsedDicomFile::CreateFromJson(json, static_cast(p.flags))); + file->SaveToMemoryBuffer(dicom); + } + + CopyToMemoryBuffer(*p.target, dicom); + } + + void OrthancPlugins::DatabaseAnswer(const void* parameters) { const _OrthancPluginDatabaseAnswer& p = @@ -1938,6 +1967,10 @@ ApplyDicomToJson(service, parameters); return true; + case _OrthancPluginService_DicomFromJson: + ApplyDicomFromJson(service, parameters); + return true; + case _OrthancPluginService_WorklistAddAnswer: { const _OrthancPluginWorklistAnswersOperation& p = diff -r 2d8191b13567 -r 580951a33583 Plugins/Engine/OrthancPlugins.h --- a/Plugins/Engine/OrthancPlugins.h Tue Nov 24 17:24:37 2015 +0100 +++ b/Plugins/Engine/OrthancPlugins.h Tue Nov 24 17:46:32 2015 +0100 @@ -140,6 +140,9 @@ void ApplyDicomToJson(_OrthancPluginService service, const void* parameters); + void ApplyDicomFromJson(_OrthancPluginService service, + const void* parameters); + void SignalChangeInternal(OrthancPluginChangeType changeType, OrthancPluginResourceType resourceType, const char* resource); diff -r 2d8191b13567 -r 580951a33583 Plugins/Include/orthanc/OrthancCPlugin.h --- a/Plugins/Include/orthanc/OrthancCPlugin.h Tue Nov 24 17:24:37 2015 +0100 +++ b/Plugins/Include/orthanc/OrthancCPlugin.h Tue Nov 24 17:46:32 2015 +0100 @@ -396,6 +396,7 @@ _OrthancPluginService_RegisterDictionaryTag = 20, _OrthancPluginService_DicomBufferToJson = 21, _OrthancPluginService_DicomInstanceToJson = 22, + _OrthancPluginService_DicomFromJson = 23, /* Registration of callbacks */ _OrthancPluginService_RegisterRestCallback = 1000, @@ -697,6 +698,19 @@ /** + * Flags to customize a JSON-to-DICOM conversion. + * @ingroup Toolbox + **/ + typedef enum + { + OrthancPluginDicomFromJsonFlags_DecodeDataUriScheme = (1 << 0), /*!< Decode fields encoded using data URI scheme */ + OrthancPluginDicomFromJsonFlags_GenerateIdentifiers = (1 << 1), /*!< Automatically generate DICOM identifiers */ + + _OrthancPluginDicomFromJsonFlags_INTERNAL = 0x7fffffff + } OrthancPluginDicomFromJsonFlags; + + + /** * The constraints on the DICOM identifiers that must be supported * by the database plugins. **/ @@ -977,6 +991,7 @@ sizeof(int32_t) != sizeof(OrthancPluginValueRepresentation) || sizeof(int32_t) != sizeof(OrthancPluginDicomToJsonFormat) || sizeof(int32_t) != sizeof(OrthancPluginDicomToJsonFlags) || + sizeof(int32_t) != sizeof(OrthancPluginDicomFromJsonFlags) || sizeof(int32_t) != sizeof(OrthancPluginIdentifierConstraint) || sizeof(int32_t) != sizeof(OrthancPluginInstanceOrigin)) { @@ -1313,7 +1328,7 @@ * file is stored into a newly allocated memory buffer. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param instanceId The Orthanc identifier of the DICOM instance of interest. * @return 0 if success, or the error code if failure. * @ingroup Orthanc @@ -1344,7 +1359,7 @@ * the query is stored into a newly allocated memory buffer. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param uri The URI in the built-in Orthanc API. * @return 0 if success, or the error code if failure. * @see OrthancPluginRestApiGetAfterPlugins @@ -1373,7 +1388,7 @@ * query is stored into a newly allocated memory buffer. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param uri The URI in the built-in Orthanc API. * @return 0 if success, or the error code if failure. * @see OrthancPluginRestApiGet @@ -1407,7 +1422,7 @@ * the query is stored into a newly allocated memory buffer. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param uri The URI in the built-in Orthanc API. * @param body The body of the POST request. * @param bodySize The size of the body. @@ -1441,7 +1456,7 @@ * query is stored into a newly allocated memory buffer. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param uri The URI in the built-in Orthanc API. * @param body The body of the POST request. * @param bodySize The size of the body. @@ -1515,7 +1530,7 @@ * the query is stored into a newly allocated memory buffer. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param uri The URI in the built-in Orthanc API. * @param body The body of the PUT request. * @param bodySize The size of the body. @@ -1550,7 +1565,7 @@ * query is stored into a newly allocated memory buffer. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param uri The URI in the built-in Orthanc API. * @param body The body of the PUT request. * @param bodySize The size of the body. @@ -2731,7 +2746,7 @@ * version of the zlib library that is used by the Orthanc core. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param source The source buffer. * @param size The size in bytes of the source buffer. * @param compression The compression algorithm. @@ -2773,7 +2788,7 @@ * a newly allocated memory buffer. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param path The path of the file to be read. * @return 0 if success, or the error code if failure. **/ @@ -3331,7 +3346,7 @@ * Orthanc instance that hosts this plugin. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param url The URL of interest. * @param username The username (can be NULL if no password protection). * @param password The password (can be NULL if no password protection). @@ -3366,7 +3381,7 @@ * the Orthanc instance that hosts this plugin. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param url The URL of interest. * @param body The content of the body of the request. * @param bodySize The size of the body of the request. @@ -3407,7 +3422,7 @@ * Orthanc instance that hosts this plugin. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param url The URL of interest. * @param body The content of the body of the request. * @param bodySize The size of the body of the request. @@ -3956,7 +3971,7 @@ * @param buffer The memory buffer containing the DICOM file. * @param size The size of the memory buffer. * @param format The output format. - * @param flags The output flags. + * @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 @@ -4005,7 +4020,7 @@ * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). * @param instanceId The Orthanc identifier of the instance. * @param format The output format. - * @param flags The output flags. + * @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 @@ -4060,7 +4075,7 @@ * allocated memory buffer. * * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). - * @param target The target memory buffer. + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). * @param uri The URI in the built-in Orthanc API. * @param headersCount The number of HTTP headers. * @param headersKeys Array containing the keys of the HTTP headers. @@ -4299,6 +4314,43 @@ } + typedef struct + { + OrthancPluginMemoryBuffer* target; + const char* json; + OrthancPluginDicomFromJsonFlags flags; + } _OrthancPluginDicomFromJson; + + /** + * @brief Create a DICOM instance from JSON. + * + * This function takes as input a string containing a JSON file + * describing the content of a DICOM instance. As an output, it + * writes the corresponding DICOM instance to a newly allocated + * memory buffer. + * + * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). + * @param target The target memory buffer. It must be freed with OrthancPluginFreeMemoryBuffer(). + * @param json The input JSON file. + * @param flags Flags governing the output. + * @return 0 if success, other value if error. + * @ingroup Toolbox + * @see OrthancPluginDicomBufferToJson + **/ + ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode OrthancPluginDicomFromJson( + OrthancPluginContext* context, + OrthancPluginMemoryBuffer* target, + const char* json, + OrthancPluginDicomFromJsonFlags flags) + { + _OrthancPluginDicomFromJson params; + params.target = target; + params.json = json; + params.flags = flags; + + return context->InvokeService(context, _OrthancPluginService_DicomFromJson, ¶ms); + } + #ifdef __cplusplus } #endif