// MainDlg.cpp : implementation file // #include "stdafx.h" #include "DicomTran.h" #include "MainDlg.h" #include "J2KOptions.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define LEAD_IMPLEMENTATION_CLASS_UID "1.2.840.114257.0.1" #define LEAD_IMPLEMENTATION_VERSION_NAME "LEADTOOLS Demo" ///////////////////////////////////////////////////////////////////////////// // CDicomTransferSyntaxDlg dialog CDicomTransferSyntaxDlg::CDicomTransferSyntaxDlg(CWnd* pParent /*=NULL*/) : CDialog(CDicomTransferSyntaxDlg::IDD, pParent) { //{{AFX_DATA_INIT(CDicomTransferSyntaxDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_J2KOptions.uStructSize =0; LDicomDS DummyDS; DummyDS.GetDefaultJ2KOptions(&m_J2KOptions,sizeof(m_J2KOptions)); } void CDicomTransferSyntaxDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDicomTransferSyntaxDlg) DDX_Control(pDX, IDC_J2KOPTIONS, m_J2kOptionsBtn); DDX_Control(pDX, IDC_COMBONEWTRANSFER, m_cmbTransferSyntax); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDicomTransferSyntaxDlg, CDialog) //{{AFX_MSG_MAP(CDicomTransferSyntaxDlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_CLOSE() ON_BN_CLICKED(IDC_INFILE, OnInfile) ON_BN_CLICKED(IDC_OUT, OnOut) ON_BN_CLICKED(IDC_CHANGE, OnChange) ON_CBN_SELCHANGE(IDC_COMBONEWTRANSFER, OnSelchangeCombonewtransfer) ON_BN_CLICKED(IDC_J2KOPTIONS, OnJ2koptions) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDicomTransferSyntaxDlg message handlers BOOL CDicomTransferSyntaxDlg::OnInitDialog() { CDialog::OnInitDialog(); // 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 for(int i = 0 ; i < SIZE_OF_ARRAY(m_TransferSyntaxArray);i++) { m_cmbTransferSyntax.AddString(m_TransferSyntaxArray[i].pszDescription); } m_cmbTransferSyntax.SetCurSel(0); CEdit * pEditQFactor = NULL; pEditQFactor = (CEdit* )GetDlgItem(IDC_EDIT_QFACTOR); if(pEditQFactor) { pEditQFactor->EnableWindow(FALSE); m_J2kOptionsBtn.EnableWindow(FALSE); pEditQFactor->SetLimitText(3); pEditQFactor->SetWindowText("2"); } return TRUE; // return TRUE unless you set the focus to a control } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CDicomTransferSyntaxDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle 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; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CDicomTransferSyntaxDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } // This is a trick, so that when the user presses the "Esc" button, our dialog doesn't get // closed. We do nothing inside OnCancel() and we call CDialog::OnCancel() inside OnClose(). void CDicomTransferSyntaxDlg::OnCancel() { } void CDicomTransferSyntaxDlg::OnClose() { CDialog::OnCancel(); } //Set input file name void CDicomTransferSyntaxDlg::OnInfile() { CString strInFileName; strInFileName.Empty(); GetDlgItemText (IDC_EDITINFILE, strInFileName); TCHAR szFilters[] =_T ("DICOM Files (*.dic;*.dcm)|*.dic;*.dcm|All files (*.*)|*.*||"); CFileDialog dlg (TRUE, NULL, strInFileName,OFN_HIDEREADONLY |OFN_FILEMUSTEXIST, szFilters); if (dlg.DoModal () == IDOK) { SetDlgItemText (IDC_EDITINFILE, dlg.GetPathName ()); UpdateData(); } } //Set output file name void CDicomTransferSyntaxDlg::OnOut() { CString strOutFileName; strOutFileName.Empty(); GetDlgItemText (IDC_EDITOUTFILE, strOutFileName); TCHAR szFilters[] =_T ("DICOM Files (*.dic;*.dcm)|*.dic;*.dcm|All files (*.*)|*.*||"); CFileDialog dlg (FALSE, NULL, strOutFileName,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, szFilters); if (dlg.DoModal () == IDOK) { SetDlgItemText (IDC_EDITOUTFILE, dlg.GetPathName ()); UpdateData(); } } // Change transfer syntax void CDicomTransferSyntaxDlg::OnChange() { CString strOutFileName; CString strInFileName; WIN32_FIND_DATA FindFileData; HANDLE hFind; LDicomDS DicomDs; L_UINT16 uDicomRet; L_CHAR szUID[128]; CWaitCursor Wait; GetDlgItemText (IDC_EDITINFILE, strInFileName); GetDlgItemText (IDC_EDITOUTFILE, strOutFileName); // Some sanity checks ! if(strInFileName.GetLength() == 0) { AfxMessageBox("Please enter a valid input file name "); return ; } hFind = FindFirstFile(strInFileName, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { AfxMessageBox("Please enter a valid input file name "); return ; } else { FindClose(hFind); } if(strOutFileName.GetLength() == 0) { AfxMessageBox("Please enter a valid output file name "); return ; } if(strInFileName == strOutFileName) { AfxMessageBox("Input and output file names can't be the same!"); return ; } // Get desired transfer syntax int nCursel = m_cmbTransferSyntax.GetCurSel(); if(nCursel != CB_ERR ) { strcpy(szUID,m_TransferSyntaxArray[nCursel].pszUID); } int nQFactor; if( (strcmp(szUID,UID_JPEG_BASELINE_1) ==0)|| (strcmp(szUID,UID_JPEG_EXTENDED_2_4)==0)|| (strcmp(szUID,UID_JPEG2000)==0)) { nQFactor = GetDlgItemInt(IDC_EDIT_QFACTOR); if((nQFactor < 2) || (nQFactor > 255)) { AfxMessageBox( "Please enter a proper quality factor." "\nThe quality factor (Q factor) is a number that determines the degree" "\nof loss in the compression process.\n" "You can set a value from 2 to 255, where 2 is the highest quality and \n" "255 is the most compression."); return ; } } else { nQFactor = 0; } //Load input dataset DicomDs.ResetDS(); uDicomRet = DicomDs.LoadDS((char * )(LPCSTR)strInFileName,0); if(uDicomRet !=DICOM_SUCCESS) { AfxMessageBox("Failed to load dataset"); return ; } if( (strcmp(szUID,UID_JPEG2000_LOSSLESS_ONLY) ==0)|| (strcmp(szUID,UID_JPEG2000)==0)) { // Setting JPEG 2000 options if(m_J2KOptions.uStructSize > 0) { uDicomRet = DicomDs.SetJ2KOptions(&m_J2KOptions); if(uDicomRet !=DICOM_SUCCESS) { AfxMessageBox("Setting JPEG 2000 options failed"); return ; } } } //Change dataset to desired transfer syntax uDicomRet = DicomDs.ChangeTransferSyntax(szUID,nQFactor,0); if(uDicomRet !=DICOM_SUCCESS) { CString strErr; strErr = "Failed to change dataset transfer syntax."; if(uDicomRet == DICOM_ERROR_PARAMETER) { strErr += "\nPossible cause:\" Bits Allocated\" for source dataset doesn't match desired \"Transfer Syntax\"."; } AfxMessageBox(strErr); return ; } // Ensure that the DICOM File Meta Information is added CheckFileMetaInfo(DicomDs); // Save dataset! uDicomRet = DicomDs.SaveDS((L_CHAR*)(LPCSTR) strOutFileName, DS_METAHEADER_PRESENT | DS_GROUP_LENGTHS); if(uDicomRet !=DICOM_SUCCESS) { AfxMessageBox("Failed to save dataset"); return ; } AfxMessageBox("Conversion Succeeded"); } void CDicomTransferSyntaxDlg::OnSelchangeCombonewtransfer() { // Get desired transfer syntax int nCursel = m_cmbTransferSyntax.GetCurSel(); if(nCursel != CB_ERR ) { CWnd *pWnd = GetDlgItem(IDC_EDIT_QFACTOR); if(pWnd) { if( (strcmp(m_TransferSyntaxArray[nCursel].pszUID,UID_JPEG_BASELINE_1) ==0)|| (strcmp(m_TransferSyntaxArray[nCursel].pszUID,UID_JPEG_EXTENDED_2_4)==0)|| (strcmp(m_TransferSyntaxArray[nCursel].pszUID,UID_JPEG2000)==0)) { pWnd->EnableWindow(TRUE); } else { pWnd->EnableWindow(FALSE); } if( (strcmp(m_TransferSyntaxArray[nCursel].pszUID,UID_JPEG2000_LOSSLESS_ONLY) ==0)|| (strcmp(m_TransferSyntaxArray[nCursel].pszUID,UID_JPEG2000)==0)) { m_J2kOptionsBtn.EnableWindow(TRUE); } else { m_J2kOptionsBtn.EnableWindow(FALSE); } } } } void CDicomTransferSyntaxDlg::OnJ2koptions() { LDicomDS DummyDS; CJ2KOptions J2KOptionsDlg; L_UINT16 uDicomRet; if(m_J2KOptions.uStructSize > 0) { uDicomRet = DummyDS.SetJ2KOptions(&m_J2KOptions); if(uDicomRet !=DICOM_SUCCESS) { AfxMessageBox("Setting JPEG 2000 options failed"); return ; } } J2KOptionsDlg.m_pDS = &DummyDS; J2KOptionsDlg.m_nQFactor = GetDlgItemInt(IDC_EDIT_QFACTOR, FALSE); int nCursel = m_cmbTransferSyntax.GetCurSel(); if(nCursel != CB_ERR ) { if((strcmp(m_TransferSyntaxArray[nCursel].pszUID,UID_JPEG2000_LOSSLESS_ONLY) ==0)) { J2KOptionsDlg.m_bLossless = TRUE; } } J2KOptionsDlg.DoModal() ; if((DummyDS.GetJ2KOptions(&m_J2KOptions,sizeof(m_J2KOptions))) != DICOM_SUCCESS) { AfxMessageBox("Setting JPEG 2000 options failed"); } } L_VOID CDicomTransferSyntaxDlg::CheckFileMetaInfo(LDicomDS& DataSet) const { pDICOMELEMENT pElement, pTemp; L_CHAR* pszValue; // File Meta Information Version pElement = DataSet.FindFirstElement(NULL, TAG_FILE_META_INFORMATION_VERSION, FALSE); if (pElement == NULL) { pElement = DataSet.InsertElement(NULL, FALSE, TAG_FILE_META_INFORMATION_VERSION, VR_OB, FALSE, 0); } if (pElement) { L_CHAR Values[] = { 0x00, 0x01 }; DataSet.SetCharValue(pElement, Values, 2); } // Media Storage SOP Class UID pElement = DataSet.FindFirstElement(NULL, TAG_MEDIA_STORAGE_SOP_CLASS_UID, FALSE); if (pElement == NULL) { pElement = DataSet.InsertElement(NULL, FALSE, TAG_MEDIA_STORAGE_SOP_CLASS_UID, VR_UI, FALSE, 0); } if (pElement) { pTemp = DataSet.FindFirstElement(NULL, TAG_SOP_CLASS_UID, FALSE); if (pTemp) { pszValue = DataSet.GetStringValue(pTemp, 0, 1); if (pszValue) { DataSet.SetStringValue(pElement, pszValue, 1); } } } // Media Storage SOP Instance UID pElement = DataSet.FindFirstElement(NULL, TAG_MEDIA_STORAGE_SOP_INSTANCE_UID, FALSE); if (pElement == NULL) { pElement = DataSet.InsertElement(NULL, FALSE, TAG_MEDIA_STORAGE_SOP_INSTANCE_UID, VR_UI, FALSE, 0); } if (pElement) { pTemp = DataSet.FindFirstElement(NULL, TAG_SOP_INSTANCE_UID, FALSE); if (pTemp) { pszValue = DataSet.GetStringValue(pTemp, 0, 1); if (pszValue) { DataSet.SetStringValue(pElement, pszValue, 1); } } } // Implementation Class UID pElement = DataSet.FindFirstElement(NULL, TAG_IMPLEMENTATION_CLASS_UID, FALSE); if (pElement == NULL) { pElement = DataSet.InsertElement(NULL, FALSE, TAG_IMPLEMENTATION_CLASS_UID, VR_UI, FALSE, 0); } if (pElement) { DataSet.SetStringValue(pElement, LEAD_IMPLEMENTATION_CLASS_UID, 1); } // Implementation Version Name pElement = DataSet.FindFirstElement(NULL, TAG_IMPLEMENTATION_VERSION_NAME, FALSE); if (pElement == NULL) { pElement = DataSet.InsertElement(NULL, FALSE, TAG_IMPLEMENTATION_VERSION_NAME, VR_SH, FALSE, 0); } if (pElement) { DataSet.SetStringValue(pElement, LEAD_IMPLEMENTATION_VERSION_NAME, 1); } }