home *** CD-ROM | disk | FTP | other *** search
- // mfcdoc.cpp : implementation of the CMfcDoc class
- //
-
- #include "stdafx.h"
- #include "imview.h"
- #include <limits.h>
-
- #include "mfcdoc.h"
- #include "sky16v3b.h"
- #include "encdlg.h"
- #include "decdlg.h"
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CDibDoc
-
- IMPLEMENT_DYNCREATE(CMfcDoc, CDocument)
-
- BEGIN_MESSAGE_MAP(CMfcDoc, CDocument)
- //{{AFX_MSG_MAP(CMfcDoc)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CMfcDoc construction/destruction
-
- CMfcDoc::CMfcDoc()
- {
- m_ddb = NULL;
- m_pal = NULL;
- m_sizeDoc = CSize(1,1); // dummy value to make CScrollView happy
- }
-
- CMfcDoc::~CMfcDoc()
- {
- if (m_ddb != NULL)
- {
- delete m_ddb;
- }
- if (m_pal != NULL)
- {
- delete m_pal;
- }
- }
-
- void CMfcDoc::InitBitmapData()
- {
- BITMAP bm;
- if (m_pal != NULL)
- {
- delete m_pal;
- m_pal = NULL;
- }
- if (m_ddb != NULL)
- {
- delete m_ddb;
- return;
- }
- // Create copy of palette
- m_pal = new CPalette;
- if (m_pal == NULL)
- {
- // we must be really low on memory
- m_ddb = NULL;
- return;
- }
- m_pal->Attach(hpal);
- m_ddb = new CBitmap;
- if (m_ddb == NULL)
- {
- // we must be really low on memory
- delete m_pal;
- m_pal = NULL;
- return;
- }
- m_ddb->Attach(hddb);
- // Set up document size
- GetObject(m_ddb->m_hObject, sizeof(BITMAP), (LPSTR)&bm);
-
- m_sizeDoc = CSize(bm.bmWidth, bm.bmHeight);
- }
-
- int CMfcDoc::DecodeFile(const char* filename)
- {
- int res = 0, colors, dither;
- char * ptr;
- int type;
- CDecDlg dlg;
- dlg.m_colors = 1;
- dlg.m_dither = 1;
- if (dlg.DoModal() == IDOK)
- {
- BeginWaitCursor();
- if (dlg.m_colors == 0)
- colors = 24;
- else if (dlg.m_colors == 1)
- colors = 8;
- else if (dlg.m_colors == 2)
- colors = 4;
- else if (dlg.m_colors == 3)
- colors = 0;
- else
- colors = 1;
- dither = dlg.m_dither;
- type = -1;
- ptr = strstr(filename, ".BMP");
- if (ptr != NULL)
- type = 1;
- ptr = strstr(filename, ".GIF");
- if (ptr != NULL)
- type = 2;
- ptr = strstr(filename, ".PCX");
- if (ptr != NULL)
- type = 3;
- ptr = strstr(filename, ".JPG");
- if (ptr != NULL)
- type = 4;
- ptr = strstr(filename, ".PNG");
- if (ptr != NULL)
- type = 5;
- ptr = strstr(filename, ".TIF");
- if (ptr != NULL)
- type = 6;
-
- switch(type)
- {
- case 1:
- res = readbmpfile(filename, colors, dither,(UINT *)&hddb,
- (UINT *)&hpal, NULL, 1);
- break;
- case 2:
- res = readgiffile(filename, colors, dither,(UINT *)&hddb,
- (UINT *)&hpal, NULL, 1);
- break;
- case 3:
- res = readpcxfile(filename, colors, dither,(UINT *)&hddb,
- (UINT *)&hpal, NULL, 1);
- break;
- case 4:
- res = readjpgfile(filename, colors, 1, dither,(UINT *)&hddb,
- (UINT *)&hpal, NULL, 1);
- break;
- case 5:
- res = readpngfile(filename, colors, dither,(UINT *)&hddb,
- (UINT *)&hpal, NULL, 1);
- break;
- case 6:
- res = readtiffile(filename, colors, dither,(UINT *)&hddb,
- (UINT *)&hpal, NULL, 1);
- break;
- default:
- res = 0;
- break;
- }
- EndWaitCursor();
- }
-
- return res;
- }
-
- BOOL CMfcDoc::OnOpenDocument(const char* pszPathName)
- {
- DeleteContents();
- // open here
- if (DecodeFile(pszPathName) != 1)
- {
- MessageBox(NULL, "Couldn't load Image", NULL,
- MB_ICONINFORMATION | MB_OK);
- return FALSE;
- }
-
- InitBitmapData();
-
- if (m_ddb == NULL)
- {
- // may not be valid format
- MessageBox(NULL, "Couldn't load Image", NULL,
- MB_ICONINFORMATION | MB_OK);
- return FALSE;
- }
- SetPathName(pszPathName);
- SetModifiedFlag(FALSE); // start off with unmodified
- return TRUE;
- }
-
-
- BOOL CMfcDoc::OnSaveDocument(const char* pszPathName)
- {
- BOOL bSuccess = FALSE;
- SetModifiedFlag(FALSE); // back to unmodified
- if (EncodeFile(pszPathName) == 1)
- bSuccess = TRUE;
- else
- bSuccess = FALSE;
- if (!bSuccess)
- {
- MessageBox(NULL, "Couldn't save Image", NULL,
- MB_ICONINFORMATION | MB_OK);
- }
-
- return bSuccess;
- }
-
-
- int CMfcDoc::EncodeFile(const char* filename)
- {
- int res = 0, colors, quality, smooth;
- char * ptr;
- int type;
- CEncDlg dlg;
- dlg.m_colors = 1;
- dlg.m_quality = 70;
- if (dlg.DoModal() == IDOK)
- {
- BeginWaitCursor();
- if (dlg.m_colors == 0)
- colors = 24;
- else if (dlg.m_colors == 1)
- colors = 8;
- else if (dlg.m_colors == 2)
- colors = 4;
- else if (dlg.m_colors == 3)
- colors = 0;
- else
- colors = 1;
- quality = dlg.m_quality;
- smooth = 0;
- type = -1;
- ptr = strstr(filename, ".BMP");
- if (ptr != NULL)
- type = 1;
- ptr = strstr(filename, ".GIF");
- if (ptr != NULL)
- type = 2;
- ptr = strstr(filename, ".PCX");
- if (ptr != NULL)
- type = 3;
- ptr = strstr(filename, ".JPG");
- if (ptr != NULL)
- type = 4;
- ptr = strstr(filename, ".PNG");
- if (ptr != NULL)
- type = 5;
- ptr = strstr(filename, ".TIF");
- if (ptr != NULL)
- type = 6;
-
- switch(type)
- {
- case 1:
- res = writebmpfile(filename, colors,(UINT)m_ddb->m_hObject,
- (UINT)m_pal->m_hObject, NULL, 1);
- break;
- case 2:
- res = writegiffile(filename, colors,(UINT)m_ddb->m_hObject,
- (UINT)m_pal->m_hObject, NULL, 1);
- break;
- case 3:
- res = writepcxfile(filename, colors,(UINT)m_ddb->m_hObject,
- (UINT)m_pal->m_hObject, NULL, 1);
- break;
- case 4:
- res = writejpgfile(filename, quality, smooth, colors, (UINT)m_ddb->m_hObject,
- (UINT)m_pal->m_hObject, NULL, 1);
- break;
- case 5:
- res = writepngfile(filename, colors, 1,(UINT)m_ddb->m_hObject,
- (UINT)m_pal->m_hObject, NULL, 1);
- break;
- case 6:
- res = writetiffile(filename, NO_COMPRESSION, 5, colors,(UINT)m_ddb->m_hObject,
- (UINT)m_pal->m_hObject, NULL, 1);
- default:
- res = 0;
- break;
- }
- EndWaitCursor();
- }
- return res;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMfcDoc diagnostics
-
- #ifdef _DEBUG
- void CMfcDoc::AssertValid() const
- {
- CDocument::AssertValid();
- }
-
- void CMfcDoc::Dump(CDumpContext& dc) const
- {
- CDocument::Dump(dc);
- }
-
- #endif //_DEBUG
-
- /////////////////////////////////////////////////////////////////////////////
- // CMfcDoc commands
-