Skip to content

Commit

Permalink
fix: [TextureConverter] Restore LoadWICFile for Windows due to incomp…
Browse files Browse the repository at this point in the history
…lete cross-platform functionality (#2435)

Co-authored-by: Basewq <Basewq@users.noreply.github.com>
  • Loading branch information
Basewq and Basewq authored Sep 8, 2024
1 parent 4ebbf54 commit 0cfc464
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ HRESULT dxtLoadTGAFile( const char* szFile, DirectX::TexMetadata* metadata, Dire
return result;
}

HRESULT dxtLoadWICFile(LPCWSTR szFile, int flags, DirectX::TexMetadata* metadata, DirectX::ScratchImage& image)
{
return DirectX::LoadFromWICFile(szFile, flags, metadata, image);
}

HRESULT dxtSaveToDDSFile( const DirectX::Image& image, DirectX::DDS_FLAGS flags, const char* szFile )
{
const wchar_t* filePath = narrowToWideString(szFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ internal class DxtTexLib : ITexLibrary
{
private static Logger Log = GlobalLogger.GetLogger("DxtTexLib");

private static HashSet<string> SupportedExtensions = new(StringComparer.InvariantCultureIgnoreCase)
private static HashSet<string> SupportedExtensionsWindows = new(StringComparer.InvariantCultureIgnoreCase)
{
".dds",
".bmp",
".tga",
".jpg",
".jpeg",
".jpe",
".png",
".tiff",
".tif",
};

private static HashSet<string> SupportedExtensionsNonWindows = new(StringComparer.InvariantCultureIgnoreCase)
{
".dds",
".tga",
Expand Down Expand Up @@ -125,7 +138,7 @@ public bool CanHandleRequest(PixelFormat format, IRequest request)
{
case RequestType.Loading:
LoadingRequest loader = (LoadingRequest)request;
return loader.Mode==LoadingRequest.LoadingMode.FilePath && SupportedExtensions.Contains(Path.GetExtension(loader.FilePath));
return loader.Mode==LoadingRequest.LoadingMode.FilePath && IsSupportedExtension(Path.GetExtension(loader.FilePath));

case RequestType.Compressing:
CompressingRequest compress = (CompressingRequest)request;
Expand Down Expand Up @@ -158,6 +171,18 @@ public bool CanHandleRequest(PixelFormat format, IRequest request)
}
}

private bool IsSupportedExtension(string fileExtension)
{
if (OperatingSystem.IsWindows())
{
return SupportedExtensionsWindows.Contains(fileExtension);
}
else
{
return SupportedExtensionsNonWindows.Contains(fileExtension);
}
}

public void Execute(TexImage image, IRequest request)
{
DxtTextureLibraryData libraryData = image.LibraryData.TryGetValue(this, out var libData) ? (DxtTextureLibraryData)libData : null;
Expand Down Expand Up @@ -222,7 +247,15 @@ private void Load(TexImage image, LoadingRequest loader)
{
hr = Utilities.LoadTGAFile(loader.FilePath, out libraryData.Metadata, libraryData.Image);
}

else if (OperatingSystem.IsWindows())
{
hr = Utilities.LoadWICFile(loader.FilePath, WIC_FLAGS.WIC_FLAGS_NONE, out libraryData.Metadata, libraryData.Image);
}
else
{
hr = (HRESULT)(-1);
}

if (hr != HRESULT.S_OK)
{
Log.Error("Loading dds file " + loader.FilePath + " failed: " + hr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ internal class Utilities
[DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Auto), SuppressUnmanagedCodeSecurity]
private extern static uint dxtLoadTGAFile(string filePath, out TexMetadata metadata, IntPtr image);

[DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
private extern static uint dxtLoadWICFile(String filePath, WIC_FLAGS flags, out TexMetadata metadata, IntPtr image);

[DllImport("DxtWrapper", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity]
private extern static bool dxtIsCompressed(DXGI_FORMAT fmt);

Expand Down Expand Up @@ -584,6 +587,11 @@ public static HRESULT LoadTGAFile(string filePath, out TexMetadata metadata, Scr
return HandleHRESULT(dxtLoadTGAFile(filePath, out metadata, image.ptr));
}

public static HRESULT LoadWICFile(String filePath, WIC_FLAGS flags, out TexMetadata metadata, ScratchImage image)
{
return HandleHRESULT(dxtLoadWICFile(filePath, flags, out metadata, image.ptr));
}

public static HRESULT SaveToDDSFile(ref DxtImage dxtImage, DDS_FLAGS flags, string szFile)
{
return HandleHRESULT(dxtSaveToDDSFile(ref dxtImage, flags, szFile));
Expand Down

0 comments on commit 0cfc464

Please sign in to comment.