Пример #1
0
void CStorageDoc::OnFileImport () {
	CFile		fImport;
	CFileDialog	dlgOpen (true);
	// #### TODO: Place here initial directory name taken from configuration structures
	dlgOpen.m_ofn.lpstrInitialDir = ".";
	dlgOpen.m_ofn.lpstrFilter = "Kontron Elektronik Images (*.img)""\0""*.IMG""\0"
		"All supported signle image formats""\0""*.JPG;*.JPEG;*.GIF;*.PNG;*.BMP;*.DIB""\0"
		"JPEG Bitmap (*.jpg, *.jpeg)""\0""*.JPG;*.JPEG""\0"
		"CumpuServe GIF (*.gif)""\0""*.GIF""\0"
		"Prtable Network Graphics (*.png)""\0""*.PNG""\0"
		"MS Windows Bitmap (*.bmp, *.dib)""\0""*.BMP;*.DIB""\0";
	dlgOpen.m_ofn.nFilterIndex = 1;
//	dlgOpen.SetWindowText (theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION));
	if (dlgOpen.DoModal () != IDOK)
		return;

	// #### TODO: Add support for video formats
	// #### TODO: Add support for multi-page tif format
	// #### TODO: Create import dialog
	// #### TODO: Add progress control on the status bar

	uvar32_64	selected;
	svar32_64	nImages;
	uvar32_64	dwSize = m_nDimX * m_nDimY;
	CString		strTitle;
	BYTE		*pbPixels = (BYTE *)aimMemoryCommit (dwSize, "CStorageDoc::OnDocAddImportseq", "pbPixels");


	if ((selected = Frames[aimActive].GetActiveImageNo()) == -1)
		selected = Images.GetCount () - 1;

	theApp.StatusState (IDI_INDICATOR_IMPORT);
	if (dlgOpen.m_ofn.nFilterIndex == 1) {
		if (!fImport.Open (dlgOpen.GetPathName (), CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite)) {
			MessageBox (theApp.m_pMainWnd->m_hWnd, theApp.LoadString (IDS_IMPORTSEQ_OPENERR), theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION), MB_OK | MB_ICONERROR);
			return;
		}
		BYTE	*pbHeader = new BYTE[0x80];
		fImport.Read ( pbHeader, 0x80 );
		if (((DWORD*)(pbHeader + 2))[0] != 0xB06D1247) {
			MessageBox  (theApp.m_pMainWnd->m_hWnd, theApp.LoadString (IDS_IMPORTSEQ_TYPEERR), theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION), MB_OK | MB_ICONERROR);
			return;
		}
		if (((WORD*)pbHeader)[5] != 0x4321) {
			MessageBox  (theApp.m_pMainWnd->m_hWnd, theApp.LoadString (IDS_IMPORTSEQ_TYPEERR), theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION), MB_OK | MB_ICONERROR);
			return;
		}
		if (((WORD*)pbHeader)[3] != m_nDimX || ((WORD*)pbHeader)[4] != m_nDimY || m_nBPP != 8) {
			MessageBox  (theApp.m_pMainWnd->m_hWnd, theApp.LoadString (IDS_IMPORTSEQ_SIZEERR), theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION), MB_OK | MB_ICONERROR);
			return;
		}
		nImages = ((WORD*)pbHeader)[6];

		theApp.StatusState (IDI_INDICATOR_IMPORT);
		while (--nImages >= 0) {
			strTitle.Format ("%s #%03d", dlgOpen.GetFileTitle (), ((WORD*)pbHeader)[6] - nImages);
			CPicture *pImg = new CPicture (this, strTitle, "");
			fImport.Read (pbPixels, dwSize);
			pImg->Channels(aimAll).SetBits (pbPixels, dwSize);
			Images.Insert (selected++, *pImg);
		}
		nImages = ((WORD*)pbHeader)[6];

		fImport.Close ();
		delete [] pbHeader;
		aimMemoryRelease (pbPixels, "CStorage::OnDocAddImportseq", "pbPixels");
	} else if (dlgOpen.m_ofn.nFilterIndex >= 2) {
		CImage image;
		image.Load (dlgOpen.GetPathName ());
		if (image.GetBPP () != m_nBPP || image.GetHeight () != m_nDimY || image.GetWidth () != m_nDimX) {
			MessageBox  (theApp.m_pMainWnd->m_hWnd, theApp.LoadString (IDS_IMPORTSEQ_SIZEERR), theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION), MB_OK | MB_ICONERROR);
			return;
		}
		CPicture *pImg = new CPicture (this, dlgOpen.GetFileTitle (), "");
		ubyte *pBits;
		if (image.IsDIBSection () && image.GetPitch () < 0)
			pBits = (ubyte*)image.GetBits () - (m_nDimY - 1) * m_nDimX;
		else
			pBits = (ubyte*)image.GetBits ();
		pImg->Channels(aimAll).SetBits (pBits, m_nDimX * m_nDimY * m_nBPP / 8);
		Images.Insert (selected++, *pImg);
	}

	theApp.StatusState (0);
}