RotateZoom.cpp——Inter
阅读原文时间:2023年07月12日阅读:3

// RotateZoom.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "RotateZoom.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include
#include

// The one and only application object

CWinApp theApp;

using namespace std;
//双三次插值系数
double fs(double w)
{
double a=-0.5;
double fs;
if (abs(w)<=) fs=(a+)*pow(abs(w),)-(a+)*pow(abs(w),)+; else if (abs(w)>&&abs(w)<=)
fs=a*pow(abs(w),)-*a*pow(abs(w),)+*a*abs(w)-*a;
else
fs=;
return fs;
}

HRESULT RotateZoom(PBYTE pbSrc,int iWidth,int iHeight,double dbRotate,double dbZoom,PBYTE pbTag);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = ;

setlocale(LC\_ALL,"chs");  
HMODULE hModule = ::GetModuleHandle(NULL);  
if (hModule != NULL)  
{  
    // initialize MFC and print and error on failure  
    CImage cImage;  
    HRESULT hResult;  
    int        iWidth,iHeight,iBytePerPixel,iPitch;  
    int        x,y;  
    PBYTE    pbSrc=NULL,pbTag=NULL;  
    PBYTE    pbImage=NULL;  
    PDWORD    pdwImage=NULL;  
    double    dbRotate=,dbZoom=;  
    do{  
        if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), ))  
        {  
            // TODO: change error code to suit your needs  
            \_tprintf(\_T("Fatal Error: MFC initialization failed\\n"));  
            nRetCode = ;  
            break;  
        }  
        // TODO: code your application's behavior here.  
        if(argc<)  
        {  
            \_tprintf(\_T("使用方法:RotateZoom 源图像文件 旋转角度 缩放倍数 输出文件\\n"));  
            nRetCode= -;  
            break;  
        }

        \_stscanf(argv\[\],\_T("%lf"),&dbRotate);  
        \_stscanf(argv\[\],\_T("%lf"),&dbZoom);  
        if(!((dbRotate> && dbRotate<) && (dbZoom>=0.25 && dbZoom<=)))  
        {  
            \_tprintf(\_T("“旋转角度” 或 “缩放倍数”参数错误!\\n"));  
            nRetCode= -;  
            break;  
        }

        hResult=cImage.Load(argv\[\]);  
        if(hResult!=ERROR\_SUCCESS)  
        {  
            \_tprintf(\_T("源图像文件名错误!\\n"));  
            nRetCode= -;  
            break;  
        }

        iWidth=cImage.GetWidth();  
        iHeight=cImage.GetHeight();  
        pbSrc = (PBYTE)malloc(iWidth\*iHeight);//存原图数据大小没问题  
        if(dbZoom>) pbTag = (PBYTE)malloc(ceil(iWidth\*dbZoom)\*ceil(iHeight\*dbZoom));  
        else         pbTag = (PBYTE)malloc(iWidth\*iHeight);  
        if(pbSrc==NULL || pbTag==NULL )  
        {  
            \_tprintf(\_T("内存申请错误!\\n"));  
            nRetCode= -;  
            break;  
        }  
        iPitch=cImage.GetPitch();  
        iBytePerPixel=(cImage.GetBPP()+)/;  
        if(iBytePerPixel==)  
        {  
            for(y=;y<iHeight;y++)  
            {  
                pbImage=(PBYTE)(PBYTE(cImage.GetBits())+iPitch\*y);//得到的是图像初始像素地址  
                for(x=;x<iWidth;x++)  
                {  
                    //pbSrc\[y\*iWidth+x\]=pbImage\[3\*x\];   //B  
                    //pbSrc\[y\*iWidth+x\]=pbImage\[3\*x+1\]; //G  
                    //pbSrc\[y\*iWidth+x\]=pbImage\[3\*x+2\]; //R

                    //pbSrc\[y\*iWidth+x\]=pbImage\[3\*x\];  
                    //pbSrc\[y\*iWidth+x+1\]=pbImage\[3\*x+1\];  
                    //pbSrc\[y\*iWidth+x+2\]=pbImage\[3\*x+2\];

                    pbSrc\[y\*iWidth+x\]=(pbImage\[\*x\]\*0.15+pbImage\[\*x+\]\*0.55+pbImage\[\*x+\]\*0.3);//转换成灰度就是单个像素了,分配大小还是原来的?。  
                }  
            }  
        }  
        cImage.Destroy();  
        hResult=RotateZoom(pbSrc,iWidth,iHeight,dbRotate,dbZoom,pbTag);  
        if(hResult!=ERROR\_SUCCESS)  
        {  
            \_tprintf(\_T("图像处理错误!\\n"));  
            nRetCode= -;  
            break;  
        }  
        iWidth=ceil(iWidth\*dbZoom);  
        iHeight=ceil(iHeight\*dbZoom);  
        cImage.Create(iWidth,-iHeight,);  
        iPitch=cImage.GetPitch();  
        for(y=;y<iHeight;y++)  
        {  
            pdwImage=(PDWORD)(PBYTE(cImage.GetBits())+iPitch\*y);  
            for(x=;x<iWidth;x++)  
            {  
                pdwImage\[x\]=pbTag\[y\*iWidth+x\]\*0x10101;  
            }  
        }  
        CString csTagName=argv\[\];  
        csTagName.Trim();  
        csTagName.MakeUpper();  
        if(csTagName.Right()!=\_T(".BMP") ) csTagName.Append(\_T(".BMP"));  
        hResult=cImage.Save(csTagName);  
        if(hResult!=ERROR\_SUCCESS)  
        {  
            \_tprintf(\_T("图像结果保存错误!\\n"));  
            nRetCode= -;  
            break;  
        }  
        \_tprintf(\_T("图像处理成功!\\n"));  
        nRetCode= ERROR\_SUCCESS;  
        break;  
    }while();  
    if(pbSrc) free(pbSrc);  
    if(pbTag) free(pbTag);  
}  
else  
{  
    // TODO: change error code to suit your needs  
    \_tprintf(\_T("Fatal Error: GetModuleHandle failed\\n"));  
    nRetCode = ;  
}  
getchar();  
return nRetCode;  

}
HRESULT RotateZoom(PBYTE pbSrc,int iWidth,int iHeight,double dbRotate,double dbZoom,PBYTE pbTag)
{
int size;
if(dbZoom>)
{
size=iWidth*iHeight;
}
else
{
size=ceil(iWidth*dbZoom)*ceil(iHeight*dbZoom);
}
//旋转中心为图像中心
double rx0=iWidth;
double ry0=iHeight;
double srcx,srcy,u,v;
int xOr,yOr;
dbRotate=dbRotate*3.1415926/180.0;
for (int y=;y=0 && xOr+2<=iWidth && yOr-1>=0 && yOr+2<=iHeight)) if( !(srcx>= && srcx<=iWidth && srcy>= && srcy<=iHeight))
pbTag[y**iWidth+x]=;//
else
{
pbTag[y**iWidth+x]=
pbSrc[(yOr-)*iWidth+(xOr-)]*fs(+u)*fs(+v)+
pbSrc[(yOr)*iWidth+(xOr-)]*fs(+u)*fs(v)+
pbSrc[(yOr+)*iWidth+(xOr-)]*fs(+u)*fs(-v)+
pbSrc[(yOr+)*iWidth+(xOr-)]*fs(+u)*fs(-v)+

                pbSrc\[(yOr-)\*iWidth+(xOr)\]\*fs(u)\*fs(+v)+  
                pbSrc\[(yOr)\*iWidth+(xOr)\]\*fs(u)\*fs(v)+  
                pbSrc\[(yOr+)\*iWidth+(xOr)\]\*fs(u)\*fs(-v)+  
                pbSrc\[(yOr+)\*iWidth+(xOr)\]\*fs(u)\*fs(-v)+

                pbSrc\[(yOr-)\*iWidth+(xOr+)\]\*fs(-u)\*fs(+v)+  
                pbSrc\[(yOr)\*iWidth+(xOr+)\]\*fs(-u)\*fs(v)+  
                pbSrc\[(yOr+)\*iWidth+(xOr+)\]\*fs(-u)\*fs(-v)+  
                pbSrc\[(yOr+)\*iWidth+(xOr+)\]\*fs(-u)\*fs(-v)+

                pbSrc\[(yOr-)\*iWidth+(xOr+)\]\*fs(-u)\*fs(+v)+  
                pbSrc\[(yOr)\*iWidth+(xOr+)\]\*fs(-u)\*fs(v)+  
                pbSrc\[(yOr+)\*iWidth+(xOr+)\]\*fs(-u)\*fs(-v)+  
                pbSrc\[(yOr+)\*iWidth+(xOr+)\]\*fs(-u)\*fs(-v);

            if(pbTag\[y\*\*iWidth+x\]>)  
                pbTag\[y\*\*iWidth+x\]=;  
        }  
    }  
}  
//memcpy(pbTag,pbSrc,size);  
return ERROR\_SUCCESS;  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章