TIFFDoc.cpp
上传用户:lbr_007
上传日期:2019-05-31
资源大小:282k
文件大小:7k
- // TIFFDoc.cpp : implementation of the TIFFDoc class
- //
- #include "stdafx.h"
- #include "TIFFSample.h"
- #include "TIFFDoc.h"
- #include "dibtiff.h"
- #include "dibbmp.h"
- #include "dibijg.h"
- using namespace std;
- char * GetInitialFolder(void);
- /////////////////////////////////////////////////////////////////////////////
- // TIFFDoc
- IMPLEMENT_DYNCREATE(TIFFDoc, CDocument)
- BEGIN_MESSAGE_MAP(TIFFDoc, CDocument)
- //{{AFX_MSG_MAP(TIFFDoc)
- ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
- ON_COMMAND(ID_FILE_SAVE, OnFileSave)
- ON_COMMAND(ID_FILE_SAVE_AS, OnFileSaveAs)
- ON_UPDATE_COMMAND_UI(ID_FILE_PROPERTIES, OnUpdateFileProperties)
- ON_COMMAND(ID_FILE_PROPERTIES, OnFileProperties)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // TIFFDoc construction/destruction
- TIFFDoc::TIFFDoc()
- {
- m_fileType = UnknownFileType;
- m_currentDIB = 0;
- }
- TIFFDoc::~TIFFDoc()
- {
- ClearImages();
- }
- BOOL TIFFDoc::OnNewDocument()
- {
- if (!CDocument::OnNewDocument())
- return FALSE;
- // TODO: add reinitialization code here
- // (SDI documents will reuse this document)
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // TIFFDoc serialization
- void TIFFDoc::Serialize(CArchive& ar)
- {
- if (ar.IsStoring())
- {
- // TODO: add storing code here
- }
- else
- {
- // TODO: add loading code here
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // TIFFDoc diagnostics
- #ifdef _DEBUG
- void TIFFDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
- void TIFFDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // TIFFDoc commands
- void TIFFDoc::OnFileOpen()
- {
- CString file_types = "BMP, JPEG, and TIFF Files|*.bmp;*.jpg;*.jpeg;*.tif;*.tiff|";
- file_types += "BMP Files (*.bmp)|*.bmp|";
- file_types += "JPEG Files (*.jpg)|*.jpg;*.jpeg|";
- file_types += "TIFF Files (*.tif)|*.tif;*.tiff|";
- file_types += "|";
- CFileDialog dlg(TRUE,NULL,NULL,
- OFN_HIDEREADONLY | OFN_EXPLORER,
- (LPCTSTR)file_types,NULL);
- if (dlg.DoModal() == IDOK)
- {
- CString filename = dlg.GetPathName();
- m_fileType = DetermineFileType(filename);
- ClearImages();
- switch (m_fileType){
- case BMPFileType:
- {
- DIBSection * dib = new DIBSection;
- OpenBMP2DIB((LPCTSTR)filename, *dib);
- if (dib->IsCreated())
- {
- m_dibs.AddElement(dib);
- }
- else
- {
- delete dib;
- }
- }
- break;
- case JPEGFileType:
- {
- DIBSection * dib = new DIBSection;
- OpenIJG2DIB((LPCTSTR)filename, *dib);
- if (dib->IsCreated())
- {
- m_dibs.AddElement(dib);
- }
- else
- {
- delete dib;
- }
- }
- break;
- case TIFFFileType:
- {
- ArrayContainer<TIFFSubImage> imgs;
- ReadTIFFDirectories((LPCTSTR)filename, imgs);
- if (imgs.GetSize() > 0)
- {
- for (UINT32 i = 0; i < imgs.GetSize(); i++){
- DIBSection * dib = new DIBSection;
- OpenTIFF2DIB((LPCTSTR)filename, *dib, i);
- if (dib->IsCreated())
- {
- m_dibs.AddElement(dib);
- }
- else
- {
- delete dib;
- }
- }
- }
- }
- break;
- default:
- {
- AfxMessageBox("No support for this type of file",MB_ICONERROR);
- }
- break;
- }
- if (m_dibs.GetSize() > 0)
- {
- m_filename = (LPCTSTR)filename;
- SetTitle(m_filename.c_str());
- }
- UpdateAllViews(NULL);
- }
- }
- void TIFFDoc::ClearImages(void)
- {
- UINT32 size = m_dibs.GetSize();
- for (UINT32 i = 0; i < size; i++){
- delete m_dibs.GetData()[i];
- }
- m_dibs.ClearArray();
- }
- TIFFDoc::ImageFileTypes TIFFDoc::DetermineFileType(CString& filename)
- {
- ImageFileTypes result = UnknownFileType;
- CString tmpFile = filename;
- tmpFile.MakeUpper();
- int length = tmpFile.GetLength();
- if (length > 4)
- {
- char * cptr = (char *)(LPCTSTR)tmpFile;
- int last4 = length - 4;
- if (last4 < 0) last4 = 0;
- int last5 = length - 5;
- if (last5 < 0) last5 = 0;
- if (strcmp(&cptr[last4], ".BMP") == 0)
- {
- result = BMPFileType;
- }
- else if ((strcmp(&cptr[last4],".JPG") == 0) ||
- (strcmp(&cptr[last5],".JPEG") == 0))
- {
- result = JPEGFileType;
- }
- else if ((strcmp(&cptr[last4],".TIF") == 0) ||
- (strcmp(&cptr[last5],".TIFF") == 0))
- {
- result = TIFFFileType;
- }
- else if (strcmp(&cptr[last4],".GIF") == 0)
- {
- result = GIFFileType;
- }
- else if (cptr[last4] == '.')
- {
- result = OtherFileType;
- }
- }
- return result;
- }
- void TIFFDoc::OnFileSave()
- {
- CString file_types;
- file_types += "BMP File (*.bmp)|*.bmp|";
- file_types += "JPEG File (*.jpg)|*.jpg;*.jpeg|";
- file_types += "TIFF File (*.tif)|*.tif;*.tiff|";
- file_types += "|";
- CFileDialog dlg(FALSE,NULL,NULL,
- OFN_HIDEREADONLY | OFN_EXPLORER,
- (LPCTSTR)file_types,NULL);
- if (dlg.DoModal() == IDOK)
- {
- CString filename = dlg.GetPathName();
- UINT32 ext = dlg.GetTypeInfoCount();
- if (DetermineFileType(filename) == UnknownFileType)
- {
- switch ((dlg.m_ofn.nFilterIndex-1)){
- case 0: filename += ".bmp"; break;
- case 1: filename += ".jpg"; break;
- case 2: filename += ".tif"; break;
- }
- }
- int result = 0;
- CString errMsg;
- ImageFileTypes fileType = DetermineFileType(filename);
- DIBSection * dib = GetDIB(m_currentDIB);
- if (dib)
- {
- DIBSection saveDIB(*dib);
- switch (fileType){
- case BMPFileType:
- {
- result = SaveDIB2BMP((const char *)(LPCTSTR)filename,saveDIB);
- }
- break;
- case JPEGFileType:
- {
- result = SaveDIB2IJG((const char *)(LPCTSTR)filename,saveDIB);
- }
- break;
- case TIFFFileType:
- {
- result = SaveDIB2TIFF((const char *)(LPCTSTR)filename,saveDIB);
- }
- break;
- case GIFFileType:
- {
- errMsg = "GIF files not supported";
- }
- break;
- case OtherFileType:
- {
- errMsg = "File type not supported";
- }
- break;
- case UnknownFileType:
- {
- errMsg = "Unknown file type";
- }
- break;
- }
- }
- }
- }
- void TIFFDoc::OnFileSaveAs()
- {
- OnFileSave();
- }
- void TIFFDoc::GetFilenameParts(const string& fullFilePath,
- string& pathOnly, string& fileNameOnly, string& extOnly)
- {
- if (fullFilePath.length() > 0)
- {
- char c;
- INT32 i;
- INT32 ndx = fullFilePath.length() - 1;
- while (ndx > 0){
- c = fullFilePath.c_str()[ndx];
- if (c == (char)92)
- {
- for (i = (ndx+1); i < fullFilePath.length(); i++){
- c = fullFilePath.c_str()[i];
- if (c != '.')
- {
- fileNameOnly += c;
- }
- else
- {
- break;
- }
- }
- for (i = 0; i < ndx; i++){
- c = fullFilePath.c_str()[i];
- pathOnly += c;
- }
- break;
- }
- else if (c == '.')
- {
- extOnly = &fullFilePath.c_str()[ndx+1];
- }
- ndx--;
- }
- }
- }
- void TIFFDoc::OnUpdateFileProperties(CCmdUI* pCmdUI)
- {
- pCmdUI->Enable(m_fileType == TIFFFileType);
- }
- void TIFFDoc::OnFileProperties()
- {
- ArrayContainer<string> properties;
- GetTIFFProperties(m_filename.c_str(), m_currentDIB, properties);
- string msg = m_filename;
- msg += " has the following properties:n";
- for (int i = 0; i < properties.GetSize(); i++){
- msg += properties.GetData()[i];
- msg += "n";
- }
- AfxMessageBox(msg.c_str(), MB_ICONINFORMATION);
- }