feat: Support Khronos texture format

This commit is contained in:
Crsky
2026-03-01 03:32:40 +08:00
parent 52205b50b3
commit 801b93aed4
3 changed files with 127 additions and 0 deletions

View File

@@ -52,9 +52,21 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="BCnEncoder, Version=2.2.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\BCnEncoder.Net.2.2.1\lib\netstandard2.0\BCnEncoder.dll</HintPath>
</Reference>
<Reference Include="CommunityToolkit.HighPerformance, Version=8.4.0.0, Culture=neutral, PublicKeyToken=4aff67a105548ee2, processorArchitecture=MSIL">
<HintPath>..\packages\CommunityToolkit.HighPerformance.8.4.0\lib\netstandard2.0\CommunityToolkit.HighPerformance.dll</HintPath>
</Reference>
<Reference Include="ICSharpCode.SharpZipLib, Version=1.4.2.13, Culture=neutral, PublicKeyToken=1b03e6acf1164f73, processorArchitecture=MSIL">
<HintPath>..\packages\SharpZipLib.1.4.2\lib\netstandard2.0\ICSharpCode.SharpZipLib.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.HashCode, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.HashCode.6.0.0\lib\net462\Microsoft.Bcl.HashCode.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Bcl.Numerics, Version=10.0.0.3, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.Numerics.10.0.3\lib\net462\Microsoft.Bcl.Numerics.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Win32.Registry, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Win32.Registry.5.0.0\lib\net461\Microsoft.Win32.Registry.dll</HintPath>
</Reference>
@@ -179,6 +191,7 @@
<Compile Include="FrontierWorks\ImageEXT.cs" />
<Compile Include="GScripter\ArcDATA.cs" />
<Compile Include="Guyzware\ArcDat.cs" />
<Compile Include="ImageKTX.cs" />
<Compile Include="Ism\ImagePNG.cs" />
<Compile Include="Kid\ArcDATRAW.cs" />
<Compile Include="Kid\ArcKLZ.cs" />

110
ArcFormats/ImageKTX.cs Normal file
View File

@@ -0,0 +1,110 @@
//! \file ImagePIC.cs
//! \date 2017 Dec 04
//! \brief Soft House Sprite modified bitmap image.
//
// Copyright (C) 2017 by morkt
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//
using BCnEncoder.Decoder;
using BCnEncoder.Shared;
using BCnEncoder.Shared.ImageFiles;
using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Reflection;
using System.Windows.Media;
namespace GameRes.Formats
{
internal class KtxMetaData : ImageMetaData
{
public KtxHeader Header;
}
[Export(typeof(ImageFormat))]
public class KtxFormat : ImageFormat
{
public override string Tag { get { return "KTX"; } }
public override string Description { get { return "Khronos texture format"; } }
public override uint Signature { get { return 0x58544BAB; } }
public override bool CanWrite { get { return false; } }
public override ImageMetaData ReadMetaData (IBinaryStream file)
{
var signature = file.ReadInt32 ();
if (0x58544BAB != signature)
return null;
file.Position = 0xC;
var header = new KtxHeader
{
Endianness = file.ReadUInt32 (),
GlType = (GlType) file.ReadUInt32 (),
GlTypeSize = file.ReadUInt32 (),
GlFormat = (GlFormat) file.ReadUInt32 (),
GlInternalFormat = (GlInternalFormat) file.ReadUInt32 (),
GlBaseInternalFormat = (GlFormat) file.ReadUInt32 (),
PixelWidth = file.ReadUInt32 (),
PixelHeight = file.ReadUInt32 (),
PixelDepth = file.ReadUInt32 (),
NumberOfArrayElements = file.ReadUInt32 (),
NumberOfFaces = file.ReadUInt32 (),
NumberOfMipmapLevels = file.ReadUInt32 (),
BytesOfKeyValueData = file.ReadUInt32 (),
};
var ktx = new KtxFile (header);
var decoder = new BcDecoder ();
if (!decoder.IsSupportedFormat (ktx))
return null;
return new KtxMetaData
{
Width = header.PixelWidth,
Height = header.PixelHeight,
BPP = 32,
Header = header,
};
}
public override ImageData Read (IBinaryStream file, ImageMetaData info)
{
var ktx = KtxFile.Load (file.AsStream);
var decoder = new BcDecoder ();
var buffer = decoder.Decode (ktx);
var pixels = new byte[buffer.Length*4];
var src = 0;
var dst = 0;
while (src < buffer.Length)
{
pixels[dst ] = buffer[src].b;
pixels[dst+1] = buffer[src].g;
pixels[dst+2] = buffer[src].r;
pixels[dst+3] = buffer[src].a;
src += 1;
dst += 4;
}
return ImageData.Create (info, PixelFormats.Bgra32, null, pixels);
}
public override void Write (Stream file, ImageData image)
{
throw new NotImplementedException ("KtxFormat.Write not implemented");
}
}
}

View File

@@ -1,5 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BCnEncoder.Net" version="2.2.1" targetFramework="net472" />
<package id="CommunityToolkit.HighPerformance" version="8.4.0" targetFramework="net472" />
<package id="Microsoft.Bcl.HashCode" version="6.0.0" targetFramework="net472" />
<package id="Microsoft.Bcl.Numerics" version="10.0.3" targetFramework="net472" />
<package id="Microsoft.Win32.Registry" version="5.0.0" targetFramework="net472" />
<package id="NAudio" version="2.2.1" targetFramework="net472" />
<package id="NAudio.Asio" version="2.2.1" targetFramework="net472" />