Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Restore LoadWICFile for Windows due to incomplete cross-platform functionality #2435

Merged
merged 1 commit into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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