libavif和libwebp使用示例
以下是将JPG格式图片解码再分别转换成AVIF和WebP格式的使用示例。
- 创建main.cpp文件,写入以下示例代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
#include <avif/avif.h> #include <iostream> #include <fstream> #include <vector> #include <turbojpeg.h> #include <png.h> #include <gif_lib.h> #include <vector> #include <map> #include <algorithm> #include <memory> #include <stdexcept> #include <wand/magick_wand.h> #include <webp/encode.h> // JPG文件解码 static bool decodeJPGtoRGBA(const std::string& inputPath, std::vector<uint8_t>& rgbaData, int& width, int& height) { // 打开JPEG文件 std::ifstream inputFile(inputPath, std::ios::binary | std::ios::ate); if (!inputFile) { std::cerr << "无法打开文件: " << inputPath << std::endl; return false; } // 获取文件大小并读取数据 std::streamsize fileSize = inputFile.tellg(); inputFile.seekg(0, std::ios::beg); std::vector<unsigned char> jpegData(fileSize); if (!inputFile.read(reinterpret_cast<char*>(jpegData.data()), fileSize)) { std::cerr << "读取文件失败: " << inputPath << std::endl; return false; } // 创建TurboJPEG解码器实例 tjhandle handle = tjInitDecompress(); if (!handle) { std::cerr << "无法初始化TurboJPEG解码器" << std::endl; return false; } // 获取JPEG图片的宽高和像素格式 int jpegSubsamp; if (tjDecompressHeader2(handle, jpegData.data(), jpegData.size(), &width, &height, &jpegSubsamp) != 0) { std::cerr << "无法解析JPEG头: " << tjGetErrorStr() << std::endl; tjDestroy(handle); return false; } // 分配RGBA数据存储空间 rgbaData.resize(width * height * 4); // 每个像素4字节 (RGBA) // 解码JPEG数据为RGBA格式 if (tjDecompress2(handle, jpegData.data(), jpegData.size(), rgbaData.data(), width, 0, height, TJPF_RGBA, TJFLAG_FASTDCT) != 0) { std::cerr << "JPEG解码失败: " << tjGetErrorStr() << std::endl; tjDestroy(handle); return false; } tjDestroy(handle); std::cout << "JPEG 文件已成功解码为RGBA格式" << std::endl; return true; } // 将 RGBA 数据编码为AVIF文件 bool encodeRGBAtoAVIF(const std::string& outputPath, const uint8_t* rgbaData, int width, int height) { // 创建AVIF编码器 avifEncoder* encoder = avifEncoderCreate(); if (!encoder) { std::cerr << "无法创建AVIF编码器" << std::endl; return false; } // 设置编码器参数 encoder->maxThreads = 8; // 设置最大线程数 encoder->minQuantizer = 19; // 最小量化器 encoder->maxQuantizer = 21; // 最大量化器 encoder->minQuantizerAlpha = 0; encoder->maxQuantizerAlpha = 0; encoder->tileRowsLog2=3; encoder->tileColsLog2=3; encoder->speed = 8; // 编码速度 encoder->codecChoice = AVIF_CODEC_CHOICE_AOM; avifImage* image = avifImageCreate(width, height, 8, AVIF_PIXEL_FORMAT_YUV420); if (!image) { std::cerr << "无法创建AVIF图像" << std::endl; avifEncoderDestroy(encoder); return false; } image->yuvFormat = AVIF_PIXEL_FORMAT_YUV420; image->depth = 8; image->width = width; image->height = height; avifRGBImage rgbImage; avifRGBImageSetDefaults(&rgbImage, image); rgbImage.format = AVIF_RGB_FORMAT_RGBA; rgbImage.depth = 8; rgbImage.width = width; rgbImage.height = height; rgbImage.rowBytes = width * 4; rgbImage.pixels = const_cast<uint8_t*>(rgbaData); if (avifImageRGBToYUV(image, &rgbImage) != AVIF_RESULT_OK) { std::cerr << "无法将RGBA数据转换为YUV" << std::endl; avifImageDestroy(image); avifEncoderDestroy(encoder); return false; } avifRWData output = AVIF_DATA_EMPTY; if (avifEncoderWrite(encoder, image, &output) != AVIF_RESULT_OK) { std::cerr << "编码失败" << std::endl; avifImageDestroy(image); avifEncoderDestroy(encoder); return false; } std::ofstream outFile(outputPath, std::ios::binary); if (!outFile.write(reinterpret_cast<char*>(output.data), output.size)) { std::cerr << "无法写入文件" << std::endl; avifRWDataFree(&output); avifImageDestroy(image); avifEncoderDestroy(encoder); return false; } avifRWDataFree(&output); avifImageDestroy(image); avifEncoderDestroy(encoder); std::cout << "AVIF文件已成功生成: " << outputPath << std::endl; return true; } // 将AVIF图像转换为RGBA格式 bool decodeAVIFtoRGBA(const std::string& inputPath, std::vector<uint8_t>& rgbaData, int& width, int& height) { // 打开AVIF文件 std::ifstream inFile(inputPath, std::ios::binary | std::ios::ate); if (!inFile) { std::cerr << "无法打开AVIF文件: " << inputPath << std::endl; return false; } std::streamsize fileSize = inFile.tellg(); inFile.seekg(0, std::ios::beg); std::vector<uint8_t> fileData(fileSize); if (!inFile.read(reinterpret_cast<char*>(fileData.data()), fileSize)) { std::cerr << "无法读取AVIF文件: " << inputPath << std::endl; return false; } // 创建AVIF解码器 avifDecoder* decoder = avifDecoderCreate(); if (!decoder) { std::cerr << "无法创建AVIF解码器" << std::endl; return false; } avifDecoderSetIOMemory(decoder, fileData.data(), fileSize); if (avifDecoderParse(decoder) != AVIF_RESULT_OK) { std::cerr << "无法解析AVIF数据" << std::endl; avifDecoderDestroy(decoder); return false; } if (avifDecoderNextImage(decoder) != AVIF_RESULT_OK) { std::cerr << "无法解码AVIF图像" << std::endl; avifDecoderDestroy(decoder); return false; } const avifImage* image = decoder->image; avifRGBImage rgbImage; avifRGBImageSetDefaults(&rgbImage, image); rgbImage.format = AVIF_RGB_FORMAT_RGBA; rgbImage.depth = 8; rgbImage.rowBytes = image->width * 4; width = image->width; height = image->height; rgbaData.resize(rgbImage.rowBytes * image->height); rgbImage.pixels = rgbaData.data(); if (avifImageYUVToRGB(image, &rgbImage) != AVIF_RESULT_OK) { std::cerr << "无法将YUV转换为RGBA" << std::endl; avifDecoderDestroy(decoder); return false; } avifDecoderDestroy(decoder); std::cout << "AVIF文件已成功解码为RGBA格式" << std::endl; return true; } // 将RGBA数据编码为WebP文件 bool encodeRGBAtoWebP(const std::string& outputPath, const uint8_t* rgbaData, int width, int height) { if (!rgbaData || width <= 0 || height <= 0) { std::cerr << "无效的输入参数" << std::endl; return false; } // 设置编码参数 WebPConfig config; if (!WebPConfigInit(&config)) { std::cerr << "无法初始化 WebPConfig" << std::endl; return false; } config.lossless = 0; // 使用有损压缩 config.quality = 75.0f; // 设定压缩质量 config.method = 6; // 编码速度(越大越慢但压缩率更高) // RGBA转WebP图片结构 WebPPicture picture; if (!WebPPictureInit(&picture)) { std::cerr << "无法初始化 WebPPicture" << std::endl; return false; } picture.width = width; picture.height = height; picture.use_argb = 0; // 使用YUV模式编码 // 将RGBA数据导入到WebPPicture if (!WebPPictureImportRGBA(&picture, rgbaData, width * 4)) { std::cerr << "无法导入RGBA数据" << std::endl; WebPPictureFree(&picture); return false; } // 输出内存结构体 WebPMemoryWriter writer; WebPMemoryWriterInit(&writer); picture.writer = WebPMemoryWrite; picture.custom_ptr = &writer; // 进行编码 if (!WebPEncode(&config, &picture)) { std::cerr << "WebP编码失败,错误代码:" << picture.error_code << std::endl; WebPPictureFree(&picture); WebPMemoryWriterClear(&writer); return false; } // 写入文件 std::ofstream outFile(outputPath, std::ios::binary); if (!outFile.write(reinterpret_cast<char*>(writer.mem), writer.size)) { std::cerr << "无法写入文件" << std::endl; WebPPictureFree(&picture); WebPMemoryWriterClear(&writer); return false; } WebPPictureFree(&picture); WebPMemoryWriterClear(&writer); std::cout << "WebP文件已成功生成: " << outputPath << std::endl; return true; } int main() { std::vector<uint8_t> rgbaData; int width, height; std::string outputPathavif = "./1.avif"; std::string outputPathWebp = "./1.webp"; std::string inputPath = "/home/imgtest/1.jpg"; bool decode = decodeJPGtoRGBA(inputPath, rgbaData, width, height); bool encodeAvif = encodeRGBAtoAVIF(outputPathavif, rgbaData.data(), width, height); // 转换成AVIF格式 bool encodeWebp = encodeRGBAtoWebP(outputPathWebp, rgbaData.data(), width, height); // 转换成WebP格式 return 0; }
- 编译执行。
1
g++ -std=c++17 -I/opt/jdimg/include -I/opt/jdimg/include/GraphicsMagick -I/opt/jdimg/include/webp -L/opt/jdimg/lib -L/opt/jdimg/lib64 -lavif -lturbojpeg -lpng -lgif -lwebp -lGraphicsMagick -lGraphicsMagick++ -lGraphicsMagickWand -Wl,-rpath,/opt/jdimg/lib64 main.cpp -o main && ./main
父主题: 安装使用libwebp和libavif