Image Processing Program

BMP ÆÄÀÏÀ» Àо È­¸é¿¡ Ç¥½ÃÇϱâ±îÁö

 

1. Dib.h/cpp ÆÄÀÏÀ» ´Ù¿î¹Þ¾Æ¼­ ÇÁ·ÎÁ§Æ®¿¡ Ãß°¡ÇÕ´Ï´Ù.

      ¿¹Á¦ ÇÁ·ÎÁ§Æ®´Â ¿©±â¼­..
2. µµÅ¥¸ÕÆ® Çì´õÆÄÀÏ Å¬·¡½º ¼±¾ð À§¿¡ ´ÙÀ½Ã³·³ ÀÎŬ·çµå ÇÕ´Ï´Ù.
    
        #include "Dib.h"

3. CDib Ŭ·¡½ºÀÇ ¸â¹ö º¯¼ö m_DIB¸¦ µµÅ¥¸ÕÆ®¿¡ Ãß°¡ÇÕ´Ï´Ù.  
        
        protected:
                CDib m_DIB;

4. ¾ÖÇø®ÄÉÀÌ¼Ç Å¬·¡½º¿¡ OnFileOpenÀÇ Çڵ鷯¸¦ ¸¸µì´Ï´Ù.

void CImgProApp::OnFileOpen()
{
        // TODO: Add your command handler code here
    char szFilter[] = "Image Files(*.BMP)|*.BMP|All Files(*.*)|*.*||";

    CFileDialog fileDlg(TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter);
    if(IDOK == fileDlg.DoModal()) {
                OpenDocumentFile(fileDlg.GetPathName());
    }               
}

5. ¸®¼Ò½ººä¿¡¼­ ½ºÆ®¸µ Å×À̺íÀ» ¿­¾î ´ÙÀ½°ú °°ÀÌ ¹®ÀÚ¿­À» Ãß°¡ÇÕ´Ï´Ù.

id : IDS_CANNOT_LOAD_DIB
caption : Cannot Load DIB

id : IDS_CANNOT_SAVE_DIB
caption : Cannot Save DIB

6. Ŭ·¡½º À§Àúµå¸¦ ¿­¾î µµÅ¥¸ÕÆ® Ŭ·¡½º¿¡ OnOpenDocument ¿Í OnSaveDocument¸¦ Ãß°¡ÇÕ´Ï´Ù.


BOOL CImgProDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
        if (!CDocument::OnOpenDocument(lpszPathName))
                return FALSE;
        
        // TODO: Add your specialized creation code here
        CFile file;
        CFileException fe;
        
        if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe)) {
                ReportSaveLoadException(lpszPathName, &fe, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
                return FALSE;
        }
        
        DeleteContents();
        BeginWaitCursor();
        
        // replace calls to Serialize with ReadDIBFile function
        TRY {
                m_DIB.Read(file);
        }
        CATCH (CFileException, eLoad) {

                file.Abort(); // will not throw an exception
                EndWaitCursor();
                ReportSaveLoadException(lpszPathName, eLoad, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
                return FALSE;
        }
        END_CATCH

        EndWaitCursor();

        if (!m_DIB.IsValid()) {
                // may not be DIB format
                CString strMsg;
                strMsg.LoadString(IDS_CANNOT_LOAD_DIB);
                MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
                return FALSE;
        }
        SetPathName(lpszPathName);
        SetModifiedFlag(FALSE);     // start off with unmodified
        // TODO ³¡...

        return TRUE;
}

BOOL CImgProDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
        // TODO: Add your specialized code here and/or call the base class
        CFile file;
        CFileException fe;

        if (!file.Open(lpszPathName, CFile::modeCreate | CFile::modeReadWrite | CFile::shareExclusive, &fe)) {
                ReportSaveLoadException(lpszPathName, &fe, TRUE, AFX_IDP_INVALID_FILENAME);
                return FALSE;
        }

        // replace calls to Serialize with SaveDIB function
        BOOL bSuccess = FALSE;
        TRY     {
                BeginWaitCursor();
                bSuccess = m_DIB.Save(file);
                file.Close();
        }
        CATCH (CException, eSave) {
                file.Abort(); // will not throw an exception
                EndWaitCursor();
                ReportSaveLoadException(lpszPathName, eSave, TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
                return FALSE;
        }
        END_CATCH

        EndWaitCursor();
        SetModifiedFlag(FALSE);     // back to unmodified

        if (!bSuccess) {
                // may be other-style DIB (load supported but not save)
                //  or other problem in Save
                CString strMsg;
                strMsg.LoadString(IDS_CANNOT_SAVE_DIB);
                MessageBox(NULL, strMsg, NULL, MB_ICONINFORMATION | MB_OK);
        }

        return TRUE;
        //return CDocument::OnSaveDocument(lpszPathName);
}

7. µµÅ¥¸ÕÆ® Ŭ·¡½º¿¡ ´ÙÀ½ ÇÔ¼öµé Ãß°¡(Çì´õÆÄÀÏ¿¡ ¹Ù·Î ¾¹´Ï´Ù.)
public ¼Ó¼ºÀÔ´Ï´Ù.

        BOOL IsValid() const { return m_DIB.IsValid(); }
        DWORD Width() const { return m_DIB.Width(); }
        DWORD Height() const { return m_DIB.Height(); }
        BOOL  PaintDIB(HDC hDC, LPRECT pDCRect, LPRECT pDIBRect) const { return m_DIB.Paint(hDC, pDCRect, pDIBRect); }

8. ºäÀÇ OnDraw¸¦ ´ÙÀ½°ú °°ÀÌ °íÃÄ ¾¹´Ï´Ù. ¿©±â±îÁö Çϰí ÄÄÆÄÀÏ Çϸé BMP Çü½ÄÀÇ ÆÄÀÏÀ» Àо È­¸é¿¡ Ãâ·Â¸¸ Çϴµ¥±îÁö ±¸ÇöµÈ °Ì´Ï´Ù.
ÄÚµå µµÁß  ÁÖ¼® ó¸®µÈ ºÎºÐÀº ÇÁ¸°ÅÍ Ã³¸® ºÎºÐÀÔ´Ï´Ù.  ¿©±â¼­´Â È­¸é¿¡¼­¸¸ Ãâ·ÂÇϴϱî ÇÁ¸°ÅÍ Ã³¸®ºÎºÐÀº ÁÖ¼®Ã³¸® ÇѰ̴ϴÙ.

void CImgProView::OnDraw(CDC* pDC)
{
        CImgProDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        // TODO: add draw code for native data here
        if (pDoc->IsValid()) {
                int cxDIB = (int)pDoc->Width();         // Size of DIB - x
                int cyDIB = (int)pDoc->Height();        // Size of DIB - y
                CRect rcDIB;
                rcDIB.top = rcDIB.left = 0;
                rcDIB.right = cxDIB;
                rcDIB.bottom = cyDIB;
                CRect rcDest;
/*              if (pDC->IsPrinting()) {  // printer DC
                        // get size of printer page (in pixels)
                        int cxPage = pDC->GetDeviceCaps(HORZRES);
                        int cyPage = pDC->GetDeviceCaps(VERTRES);
                        // get printer pixels per inch
                        int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
                        int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);

                        //
                        // Best Fit case -- create a rectangle which preserves
                        // the DIB's aspect ratio, and fills the page horizontally.
                        //
                        // The formula in the "->bottom" field below calculates the Y
                        // position of the printed bitmap, based on the size of the
                        // bitmap, the width of the page, and the relative size of
                        // a printed pixel (cyInch / cxInch).
                        //
                        rcDest.top = rcDest.left = 0;
                        rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
                                        / ((double)cxDIB * cxInch));
                        rcDest.right = cxPage;
                }

                else   // not printer DC
*/              {
                        rcDest = rcDIB;
                }
                pDoc->PaintDIB(pDC->m_hDC, &rcDest, &rcDIB);
        }
}