changeset 1829:31df3b48925e

OrthancPluginDecodeDicomImage
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Nov 2015 17:35:52 +0100
parents a71d74987090
children 2921384cc352
files NEWS Plugins/Engine/OrthancPlugins.cpp Plugins/Include/orthanc/OrthancCPlugin.h
diffstat 3 files changed, 87 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Wed Nov 25 16:56:24 2015 +0100
+++ b/NEWS	Wed Nov 25 17:35:52 2015 +0100
@@ -45,6 +45,7 @@
   - "OrthancPluginRestApiGet2()" to provide HTTP headers when calling Orthanc API
   - "OrthancPluginGetInstanceOrigin()" to know through which mechanism an instance was received
   - "OrthancPluginCreateImage()" and "OrthancPluginCreateImageAccessor()" to create images
+  - "OrthancPluginDecodeDicomImage()" to decode DICOM images
 * New events in change callbacks:
   - "OrthancStarted"
   - "OrthancStopped"
--- a/Plugins/Engine/OrthancPlugins.cpp	Wed Nov 25 16:56:24 2015 +0100
+++ b/Plugins/Engine/OrthancPlugins.cpp	Wed Nov 25 17:35:52 2015 +0100
@@ -1285,6 +1285,13 @@
         break;
       }
 
+      case OrthancPluginImageFormat_Dicom:
+      {
+        ParsedDicomFile dicom(p.data, p.size);
+        image.reset(Decode(dicom, 0));
+        break;
+      }
+
       default:
         throw OrthancException(ErrorCode_ParameterOutOfRange);
     }
@@ -1456,10 +1463,18 @@
       *reinterpret_cast<const _OrthancPluginCreateDicom*>(parameters);
 
     Json::Value json;
-    Json::Reader reader;
-    if (!reader.parse(p.json, json))
+
+    if (p.json == NULL)
     {
-      throw OrthancException(ErrorCode_BadJson);
+      json = Json::objectValue;
+    }
+    else
+    {
+      Json::Reader reader;
+      if (!reader.parse(p.json, json))
+      {
+        throw OrthancException(ErrorCode_BadJson);
+      }
     }
 
     std::string dicom;
@@ -1488,14 +1503,26 @@
 
     std::auto_ptr<ImageAccessor> result;
 
-    if (service == _OrthancPluginService_CreateImage)
+    switch (service)
     {
-      result.reset(new Image(Plugins::Convert(p.format), p.width, p.height));
-    }
-    else
-    {
-      result.reset(new ImageAccessor);
-      result->AssignWritable(Plugins::Convert(p.format), p.width, p.height, p.pitch, p.buffer);
+      case _OrthancPluginService_CreateImage:
+        result.reset(new Image(Plugins::Convert(p.format), p.width, p.height));
+        break;
+
+      case _OrthancPluginService_CreateImageAccessor:
+        result.reset(new ImageAccessor);
+        result->AssignWritable(Plugins::Convert(p.format), p.width, p.height, p.pitch, p.buffer);
+        break;
+
+      case _OrthancPluginService_DecodeDicomImage:
+      {
+        ParsedDicomFile dicom(p.constBuffer, p.bufferSize);
+        result.reset(Decode(dicom, p.frameIndex));
+        break;
+      }
+
+      default:
+        throw OrthancException(ErrorCode_InternalError);
     }
 
     *(p.target) = reinterpret_cast<OrthancPluginImage*>(result.release());
@@ -2069,6 +2096,7 @@
 
       case _OrthancPluginService_CreateImage:
       case _OrthancPluginService_CreateImageAccessor:
+      case _OrthancPluginService_DecodeDicomImage:
         ApplyCreateImage(service, parameters);
         return true;
 
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Wed Nov 25 16:56:24 2015 +0100
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Wed Nov 25 17:35:52 2015 +0100
@@ -473,6 +473,7 @@
     _OrthancPluginService_DrawText = 6011,
     _OrthancPluginService_CreateImage = 6012,
     _OrthancPluginService_CreateImageAccessor = 6013,
+    _OrthancPluginService_DecodeDicomImage = 6014,
 
     /* Primitives for handling worklists */
     _OrthancPluginService_WorklistAddAnswer = 7000,
@@ -624,8 +625,9 @@
    **/
   typedef enum
   {
-    OrthancPluginImageFormat_Png = 0,   /*!< Image compressed using PNG */
-    OrthancPluginImageFormat_Jpeg = 1,  /*!< Image compressed using JPEG */
+    OrthancPluginImageFormat_Png = 0,    /*!< Image compressed using PNG */
+    OrthancPluginImageFormat_Jpeg = 1,   /*!< Image compressed using JPEG */
+    OrthancPluginImageFormat_Dicom = 2,  /*!< Image compressed using DICOM */
 
     _OrthancPluginImageFormat_INTERNAL = 0x7fffffff
   } OrthancPluginImageFormat;
@@ -4410,6 +4412,9 @@
     uint32_t                   height;
     uint32_t                   pitch;
     void*                      buffer;
+    const void*                constBuffer;
+    uint32_t                   bufferSize;
+    uint32_t                   frameIndex;
   } _OrthancPluginCreateImage;
 
 
@@ -4498,6 +4503,47 @@
   }
 
 
+
+  /**
+   * @brief Decode one frame from a DICOM instance.
+   *
+   * This function decodes one frame of a DICOM image that is stored
+   * in a memory buffer. This function will give the same result as
+   * OrthancPluginUncompressImage() for single-frame DICOM images.
+   *
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @param buffer Pointer to a memory buffer containing the DICOM image.
+   * @param bufferSize Size of the memory buffer containing the DICOM image.
+   * @param frameIndex The index of the frame of interest in a multi-frame image.
+   * @return The uncompressed image. It must be freed with OrthancPluginFreeImage().
+   * @ingroup Images
+   **/
+  ORTHANC_PLUGIN_INLINE OrthancPluginImage* OrthancPluginDecodeDicomImage(
+    OrthancPluginContext*  context,
+    const void*            buffer,
+    uint32_t               bufferSize,
+    uint32_t               frameIndex)
+  {
+    OrthancPluginImage* target = NULL;
+
+    _OrthancPluginCreateImage params;
+    memset(&params, 0, sizeof(params));
+    params.target = &target;
+    params.constBuffer = buffer;
+    params.bufferSize = bufferSize;
+    params.frameIndex = frameIndex;
+
+    if (context->InvokeService(context, _OrthancPluginService_DecodeDicomImage, &params) != OrthancPluginErrorCode_Success)
+    {
+      return NULL;
+    }
+    else
+    {
+      return target;
+    }
+  }
+
+
 #ifdef  __cplusplus
 }
 #endif