用C写一个缩小图像的程序
时间: 2023-03-29 08:00:39 浏览: 82
你好,以下是用C语言编写缩小图像的程序示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Usage: %s input_image output_image scale\n", argv[]);
return 1;
}
char *input_path = argv[1];
char *output_path = argv[2];
float scale = atof(argv[3]);
int width, height, channels;
unsigned char *image = stbi_load(input_path, &width, &height, &channels, );
if (image == NULL) {
printf("Failed to load image: %s\n", input_path);
return 1;
}
int new_width = (int)(width * scale);
int new_height = (int)(height * scale);
unsigned char *new_image = (unsigned char *)malloc(new_width * new_height * channels);
for (int y = ; y < new_height; y++) {
for (int x = ; x < new_width; x++) {
int src_x = (int)(x / scale);
int src_y = (int)(y / scale);
for (int c = ; c < channels; c++) {
new_image[(y * new_width + x) * channels + c] = image[(src_y * width + src_x) * channels + c];
}
}
}
stbi_write_png(output_path, new_width, new_height, channels, new_image, new_width * channels);
stbi_image_free(image);
free(new_image);
return ;
}
希望对你有所帮助。
阅读全文