#pragma once #include "stdafx.h" #include "BSEImgFileMgr.h" namespace OTSMODEL { // CMsrParamFileMrg IMPLEMENT_SERIAL(CBSEImgFileMgr, CObject, 1) // constructor CBSEImgFileMgr::CBSEImgFileMgr() { // initialization Init(); } // destructor CBSEImgFileMgr::~CBSEImgFileMgr() { // cleanup Cleanup(); } // CMsrParamFileMrg member functions // public // serialization void CBSEImgFileMgr::Serialize(CArchive& ar) { // store? if (ar.IsStoring()) { // store ar << BSE_IMG_FILE_MARK; ar << BSE_IMG_FILE_VERSION; } else { // load int nMark; ar >> nMark; if (nMark != BSE_IMG_FILE_MARK) { // invalid stage file LogErrorTrace(__FILE__, __LINE__, _T("Serialize: invalid BSE image file.")); return; } CString strFileVersion; ar >> strFileVersion; } // member objects serialization m_poBSE->Serialize(ar); // base object serialization CObject::Serialize(ar); } // Load/Save BOOL CBSEImgFileMgr::Load(CString a_strPathName /*= _T("")*/, BOOL a_bClear /*= TRUE*/) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // clear all data if necessary if (a_bClear) { Init(); } // check file pathname a_strPathName.Trim(); if (a_strPathName.IsEmpty()) { // file open dialog CFileDialog dlg(TRUE, BSE_IMG_FILE_EXT, NULL, OFN_FILEMUSTEXIST, BSE_IMG_FILE_FILTER); if (dlg.DoModal() != IDOK) { return FALSE; } // get file pathname a_strPathName = dlg.GetPathName(); } // open the particle analysis standard file CFile hFile; CFileException ex; if (!hFile.Open(a_strPathName, CFile::modeRead, &ex)) { // failed to open the file TCHAR szCause[255]; ex.GetErrorMessage(szCause, 255); LogErrorTrace(__FILE__, __LINE__, _T("Load: can't open file %s. error: %s"), a_strPathName, szCause); return FALSE; } // create a loading archive CArchive ar(&hFile, CArchive::load); // file serialization (load) Serialize(ar); // close the file ar.Close(); // file pathname m_strPathName = a_strPathName; // ok, return TRUE return TRUE; } BOOL CBSEImgFileMgr::Save(CString a_strPathName /*= _T("")*/) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // check file pathname a_strPathName.Trim(); if (a_strPathName.IsEmpty()) { // file save as dialog CFileDialog dlg(FALSE, BSE_IMG_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, BSE_IMG_FILE_FILTER); if (dlg.DoModal() != IDOK) { return FALSE; } // get file pathname a_strPathName = dlg.GetPathName(); } // create the file CFile hFile; CFileException ex; if (!hFile.Open(a_strPathName, CFile::modeCreate | CFile::modeWrite, &ex)) { // failed to open file TCHAR szCause[255]; ex.GetErrorMessage(szCause, 255); LogErrorTrace(__FILE__, __LINE__, _T("Save: failed to create file. %s. error: %s"), a_strPathName, szCause); return FALSE; } // create a store archive CArchive ar(&hFile, CArchive::store); // file serialization (store) Serialize(ar); // close the file //hFile.Close(); // file pathname m_strPathName = a_strPathName; // ok, return TRUE return TRUE; } BOOL CBSEImgFileMgr::LoadFromBitmap(CString a_strPathName /*= _T("")*/, BOOL a_bClear/* = TRUE*/) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // prepare file CFile file; CFileException fe; if (a_bClear) { Init(); } // check file pathname a_strPathName.Trim(); if (a_strPathName.IsEmpty()) { // file save as dialog CFileDialog dlg(TRUE, BMP_IMG_FILE_EXT, NULL, OFN_FILEMUSTEXIST, BMP_IMG_FILE_FILTER); if (dlg.DoModal() != IDOK) { return FALSE; } // get file pathname m_strPathName = dlg.GetPathName(); } else { m_strPathName = a_strPathName; } BYTE* pPixel; BYTE* pBmInfo; BITMAPFILEHEADER* pBmfh; pBmfh = new BITMAPFILEHEADER; if (!file.Open(m_strPathName, CFile::modeRead, &fe)) { LogInfoTrace(__FILE__, __LINE__, _T("CBSEImgFileMgr::LoadFromBitmap: failed to open image file %s."), a_strPathName); return FALSE; } // read file header file.Read(pBmfh, sizeof(BITMAPFILEHEADER)); pBmInfo = new BYTE[pBmfh->bfOffBits - 14]; file.Read(pBmInfo, pBmfh->bfOffBits - 14); BITMAPINFOHEADER bmih; memcpy(&bmih, pBmInfo, sizeof(BITMAPINFOHEADER)); long width = bmih.biWidth;//获取宽度 int bitCount = bmih.biBitCount;//获取位数 long height = bmih.biHeight;//获取高度 long LineBytes = (width*bitCount + 31) / 32 * 4;//计算每行像素所占的字节数 pPixel = new BYTE[height*LineBytes]; file.Read(pPixel, height*LineBytes); file.Close(); CRect imageRect(0, 0, width, height); m_poBSE->SetImageRect(imageRect); m_poBSE->InitImageData(width,height); BYTE* pImageData = m_poBSE->GetImageDataPointer(); long nActImageSize = width * height; for (int i = 0; i < height; i++) { memcpy(pImageData + i*width, pPixel+(height-1-i)*width, width); } delete[]pPixel; delete[]pBmInfo; delete pBmfh; return TRUE; } BOOL CBSEImgFileMgr::SaveIntoBitmap(CString a_strPathName) { AFX_MANAGE_STATE(AfxGetStaticModuleState()); // prepare file CFile file; CFileException fe; // check file pathname a_strPathName.Trim(); if (a_strPathName.IsEmpty()) { // file save as dialog CFileDialog dlg(FALSE, BMP_IMG_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, BMP_IMG_FILE_FILTER); if (dlg.DoModal() != IDOK) { return FALSE; } // get file pathname a_strPathName = dlg.GetPathName(); } // create the file if (!file.Open(a_strPathName, CFile::modeCreate | CFile::modeWrite, &fe)) { // failed to open file TCHAR szCause[255]; fe.GetErrorMessage(szCause, 255); LogErrorTrace(__FILE__, __LINE__, _T("SaveIntoBitmap: failed to create file. %s. error: %s"), a_strPathName, szCause); return FALSE; } LONG nHeight = 0; LONG nWidth = 0; DWORD bfSize = 0; nHeight = m_poBSE->GetHeight(); nWidth = m_poBSE->GetWidth(); BITMAPFILEHEADER* pBmfh; BITMAPINFOHEADER* pBmInfo; RGBQUAD* pColorTable; BYTE* pPixel; bfSize = nHeight + nWidth + 1078; pBmfh = new BITMAPFILEHEADER; pBmfh->bfType = 0x4d42; // BM pBmfh->bfSize = bfSize; pBmfh->bfReserved1 = 0; pBmfh->bfReserved2 = 0; pBmfh->bfOffBits = 1078; // 40 + 14 +1024 pBmInfo = new BITMAPINFOHEADER; pBmInfo->biSize = 40; pBmInfo->biWidth = nWidth; pBmInfo->biHeight = nHeight; pBmInfo->biPlanes = 0; pBmInfo->biBitCount = 8; pBmInfo->biCompression = 0; pBmInfo->biSizeImage = nWidth * nHeight; pBmInfo->biXPelsPerMeter = 0; pBmInfo->biYPelsPerMeter = 0; pBmInfo->biClrUsed = 256; pBmInfo->biClrImportant = 0; pColorTable = new RGBQUAD[256]; for (int i = 0; i<256; i++) { pColorTable[i].rgbBlue = i; pColorTable[i].rgbGreen = i; pColorTable[i].rgbRed = i; pColorTable[i].rgbReserved = 0; } try { file.Write(pBmfh, sizeof(BITMAPFILEHEADER)); file.Write(pBmInfo, sizeof(BITMAPINFOHEADER)); file.Write(pColorTable, sizeof(RGBQUAD) * 256); long LineBytes = (nWidth*pBmInfo->biBitCount + 31) / 32 * 4;//计算每行像素所占的字节数 pPixel = new BYTE[nHeight*LineBytes]; BYTE* pImageData = m_poBSE->GetImageDataPointer(); for (int i = 0; i < nHeight; i++) { memcpy(pPixel + (nHeight - 1 - i)*nWidth, pImageData + i*nWidth, nWidth); } file.Write(pPixel, nHeight*LineBytes); delete[]pPixel; } catch (CException *pe) { pe->Delete(); LogErrorTrace(__FILE__, __LINE__, _T("SaveIntoBitmap:can't write into file.")); } delete pBmInfo; delete pBmfh; delete[] pColorTable; return TRUE; } void CBSEImgFileMgr::SetBSEImg(CBSEImgPtr a_poBSE) { ASSERT(a_poBSE); if (!a_poBSE) { LogErrorTrace(__FILE__, __LINE__, _T("SetBSEImg::invalid BSE image pointer")); return; } m_poBSE = a_poBSE; } void CBSEImgFileMgr::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode) { //xmls::xBool xbModify; xmls::xInt xBseImgFileMark; xmls::xString xBseImgFileVersion; //xmls::Collection xStagelist; xmls::Slo slo; slo.Register("BseImgFileMark", &xBseImgFileMark); slo.Register("BseImgFileVersion", &xBseImgFileVersion); //slo.Register("BSE", m_poBSE.get()); //this->Register("WorkingStageId", &xBseImgFileMark); //this->Register("Stagelist", &xStagelist); //->Register("StageData", m_pStageData.get()); if (isStoring) { xBseImgFileMark = BSE_IMG_FILE_MARK; xBseImgFileVersion = BSE_IMG_FILE_VERSION; //xBseImgFileMark = m_nWorkingStageId; slo.Serialize(true, classDoc, rootNode); } else { slo.Serialize(false, classDoc, rootNode); } } // protected // cleanup void CBSEImgFileMgr::Cleanup() { // need to do nothing at the moment } // initialization void CBSEImgFileMgr::Init() { // initialization m_poBSE = CBSEImgPtr(new CBSEImg()); } // duplication void CBSEImgFileMgr::Duplicate(const CBSEImgFileMgr& a_oSource) { // initialization Init(); // copy data over m_poBSE = CBSEImgPtr(new CBSEImg(a_oSource.m_poBSE.get())); } }