|
现在相对摄像头保存下来的bmp图像文件做旋转处理,借鉴桌面版Windows的图像处理例程(编译运行通过),关键部分如下:
- int i,j,i0,j0,width1,height1;
- float rotateangle,fsin,fcos;
- float srcx1,srcx2,srcx3,srcx4,srcy1,srcy2,srcy3,srcy4;
- float dstx1,dstx2,dstx3,dstx4,dsty1,dsty2,dsty3,dsty4;
- //int tempvalue=Dialog1.m_value;
-
- i=j=i0=j0=0;
- //计算旋转弧度
- rotateangle=(float)((180)*PI/180); //rotateangle=(float)((tempvalue)*PI/180);
- fsin=(float)sin((double)rotateangle);
- fcos=(float)cos((double)rotateangle);
-
- BYTE * dibdata1;
- dibdata1 = DIBdata; //dibdata1=tempdib->GetDibData();
- RETAILMSG(1,(TEXT("OnRotate:Get DIBdata.\r\n")));
- //原图宽和高
- width1=width; //width1=tempdib->GetWidth();
- height1=height; //height1=tempdib->GetHeight();
-
- //位图文件信息头指针
- //LPBITMAPINFOHEADER m_bitmapinfoheader;
- //LPBITMAPINFO m_bitmapinfo;
- //m_bitmapinfoheader=tempdib->Getmapinfoheader();
- //m_bitmapinfo=tempdib->Getmapinfo();
- BYTE * buffer=new BYTE[3*width1*height1];
-
- for(i=0;i
- for(j=0;j
- {
- *(buffer+3*(i*width1+j))=*(dibdata1+3*(i*width1+j));
- *(buffer+3*(i*width1+j)+1)=*(dibdata1+3*(i*width1+j)+1);
- *(buffer+3*(i*width1+j)+2)=*(dibdata1+3*(i*width1+j)+2);
- }
-
- //平移一次后源图像四角坐标
- srcx1=(float)(-(width1-1)/2);
- srcy1=(float)((height1-1)/2);
- srcx2=(float)((width1-1)/2);
- srcy2=(float)((height1-1)/2);
- srcx3=(float)((width1-1)/2);
- srcy3=(float)(-(height1-1)/2);
- srcx4=(float)(-(width1-1)/2);
- srcy4=(float)(-(height1-1)/2);
- //计算旋转后四角坐标
- dstx1=srcx1*fcos+srcy1*fsin;
- dsty1=-srcx1*fsin+srcy1*fcos;
- dstx2=srcx2*fcos+srcy2*fsin;
- dsty2=-srcx2*fsin+srcy2*fcos;
- dstx3=srcx3*fcos+srcy3*fsin;
- dsty3=-srcx3*fsin+srcy3*fcos;
- dstx4=srcx4*fcos+srcy4*fsin;
- dsty4=-srcx4*fsin+srcy4*fcos;
- int lnewwidth,lnewheight;
- //矩形区域宽(为4的倍数)
- lnewwidth=(int)(max(fabs(dstx1-dstx3),fabs(dstx2-dstx4))+0.5);
-
- while(lnewwidth%4!=0)
- {
- lnewwidth++;
- }
-
- //矩形区域长
- lnewheight=(int)(max(fabs(dsty1-dsty3),fabs(dsty2-dsty4))+0.5);
- while(lnewheight%4!=0)
- {
- lnewheight++;
- }
- float a,b,c,d;
- a=(float)(width1-1)/2;
- b=(float)(height1-1)/2;
- c=(float)(lnewwidth-1)/2;
- d=(float)(lnewheight-1)/2;
- BYTE * buffermemory=new BYTE[3*lnewwidth*lnewheight];
- if(buffermemory == NULL) AfxMessageBox(_T("内存分配错误!"));
- for(i=0;i
- for(j=0;j
- {
- i0=(int)(-(float)(j) * fsin +(float)(i) * fcos + c*fsin - d*fcos + b +0.5);
- j0=(int)((float)(j) * fcos +(float)(i) * fsin - c*fcos - d*fsin + a +0.5);
- if((i0>=0)&&(i0<=height1-1)&&(j0>=0)&&(j0<=width1-1))
- {
- //(j0,i0)对应的点在未经平移和旋转的源像所在的矩形区域
- *(buffermemory+3*(i*lnewwidth+j))=*(buffer+3*(i0*width1+j0));
- *(buffermemory+3*(i*lnewwidth+j)+1)=*(buffer+3*(i0*width1+j0)+1);
- *(buffermemory+3*(i*lnewwidth+j)+2)=*(buffer+3*(i0*width1+j0)+2);
- }
- else
- {
- *(buffermemory+3*(i*lnewwidth+j))=(BYTE)255;
- *(buffermemory+3*(i*lnewwidth+j)+1)=(BYTE)255;
- *(buffermemory+3*(i*lnewwidth+j)+2)=(BYTE)255;
- }
- }//end for(i=0;i
-
- //改变源图像的一些属性值,如宽/高和图像大小(即整个数据区包含的字节数)
- bih.biWidth = lnewwidth; //m_bitmapinfoheader->biWidth=lnewwidth;
- bih.biHeight = lnewheight; //m_bitmapinfoheader->biHeight=lnewheight;
- bih.biSizeImage = lnewwidth*lnewheight*3; //m_bitmapinfoheader->biSizeImage=lnewwidth*lnewheight*3;
-
- //将源图像的数据区指针指向新的区域(即新的矩形区域)
- //tempdib->SetData(buffermemory);
- //Invalidate();
- delete[]buffer;
复制代码
以上代码移植到WinCE上,编译通过,但是实际运行时(要旋转的图像大小为320×240),在
for(i=0;i
for(j=0;j
循环运行到i=162,j=320时,出现Data Abort错误,软件崩溃!
求解!!!
|
|