合成图片

来源:互联网 发布:软件特性是哪些 编辑:程序博客网 时间:2024/04/28 17:26

//.h头文件
// CPBmpDlg.h : header file
//

#if !defined(AFX_CPBMPDLG_H__8925A03A_701B_4642_B2C9_9E729B0524D3__INCLUDED_)
#define AFX_CPBMPDLG_H__8925A03A_701B_4642_B2C9_9E729B0524D3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CCPBmpDlg dialog
#define WIDTHBYTES(i) ((i+31)/32*4) //把位图宽度转化为4的整数倍,4字节对齐。

class CCPBmpDlg : public CDialog
{
// Construction
public:
    CCPBmpDlg(CWnd* pParent = NULL);    // standard constructor

// Dialog Data
    //{{AFX_DATA(CCPBmpDlg)
    enum { IDD = IDD_CPBMP_DIALOG };
        // NOTE: the ClassWizard will add data members here
    //}}AFX_DATA

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CCPBmpDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
public:
    BOOL    GetBMP(const CString& fn,LPBITMAPFILEHEADER,LPBITMAPINFOHEADER,VOID** p,BITMAP& bmp,LPRGBQUAD);

protected:
    HICON m_hIcon;

    // Generated message map functions
    //{{AFX_MSG(CCPBmpDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    afx_msg void OnButton5();
    afx_msg void OnButton1();
    afx_msg void OnButton2();
    afx_msg void OnButton3();
    afx_msg void OnButton4();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CPBMPDLG_H__8925A03A_701B_4642_B2C9_9E729B0524D3__INCLUDED_)

//.cpp实现文件

// CPBmpDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CPBmp.h"
#include "CPBmpDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
    CAboutDlg();

// Dialog Data
    //{{AFX_DATA(CAboutDlg)
    enum { IDD = IDD_ABOUTBOX };
    //}}AFX_DATA

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CAboutDlg)
    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
    //}}AFX_VIRTUAL

// Implementation
protected:
    //{{AFX_MSG(CAboutDlg)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
    //{{AFX_DATA_INIT(CAboutDlg)
    //}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CAboutDlg)
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
    //{{AFX_MSG_MAP(CAboutDlg)
        // No message handlers
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCPBmpDlg dialog

CCPBmpDlg::CCPBmpDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CCPBmpDlg::IDD, pParent)
{
    //{{AFX_DATA_INIT(CCPBmpDlg)
        // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCPBmpDlg::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CCPBmpDlg)
        // NOTE: the ClassWizard will add DDX and DDV calls here
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCPBmpDlg, CDialog)
    //{{AFX_MSG_MAP(CCPBmpDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
    ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
    ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
    ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
    ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCPBmpDlg message handlers

BOOL CCPBmpDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // Add "About..." menu item to system menu.

    // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000);

    CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
        CString strAboutMenu;
        strAboutMenu.LoadString(IDS_ABOUTBOX);
        if (!strAboutMenu.IsEmpty())
        {
            pSysMenu->AppendMenu(MF_SEPARATOR);
            pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
        }
    }

    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
   
    // TODO: Add extra initialization here
   
    return TRUE; // return TRUE unless you set the focus to a control
}

void CCPBmpDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CCPBmpDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // device context for painting

        SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

        // Center icon in client rectangle
        int cxIcon = GetSystemMetrics(SM_CXICON);
        int cyIcon = GetSystemMetrics(SM_CYICON);
        CRect rect;
        GetClientRect(&rect);
        int x = (rect.Width() - cxIcon + 1) / 2;
        int y = (rect.Height() - cyIcon + 1) / 2;

        // Draw the icon
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

HCURSOR CCPBmpDlg::OnQueryDragIcon()
{
    return (HCURSOR) m_hIcon;
}

///////////////////////////////////////////
//////关闭对话框
///////////////////////////////////////////
void CCPBmpDlg::OnButton5()   
 {
    this->EndDialog(FALSE);   
}

///////////////////////////////////////////
//////打开第一幅图片
///////////////////////////////////////////
void CCPBmpDlg::OnButton1()
{
    CFileDialog file(true,"*.BMP","", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT
            ,"BMP Files (*.bmp)|*.bmp||", NULL );
    if ( IDOK == file.DoModal() )
    {
        SetDlgItemText(IDC_EDIT1,file.GetPathName() );
    }
   
}

///////////////////////////////////////////
//////打开第二幅图片
///////////////////////////////////////////
void CCPBmpDlg::OnButton2()
{
    CFileDialog file(true,"*.BMP","", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT
            ,"BMP Files (*.bmp)|*.bmp||", NULL );
    if ( IDOK == file.DoModal() )
    {
        SetDlgItemText(IDC_EDIT2,file.GetPathName() );
    }   
}

///////////////////////////////////////////
//////保存的图片名称
///////////////////////////////////////////
void CCPBmpDlg::OnButton3()
{
    CFileDialog file(FALSE,"*.BMP","", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT
            ,"BMP Files (*.bmp)|*.bmp||", NULL );
    if ( IDOK == file.DoModal() )
    {
        SetDlgItemText(IDC_EDIT3,file.GetPathName() );
    }   
}

///////////////////////////////////////////
////////做合成处理
///////////////////////////////////////////
void CCPBmpDlg::OnButton4()
{
    CString one,two,saven;
    GetDlgItemText(IDC_EDIT1,one);
    GetDlgItemText(IDC_EDIT2,two);
    GetDlgItemText(IDC_EDIT3,saven);
    if( one.IsEmpty() || two.IsEmpty() || saven.IsEmpty() ){
        MessageBox("NULL PATH");
        return;
    }

    BITMAP    obit,tbit,sbit;
    memset(&obit,0,sizeof(obit));
    memset(&tbit,0,sizeof(tbit));
    memset(&sbit,0,sizeof(sbit));
   
    BITMAPFILEHEADER    ofh,tfh,sfh;    //one two save
    BITMAPINFOHEADER    oih,tih,sih;
    VOID                *opd=NULL,*tpd=NULL,*spd=NULL;
    LPRGBQUAD            oPla = NULL, tPla = NULL, Pla = NULL;

        ZeroMemory(&ofh,sizeof(BITMAPFILEHEADER));
        ZeroMemory(&tfh,sizeof(BITMAPFILEHEADER));
        ZeroMemory(&sfh,sizeof(BITMAPFILEHEADER));
        ZeroMemory(&oih,sizeof(BITMAPINFOHEADER));
        ZeroMemory(&tih,sizeof(BITMAPINFOHEADER));
        ZeroMemory(&sih,sizeof(BITMAPINFOHEADER));
   
    if( GetBMP(one,&ofh,&oih,&opd,obit,oPla) )
    {
        if( GetBMP(two,&tfh,&tih,&tpd,tbit,tPla) )
        {
            if( oPla )
            {
                Pla = oPla ;
            }
            if( tPla )
            {
                Pla = tPla;
            }

           
            sfh.bfType = 'MB';
            DWORD    dwWidth = 0;
            int        nBit    = oih.biBitCount>tih.biBitCount?oih.biBitCount:tih.biBitCount;
            switch(nBit)
            {
            case 1:nBit = 2;break;
            case 4:nBit = 16;break;
            case 8:nBit = 256;break;
            case 24:nBit = 0;break;
            default:nBit = 0;break;
            }
            if( obit.bmWidthBytes > tbit.bmWidthBytes )
            {
                dwWidth = obit.bmWidthBytes ;
            }else if( obit.bmWidthBytes < tbit.bmWidthBytes )
            {
                dwWidth = tbit.bmWidthBytes;
            }else{
                dwWidth = tbit.bmWidthBytes;
            }

            LONG    lImgSize = (oih.biHeight+tih.biHeight) * dwWidth;

            sfh.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nBit*sizeof(RGBQUAD);
            sfh.bfSize = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nBit*sizeof(RGBQUAD)+lImgSize;
            sfh.bfReserved1 = 0;
            sfh.bfReserved2 = 0;

            sih.biBitCount = (nBit==0)?24:(nBit==1?2:(nBit==4?16:(nBit==8?256:0)));
            sih.biClrUsed = nBit;
            sih.biHeight   = tbit.bmHeight + obit.bmHeight;
            sih.biWidth       = (tbit.bmWidth>obit.bmWidth)?tbit.bmWidth:obit.bmWidth;
            sih.biSize       = sizeof(sih);
           
            sih.biSizeImage= lImgSize;
            sih.biClrImportant = 0;
            sih.biPlanes = 0;
            sih.biYPelsPerMeter = 0;
            sih.biXPelsPerMeter = 0;
           

            spd = malloc(lImgSize);
            memset(spd,255,lImgSize);
            if( !spd ){return;}
            if( 0!=obit.bmBitsPixel ){
                if( obit.bmWidthBytes > tbit.bmWidthBytes )
                {
                    memcpy(spd,opd,obit.bmWidthBytes * obit.bmHeight );
                    LPBYTE    lpTempPtr,lpPtr;
                    for(int y=0;y<tbit.bmHeight;y++)
                    {
                        lpTempPtr=(LPBYTE)spd+y*obit.bmWidthBytes+(obit.bmHeight)*obit.bmWidthBytes;
                        lpPtr=(LPBYTE)tpd+y*tbit.bmWidthBytes;
                       
                        memcpy(lpTempPtr,lpPtr,tbit.bmWidthBytes);
                    }
                }else{
                    LPBYTE    lpT = NULL,lpP = NULL;
                    for(int h=0; h<obit.bmHeight; h++)
                    {
                        lpT = (LPBYTE)spd+h*tbit.bmWidthBytes;
                        lpP = (LPBYTE)opd+h*obit.bmWidthBytes;
                        memcpy(lpT,lpP,obit.bmWidthBytes);
                    }
                    memcpy((LPBYTE)spd+obit.bmHeight*tbit.bmWidthBytes,tpd,tbit.bmWidthBytes*tbit.bmHeight);
                }
               
            }else{
                if( obit.bmWidthBytes > tbit.bmWidthBytes )
                {
                    memcpy(spd,opd,obit.bmWidthBytes * obit.bmHeight );
                    LPBYTE    lpTempPtr,lpPtr;
                    for(int y=0;y<tbit.bmHeight;y++)
                    {
                        lpTempPtr=(LPBYTE)spd+y*obit.bmWidthBytes+(obit.bmHeight)*obit.bmWidthBytes;
                        lpPtr=(LPBYTE)tpd+y*tbit.bmWidthBytes;
                       
                        for(int x=0;x<tbit.bmWidth;x++)
                        {
                            *(lpTempPtr++)    = (UCHAR)*(lpPtr++);
                            *(lpTempPtr++)    = (UCHAR)*(lpPtr++);
                            *(lpTempPtr++)    = (UCHAR)*(lpPtr++);
                        }   
                    }
                }else{
                    LPBYTE    lpT = NULL,lpP = NULL;
                    for(int h=0; h<obit.bmHeight; h++)
                    {
                        lpT = (LPBYTE)spd+h*tbit.bmWidthBytes;
                        lpP = (LPBYTE)opd+h*obit.bmWidthBytes;
                        for(int x=0;x<obit.bmWidth;x++)
                        {
                            *(lpT++) = (UCHAR)*(lpP++);
                            *(lpT++) = (UCHAR)*(lpP++);
                            *(lpT++) = (UCHAR)*(lpP++);
                        }
                    }
                    memcpy((LPBYTE)spd+obit.bmHeight*tbit.bmWidthBytes,tpd,tbit.bmWidthBytes*tbit.bmHeight);
                }
               
            }
            CFile    sf;
            if( sf.Open( saven,CFile::modeCreate|CFile::modeWrite ) )
            {
                sf.Write( &sfh,sizeof(sfh) );
                sf.Write( &sih,sizeof(sih) );
                if(Pla)
                {
                    sf.Write(Pla,nBit*sizeof(RGBQUAD));
                }
                sf.Write( spd,lImgSize );
                //sf.Write( opd,obit.bmWidthBytes*obit.bmHeight);
                //sf.Write( tpd,tbit.bmWidthBytes*tbit.bmHeight);
                sf.Flush();
                sf.Close();
            }
        }else{
            MessageBox( two,"open failed" );
        }
    }else{
        MessageBox( one,"open failed" );
    }

 

    if(opd)
        free(opd);opd = NULL;
    if(tpd)
        free(tpd);tpd = NULL;
    if(spd)
        free(spd);spd = NULL;

    if( oPla )
    {
        delete [] oPla;oPla = NULL;
    }
    if( tPla )
    {
        delete [] tPla ;tPla = NULL;
    }
    if( Pla ){
    //    delete [] Pla ;
    }
}

 

///////////////////////////////////////////
////
///////////////////////////////////////////
BOOL CCPBmpDlg::GetBMP(const CString &fn,LPBITMAPFILEHEADER fh,LPBITMAPINFOHEADER ih,VOID **p,BITMAP &bmp,LPRGBQUAD pPal)
{
    CFile    f;
    BOOL    bRel = TRUE;
    DWORD    dwClrNum = 0;
    DWORD    dwLineBs = 0;
    LONG    lImgSize = 0;
    if( f.Open( fn,CFile::modeRead ) )
    {
        f.SeekToBegin();
        bRel = f.Read((void*)fh,sizeof(BITMAPFILEHEADER));
        if( bRel )
        bRel = f.Read((void*)ih,sizeof(BITMAPINFOHEADER));
       
        if( bRel )
        {
            bmp.bmWidth    = ih->biWidth;
            bmp.bmType = 'MB';
            bmp.bmHeight = ih->biHeight;
            bmp.bmBitsPixel = ih->biBitCount;
            dwLineBs = WIDTHBYTES( ih->biBitCount*ih->biWidth );
            bmp.bmWidthBytes = dwLineBs ;
            lImgSize = dwLineBs * ih->biHeight;

            if( ih->biClrUsed != 0)
            {
                dwClrNum = ih->biClrUsed;
            }else{
                switch(ih->biBitCount)
                {
                case 1:
                    dwClrNum = 2;
                    break;
                case 4:
                    dwClrNum = 16;break;
                case 8:
                    dwClrNum = 256;break;
                case 24:
                    dwClrNum = 0;break;
                default:
                    return FALSE;//bRel = FALSE;
                    break;
                }
            }
        }else{    return FALSE;    }


        if( bRel )
        {
            if( fh->bfOffBits != sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+dwClrNum*sizeof(RGBQUAD) )
            {
//                return FALSE;    //bRel = FALSE;
            }
        }else{    return FALSE;    }
        *p = malloc( lImgSize );
        if( dwClrNum != 0 )
        {
            pPal = new RGBQUAD[dwClrNum];
            bRel = f.Read( pPal,dwClrNum*sizeof(RGBQUAD) );
        }
        if( bRel )
        {
            f.Read( *p ,lImgSize );
        }


        f.Close();
    }else{ MessageBox("O F");return FALSE; }

///    bmp.bmBits = dwClrNum;    //×¢Ò⣬Õâ¸ö²»ÄÜÓÃ
    return bRel;
}

原创粉丝点击