Image Plugin for Webp format

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Image Plugin for Webp format

Post by IdeasVacuum »

Pure Basic is handy for automating the creation of HTML files. However, the Webp format is becoming very popular (High Quality, small file size) but PB does not support it.
https://developers.google.com/speed/webp

Encoders are available for Windows, Linux and macOS:
https://developers.google.com/speed/web ... recompiled
https://developers.google.com/speed/web ... iling#unix
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Image Plugin for Webp format

Post by Seymour Clufley »

+1

Definitely. This format is everywhere now.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 230
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Image Plugin for Webp format

Post by DeanH »

+1

High priority please!

In the meantime, does anyone know of a freeware dll that can load this format?
User avatar
Caronte3D
Addict
Addict
Posts: 1056
Joined: Fri Jan 22, 2016 5:33 pm
Location: Some Universe

Re: Image Plugin for Webp format

Post by Caronte3D »

+1
User avatar
jacdelad
Addict
Addict
Posts: 1492
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Image Plugin for Webp format

Post by jacdelad »

+1 and I would pay a few bucks. (Also I'd like to see JPEGXL, which also has cross platform libraries and will cause a heated debate if I ask for it).
PureBasic 6.11/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/150TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
BarryG
Addict
Addict
Posts: 3331
Joined: Thu Apr 18, 2019 8:17 am

Re: Image Plugin for Webp format

Post by BarryG »

DeanH wrote: Mon Nov 21, 2022 10:34 pmIn the meantime, does anyone know of a freeware dll that can load this format?
The FreeImage Project does (it's a DLL) -> https://freeimage.sourceforge.io/

But see this post -> viewtopic.php?p=576176#p576176
User avatar
jacdelad
Addict
Addict
Posts: 1492
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Image Plugin for Webp format

Post by jacdelad »

Thank Barry, I know, but I couldn't find code how to use it (and my attempts didn't work for now, also maybe someone will share it so there would be no need to reinvent the wheel).

Edit: Following your post I saw there maybe already is come code available. Thanks. The power of this library seems to be limitless, there's a lot to do.
PureBasic 6.11/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/150TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Image Plugin for Webp format

Post by Seymour Clufley »

WebP support is really becoming a glaring omission at this point.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User23
New User
New User
Posts: 2
Joined: Mon Apr 24, 2023 4:33 pm

Re: Image Plugin for Webp format

Post by User23 »

+1
In the meantime, I use dwebp with RunProgram to transform the image to PNG.
Only 460 ko but then because I need JPG, I have to do then a LoadImage/SaveImage to go from PNG to JPG so load two plugins as well (PNGdecoder/ JPGEncoder)
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Image Plugin for Webp format

Post by Seymour Clufley »

User23 wrote: Fri May 12, 2023 1:32 pm +1
In the meantime, I use dwebp with RunProgram to transform the image to PNG.
Only 460 ko but then because I need JPG, I have to do then a LoadImage/SaveImage to go from PNG to JPG so load two plugins as well (PNGdecoder/ JPGEncoder)
Thanks for that suggestion. Good idea for the meantime.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
Seymour Clufley
Addict
Addict
Posts: 1233
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: Image Plugin for Webp format

Post by Seymour Clufley »

Here is code for loading and saving WebP images in the meantime, using LibWebP. You just need to change the first line to point to the LibWebP bin folder.

Code: Select all

#LibWebpDirectory = "P:\libwebp-1.3.0-windows-x64\libwebp-1.3.0-windows-x64\bin\"
Global c34.s{1} = Chr(34)

#DwebpExe = #LibWebpDirectory+"dwebp.exe"
Procedure.i LoadWebpImageUsingTempFile(webp_fn.s)
  folder.s = GetTemporaryDirectory()
  png_fn.s = folder+Str(Random(9999999,1000000))+".png"
  param.s = c34+webp_fn+c34+" -o "+c34+png_fn+c34
  RunProgram(#DwebpExe,param,folder,#PB_Program_Wait)
  UsePNGImageDecoder()
  img.i = LoadImage(#PB_Any,png_fn)
  ProcedureReturn img
EndProcedure

Procedure.i LoadWebpImage(webp_fn.s)
  param.s = c34+webp_fn+c34+" -o -"
  prog.i = RunProgram(#DwebpExe,param,GetPathPart(webp_fn),#PB_Program_Open | #PB_Program_Error | #PB_Program_Read)
  
  memsize.i = 0
  While ProgramRunning(prog)
    temp_memsize.i = AvailableProgramOutput(prog)
    If temp_memsize
      memsize = temp_memsize
      ;Debug "avo: "+Str(memsize)
      *mem = ReAllocateMemory(*mem,memsize)
      ReadProgramData(prog,*mem,memsize)
    EndIf
    er.s = ReadProgramError(prog)
    If er="Saved to stdout"
      ;Debug "err: "+er
      Break
    EndIf
  Wend
  CloseProgram(prog)
  
  UsePNGImageDecoder()
  img.i = CatchImage(#PB_Any,*mem,memsize)
  ProcedureReturn img
EndProcedure

#CwebpExe = #LibWebpDirectory+"cwebp.exe"
Procedure.b SaveWebpImage(img.i,webp_fn.s)
  folder.s = GetTemporaryDirectory()
  png_fn.s = folder+Str(Random(9999999,1000000))+".png"
  UsePNGImageEncoder()
  SaveImage(img,png_fn,#PB_ImagePlugin_PNG)
  param.s = c34+png_fn+c34+" -o "+c34+webp_fn+c34
  prog.i = RunProgram(#CwebpExe,param,GetPathPart(webp_fn),#PB_Program_Open | #PB_Program_Error | #PB_Program_Read)
  While ProgramRunning(prog)
    st.s = ReadProgramString(prog)
    If st<>""
      Debug "str: "+st
    EndIf
    er.s = ReadProgramError(prog)
    If er<>""
      Debug "err: "+er
    EndIf
  Wend
  CloseProgram(prog)
  
  DeleteFile(png_fn)
  ProcedureReturn #True
EndProcedure
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 230
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Image Plugin for Webp format

Post by DeanH »

Seymour that is really useful! I was able to get it to work with my folder arrangement. Now have to work out whether I want it to change format when a user saves a .webp file or do it on the fly when displaying. Probably the former. It would be better if it was a dll instead of an exe, and am wondering why the developers didn't produce that. Or have I missed it?
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 230
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Image Plugin for Webp format

Post by DeanH »

I have found a webp image file that LoadWebpImage does not load, at least on my laptop here at work. It seems to go through the steps but *mem and memsize are both 0. No error is reported. Other webp images I have tried do load successfully.

The image file is at http://bookmark.central.sa.edu.au/1865154954.webp
It displays in Chrome, Firefox and Edge browsers.
Post Reply