changeset 4076:eab8010c05fc framework

avoid relying on boost::bad_lexical_cast in DicomImageInformation
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 19 Jun 2020 18:45:16 +0200
parents ea95eecead6f
children c5c41f66ec29
files OrthancFramework/Sources/DicomFormat/DicomImageInformation.cpp OrthancFramework/Sources/DicomFormat/DicomImageInformation.h
diffstat 2 files changed, 17 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DicomFormat/DicomImageInformation.cpp	Wed Jun 17 18:19:45 2020 +0200
+++ b/OrthancFramework/Sources/DicomFormat/DicomImageInformation.cpp	Fri Jun 19 18:45:16 2020 +0200
@@ -52,8 +52,8 @@
 {
   DicomImageInformation::DicomImageInformation(const DicomMap& values)
   {
-    unsigned int pixelRepresentation;
-    unsigned int planarConfiguration = 0;
+    uint32_t pixelRepresentation = 0;
+    uint32_t planarConfiguration = 0;
 
     try
     {
@@ -120,40 +120,27 @@
       values.GetValue(DICOM_TAG_COLUMNS).ParseFirstUnsignedInteger(width_); // in some US images, we've seen tag values of "800\0"; that's why we parse the 'first' value
       values.GetValue(DICOM_TAG_ROWS).ParseFirstUnsignedInteger(height_);
 
-      bitsAllocated_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_BITS_ALLOCATED).GetContent());
-
-      try
+      if (!values.ParseUnsignedInteger32(bitsAllocated_, DICOM_TAG_BITS_ALLOCATED))
       {
-        samplesPerPixel_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_SAMPLES_PER_PIXEL).GetContent());
+        throw OrthancException(ErrorCode_BadFileFormat);
       }
-      catch (OrthancException&)
+
+      if (!values.ParseUnsignedInteger32(samplesPerPixel_, DICOM_TAG_SAMPLES_PER_PIXEL))
       {
         samplesPerPixel_ = 1;  // Assume 1 color channel
       }
 
-      try
-      {
-        bitsStored_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_BITS_STORED).GetContent());
-      }
-      catch (OrthancException&)
+      if (!values.ParseUnsignedInteger32(bitsStored_, DICOM_TAG_BITS_STORED))
       {
         bitsStored_ = bitsAllocated_;
       }
 
-      try
-      {
-        highBit_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_HIGH_BIT).GetContent());
-      }
-      catch (OrthancException&)
+      if (!values.ParseUnsignedInteger32(highBit_, DICOM_TAG_HIGH_BIT))
       {
         highBit_ = bitsStored_ - 1;
       }
 
-      try
-      {
-        pixelRepresentation = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_PIXEL_REPRESENTATION).GetContent());
-      }
-      catch (OrthancException&)
+      if (!values.ParseUnsignedInteger32(pixelRepresentation, DICOM_TAG_PIXEL_REPRESENTATION))
       {
         pixelRepresentation = 0;  // Assume unsigned pixels
       }
@@ -162,11 +149,8 @@
       {
         // The "Planar Configuration" is only set when "Samples per Pixels" is greater than 1
         // http://dicom.nema.org/medical/dicom/current/output/html/part03.html#sect_C.7.6.3.1.3
-        try
-        {
-          planarConfiguration = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_PLANAR_CONFIGURATION).GetContent());
-        }
-        catch (OrthancException&)
+
+        if (!values.ParseUnsignedInteger32(planarConfiguration, DICOM_TAG_PLANAR_CONFIGURATION))
         {
           planarConfiguration = 0;  // Assume interleaved color channels
         }
@@ -181,13 +165,10 @@
       throw OrthancException(ErrorCode_NotImplemented);
     }
 
+    
     if (values.HasTag(DICOM_TAG_NUMBER_OF_FRAMES))
     {
-      try
-      {
-        numberOfFrames_ = boost::lexical_cast<unsigned int>(values.GetValue(DICOM_TAG_NUMBER_OF_FRAMES).GetContent());
-      }
-      catch (boost::bad_lexical_cast&)
+      if (!values.ParseUnsignedInteger32(numberOfFrames_, DICOM_TAG_NUMBER_OF_FRAMES))
       {
         throw OrthancException(ErrorCode_NotImplemented);
       }
--- a/OrthancFramework/Sources/DicomFormat/DicomImageInformation.h	Wed Jun 17 18:19:45 2020 +0200
+++ b/OrthancFramework/Sources/DicomFormat/DicomImageInformation.h	Fri Jun 19 18:45:16 2020 +0200
@@ -45,15 +45,15 @@
     unsigned int width_;
     unsigned int height_;
     unsigned int samplesPerPixel_;
-    unsigned int numberOfFrames_;
+    uint32_t numberOfFrames_;
 
     bool isPlanar_;
     bool isSigned_;
     size_t bytesPerValue_;
 
-    unsigned int bitsAllocated_;
-    unsigned int bitsStored_;
-    unsigned int highBit_;
+    uint32_t bitsAllocated_;
+    uint32_t bitsStored_;
+    uint32_t highBit_;
 
     PhotometricInterpretation  photometric_;