📄 geooperator.cpp
字号:
#include "StdAfx.h"
#include "GeoOperator.h"
#include <algorithm>
using namespace std;
#include <cmath>
#include <iostream>
#include <ctime>
/******************************************************************************
* 作用: 平移变换函数
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
* xx 平移变换的水平偏移量
* yy 平移变换的垂直偏移量
* 返回值: 是否成功
******************************************************************************/
bool MoveTransNormal(BYTE* pixel, BYTE*& tempPixel, int width, int height,
int xx, int yy)
{
// 输出图像大小
int size = width * height * 4;
tempPixel = new BYTE[size];
memset(tempPixel, 255, size);
// 偏移量是否在合法范围内
if (xx >= width || yy >= height || xx <= -width || yy <= -height)
return false;
int y = 0; // 变换后的像素纵坐标
BYTE* copyPixel = NULL; // 指向原始图像中需要拷贝像素的起始位置
BYTE* objPixel = NULL; // 指向被复制像素的起始位置
int copyWidth = width - abs(xx); // 需要拷贝图像的宽度
for (int j = 0; j < height; j++)
{
// 指向原始图像第j行的起始位置
copyPixel = pixel + j * width * 4;
// 修正被复制像素的起始位置
if (xx < 0)
copyPixel += abs(xx) * 4;
// 计算变换后的像素纵坐标
y = j + yy;
if (y >= 0 && y < height)
{
objPixel = tempPixel + y * width * 4;
if (xx > 0)
objPixel += abs(xx) * 4;
// 拷贝一行图像
memcpy(objPixel, copyPixel, copyWidth * 4);
}
}
return true;
}
/******************************************************************************
* 作用: 平移变换函数
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
* xx 平移变换的水平偏移量
* yy 平移变换的垂直偏移量
* outWidth [out]输出图像的宽度
* outHeight [out]输出图像的高度
******************************************************************************/
void MoveTransSize(BYTE* pixel, BYTE*& tempPixel, int width, int height,
int xx, int yy, UINT& outWidth, UINT& outHeight)
{
outWidth = width + abs(xx);
outHeight = height + abs(yy);
int size = outWidth * outHeight * 4;
tempPixel = new BYTE[size];
memset(tempPixel, 255, size);
int x = 0; // 每行复制起点的横坐标
int y = 0; // 每行复制起点的纵坐标
BYTE* copyPixel = NULL; // 指向原始图像中需要拷贝像素的起始位置
BYTE* objPixel = NULL; // 指向被复制像素的起始位置
if (xx > 0)
x = xx;
if (yy > 0)
y = yy;
for (int j = 0; j < height; j++)
{
copyPixel = pixel + j * width * 4;
objPixel = tempPixel + y * outWidth * 4 + x * 4;
y++;
memcpy(objPixel, copyPixel, width * 4);
}
}
/******************************************************************************
* 作用: 水平镜像变换函数
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
******************************************************************************/
void HMirrorTrans(BYTE* pixel, BYTE*& tempPixel, int width, int height)
{
int size = width * height * 4;
tempPixel = new BYTE[size];
memset(tempPixel, 255, size);
int x = 0; // 变换后的像素横坐标
int y = 0; // 变换后的像素纵坐标
BYTE* copyPixel = NULL; // 指向原始图像中需要拷贝像素的起始位置
BYTE* objPixel = NULL; // 指向被复制像素的起始位置
for (int j = 0; j < height; j++)
{
y = j;
for (int i = 0; i < width; i++)
{
x = width - i - 1;
copyPixel = pixel + j * width * 4 + i * 4;
objPixel = tempPixel + y * width * 4 + x * 4;
memcpy(objPixel, copyPixel, 4);
}
}
}
/******************************************************************************
* 作用: 垂直镜像变换函数
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
******************************************************************************/
void VMirrorTrans(BYTE* pixel, BYTE*& tempPixel, int width, int height)
{
int size = width * height * 4;
tempPixel = new BYTE[size];
memset(tempPixel, 255, size);
int x = 0; // 变换后的像素横坐标
int y = 0; // 变换后的像素纵坐标
BYTE* copyPixel = NULL; // 指向原始图像中需要拷贝像素的起始位置
BYTE* objPixel = NULL; // 指向被复制像素的起始位置
for (int j = 0; j < height; j++)
{
y = height - j - 1;
for (int i = 0; i < width; i++)
{
x = i;
copyPixel = pixel + j * width * 4 + i * 4;
objPixel = tempPixel + y * width * 4 + x * 4;
memcpy(objPixel, copyPixel, 4);
}
}
}
/******************************************************************************
* 作用: 图像的缩放函数(最临近插值法)
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
* outWidth [out]输出图像的宽度
* outHeight [out]输出图像的高度
* fx 水平缩放系数
* fy 垂直缩放系数
******************************************************************************/
void ZoomNormal(BYTE* pixel, BYTE*& tempPixel, int width, int height,
UINT& outWidth, UINT& outHeight, double fx, double fy)
{
// 计算缩放后的图像大小
outWidth = (UINT)(width * fx);
outHeight = (UINT)(height * fy);
int size = outWidth * outHeight * 4;
tempPixel = new BYTE[size];
memset(tempPixel, 255, size);
BYTE* copyPixel = NULL; // 指向原始图像中需要拷贝像素的起始位置
BYTE* objPixel = NULL; // 指向被复制像素的起始位置
int x = 0; // 变换后的像素横坐标
int y = 0; // 变换后的像素纵坐标
long tempY; // 存储中间值,提高函数速度
long tempJ; // 存储中间值,提高函数速度
for (UINT j = 0; j < outHeight; j++)
{
// 获得临近像素的纵坐标
y = (int)(j / fy + 0.5);
// 修正坐标
if (y >= height)
y--;
// 计算与i,x无关的中间值
tempY = y * width * 4;
tempJ = j * outWidth * 4;
for (UINT i = 0; i < outWidth; i++)
{
// 获得临近像素的横坐标
x = (int)(i / fx + 0.5);
// 修正坐标
if (x >= width)
x--;
copyPixel = pixel + tempY + x * 4;
objPixel = tempPixel + tempJ + i * 4;
memcpy(objPixel, copyPixel, 4);
}
}
}
/******************************************************************************
* 作用: 图像的缩放函数(双线性插值法)
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
* outWidth [out]输出图像的宽度
* outHeight [out]输出图像的高度
* fx 水平缩放系数
* fy 垂直缩放系数
******************************************************************************/
void ZoomInterpolation(BYTE* pixel, BYTE*& tempPixel, int width, int height,
UINT& outWidth, UINT& outHeight, double fx, double fy)
{
// 计算缩放后的图像大小
outWidth = (UINT)(width * fx + 0.5);
outHeight = (UINT)(height * fy + 0.5);
int size = outWidth * outHeight * 4;
tempPixel = new BYTE[size];
memset(tempPixel, 255, size);
BYTE* copyPixel = NULL; // 指向原始图像中需要拷贝像素的起始位置
BYTE* objPixel = NULL; // 指向被复制像素的起始位置
double x = 0.0; // 变换后的像素横坐标
double y = 0.0; // 变换后的像素纵坐标
long tempJ = 0; // 存储中间值,提高函数速度
for (UINT j = 0; j < outHeight; j++)
{
y = j / fy;
tempJ = j * outWidth * 4;
for (UINT i = 0; i < outWidth; i++)
{
x = i / fx;
objPixel = tempPixel + tempJ + i * 4;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -