1、图片缩放最近邻算法函数声明
/// srcWight 原图像宽度
/// srcHeight 原图像高度
/// destWight 目标图像宽度
/// destHeight 目标图像高度
/// srcBytesPerPixel 原始图像一个像素所占字节数
/// srcBytesPerLine 原始图像一行像素所占字节数
/// destBytesPerLine 目标图像一行像素所占字节数
/// srcData 原始图像数据
/// destData 目标像素数据
void NearValueCalculate(int srcWight,int srcHeight,int destWight,int destHeight,int srcBytesPerPixel,int srcBytesPerLine, int destBytesPerLine, std::vector<BYTE> &srcData,std::vector<BYTE> &destData)
2、图片缩放最近邻算法函数定义
void NearValueCalculate(int srcWight,int srcHeight,int destWight,int destHeight,int srcBytesPerPixel,int srcBytesPerLine, int destBytesPerLine, std::vector<BYTE> &srcData,std::vector<BYTE> &destData)
{
for (int y = 0; y < destHeight; y++)
{
for (int x = 0; x < destWight; x++)
{
int srcX = x * srcWight / destWight;
int srcY = y * srcHeight / destHeight;
for (int i = 0; i < srcBytesPerPixel; i++)
{
destData[y * destBytesPerLine + x * srcBytesPerPixel + i] =
srcData[srcY * srcBytesPerLine + srcX * srcBytesPerPixel + i];
}
}
}
}
3、图片缩放插值算法函数声明
/// srcWight 原图像宽度
/// srcHeight 原图像高度
/// destWight 目标图像宽度
/// destHeight 目标图像高度
/// srcBytesPerPixel 原始图像一个像素所占字节数
/// srcBytesPerLine 原始图像一行像素所占字节数
/// destBytesPerLine 目标图像一行像素所占字节数
/// srcData 原始图像数据
/// destData 目标像素数据
void InsertValueCalculate(int srcWight,int srcHeight,int destWight,int destHeight,int srcBytesPerPixel,int srcBytesPerLine, int destBytesPerLine, std::vector<BYTE> &srcData,std::vector<BYTE> &destData);
4、图片缩放插值算法函数定义
void InsertValueCalculate(int srcWight,int srcHeight,int destWight,int destHeight,int srcBytesPerPixel,int srcBytesPerLine, int destBytesPerLine, std::vector<BYTE> &srcData,std::vector<BYTE> &destData)
{
for (int hnum = 0; hnum < destHeight; hnum++){
for (int wnum = 0; wnum < destWight; wnum++)
{
double d_original_img_hnum = 1.0 * hnum * srcHeight / destHeight;
double d_original_img_wnum = 1.0 * wnum * srcWight / destWight;
int i_original_img_hnum = d_original_img_hnum;
int i_original_img_wnum = d_original_img_wnum;
double distance_to_a_x = d_original_img_wnum - i_original_img_wnum;//在原图像中与a点的水平距离
double distance_to_a_y = d_original_img_hnum - i_original_img_hnum;//在原图像中与a点的垂直距离
int original_point_a = i_original_img_hnum * destBytesPerLine + i_original_img_wnum * srcBytesPerPixel;//数组位置偏移量,对应于图像的各像素点RGB的起点,相当于点A
int original_point_b = i_original_img_hnum * destBytesPerLine + (i_original_img_wnum + 1) * srcBytesPerPixel;//数组位置偏移量,对应于图像的各像素点RGB的起点,相当于点B
int original_point_c = (i_original_img_hnum + 1)*destBytesPerLine + i_original_img_wnum * srcBytesPerPixel;//数组位置偏移量,对应于图像的各像素点RGB的起点,相当于点C
int original_point_d = (i_original_img_hnum + 1)*destBytesPerLine + (i_original_img_wnum + 1) * srcBytesPerPixel;//数组位置偏移量,对应于图像的各像素点RGB的起点,相当于点D
if (i_original_img_hnum +1== destHeight - 1)
{
original_point_c = original_point_a;
original_point_d = original_point_b;
}
if (i_original_img_wnum +1== destWight - 1)
{
original_point_b = original_point_a;
original_point_d = original_point_c;
}
int pixel_point = hnum * srcBytesPerLine + wnum * srcBytesPerPixel;//映射尺度变换图像数组位置偏移量
for(int i = 0; i < srcBytesPerPixel; i++){
destData[pixel_point + i] =
srcData[original_point_a + i] * (1 - distance_to_a_x) * (1 - distance_to_a_y) +
srcData[original_point_b + i] * distance_to_a_x * (1 - distance_to_a_y) +
srcData[original_point_c + i] * distance_to_a_y * (1 - distance_to_a_x) +
srcData[original_point_c + i] * distance_to_a_y * distance_to_a_x;
}
}
}
}
5、图片缩放调用函数声明
static HBITMAP BitmapResize(HDC hdc, HBITMAP hBmpSrc, int calculateType = 0);
6、图片缩放调用函数定义
HBITMAP BitmapResize(HDC hdc, HBITMAP hBmpSrc,int calculateType)
{
// 获取源位图的信息
BITMAP bmpSrc;
GetObject(hBmpSrc, sizeof(BITMAP), &bmpSrc);
int newWidth = bmpSrc.bmWidth;
int newHeight = (int)(1.0 * bmpSrc.bmHeight / SCALE_FACTOR);
// 创建目标位图
HBITMAP hBmpDest = CreateCompatibleBitmap(hdc, newWidth, newHeight);
// 获取源位图和目标位图的数据
int srcWidth = bmpSrc.bmWidth;
int srcHeight = bmpSrc.bmHeight;
int srcBytesPerPixel = bmpSrc.bmBitsPixel / 8; //一个像素占字节数
int srcBytesPerLine = ((srcWidth * srcBytesPerPixel + (srcBytesPerPixel - 1)) / srcBytesPerPixel) * srcBytesPerPixel;//原始图像一行像素所占字节数
int destBytesPerLine = ((newWidth * srcBytesPerPixel + (srcBytesPerPixel - 1)) / srcBytesPerPixel) * srcBytesPerPixel;//目标图像一行像素所占字节数
std::vector<BYTE> srcData(srcBytesPerLine * srcHeight);//存储原始图像数据
std::vector<BYTE> destData(destBytesPerLine * newHeight);//存储目标像素数据
GetBitmapBits(hBmpSrc, srcData.size(), srcData.data());
switch (calculateType)
{
case 0:
InsertValueCalculate(srcWidth,srcHeight,newWidth,newHeight,srcBytesPerPixel,srcBytesPerLine,destBytesPerLine,srcData,destData);
break;
case 1:
NearValueCalculate(srcWidth,srcHeight,newWidth,newHeight,srcBytesPerPixel,srcBytesPerLine,destBytesPerLine,srcData,destData);
break;
default:
break;
}
// 设置目标位图的数据
SetBitmapBits(hBmpDest, destData.size(), destData.data());
return hBmpDest;
}
7、调用实例
#define NEW_WIDTH 800
#define NEW_HEIGHT 480
#define SCALE_FACTOR 1.25 //图片缩放系数
HBITMAP hBitmap;
HDC hdc = GetDC()->GetSafeHdc();;
CStatic* picSetDlgBack;
RECT rect;
int y,height;
//缩放窗体
this->MoveWindow(0,0,NEW_WIDTH,NEW_HEIGHT);
//缩放图片
picSetDlgBack = (CStatic *)GetDlgItem(IDC_STATIC);//获取控件指针
picSetDlgBack->GetWindowRect(&rect);
ScreenToClient(&rect);
y = (int)1.0 * rect.top / SCALE_FACTOR;
height = (int)(1.0 * (rect.bottom - rect.top)/SCALE_FACTOR);
picSetDlgBack->MoveWindow(rect.left,y,rect.right - rect.left,height);
hBitmap = ::LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_INITBKG_T));
hBitmap2 = BitmapResize(hdc,hBitmap);
picSetDlgBack->SetBitmap(hBitmap2);
技术交流请加群:996775415