dicom转换bmp,C++程序注释求大神帮助

slljingling 2016-08-30 11:13:35
// DicomBMPDlg.cpp : implementation file
#include "stdafx.h"
#include "DicomBMP.h"
#include "DicomBMPDlg.h"
#include <stdio.h>
enum CONVERSION_MODE
{
MODE_DICOM2BMP = 0,
MODE_BMP2DICOM
};
enum COMPRESSION_MODE
{
COMPRESS_NONE = 0,
COMPRESS_RLE,
COMPRESS_JPEGLOSSY,
COMPRESS_JPEGLOSSY12BIT,
COMPRESS_JPEGLOSSLESS,
COMPRESS_JPEGLOSSLESS2
};
static char szFilter_in[] = "DICOM File(*.dcm)|*.dcm|BMP File(*.bmp)|*.bmp|All files(*.*)|*.*||";
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

class CAboutDlg : public CDialog
{
public:
CAboutDlg();
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

END_MESSAGE_MAP()
CDicomBMPDlg::CDicomBMPDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDicomBMPDlg::IDD, pParent)
{

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strFileDir = AfxGetApp()->GetProfileString("Defaults", "Dir", "");
m_nFileFormat = AfxGetApp()->GetProfileInt("Defaults", "Format", 0);
m_nConvertMode = AfxGetApp()->GetProfileInt("Defaults", "Mode", MODE_DICOM2BMP);
}

void CDicomBMPDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CDicomBMPDlg, CDialog)

END_MESSAGE_MAP()

void CDicomBMPDlg::removeTailingSpace(char *pszStr)
{
char *cc = pszStr + strlen(pszStr) - 1;

while (((*cc == ' ') || (*cc == '\t')) && (cc != pszStr))
{
*cc -- = '\0';
}
}
void CDicomBMPDlg::SwapWord(char *pArray, int nWords)
{
char *cc = pArray, c0;
int i;

// Swap every two bytes
for (i = 0; i < nWords; i ++)
{
c0 = *cc;
*cc = *(cc + 1);
*(cc + 1) = c0;

cc += 2;
}
}
void CDicomBMPDlg::SwapDWord(char *pArray, int nDWords)
{
char *cc = pArray, c0;
int i;
for (i = 0; i < nDWords; i ++)
{

// Swap first and last bytes
c0 = *cc;
*cc = *(cc + 3);
*(cc + 3) = c0;

// Swap middle two bytes
c0 = *(cc + 2);
*(cc + 2) = *(cc + 1);
*(cc + 1) = c0;

cc += 4;
}
}
int CDicomBMPDlg::readUS(FILE *fp, DATA_ENDIAN nDataEndian)
{
unsigned short nVal;

fseek(fp, 4, SEEK_CUR);
fread(&nVal, 1, sizeof(short), fp);
if (nDataEndian == BIG_ENDIAN)
SwapWord((char *) &nVal, 1);
return (int) nVal;
}
long int CDicomBMPDlg::readLength(FILE *fp, BOOL bImplicitVR, DATA_ENDIAN nDataEndian)
{
long int nValLength = 0;
short int nsLength;

if (bImplicitVR)
{
fread(&nValLength, sizeof(long), 1, fp);
if (nDataEndian == BIG_ENDIAN)
SwapDWord((char *) &nValLength, 1);
}
else
{
fseek(fp, 2, SEEK_CUR); // Skip 2 VR bytes

fread(&nsLength, sizeof(short), 1, fp);
if (nDataEndian == BIG_ENDIAN)
SwapWord((char *) &nsLength, 1);

nValLength = nsLength;
}

return nValLength;
}
int CDicomBMPDlg::readIS(FILE *fp, BOOL bImplicitVR, DATA_ENDIAN nDataEndian)
{
char szTemp[64]="";
int nVal = 0;

if (readString(fp, szTemp, bImplicitVR, nDataEndian) == 0)
sscanf(szTemp, "%d", &nVal);

return nVal;
}
float CDicomBMPDlg::readDS(FILE *fp, BOOL bImplicitVR, DATA_ENDIAN nDataEndian)
{
char szTemp[64]="";
float fVal = 0;

if (readString(fp, szTemp, bImplicitVR, nDataEndian) == 0)
sscanf(szTemp, "%f", &fVal);

return fVal;
}
int CDicomBMPDlg::readString(FILE *fp, char *pszStr, BOOL bImplicitVR, DATA_ENDIAN nDataEndian)
{
long int nValLength = 0;

nValLength = readLength(fp, bImplicitVR, nDataEndian);

if ((nValLength > 64) || (nValLength < 0))
return -1;

fread(pszStr, 1, nValLength, fp);
pszStr[nValLength] = '\0';
removeTailingSpace(pszStr);

return 0;
}
char *CDicomBMPDlg::convertTo8Bit(char *pData, long int nNumPixels, BOOL bIsSigned, short nHighBit,
float fRescaleSlope, float fRescaleIntercept,
float fWindowCenter, float fWindowWidth)
{
unsigned char *pNewData = 0;
long int nCount;
short *pp;

// 1. Clip the high bits.
if (nHighBit < 15)
{
short nMask;
short nSignBit;

pp = (short *)pData;
nCount = nNumPixels;

if(bIsSigned == 0 ) // Unsigned integer
{
nMask = 0xffff << (nHighBit + 1);

while( nCount-- > 0 )
*(pp ++) &= ~nMask;
}
else
{

nSignBit = 1 << nHighBit;
nMask = 0xffff << (nHighBit + 1);
while( nCount -- > 0 )
{
if ((*pp & nSignBit) != 0)
*(pp ++) |= nMask;
else
*(pp ++) &= ~nMask;
}
}
}

// 2. Rescale if needed (especially for CT)
if ((fRescaleSlope != 1.0f) || (fRescaleIntercept != 0.0f))
{
float fValue;

pp = (short *)pData;
nCount = nNumPixels;

while( nCount-- > 0 )
{
fValue = (*pp) * fRescaleSlope + fRescaleIntercept;
*pp ++ = (short)fValue;
}

}

// 3. Window-level or rescale to 8-bit
if ((fWindowCenter != 0) || (fWindowWidth != 0))
{
float fSlope;
float fShift;
float fValue;
unsigned char *np = new unsigned char[nNumPixels+4];

pNewData = np;

// Since we have window level info, we will only map what are
// within the Window.

fShift = fWindowCenter - fWindowWidth / 2.0f;
fSlope = 255.0f / fWindowWidth;

nCount = nNumPixels;
pp = (short *)pData;

while (nCount-- > 0)
{
fValue = ((*pp ++) - fShift) * fSlope;
if (fValue < 0)
fValue = 0;
else if (fValue > 255)
fValue = 255;

*np ++ = (unsigned char) fValue;
}

}
else
{
// We will map the whole dynamic range.
float fSlope;
float fValue;
int nMin, nMax;
unsigned char *np = new unsigned char[nNumPixels+4];

pNewData = np;

// First compute the min and max.
nCount = nNumPixels;
pp = (short *)pData;
nMin = nMax = *pp;
while (nCount-- > 0)
{
if (*pp < nMin)
nMin = *pp;

if (*pp > nMax)
nMax = *pp;

pp ++;
}

// Calculate the scaling factor.
if (nMax != nMin)
fSlope = 255.0f / (nMax - nMin);
else
fSlope = 1.0f;

nCount = nNumPixels;
pp = (short *)pData;
while (nCount-- > 0)
{
fValue = ((*pp ++) - nMin) * fSlope;
if (fValue < 0)
fValue = 0;
else if (fValue > 255)
fValue = 255;

*np ++ = (unsigned char) fValue;
}
}

return (char *)pNewData;
}

BOOL CDicomBMPDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

void CDicomBMPDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
void CDicomBMPDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this);
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;


dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

HCURSOR CDicomBMPDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CDicomBMPDlg::OnModeChanged()
{
UpdateData();
AfxGetApp()->WriteProfileInt("Defaults", "Mode", m_nConvertMode);

}

void CDicomBMPDlg::OnConvertFile()
{
int nIndex = 0;


CFileDialog FileDlg( TRUE, NULL, NULL, OFN_ENABLESIZING,
szFilter_in );
FileDlg.m_ofn.nFilterIndex = (DWORD) (m_nConvertMode+1);
if (m_strFileDir.GetLength())
FileDlg.m_ofn.lpstrInitialDir = m_strFileDir.GetBuffer(3);

if( FileDlg.DoModal() == IDOK ){

CString PathName = FileDlg.GetPathName();
PathName.MakeUpper();

m_strFileName = PathName;
m_strFileDir = PathName;
int index;
if (((index = m_strFileDir.ReverseFind('\\')) != -1)
&& (m_strFileDir.GetAt(index-1) != ':'))
m_strFileDir.SetAt(index, '\0');


nIndex = (int) FileDlg.m_ofn.nFilterIndex;


AfxGetApp()->WriteProfileString("Defaults", "Dir", m_strFileDir);


switch(m_nConvertMode)
{
case MODE_DICOM2BMP:
ConvertDicomToBMP();
break;

case MODE_BMP2DICOM:
ConvertBmpToDicom();
break;
}


}

}

void CDicomBMPDlg::ConvertBmpToDicom()
{
AfxMessageBox("ConvertBmpToDicom() not implemented");


}
void CDicomBMPDlg::ConvertDicomToBMP()
{

short int nCols = 0, nRows = 0;
short int nBitsAllocated, nSamplesPerPixel = 1;
short int nHighBit = 0;
float fWindowWidth = 0, fWindowCenter = 0 , fRescaleSlope = 1, fRescaleIntercept = 0;
BOOL bIsSigned = FALSE;

BOOL bGroup2Done = FALSE, bGroup28Done = FALSE, bPixelDataDone = FALSE;

int nBytesP = 0;
int nFrameSize = 0;
long int nLength;
const char *pszFileName = m_strFileName.GetBuffer(3);
char szPhotometric[32]="", szTemp[32]="", szTransferSyntaxUID[80]="";

BOOL bImplicitVR = TRUE;
COMPRESSION_MODE nCompressionMode = COMPRESS_NONE;
DATA_ENDIAN nDataEndian = LITTLE_ENDIAN;
int i;
int nBytes;


FILE *fp;
char *pData = 0;
short int gTag, eTag;
int nNumFrames = 1;

fp = fopen(pszFileName, "rb");
if (!fp)
{
AfxMessageBox("Failed to open file for read.");
return;
}
未完待续.....

...全文
2080 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

703

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder ActiveX/COM/DCOM
社区管理员
  • ActiveX/COM/DCOM社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧