0

Im having trouble encoding and decoding from webp using libwebp in c++. I built this test application which takes webp file i created (which looks fine in chrome), and tried decoding it to rgb, and back to webp, just to understand how to use it, and the output is wrong:

//Code before reads the webp file, and assigning it to a uint8_t buffer called "pic" ///

WebPConfig config;
ASSERT(WebConfigIit(&config) == TRUE)

int width=0 height =0;
uint8_t * rgb = WebPDecodeRGB(pic, size, &width, &height)
ASSERT (rgb != NULL)
// At this point the width and the height is valid, and the rgb is assigned.

uint8_t* originalPic = null;
size_t size = WebPEncodeRGB(rgb, width, height, height, &originalPic);
// Didn't quite know what to put in the stride param.. 

/// Code after saves the originalPic buffer to a file ////

As you can see, all i tried to do is manage to encode and decode the webp, and trying to save it back to a file, but as i try to open the file, it looks corrupted.

Can you please help me figure out what the problem is? more over, i would be happy to know some more about the stride param, and about the rgb format, and how to convert pictures to it to check if my code works.

Thanks alot!

bcesars
  • 1,016
  • 1
  • 17
  • 36
user1612927
  • 95
  • 1
  • 5
  • 3
    The stride parameter should be `width * 3` for RGB colorspace, and `width * 4` for RGBA, it is basically how many bytes does it take to store a scanline or a row. `size_t size = WebPEncodeRGB(rgb, width, height, width * 3, &originalPic);` – Mohamed El-Sayed Jan 10 '16 at 04:06
  • I searched google for about an hour and read spec sheets on PNG to finally find this comment. Why would google leave this obviously useful detail out of the main docs for libwebp.. why must it remain tribal knowledge.. why can't we have nice things? – Dagrooms Mar 21 '23 at 23:54

1 Answers1

1

The 4th parameter to WebPEncodeRGB is the stride (https://stackoverflow.com/a/2004638). Passing height may not work unless the height coincides with the stride.

Community
  • 1
  • 1
techolic
  • 358
  • 3
  • 14