简单代码实现:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#define HEIGHT 1080
#define WIDTH 1440
int is_in_array(short x, short y, short height, short width)
{
if (x >= 0 && x < width && y >= 0 && y < height)
return 1;
else
return 0;
}
void bilinera_interpolation(short** in_array, short height, short width,
short** out_array, short out_height, short out_width)
{
double h_times = (double)out_height / (double)height,
w_times = (double)out_width / (double)width;
short x1, y1, x2, y2, f11, f12, f21, f22;
double x, y;
for (int i = 0; i < out_height; i++){
for (int j = 0; j < out_width; j++){
x = j / w_times;
y = i / h_times;
x1 = (short)(x - 1);
x2 = (short)(x + 1);
y1 = (short)(y + 1);
y2 = (short)(y - 1);
f11 = is_in_array(x1, y1, height, width) ? in_array[y1][x1] : 0;
f12 = is_in_array(x1, y2, height, width) ? in_array[y2][x1] : 0;
f21 = is_in_array(x2, y1, height, width) ? in_array[y1][x2] : 0;
f22 = is_in_array(x2, y2, height, width) ? in_array[y2][x2] : 0;
out_array[i][j] = (short)(((f11 * (x2 - x) * (y2 - y)) +
(f21 * (x - x1) * (y2 - y)) +
(f12 * (x2 - x) * (y - y1)) +
(f22 * (x - x1) * (y - y1))) / ((x2 - x1) * (y2 - y1)));
}
}
}
typedef BYTE; // 定义BYTE类型,占1个字节
int main(void)
{
FILE *fp = NULL;
BYTE PicZoomOut[1280][720];
BYTE *ptr;
BYTE **Pic = new BYTE *[HEIGHT];
for (int i = 0; i != HEIGHT; ++i)
{
Pic[i] = new BYTE[WIDTH];
}
ptr = (BYTE*)malloc(WIDTH * HEIGHT * sizeof(BYTE));//创建内存
fp = fopen("1.jpg", "rb");
fseek(fp, 0L, SEEK_END);
int len = ftell(fp);
fseek(fp, 0L, SEEK_SET);
fread(ptr, sizeof(BYTE), len, fp);
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
Pic[i][j] = *ptr;
ptr++;
}
}
fclose(fp);
fp = fopen("2.jpg", "wb");
bilinera_interpolation(Pic,HEIGHT,WIDTH,PicZoomOut,1280,720);
fwrite(PicZoomOut, 1, 1, fp);
fclose(fp);
return 0;
}