I need to rescale an image to webp of 5 difference sizes quickly, and encoding takes up a lot of time. I want to encode, and then rescale, but I'm not sure how/ if i can do that. I'm using encode.h and cwebp.c as a reference.
In the code below, one thing I notice is that after rescaling the memory writer remains the same, and so the size in memory remains the same, so it seems like these aren't being properly rescaled.
Also I'd like to ask how to save these from cpp to my hard drive. Is it something with the memory writer?
Thanks so much!
#include "/usr/local/include/webp/encode.h"
int main(){
std::cout << "Hello World!" << std::endl;
int l = 200;
int w = 200;
uint8_t* bgra_ptr = new uint8_t[l*w*4];
//populate the bgra_image for testing
for (int i = 0; i < l*w*4; i++){
bgra_ptr[i] = i % 255;
}
int bgra_stride = 1;
int keep_alpha = 1;
WebPConfig config;
WebPConfigInit(&config);
config.alpha_quality = 50;
config.quality = 50;
int config_error = WebPValidateConfig(&config);
WebPPicture pic;
if (!WebPPictureInit(&pic)) return 0; // version error
pic.width = w;
pic.height = l;
pic.use_argb = 1;
if (!WebPPictureAlloc(&pic)) return 0; // memory error
WebPMemoryWriter wrt;
WebPMemoryWriterInit(&wrt);
pic.writer = WebPMemoryWrite;
pic.custom_ptr = &wrt;
int ok = WebPEncode(&config, &pic);
if (!ok) {
std::cout << "Encoding error: "<< pic.error_code << std::endl; // 4 means invalid configuration
} else {
std::cout << "Output size: "<< wrt.size << std::endl;
}
int sizes[5] = {10, 100, 500, 1000, 2000};
int size_cur;
WebPPicture rescale_pic;
for (int i = 0; i < 5; i++){
size_cur = sizes[i];
WebPPictureCopy(&pic, &rescale_pic);
int rescaled_successfully = WebPPictureRescale(&rescale_pic, size_cur, 0);
}
WebPMemoryWriterClear(&wrt);
return 0;
}