要在Linux下使用libpng库从本地读取灰度图像并将其存储到本地,可以按照以下步骤进行操作:
- 引入必要的头文件:
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
- 定义一个函数来读取PNG文件:
void read_png_file(const char* filename, int* width, int* height, png_byte** image_data) {
FILE *fp = fopen(filename, "rb");
if (!fp) {
fprintf(stderr, "Error opening file %s\n", filename);
return;
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fclose(fp);
fprintf(stderr, "Error creating PNG read struct\n");
return;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
fclose(fp);
png_destroy_read_struct(&png_ptr, NULL, NULL);
fprintf(stderr, "Error creating PNG info struct\n");
return;
}
if (setjmp(png_jmpbuf(png_ptr))) {
fclose(fp);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fprintf(stderr, "Error during PNG read: longjmp error\n");
return;
}
png_init_io(png_ptr, fp);
png_read_info(png_ptr, info_ptr);
*width = png_get_image_width(png_ptr, info_ptr);
*height = png_get_image_height(png_ptr, info_ptr);
// 确保图像是灰度图像(如有需要,可以添加相应的错误处理逻辑)
if (png_get_color_type(png_ptr, info_ptr) != PNG_COLOR_TYPE_GRAY) {
fclose(fp);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fprintf(stderr, "Error: Input image is not grayscale\n");
return;
}
// 确保图像的位深度为8(如有需要,可以添加相应的错误处理逻辑)
if (png_get_bit_depth(png_ptr, info_ptr) != 8) {
fclose(fp);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fprintf(stderr, "Error: Input image does not have 8 bits per sample\n");
return;
}
// 分配内存来保存图像数据
*image_data = (png_byte*)malloc((*width) * (*height));
// 读取图像数据
for (int y = 0; y < (*height); ++y) {
png_bytep row = png_malloc(png_ptr, sizeof(png_byte));
png_read_row(png_ptr, row, NULL);
for (int x = 0; x < (*width); ++x) {
(*image_data)[(*width) * y + x] = row[x];
}
png_free(png_ptr,row);
}
// 清理内存和关闭文件
fclose(fp);
png_destroy_read_struct(&png_ptr,&info_ptr,NULL);
}
- 定义一个函数来写入PNG文件:
void write_png_file(const char* filename,int width,int height,const png_byte* image_data){
FILE *fp=fopen(filename,"wb");
if(!fp){
fprintf(stderr,"Error opening file %s\n",filename);
return;
}
png_structp png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
if(!png_ptr){
fclose(fp);
fprintf(stderr,"Error creating PNG write struct\n");
return;
}
png_infop info_ptr=png_create_info_struct(png_ptr);
if(!info_ptr){
fclose(fp);
png_destroy_write_struct(&png_ptr,NULL);
fprintf(stderr,"Error creating PNG info struct\n");
return;
}
if(setjmp(png_jmpbuf(png_ptr))){
fclose(fp);
png_destroy_write_struct(&png_ptr,&info_ptr);
fprintf(stderr,"Error during PNG write: longjmp error\n");
return;
}
// 设置图像信息
png_init_io(png_ptr,fp);
png_set_IHDR(png_ptr,info_ptr,width,height,8,PNG_COLOR_TYPE_GRAY,
PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_BASE,PNG_FILTER_TYPE_BASE);
// 写入图像数据
png_bytep* row_pointers=(png_bytep*)malloc(sizeof(png_bytep)*height);
for(int y=0;y<height;++y){
row_pointers[y]=(png_byte*)malloc(width*sizeof(png_byte));
memcpy(row_pointers[y],&image_data[width*y],width*sizeof(png_byte));
}
png_set_rows(png_ptr,info_ptr,row_pointers);
// 写入PNG文件
png_write_png(png_ptr,info_ptr,PNG_TRANSFORM_IDENTITY,NULL);
// 清理内存和关闭文件
for(int y=0;y<height;++y){
free(row_pointers[y]);
}
free(row_pointers);
fclose(fp);
png_destroy_write_struct(&png_ptr,&info_ptr);
}
- 调用上述函数来读取灰度图并保存到本地:
int main() {
const char* input_file = "input.png";
const char* output_file = "output.png";
int width, height;
png_byte* image_data;
// 从文件中读取灰度图像
read_png_file(input_file, &width, &height, &image_data);
// 在此处可以对图像数据进行处理(如滤波、变换等)
// 将处理后的图像保存到本地
write_png_file(output_file, width, height, image_data);
// 释放内存
free(image_data);
return 0;
}
确保将libpng库正确链接到您的项目中,并使用合适的编译命令来构建和运行程序。
内容由零声教学AI助手提供,问题来源于学员提问