程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 圖像鏡像

圖像鏡像

編輯:C++入門知識

三個函數,垂直鏡像、水平鏡像、自定義鏡像。

 

[cpp]
void CDIGTLSView::OnMirrorVertical()    //垂直鏡像 
//程序編制:李立宗  [email protected] 
// 2012-8-6 
    { 
        if(myImage1.IsNull()) 
            OnOpenResourceFile(); 
        if(myImage2.IsNull()){ 
            //myImage2.Destroy(); 
            myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0); 
        } 
        COLORREF pixel;  
        int maxY = myImage1.GetHeight(); 
        int maxX=myImage1.GetWidth(); 
        int r,g,b,avg;  
        double c; 
        byte* pRealData; 
        byte* pRealData2; 
        pRealData=(byte*)myImage1.GetBits(); 
        pRealData2=(byte*)myImage2.GetBits(); 
        int pit=myImage1.GetPitch(); 
        int pit2=myImage2.GetPitch(); 
        //需要注意,pit和pit2的值並不一樣,所以如果使用一個值,會導致不同的結果出現 
        //CString str; 
        //str.Format(TEXT("%d"),pit2); 
        //MessageBox(str); 
        //str.Format(TEXT("%d"),pit2); 
        //MessageBox(str); 
        int bitCount=myImage1.GetBPP()/8; 
        int bitCount2=myImage2.GetBPP()/8; 
        //CString str; 
        //str.Format(TEXT("%d"),bitCount); 
        //MessageBox(str); 
        //str.Format(TEXT("%d"),bitCount2); 
        //MessageBox(str); 
        int newValue; 
        int tempR,tempG,tempB; 
        int key1,key2;  //用來控制方向 
        //srand((unsigned)time(NULL));  
        //key1=rand()%100; 
        //key2=rand()%100; 
        //if(key1>50) 
        //  key1=-1; 
        //else 
        //  key1=1; 
        //if(key2<50) 
        //  key2=1; 
        //else 
        //  key2=-1; 
        //int xTrans=key1*rand()%(int)(maxX/1.5)+1; 
        //int yTrans=key2*rand()%(int)(maxY/1.5)+1; 
        //說明:將生產的圖像作為24位圖處理。 
        for (int y=0; y<maxY; y++) { 
            for (int x=0; x<maxX; x++) { 
                if((maxY-1-y>=0)&&(maxY-1-y<=maxY)) 
                {   tempR=(int)(int)(*(pRealData+pit*(maxY-1-y)+x*bitCount)); 
                if(bitCount==1) 
                {tempG=tempR; 
                tempB=tempR;} 
                else 
                { 
 
                    tempG=(int)(int)(*(pRealData+pit*(maxY-1-y)+x*bitCount+1)); 
                    tempB=(int)(int)(*(pRealData+pit*(maxY-1-y)+x*bitCount+2)); 
                } 
                } 
                else 
                { 
                    tempR=255; 
                    tempG=0; 
                    tempB=0; 
                } 
 
                *(pRealData2+pit2*y+x*bitCount2)=tempR; 
                *(pRealData2+pit2*y+x*bitCount2+1)=tempG; 
                *(pRealData2+pit2*y+x*bitCount2+2)=tempB; 
            } 
 
        } 
        Invalidate(); 
    } 
 
 
    void CDIGTLSView::OnMirrorHorizental()    //水平鏡像 
    { 
        if(myImage1.IsNull()) 
            OnOpenResourceFile(); 
        if(myImage2.IsNull()){ 
            //myImage2.Destroy(); 
            myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0); 
        } 
        COLORREF pixel;  
        int maxY = myImage1.GetHeight(); 
        int maxX=myImage1.GetWidth(); 
        int r,g,b,avg;  
        double c; 
        byte* pRealData; 
        byte* pRealData2; 
        pRealData=(byte*)myImage1.GetBits(); 
        pRealData2=(byte*)myImage2.GetBits(); 
        int pit=myImage1.GetPitch(); 
        int pit2=myImage2.GetPitch(); 
        //需要注意,pit和pit2的值並不一樣,所以如果使用一個值,會導致不同的結果出現 
        //CString str; 
        //str.Format(TEXT("%d"),pit2); 
        //MessageBox(str); 
        //str.Format(TEXT("%d"),pit2); 
        //MessageBox(str); 
        int bitCount=myImage1.GetBPP()/8; 
        int bitCount2=myImage2.GetBPP()/8; 
        //CString str; 
        //str.Format(TEXT("%d"),bitCount); 
        //MessageBox(str); 
        //str.Format(TEXT("%d"),bitCount2); 
        //MessageBox(str); 
        int newValue; 
        int tempR,tempG,tempB; 
        int key1,key2;  //用來控制方向 
        //srand((unsigned)time(NULL));  
        //key1=rand()%100; 
        //key2=rand()%100; 
        //if(key1>50) 
        //  key1=-1; 
        //else 
        //  key1=1; 
        //if(key2<50) 
        //  key2=1; 
        //else 
        //  key2=-1; 
        //int xTrans=key1*rand()%(int)(maxX/1.5)+1; 
        //int yTrans=key2*rand()%(int)(maxY/1.5)+1; 
        //說明:將生產的圖像作為24位圖處理。 
        for (int y=0; y<maxY; y++) { 
            for (int x=0; x<maxX; x++) { 
                if((maxX-1-x>=0)&&(maxX-1-x<=maxX)) 
                {   tempR=(int)(int)(*(pRealData+pit*y+(maxX-1-x)*bitCount)); 
                if(bitCount==1) 
                {tempG=tempR; 
                tempB=tempR;} 
                else 
                { 
 
                    tempG=(int)(int)(*(pRealData+pit*y+(maxX-1-x)*bitCount+1)); 
                    tempB=(int)(int)(*(pRealData+pit*y+(maxX-1-x)*bitCount+2)); 
                } 
                } 
                else 
                { 
                    tempR=255; 
                    tempG=0; 
                    tempB=0; 
                } 
 
                *(pRealData2+pit2*y+x*bitCount2)=tempR; 
                *(pRealData2+pit2*y+x*bitCount2+1)=tempG; 
                *(pRealData2+pit2*y+x*bitCount2+2)=tempB; 
            } 
 
        } 
        Invalidate(); 
    } 
 
 
    void CDIGTLSView::OnMirrorCustome()    //自定義水平、垂直鏡像 
    { 
        if(myImage1.IsNull()) 
            OnOpenResourceFile(); 
        CMyMirror myDlg; 
        int myAngle; 
        if(myDlg.DoModal () == IDOK)     
        {    www.2cto.com
            myAngle=myDlg.myMirror; 
            //          CString str; 
            //str.Format(TEXT("%d"),myAngle); 
            //MessageBox(str); 
            if(myAngle==0) 
                OnMirrorHorizental(); 
            else 
 
                OnMirrorVertical(); 
        } 
        else 
        { 
            myImage2.Destroy(); 
            Invalidate(); 
        } 
    } 
作者:superdont

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved