在linux系统中,可以使用libwebp库进行webp图片的解码

sudo apt-get install libwebp-dev登录后复制
在CentOS和RHEL系统上,可以使用以下命令安装:
sudo yum install libwebp-devel登录后复制 编写一个简单的C程序来解码WebP图片。以下是一个示例程序:
#<span>include <stdio.h></span>
#<span>include <stdlib.h></span>
#<span>include <webp/decode.h></span>
int main(<span>int argc, char *argv[])</span> {
if (argc != 3) {
printf("Usage: %s <input_file> <output_file>
", argv[0]);
return 1;
}
const char *input_file = argv[1];
const char *output_file = argv[2];
FILE *infile = fopen(input_file, "rb");
if (!infile) {
perror("Error opening input file");
return 1;
}
size_t width, height;
uint8_t *image_data = fread(infile, 1, 1024, infile);
if (!image_data) {
perror("Error reading input file");
fclose(infile);
return 1;
}
WebPDecoderConfig config;
WebPInitDecoderConfig(&config);
config.output_format = WEBP_FORMAT_RGBA;
config.output_size = width * height * 4;
int decode_status = WebPDecodeRGBA(image_data, image_data + fread(infile, 1, 1024, infile), &width, &height, &config);
if (decode_status != VP8_DECODE_OK) {
fprintf(stderr, "Error decoding WebP file
");
free(image_data);
fclose(infile);
return 1;
}
infile = fopen(output_file, "wb");
if (!infile) {
perror("Error opening output file");
free(image_data);
return 1;
}
fwrite(image_data, 1, width * height * 4, infile);
fclose(infile);
free(image_data);
printf("WebP file decoded successfully and saved to %s
", output_file);
return 0;
}
登录后复制
使用gcc编译器编译上述程序。确保链接到libwebp库:
gcc -o webp_decoder webp_decoder.c -lwebp登录后复制 运行编译后的程序,传入WebP文件的路径以及输出PNG文件的路径:
./webp_decoder input.webp output.png登录后复制
程序将读取WebP文件并将其解码为RGBA图像,然后将解码后的图像保存为PNG文件。
本文来自投稿,不代表本站立场,如若转载,请注明出处:
