【VC开源代码栏目提醒】:网学会员在VC开源代码频道为大家收集整理了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);
}
}
/* // 方法二 通过交换像素进行变换
memcpy(tempPixel, pixel, size);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width / 2; j++)
{
swap(tempPixel[j*4 + i*width*4], tempPixel[(i+1)*width*4 - j*4 - 4]);
swap(tempPixel[j*4 + i*width*4 + 1], tempPixel[(i+1)*width*4 - j*4 - 3]);
swap(tempPixel[j*4 + i*width*4 + 2], tempPixel[(i+1)*width*4 - j*4 - 2]);
swap(tempPixel[j*4 + i*width*4 + 3], tempPixel[(i+1)*width*4 - j*4 - 1]);
}
}*/
}
/******************************************************************************
* 作用: 垂直镜像变换函数
* 参数:
* 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);
}
}
/* // 方法二 通过交换像素进行变换
// 拷贝原始图像
memcpy(tempPixel, pixel, size);
for(int i = 0; i < width * 4; i+=4)
{
for (int j = 0; j < height / 2; j++)
{
swap(tempPixel[j*width*4 + i], tempPixel[(height-j-1)*width*4 + i]);
swap(tempPixel[j*width*4 + i + 1], tempPixel[(height-j-1)*width*4 + i + 1]);
swap(tempPixel[j*width*4 + i + 2], tempPixel[(height-j-1)*width*4 + i + 2]);
swap(tempPixel[j*width*4 + i + 3], tempPixel[(height-j-1)*width*4 + i + 3]);
}
}*/
}
/******************************************************************************
* 作用: 图像的转置函数
* 参数:
* pixel 原始像素数组
* tempPixel 保存变换后图像的像素数组
* width 原始图像宽度
* height 原始图像高度
* outWidth [out]输出图像的宽度
* outHeight [out]输出图像的高度
******************************************************************************/
void Transpose(BYTE* pixel, BYTE*& tempPixel, int width, int height, UINT& outWidth, UINT& outHeight)
{
// 转置后图像的宽度和高度
outWidth = (UINT)height;
outHeight = (UINT)width;
int size = outWidth * outHeight * 4;
tempPixel = new BYTE[size];
memset(tempPixel, 0, size);
BYTE* copyPixel = NULL; // 指向原始图像中需要拷贝像素的起始位置
BYTE* objPixel = NULL; // 指向被复制像素的起始位置
int x = 0; // 变换后的像素横坐标
int y = 0; // 变换后的像素纵坐标
int tempJ = 0; // 存储中间值,提高函数速度
for (UINT j = 0; j < outHeight; j++)
{
x = j;
// 可以提前计算与i无关的中间值
// 可以大大减少重复计算
tempJ = j * outWidth * 4;
f
上一篇:
geom.cpp
下一篇:
西班牙语医院科室名称