Raw.cpp
上传用户:gzboli
上传日期:2013-04-10
资源大小:471k
文件大小:3k
- // Raw.cpp: implementation of the CRaw class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "QuickImage.h"
- #include "Raw.h"
- #include "dlgguesswidth.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CRaw::CRaw() : CImage()
- {
- }
- CRaw::~CRaw()
- {
- }
- BOOL CRaw::ReadFromFile(const LPCTSTR lpszPathName)
- {
- ASSERT(NULL != lpszPathName);
-
- CDlgGuessWidth dlg;
- strcpy(dlg.PATHNAME, lpszPathName);
- strcpy(dlg.TITLE, "Open Raw File:");
- dlg.m_iDepth=0;
- if(dlg.DoModal() != IDOK)
- {
- return FALSE;
- }
- m_iWidth=dlg.m_iWidth;
- m_iHeight=dlg.m_iHeight;
- strcpy(m_szPathName,lpszPathName);
- return TRUE;
- }
- HDIB CRaw::Show()
- {
- CFile file;
- if(!file.Open(m_szPathName,CFile::modeRead | CFile::shareExclusive))
- {
- return NULL;
- }
- DWORD size=m_iWidth*m_iHeight;
- HANDLE hByte=NULL;
- BYTE* pByte=NULL;
- if((hByte=GlobalAlloc(GHND, size)) == NULL)
- {
- file.Close();
- return NULL;
- }
- if((pByte=(BYTE*)GlobalLock(hByte)) == NULL)
- {
- file.Close();
- return NULL;
- }
- if(file.Read(pByte, size) != size)
- {
- file.Close();
- GlobalUnlock(hByte);
- GlobalFree(hByte);
- return NULL;
- }
- file.Close();
- HDIB hDIB = NULL;
- BYTE *pTemp = new BYTE[m_iWidth];
- if(NULL != pTemp)
- {
- BYTE *top = pByte;
- BYTE *bottom = pByte + m_iWidth * (m_iHeight - 1);
- while(top < bottom)
- {
- memcpy(pTemp, top, m_iWidth);
- memcpy(top, bottom, m_iWidth);
- memcpy(bottom, pTemp, m_iWidth);
- top += m_iWidth;
- bottom -= m_iWidth;
- }
- delete pTemp;
- hDIB = ::GenBmp(m_iWidth, m_iHeight, 8, pByte);
- }
- try
- {
- GlobalUnlock(hByte);
- GlobalFree(hByte);
- }
- catch(CMemoryException *e)
- {
- e->ReportError();
- e->Delete();
- if(hDIB)
- {
- ::DeleteObject(hDIB);
- }
- return NULL;
- }
- return hDIB;
- }
- BOOL CRaw::SaveRaw(const LPCTSTR lpszPathName, const HDIB hDIB)
- {
- ASSERT(strlen(lpszPathName) > 0);
- ASSERT(NULL != hDIB);
- CFile file;
- CFileException fe;
- if (!file.Open(lpszPathName, CFile::modeCreate |
- CFile::modeReadWrite | CFile::shareExclusive, &fe))
- {
- return FALSE;
- }
- TRY
- {
- LPSTR lpDIB = (LPSTR)GlobalLock(hDIB);
- LPSTR lpBITs = ::FindDIBBits(lpDIB);
- int iWidth = ::DIBWidth(lpDIB);
- int iHeight = abs(::DIBHeight(lpDIB));
- BYTE *pTemp = new BYTE[iWidth];
- if(NULL != pTemp)
- {
- BYTE *top = (BYTE*)lpBITs;
- BYTE *bottom = (BYTE*)lpBITs + iWidth * (iHeight - 1);
- while(top < bottom)
- {
- memcpy(pTemp, top, iWidth);
- memcpy(top, bottom, iWidth);
- memcpy(bottom, pTemp, iWidth);
- top += iWidth;
- bottom -= iWidth;
- }
- file.Write(lpBITs,
- ::DIBWidth(lpDIB) * iHeight * int(((PBITMAPINFOHEADER)lpDIB)->biBitCount / 8.0));
- top = (BYTE*)lpBITs;
- bottom = (BYTE*)lpBITs + iWidth * (iHeight - 1);
- while(top < bottom)
- {
- memcpy(pTemp, top, iWidth);
- memcpy(top, bottom, iWidth);
- memcpy(bottom, pTemp, iWidth);
- top += iWidth;
- bottom -= iWidth;
- }
- delete pTemp;
- }
- GlobalUnlock(hDIB);
- file.Close();
- }
- CATCH (CException, eSave)
- {
- file.Abort(); // will not throw an exception
- return FALSE;
- }
- END_CATCH
- // replace calls to Serialize with SaveDIB function
- return FALSE;
- }