BSEImgFileMgr.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. //
  98. // // check file pathname
  99. // a_strPathName.Trim();
  100. // if (a_strPathName.IsEmpty())
  101. // {
  102. // // file save as dialog
  103. // CFileDialog dlg(FALSE, BSE_IMG_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, BSE_IMG_FILE_FILTER);
  104. // if (dlg.DoModal() != IDOK)
  105. // {
  106. // return FALSE;
  107. // }
  108. // // get file pathname
  109. // a_strPathName = dlg.GetPathName();
  110. // }
  111. // // create the file
  112. // CFile hFile;
  113. // CFileException ex;
  114. // if (!hFile.Open(a_strPathName, CFile::modeCreate | CFile::modeWrite, &ex))
  115. // {
  116. // // failed to open file
  117. // TCHAR szCause[255];
  118. // ex.GetErrorMessage(szCause, 255);
  119. // LogErrorTrace(__FILE__, __LINE__, _T("Save: failed to create file. %s. error: %s"), a_strPathName, szCause);
  120. // return FALSE;
  121. // }
  122. // // create a store archive
  123. // CArchive ar(&hFile, CArchive::store);
  124. // // file serialization (store)
  125. // Serialize(ar);
  126. // // close the file
  127. // //hFile.Close();
  128. // // file pathname
  129. // m_strPathName = a_strPathName;
  130. // // ok, return TRUE
  131. // return TRUE;
  132. //}
  133. BOOL CBSEImgFileMgr::LoadFromBitmap(CString a_strPathName /*= _T("")*/, BOOL a_bClear/* = TRUE*/)
  134. {
  135. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  136. // prepare file
  137. CFile file;
  138. CFileException fe;
  139. if (a_bClear)
  140. {
  141. //Init();
  142. }
  143. // check file pathname
  144. a_strPathName.Trim();
  145. if (a_strPathName.IsEmpty())
  146. {
  147. // file save as dialog
  148. CFileDialog dlg(TRUE, BMP_IMG_FILE_EXT, NULL, OFN_FILEMUSTEXIST, BMP_IMG_FILE_FILTER);
  149. if (dlg.DoModal() != IDOK)
  150. {
  151. return FALSE;
  152. }
  153. // get file pathname
  154. m_strPathName = dlg.GetPathName();
  155. }
  156. else
  157. {
  158. m_strPathName = a_strPathName;
  159. }
  160. BYTE* pPixel;
  161. BYTE* pBmInfo;
  162. BITMAPFILEHEADER* pBmfh;
  163. pBmfh = new BITMAPFILEHEADER;
  164. if (!file.Open(m_strPathName, CFile::modeRead, &fe))
  165. {
  166. LogInfoTrace(__FILE__, __LINE__, _T("CBSEImgFileMgr::LoadFromBitmap: failed to open image file %s."), a_strPathName);
  167. return FALSE;
  168. }
  169. // read file header
  170. file.Read(pBmfh, sizeof(BITMAPFILEHEADER));
  171. pBmInfo = new BYTE[pBmfh->bfOffBits - 14];
  172. file.Read(pBmInfo, pBmfh->bfOffBits - 14);
  173. BITMAPINFOHEADER bmih;
  174. memcpy(&bmih, pBmInfo, sizeof(BITMAPINFOHEADER));
  175. long width = bmih.biWidth;//获取宽度
  176. int bitCount = bmih.biBitCount;//获取位数
  177. long height = bmih.biHeight;//获取高度
  178. long LineBytes = (width*bitCount + 31) / 32 * 4;//计算每行像素所占的字节数
  179. pPixel = new BYTE[height*LineBytes];
  180. file.Read(pPixel, height*LineBytes);
  181. file.Close();
  182. CRect imageRect(0, 0, width, height);
  183. m_poBSE = CBSEImgPtr(new CBSEImg(imageRect));
  184. //m_poBSE->SetImageRect(imageRect);
  185. m_poBSE->InitImageData(width,height);
  186. BYTE* pImageData = m_poBSE->GetImageDataPointer();
  187. long nActImageSize = width * height;
  188. for (int i = 0; i < height; i++)
  189. {
  190. memcpy(pImageData + i*width, pPixel+(height-1-i)*width, width);
  191. }
  192. delete[]pPixel;
  193. delete[]pBmInfo;
  194. delete pBmfh;
  195. return TRUE;
  196. }
  197. BOOL CBSEImgFileMgr::SaveIntoBitmap(CString a_strPathName)
  198. {
  199. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  200. // prepare file
  201. CFile file;
  202. CFileException fe;
  203. // check file pathname
  204. a_strPathName.Trim();
  205. if (a_strPathName.IsEmpty())
  206. {
  207. // file save as dialog
  208. CFileDialog dlg(FALSE, BMP_IMG_FILE_EXT, NULL, OFN_OVERWRITEPROMPT, BMP_IMG_FILE_FILTER);
  209. if (dlg.DoModal() != IDOK)
  210. {
  211. return FALSE;
  212. }
  213. // get file pathname
  214. a_strPathName = dlg.GetPathName();
  215. }
  216. // create the file
  217. if (!file.Open(a_strPathName, CFile::modeCreate | CFile::modeWrite, &fe))
  218. {
  219. // failed to open file
  220. TCHAR szCause[255];
  221. fe.GetErrorMessage(szCause, 255);
  222. LogErrorTrace(__FILE__, __LINE__, _T("SaveIntoBitmap: failed to create file. %s. error: %s"), a_strPathName, szCause);
  223. return FALSE;
  224. }
  225. LONG nHeight = 0;
  226. LONG nWidth = 0;
  227. DWORD bfSize = 0;
  228. nHeight = m_poBSE->GetHeight();
  229. nWidth = m_poBSE->GetWidth();
  230. BITMAPFILEHEADER* pBmfh;
  231. BITMAPINFOHEADER* pBmInfo;
  232. RGBQUAD* pColorTable;
  233. BYTE* pPixel;
  234. bfSize = nHeight + nWidth + 1078;
  235. pBmfh = new BITMAPFILEHEADER;
  236. pBmfh->bfType = 0x4d42; // BM
  237. pBmfh->bfSize = bfSize;
  238. pBmfh->bfReserved1 = 0;
  239. pBmfh->bfReserved2 = 0;
  240. pBmfh->bfOffBits = 1078; // 40 + 14 +1024
  241. pBmInfo = new BITMAPINFOHEADER;
  242. pBmInfo->biSize = 40;
  243. pBmInfo->biWidth = nWidth;
  244. pBmInfo->biHeight = nHeight;
  245. pBmInfo->biPlanes = 0;
  246. pBmInfo->biBitCount = 8;
  247. pBmInfo->biCompression = 0;
  248. pBmInfo->biSizeImage = nWidth * nHeight;
  249. pBmInfo->biXPelsPerMeter = 0;
  250. pBmInfo->biYPelsPerMeter = 0;
  251. pBmInfo->biClrUsed = 256;
  252. pBmInfo->biClrImportant = 0;
  253. pColorTable = new RGBQUAD[256];
  254. for (int i = 0; i<256; i++)
  255. {
  256. pColorTable[i].rgbBlue = i;
  257. pColorTable[i].rgbGreen = i;
  258. pColorTable[i].rgbRed = i;
  259. pColorTable[i].rgbReserved = 0;
  260. }
  261. try
  262. {
  263. file.Write(pBmfh, sizeof(BITMAPFILEHEADER));
  264. file.Write(pBmInfo, sizeof(BITMAPINFOHEADER));
  265. file.Write(pColorTable, sizeof(RGBQUAD) * 256);
  266. long LineBytes = (nWidth*pBmInfo->biBitCount + 31) / 32 * 4;//计算每行像素所占的字节数
  267. pPixel = new BYTE[nHeight*LineBytes];
  268. BYTE* pImageData = m_poBSE->GetImageDataPointer();
  269. for (int i = 0; i < nHeight; i++)
  270. {
  271. memcpy(pPixel + (nHeight - 1 - i)*nWidth, pImageData + i*nWidth, nWidth);
  272. }
  273. file.Write(pPixel, nHeight*LineBytes);
  274. delete[]pPixel;
  275. }
  276. catch (CException *pe)
  277. {
  278. pe->Delete();
  279. LogErrorTrace(__FILE__, __LINE__, _T("SaveIntoBitmap:can't write into file."));
  280. }
  281. delete pBmInfo;
  282. delete pBmfh;
  283. delete[] pColorTable;
  284. return TRUE;
  285. }
  286. void CBSEImgFileMgr::SetBSEImg(CBSEImgPtr a_poBSE)
  287. {
  288. ASSERT(a_poBSE);
  289. if (!a_poBSE)
  290. {
  291. LogErrorTrace(__FILE__, __LINE__, _T("SetBSEImg::invalid BSE image pointer"));
  292. return;
  293. }
  294. m_poBSE = a_poBSE;
  295. }
  296. //void CBSEImgFileMgr::Serialize(bool isStoring, tinyxml2::XMLDocument * classDoc, tinyxml2::XMLElement * rootNode)
  297. //{
  298. // //xmls::xBool xbModify;
  299. //
  300. // xmls::xInt xBseImgFileMark;
  301. // xmls::xString xBseImgFileVersion;
  302. // //xmls::Collection<CStage> xStagelist;
  303. // xmls::Slo slo;
  304. // slo.Register("BseImgFileMark", &xBseImgFileMark);
  305. // slo.Register("BseImgFileVersion", &xBseImgFileVersion);
  306. // //slo.Register("BSE", m_poBSE.get());
  307. // //this->Register("WorkingStageId", &xBseImgFileMark);
  308. // //this->Register("Stagelist", &xStagelist);
  309. // //->Register("StageData", m_pStageData.get());
  310. // if (isStoring)
  311. // {
  312. // xBseImgFileMark = BSE_IMG_FILE_MARK;
  313. // xBseImgFileVersion = BSE_IMG_FILE_VERSION;
  314. // //xBseImgFileMark = m_nWorkingStageId;
  315. // slo.Serialize(true, classDoc, rootNode);
  316. // }
  317. // else
  318. // {
  319. // slo.Serialize(false, classDoc, rootNode);
  320. // }
  321. //}
  322. // protected
  323. // cleanup
  324. //void CBSEImgFileMgr::Cleanup()
  325. //{
  326. // // need to do nothing at the moment
  327. //}
  328. //// initialization
  329. //void CBSEImgFileMgr::Init(int w,int h)
  330. //{
  331. // // initialization
  332. // m_poBSE = CBSEImgPtr(new CBSEImg(CRect(0,0,w,h)));
  333. //}
  334. // duplication
  335. //void CBSEImgFileMgr::Duplicate(const CBSEImgFileMgr& a_oSource)
  336. //{
  337. // // initialization
  338. // //Init();
  339. // // copy data over
  340. // m_poBSE = CBSEImgPtr(new CBSEImg(a_oSource.m_poBSE.get()));
  341. //}
  342. }