BSEImgFileMgr.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "BSEImgFileMgr.h"
  4. namespace OTSMODEL {
  5. // CMsrParamFileMrg
  6. IMPLEMENT_SERIAL(CBSEImgFileMgr, CObject, 1)
  7. // constructor
  8. CBSEImgFileMgr::CBSEImgFileMgr()
  9. {
  10. // initialization
  11. Init();
  12. }
  13. // destructor
  14. CBSEImgFileMgr::~CBSEImgFileMgr()
  15. {
  16. // cleanup
  17. Cleanup();
  18. }
  19. // CMsrParamFileMrg member functions
  20. // public
  21. // serialization
  22. void CBSEImgFileMgr::Serialize(CArchive& ar)
  23. {
  24. // store?
  25. if (ar.IsStoring())
  26. {
  27. // store
  28. ar << BSE_IMG_FILE_MARK;
  29. ar << BSE_IMG_FILE_VERSION;
  30. }
  31. else
  32. {
  33. // load
  34. int nMark;
  35. ar >> nMark;
  36. if (nMark != BSE_IMG_FILE_MARK)
  37. {
  38. // invalid stage file
  39. LogErrorTrace(__FILE__, __LINE__, _T("Serialize: invalid BSE image file."));
  40. return;
  41. }
  42. CString strFileVersion;
  43. ar >> strFileVersion;
  44. }
  45. // member objects serialization
  46. m_poBSE->Serialize(ar);
  47. // base object serialization
  48. CObject::Serialize(ar);
  49. }
  50. // Load/Save
  51. BOOL CBSEImgFileMgr::Load(CString a_strPathName /*= _T("")*/, BOOL a_bClear /*= TRUE*/)
  52. {
  53. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  54. // clear all data if necessary
  55. if (a_bClear)
  56. {
  57. Init();
  58. }
  59. // check file pathname
  60. a_strPathName.Trim();
  61. if (a_strPathName.IsEmpty())
  62. {
  63. // file open dialog
  64. CFileDialog dlg(TRUE, BSE_IMG_FILE_EXT, NULL, OFN_FILEMUSTEXIST, BSE_IMG_FILE_FILTER);
  65. if (dlg.DoModal() != IDOK)
  66. {
  67. return FALSE;
  68. }
  69. // get file pathname
  70. a_strPathName = dlg.GetPathName();
  71. }
  72. // open the particle analysis standard file
  73. CFile hFile;
  74. CFileException ex;
  75. if (!hFile.Open(a_strPathName, CFile::modeRead, &ex))
  76. {
  77. // failed to open the file
  78. TCHAR szCause[255];
  79. ex.GetErrorMessage(szCause, 255);
  80. LogErrorTrace(__FILE__, __LINE__, _T("Load: can't open file %s. error: %s"), a_strPathName, szCause);
  81. return FALSE;
  82. }
  83. // create a loading archive
  84. CArchive ar(&hFile, CArchive::load);
  85. // file serialization (load)
  86. Serialize(ar);
  87. // close the file
  88. ar.Close();
  89. // file pathname
  90. m_strPathName = a_strPathName;
  91. // ok, return TRUE
  92. return TRUE;
  93. }
  94. BOOL CBSEImgFileMgr::Save(CString a_strPathName /*= _T("")*/)
  95. {
  96. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  97. // check file pathname
  98. a_strPathName.Trim();
  99. if (a_strPathName.IsEmpty())
  100. {
  101. // file save as dialog
  102. CFileDialog dlg(FALSE, BSE_IMG_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, BSE_IMG_FILE_FILTER);
  103. if (dlg.DoModal() != IDOK)
  104. {
  105. return FALSE;
  106. }
  107. // get file pathname
  108. a_strPathName = dlg.GetPathName();
  109. }
  110. // create the file
  111. CFile hFile;
  112. CFileException ex;
  113. if (!hFile.Open(a_strPathName, CFile::modeCreate | CFile::modeWrite, &ex))
  114. {
  115. // failed to open file
  116. TCHAR szCause[255];
  117. ex.GetErrorMessage(szCause, 255);
  118. LogErrorTrace(__FILE__, __LINE__, _T("Save: failed to create file. %s. error: %s"), a_strPathName, szCause);
  119. return FALSE;
  120. }
  121. // create a store archive
  122. CArchive ar(&hFile, CArchive::store);
  123. // file serialization (store)
  124. Serialize(ar);
  125. // close the file
  126. //hFile.Close();
  127. // file pathname
  128. m_strPathName = a_strPathName;
  129. // ok, return TRUE
  130. return TRUE;
  131. }
  132. BOOL CBSEImgFileMgr::LoadFromBitmap(CString a_strPathName /*= _T("")*/, BOOL a_bClear/* = TRUE*/)
  133. {
  134. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  135. // prepare file
  136. CFile file;
  137. CFileException fe;
  138. if (a_bClear)
  139. {
  140. Init();
  141. }
  142. // check file pathname
  143. a_strPathName.Trim();
  144. if (a_strPathName.IsEmpty())
  145. {
  146. // file save as dialog
  147. CFileDialog dlg(TRUE, BMP_IMG_FILE_EXT, NULL, OFN_FILEMUSTEXIST, BMP_IMG_FILE_FILTER);
  148. if (dlg.DoModal() != IDOK)
  149. {
  150. return FALSE;
  151. }
  152. // get file pathname
  153. m_strPathName = dlg.GetPathName();
  154. }
  155. else
  156. {
  157. m_strPathName = a_strPathName;
  158. }
  159. BYTE* pPixel;
  160. BYTE* pBmInfo;
  161. BITMAPFILEHEADER* pBmfh;
  162. pBmfh = new BITMAPFILEHEADER;
  163. if (!file.Open(m_strPathName, CFile::modeRead, &fe))
  164. {
  165. LogInfoTrace(__FILE__, __LINE__, _T("CBSEImgFileMgr::LoadFromBitmap: failed to open image file %s."), a_strPathName);
  166. return FALSE;
  167. }
  168. // read file header
  169. file.Read(pBmfh, sizeof(BITMAPFILEHEADER));
  170. pBmInfo = new BYTE[pBmfh->bfOffBits - 14];
  171. file.Read(pBmInfo, pBmfh->bfOffBits - 14);
  172. BITMAPINFOHEADER bmih;
  173. memcpy(&bmih, pBmInfo, sizeof(BITMAPINFOHEADER));
  174. long width = bmih.biWidth;//获取宽度
  175. int bitCount = bmih.biBitCount;//获取位数
  176. long height = bmih.biHeight;//获取高度
  177. long LineBytes = (width*bitCount + 31) / 32 * 4;//计算每行像素所占的字节数
  178. pPixel = new BYTE[height*LineBytes];
  179. file.Read(pPixel, height*LineBytes);
  180. file.Close();
  181. CRect imageRect(0, 0, width, height);
  182. m_poBSE->SetImageRect(imageRect);
  183. m_poBSE->InitImageData(width,height);
  184. BYTE* pImageData = m_poBSE->GetImageDataPointer();
  185. long nActImageSize = width * height;
  186. for (int i = 0; i < height; i++)
  187. {
  188. memcpy(pImageData + i*width, pPixel+(height-1-i)*width, width);
  189. }
  190. delete[]pPixel;
  191. delete[]pBmInfo;
  192. delete pBmfh;
  193. return TRUE;
  194. }
  195. BOOL CBSEImgFileMgr::SaveIntoBitmap(CString a_strPathName)
  196. {
  197. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  198. // prepare file
  199. CFile file;
  200. CFileException fe;
  201. // check file pathname
  202. a_strPathName.Trim();
  203. if (a_strPathName.IsEmpty())
  204. {
  205. // file save as dialog
  206. CFileDialog dlg(FALSE, BMP_IMG_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, BMP_IMG_FILE_FILTER);
  207. if (dlg.DoModal() != IDOK)
  208. {
  209. return FALSE;
  210. }
  211. // get file pathname
  212. a_strPathName = dlg.GetPathName();
  213. }
  214. // create the file
  215. if (!file.Open(a_strPathName, CFile::modeCreate | CFile::modeWrite, &fe))
  216. {
  217. // failed to open file
  218. TCHAR szCause[255];
  219. fe.GetErrorMessage(szCause, 255);
  220. LogErrorTrace(__FILE__, __LINE__, _T("SaveIntoBitmap: failed to create file. %s. error: %s"), a_strPathName, szCause);
  221. return FALSE;
  222. }
  223. LONG nHeight = 0;
  224. LONG nWidth = 0;
  225. DWORD bfSize = 0;
  226. nHeight = m_poBSE->GetHeight();
  227. nWidth = m_poBSE->GetWidth();
  228. BITMAPFILEHEADER* pBmfh;
  229. BITMAPINFOHEADER* pBmInfo;
  230. RGBQUAD* pColorTable;
  231. BYTE* pPixel;
  232. bfSize = nHeight + nWidth + 1078;
  233. pBmfh = new BITMAPFILEHEADER;
  234. pBmfh->bfType = 0x4d42; // BM
  235. pBmfh->bfSize = bfSize;
  236. pBmfh->bfReserved1 = 0;
  237. pBmfh->bfReserved2 = 0;
  238. pBmfh->bfOffBits = 1078; // 40 + 14 +1024
  239. pBmInfo = new BITMAPINFOHEADER;
  240. pBmInfo->biSize = 40;
  241. pBmInfo->biWidth = nWidth;
  242. pBmInfo->biHeight = nHeight;
  243. pBmInfo->biPlanes = 0;
  244. pBmInfo->biBitCount = 8;
  245. pBmInfo->biCompression = 0;
  246. pBmInfo->biSizeImage = nWidth * nHeight;
  247. pBmInfo->biXPelsPerMeter = 0;
  248. pBmInfo->biYPelsPerMeter = 0;
  249. pBmInfo->biClrUsed = 256;
  250. pBmInfo->biClrImportant = 0;
  251. pColorTable = new RGBQUAD[256];
  252. for (int i = 0; i<256; i++)
  253. {
  254. pColorTable[i].rgbBlue = i;
  255. pColorTable[i].rgbGreen = i;
  256. pColorTable[i].rgbRed = i;
  257. pColorTable[i].rgbReserved = 0;
  258. }
  259. try
  260. {
  261. file.Write(pBmfh, sizeof(BITMAPFILEHEADER));
  262. file.Write(pBmInfo, sizeof(BITMAPINFOHEADER));
  263. file.Write(pColorTable, sizeof(RGBQUAD) * 256);
  264. long LineBytes = (nWidth*pBmInfo->biBitCount + 31) / 32 * 4;//计算每行像素所占的字节数
  265. pPixel = new BYTE[nHeight*LineBytes];
  266. BYTE* pImageData = m_poBSE->GetImageDataPointer();
  267. for (int i = 0; i < nHeight; i++)
  268. {
  269. memcpy(pPixel + (nHeight - 1 - i)*nWidth, pImageData + i*nWidth, nWidth);
  270. }
  271. file.Write(pPixel, nHeight*LineBytes);
  272. delete[]pPixel;
  273. }
  274. catch (CException *pe)
  275. {
  276. pe->Delete();
  277. LogErrorTrace(__FILE__, __LINE__, _T("SaveIntoBitmap:can't write into file."));
  278. }
  279. delete pBmInfo;
  280. delete pBmfh;
  281. delete[] pColorTable;
  282. return TRUE;
  283. }
  284. void CBSEImgFileMgr::SetBSEImg(CBSEImgPtr a_poBSE)
  285. {
  286. ASSERT(a_poBSE);
  287. if (!a_poBSE)
  288. {
  289. LogErrorTrace(__FILE__, __LINE__, _T("SetBSEImg::invalid BSE image pointer"));
  290. return;
  291. }
  292. m_poBSE = a_poBSE;
  293. }
  294. void CBSEImgFileMgr::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  295. {
  296. //xmls::xBool xbModify;
  297. xmls::xInt xBseImgFileMark;
  298. xmls::xString xBseImgFileVersion;
  299. //xmls::Collection<CStage> xStagelist;
  300. xmls::Slo slo;
  301. slo.Register("BseImgFileMark", &xBseImgFileMark);
  302. slo.Register("BseImgFileVersion", &xBseImgFileVersion);
  303. //slo.Register("BSE", m_poBSE.get());
  304. //this->Register("WorkingStageId", &xBseImgFileMark);
  305. //this->Register("Stagelist", &xStagelist);
  306. //->Register("StageData", m_pStageData.get());
  307. if (isStoring)
  308. {
  309. xBseImgFileMark = BSE_IMG_FILE_MARK;
  310. xBseImgFileVersion = BSE_IMG_FILE_VERSION;
  311. //xBseImgFileMark = m_nWorkingStageId;
  312. slo.Serialize(true, classDoc, rootNode);
  313. }
  314. else
  315. {
  316. slo.Serialize(false, classDoc, rootNode);
  317. }
  318. }
  319. // protected
  320. // cleanup
  321. void CBSEImgFileMgr::Cleanup()
  322. {
  323. // need to do nothing at the moment
  324. }
  325. // initialization
  326. void CBSEImgFileMgr::Init()
  327. {
  328. // initialization
  329. m_poBSE = CBSEImgPtr(new CBSEImg());
  330. }
  331. // duplication
  332. void CBSEImgFileMgr::Duplicate(const CBSEImgFileMgr& a_oSource)
  333. {
  334. // initialization
  335. Init();
  336. // copy data over
  337. m_poBSE = CBSEImgPtr(new CBSEImg(a_oSource.m_poBSE.get()));
  338. }
  339. }