环境:xp sp3,vc2003(6.0没有CImage类)
#include <windows.h>
#include <stdio.h>
#include <atlimage.h>
#include <math.h>
#include <fstream>
struct IMAGEPARAMENT {
int nWidth;
int nHeight;
int nBitCount;
int nBytesPerLine;
int nBytesPerPixel;
int nNumColors;
int nSize;
};
int PaletteType(RGBQUAD *ColorTab)
{
int i,k,m,n,r,g,b;
m=n=0;
for (i=0; i<256; i++)
{
r = ColorTab[i].rgbRed;
g = ColorTab[i].rgbGreen;
b = ColorTab[i].rgbBlue;
if ((r != g)||(r != b)) m=0;
if ((i>0)&&(r>ColorTab[i-1].rgbRed)) m++;
if (r+g+b==0) n++;
}
k=3;
if (m == 255) k=2;
else if (256-n==1) k=0;
else if (256-n==15) k=1;
return(k);
}
int ImageType(CImage *pImgm) //判断图像类型
{
RGBQUAD ColorTab[256];
int k;
if (pImgm->IsNull()) return(0);
switch(pImgm->GetBPP())
{
case 1: k=0; break;
case 4: k=1; break;
case 8: k=3; break;
default: k=4; break;
}
if (k==3)
{
pImgm->GetColorTable(0,256,ColorTab);
k=PaletteType(ColorTab);
}
return(k);
}
void GetImageParament(CImage *pImg,struct IMAGEPARAMENT *ppImgParam)
{
if (pImg->IsNull()) return;
ppImgParam->nWidth = pImg->GetWidth(); //图像宽度
ppImgParam->nHeight = pImg->GetHeight(); //图像高度
ppImgParam->nBitCount = pImg->GetBPP(); //每像素位数
ppImgParam->nBytesPerLine = (pImg->GetWidth()*pImg->GetBPP()+31)/32*4; //每行字节数
ppImgParam->nBytesPerPixel = pImg->GetBPP()/8; //每像素字节数
if (pImg->GetBPP()<=8)
ppImgParam->nNumColors= 1 << pImg->GetBPP(); //调色板单元数
else
ppImgParam->nNumColors= 0;
ppImgParam->nSize = ppImgParam->nBytesPerLine*ppImgParam->nHeight; //像素总字节数
}
void GetAllPalette(CImage *pImg,RGBQUAD *ColorTab)
{
struct IMAGEPARAMENT P;
GetImageParament(pImg,&P);
pImg->GetColorTable(0, P.nNumColors, ColorTab);
}
void SetAllPalette(CImage *pImg, RGBQUAD *ColorTab)
{
struct IMAGEPARAMENT P;
GetImageParament(pImg,&P);
pImg->SetColorTable(0, P.nNumColors, ColorTab);
}
int InImage(CImage *pImg,int x,int y)
{
struct IMAGEPARAMENT P;
GetImageParament(pImg,&P);
if ((x<0)||(y<0)||(x>=P.nWidth)||(y>=P.nHeight)) return 0;
else return 1;
}
void SetRectValue(CImage *pImg,int x,int y,int Dx,int Dy,BYTE *buf)
{
struct IMAGEPARAMENT P;
BYTE *lp;
int i,dw,dh,x1,y1,alpha,delta,Dxb,dwb;
GetImageParament(pImg,&P);
if (P.nBitCount<8) return;
x1=x;
y1=y;
alpha=delta=0;
if (x<0) {
alpha=-x; x1=0;
}
if (y<0) {
delta=-y; y1=0;
}
if (!InImage(pImg,x1,y1)) return;
dw=min(Dx,(int) P.nWidth-x1);
dh=min(Dy,(int) P.nHeight-y1);
dw -= alpha;
dh -= delta;
Dxb = Dx*P.nBytesPerPixel;
dwb = dw*P.nBytesPerPixel;
lp = (BYTE*) pImg->GetPixelAddress(x1,y1);
buf += (delta*Dx+alpha)*P.nBytesPerPixel;
for (i=0;i<dh;i++) {
memcpy(lp,buf,dwb);
buf += Dxb;
lp -= P.nBytesPerLine;
}
}
int main()
{
std::ofstream out("e:\\1.txt");
double alpha = 1.57079632679489661923;
CImage img;
CImage* Imgm = &img;
img.Load("e:\\1.png");
BYTE **list, *sc;
int i, j, ww, Xd, Yd, Dx, Dy,nSize;
double X1, Y1, X2, Y2, theta, xx, yy, rr;
int x1, y1, x2, y2, flag;
double p, q, a, b, c, d, t1, t2, t3;
RGBQUAD ColorTab[256];
if (ImageType(&img) == 2) flag = 1; //flag为标志位,当取值为1时,表示双线性内插法 ,此时图像类型为灰阶图像
else flag = 0; //0表示最近邻点法
IMAGEPARAMENT P;
GetImageParament(&img, &P);
Dx = P.nWidth;
Dy = P.nHeight;
nSize = 0;
if (Dx < Dy)
{
nSize = Dy;
}
else
{
nSize = Dx;
}
int nLineBytes = (nSize * P.nBitCount + 31) / 32 * 4;
//还有一点要修改,不然当图像高度远大于宽度时会崩溃
sc = (BYTE*) malloc(2 * nLineBytes); // * P.nBytesPerLine); //申请工作单元
//
list = (BYTE**) malloc(Dy * sizeof(BYTE*)); //对原位图建立二维数组
for (i = 0; i < Dy; i++)
list[i] = (BYTE*) Imgm ->GetPixelAddress(0, i);
double centerx, centery, sintheta, costheta;
centerx = Dx / 2.0; //计算位图中心位置
centery = Dy / 2.0;
rr = sqrt(centerx * centerx + centery *centery); //计算对角线长度
theta = atan((double) centery / (double) centerx);
X1 = fabs(rr * cos(alpha + theta));
Y1 = fabs(rr * sin(alpha + theta));
X2 = fabs(rr * cos(alpha - theta));
Y2 = fabs(rr * sin(alpha - theta));
if (X2 > X1) X1 = X2; //得外接矩形宽度
if (Y2 > Y1) Y1 = Y2; //外接矩形高度
ww = (int) (2*X1+0.1);
CImage mgn;
CImage* Imgn = &mgn;
Imgn ->Destroy();
Imgn ->Create(ww, (int) (2*Y1+0.1), P.nBitCount ); //建立结果位图
if (P.nBitCount == 8)
{
GetAllPalette(Imgm, ColorTab);
//修改一,设置目标调色板
SetAllPalette(Imgn, ColorTab); //复制调色板
}
sintheta = sin(alpha);
costheta = cos(alpha);
for(j=0,Yd = 0; j<P.nHeight+centery; j++,Yd++)
{
if (P.nBitCount == 8)
memset (sc, 0, ww); //256色位图像素行置背景值
else
memset(sc, 0, ww * P.nBytesPerPixel); //真彩色位图像素行置背景值
for (i = 0, Xd = 0; i < P.nWidth+centerx; i++, Xd += P.nBytesPerPixel)
{
xx = centerx + costheta * (centerx-X1+i - centerx) + sintheta * (centery-Y1+j - centery)+0.9;
yy = centery - sintheta * (centerx-X1+i - centerx) + costheta * (centery-Y1+j - centery)-0.9;
x1 = (int) xx;
y1 = (int) yy;
if (((x1 < 0)||(x1+1 > P.nWidth )||(y1 < 0)||(y1+1 > P.nHeight )) || yy<0)
continue;
if (flag == 0)
{
//修改二, sc[Xd]
char buf[30];
sprintf(buf, "%d,%d,%d,%d\n", Yd,Xd,y1, x1);
out<<buf;
memcpy(&sc[Xd], &list[y1][x1 * P.nBytesPerPixel], P.nBytesPerPixel); //从源位图复制像素数据
}
else
{ // flag等于1,双线性内插法
a = (double) list[y1][x1]; //从源位图取数据
b = (double) list[y1][x2];
c = (double) list[y2][x1];
d = (double) list[y2][x2];
t1 = (1 - p) * a + p * b; //双线性内插计算
t2 = (1 - p) * c + p * d;
t3 = (1 - q) * t1 + q * t2;
//修改三
sc[Xd] = (BYTE) t3;
}
}
SetRectValue(Imgn, 0, Yd, ww, 1, sc);
}
Imgn->Save("c:\\1.png");
free(list); //释放工作单元
free(sc);
out.close();
return 0;
}
参考:http://bbs.csdn.net/topics/330025467
备注:原先的算法旋转的图形有些bug,在原来的基础上改写了for(j...),for(i...)循环,测试的图片也是32位png,欢迎指教
测试图片: