#include "bmptrans.h"
#include <STRING>
#include <MATH.H>
using namespace std;
#define WIDTHBYTES(bits) (((bits)+31)/32*4)
namespace bmp{
BmpTrans::BmpTrans(){}
BmpTrans::BmpTrans(char* strFile){
rgbToMatrix = new RgbToMatrix(strFile);
pfile = rgbToMatrix->pfile;
dataOfBmp = rgbToMatrix->dataOfBmp;
nPlantNum = rgbToMatrix->nPlantNum;
bitHead = rgbToMatrix->bitHead;
bitInfoHead = rgbToMatrix->bitInfoHead;
height = rgbToMatrix->height;
width = rgbToMatrix->width;
l_width = rgbToMatrix->l_width;
nData = rgbToMatrix->nData;
pRgb = rgbToMatrix->pRgb;
pColorData = rgbToMatrix->pColorData;
}
BmpTrans::~BmpTrans(){
delete rgbToMatrix;
if (bitInfoHead.biBitCount < 24)
{
delete pRgb;
}
delete pColorData;
delete []dataOfBmp;
}
void BmpTrans::menu(){
int x, y;
for (;;)
{
switch (showMenu())
{
case 1:
cout << endl << "请输入平移量:" << endl;
cout << "x = " << flush;
cin >> x;
cout << "y = " << flush;
cin >> y;
translation(x, y);
break;
case 2:
cout << endl << "请输入平移量:" << endl;
cout << "x = " << flush;
cin >> x;
cout << "y = " << flush;
cin >> y;
magnifyTranslation(x , y);
break;
case 3:
standMirror();
break;
case 4:
verticalMirror();
break;
case 5:
spin();
break;
case 6:
magnifySpin();
break;
case 7:
zhuanzhi();
break;
case 8:
zoom();
break;
case 0:
return;
default:
cout << endl << "选择错误!" << endl;
break;
}
}
}
int BmpTrans::showMenu(){
cout << endl;
cout << " 图像变换" << endl;
cout << "===================" << endl;
cout << "[1] 无放大平移" << endl;
cout << "[2] 放大平移" << endl;
cout << "[3] 水平镜像" << endl;
cout << "[4] 垂直镜像" << endl;
cout << "[5] 无放大旋转" << endl;
cout << "[6] 放大旋转" << endl;
cout << "[7] 图像转置" << endl;
cout << "[8] 图像放缩" << endl;
cout << "[0] 返回" << endl;
cout << "===================" << endl;
cout << "请选择:" << flush;
string strEnter;
cin >> strEnter;
if ("1" == strEnter)
return 1;
else if ("2" == strEnter)
return 2;
else if ("3" == strEnter)
return 3;
else if ("4" == strEnter)
return 4;
else if ("5" == strEnter)
return 5;
else if ("6" == strEnter)
return 6;
else if ("7" == strEnter)
return 7;
else if ("8" == strEnter)
return 8;
else if ("0" == strEnter)
return 0;
else
return -1;
}
/*无放大平移*/
void BmpTrans::translation(int x, int y){
//定义一个二维的结构指针**dataOfBmp2,存放变换后的RGB数据
dataOfBmp2 = new tagRGBQUAD*[height * sizeof(tagRGBQUAD)];
dataOfBmp2[0] = new tagRGBQUAD[width*height*sizeof(tagRGBQUAD)];
for (int j = 1;j < height; j++)
{
dataOfBmp2[j] = dataOfBmp2[j-1] + width;
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
if( (i-y>=0) && (j-x>=0) && (i-y<height) && (j-x<=width))
dataOfBmp2[i][j] = dataOfBmp[i-y][j-x];
}
}
saveBmp();
}
/*放大平移*/
void BmpTrans::magnifyTranslation(int x, int y){
height = height + abs(y);
width = width + abs(x);
l_width = WIDTHBYTES(width * bitInfoHead.biBitCount);
nData = height * l_width;
bitInfoHead.biHeight = height;
bitInfoHead.biWidth = width;
bitInfoHead.biSize = nData + bitHead.bfOffBits;
//定义一个二维的结构指针**dataOfBmp2,存放变换后的RGB数据
dataOfBmp2 = new tagRGBQUAD*[height * sizeof(tagRGBQUAD)];
dataOfBmp2[0] = new tagRGBQUAD[width*height*sizeof(tagRGBQUAD)];
for (int j = 1;j < height; j++)
{
dataOfBmp2[j] = dataOfBmp2[j-1] + width;
}
for(int i=0;i<height;i++)
{
for(int j=0;j<width;j++)
{
if( y>=0 && x>=0 && i-y>=0 && j-x>=0 )
{
dataOfBmp2[i][j] = dataOfBmp[i-y][j-x];
}
if( y<0 && x>=0 && i-y<height && j-x>=0 )
{
dataOfBmp2[i][j] = dataOfBmp[i][j-x];
}
if( y>=0 && x<0 && i-y>=0 && j-x<width )
{
dataOfBmp2[i][j] = dataOfBmp[i-y][j];
}
if( y<0 && x<0 && i-y<height && j-x<width )
{
dataOfBmp2[i][j] = dataOfBmp[i][j];
}
}
}
saveBmp();
//恢复原图像的大小
height = height - abs(y);
width = width - abs(x);
l_width = WIDTHBYTES(width * bitInfoHead.biBitCount);
nData = height * l_width;
bitInfoHead.biHeight = height;
bitInfoHead.biWidth = width;
bitInfoHead.biSize = nData + bitHead.bfOffBits;
}
/*水平镜像*/
void BmpTrans::standMirror(){
//定义一个二维的结构指针**dataOfBmp2,存放变换后的RGB数据
dataOfBmp2 = new tagRGBQUAD*[height * sizeof(tagRGBQUAD)];
dataOfBmp2[0] = new tagRGBQUAD[width*height*sizeof(tagRGBQUAD)];
for (int j = 1;j < height; j++)
{
dataOfBmp2[j] = dataOfBmp2[j-1] + width;
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
dataOfBmp2[y][x] = dataOfBmp[y][width - x - 1];
}
}
saveBmp();
}
/*垂直镜像*/
void BmpTrans::verticalMirror(){
//定义一个二维的结构指针**dataOfBmp2,存放变换后的RGB数据
dataOfBmp2 = new tagRGBQUAD*[height * sizeof(tagRGBQUAD)];
dataOfBmp2[0] = new tagRGBQUAD[width*height*sizeof(tagRGBQUAD)];
for (int j = 1;j < height; j++)
{
dataOfBmp2[j] = dataOfBmp2[j-1] + width;
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
dataOfBmp2[y][x] = dataOfBmp[height - y -1][width - x - 1];
}
}
saveBmp();
}
/*无放大旋转*/
void BmpTrans::spin(){
double a;
int m = (int)width / 2;
int n = (int)height / 2; //计算图像中心点坐标
cout << endl << "请输入旋转角度:" << flush;
cin >> a;
a = (a / 180) * 3.1415926;
//定义一个二维的结构指针**dataOfBmp2,存放变换后的RGB数据
dataOfBmp2 = new tagRGBQUAD*[height * sizeof(tagRGBQUAD)];
dataOfBmp2[0] = new tagRGBQUAD[width*height*sizeof(tagRGBQUAD)];
for (int j = 1;j < height; j++)
{
dataOfBmp2[j] = dataOfBmp2[j-1] + width;
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int x1 = (int)((x - m) * cos(a) + (y - n) * sin(a) + m);
int y1 = (int)((x - m) * (-sin(a)) + (y - n) * cos(a) + n);
if (x1 >= 0 && x1 < width && y1 >=0 && y1 <height)
{
dataOfBmp2[y][x] = dataOfBmp[y1][x1];
}
}
}
saveBmp();
}
/*放大旋转*/
void BmpTrans::magnifySpin(){
double a;
int m = (int)width / 2;
int n = (int)height / 2; //计算图像中心点坐标
cout << endl << "请输入旋转角度:" << flush;
cin >> a;
a = (a / 180) * 3.1415926;
int height_midd = height;
int width_midd = width;
int x_trans = int(((width-1)-m)*cos(a)+((height-1)-n)*sin(a)) - m;
int y_trans = int(((height-1)-n)*cos(a)-(0-m)*sin(a)) - n;
width = width + 2 * abs(x_trans);
height = height + 2 * abs(y_trans);
m = (int)width / 2;
n = (int)height / 2;
//计算位图的实际宽度并确保它为32的倍数
l_width = WIDTHBYTES(width* bitInfoHead.biBitCount);
nData = height*l_width;
bitInfoHead.biHeight = height;
bitInfoHead.biWidth = width;
bitHead.bfSize =nData + bitHead.bfOffBits;
//定义一个二维的结构指针**dataOfBmp2,存放变换后的RGB数据
dataOfBmp2 = new tagRGBQUAD*[height * sizeof(tagRGBQUAD)];
dataOfBmp2[0] = new tagRGBQUAD[width*height*sizeof(tagRGBQUAD)];
for (int j = 1;j < height; j++)
{
dataOfBmp2[j] = dataOfBmp2[j-1] + width;
}
tagRGBQUAD** dataOfBmp3 = new tagRGBQUAD*[height * sizeof(tagRGBQUAD)];
dataOfBmp3[0] = new tagRGBQUAD[width*height*sizeof(tagRGBQUAD)];
for (j = 1;j < height; j++)
{
dataOfBmp3[j] = dataOfBmp3[j-1] + width;
}
for (int i = 0; i < height - 2 * y_trans; i++)
{
for (int j = 0; j < width - 2 * x_trans; j++)
{
dataOfBmp3[i + y_trans][j + x_trans] = dataOfBmp[i][j];
}
}
for (i = 0; i < height