使用c语言写一个图片缩放算法
时间: 2023-09-02 20:07:43 浏览: 264
图片 缩放 算法
下面是一个简单的图片缩放算法的 C 语言实现,使用了双线性插值法来进行图像的缩放:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char BYTE;
// 读取 BMP 图像文件
BYTE* ReadBMP(char* bmpFileHeader, char* bmpInfoHeader, char* bmpPixelData) {
FILE* fp = fopen("image.bmp", "rb");
if (fp == NULL) {
printf("Failed to open the file!\n");
return NULL;
}
BYTE* bmpData = NULL;
BYTE* bmpPixel = NULL;
int width = *(int*)(bmpInfoHeader + 4);
int height = *(int*)(bmpInfoHeader + 8);
int bitCount = *(int*)(bmpInfoHeader + 14);
int lineByte = (width * bitCount / 8 + 3) / 4 * 4;
bmpData = (BYTE*)malloc(lineByte * height);
memset(bmpData, 0, lineByte * height);
bmpPixel = (BYTE*)malloc(bitCount / 8);
memset(bmpPixel, 0, bitCount / 8);
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j++) {
fread(bmpPixel, bitCount / 8, 1, fp);
bmpData[i * lineByte + j * bitCount / 8 + 0] = bmpPixel[0];
bmpData[i * lineByte + j * bitCount / 8 + 1] = bmpPixel[1];
bmpData[i * lineByte + j * bitCount / 8 + 2] = bmpPixel[2];
}
fseek(fp, lineByte - width * bitCount / 8, SEEK_CUR);
}
fclose(fp);
free(bmpPixel);
return bmpData;
}
// 写入 BMP 图像文件
void WriteBMP(char* bmpFileHeader, char* bmpInfoHeader, BYTE* bmpData) {
FILE* fp = fopen("output.bmp", "wb");
if (fp == NULL) {
printf("Failed to open the file!\n");
return;
}
int width = *(int*)(bmpInfoHeader + 4);
int height = *(int*)(bmpInfoHeader + 8);
int bitCount = *(int*)(bmpInfoHeader + 14);
int lineByte = (width * bitCount / 8 + 3) / 4 * 4;
fwrite(bmpFileHeader, 1, 14, fp);
fwrite(bmpInfoHeader, 1, 40, fp);
for (int i = height - 1; i >= 0; i--) {
fwrite(bmpData + i * lineByte, lineByte, 1, fp);
}
fclose(fp);
}
// 图像缩放
BYTE* Resize(BYTE* bmpData, int width, int height, float ratio) {
int newWidth = (int)(width * ratio);
int newHeight = (int)(height * ratio);
int bitCount = 24;
int lineByte = (newWidth * bitCount / 8 + 3) / 4 * 4;
BYTE* newBmpData = (BYTE*)malloc(lineByte * newHeight);
memset(newBmpData, 0, lineByte * newHeight);
for (int i = 0; i < newHeight; i++) {
for (int j = 0; j < newWidth; j++) {
int x = (int)(j / ratio);
int y = (int)(i / ratio);
float u = j / ratio - x;
float v = i / ratio - y;
int pos1 = y * width + x;
int pos2 = pos1 + 1;
int pos3 = (y + 1) * width + x;
int pos4 = pos3 + 1;
BYTE b1 = bmpData[pos1 * 3 + 0];
BYTE g1 = bmpData[pos1 * 3 + 1];
BYTE r1 = bmpData[pos1 * 3 + 2];
BYTE b2 = bmpData[pos2 * 3 + 0];
BYTE g2 = bmpData[pos2 * 3 + 1];
BYTE r2 = bmpData[pos2 * 3 + 2];
BYTE b3 = bmpData[pos3 * 3 + 0];
BYTE g3 = bmpData[pos3 * 3 + 1];
BYTE r3 = bmpData[pos3 * 3 + 2];
BYTE b4 = bmpData[pos4 * 3 + 0];
BYTE g4 = bmpData[pos4 * 3 + 1];
BYTE r4 = bmpData[pos4 * 3 + 2];
BYTE b = (BYTE)((1 - u) * (1 - v) * b1 + u * (1 - v) * b2 + (1 - u) * v * b3 + u * v * b4);
BYTE g = (BYTE)((1 - u) * (1 - v) * g1 + u * (1 - v) * g2 + (1 - u) * v * g3 + u * v * g4);
BYTE r = (BYTE)((1 - u) * (1 - v) * r1 + u * (1 - v) * r2 + (1 - u) * v * r3 + u * v * r4);
newBmpData[i * lineByte + j * bitCount / 8 + 0] = b;
newBmpData[i * lineByte + j * bitCount / 8 + 1] = g;
newBmpData[i * lineByte + j * bitCount / 8 + 2] = r;
}
}
return newBmpData;
}
int main() {
char bmpFileHeader[14];
char bmpInfoHeader[40];
char bmpPixelData[100000];
BYTE* bmpData = ReadBMP(bmpFileHeader, bmpInfoHeader, bmpPixelData);
int width = *(int*)(bmpInfoHeader + 4);
int height = *(int*)(bmpInfoHeader + 8);
float ratio = 0.5;
BYTE* newBmpData = Resize(bmpData, width, height, ratio);
WriteBMP(bmpFileHeader, bmpInfoHeader, newBmpData);
free(bmpData);
free(newBmpData);
return 0;
}
```
这个示例代码使用了双线性插值算法,可以将原始图像缩小到原来的一半。你可以根据实际需求来修改代码,以适应更多的图像缩放需求。
阅读全文