Using API to rescale after encoding

115 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Alex Lawson

ungelesen,
03.11.2022, 16:56:4703.11.22
an WebP Discussion
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;
}

James Zern

ungelesen,
03.11.2022, 18:14:2703.11.22
an webp-d...@webmproject.org
Hi Alex,

On Thu, Nov 3, 2022 at 1:56 PM Alex Lawson <amlaw...@gmail.com> wrote:
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. 

The rescaling calls are meant to work on uncompressed data, so what you're attempting won't work correctly. With any compressed format you'd need to decompress it, scale and then reencode; that's the process cwebp is doing for input jpegs, pngs, etc. In general it would be better to scale the source first then encode to avoid the extra overhead and degradation that reencoding would introduce.
If speed is an issue you can try setting a lower method value [1] (try 2 or 3) and see if the quality / size work for your use case.

 
--
You received this message because you are subscribed to the Google Groups "WebP Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webp-discuss...@webmproject.org.
To view this discussion on the web visit https://groups.google.com/a/webmproject.org/d/msgid/webp-discuss/a01d0bbf-a016-48ce-8ba8-41d45cabcf7fn%40webmproject.org.

Alex Lawson

ungelesen,
03.11.2022, 18:50:3603.11.22
an WebP Discussion, James Zern
Thank You James,

That is good to know. I can do this rescaling first then. The one question would be how to save the webps using this API? Is that straightforward to do? In addition to changing the method, I'm thinking I can slim down some time by not saving the images to png and using the command line.

Thank you!

Alex

Yannis Guyon

ungelesen,
04.11.2022, 05:18:4104.11.22
an WebP Discussion, amlaw...@gmail.com, James Zern
Once you encoded your image using a WebPMemoryWriter, you can do the following to write it as a file:

FILE * file = fopen("/path/to/your/file.webp", "wb");
fwrite(memory_writer.mem, memory_writer.size, 1, file);
fclose(file);

James Zern

ungelesen,
04.11.2022, 22:42:2604.11.22
an WebP Discussion
On Fri, Nov 4, 2022 at 2:18 AM Yannis Guyon <ygu...@google.com> wrote:
Once you encoded your image using a WebPMemoryWriter, you can do the following to write it as a file:

FILE * file = fopen("/path/to/your/file.webp", "wb");
fwrite(memory_writer.mem, memory_writer.size, 1, file);
fclose(file);

That works or you could take a look at the MyWriter function in cwebp [2] for a slightly simpler approach if you don't need to hang on to the data:

Allen antworten
Antwort an Autor
Weiterleiten
Die Nachricht wurde gelöscht
Die Nachricht wurde gelöscht
0 neue Nachrichten