天天看點

Windows Mobile上實作圖檔任意角度旋轉

Windows Mobile上實作圖檔任意角度旋轉

作者:金海建

目的:在Windows Mobile上,微軟的API和庫不支援圖檔的任意角度旋轉,隻支援90,180,270度旋轉。既然它不支援我們隻能自力更生了。

簡介:通過介紹和實作旋轉PNG圖檔,來說明實作圖檔旋轉的方法。過程大概如下,先用Imaging讀取并解碼png圖檔,使之轉成ARGB格式的位圖。然後利用頂點旋轉的公式,對位圖矩陣進行旋轉,旋轉完成後,利用Imaging庫,轉換成IImage接口。最後利用IImage接口來畫圖。

    先來看下平面直角坐标變換的旋轉坐标變換,其定義是

定義:若二坐标系{O;i,j}和{O′;i′,j′}滿足O≡O′,另∠(i,j′)=θ

        則坐标系{O′;i′,j′}可看成是由坐标系{O;i,j}繞O旋轉θ角得到的,稱由{O;i,j}到{O′;i′,j′}的變換為旋轉坐标變換。

旋轉公式為:

X' =  X cosθ -  Y sinθ

Y' =  X sinθ  + Y cosθ

由于我們是用數組的下表來表示坐标的,是以最小的坐标是為(0,0)。我們需要先做坐标旋轉,然後平移坐标。如下圖所示:

Windows Mobile上實作圖檔任意角度旋轉

平移的動作,是把是以的負坐标變成正坐标。用MinX和MinY來表示最小的X坐标和Y坐标。

X' =  X cosθ -  Y sinθ - MinX;

Y' =  X sinθ  + Y cosθ - MinY;

根據上面的公式我們推出

x = (x'+MinX)cosθ+ (y'+MinY)sinθ

y = (y'+MinY)conθ- (x'+MinX)sinθ

圖檔旋轉後,會出現失真現象,需要用雙線性内插值進行優化

雙線性内插值:對于一個目的像素,設定坐标通過反向變換得到的浮點坐标為(i+u,j+v),其中i、j均為非負整數,u、v為[0,1)區間的浮點數,則這個像素得值 f(i+u,j+v) 可由原圖像中坐标為 (i,j)、(i+1,j)、(i,j+1)、(i+1,j+1)所對應的周圍四個像素的值決定,即:

 f(i+u,j+v) = (1-u)(1-v)f(i,j) + (1-u)vf(i,j+1) + u(1-v)f(i+1,j) + uvf(i+1,j+1)

其中f(i,j)表示源圖像(i,j)處的的像素值,以此類推。

這就是雙線性内插值法。雙線性内插值法計算量大,但縮放後圖像品質高,不會出現像素值不連續的的情況。由于雙線性插值具有低通濾波器的性質,使高頻分量受損,是以可能會使圖像輪廓在一定程度上變得模糊。

旋轉代碼

  1. void RotateImageBuf(const BYTE* src, int srcWidth, int srcHeight, BYTE*& dst, int& dstWidth, int& dstHeight, double angle)     
  2. {    
  3.     double cosA = cos(angle);  
  4.     double sinA = sin(angle);  
  5.     double x1 = srcWidth * cosA;  
  6.     double y1 = srcWidth * sinA;  
  7.     double x2 = srcWidth * cosA - srcHeight * sinA;  
  8.     double y2 = srcWidth * sinA + srcHeight * cosA;  
  9.     double x3 = - srcHeight * sinA;  
  10.     double y3 = srcHeight * cosA;  
  11.     double minWidth = min(0,min(x3,min(x1,x2)));  
  12.     double minHeight = min(0,min(y3,min(y1,y2)));  
  13.     double maxWidth = max(0,max(x3,max(x1,x2)));  
  14.     double maxHeight = max(0,max(y3,max(y1,y2)));  
  15.     dstWidth = abs(maxWidth - minWidth) + 1;  
  16.     dstHeight = abs(maxHeight - minHeight) + 1;   
  17.     double ndstX = 0;  
  18.     double ndstY = 0;  
  19.     dst = new BYTE[dstWidth * dstHeight * 4];  
  20.     UINT32  *pDst = (UINT32 *)dst;  
  21.     UINT32  *pSrc = (UINT32 *)src;  
  22.     UINT32   crDefault = ((UINT32 *)src)[0];  
  23.     UINT32 color [2][2];  
  24.     BYTE alpha[2][2];  
  25.     BYTE rColor[2][2];  
  26.     BYTE gColor[2][2];  
  27.     BYTE bColor[2][2];  
  28.     BYTE afinal,rfinal,gfinal,bfinal;  
  29.     double ox,oy;  
  30.     for(int y=0; y < dstHeight; y++)  
  31.     {  
  32.         for(int x=0; x < dstWidth; x++)  
  33.         {  
  34.             ndstX = (x + minWidth) * cosA + (y + minHeight) * sinA;  
  35.             ndstY = (y + minHeight) * cosA - (x +minWidth) * sinA;  
  36.             if((ndstX >= 0) && (ndstX < srcWidth) && (ndstY >= 0) && (ndstY < srcHeight))  
  37.             {  
  38.                 color[0][0] = pSrc[ (int)((int)ndstY*srcWidth + ndstX) ];      
  39.                 if((int)ndstX + 1 >= srcWidth)  
  40.                     color[1][0] = crDefault;  
  41.                 else  
  42.                     color[1][0] = pSrc[ (int)((int)ndstY*srcWidth + ndstX +1) ];      
  43.                 if((int)ndstY + 1 >= srcHeight)  
  44.                     color[0][1] = crDefault;  
  45.                     color[0][1] = pSrc[ (int)(((int)ndstY+1)*srcWidth + ndstX) ];      
  46.                 if((int)ndstY + 1 >= srcHeight || (int)ndstX + 1 >= srcWidth)  
  47.                     color[1][1] = crDefault;  
  48.                     color[1][1] = pSrc[ (int)(((int)ndstY+1)*srcWidth + ndstX+1) ];     
  49.                 alpha[0][0]  = (color[0][0] & 0XFF000000) >> 24;  
  50.                 bColor[0][0] = (color[0][0] & 0XFF0000) >> 16;  
  51.                 gColor[0][0] = (color[0][0] & 0X00FF00) >> 8;  
  52.                 rColor[0][0] = color[0][0] & 0X0000FF;  
  53.                 alpha[1][0]  = (color[1][0] & 0XFF000000) >> 24;  
  54.                 bColor[1][0] = (color[1][0] & 0XFF0000) >> 16;  
  55.                 gColor[1][0] = (color[1][0] & 0X00FF00) >> 8;  
  56.                 rColor[1][0] = color[1][0] & 0X0000FF;  
  57.                 alpha[0][1]  = (color[0][1] & 0XFF000000) >> 24;  
  58.                 bColor[0][1] = (color[0][1] & 0XFF0000) >> 16;  
  59.                 gColor[0][1] = (color[0][1] & 0X00FF00) >> 8;  
  60.                 rColor[0][1] = color[0][1] & 0X0000FF;  
  61.                 alpha[1][1]  = (color[1][1] & 0XFF000000) >> 24;  
  62.                 bColor[1][1] = (color[1][1] & 0XFF0000) >> 16;  
  63.                 gColor[1][1] = (color[1][1] & 0X00FF00) >> 8;  
  64.                 rColor[1][1] = color[1][1] & 0X0000FF;  
  65.                 //f(i+u,j+v) = (1-u)(1-v)f(i,j) + (1-u)vf(i,j+1) + u(1-v)f(i+1,j) + uvf(i+1,j+1)  
  66.                 ox = (ndstX - (int)ndstX);     
  67.                 oy = (ndstY - (int)ndstY);  
  68.                 rfinal = (1 - ox)*(1 - oy)*rColor[0][0] + ox*(1 - oy)*rColor[1][0] + (1-ox)*oy*rColor[0][1] + ox*oy*rColor[1][1];     
  69.                 gfinal = (1 - ox)*(1 - oy)*gColor[0][0] + ox*(1 - oy)*gColor[1][0] + (1-ox)*oy*gColor[0][1] + ox*oy*gColor[1][1];   
  70.                 bfinal = (1 - ox)*(1 - oy)*bColor[0][0] + ox*(1 - oy)*bColor[1][0] + (1-ox)*oy*bColor[0][1] + ox*oy*bColor[1][1];   
  71.                 afinal = (1 - ox)*(1 - oy)*alpha[0][0] + ox*(1 - oy)*alpha[1][0] + (1-ox)*oy*alpha[0][1] + ox*oy*alpha[1][1];   
  72.                 pDst[y * (int)dstWidth + x] = RGBA(rfinal,gfinal,bfinal,afinal);  
  73.             }  
  74.             else  
  75.                 pDst[y * (int)dstWidth + x] = crDefault;  
  76.         }  
  77.     }  
  78. }  

需要對浮點型運算,改為整形預算。在模拟上測試過,旋轉一張圖檔,整形預算要比浮點型預算快20倍。

改為整形運算代碼。

  1. void RotateImageZhenxingBuf(const BYTE* src, int srcWidth, int srcHeight, BYTE*& dst, int& dstWidth, int& dstHeight, double angle)     
  2.     double minWidthD = min(0,min(x3,min(x1,x2)));  
  3.     double minHeightD = min(0,min(y3,min(y1,y2)));  
  4.     double maxWidthD = max(0,max(x3,max(x1,x2)));  
  5.     double maxHeightD = max(0,max(y3,max(y1,y2)));  
  6.     dstWidth = abs(minWidthD - maxWidthD) + 1;  
  7.     dstHeight = abs(maxHeightD - minHeightD) + 1;   
  8.     int minWidth= minWidthD;  
  9.     int minHeight=  minHeightD;  
  10.     int maxWidth =  maxWidthD;  
  11.     int maxHeight =  maxHeightD;  
  12.     int ndstX = 0;  
  13.     int ndstY = 0;  
  14.     int afinal,rfinal,gfinal,bfinal;  
  15.     long icosA = cosA * 256 * 256;  
  16.     long isinA = sinA * 256 * 256;  
  17.     int x;     
  18.     int y;     
  19.     int kx;     
  20.     int ky;  
  21.     for(int j=0; j < dstHeight; j++)  
  22.         for(int i=0; i < dstWidth; i++)  
  23.             ndstX = (i + minWidth) * icosA + (j + minHeight) * isinA;  
  24.             ndstY = (j + minHeight) * icosA - (i +minWidth) * isinA;  
  25.             ndstX = ndstX >> 8;  
  26.             ndstY = ndstY >> 8;  
  27.             if ( (ndstX >> 8) < srcWidth && (ndstX >> 8) >=0 && (ndstY >> 8) < srcHeight && (ndstY >> 8) >= 0)     
  28.             {    
  29.                 kx = ndstX >> 8;     
  30.                 ky = ndstY >> 8;     
  31.                 x = ndstX & 0xFF;     
  32.                 y = ndstY & 0xFF;     
  33.                 color[0][0] = pSrc[ ky*srcWidth + kx ];   
  34.                 if(kx + 1 >= srcWidth)  
  35.                     color[1][0] = pSrc[ ky*srcWidth + kx +1 ];  
  36.                 if(ky + 1 >= srcHeight)  
  37.                     color[0][1] = pSrc[ (ky+1)*srcWidth + kx ];   
  38.                 if(ky + 1 >= srcHeight || kx + 1 >= srcWidth)  
  39.                     color[1][1] = pSrc[ (ky+1)*srcWidth + kx+1 ];    
  40.                 rColor[0][0] = color[0][0] & 0XFF;  
  41.                 rColor[1][0] = color[1][0] & 0XFF;  
  42.                 rColor[0][1] = color[0][1] & 0XFF;  
  43.                 rColor[1][1] = color[1][1] & 0XFF;  
  44.                 afinal = (0x100 - x)*(0x100 - y)*alpha[0][0] + x*(0x100 - y)*alpha[1][0] + (0x100-x)*y*alpha[0][1] + x*y*alpha[1][1];     
  45.                 rfinal = (0x100 - x)*(0x100 - y)*rColor[0][0] + x*(0x100 - y)*rColor[1][0] + (0x100-x)*y*rColor[0][1] + x*y*rColor[1][1];     
  46.                 gfinal = (0x100 - x)*(0x100 - y)*gColor[0][0] + x*(0x100 - y)*gColor[1][0] + (0x100-x)*y*gColor[0][1] + x*y*gColor[1][1];     
  47.                 bfinal = (0x100 - x)*(0x100 - y)*bColor[0][0] + x*(0x100 - y)*bColor[1][0] + (0x100-x)*y*bColor[0][1] + x*y*bColor[1][1];     
  48.                 afinal = afinal >> 16;     
  49.                 if (afinal>255)     
  50.                     afinal = 255;     
  51.                 if (afinal<0)     
  52.                     afinal = 0;     
  53.                 rfinal = rfinal >> 16;     
  54.                 if (rfinal>255)     
  55.                     rfinal = 255;     
  56.                 if (rfinal<0)     
  57.                     rfinal = 0;    
  58.                 gfinal = gfinal >> 16;     
  59.                 if (gfinal>255)     
  60.                     gfinal = 255;     
  61.                 if (gfinal<0)     
  62.                     gfinal = 0;    
  63.                 bfinal = bfinal >> 16;     
  64.                 if (bfinal>255)     
  65.                     bfinal = 255;     
  66.                 if (bfinal<0)     
  67.                     bfinal = 0;    
  68.                 pDst[j * dstWidth + i] = RGBA(rfinal,gfinal,bfinal,afinal);  
  69.                 pDst[j * dstWidth + i] = crDefault;  

用Imaging接口加載png圖檔,并調用RotateImageBuf進行旋轉,最後傳回IImage接口。

  1. // need to delete pImagebuf;  
  2. HRESULT RotateImage(IImagingFactory *pImgFactory, IImage *pImage, IImage * &pImageOut, LPBYTE &pImageBuf, double angle)  
  3. {  
  4.     HRESULT hr = S_OK;  
  5.     IBitmapImage *pIBitmap = NULL;  
  6.     ImageInfo ImInfo;  
  7.     pImage->GetImageInfo(&ImInfo);  
  8.     if((ImInfo.PixelFormat & 0xFF00)>>8 != 32)  
  9.         hr = S_FALSE;  
  10.         goto Error;  
  11.     if(S_OK != pImgFactory->CreateBitmapFromImage(  
  12.         pImage, ImInfo.Width, ImInfo.Height, ImInfo.PixelFormat, InterpolationHintDefault, &pIBitmap))  
  13.     RECT rcLocked ={0,0,ImInfo.Width,ImInfo.Height};  
  14.     BitmapData bmpData;  
  15.     if(S_OK != pIBitmap->LockBits(&rcLocked, ImageLockModeRead | ImageLockModeWrite, ImInfo.PixelFormat, &bmpData))  
  16.     BYTE *pDst = NULL;  
  17.     int nDstWidth=0,nDstHeight=0;  
  18.     RotateImageZhenxingBuf((BYTE *)bmpData.Scan0, ImInfo.Width, ImInfo.Height, pDst, nDstWidth, nDstHeight, angle);  
  19.     pIBitmap->UnlockBits(&bmpData);  
  20.     BitmapData bmpDataNew;  
  21.     bmpDataNew.Height = nDstHeight;  
  22.     bmpDataNew.Width = nDstWidth;  
  23.     bmpDataNew.PixelFormat = ImInfo.PixelFormat;  
  24.     bmpDataNew.Stride = nDstWidth * 4;  
  25.     bmpDataNew.Reserved = 0;  
  26.     bmpDataNew.Scan0 = (void *)pDst;  
  27.     IBitmapImage *pBmpImageNew = NULL;  
  28.     hr = pImgFactory->CreateBitmapFromBuffer(&bmpDataNew, &pBmpImageNew);  
  29.     IImage * pImageNew = NULL;  
  30.     pBmpImageNew->QueryInterface(IID_IImage, (void **)&pImageNew);  
  31.     pImageOut = pImageNew;  
  32.     pImageBuf = pDst;  
  33. Error:  
  34.     if(pBmpImageNew)  
  35.         pBmpImageNew->Release();  
  36.     if(pIBitmap)  
  37.         pIBitmap->Release();  
  38.     return hr;