⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gdcmfile.cxx

📁 DTMK软件开发包,此为开源软件,是一款很好的医学图像开发资源.
💻 CXX
📖 第 1 页 / 共 5 页
字号:
      return 0.;
   }

   return yImPos;
}

/**
 * \brief gets the info from 0020,0032 : Image Position Patient
 *                 else from 0020,0030 : Image Position (RET)
 *                 else from 0020,1041 : Slice Location
 *                 else from 0020,0050 : Location
 *                 else 0.
 * @return up-left image corner Z position
 */
float File::GetZOrigin()
{
   float xImPos, yImPos, zImPos; 
   std::string strImPos = GetEntryValue(0x0020,0x0032);

   if ( strImPos != GDCM_UNFOUND )
   {
      if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos) != 3)
      {
         gdcmWarningMacro( "Wrong Image Position Patient (0020,0032)");
         return 0.;  // bug in the element 0x0020,0x0032
      }
      else
      {
         return zImPos;
      }
   }

   strImPos = GetEntryValue(0x0020,0x0030); // For ACR-NEMA images
   if ( strImPos != GDCM_UNFOUND )
   {
      if ( sscanf( strImPos.c_str(), 
          "%f \\%f \\%f ", &xImPos, &yImPos, &zImPos ) != 3 )
      {
         gdcmWarningMacro( "Wrong Image Position (RET) (0020,0030)");
         return 0.;  // bug in the element 0x0020,0x0032
      }
      else
      {
         return zImPos;
      }
   }

   // for *very* old ACR-NEMA images
   std::string strSliceLocation = GetEntryValue(0x0020,0x1041);
   if ( strSliceLocation != GDCM_UNFOUND )
   {
      if ( sscanf( strSliceLocation.c_str(), "%f ", &zImPos) != 1)
      {
         gdcmWarningMacro( "Wrong Slice Location (0020,1041)");
         return 0.;  // bug in the element 0x0020,0x1041
      }
      else
      {
         return zImPos;
      }
   }
   gdcmWarningMacro( "Unfound Slice Location (0020,1041)");

   std::string strLocation = GetEntryValue(0x0020,0x0050);
   if ( strLocation != GDCM_UNFOUND )
   {
      if ( sscanf( strLocation.c_str(), "%f ", &zImPos) != 1 )
      {
         gdcmWarningMacro( "Wrong Location (0020,0050)");
         return 0.;  // bug in the element 0x0020,0x0050
      }
      else
      {
         return zImPos;
      }
   }
   gdcmWarningMacro( "Unfound Location (0020,0050)");  

   return 0.; // Hopeless
}

/**
  * \brief gets the info from 0020,0037 : Image Orientation Patient
  *                   or from 0020 0035 : Image Orientation (RET)
  * (needed to organize DICOM files based on their x,y,z position)
  * @param iop adress of the (6)float array to receive values
  * @return true when one of the tag is found
  *         false when nothing is found
  */
bool File::GetImageOrientationPatient( float iop[6] )
{
   std::string strImOriPat;
   //iop is supposed to be float[6]
   iop[0] = iop[4] = 1.;
   iop[1] = iop[2] = iop[3] = iop[5] = 0.;

   // 0020 0037 DS REL Image Orientation (Patient)
   if ( (strImOriPat = GetEntryValue(0x0020,0x0037)) != GDCM_UNFOUND )
   {
      if ( sscanf( strImOriPat.c_str(), "%f \\ %f \\%f \\%f \\%f \\%f ", 
          &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
      {
         gdcmWarningMacro( "Wrong Image Orientation Patient (0020,0037)."
                        << " Less than 6 values were found." );
         return false;
      }
      return true;
   }
   //For ACR-NEMA
   // 0020 0035 DS REL Image Orientation (RET)
   else if ( (strImOriPat = GetEntryValue(0x0020,0x0035)) != GDCM_UNFOUND )
   {
      if ( sscanf( strImOriPat.c_str(), "%f \\ %f \\%f \\%f \\%f \\%f ", 
          &iop[0], &iop[1], &iop[2], &iop[3], &iop[4], &iop[5]) != 6 )
      {
         gdcmWarningMacro( "wrong Image Orientation Patient (0020,0035). "
                        << "Less than 6 values were found." );
         return false;
      }
      return true;
   }
   return false;
}

/**
 * \brief   Retrieve the number of Bits Stored (actually used)
 *          (as opposed to number of Bits Allocated)
 * @return  The encountered number of Bits Stored, 0 by default.
 *          0 means the file is NOT USABLE. The caller has to check it !
 */
int File::GetBitsStored()
{
   std::string strSize = GetEntryValue( 0x0028, 0x0101 );
   if ( strSize == GDCM_UNFOUND )
   {
      gdcmWarningMacro("BitsStored (0028,0101) is supposed to be mandatory");
      return 0;
   }
   return atoi( strSize.c_str() );
}

/**
 * \brief   Retrieve the number of Bits Allocated
 *          (8, 12 -compacted ACR-NEMA files-, 16, 24 -old RGB ACR-NEMA files-,)
 * @return  The encountered Number of Bits Allocated, 0 by default.
 *          0 means the file is NOT USABLE. The caller has to check it !
 */
int File::GetBitsAllocated()
{
   std::string strSize = GetEntryValue(0x0028,0x0100);
   if ( strSize == GDCM_UNFOUND  )
   {
      gdcmWarningMacro("BitsAllocated (0028,0100) is supposed to be mandatory");
      return 0;
   }
   return atoi( strSize.c_str() );
}

/**
 * \brief   Retrieve the high bit position.
 * \warning The method defaults to 0 when information is missing.
 *          The responsability of checking this value is left to the caller.
 * @return  The high bit position when present. 0 when missing.
 */
int File::GetHighBitPosition()
{
   std::string strSize = GetEntryValue( 0x0028, 0x0102 );
   if ( strSize == GDCM_UNFOUND )
   {
      gdcmWarningMacro("HighBitPosition (0028,0102) is supposed to be mandatory");
      return 0;
   }
   return atoi( strSize.c_str() );
}

/**
 * \brief   Retrieve the number of Samples Per Pixel
 *          (1 : gray level, 3 : RGB/YBR -1 or 3 Planes-)
 * @return  The encountered number of Samples Per Pixel, 1 by default.
 *          (we assume Gray level Pixels)
 */
int File::GetSamplesPerPixel()
{
   const std::string &strSize = GetEntryValue(0x0028,0x0002);
   if ( strSize == GDCM_UNFOUND )
   {
      gdcmWarningMacro("SamplesPerPixel (0028,0002) is supposed to be mandatory");
      return 1; // Well, it's supposed to be mandatory ...
                // but sometimes it's missing : *we* assume Gray pixels
   }
   return atoi( strSize.c_str() );
}

/**
 * \brief   Retrieve the Planar Configuration for RGB images
 *          (0 : RGB Pixels , 1 : R Plane + G Plane + B Plane)
 * @return  The encountered Planar Configuration, 0 by default.
 */
int File::GetPlanarConfiguration()
{
   std::string strSize = GetEntryValue(0x0028,0x0006);
   if ( strSize == GDCM_UNFOUND )
   {
      return 0;
   }
   return atoi( strSize.c_str() );
}

/**
 * \brief   Return the size (in bytes) of a single pixel of data.
 * @return  The size in bytes of a single pixel of data; 0 by default
 *          0 means the file is NOT USABLE; the caller will have to check
 */
int File::GetPixelSize()
{
   // 0028 0100 US IMG Bits Allocated
   // (in order no to be messed up by old ACR-NEMA RGB images)
   assert( !(GetEntryValue(0x0028,0x0100) == "24") );

   std::string pixelType = GetPixelType();
   if ( pixelType ==  "8U" || pixelType == "8S" )
   {
      return 1;
   }
   if ( pixelType == "16U" || pixelType == "16S")
   {
      return 2;
   }
   if ( pixelType == "32U" || pixelType == "32S")
   {
      return 4;
   }
   if ( pixelType == "FD" )
   {
      return 8;
   }
   gdcmWarningMacro( "Unknown pixel type: " << pixelType);
   return 0;
}

/**
 * \brief   Build the Pixel Type of the image.
 *          Possible values are:
 *          - 8U  unsigned  8 bit,
 *          - 8S    signed  8 bit,
 *          - 16U unsigned 16 bit,
 *          - 16S   signed 16 bit,
 *          - 32U unsigned 32 bit,
 *          - 32S   signed 32 bit,
 *          - FD floating double 64 bits (Not kosher DICOM, but so usefull!)
 * \warning 12 bit images appear as 16 bit.
 *          24 bit images appear as 8 bit + photochromatic interp ="RGB "
 *                                        + Planar Configuration = 0
 * @return  0S if nothing found. NOT USABLE file. The caller has to check
 */
std::string File::GetPixelType()
{
   std::string bitsAlloc = GetEntryValue(0x0028, 0x0100); // Bits Allocated
   if ( bitsAlloc == GDCM_UNFOUND )
   {
      gdcmWarningMacro( "Bits Allocated (0028,0100) supposed to be mandatory");
      bitsAlloc = "16"; // default and arbitrary value, not to polute the output
   }

   if ( bitsAlloc == "64" )
   {
      return "FD";
   }
   else if ( bitsAlloc == "12" )
   {
      // It will be unpacked
      bitsAlloc = "16";
   }
   else if ( bitsAlloc == "24" )
   {
      // (in order no to be messed up by old RGB images)
      bitsAlloc = "8";
   }

   int i= atoi(bitsAlloc.c_str());  // fix a bug in some headers
   if ( i > 8 &&  i < 16 )
   {
      bitsAlloc = "16";
   }

   std::string sign = GetEntryValue(0x0028, 0x0103);//"Pixel Representation"

   if (sign == GDCM_UNFOUND )
   {
      gdcmWarningMacro( "Missing Pixel Representation (0028,0103)");
      sign = "U"; // default and arbitrary value, not to polute the output
   }
   else if ( sign == "0" )
   {
      sign = "U";
   }
   else
   {
      sign = "S";
   }
   return bitsAlloc + sign;
}

/**
 * \brief   Check whether the pixels are signed (1) or UNsigned (0) data.
 * \warning The method defaults to false (UNsigned) when tag 0028|0103
 *          is missing.
 *          The responsability of checking this value is left to the caller
 *          (NO transformation is performed on the pixels to make then >0)
 * @return  True when signed, false when UNsigned
 */
bool File::IsSignedPixelData()
{
   std::string strSign = GetEntryValue( 0x0028, 0x0103 );
   if ( strSign == GDCM_UNFOUND )
   {
      gdcmWarningMacro( "Pixel Representation (0028,0103) supposed to be "
                      << "mandatory");
      return false;
   }
   int sign = atoi( strSign.c_str() );
   if ( sign == 0 ) 
   {
      return false;
   }
   return true;
}

/**
 * \brief   Check whether this a monochrome picture (gray levels) or not,
 *          using "Photometric Interpretation" tag (0x0028,0x0004).
 * @return  true when "MONOCHROME1" or "MONOCHROME2". False otherwise.
 */
bool File::IsMonochrome()
{
   const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
   if (  Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1")
      || Util::DicomStringEqual(PhotometricInterp, "MONOCHROME2") )
   {
      return true;
   }
   if ( PhotometricInterp == GDCM_UNFOUND )
   {
      gdcmWarningMacro( "Photometric Interpretation (0028,0004) supposed to be "
                         << "mandatory");
   }
   return false;
}

/**
 * \brief   Check whether this a MONOCHROME1 picture (high values = dark)
 *            or not using "Photometric Interpretation" tag (0x0028,0x0004).
 * @return  true when "MONOCHROME1" . False otherwise.
 */
bool File::IsMonochrome1()
{
   const std::string &PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
   if (  Util::DicomStringEqual(PhotometricInterp, "MONOCHROME1") )
   {
      return true;
   }
   if ( PhotometricInterp == GDCM_UNFOUND )
   {
      gdcmWarningMacro( "Photometric Interpretation (0028,0004) : supposed to"
      << " be mandatory! ");
   }
   return false;
}

/**
 * \brief   Check whether this a "PALETTE COLOR" picture or not by accessing
 *          the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
 * @return  true when "PALETTE COLOR". False otherwise.
 */
bool File::IsPaletteColor()
{
   std::string PhotometricInterp = GetEntryValue( 0x0028, 0x0004 );
   if (   PhotometricInterp == "PALETTE COLOR " )
   {
      return true;
   }
   if ( PhotometricInterp == GDCM_UNFOUND )
   {
      gdcmWarningMacro( "Not found : Palette color (0028,0004)");
   }
   return false;
}

/**
 * \brief   Check whether this a "YBR_FULL" color picture or not by accessing
 *          the "Photometric Interpretation" tag ( 0x0028, 0x0004 ).
 * @return  true when "YBR_FULL". False otherwise.
 */
bool File::IsYBRFull()
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -