20180622代码加入随意变换图像大小
批处理框架先不看:-B src3.bmp 10 1 30 2 0.1 3 tar.bmp
src2.bmp 37.5 2.1 tar
// 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);
}
int newWidth=ceil(dbZoom*iWidth);
//旋转中心为图像中心
double rx0=ceil(dbZoom*iWidth)*0.5;
double ry0=ceil(dbZoom*iHeight)*0.5;
double srcx,srcy,u,v;
int xOr,yOr;
dbRotate=dbRotate*3.1415926/180.0;
for (int y=;y
{
xOr=xOr+1;
yOr=yOr+1;
}
else if(u>0.5&&v<0.5)
{
xOr=xOr+1;
yOr=yOr;
}
else if(u<0.5&&v>0.5)
{
xOr=xOr;
yOr=yOr+1;
}*/
// if( !(xOr-1>=0 && xOr+2<=iWidth && yOr-1>=0 && yOr+2<=iHeight))
int newWidth=ceil(dbZoom*iWidth);
if( !(srcx>= && srcx<=iWidth && srcy>= && srcy<=iHeight))
{
pbTag[y*newWidth+x]=;//
}
else
{
double middle=
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(middle<=&&middle>=)
pbTag\[y\*newWidth+x\]=middle;
else if(middle>)
pbTag\[y\*newWidth+x\]=;
else
pbTag\[y\*newWidth+x\]=;
}
}
}
//memcpy(pbTag,pbSrc,size);
return ERROR\_SUCCESS;
}
在批处理框架中:
// 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 i,j,x,y,w,h;
PBYTE pbSrc=NULL,pbTag=NULL;
PBYTE pbImage=NULL;
PDWORD pdwImage=NULL;
double dbRotate=,dbZoom=;
double dbRotateStart=,dbRotateEnd=,dbRotateStep;
double dbZoomStart=,dbZoomEnd=,dbZoomStep;
BOOL bBenchWork=FALSE;
CString csSrcName,csTagName,csNameRoot;
TCHAR \*ptChar;
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!= && argc!= )
{
\_tprintf(\_T("使用方法:\\n RotateZoom -S 源图像文件 旋转角度 缩放倍数 输出文件\\n或:\\n RotateZoom -B 源图像文件 旋转起始角度 步长 旋转终止角度 缩放起始倍数 步长 缩放终止倍数 输出文件\\n"));
nRetCode= -;
break;
}
if(\_tcsnicmp(argv\[\],\_T("-B"),)==)
{
bBenchWork=TRUE;
}
else if(\_tcsnicmp(argv\[\],\_T("-S"),)==)
{
bBenchWork=FALSE;
}
else
{
\_tprintf(\_T("使用方法:\\n RotateZoom -S 源图像文件 旋转角度 缩放倍数 输出文件\\n或:\\nRotateZoom -B 源图像文件 旋转起始角度 步长 旋转终止角度 缩放起始倍数 步长 缩放终止倍数 输出文件\\n"));
nRetCode= -;
break;
}
if(bBenchWork)
{
csSrcName=argv\[\];
\_stscanf(argv\[\],\_T("%lf"),&dbRotateStart);
\_stscanf(argv\[\],\_T("%lf"),&dbRotateStep);
\_stscanf(argv\[\],\_T("%lf"),&dbRotateEnd);
\_stscanf(argv\[\],\_T("%lf"),&dbZoomStart);
\_stscanf(argv\[\],\_T("%lf"),&dbZoomStep);
\_stscanf(argv\[\],\_T("%lf"),&dbZoomEnd);
dbZoom=dbZoomStart>dbZoomEnd?dbZoomStart:dbZoomEnd;
csTagName=argv\[\];
if(!((dbRotateStart>= && dbRotateStart<) &&
(dbRotateEnd>= && dbRotateEnd<) &&
(dbZoomStart>=0.26 && dbZoomStart<=) &&
(dbZoomStart>=0.25 && dbZoomStart<=) &&
(dbRotateStep>=0.01) && (dbZoomEnd>=0.01)))
{
\_tprintf(\_T("旋转角度范围(0~360),缩放倍数范围(0.25~16),步长范围(>=0.01)。输入参数超出范围!\\n"));
nRetCode= -;
break;
}
}
else
{
csSrcName=argv\[\];
\_stscanf(argv\[\],\_T("%lf"),&dbRotate);
\_stscanf(argv\[\],\_T("%lf"),&dbZoom);
csTagName=argv\[\];
if(!((dbRotate>= && dbRotate<) && (dbZoom>=0.25 && dbZoom<=)))
{
\_tprintf(\_T("旋转角度范围(0~360),缩放倍数范围(0.25~16)。输入参数超出范围!\\n"));
nRetCode= -;
break;
}
}
hResult=cImage.Load(csSrcName);
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\[\*x\]\*0.15+pbImage\[\*x+\]\*0.55+pbImage\[\*x+\]\*0.3);//转换成灰度就是单个像素了,分配大小还是原来的?。
}
}
}
cImage.Destroy();
csTagName.Trim();
csTagName.MakeUpper();
if(bBenchWork)
{
if(csTagName.Right()!=\_T(".BMP") ) csNameRoot=csTagName;
else csNameRoot=csTagName.Left (csTagName.GetLength()-);
j=;
for(dbZoom=dbZoomStart;dbZoom<=dbZoomEnd;dbZoom+=dbZoomStep)
{
w=ceil(iWidth\*dbZoom);
h=ceil(iHeight\*dbZoom);
cImage.Create(w,-h,);
iPitch=cImage.GetPitch();
for(dbRotate=dbRotateStart;dbRotate<=dbRotateEnd;dbRotate+=dbRotateStep)
{
hResult=RotateZoom(pbSrc,iWidth,iHeight,dbRotate,dbZoom,pbTag);
if(hResult!=ERROR\_SUCCESS)
{
\_tprintf(\_T("图像处理错误!\\n"));
nRetCode= -;
break;
}
for(y=;y<h;y++)
{
pdwImage=(PDWORD)(PBYTE(cImage.GetBits())+iPitch\*y);
for(x=;x<w;x++)
{
pdwImage\[x\]=pbTag\[y\*w+x\]\*0x10101;
}
}
csTagName=csNameRoot;
csTagName.AppendFormat(\_T("\_Z%05.2f\_R%06.2f.BMP"),dbZoom,dbRotate);
hResult=cImage.Save(csTagName,Gdiplus::ImageFormatBMP);
if(hResult!=ERROR\_SUCCESS)
{
\_tprintf(\_T("图像结果保存错误!\\n"));
nRetCode= -;
break;
}
i=csTagName.GetLength();
ptChar=csTagName.GetBuffer(i+);
ptChar\[i\]=\_T('\\0');
\_tprintf(\_T("图像处理成功!%s\\n"),ptChar);
csTagName.ReleaseBuffer();
nRetCode= ERROR\_SUCCESS;
j++;
}
cImage.Destroy();
if(nRetCode!=ERROR\_SUCCESS) break;
}
if(nRetCode==ERROR\_SUCCESS) \_tprintf(\_T("批处理完成!共处理%d\\n"),j);
else \_tprintf(\_T("批处理出错!已处理%d\\n"),j);
}
else
{
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;
}
}
if(csTagName.Right()!=\_T(".BMP") ) csTagName.Append(\_T(".BMP"));
hResult=cImage.Save(csTagName,Gdiplus::ImageFormatBMP);
if(hResult!=ERROR\_SUCCESS)
{
\_tprintf(\_T("图像结果保存错误!\\n"));
nRetCode= -;
break;
}
i=csTagName.GetLength();
ptChar=csTagName.GetBuffer(i+);
ptChar\[i\]=\_T('\\0');
\_tprintf(\_T("图像处理成功!%s\\n"),ptChar);
csTagName.ReleaseBuffer();
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 = ;
}
\_tprintf(\_T("按任意键退出!"));
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);
}
int newWidth=ceil(dbZoom\*iWidth);
//旋转中心为图像中心
double rx0=ceil(dbZoom\*iWidth)\*0.5;
double ry0=ceil(dbZoom\*iHeight)\*0.5;
double srcx,srcy,u,v;
int xOr,yOr;
dbRotate=dbRotate\*3.1415926/180.0;
for (int y=;y<ceil(dbZoom\*iHeight);y++)
{
for (int x=;x<ceil(dbZoom\*iWidth);x++)
{
srcx=(double)((x-rx0)\*cos(dbRotate) - (y-ry0)\*sin(dbRotate) + rx0) ;
srcy=(double)((x-rx0)\*sin(dbRotate) + (y-ry0)\*cos(dbRotate) + ry0) ;
srcx=srcx\*/dbZoom;
srcy=srcy\*/dbZoom;
xOr = floor(srcx);
yOr = floor(srcy);
u=srcx-xOr;
v=srcy-yOr;
int newWidth=ceil(dbZoom\*iWidth);
if( !(xOr->= && xOr+<=iWidth && yOr->= && yOr+<=iHeight))
{
pbTag\[y\*newWidth+x\]=;//
}
else
{
double middle=
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(middle<=&&middle>=)
pbTag\[y\*newWidth+x\]=middle;
else if(middle>)
pbTag\[y\*newWidth+x\]=;
else
pbTag\[y\*newWidth+x\]=;
}
}
}
//memcpy(pbTag,pbSrc,size);
return ERROR\_SUCCESS;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章