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

📄 udataset.cpp

📁 dicom file 查看工具 DICOM文件是医疗设备使用的文件格式。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        else if(m_nLength==0) m_pData->Value=NULL;
        else                                          //Problem
        {
          Skip(m_nLength);
        }
 
        m_pList->Add(m_pData);
      }
      else
      {
        ReadLength(VALUE_DWORD);
        m_pData->ValueWidth=m_nLength;

        if(m_nLength>0&&m_nLength<100)
        {
          ReadValue();
          CopyValue();
        }
        else if(!m_nLength) m_pData->Value=NULL;
        else 
        {
          Skip(m_nLength);
        }
        
        m_pList->Add(m_pData);
      }
    }
  }
}

//---------------------------------------------------------------------------
void __fastcall CDataSet::CopyValue(void)
{
  unsigned int i;
  m_pData->Value=new char[m_nLength+1];
  
  for(i=0;i<m_nLength;i++) m_pData->Value[i]=m_pValue[i];
  m_pData->Value[m_nLength]=0;
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::ReadValue(void)
{
  unsigned long nBytesRead;
  ReadFile(m_hFile,m_pValue,m_nLength,&nBytesRead,NULL);
  m_nOffset+=m_nLength;
}
//---------------------------------------------------------------------------
bool __fastcall CDataSet::ReadTag(void)
{
  bool bFlag;
  unsigned long nBytesRead;

  bFlag=ReadFile(m_hFile,&m_nGroup,2,&nBytesRead, NULL);
  bFlag=ReadFile(m_hFile,&m_nElement,2,&nBytesRead,NULL);
  m_nOffset+=4;
  
  return (bFlag&&(nBytesRead!=0));
}
//---------------------------------------------------------------------------
inline void __fastcall CDataSet::Skip(int nStep)
{
  SetFilePointer(m_hFile,nStep,NULL,FILE_CURRENT);
  m_nOffset+=nStep;
}
//---------------------------------------------------------------------------
inline void __fastcall CDataSet::ReadVR(void)
{
  unsigned long nBytesRead;

  ReadFile(m_hFile,m_pValue,2,&nBytesRead,NULL);
  m_nOffset+=2;
}
//---------------------------------------------------------------------------
inline void __fastcall CDataSet::CopyVR(void)
{
  m_pValue[2]=0;
  m_pData->VR=AnsiString(m_pValue);
  m_pData->Type=VRTypeToInt(m_pValue);
}
//---------------------------------------------------------------------------
inline void __fastcall CDataSet::ReadLength(int nWidth)
{
  unsigned long nBytesRead;

  ReadFile(m_hFile,&m_nLength,nWidth,&nBytesRead,NULL);
  m_nOffset+=nWidth;
}
//---------------------------------------------------------------------------
int __fastcall CDataSet::VRTypeToInt(char *pValue)
{
  unsigned int nHigh,nLow;

  nHigh=pValue[1];
  nLow=pValue[0];
  return ((nHigh<<8)+nLow);
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::DSToFloat(TData *pData,float &fFst,float &fSec)
{
  bool bIsDigit;
  int nIndex;
  unsigned long nBytesRead;
  char *pValue;
  int i;
  
  if(!pData->ValueWidth) return;

  pValue=new char[pData->ValueWidth];
  memset(pValue,0,pData->ValueWidth);

  for(nIndex=0;nIndex<pData->ValueWidth;nIndex++)
  {
    if(!IsValid(pData->Value[nIndex])) pData->Value[nIndex]=' ';
  }
  
  bIsDigit=false;
  nIndex=0;
  i=0;
  while(true)
  {
    if(IsValid(pData->Value[nIndex])) pValue[i]=pData->Value[nIndex];
    if((pData->Value[nIndex]>='0')&&(pData->Value[nIndex]<='9')) bIsDigit=true;
    i++;
    nIndex++;
    if ((nIndex>=pData->ValueWidth)||bIsDigit) break;
  }

  if(nIndex<pData->ValueWidth)
  {
    while(true)
    {
      if (!IsValid(pData->Value[nIndex])) bIsDigit=false;
      else
      {
        if (pData->Value[nIndex]=='E') pValue[i]='e';
        else  pValue[i]=pData->Value[nIndex];
      }
      i++;
      nIndex++;
      if ((nIndex>=pData->ValueWidth)||!bIsDigit) break;
    }
  }

  pValue[nIndex]=NULL;
  try
  {
    fFst=StrToFloat(AnsiString(pValue));
  }
  catch(const EConvertError &Error)
  {
    if (pValue) delete []pValue;
    throw CException("Error in decoding! Probably this is some errors in the file!",ERROR_DATASET);
  }

  if (nIndex>=pData->ValueWidth)
  {
    if (pValue) delete []pValue;
    fSec=0;
    return;
  }

  i=0;
  memset(pValue,0,pData->ValueWidth);
  while(true)
  {
    if (IsValid(pData->Value[nIndex]))
    {
      if (pData->Value[nIndex]=='E') pValue[i]='e';
      else pValue[i]=pData->Value[nIndex];
    }
    if((pData->Value[nIndex]>='0')&&(pData->Value[nIndex]<='9')) bIsDigit=true;
    nIndex++;
    i++;
    if ((nIndex>=pData->ValueWidth)||(bIsDigit&&(pData->Value[nIndex]==' '))) break;
  }

  if (!bIsDigit)
  {
    if (pValue) delete []pValue;
    fSec=0;
    return;
  }
  
  pValue[nIndex]=NULL;
  try
  {
    fSec=StrToFloat(AnsiString(pValue));
  }
  catch(const EConvertError &Error)
  {
    if (pValue) delete []pValue;
    throw CException("Error in decoding! Probably this is some errors in the file!",ERROR_DATASET);
  }

  if (pValue) delete []pValue;
}
//---------------------------------------------------------------------------
bool CDataSet::IsValid(char Ch)
{
  return ((Ch=='.')||(Ch=='+')||(Ch=='-')||(Ch=='e')||(Ch=='E')||((Ch>='0')&&(Ch<='9')));
}
//---------------------------------------------------------------------------
int __fastcall CDataSet::Bind(void)
{
  m_Item="(,)";
  FormatHex(m_pValue,m_nGroup);
  m_Item.Insert((AnsiString)(char *)m_pValue,2);

  FormatHex(m_pValue,m_nElement);
  m_Item.Insert((AnsiString)(char *)m_pValue,7);
  return ((m_nGroup<<16)+m_nElement);
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::GetImageInfomation(void)
{
  int i;
  float fFst,fSec;
  
  for (i=0;i<m_pList->Count;i++)
  {
    m_pData=(TData *)m_pList->Items[i];
    if(!m_pData->Value) continue;

    switch(m_pData->TagCode)
    {
      case 0x00080060:
           m_pImageInformation->AsImageModality=AnsiString(m_pData->Value);
           break;
      case 0x00280004:
           m_pImageInformation->nImageColorType=ColorTypeToInt(m_pData->Value,m_pData->ValueWidth);
           break;
      case 0x00280008:
           m_pImageInformation->nImageFrameCount=ISToInt(m_pData->Value,m_pData->ValueWidth);
           break;
      case 0x00280010:
           m_pImageInformation->nImageHeight=*(unsigned short *)m_pData->Value;
           break;
      case 0x00280011:
           m_pImageInformation->nImageWidth=*(unsigned short *)m_pData->Value;
           break;
      case 0x00280100:
           m_pImageInformation->nImageBitsAllocated=*(unsigned short *)m_pData->Value;
           break;
      case 0x00280101:
           m_pImageInformation->nImageBitsStored=*(unsigned short *)m_pData->Value;
           break;
      case 0x00280102:
           m_pImageInformation->nImageHighBit=*(unsigned short *)m_pData->Value;
           break;
      case 0x00280106:
           m_pImageInformation->nImageSmallestPixel=*(short *)m_pData->Value;
           break;
      case 0x00280107:
           m_pImageInformation->nImageLargestPixel=*(unsigned short *)m_pData->Value;
           break;
      case 0x00281050:
           DSToFloat(m_pData,fFst,fSec);
           m_pImageInformation->nWindowCenter=(int)fFst;
           break;
      case 0x00281051:
           DSToFloat(m_pData,fFst,fSec);
           m_pImageInformation->nWindowWidth=(int)fFst;
           break;
      case 0x00281052:
           DSToFloat(m_pData,fFst,fSec);
           m_pImageInformation->fRescaleIntercept=fFst;
           break;
      case 0x00281053:
           DSToFloat(m_pData,fFst,fSec);
           m_pImageInformation->fRescaleSlope=fFst;
           break;
    }
  }
}
//---------------------------------------------------------------------------
unsigned int __fastcall CDataSet::ISToInt(char *pValue,unsigned int nValueWidth)
{
  int i;
  return (*pValue-'0');
}
//---------------------------------------------------------------------------
unsigned int __fastcall CDataSet::ColorTypeToInt(char *pValue,unsigned int nValueWidth)
{
  if(strcmp(pValue,"MONOCHROME1 ")==0)
  {
    return COLOR_MONOCHROME1;
  }
  else if(strcmp(pValue,"MONOCHROME2 ")==0)
  {
    return COLOR_MONOCHROME2;
  }
  else if(strcmp(pValue,"RGB ")==0)
  {
    return COLOR_RGB;
  }
  else if(strcmp(pValue,"HSV ")==0)
  {
    return COLOR_HSV;
  }
  else if(strcmp(pValue,"PALETTE COLOR ")==0)
  {
    return COLOR_PALETTE;
  }
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::GetTransferSyntax(void)
{
  int i;

  for(i=0;i<m_pList->Count;i++)
  {
    m_pData=(TData *)m_pList->Items[i];
    if(m_pData->TagCode==0x00020010)
    {
      if(strcmp(m_pData->Value,"1.2.840.10008.1.2")==0)
      {
        m_pImageInformation->bIsExplicitVR=false;
        m_pImageInformation->bIsLittleEndian=true;
        m_pImageInformation->nImageCompressionType=IMAGE_RAW;
      }
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.1")==0)
      {
        m_pImageInformation->bIsExplicitVR=true;
        m_pImageInformation->bIsLittleEndian=true;
        m_pImageInformation->nImageCompressionType=IMAGE_RAW;
      }
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.2")==0)
      {
        m_pImageInformation->bIsExplicitVR=true;
        m_pImageInformation->bIsLittleEndian=false;
        m_pImageInformation->nImageCompressionType=IMAGE_RAW;
      }
      /*
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.4")==0)
      {
        m_pImageInformation->bIsExplicitVR=false;
        m_pImageInformation->bIsLittleEndian=true;
        m_pImageInformation->nImageType=IMAGE_RAW;
      }
      */
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.4.50")==0)
      {
        m_pImageInformation->nImageCompressionType=IMAGE_JPEG_BASELINE;
      }
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.4.51")==0)
      {
        m_pImageInformation->nImageCompressionType=IMAGE_JPEG_EXTENDED;
      }
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.4.70")==0)
      {
        m_pImageInformation->nImageCompressionType=IMAGE_JPEG_LOSSLESS;
      }
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.4.80")==0)
      {
        m_pImageInformation->nImageCompressionType=IMAGE_JPEG_LS_LOSSLESS;
      }
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.4.81")==0)
      {
        m_pImageInformation->nImageCompressionType=IMAGE_JPEG_LS_LOSSY;
      }
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.4.90")==0)
      {
        m_pImageInformation->nImageCompressionType=IMAGE_JPEG2000_LOSSLESS;
      }
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.4.91")==0)
      {
        m_pImageInformation->nImageCompressionType=IMAGE_JPEG2000;
      }
      else if(strcmp(m_pData->Value,"1.2.840.10008.1.2.5")==0)
      {
        m_pImageInformation->nImageCompressionType=IMAGE_RLE;
      }
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::FormatHex(char *pBuffer,int nItem)
{
  int nLength;
  int i,j;

  sprintf(pBuffer,"%x",nItem);
  nLength=strlen(pBuffer);

  for(i=0;i<nLength;i++)
  {
    if(pBuffer[i]>='a' && pBuffer[i]<='z') pBuffer[i]-=32;
  }
  if(nLength>=4) return;
  else
  {
    for(i=1;i<=4-nLength;i++)
    {
      for(j=(nLength+i);j>=1;j--) pBuffer[j]=pBuffer[j-1];
      pBuffer[0]='0';
    }
  }
}
//---------------------------------------------------------------------------
bool __fastcall CDataSet::Process(void)
{
   PreCheck();
   if(m_pImageInformation->bIsStandard)
   {
     ReadMeta();
     GetTransferSyntax();
   }
   else
   {
     m_pImageInformation->nImageCompressionType=IMAGE_RAW;
   }

   ReadDataSet();
   GetImageInfomation();

   return true;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

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