// raw2BmpDoc.cpp : implementation of the CRaw2BmpDoc class
//
#include "stdafx.h"
#include "raw2Bmp.h"
#include "raw2BmpDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRaw2BmpDoc
IMPLEMENT_DYNCREATE(CRaw2BmpDoc, CDocument)
BEGIN_MESSAGE_MAP(CRaw2BmpDoc, CDocument)
//{{AFX_MSG_MAP(CRaw2BmpDoc)
ON_COMMAND(IDM_BMP_IMPORT, OnBmpImport)
ON_COMMAND(IDM_BMP_EXPORT, OnBmpExport)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRaw2BmpDoc construction/destruction
CRaw2BmpDoc::CRaw2BmpDoc()
{
// TODO: add one-time construction code here
m_nWidth = 512;
m_nHeight = 512;
m_hImage = NULL;
}
CRaw2BmpDoc::~CRaw2BmpDoc()
{
}
BOOL CRaw2BmpDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CRaw2BmpDoc serialization
void CRaw2BmpDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
if (m_hImage == NULL) return;
DWORD dwImageSize = GlobalSize(m_hImage);
LPBYTE pImage = (LPBYTE)GlobalLock(m_hImage);
ar.Write(pImage, dwImageSize);
GlobalUnlock(m_hImage);
}
else
{
// TODO: add loading code here
if (m_hImage != NULL) GlobalFree(m_hImage);
CFile *pFile = ar.GetFile();
DWORD dwImageSize = pFile->GetLength();
m_hImage = GlobalAlloc(GHND, dwImageSize);
LPBYTE pImage = (LPBYTE)GlobalLock(m_hImage);
ar.Read(pImage, dwImageSize);
GlobalUnlock(m_hImage);
m_nHeight = dwImageSize / m_nWidth / 3;
}
}
/////////////////////////////////////////////////////////////////////////////
// CRaw2BmpDoc diagnostics
#ifdef _DEBUG
void CRaw2BmpDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CRaw2BmpDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CRaw2BmpDoc commands
void CRaw2BmpDoc::OnBmpImport()
{
// TODO: Add your command handler code here
char szFilter[] = "BMP 파일 (*.bmp)|*.bmp|모든 파일 (*.*)|*.*||";
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter);
if (dlg.DoModal() != IDOK) return;
char str[200];
LPCTSTR lpszPathName = (LPCTSTR)strcpy(str, dlg.GetPathName());
BITMAPFILEHEADER bmpFileHeader;
BITMAPINFO bmpInfo;
CFile file;
file.Open(lpszPathName, CFile::modeRead);
file.Read(&bmpFileHeader, sizeof(BITMAPFILEHEADER));
file.Read(&bmpInfo, sizeof(BITMAPINFO));
file.Close();
if (bmpFileHeader.bfType != *(WORD *)"BM") return;
int nBitCount;
if (bmpInfo.bmiHeader.biSize == sizeof(BITMAPCOREHEADER)) {
m_nWidth = ((BITMAPCOREHEADER *)&bmpInfo)->bcWidth;
m_nHeight = ((BITMAPCOREHEADER *)&bmpInfo)->bcHeight;
nBitCount = ((BITMAPCOREHEADER *)&bmpInfo)->bcBitCount;
} else {
m_nWidth = bmpInfo.bmiHeader.biWidth;
m_nHeight = bmpInfo.bmiHeader.biHeight;
nBitCount = bmpInfo.bmiHeader.biBitCount;
}
if (nBitCount != 24) return;
int nBytesPerPixel = nBitCount / 8;
int nBytesPerLine = (m_nWidth * nBytesPerPixel + 3) / 4 * 4;
DWORD dwBmpImageSize = (DWORD)m_nHeight * nBytesPerLine;
if (m_hImage != NULL) { GlobalFree(m_hImage); m_hImage = NULL; }
HGLOBAL hImageBmp = GlobalAlloc(GHND, dwBmpImageSize);
LPBYTE pImageBmp = (LPBYTE)GlobalLock(hImageBmp);
file.Open(lpszPathName, CFile::modeRead);
file.Seek(bmpFileHeader.bfOffBits, CFile::begin);
file.Read(pImageBmp, dwBmpImageSize);
file.Close();
if (m_hImage != NULL) { GlobalFree(m_hImage); m_hImage = NULL; }
DWORD dwRawImageSize = (DWORD)m_nWidth * m_nHeight * nBytesPerPixel;
m_hImage = GlobalAlloc(GHND, dwRawImageSize);
LPBYTE pImageDst = (LPBYTE)GlobalLock(m_hImage);
for (int y=0; y<m_nHeight; y++) {
LPBYTE pImageSrc = pImageBmp + (DWORD)(m_nHeight - 1 - y) * nBytesPerLine;
for (int x=0; x<m_nWidth; x++) {
int r = *(pImageSrc + 2);
int g = *(pImageSrc + 1);
int b = *(pImageSrc + 0);
*(pImageDst + 0) = r;
*(pImageDst + 1) = g;
*(pImageDst + 2) = b;
pImageSrc += 3;
pImageDst += 3;
}
}
GlobalUnlock(hImageBmp);
GlobalFree(hImageBmp);
GlobalUnlock(m_hImage);
UpdateAllViews(NULL);
}
void CRaw2BmpDoc::OnBmpExport()
{
// TODO: Add your command handler code here
if (m_hImage == NULL) return;
char szFilter[] = "BMP 파일 (*.bmp)|*.bmp|모든 파일 (*.*)|*.*||";
CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter);
if (dlg.DoModal() != IDOK) return;
char str[200];
LPCTSTR lpszPathName = (LPCTSTR)strcpy(str, dlg.GetPathName());
int nBytesPerPixel = 3;
int nBytesPerLine = (m_nWidth * nBytesPerPixel + 3) / 4 * 4;
DWORD dwBmpImageSize = (DWORD)m_nHeight * nBytesPerLine;
BITMAPFILEHEADER bmpFileHeader;
bmpFileHeader.bfType = *(WORD *)"BM";
bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO);
bmpFileHeader.bfSize = bmpFileHeader.bfOffBits + dwBmpImageSize;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
BITMAPINFO bmpInfo;
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFO);
bmpInfo.bmiHeader.biWidth = m_nWidth;
bmpInfo.bmiHeader.biHeight = m_nHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = nBytesPerPixel * 8;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = 0;
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
HGLOBAL hImageBmp = GlobalAlloc(GHND, dwBmpImageSize);
LPBYTE pImageBmp = (LPBYTE)GlobalLock(hImageBmp);
LPBYTE pImageSrc = (LPBYTE)GlobalLock(m_hImage);
for (int y=0; y<m_nHeight; y++) {
LPBYTE pImageDst = pImageBmp + (DWORD)(m_nHeight - 1 - y) * nBytesPerLine;
for (int x=0; x<m_nWidth; x++) {
int r = *(pImageSrc + 0);
int g = *(pImageSrc + 1);
int b = *(pImageSrc + 2);
*(pImageDst + 2) = r;
*(pImageDst + 1) = g;
*(pImageDst + 0) = b;
pImageSrc += 3;
pImageDst += 3;
}
}
GlobalUnlock(m_hImage);
CFile file;
file.Open(lpszPathName, CFile::modeWrite | CFile::modeCreate);
file.Write(&bmpFileHeader, sizeof(BITMAPFILEHEADER));
file.Write(&bmpInfo, sizeof(BITMAPINFO));
file.Write(pImageBmp, dwBmpImageSize);
file.Close();
GlobalUnlock(hImageBmp);
GlobalFree(hImageBmp);
}