📄 udataset.cpp
字号:
//---------------------------------------------------------------------------
// This file is part of Dicom Explorer, see http://www.sourceforge.net/projects/dcmsee
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This software is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Liu Jie (liucoldstar@yahoo.com)
//
//---------------------------------------------------------------------------
#pragma hdrstop
#include "uDataSet.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall CDataSet::CDataSet(void)
{
m_pData=NULL;
m_pDictionary=new TfrmDictionary(NULL);
m_pValue=new char[1024];
m_hFile=NULL;
m_Item="(,)";
m_pDictionary->Connect();
m_pList=NULL;
m_pImageInformation=new TImageInformation;
m_nOffset=0;
m_nLength=0;
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::SetFile(AnsiString FileName)
{
m_FileName=FileName;
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::SetFile(char *pFileName)
{
m_hFile = CreateFile(pFileName,
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);
if (m_hFile == INVALID_HANDLE_VALUE)
{
m_hFile=NULL;
throw CException("Error in reading a file!",ERROR_IO);
}
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::SetFile(HANDLE hFile)
{
int i,nCount;
m_hFile=hFile;
if(m_pList)
{
nCount=m_pList->Count;
for (i=0;i<nCount;i++)
{
m_pData=(TData *)m_pList->Items[i];
delete m_pData;
}
}
delete m_pList;
m_pList=new TList;
m_nFileSize=GetFileSize(m_hFile,NULL);
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::Clear(void)
{
int i,nCount;
if(m_pList)
{
nCount=m_pList->Count;
for (i=0;i<nCount;i++)
{
m_pData=(TData *)m_pList->Items[i];
delete m_pData;
}
delete m_pList;
}
m_pList=NULL;
m_pData=NULL;
m_hFile=NULL;
m_Item="(,)";
m_pImageInformation->Clear();
m_nOffset=0;
m_nLength=0;
}
//---------------------------------------------------------------------------
__fastcall CDataSet::~CDataSet()
{
int i;
if(m_pDictionary) delete m_pDictionary;
if(m_pValue) delete []m_pValue;
if(m_pImageInformation) delete m_pImageInformation;
if(m_pList)
{
for (i=0;i<m_pList->Count;i++)
{
m_pData=(TData *)m_pList->Items[i];
delete m_pData;
}
delete m_pList;
}
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::ReadMeta(void)
{
unsigned long nMetaLength;
unsigned long nBytesRead;
unsigned long nMetaStart;
bool bFound;
m_nLength=0;
m_nOffset=132;
ReadTag();
if((m_nGroup!=2)&&!m_nElement)
{
throw CException("This is not a DICOM file!",ERROR_DATASET);
}
m_pData=new TData;
m_pData->TagCode=Bind();
bFound=m_pDictionary->Search(m_Item,m_pData);
ReadVR();
CopyVR();
ReadLength(VALUE_WORD);
ReadValue();
m_pData->ValueWidth=m_nLength;
m_pData->Value=new char[m_nLength+1];
strncpy(m_pData->Value,m_pValue,m_nLength);
m_pData->Value[m_nLength]=0;
m_pList->Add(m_pData);
nMetaLength=*(int *)m_pValue;
nMetaStart=m_nOffset;
while(true)
{
ReadTag();
m_pData=new TData;
m_pData->TagCode=Bind();
bFound=m_pDictionary->Search(m_Item,m_pData);
if(bFound)
{
ReadVR();
CopyVR();
if(m_pData->Type==VR_OB||m_pData->Type==VR_OW||m_pData->Type==VR_SQ||m_pData->Type==VR_UN)
{
Skip(2);
ReadLength(VALUE_DWORD);
}
else ReadLength(VALUE_WORD);
m_pData->ValueWidth=m_nLength;
ReadValue();
CopyValue();
m_pList->Add(m_pData);
}
if (m_nOffset-nMetaStart>=nMetaLength) break;
}
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::Check(void)
{
if(((m_Item[2]=='6')||(m_Item[2]=='5'))&&(m_Item[3]=='0'))
{
m_Item[4]='x';
m_Item[5]='x';
}
}
//---------------------------------------------------------------------------
bool __fastcall CDataSet::DistillImage(void)
{
char *pBuffer;
int i;
int nCount;
int nLeft;
HANDLE hSaveFile=NULL;
unsigned long nBytesRead;
pBuffer=new char[2050];
AnsiString TempFileName;
TempFileName=ExtractFilePath(Application->ExeName)+"\\Save.dat";
if(pBuffer)
{
hSaveFile = CreateFile(TempFileName.c_str(),
GENERIC_WRITE, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // no security
CREATE_ALWAYS, //
FILE_ATTRIBUTE_NORMAL, // normal file
NULL);
nCount=m_pImageInformation->nImageFileSize/2048;
nLeft=m_pImageInformation->nImageFileSize-nCount*2048;
for(i=0;i<nCount;i++)
{
ReadFile(m_hFile,pBuffer,2048,&nBytesRead, NULL);
WriteFile(hSaveFile,pBuffer,2048,&nBytesRead, NULL);
}
ReadFile(m_hFile,pBuffer,nLeft,&nBytesRead, NULL);
WriteFile(hSaveFile,pBuffer,nLeft,&nBytesRead, NULL);
CloseHandle(hSaveFile);
delete []pBuffer;
return true;
}
else return false;
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::PreCheck(void)
{
unsigned long nBytesRead;
SetFilePointer(m_hFile,128,NULL,FILE_BEGIN);
ReadFile(m_hFile,m_pValue,4,&nBytesRead, NULL);
if ((m_pValue[0]!='D')&&(m_pValue[1]!='I')&&(m_pValue[2]!='C')&&(m_pValue[3]!='M'))
{
m_pImageInformation->bIsStandard=false;
SetFilePointer(m_hFile,0,NULL,FILE_BEGIN);
ReadFile(m_hFile,m_pValue,4,&nBytesRead, NULL);
if ((m_pValue[0]!='D')||(m_pValue[1]!='I')||(m_pValue[2]!='C')||(m_pValue[3]!='M'))
{
SetFilePointer(m_hFile,0,NULL,FILE_BEGIN);
ReadTag();
if ((m_nGroup!=0x008)&&(m_nGroup!=0x0800))
{
throw CException("This is not a DICOM file!",ERROR_DATASET);
}
if (m_nGroup>0x0008) m_pImageInformation->bIsLittleEndian=false;
m_pImageInformation->bIsExplicitVR=false;
m_nOffset=0;
SetFilePointer(m_hFile,0,NULL,FILE_BEGIN);
}
}
}
//---------------------------------------------------------------------------
inline int __fastcall CDataSet::Swap(int nItem)
{
int nHigh,nLow;
nHigh=nItem&0xff;
nLow=nItem&0xff00;
return ((nHigh<<8)+(nLow>>8));
}
//---------------------------------------------------------------------------
void __fastcall CDataSet::ReadDataSet(void)
{
bool bFound=false;
//int nOldGroup=0,nOldElement=0;
int *pItem=NULL;
while(true)
{
if(!ReadTag()) break;
if(!m_pImageInformation->bIsLittleEndian)
{
m_nGroup=Swap(m_nGroup);
m_nElement=Swap(m_nElement);
}
m_pData=new TData;
m_pData->TagCode=Bind();
Check();
bFound=m_pDictionary->Search(m_Item,m_pData);
if(!bFound)
{
m_pData->Tag=m_Item;
m_pData->Name="Private";
ReadVR();
CopyVR();
if((m_pData->Type==VR_OB)||(m_pData->Type==VR_BW)||(m_pData->Type==VR_OW)||(m_pData->Type==VR_UN)||(m_pData->Type==VR_SQ))
{
Skip(2);
ReadLength(VALUE_DWORD);
}
else ReadLength(VALUE_WORD);
m_pData->ValueWidth=m_nLength;
if(m_nLength==0xffffffff) m_nLength=0;
if(m_nLength>0&&m_nLength<100)
{
ReadValue();
CopyValue();
}
else if(m_nLength==0) m_pData->Value=NULL;
else //Problem
{
Skip(m_nLength);
}
m_pList->Add(m_pData);
}
else
{
if((m_nGroup==0x7fe0)&&(m_nElement==0x0010))
{
if(m_pImageInformation->bIsExplicitVR)
{
ReadVR();
CopyVR();
Skip(2);
}
ReadLength(VALUE_DWORD);
m_pData->ValueWidth=m_nLength;
if(m_nLength==0xffffffff)
{
m_pList->Add(m_pData);
}
else
{
m_pList->Add(m_pData);
m_pImageInformation->nImageFileSize=m_nLength;
pItem=new int;
*pItem=m_nOffset;
m_pImageInformation->pImageSlicePos->Add(pItem);
DistillImage();
}
}
else if((m_nGroup==0xfffe)&&((m_nElement==0xe000)||(m_nElement==0xe00d)))
{
ReadLength(VALUE_DWORD);
m_pData->ValueWidth=m_nLength;
if(m_nLength==0xffffffff) m_nLength=0;
if(m_nLength)
{
if(m_nLength+m_nOffset+8==m_nFileSize)
{
m_pData->ValueWidth=m_nLength;
m_pImageInformation->nImageFileSize=m_nLength;
pItem=new int;
*pItem=m_nOffset;
m_pImageInformation->pImageSlicePos->Add(pItem);
DistillImage();
m_nOffset+=m_nLength;
break;
}
else
{
ReadValue();
CopyValue();
}
}
m_pList->Add(m_pData);
}
else if((m_nGroup==0xfffe)&&(m_nElement==0xe0dd))
{
ReadLength(VALUE_DWORD);
m_pData->ValueWidth=m_nLength;
if(m_nLength==0xffffffff) m_nLength=0;
if(m_nLength)
{
ReadValue();
CopyValue();
}
m_pList->Add(m_pData);
}
else if(m_pImageInformation->bIsExplicitVR)
{
ReadVR();
CopyVR();
if((m_pData->Type==VR_OB)||(m_pData->Type==VR_BW)||(m_pData->Type==VR_OW)||(m_pData->Type==VR_UN)||(m_pData->Type==VR_SQ))
{
Skip(2);
ReadLength(VALUE_DWORD);
}
else ReadLength(VALUE_WORD);
m_pData->ValueWidth=m_nLength;
if(m_nLength==0xffffffff) m_nLength=0;
if(m_nLength>0&&m_nLength<100)
{
ReadValue();
CopyValue();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -