[RESOLVED] webp convert to jpg
Hi All. does anyone have any code that converts a webp image to jpg. i have used freeimage.dll ver 3.18 and MFreeImage ver 2.24 and strangely one time it works and then the next it doesn't on the same image.
i am trying to use.
Code:
dib = FreeImage_LoadEx(Source, FILO_LOAD_DEFAULT, 300, 450)
bOK = FreeImage_Save(FIF_JPEG, dib, Destination, 0)
' Unload the dib
FreeImage_Unload (dib)
or this
Code:
Public Function ConvertWebPToJPG2(webpFile As String, jpgFile As String) As Boolean
Dim dib As Long
Dim Success As Long
' Initialize FreeImage
FreeImage_Initialise True
' Load WebP image
dib = FreeImage_Load(FIF_WEBP, webpFile, 0)
If dib = 0 Then
MsgBox "Failed to load WebP image!"
FreeImage_DeInitialise
ConvertWebPToJPG2 = False
Exit Function
End If
' Save as JPG
Success = FreeImage_Save(FIF_JPEG, dib, jpgFile, 0)
' Free resources
FreeImage_Unload dib
FreeImage_DeInitialise
ConvertWebPToJPG2 = (Success <> 0)
End Function
I am downloading images from trakt and then want to convert to jpg and resize to show in a picture box.
i just need something 100% everytime.
thanks
Re: [RESOLVED] webp convert to jpg
For what it's worth here's the shortest possible equivalent function that converts any image file (including webp) to JPG with optional resizing. No Wic, no GdiPlus required:
Code:
Private Sub ConvertToJpg(sSourceFile As String, sJpgFile As String, Optional ByVal NewWidth As Long, Optional ByVal NewHeight As Long)
Dim StorageFile As IStorageFile, DecoderStream As IRandomAccessStream, EncoderStream As IRandomAccessStream, BitmapEncoder As IBitmapEncoder, JpegEncoderId As UUID, _
BitmapEncoderWithSoftwareBitmap As IBitmapEncoderWithSoftwareBitmap, BitmapFrameWithSoftwareBitmap As IBitmapFrameWithSoftwareBitmap, SoftwareBitmap As ISoftwareBitmap
With New cAwait
If .Await(StorageFileStatics.GetFileFromPathAsync(StrRef(sSourceFile))) = AsyncStatus_Completed Then
Set StorageFile = .GetResults
If .Await(StorageFile.OpenAsync(FileAccessMode_Read)) = AsyncStatus_Completed Then
Set DecoderStream = .GetResults
If .Await(BitmapDecoderStatics.CreateAsync(DecoderStream)) = AsyncStatus_Completed Then
Set BitmapFrameWithSoftwareBitmap = .GetResults
If .Await(BitmapFrameWithSoftwareBitmap.GetSoftwareBitmapAsync) = AsyncStatus_Completed Then
Set SoftwareBitmap = .GetResults
If .Await(FileRandomAccessStreamStatics.OpenWithOptionsAsync(StrRef(sJpgFile), FileAccessMode_Write, StorageOpenOptions_None, FileOpenDisposition_CreateAlways)) = AsyncStatus_Completed Then
Set EncoderStream = .GetResults: JpegEncoderId = BitmapEncoderStatics.JpegEncoderId
If .Await(BitmapEncoderStatics.CreateAsync(JpegEncoderId.Data1, JpegEncoderId.Data2, JpegEncoderId.Data3, JpegEncoderId.Data4, EncoderStream)) = AsyncStatus_Completed Then
Set BitmapEncoder = .GetResults: Set BitmapEncoderWithSoftwareBitmap = BitmapEncoder: BitmapEncoderWithSoftwareBitmap.SetSoftwareBitmap SoftwareBitmap
If NewWidth > 0 And NewHeight > 0 Then
With BitmapEncoder.BitmapTransform: .ScaledWidth = NewWidth: .ScaledHeight = NewHeight: .InterpolationMode = BitmapInterpolationMode_Fant: End With
End If
If .Await(BitmapEncoder.FlushAsync) = AsyncStatus_Completed Then Dispose EncoderStream: Dispose DecoderStream: Dispose SoftwareBitmap
End If
End If
End If
End If
End If
End If
End With
End Sub
Typical usage:
Code:
ConvertToJpg App.Path & "\FileName.webp", App.Path & "\FileName.jpg"
https://www.youtube.com/watch?v=vAjEHyVaufg
Requires the WinRT TypeLib and helper modules.
Re: [RESOLVED] webp convert to jpg
RT is nice and definitely something good to learn and use.
WIC works for windows 7 as well, so the pick if windows 7 is what u also require.
if u need for windows 7, u need to include the webp codec as its not part of the OS.
Re: [RESOLVED] webp convert to jpg
I believe that WinRT uses WIC in the background for image files, and Media Foundation for audio and video files. The difference is that, in WinRT, the code is somewhat shorter and simpler compared to writing the same functionality using WIC or Media Foundation directly.
Re: [RESOLVED] webp convert to jpg
Quote:
Originally Posted by
VanGoghGaming
For what it's worth here's the shortest possible equivalent function that converts any image file (including webp) to JPG with optional resizing. No Wic, no GdiPlus required:
Code:
Private Sub ConvertToJpg(sSourceFile As String, sJpgFile As String, Optional ByVal NewWidth As Long, Optional ByVal NewHeight As Long)
Dim StorageFile As IStorageFile, DecoderStream As IRandomAccessStream, EncoderStream As IRandomAccessStream, BitmapEncoder As IBitmapEncoder, JpegEncoderId As UUID, _
BitmapEncoderWithSoftwareBitmap As IBitmapEncoderWithSoftwareBitmap, BitmapFrameWithSoftwareBitmap As IBitmapFrameWithSoftwareBitmap, SoftwareBitmap As ISoftwareBitmap
With New cAwait
If .Await(StorageFileStatics.GetFileFromPathAsync(StrRef(sSourceFile))) = AsyncStatus_Completed Then
Set StorageFile = .GetResults
If .Await(StorageFile.OpenAsync(FileAccessMode_Read)) = AsyncStatus_Completed Then
Set DecoderStream = .GetResults
If .Await(BitmapDecoderStatics.CreateAsync(DecoderStream)) = AsyncStatus_Completed Then
Set BitmapFrameWithSoftwareBitmap = .GetResults
If .Await(BitmapFrameWithSoftwareBitmap.GetSoftwareBitmapAsync) = AsyncStatus_Completed Then
Set SoftwareBitmap = .GetResults
If .Await(FileRandomAccessStreamStatics.OpenWithOptionsAsync(StrRef(sJpgFile), FileAccessMode_Write, StorageOpenOptions_None, FileOpenDisposition_CreateAlways)) = AsyncStatus_Completed Then
Set EncoderStream = .GetResults: JpegEncoderId = BitmapEncoderStatics.JpegEncoderId
If .Await(BitmapEncoderStatics.CreateAsync(JpegEncoderId.Data1, JpegEncoderId.Data2, JpegEncoderId.Data3, JpegEncoderId.Data4, EncoderStream)) = AsyncStatus_Completed Then
Set BitmapEncoder = .GetResults: Set BitmapEncoderWithSoftwareBitmap = BitmapEncoder: BitmapEncoderWithSoftwareBitmap.SetSoftwareBitmap SoftwareBitmap
If NewWidth > 0 And NewHeight > 0 Then
With BitmapEncoder.BitmapTransform: .ScaledWidth = NewWidth: .ScaledHeight = NewHeight: .InterpolationMode = BitmapInterpolationMode_Fant: End With
End If
If .Await(BitmapEncoder.FlushAsync) = AsyncStatus_Completed Then Dispose EncoderStream: Dispose DecoderStream: Dispose SoftwareBitmap
End If
End If
End If
End If
End If
End If
End With
End Sub
Typical usage:
Code:
ConvertToJpg App.Path & "\FileName.webp", App.Path & "\FileName.jpg"
https://www.youtube.com/watch?v=vAjEHyVaufg
Requires the
WinRT TypeLib and helper modules.
just tried your code but stops on first line with error. are there any references i need to add etc ie IStorageFile not defined
Re: [RESOLVED] webp convert to jpg
Quote:
Originally Posted by
k_zeon
just tried your code but stops on first line with error. are there any references i need to add etc ie IStorageFile not defined
You need the "WinRT Interfaces x86.tlb" file from "WinRT Interfaces x86.zip". Copy it to the "C:\Windows\SysWOW64" folder and set a reference to this TLB. You also need several files from "WinRT Samples Collection.zip" (specifically from the "WinRT Samples Collection\Common" subdirectory) that you must add to your project (mdlHelpers.bas, mdlStatics.bas, and cAwait.cls). See the link from VanGoghGaming.
Re: [RESOLVED] webp convert to jpg
Thanks for the detailed instructions Franky, you rock! :thumb:
Re: [RESOLVED] webp convert to jpg
If you want a minimal implementation using WIC directly without the WinRT layer on top, this uses oleexp.tlb in VB6 or WinDevLib in twinBASIC:
Code:
Private Sub ConvertToJpg(sSourceFile As String, sJpgFile As String, Optional ByVal NewWidth As Long, Optional ByVal NewHeight As Long)
Dim pFact As WICImagingFactory, pFrame As IWICBitmapFrameDecode, pConverter As IWICFormatConverter, pScaler As IWICBitmapScaler
Dim mSave As IWICBitmapSource, pDecoder As IWICBitmapDecoder, GUIDNULL As UUID
#If TWINBASIC = 0 Then
Const vbNullPtr As LongPtr = 0
#End If
Set pFact = New WICImagingFactory
If (pFact Is Nothing) = False Then
Set pDecoder = pFact.CreateDecoderFromFilename(StrPtr(sSourceFile), GUIDNULL, &H80000000, WICDecodeMetadataCacheOnDemand)
If (pDecoder Is Nothing) = False Then
Set pFrame = pDecoder.GetFrame(0)
If NewWidth > 0 And NewHeight > 0 Then
pFact.CreateBitmapScaler pScaler
pScaler.Initialize pFrame, NewWidth, NewHeight, WICBitmapInterpolationModeHighQualityCubic
Set mSave = pScaler
Else
Set mSave = pFrame
End If
Set pConverter = pFact.CreateFormatConverter()
pConverter.Initialize mSave, GUID_WICPixelFormat24bppBGR, WICBitmapDitherTypeNone, Nothing, 0, WICBitmapPaletteTypeCustom
Dim pStream As IWICStream, pEncoder As IWICBitmapEncoder, pFrameEnc As IWICBitmapFrameEncode
pFact.CreateStream pStream
pStream.InitializeFromFilename StrPtr(sJpgFile), &H40000000
Set pEncoder = pFact.CreateEncoder(GUID_ContainerFormatJpeg, GUIDNULL)
pEncoder.Initialize pStream, WICBitmapEncoderNoCache
Dim pPropBag As IPropertyBag2
pEncoder.CreateNewFrame pFrameEnc, pPropBag
pFrameEnc.Initialize pPropBag
Dim w As Long, h As Long
pConverter.GetSize w, h
pFrameEnc.SetSize w, h
pFrameEnc.SetPixelFormat GUID_WICPixelFormat24bppBGR
pFrameEnc.WriteSource pConverter, ByVal vbNullPtr
pFrameEnc.Commit
pEncoder.Commit
End If
End If
End Sub
Then in VB6 for the GUIDs the oleexp included addons mIID.bas/mWIC.bas or just this:
Code:
Public Function GUID_WICPixelFormat24bppBGR() As UUID
Static iid As UUID
If (iid.Data1 = 0&) Then Call DEFINE_UUID(iid, &H6FDDC324, &H4E03, &H4BFE, &HB1, &H85, &H3D, &H77, &H76, &H8D, &HC9, &HC)
GUID_WICPixelFormat24bppBGR = iid
End Function
Public Function GUID_ContainerFormatJpeg() As UUID
Static iid As UUID
If (iid.Data1 = 0&) Then Call DEFINE_UUID(iid, &H19E4A5AA, &H5662, &H4FC5, &HA0, &HC0, &H17, &H58, &H2, &H8E, &H10, &H57)
GUID_ContainerFormatJpeg = iid
End Function
Private Sub DEFINE_UUID(Name As UUID, L As Long, w1 As Integer, w2 As Integer, B0 As Byte, b1 As Byte, b2 As Byte, B3 As Byte, b4 As Byte, b5 As Byte, b6 As Byte, b7 As Byte)
With Name
.Data1 = L: .Data2 = w1: .Data3 = w2
.Data4(0) = B0: .Data4(1) = b1: .Data4(2) = b2: .Data4(3) = B3: .Data4(4) = b4: .Data4(5) = b5: .Data4(6) = b6: .Data4(7) = b7
End With
End Sub
WinRT uses WIC under the hood so this has all the same format support, without higher version requirements that WinRT imposes. :bigyello: