DIBDoc.cpp
上传用户:gzboli
上传日期:2013-04-10
资源大小:471k
文件大小:19k
- // DIBDoc.cpp : implementation of the CDIBDoc class
- //
- #include "stdafx.h"
- #include "QuickImage.h"
- #include "DIBDoc.h"
- #include "DlgHistogram.h"
- #include "raw.h"
- #include "importsjpegfile.h"
- #include "mainfrm.h"
- #include "ConvoluteDlg.h"
- #include "ImgProcess.h"
- #include "importsgifapi.h"
- #include "Global.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDIBDoc
- extern CString g_strCurrentDir;
- IMPLEMENT_DYNCREATE(CDIBDoc, CDocument)
- BEGIN_MESSAGE_MAP(CDIBDoc, CDocument)
- //{{AFX_MSG_MAP(CDIBDoc)
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_REALIZEPAL, OnRealizePal)
- ON_COMMAND_RANGE(ID_IMAGE_HISTOGRAM, ID_IMAGE_THIN, OnImageProcess)
- ON_UPDATE_COMMAND_UI_RANGE(ID_IMAGE_HISTOGRAM, ID_IMAGE_THIN, OnUpdateImageProcess)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDIBDoc construction/destruction
- CDIBDoc::CDIBDoc()
- {
- m_hDIB = NULL;
- m_palDIB = NULL;
- m_sizeDoc = CSize(1,1); // dummy value to make CScrollView happy
- // m_hRightDIB = NULL;
- // *m_strRightFile = ' ';
- }
- CDIBDoc::~CDIBDoc()
- {
- if (m_hDIB != NULL)
- {
- ::GlobalFree((HGLOBAL) m_hDIB);
- m_hDIB = NULL;
- }
- if (m_palDIB != NULL)
- {
- delete m_palDIB;
- m_palDIB = NULL;
- }
- /* if (m_hRightDIB != NULL)
- {
- ::GlobalFree((HGLOBAL) m_hRightDIB);
- }*/
- }
- BOOL CDIBDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
- return TRUE;
- }
- void CDIBDoc::InitDIBData()
- {
- if (m_palDIB != NULL)
- {
- delete m_palDIB;
- m_palDIB = NULL;
- }
- if (m_hDIB == NULL)
- {
- return;
- }
- // Set up document size
- LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
- if (::DIBWidth(lpDIB) > INT_MAX ||abs(::DIBHeight(lpDIB)) > INT_MAX)
- {
- ::GlobalUnlock((HGLOBAL) m_hDIB);
- ::GlobalFree((HGLOBAL) m_hDIB);
- m_hDIB = NULL;
- CString strMsg;
- strMsg.LoadString(IDS_DIB_TOO_BIG);
- MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
- return;
- }
- m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) abs(::DIBHeight(lpDIB)));
- ::GlobalUnlock((HGLOBAL) m_hDIB);
- // Create copy of palette
- if(::DIBBitCount(lpDIB) < 24)
- {
- m_palDIB = new CPalette;
- if (m_palDIB == NULL)
- {
- // we must be really low on memory
- ::GlobalFree((HGLOBAL) m_hDIB);
- m_hDIB = NULL;
- return;
- }
- if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
- {
- // DIB may not have a palette
- delete m_palDIB;
- m_palDIB = NULL;
- return;
- }
- }
- }
- void CDIBDoc::ReplaceHDIB(HDIB hDIB)
- {
- if (m_hDIB != NULL)
- {
- ::GlobalFree((HGLOBAL) m_hDIB);
- }
- m_hDIB = hDIB;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDIBDoc serialization
- void CDIBDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- }
- else
- {
- // TODO: add loading code here
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDIBDoc diagnostics
- #ifdef _DEBUG
- void CDIBDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void CDIBDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CDIBDoc commands
- BOOL CDIBDoc::OnOpenDocument(LPCTSTR lpszPathName)
- {
- BeginWaitCursor();
- CMultiDocTemplate * pDocTemplate = ((CQuickImageApp*)AfxGetApp())->m_pImageTemplate;
-
- POSITION pos = pDocTemplate->GetFirstDocPosition();
- CDIBDoc* pDoc = (CDIBDoc*)pDocTemplate->GetNextDoc(pos);
- if(NULL != pDoc && pDoc != this)
- {
- if(pDoc->IsModified())
- {
- CString strMsg = _T("Save change to") + pDoc->GetTitle() + _T("?");
- if(AfxMessageBox(strMsg, MB_ICONWARNING | MB_YESNO) == IDYES)
- {
- pDoc->SaveModified();//>OnSaveDocument(pDoc->GetPathName());
- }
- }
- pDoc->OnCloseDocument();
- }
- char *pExt=strrchr(lpszPathName,'.');
- char cExt[4];
- strcpy(cExt,++pExt);
- _strlwr(cExt);
- DeleteContents();
-
- if (strcmp(cExt,"raw")==0)
- {
- CRaw raw;
- if(!raw.ReadFromFile(lpszPathName))
- {
- return FALSE;
- }
- m_hDIB = raw.Show();
- }
- else if(strcmp(cExt,"bmp")==0
- || strcmp(cExt,"dib")==0
- || strcmp(cExt,"ddb")==0)
- {
- CFile file;
- CFileException fe;
- if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
- {
- fe.ReportError();
- return NULL;
- }
- // replace calls to Serialize with ReadDIBFile function
- TRY
- {
- m_hDIB = ::ReadDIBFile(file);
- }
- CATCH (CFileException, eLoad)
- {
- file.Abort(); // will not throw an exception
- eLoad->ReportError();
- m_hDIB = NULL;
- return NULL;
- }
- END_CATCH
- if (m_hDIB == NULL)
- {
- // may not be DIB format
- CString strMsg;
- strMsg.LoadString(IDS_CANNOT_LOAD_DIB);
- MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
- return NULL;
- }
- }
- else if(strcmp(cExt,"jpg")==0 || strcmp(cExt,"jpeg")==0)
- {
- UINT height,width;
- BYTE *pData = NULL;
- pData = JpegFile::JpegFileToRGB(lpszPathName, &width, &height);
- if(NULL == pData)return NULL;
- JpegFile::BGRFromRGB(pData, width, height);
- JpegFile::VertFlipBuf(pData, width * 3, height);
-
- UINT widthDW;
- BYTE *pDIB = JpegFile::MakeDwordAlignedBuf(pData, width, height, &widthDW);
-
- delete []pData;
- if(NULL == pDIB)
- {
- char strErr[100];
- sprintf(strErr, "Error: %d", GetLastError());
- MessageBox(AfxGetMainWnd()->GetSafeHwnd(), strErr, "Read File Error",
- MB_OK|MB_ICONERROR);
- }
- m_hDIB = ::ShowJPEG(width, height, pDIB);
- delete []pDIB;
- }
- else if(strcmp(cExt, "gif") == 0)
- {
- m_hDIB = ReadGIF(lpszPathName);
- }
- else if(strcmp(cExt,"pcx") == 0)
- {
- m_hDIB = ::ReadPCX256(lpszPathName);
- }
- else
- {
- // CString strMsg;
- // strMsg.Format("%sn不是图像文件!", lpszPathName);
- AfxMessageBox("不识别的文件格式!");
- return FALSE;
- }
-
- if(NULL != m_hDIB)
- {
- InitDIBData();
- }
- else
- {
- return FALSE;
- }
- SetPathName(lpszPathName);
- SetModifiedFlag(FALSE);
- EndWaitCursor();
- // if(g_strCurrentDir.IsEmpty())
- {
- CString strCurDir = GetPathName();
- char *pTitle = strrchr(strCurDir, '\');
- pTitle[1] = ' ';
- CGlobal::ChangeDirectory(strCurDir);
- }
- return TRUE;
- }
- BOOL CDIBDoc::OnSaveDocument(LPCTSTR lpszPathName)
- {
- char *pExt=strrchr(lpszPathName,'.');
- char cExt[4];
- strcpy(cExt,++pExt);
- _strlwr(cExt);
- BeginWaitCursor();
- BOOL bSuccess = FALSE;
- // CTime t_Start = CTime::GetCurrentTime();
-
- if(strcmp(cExt,"bmp")==0
- || strcmp(cExt,"dib")==0
- || strcmp(cExt,"ddb")==0)
- {
- CFile file;
- CFileException fe;
- if (!file.Open(lpszPathName, CFile::modeCreate |
- CFile::modeReadWrite | CFile::shareExclusive, &fe))
- {
- ReportSaveLoadException(lpszPathName, &fe,
- TRUE, AFX_IDP_INVALID_FILENAME);
- return FALSE;
- }
- // replace calls to Serialize with SaveDIB function
- TRY
- {
- bSuccess = ::SaveDIB(m_hDIB, file);
- file.Close();
- }
- CATCH (CException, eSave)
- {
- file.Abort(); // will not throw an exception
- EndWaitCursor();
- ReportSaveLoadException(lpszPathName, eSave,
- TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
- }
- END_CATCH
- }
- else if (strcmp(cExt,"raw")==0)
- {
- bSuccess = CRaw::SaveRaw(lpszPathName, m_hDIB);
- }
- else if(strcmp(cExt,"pcx") == 0)
- {
- LPSTR lpDIB;
- LPSTR lpDIBBits;
- lpDIB = (LPSTR) ::GlobalLock(m_hDIB);
- lpDIBBits = ::FindDIBBits(lpDIB);
-
- // 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的行程编码)
- if (::DIBNumColors(lpDIB) != 256)
- {
- // 提示用户
- AfxMessageBox("目前只支持256色位图的行程编码!", MB_ICONINFORMATION | MB_OK);
- ::GlobalUnlock(m_hDIB);
- return FALSE;
- }
- bSuccess = ::DIBToPCX256(lpDIB, lpszPathName);
- GlobalUnlock(m_hDIB);
- }
- else
- {
- // ASSERT(FALSE);
- }
- SetPathName(lpszPathName);
- SetModifiedFlag(FALSE);
- // CTimeSpan t_Span = CTime::GetCurrentTime() - t_Start;
- // ((CMainFrame*)AfxGetMainWnd())->SetStatusText(
- // t_Span.Format("Elapsed time- %H : %M : %S"));
- EndWaitCursor();
-
- return bSuccess;
- return CDocument::OnSaveDocument(lpszPathName);
- }
- void CDIBDoc::OnRealizePal(WPARAM wParam, LPARAM lParam)
- {
- // ASSERT(wParam != NULL);
- if (NULL == m_hDIB)
- return; // must be a new document
- if (m_palDIB != NULL)
- {
- CWnd* pAppFrame = AfxGetApp()->m_pMainWnd;
- CClientDC appDC(pAppFrame);
- // All views but one should be a background palette.
- // wParam contains a handle to the active view, so the SelectPalette
- // bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
- CPalette* oldPalette = appDC.SelectPalette(m_palDIB, FALSE);
- if (oldPalette != NULL)
- {
- UINT nColorsChanged = appDC.RealizePalette();
- if (nColorsChanged > 0)
- UpdateAllViews(NULL);
- appDC.SelectPalette(oldPalette, TRUE);
- }
- else
- {
- TRACE0("tSelectPalette failed!n");
- }
- }
- }
- void CDIBDoc::OnImageProcess(UINT uID)
- {
- CTime t_Start = CTime::GetCurrentTime();
- if(ID_IMAGE_HISTOGRAM == uID)
- {
- CDlgHistogram dlg(m_hDIB);
- dlg.DoModal();
- }
- else if(ID_IMAGE_HISTOGRAM_EQUAL == uID)
- {
- HDIB hDIB = CImage::HistoEqualize(m_hDIB);
- if(NULL != hDIB)
- {
- ReplaceHDIB(hDIB);
- InitDIBData();
- SetModifiedFlag();
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_MEDIANFILTER == uID)
- {
- if (CImgProcess::MedianFilterDIB(m_hDIB))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0,0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_SMOTH_LF1 == uID)
- {
- CConvoluteDlg FD(1, 10, 3);
- if (FD.DoModal() == IDOK)
- {
- if(CImgProcess::LowPassDIB(m_hDIB, FD.m_nStreng, FILTER1))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- }
- else if(ID_IMAGE_SMOTH_LF2 == uID)
- {
- CConvoluteDlg FD(1, 10, 3);
- if (FD.DoModal() == IDOK)
- {
- if(CImgProcess::LowPassDIB(m_hDIB, FD.m_nStreng, FILTER2))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- }
- else if(ID_IMAGE_SMOTH_LF3 == uID)
- {
- CConvoluteDlg FD(1, 10, 3);
- if (FD.DoModal() == IDOK)
- {
- if(CImgProcess::LowPassDIB(m_hDIB, FD.m_nStreng, FILTER3))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- }
- else if(ID_IMAGE_SHARP_HF1 == uID)
- {
- CConvoluteDlg FD(1, 10, 3);
- if (FD.DoModal() == IDOK)
- {
- if(CImgProcess::HighPassDIB(m_hDIB, FD.m_nStreng, FILTER1))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0,0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- }
- else if(ID_IMAGE_SHARP_HF2 == uID)
- {
- CConvoluteDlg FD(1, 10, 3);
- if (FD.DoModal() == IDOK)
- {
- if(CImgProcess::HighPassDIB(m_hDIB, FD.m_nStreng, FILTER2))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0,0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- }
- else if(ID_IMAGE_SHARP_HF3 == uID)
- {
- CConvoluteDlg FD(1, 10, 3);
- if (FD.DoModal() == IDOK)
- {
- if(CImgProcess::HighPassDIB(m_hDIB, FD.m_nStreng, FILTER3))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0,0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- }
- else if(ID_IMAGE_EDGE_H == uID)
- {
- EdgeEnhance(HORZ);
- }
- else if(ID_IMAGE_EDGE_V == uID)
- {
- EdgeEnhance(VERT);
- }
- else if(ID_IMAGE_EDGE_VH == uID)
- {
- EdgeEnhance(VERTHORZ);
- }
- else if(ID_IMAGE_EDGE_N == uID)
- {
- EdgeEnhance(NORTH);
- }
- else if(ID_IMAGE_EDGE_NE == uID)
- {
- EdgeEnhance(NORTHEAST);
- }
- else if(ID_IMAGE_EDGE_E == uID)
- {
- EdgeEnhance(EAST);
- }
- else if(ID_IMAGE_EDGE_SE == uID)
- {
- EdgeEnhance(SOUTHEAST);
- }
- else if(ID_IMAGE_EDGE_S == uID)
- {
- EdgeEnhance(SOUTH);
- }
- else if(ID_IMAGE_EDGE_SW == uID)
- {
- EdgeEnhance(SOUTHWEST);
- }
- else if(ID_IMAGE_EDGE_W == uID)
- {
- EdgeEnhance(WEST);
- }
- else if(ID_IMAGE_EDGE_NW == uID)
- {
- EdgeEnhance(NORTHWEST);
- }
- else if(ID_IMAGE_EDGE_LP1 == uID)
- {
- EdgeEnhance(LAP1);
- }
- else if(ID_IMAGE_EDGE_LP2 == uID)
- {
- EdgeEnhance(LAP2);
- }
- else if(ID_IMAGE_EDGE_LP3 == uID)
- {
- EdgeEnhance(LAP3);
- }
- else if(ID_IMAGE_EDGE_LP4 == uID)
- {
- EdgeEnhance(LAP4);
- }
- else if(ID_IMAGE_CONTOUR_H == uID)
- {
- if(CImgProcess::ContourDIB(m_hDIB, TRUE))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_CONTOUR_V == uID)
- {
- if(CImgProcess::ContourDIB(m_hDIB, FALSE))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_CONTOUR_SOBER == uID)
- {
- EdgeEnhance(SOBEL);
- }
- else if(ID_IMAGE_CONTOUR_HOUGH == uID)
- {
- EdgeEnhance(HOUGH);
- }
- else if(ID_IMAGE_REVERSE == uID)
- {
- if(CImgProcess::ReverseDIB(m_hDIB))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_FFT == uID)
- {
- if(CImgProcess::FFTDIB(m_hDIB))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_DCT == uID)
- {
- if(CImgProcess::DCTDIB(m_hDIB))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_WALH == uID)
- {
- if(CImgProcess::WALhDIB(m_hDIB))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_EROSION_H == uID)
- {
- if(CImgProcess::ErosionDIB(m_hDIB, TRUE))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_EROSION_V == uID)
- {
- if(CImgProcess::ErosionDIB(m_hDIB, FALSE))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_DILATION_H == uID)
- {
- if(CImgProcess::DilationDIB(m_hDIB, TRUE))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_DILATION_V == uID)
- {
- if(CImgProcess::DilationDIB(m_hDIB, FALSE))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_CLOSE_H == uID)
- {
- if(CImgProcess::MorphCloseDIB(m_hDIB, TRUE))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_CLOSE_V == uID)
- {
- if(CImgProcess::MorphCloseDIB(m_hDIB, FALSE))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_OPEN_H == uID)
- {
- if(CImgProcess::MorphOpenDIB(m_hDIB, TRUE))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_OPEN_V == uID)
- {
- if(CImgProcess::MorphOpenDIB(m_hDIB, FALSE))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else if(ID_IMAGE_THIN == uID)
- {
- if(CImgProcess::ThinningDIB(m_hDIB))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- else
- {
- AfxMessageBox("Hi, what are you doing?");
- }
- CTimeSpan t_Span = CTime::GetCurrentTime() - t_Start;
- CString strStatus = t_Span.Format("Elapsed time- %H : %M : %S");
- ((CMainFrame*)AfxGetMainWnd())->SetStatusText(0, strStatus);
- }
- void CDIBDoc::OnUpdateImageProcess(CCmdUI *pCmdUI)
- {
- pCmdUI->Enable(NULL != m_hDIB);
- }
- void CDIBDoc::EdgeEnhance(int nAlgorithm)
- {
- CConvoluteDlg FD(1, 10, 3);
- if (FD.DoModal() == IDOK)
- {
- if(CImgProcess::EdgeEnhanceDIB(m_hDIB, FD.m_nStreng, nAlgorithm))
- {
- InitDIBData();
- SetModifiedFlag(TRUE);
- // OnRealizePal((WPARAM)0, 0); // realize the new palette
- UpdateAllViews(NULL);
- }
- }
- }
- BOOL CDIBDoc::EnumFile(LPCTSTR lpszPathName)
- {
- ASSERT(strlen(lpszPathName) > 0);
- BeginWaitCursor();
- char *pExt=strrchr(lpszPathName,'.');
- char cExt[4];
- strcpy(cExt,++pExt);
- _strlwr(cExt);
- DeleteContents();
- HDIB hDIB = NULL;
-
- if (strcmp(cExt,"raw")==0)
- {
- CRaw raw;
- if(!raw.ReadFromFile(lpszPathName))
- {
- return FALSE;
- }
- hDIB = raw.Show();
- }
- else if(strcmp(cExt,"bmp")==0
- || strcmp(cExt,"dib")==0
- || strcmp(cExt,"ddb")==0)
- {
- CFile file;
- CFileException fe;
- if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
- {
- fe.ReportError();
- return NULL;
- }
- // replace calls to Serialize with ReadDIBFile function
- TRY
- {
- hDIB = ::ReadDIBFile(file);
- }
- CATCH (CFileException, eLoad)
- {
- file.Abort(); // will not throw an exception
- eLoad->ReportError();
- hDIB = NULL;
- return NULL;
- }
- END_CATCH
- if (hDIB == NULL)
- {
- // may not be DIB format
- CString strMsg;
- strMsg.LoadString(IDS_CANNOT_LOAD_DIB);
- MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
- return NULL;
- }
- }
- else if(strcmp(cExt,"jpg")==0 || strcmp(cExt,"jpeg")==0)
- {
- UINT height,width;
- BYTE *pData = NULL;
- pData = JpegFile::JpegFileToRGB(lpszPathName, &width, &height);
- if(NULL == pData)return NULL;
- JpegFile::BGRFromRGB(pData, width, height);
- JpegFile::VertFlipBuf(pData, width * 3, height);
-
- UINT widthDW;
- BYTE *pDIB = JpegFile::MakeDwordAlignedBuf(pData, width, height, &widthDW);
-
- delete []pData;
- if(NULL == pDIB)
- {
- char strErr[100];
- sprintf(strErr, "Error: %d", GetLastError());
- MessageBox(AfxGetMainWnd()->GetSafeHwnd(), strErr, "Read File Error",
- MB_OK|MB_ICONERROR);
- }
- hDIB = ::ShowJPEG(width, height, pDIB);
- delete []pDIB;
- }
- else if(strcmp(cExt, "gif") == 0)
- {
- hDIB = ReadGIF(lpszPathName);
- }
- else if(strcmp(cExt,"pcx") == 0)
- {
- hDIB = ::ReadPCX256(lpszPathName);
- }
- else
- {
- // CString strMsg;
- // strMsg.Format("%sn不是图像文件!", lpszPathName);
- AfxMessageBox("不识别的文件格式!");
- return FALSE;
- }
-
- if(NULL != hDIB)
- {
- ReplaceHDIB(hDIB);
- InitDIBData();
- }
- else
- {
- return FALSE;
- }
- SetPathName(lpszPathName);
- char *pTitle = strrchr(lpszPathName, '\');
- SetTitle(++pTitle);
- SetModifiedFlag(FALSE);
- EndWaitCursor();
- return TRUE;
- }