CImage实现旋转

44 篇文章 0 订阅
8 篇文章 0 订阅
//   IProcessB.h 
 
//#pragma once 
 
#ifndef		_IPROCESSB_H
#define		_IPROCESSB_H
#include <atlimage.h>
 
struct IMAGEPARAMENT {        
	int		nWidth; 
	int		nHeight; 
	int		nBitCount; 
    int		nBytesPerLine; 
	int		nBytesPerPixel; 
	int		nNumColors; 
	int		nSize; 
}; 
 
void  GetImageParament(CImage *pImg,struct IMAGEPARAMENT *ppImgParam); 
int	  InImage(CImage *pImg,int x,int y); 
// 
//DWORD GetPixelValue(CImage *pImg,int x,int y); 
//void  SetPixelValue(CImage *pImg,int x,int y,DWORD c); 
//void  GetRectValue(CImage *pImg,int x,int y,int Dx,int Dy,BYTE *buf); 
void  SetRectValue(CImage *pImg,int x,int y,int Dx,int Dy,BYTE *buf); 
// 
//void  GetPalette(CImage *pImg,int n,int *pr,int *pg,int *pb); 
//void  SetPalette(CImage *pImg,int n,int r,int g,int b); 
void  GetAllPalette(CImage *pImg,RGBQUAD *ColorTab); 
void  SetAllPalette(CImage *pImg,RGBQUAD *ColorTab); 
// 
//void  RectFillValue(CImage *pImg,int x,int y,int Dx,int Dy,DWORD color); 
//void  DrawBox(CImage *pImg,int x,int y,int Dx,int Dy,int w,DWORD c); 
//void  SetDefaultPalette(CImage *pImg); 
// 
int   PaletteType(RGBQUAD	*ColorTab); 
int   ImageType(CImage *pImgm); 
//void  ImageCopy(CImage *pImgn,CImage *pImgm); 
// 
//BYTE** Create2DList(CImage *pImgm); 
//void  Release2DList(BYTE** list); 
// 
//COLORREF DwordToColorref(DWORD c); 

void RotateCimage(CImage *Imgm, CImage *Imgn, double alpha);
  
#endif  //!_IPROCESSB_H 


//   IProcessB.cpp

#include "stdafx.h"
#include "IProcessB.h"
#include <math.h>
//RGBQUAD  D_pal[256] = { 
//	{0x00,0x00,0x00,0x00},{0xa8,0x00,0x00,0x00},{0x00,0xa8,0x00,0x00},
//	{0xa8,0xa8,0x00,0x00},{0x00,0x00,0xa8,0x00},{0xa8,0x00,0xa8,0x00},
//	{0x00,0x54,0xa8,0x00},{0xa8,0xa8,0xa8,0x00},{0x54,0x54,0x54,0x00},
//	{0xfc,0x54,0x54,0x00},{0x54,0xfc,0x54,0x00},{0xfc,0xfc,0x54,0x00},
//	{0x54,0x54,0xfc,0x00},{0xfc,0x54,0xfc,0x00},{0x54,0xfc,0xfc,0x00},
//	{0xfc,0xfc,0xfc,0x00},{0x00,0x00,0x00,0x00},{0x14,0x14,0x14,0x00},
//	{0x20,0x20,0x20,0x00},{0x2c,0x2c,0x2c,0x00},{0x38,0x38,0x38,0x00},
//	{0x44,0x44,0x44,0x00},{0x50,0x50,0x50,0x00},{0x60,0x60,0x60,0x00},
//	{0x70,0x70,0x70,0x00},{0x80,0x80,0x80,0x00},{0x90,0x90,0x90,0x00},
//	{0xa0,0xa0,0xa0,0x00},{0xb4,0xb4,0xb4,0x00},{0xc8,0xc8,0xc8,0x00},
//	{0xe0,0xe0,0xe0,0x00},{0xfc,0xfc,0xfc,0x00},{0xfc,0x00,0x00,0x00},
//	{0xfc,0x00,0x40,0x00},{0xfc,0x00,0x7c,0x00},{0xfc,0x00,0xbc,0x00},
//	{0xfc,0x00,0xfc,0x00},{0xbc,0x00,0xfc,0x00},{0x7c,0x00,0xfc,0x00},
//	{0x40,0x00,0xfc,0x00},{0x00,0x00,0xfc,0x00},{0x00,0x40,0xfc,0x00},
//	{0x00,0x7c,0xfc,0x00},{0x00,0xbc,0xfc,0x00},{0x00,0xfc,0xfc,0x00},
//	{0x00,0xfc,0xbc,0x00},{0x00,0xfc,0x7c,0x00},{0x00,0xfc,0x40,0x00},
//	{0x00,0xfc,0x00,0x00},{0x40,0xfc,0x00,0x00},{0x7c,0xfc,0x00,0x00},
//	{0xbc,0xfc,0x00,0x00},{0xfc,0xfc,0x00,0x00},{0xfc,0xbc,0x00,0x00},
//	{0xfc,0x7c,0x00,0x00},{0xfc,0x40,0x00,0x00},{0xfc,0x7c,0x7c,0x00},
//	{0xfc,0x7c,0x9c,0x00},{0xfc,0x7c,0xbc,0x00},{0xfc,0x7c,0xdc,0x00},
//	{0xfc,0x7c,0xfc,0x00},{0xdc,0x7c,0xfc,0x00},{0xbc,0x7c,0xfc,0x00},
//	{0x9c,0x7c,0xfc,0x00},{0x7c,0x7c,0xfc,0x00},{0x7c,0x9c,0xfc,0x00},
//	{0x7c,0xbc,0xfc,0x00},{0x7c,0xdc,0xfc,0x00},{0x7c,0xfc,0xfc,0x00},
//	{0x7c,0xfc,0xdc,0x00},{0x7c,0xfc,0xbc,0x00},{0x7c,0xfc,0x9c,0x00},
//	{0x7c,0xfc,0x7c,0x00},{0x9c,0xfc,0x7c,0x00},{0xbc,0xfc,0x7c,0x00},
//	{0xdc,0xfc,0x7c,0x00},{0xfc,0xfc,0x7c,0x00},{0xfc,0xdc,0x7c,0x00},
//	{0xfc,0xbc,0x7c,0x00},{0xfc,0x9c,0x7c,0x00},{0xfc,0xb4,0xb4,0x00},
//	{0xfc,0xb4,0xc4,0x00},{0xfc,0xb4,0xd8,0x00},{0xfc,0xb4,0xe8,0x00},
//	{0xfc,0xb4,0xfc,0x00},{0xe8,0xb4,0xfc,0x00},{0xd8,0xb4,0xfc,0x00},
//	{0xc4,0xb4,0xfc,0x00},{0xb4,0xb4,0xfc,0x00},{0xb4,0xc4,0xfc,0x00},
//	{0xb4,0xd8,0xfc,0x00},{0xb4,0xe8,0xfc,0x00},{0xb4,0xfc,0xfc,0x00},
//	{0xb4,0xfc,0xe8,0x00},{0xb4,0xfc,0xd8,0x00},{0xb4,0xfc,0xc4,0x00},
//	{0xb4,0xfc,0xb4,0x00},{0xc4,0xfc,0xb4,0x00},{0xd8,0xfc,0xb4,0x00},
//	{0xe8,0xfc,0xb4,0x00},{0xfc,0xfc,0xb4,0x00},{0xfc,0xe8,0xb4,0x00},
//	{0xfc,0xd8,0xb4,0x00},{0xfc,0xc4,0xb4,0x00},{0x70,0x00,0x00,0x00},
//	{0x70,0x00,0x1c,0x00},{0x70,0x00,0x38,0x00},{0x70,0x00,0x54,0x00},
//	{0x70,0x00,0x70,0x00},{0x54,0x00,0x70,0x00},{0x38,0x00,0x70,0x00},
//	{0x1c,0x00,0x70,0x00},{0x00,0x00,0x70,0x00},{0x00,0x1c,0x70,0x00},
//	{0x00,0x38,0x70,0x00},{0x00,0x54,0x70,0x00},{0x00,0x70,0x70,0x00},
//	{0x00,0x70,0x54,0x00},{0x00,0x70,0x38,0x00},{0x00,0x70,0x1c,0x00},
//	{0x00,0x70,0x00,0x00},{0x1c,0x70,0x00,0x00},{0x38,0x70,0x00,0x00},
//	{0x54,0x70,0x00,0x00},{0x70,0x70,0x00,0x00},{0x70,0x54,0x00,0x00},
//	{0x70,0x38,0x00,0x00},{0x70,0x1c,0x00,0x00},{0x70,0x38,0x38,0x00},
//	{0x70,0x38,0x44,0x00},{0x70,0x38,0x54,0x00},{0x70,0x38,0x60,0x00},
//	{0x70,0x38,0x70,0x00},{0x60,0x38,0x70,0x00},{0x54,0x38,0x70,0x00},
//	{0x44,0x38,0x70,0x00},{0x38,0x38,0x70,0x00},{0x38,0x44,0x70,0x00},
//	{0x38,0x54,0x70,0x00},{0x38,0x60,0x70,0x00},{0x38,0x70,0x70,0x00},
//	{0x38,0x70,0x60,0x00},{0x38,0x70,0x54,0x00},{0x38,0x70,0x44,0x00},
//	{0x38,0x70,0x38,0x00},{0x44,0x70,0x38,0x00},{0x54,0x70,0x38,0x00},
//	{0x60,0x70,0x38,0x00},{0x70,0x70,0x38,0x00},{0x70,0x60,0x38,0x00},
//	{0x70,0x54,0x38,0x00},{0x70,0x44,0x38,0x00},{0x70,0x50,0x50,0x00},
//	{0x70,0x50,0x58,0x00},{0x70,0x50,0x60,0x00},{0x70,0x50,0x68,0x00},
//	{0x70,0x50,0x70,0x00},{0x68,0x50,0x70,0x00},{0x60,0x50,0x70,0x00},
//	{0x58,0x50,0x70,0x00},{0x50,0x50,0x70,0x00},{0x50,0x58,0x70,0x00},
//	{0x50,0x60,0x70,0x00},{0x50,0x68,0x70,0x00},{0x50,0x70,0x70,0x00},
//	{0x50,0x70,0x68,0x00},{0x50,0x70,0x60,0x00},{0x50,0x70,0x58,0x00},
//	{0x50,0x70,0x50,0x00},{0x58,0x70,0x50,0x00},{0x60,0x70,0x50,0x00},
//	{0x68,0x70,0x50,0x00},{0x70,0x70,0x50,0x00},{0x70,0x68,0x50,0x00},
//	{0x70,0x60,0x50,0x00},{0x70,0x58,0x50,0x00},{0x40,0x00,0x00,0x00},
//	{0x40,0x00,0x10,0x00},{0x40,0x00,0x20,0x00},{0x40,0x00,0x30,0x00},
//	{0x40,0x00,0x40,0x00},{0x30,0x00,0x40,0x00},{0x20,0x00,0x40,0x00},
//	{0x10,0x00,0x40,0x00},{0x00,0x00,0x40,0x00},{0x00,0x10,0x40,0x00},
//	{0x00,0x20,0x40,0x00},{0x00,0x30,0x40,0x00},{0x00,0x40,0x40,0x00},
//	{0x00,0x40,0x30,0x00},{0x00,0x40,0x20,0x00},{0x00,0x40,0x10,0x00},
//	{0x00,0x40,0x00,0x00},{0x10,0x40,0x00,0x00},{0x20,0x40,0x00,0x00},
//	{0x30,0x40,0x00,0x00},{0x40,0x40,0x00,0x00},{0x40,0x30,0x00,0x00},
//	{0x40,0x20,0x00,0x00},{0x40,0x10,0x00,0x00},{0x40,0x20,0x20,0x00},
//	{0x40,0x20,0x28,0x00},{0x40,0x20,0x30,0x00},{0x40,0x20,0x38,0x00},
//	{0x40,0x20,0x40,0x00},{0x38,0x20,0x40,0x00},{0x30,0x20,0x40,0x00},
//	{0x28,0x20,0x40,0x00},{0x20,0x20,0x40,0x00},{0x20,0x28,0x40,0x00},
//	{0x20,0x30,0x40,0x00},{0x20,0x38,0x40,0x00},{0x20,0x40,0x40,0x00},
//	{0x20,0x40,0x38,0x00},{0x20,0x40,0x30,0x00},{0x20,0x40,0x28,0x00},
//	{0x20,0x40,0x20,0x00},{0x28,0x40,0x20,0x00},{0x30,0x40,0x20,0x00},
//	{0x38,0x40,0x20,0x00},{0x40,0x40,0x20,0x00},{0x40,0x38,0x20,0x00},
//	{0x40,0x30,0x20,0x00},{0x40,0x28,0x20,0x00},{0x40,0x2c,0x2c,0x00},
//	{0x40,0x2c,0x30,0x00},{0x40,0x2c,0x34,0x00},{0x40,0x2c,0x3c,0x00},
//	{0x40,0x2c,0x40,0x00},{0x3c,0x2c,0x40,0x00},{0x34,0x2c,0x40,0x00},
//	{0x30,0x2c,0x40,0x00},{0x2c,0x2c,0x40,0x00},{0x2c,0x30,0x40,0x00},
//	{0x2c,0x34,0x40,0x00},{0x2c,0x3c,0x40,0x00},{0x2c,0x40,0x40,0x00},
//	{0x2c,0x40,0x3c,0x00},{0x2c,0x40,0x34,0x00},{0x2c,0x40,0x30,0x00},
//	{0x2c,0x40,0x2c,0x00},{0x30,0x40,0x2c,0x00},{0x34,0x40,0x2c,0x00},
//	{0x3c,0x40,0x2c,0x00},{0x40,0x40,0x2c,0x00},{0x40,0x3c,0x2c,0x00},
//	{0x40,0x34,0x2c,0x00},{0x40,0x30,0x2c,0x00},{0x00,0x00,0x00,0x00},
//	{0x00,0x00,0x00,0x00},{0x00,0x00,0x00,0x00},{0x00,0x00,0x00,0x00},
//	{0x00,0x00,0x00,0x00},{0x00,0x00,0x00,0x00},{0x00,0x00,0x00,0x00},
//	{0x00,0x00,0x00,0x00}   
//};
//
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;		//像素总字节数
}

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;
}
//
//DWORD GetPixelValue(CImage *pImg,int x,int y)
//{
// 	struct	IMAGEPARAMENT  P;
//	BYTE	*lp;
// 	DWORD	dd=0;
//
//	GetImageParament(pImg,&P);
//	if ((P.nBitCount<8)||(!InImage(pImg,x,y))) return 0;
//	lp = (BYTE*) pImg->GetPixelAddress(x,y);
// 	memcpy(&dd,lp,P.nBytesPerPixel);
//	return(dd);
//}
//
//void SetPixelValue(CImage *pImg,int x,int y,DWORD c)
//{
// 	struct	IMAGEPARAMENT  P;
//	BYTE	*lp;
//  
// 	GetImageParament(pImg,&P);
//	if ((P.nBitCount<8)||(!InImage(pImg,x,y))) return;
//	lp = (BYTE*) pImg->GetPixelAddress(x,y);
//  	memcpy(lp,&c,P.nBytesPerPixel);
//}
//
//void GetRectValue(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(buf,lp,dwb);  
// 		buf += Dxb;	
//		lp -= P.nBytesPerLine;
//	}
//}
//
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;
	}
 }
//
//void GetPalette(CImage *pImg,int n,int *pr,int *pg,int *pb)
//{
//	RGBQUAD ColorTab;
//
//	pImg->GetColorTable(n, 1, &ColorTab);
//	*pb = ColorTab.rgbBlue; 
//	*pg = ColorTab.rgbGreen; 
//	*pr = ColorTab.rgbRed;
//}
//
//void SetPalette(CImage *pImg,int n,int r,int g,int b)
//{
//	RGBQUAD ColorTab;
//
//	ColorTab.rgbBlue  = b; 
//	ColorTab.rgbGreen = g; 
//	ColorTab.rgbRed   = r;
//	pImg->SetColorTable(n, 1, &ColorTab);
//}
//
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);
}
//
//void RectFillValue(CImage *pImg,int x,int y,int Dx,int Dy,DWORD color)
//{
// 	struct	IMAGEPARAMENT  P;
//	BYTE	*buf,*buf0;
//	int		i,j,dw,dh,x1,y1,alpha,delta,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;
// 	dwb = dw*P.nBytesPerPixel;
//
//	if (dw==1)
//	{
// 		for (i=0;i<dh;i++)
//		{
//			buf = (BYTE*) pImg->GetPixelAddress(x1,y1+i);
//			memcpy(buf,&color,P.nBytesPerPixel);
// 		}
// 	}
//	else {
//		buf0= (BYTE*) pImg->GetPixelAddress(x1,y1);
//		buf = buf0;
//		for (j=0;j<dw;j++)
//		{
//			memcpy(buf,&color,P.nBytesPerPixel);
//			buf += P.nBytesPerPixel;
//		}
// 		for (i=1;i<dh;i++)
//		{
//			buf = (BYTE*) pImg->GetPixelAddress(x1,y1+i);
//  			memcpy(buf,buf0,dwb);
//   		}
//	}
//}
//
//void DrawBox(CImage *pImg,int x,int y,int Dx,int Dy,int w,DWORD c)
//{
//	if (pImg->GetBPP()<=8) c = c &0xff;
//	RectFillValue(pImg,x,y,Dx,w,c);
//	RectFillValue(pImg,x,y+Dy-w,Dx,w,c);
//	RectFillValue(pImg,x,y,w,Dy-w,c);
//	RectFillValue(pImg,x+Dx-w,y,w,Dy,c);
//}
//
//void SetDefaultPalette(CImage *pImg)
//{
//	if (pImg->GetBPP()>8) return;
//	pImg->SetColorTable(0, 256, D_pal);
//}
//
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 ImageCopy(CImage *pImgn,CImage *pImgm)		//位图的复制 
//{
// 	struct	IMAGEPARAMENT P;
// 	RGBQUAD	ColorTab[256];
// 	CDC		*pOrigDC,*pDC;
//  
//	GetImageParament(pImgm,&P);			//获取原始位图参数
// 
//	if (!pImgn->IsNull())				//目标位图非空      
//		pImgn->Destroy();				//删除目标位图
//	pImgn->Create(P.nWidth,P.nHeight,P.nBitCount,0);                        
//
//	if (P.nNumColors>0)					//原始位图有调色板
//	{
// 		pImgm->GetColorTable(0,P.nNumColors,ColorTab);
//		pImgn->SetColorTable(0,P.nNumColors,ColorTab);		//复制调色板数据   
//	}
// 	pOrigDC= CDC::FromHandle(pImgm->GetDC());				//建立原位图CDC
// 	pDC    = CDC::FromHandle(pImgn->GetDC());				//建立目标位图CDC
// 	pDC->BitBlt(0,0,P.nWidth,P.nHeight,pOrigDC,0,0,SRCCOPY);//复制像素数据
// 	pImgm->ReleaseDC();				//释放CDC指针
// 	pImgn->ReleaseDC();
//}
// 
//BYTE** Create2DList(CImage *pImg)		//创建位图二维数组
//{
// 	struct		IMAGEPARAMENT P;
//	int			i;
//	BYTE		**list;
//
// 	GetImageParament(pImg,&P);
//	list=(BYTE**)malloc(P.nHeight*sizeof(BYTE*));        
//	for (i=0;i<P.nHeight;i++) 
//		list[i]=(BYTE*) pImg->GetPixelAddress(0,i); 
//
//	return(list);
//}
//
//void Release2DList(BYTE** list)			//释放位图二维数组
//{
//	free(list);
//}
//
//COLORREF DwordToColorref(DWORD c)
//{
//   union COLOR {
//      COLORREF  clrref;
//	  BYTE		c[4];
//   } col;
//   BYTE	 i;
//
//   col.clrref=c;
//   i=col.c[0];		col.c[0]=col.c[2];
//   col.c[2]=i;
//   return(col.clrref);
//}

/*图像旋转函数
    Imgn/Imgm        目标图像/源图像指针
    alpha            旋转角度,顺时针方向为正*/
void RotateCimage(CImage *Imgm, CImage *Imgn, double alpha)
{
    IMAGEPARAMENT P; 
    RGBQUAD ColorTab[256]; 
    int i, j, ww, Xd, Yd, Dx, Dy,nSize; 
    double centerx, centery, sintheta, costheta; 
    double X1, Y1, X2, Y2, theta, xx, yy, rr; 
    BYTE **list, *sc; 
    int x1, y1, x2, y2, flag; 
    double p, q, a, b, c, d, t1, t2, t3; 
     
    if (ImageType(Imgm) == 2)  flag = 1; //flag为标志位,当取值为1时,表示双线性内插法 ,此时图像类型为灰阶图像
    else flag = 0; //0表示最近邻点法 
    GetImageParament(Imgm, &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); 
     
    centerx = Dx / 2; //计算位图中心位置 
    centery = Dy / 2; 
    rr = sqrt(centerx * centerx + centery *centery); //计算对角线长度 
     
    theta = atan((double) centery / (double) centerx); 
     
    X1 = fabs(rr * cos(alpha + theta)) + 0.5; 
    Y1 = fabs(rr * sin(alpha + theta)) + 0.5; 
    X2 = fabs(rr * cos(alpha - theta)) + 0.5; 
    Y2 = fabs(rr * sin(alpha - theta)) + 0.5; 
     
    if (X2 > X1) X1 = X2; //得外接矩形宽度 
    if (Y2 > Y1) Y1 = Y2; //外接矩形高度 
    ww = (int) (2 * X1); 
     
    Imgn ->Destroy(); 
     
    Imgn ->Create(ww, (int) (2 * Y1), P.nBitCount ); //建立结果位图 
    if (P.nBitCount == 8) 
    { 
        GetAllPalette(Imgm, ColorTab); 
        //修改一,设置目标调色板
        SetAllPalette(Imgn, ColorTab); //复制调色板 
    } 
    sintheta = sin(alpha); 
    costheta = cos(alpha); 
     
    for (j = (int) (centery - Y1), Yd = 0; j <= (centery + Y1); j++, Yd++) 
    { 
        if (P.nBitCount == 8) 
            memset (sc, 0, ww); //256色位图像素行置背景值 
        else 
            memset(sc, 0, ww * P.nBytesPerPixel); //真彩色位图像素行置背景值 
         
        for (i = (int) (centerx - X1), Xd = 0; i <= centerx + X1; i++, Xd += P.nBytesPerPixel) 
        { 
            xx = centerx + costheta * (i - centerx) + sintheta * (j - centery); 
            yy = centery - sintheta * (i - centerx) + costheta * (j - centery); 
            x1 = (int) xx; 
            x2 = x1 + 1; 
            p = xx - x1; 
            y1 = (int) yy; 
            y2 = y1 + 1; 
            q = yy - y1; 
            if (((x1 < 0)||(x2 >= P.nWidth )||(y1 < 0)||(y2 >= P.nHeight ))) 
                continue; 
            if (flag == 0) 
            { 
                if (q > 0.5)  y1 = y2; 
                if (p > 0.5)  x1 = x2; 
                //修改二, sc[Xd]
                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); 
    } 
    free(list); //释放工作单元 
    free(sc); 
 
}


调用处:

CImage* pImage2 = new CImage;

RotateCimage(pImage, pImage2, 1.570796);//顺时针90度

注:在旋转之前应该学会使用CImage绘制简单图形

参考:

http://bbs.csdn.net/topics/330025467

http://download.csdn.net/download/dkbrain/1368853

http://www.pudn.com/downloads113/sourcecode/graph/texture_mapping/detail475636.html


评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值