c语言使用libwebp压缩

// *rgb_frame里面存的是bgra (FFMPEG:AV_PIX_FMT_BGRA)格式的数据,大小height * width * 4
int encodeWebpRGB(uint8_t *rgb_frame, int height, int width){
    FILE *out = fopen("d:\\test.webp", "wb");
    WebPMemoryWriter memory_writer;
    WebPMemoryWriterInit(&memory_writer);
    WebPPicture picture;
    WebPConfig config;
    if (!WebPPictureInit(&picture) ||
        !WebPConfigInit(&config)) {
        fprintf(stderr, "Error! Version mismatch!\n");
        return -1;
    }
    //可以在这里控制编码快慢和图像质量
    config.method = 0;
//    config.quality = 100;
    picture.use_argb = 1;
    picture.width = 1920;
    picture.height = 1080;
    picture.writer = WebPMemoryWrite;
    picture.custom_ptr = (void*)&memory_writer;
    if (!WebPPictureAlloc(&picture)) {
        return 0;   // memory error
    }
    memcpy(picture.argb, rgb_frame, height * width * 4);
    int ok = WebPEncode(&config, &picture);
    if (!ok) {
        printf("encoding error\n");
    }
    fwrite(memory_writer.mem, 1, memory_writer.max_size, out);
    WebPPictureFree(&picture);
    WebPMemoryWriterClear(&memory_writer);
    fclose(out);
}

你可能感兴趣的:(c语言使用libwebp压缩)