码迷,mamicode.com
首页 > 其他好文 > 详细

图像旋转中原图16×16分块测试2,映射后插入图中,测试边界信息

时间:2018-07-25 20:11:22      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:using   upper   length   ons   分块   信息   计算   wim   cat   

技术分享图片

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

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


#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include <atlimage.h>
#include <locale.h>

// The one and only application object

CWinApp theApp;

using namespace std;
//双三次插值系数
double fs(double w)
{    
        double a=-0.5;
        double fs;
        if (abs(w)<=1)
            fs=(a+2)*pow(abs(w),3)-(a+3)*pow(abs(w),2)+1;
        else if (abs(w)>1&&abs(w)<=2)
            fs=a*pow(abs(w),3)-5*a*pow(abs(w),2)+8*a*abs(w)-4*a;
        else
            fs=0;
        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 = 0;

    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=0,dbZoom=0;
        double    dbRotateStart=0,dbRotateEnd=0,dbRotateStep;
        double    dbZoomStart=0,dbZoomEnd=0,dbZoomStep;
        BOOL    bBenchWork=FALSE;
        CString csSrcName,csTagName,csNameRoot;
        TCHAR    *ptChar;
        do{
            if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
            {
                // TODO: change error code to suit your needs
                _tprintf(_T("Fatal Error: MFC initialization failed\n"));
                nRetCode = 1;
                break;
            }
            // TODO: code your application‘s behavior here.
            if(argc!=6 && argc!=10 )
            {
                _tprintf(_T("使用方法:\n  RotateZoom -S 源图像文件 旋转角度 缩放倍数 输出文件\n或:\n  RotateZoom -B 源图像文件 旋转起始角度 步长 旋转终止角度 缩放起始倍数 步长 缩放终止倍数 输出文件\n"));
                nRetCode= -1;
                break;
            }
            if(_tcsnicmp(argv[1],_T("-B"),2)==0)
            {
                bBenchWork=TRUE;
            }
            else if(_tcsnicmp(argv[1],_T("-S"),2)==0)
            {
                bBenchWork=FALSE;
            }
            else
            {
                _tprintf(_T("使用方法:\n  RotateZoom -S 源图像文件 旋转角度 缩放倍数 输出文件\n或:\nRotateZoom -B 源图像文件 旋转起始角度 步长 旋转终止角度 缩放起始倍数 步长 缩放终止倍数 输出文件\n"));
                nRetCode= -1;
                break;
            }
            if(bBenchWork)
            {
                csSrcName=argv[2];
                _stscanf(argv[3],_T("%lf"),&dbRotateStart);
                _stscanf(argv[4],_T("%lf"),&dbRotateStep);
                _stscanf(argv[5],_T("%lf"),&dbRotateEnd);
                _stscanf(argv[6],_T("%lf"),&dbZoomStart);
                _stscanf(argv[7],_T("%lf"),&dbZoomStep);
                _stscanf(argv[8],_T("%lf"),&dbZoomEnd);
                dbZoom=dbZoomStart>dbZoomEnd?dbZoomStart:dbZoomEnd;
                csTagName=argv[9];
                if(!((dbRotateStart>=0 && dbRotateStart<360) && 
                     (dbRotateEnd>=0 && dbRotateEnd<360)     &&
                     (dbZoomStart>=0.26 && dbZoomStart<=16)  && 
                     (dbZoomStart>=0.25 && dbZoomStart<=16)  &&
                     (dbRotateStep>=0.01) && (dbZoomEnd>=0.01)))
                {
                    _tprintf(_T("旋转角度范围(0~360),缩放倍数范围(0.25~16),步长范围(>=0.01)。输入参数超出范围!\n"));
                    nRetCode= -2;
                    break;
                }    
            }
            else
            {
                csSrcName=argv[2];
                _stscanf(argv[3],_T("%lf"),&dbRotate);
                _stscanf(argv[4],_T("%lf"),&dbZoom);
                csTagName=argv[5];
                if(!((dbRotate>=0 && dbRotate<360) && (dbZoom>=0.25 && dbZoom<=16)))
                {
                    _tprintf(_T("旋转角度范围(0~360),缩放倍数范围(0.25~16)。输入参数超出范围!\n"));
                    nRetCode= -2;
                    break;
                }            
            }

            hResult=cImage.Load(csSrcName);
            if(hResult!=ERROR_SUCCESS)
            {
                _tprintf(_T("源图像文件名错误!\n"));
                nRetCode= -3;
                break;
            }
            iWidth=cImage.GetWidth();
            iHeight=cImage.GetHeight();
            pbSrc = (PBYTE)malloc(iWidth*iHeight);//存原图数据大小没问题
            if(dbZoom>1) pbTag = (PBYTE)malloc(ceil(iWidth*dbZoom)*ceil(iHeight*dbZoom));
            else         pbTag = (PBYTE)malloc(iWidth*iHeight);
            if(pbSrc==NULL || pbTag==NULL )
            {
                _tprintf(_T("内存申请错误!\n"));
                nRetCode= -4;
                break;
            }
            iPitch=cImage.GetPitch();
            iBytePerPixel=(cImage.GetBPP()+7)/8;
            if(iBytePerPixel==3)
            {
                for(y=0;y<iHeight;y++)
                {
                    pbImage=(PBYTE)(PBYTE(cImage.GetBits())+iPitch*y);//得到的是图像初始像素地址
                    for(x=0;x<iWidth;x++)
                    {
                        pbSrc[y*iWidth+x]=(pbImage[3*x]*0.15+pbImage[3*x+1]*0.55+pbImage[3*x+2]*0.3);//转换成灰度就是单个像素了,分配大小还是原来的?。
                    }
                }
            }
            cImage.Destroy();

            csTagName.Trim();
            csTagName.MakeUpper();        
            if(bBenchWork)
            {

                if(csTagName.Right(4)!=_T(".BMP") ) csNameRoot=csTagName;
                else csNameRoot=csTagName.Left (csTagName.GetLength()-4);
                j=0;
                for(dbZoom=dbZoomStart;dbZoom<=dbZoomEnd;dbZoom+=dbZoomStep)
                {
                    w=ceil(iWidth*dbZoom);
                    h=ceil(iHeight*dbZoom);
                    cImage.Create(w,-h,32);
                    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= -5;
                            break;
                        }

                        for(y=0;y<h;y++)
                        {
                            pdwImage=(PDWORD)(PBYTE(cImage.GetBits())+iPitch*y);
                            for(x=0;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= -5;
                            break;
                        }
                        i=csTagName.GetLength();
                        ptChar=csTagName.GetBuffer(i+1);
                        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= -5;
                    break;
                }
                iWidth=ceil(iWidth*dbZoom);
                iHeight=ceil(iHeight*dbZoom);
                cImage.Create(iWidth,-iHeight,32);
                iPitch=cImage.GetPitch();
                for(y=0;y<iHeight;y++)
                {
                    pdwImage=(PDWORD)(PBYTE(cImage.GetBits())+iPitch*y);
                    for(x=0;x<iWidth;x++)
                    {
                        pdwImage[x]=pbTag[y*iWidth+x]*0x10101;  
                    }
                }    
                if(csTagName.Right(4)!=_T(".BMP") ) csTagName.Append(_T(".BMP"));
                hResult=cImage.Save(csTagName,Gdiplus::ImageFormatBMP);
                if(hResult!=ERROR_SUCCESS)
                {
                    _tprintf(_T("图像结果保存错误!\n"));
                    nRetCode= -5;
                    break;
                }
                i=csTagName.GetLength();
                ptChar=csTagName.GetBuffer(i+1);
                ptChar[i]=_T(\0);
                _tprintf(_T("图像处理成功!%s\n"),ptChar);
                csTagName.ReleaseBuffer();
                nRetCode= ERROR_SUCCESS;
            }
            break;
        }while(0);
        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 = 1;
    }
    _tprintf(_T("按任意键退出!"));
    getchar();    
    return nRetCode;
}
HRESULT RotateZoom(PBYTE pbSrc,int iWidth,int iHeight,double dbRotate,double dbZoom,PBYTE pbTag)
{
    double phase[33]={0};//16相位,包含端点存在33个距离
    for (int i=0;i<33;i++)
    {
        double i2=1.0*i;
        phase[i]=fs(i2/16);
    }
    int size;
    if(dbZoom>1)
    {
        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;
    int xo=250,yo=250;
         for(int y=yo;y<yo+250;y++)
            {
                for(int x=xo;x<xo+250;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*1/dbZoom;
                    srcy=srcy*1/dbZoom;
                    xOr = floor(srcx);
                    yOr = floor(srcy);
                    u=srcx-xOr;
                    v=srcy-yOr;
                    int phasex=floor(16*u+0.5);//16相位
                    int phasey=floor(16*v+0.5);

                    double A1,B1,C1,D1,A2,B2,C2,D2;

                    A1=phase[16+phasex];
                    B1=phase[phasex];
                    C1=phase[16-phasex];
                    D1=phase[32-phasex];

                    A2=phase[16+phasey];
                    B2=phase[phasey];
                    C2=phase[16-phasey];
                    D2=phase[32-phasey];

                    int newWidth=ceil(dbZoom*iWidth);
                    int block_numx=ceil((xOr+1.0)/16);
                    int block_numy=ceil((yOr+1.0)/16);

                    double blockSrc[256]={0};
                    int newcount=0;
                    //int k=0,l=0;
                    //分块存储进数组blockSrc[]中
                    for(int k=0;k<16;k++)
                    {
                        for(int l=0;l<16;l++)
                        {
                            int oldcount=(16*block_numy-16+k)*iWidth+16*block_numx-16+l;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            

                            blockSrc[newcount]=pbSrc[oldcount];
                            //pbTag[newcount]=pbSrc[oldcount];
                            
                            newcount++;
                            //cout<<oldcount<<endl;
                        }
                        

                    }//分块存入完毕
                    if( !(xOr-1>=0 && xOr+2<=iWidth && yOr-1>=0 && yOr+2<=iHeight))
                    {
                        pbTag[y*newWidth+x]=0;//255
                    }
                    else
                    { 
                        //映射到16×16块内的初始点位置
                        xOr=xOr-(16*block_numx-16);
                        yOr=yOr-(16*block_numy-16);
                        //if(xOr>=1 && xOr<=14 && yOr>=1 && yOr<=14)
                        pbTag[y*newWidth+x]=blockSrc[yOr*16+xOr];
                        //else
                        //    pbTag[y*newWidth+x]=255;
                    }

                }
                
            }

    return ERROR_SUCCESS;
}

测试1:-S src3.bmp 22.5 2 tar

pbTag[y*newWidth+x]=blockSrc[yOr*16+xOr];

处理后图起始点(250,250),区域250×250区域;

直接赋值过去,由于图像是放大二倍,效果近似最近邻插值。

技术分享图片

 

测试2:加入4×4区域内边界判别

边界待定值赋白色

 

if(xOr>=1 && xOr<=14 && yOr>=1 && yOr<=14)
  pbTag[y*newWidth+x]=blockSrc[yOr*16+xOr];
else
  pbTag[y*newWidth+x]=255;   

技术分享图片

测试3:边界待定值赋黑色

if(xOr>=1 && xOr<=14 && yOr>=1 && yOr<=14)
  pbTag[y*newWidth+x]=blockSrc[yOr*16+xOr];
else
  pbTag[y*newWidth+x]=0;   

技术分享图片

测试4:扩展为全图进行处理

int xo=0,yo=0;
for(int y=yo;y<ceil(dbZoom*iHeight);y++)
  {
    for(int x=xo;x<ceil(dbZoom*iWidth);x++)
      {

技术分享图片

技术分享图片

 

技术分享图片

原因在于映射回的初始点(xOr、yOr)没有事先判断,

要先判断是否映射落回原图点上再进行后续计算,因为后续用到了xOr、yOr的值。

 技术分享图片

技术分享图片

代码如下:(还需要再处理下边界处、引入双三次插值 就OK了)

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

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


#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#include <atlimage.h>
#include <locale.h>

// The one and only application object

CWinApp theApp;

using namespace std;
//双三次插值系数
double fs(double w)
{    
        double a=-0.5;
        double fs;
        if (abs(w)<=1)
            fs=(a+2)*pow(abs(w),3)-(a+3)*pow(abs(w),2)+1;
        else if (abs(w)>1&&abs(w)<=2)
            fs=a*pow(abs(w),3)-5*a*pow(abs(w),2)+8*a*abs(w)-4*a;
        else
            fs=0;
        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 = 0;

    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=0,dbZoom=0;
        double    dbRotateStart=0,dbRotateEnd=0,dbRotateStep;
        double    dbZoomStart=0,dbZoomEnd=0,dbZoomStep;
        BOOL    bBenchWork=FALSE;
        CString csSrcName,csTagName,csNameRoot;
        TCHAR    *ptChar;
        do{
            if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
            {
                // TODO: change error code to suit your needs
                _tprintf(_T("Fatal Error: MFC initialization failed\n"));
                nRetCode = 1;
                break;
            }
            // TODO: code your application‘s behavior here.
            if(argc!=6 && argc!=10 )
            {
                _tprintf(_T("使用方法:\n  RotateZoom -S 源图像文件 旋转角度 缩放倍数 输出文件\n或:\n  RotateZoom -B 源图像文件 旋转起始角度 步长 旋转终止角度 缩放起始倍数 步长 缩放终止倍数 输出文件\n"));
                nRetCode= -1;
                break;
            }
            if(_tcsnicmp(argv[1],_T("-B"),2)==0)
            {
                bBenchWork=TRUE;
            }
            else if(_tcsnicmp(argv[1],_T("-S"),2)==0)
            {
                bBenchWork=FALSE;
            }
            else
            {
                _tprintf(_T("使用方法:\n  RotateZoom -S 源图像文件 旋转角度 缩放倍数 输出文件\n或:\nRotateZoom -B 源图像文件 旋转起始角度 步长 旋转终止角度 缩放起始倍数 步长 缩放终止倍数 输出文件\n"));
                nRetCode= -1;
                break;
            }
            if(bBenchWork)
            {
                csSrcName=argv[2];
                _stscanf(argv[3],_T("%lf"),&dbRotateStart);
                _stscanf(argv[4],_T("%lf"),&dbRotateStep);
                _stscanf(argv[5],_T("%lf"),&dbRotateEnd);
                _stscanf(argv[6],_T("%lf"),&dbZoomStart);
                _stscanf(argv[7],_T("%lf"),&dbZoomStep);
                _stscanf(argv[8],_T("%lf"),&dbZoomEnd);
                dbZoom=dbZoomStart>dbZoomEnd?dbZoomStart:dbZoomEnd;
                csTagName=argv[9];
                if(!((dbRotateStart>=0 && dbRotateStart<360) && 
                     (dbRotateEnd>=0 && dbRotateEnd<360)     &&
                     (dbZoomStart>=0.26 && dbZoomStart<=16)  && 
                     (dbZoomStart>=0.25 && dbZoomStart<=16)  &&
                     (dbRotateStep>=0.01) && (dbZoomEnd>=0.01)))
                {
                    _tprintf(_T("旋转角度范围(0~360),缩放倍数范围(0.25~16),步长范围(>=0.01)。输入参数超出范围!\n"));
                    nRetCode= -2;
                    break;
                }    
            }
            else
            {
                csSrcName=argv[2];
                _stscanf(argv[3],_T("%lf"),&dbRotate);
                _stscanf(argv[4],_T("%lf"),&dbZoom);
                csTagName=argv[5];
                if(!((dbRotate>=0 && dbRotate<360) && (dbZoom>=0.25 && dbZoom<=16)))
                {
                    _tprintf(_T("旋转角度范围(0~360),缩放倍数范围(0.25~16)。输入参数超出范围!\n"));
                    nRetCode= -2;
                    break;
                }            
            }

            hResult=cImage.Load(csSrcName);
            if(hResult!=ERROR_SUCCESS)
            {
                _tprintf(_T("源图像文件名错误!\n"));
                nRetCode= -3;
                break;
            }
            iWidth=cImage.GetWidth();
            iHeight=cImage.GetHeight();
            pbSrc = (PBYTE)malloc(iWidth*iHeight);//存原图数据大小没问题
            if(dbZoom>1) pbTag = (PBYTE)malloc(ceil(iWidth*dbZoom)*ceil(iHeight*dbZoom));
            else         pbTag = (PBYTE)malloc(iWidth*iHeight);
            if(pbSrc==NULL || pbTag==NULL )
            {
                _tprintf(_T("内存申请错误!\n"));
                nRetCode= -4;
                break;
            }
            iPitch=cImage.GetPitch();
            iBytePerPixel=(cImage.GetBPP()+7)/8;
            if(iBytePerPixel==3)
            {
                for(y=0;y<iHeight;y++)
                {
                    pbImage=(PBYTE)(PBYTE(cImage.GetBits())+iPitch*y);//得到的是图像初始像素地址
                    for(x=0;x<iWidth;x++)
                    {
                        pbSrc[y*iWidth+x]=(pbImage[3*x]*0.15+pbImage[3*x+1]*0.55+pbImage[3*x+2]*0.3);//转换成灰度就是单个像素了,分配大小还是原来的?。
                    }
                }
            }
            cImage.Destroy();

            csTagName.Trim();
            csTagName.MakeUpper();        
            if(bBenchWork)
            {

                if(csTagName.Right(4)!=_T(".BMP") ) csNameRoot=csTagName;
                else csNameRoot=csTagName.Left (csTagName.GetLength()-4);
                j=0;
                for(dbZoom=dbZoomStart;dbZoom<=dbZoomEnd;dbZoom+=dbZoomStep)
                {
                    w=ceil(iWidth*dbZoom);
                    h=ceil(iHeight*dbZoom);
                    cImage.Create(w,-h,32);
                    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= -5;
                            break;
                        }

                        for(y=0;y<h;y++)
                        {
                            pdwImage=(PDWORD)(PBYTE(cImage.GetBits())+iPitch*y);
                            for(x=0;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= -5;
                            break;
                        }
                        i=csTagName.GetLength();
                        ptChar=csTagName.GetBuffer(i+1);
                        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= -5;
                    break;
                }
                iWidth=ceil(iWidth*dbZoom);
                iHeight=ceil(iHeight*dbZoom);
                cImage.Create(iWidth,-iHeight,32);
                iPitch=cImage.GetPitch();
                for(y=0;y<iHeight;y++)
                {
                    pdwImage=(PDWORD)(PBYTE(cImage.GetBits())+iPitch*y);
                    for(x=0;x<iWidth;x++)
                    {
                        pdwImage[x]=pbTag[y*iWidth+x]*0x10101;  
                    }
                }    
                if(csTagName.Right(4)!=_T(".BMP") ) csTagName.Append(_T(".BMP"));
                hResult=cImage.Save(csTagName,Gdiplus::ImageFormatBMP);
                if(hResult!=ERROR_SUCCESS)
                {
                    _tprintf(_T("图像结果保存错误!\n"));
                    nRetCode= -5;
                    break;
                }
                i=csTagName.GetLength();
                ptChar=csTagName.GetBuffer(i+1);
                ptChar[i]=_T(\0);
                _tprintf(_T("图像处理成功!%s\n"),ptChar);
                csTagName.ReleaseBuffer();
                nRetCode= ERROR_SUCCESS;
            }
            break;
        }while(0);
        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 = 1;
    }
    _tprintf(_T("按任意键退出!"));
    getchar();    
    return nRetCode;
}
HRESULT RotateZoom(PBYTE pbSrc,int iWidth,int iHeight,double dbRotate,double dbZoom,PBYTE pbTag)
{
    double phase[33]={0};//16相位,包含端点存在33个距离
    for (int i=0;i<33;i++)
    {
        double i2=1.0*i;
        phase[i]=fs(i2/16);
    }
    int size;
    if(dbZoom>1)
    {
        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;
    int xo=0,yo=0;
         for(int y=yo;y<ceil(dbZoom*iHeight);y++)
            {
                for(int x=xo;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*1/dbZoom;
                    srcy=srcy*1/dbZoom;
                    xOr = floor(srcx);
                    yOr = floor(srcy);
                    if( !(xOr-1>=0 && xOr+2<=iWidth && yOr-1>=0 && yOr+2<=iHeight))
                    {
                        pbTag[y*newWidth+x]=0;//255
                    }
                    else
                    {
                        u=srcx-xOr;
                        v=srcy-yOr;
                        int phasex=floor(16*u+0.5);//16相位
                        int phasey=floor(16*v+0.5);

                        double A1,B1,C1,D1,A2,B2,C2,D2;
                        A1=phase[16+phasex];
                        B1=phase[phasex];
                        C1=phase[16-phasex];
                        D1=phase[32-phasex];
                        A2=phase[16+phasey];
                        B2=phase[phasey];
                        C2=phase[16-phasey];
                        D2=phase[32-phasey];

                        int newWidth=ceil(dbZoom*iWidth);
                        int block_numx=ceil((xOr+1.0)/16);
                        int block_numy=ceil((yOr+1.0)/16);

                        double blockSrc[256]={0};
                        int newcount=0;
                        //int k=0,l=0;
                        //分块存储进数组blockSrc[]中
                        for(int k=0;k<16;k++)
                        {
                            for(int l=0;l<16;l++)
                            {
                                int oldcount=(16*block_numy-16+k)*iWidth+16*block_numx-16+l;   
                                blockSrc[newcount]=pbSrc[oldcount];
                                //pbTag[newcount]=pbSrc[oldcount];
                                newcount++;
                                //cout<<oldcount<<endl;
                            }
                        }//分块存入完毕
                        //映射到16×16块内的初始点位置
                        xOr=xOr-(16*block_numx-16);
                        yOr=yOr-(16*block_numy-16);
                        if(xOr>=1 && xOr<=14 && yOr>=1 && yOr<=14)
                            pbTag[y*newWidth+x]=blockSrc[yOr*16+xOr];
                        else
                            pbTag[y*newWidth+x]=255;
                    }
                }             
            }
    return ERROR_SUCCESS;
}

 

图像旋转中原图16×16分块测试2,映射后插入图中,测试边界信息

标签:using   upper   length   ons   分块   信息   计算   wim   cat   

原文地址:https://www.cnblogs.com/wxl845235800/p/9367785.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!