bgrabitmap icon indicating copy to clipboard operation
bgrabitmap copied to clipboard

Slow WebP

Open regs01 opened this issue 5 months ago • 3 comments

I tried WebP to store 256x256 thumbnails. But it appears to be almost 15 times slower than PNG. 1800 thumbnails takes additional 8 to 12 seconds on SaveToStreamAs(ThumbStream, ifPNG), but over 110-120 seconds with SaveToStreamAs(ThumbStream, ifWebP). I tried to compile most recent version of llbwebp, which made little fast to those 110-120 seconds. It was even longer with older library from here. Is there any way to play with WebP settings?

if ThumbBitmap = nil then
    ThumbBitmap := GetFileThumbnail(AFileName, AThumbWidth, AThumbHeight, BGRAPixelTransparent, True);

ThumbStream := TMemoryStream.Create;
try
  ...
  ThumbBitmap.SaveToStreamAs(ThumbStream, ifWebP);
  ...
finally
  ThumbStream.Free;
end;  

regs01 avatar Aug 08 '25 21:08 regs01

Well, i managed with explicitly using a writer with lossy encoding, which support alpha channel as well. Much faster and smaller.

var
WriterWebP: TBGRAWriterWebP; 
...
begin

  ...
  
  ThumbStream := TMemoryStream.Create;
  WriterWebP := TBGRAWriterWebP.Create;  
  
  try
    ...
    WriterWebP.Lossless := False;
    WriterWebP.QualityPercent := 60; 
    ThumbBitmap.SaveToStream(ThumbStream, WriterWebP);
    ...
  finally
    WriterWebP.Free;
    ThumbStream.Free;   
  end;
  
...

end;

regs01 avatar Aug 11 '25 20:08 regs01

By the way, implementation is a little old. libwebp now support method and compression for lossless.

regs01 avatar Aug 12 '25 14:08 regs01

Hello @regs01

Thanks for popping by.

Indeed, WebP writer is by default lossless and with 100% quality. This way, it is as good as PNG to that regard. However, it is indeed a bit slow.

Creating your own writer is indeed a way to specify the quality. Good to know it is faster and suits the thumbnail generation.

Thanks for letting us know about additional parameters. I found the method parameter in WebPConfig the documentation (quality over speed trade-off from 0=fast-draft to 6=slower-better). I am not sure however what compression parameter is. Would you like to expand on that?

Regards

circular17 avatar Sep 08 '25 11:09 circular17