Skip to content

Commit 17f52a0

Browse files
committed
Add project files.
1 parent 2464809 commit 17f52a0

33 files changed

Lines changed: 2772 additions & 0 deletions

BCnEnc.Net/AssemblyInfo.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("BCnEncTests")]

BCnEnc.Net/BCnEnc.Net.csproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
9+
</PropertyGroup>
10+
11+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
12+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Accord.Statistics" Version="3.8.0" />
17+
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" />
18+
</ItemGroup>
19+
20+
</Project>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.IO;
3+
using System.Runtime.InteropServices;
4+
using BCnComp.Net.Shared;
5+
6+
namespace BCnComp.Net.Decoder
7+
{
8+
internal interface IBcBlockDecoder {
9+
RawBlock4X4Rgba32[,] Decode(ReadOnlySpan<byte> data, int pixelWidth, int pixelHeight, out int blockWidth,
10+
out int blockHeight);
11+
}
12+
13+
internal class Bc1NoAlphaDecoder : IBcBlockDecoder {
14+
public RawBlock4X4Rgba32[,] Decode(ReadOnlySpan<byte> data, int pixelWidth, int pixelHeight, out int blockWidth, out int blockHeight) {
15+
blockWidth = (int)MathF.Ceiling(pixelWidth / 4.0f);
16+
blockHeight = (int)MathF.Ceiling(pixelHeight / 4.0f);
17+
18+
if (data.Length != (blockWidth * blockHeight * Marshal.SizeOf<Bc1Block>())) {
19+
throw new InvalidDataException();
20+
}
21+
22+
var encodedBlocks = MemoryMarshal.Cast<byte, Bc1Block>(data);
23+
24+
RawBlock4X4Rgba32[,] output = new RawBlock4X4Rgba32[blockWidth, blockHeight];
25+
26+
for (int x = 0; x < blockWidth; x++) {
27+
for (int y = 0; y < blockHeight; y++) {
28+
output[x, y] = encodedBlocks[x + y * blockWidth].Decode(false);
29+
}
30+
}
31+
32+
return output;
33+
}
34+
}
35+
36+
internal class Bc1ADecoder : IBcBlockDecoder {
37+
public RawBlock4X4Rgba32[,] Decode(ReadOnlySpan<byte> data, int pixelWidth, int pixelHeight, out int blockWidth, out int blockHeight) {
38+
blockWidth = (int)MathF.Ceiling(pixelWidth / 4.0f);
39+
blockHeight = (int)MathF.Ceiling(pixelHeight / 4.0f);
40+
41+
if (data.Length != (blockWidth * blockHeight * Marshal.SizeOf<Bc1Block>())) {
42+
throw new InvalidDataException();
43+
}
44+
45+
var encodedBlocks = MemoryMarshal.Cast<byte, Bc1Block>(data);
46+
47+
RawBlock4X4Rgba32[,] output = new RawBlock4X4Rgba32[blockWidth, blockHeight];
48+
49+
for (int x = 0; x < blockWidth; x++) {
50+
for (int y = 0; y < blockHeight; y++) {
51+
output[x, y] = encodedBlocks[x + y * blockWidth].Decode(true);
52+
}
53+
}
54+
55+
return output;
56+
}
57+
}
58+
}

BCnEnc.Net/Decoder/BcDecoder.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using BCnComp.Net.Shared;
3+
using SixLabors.ImageSharp;
4+
using SixLabors.ImageSharp.PixelFormats;
5+
6+
namespace BCnComp.Net.Decoder
7+
{
8+
public class BcDecoder
9+
{
10+
private IBcBlockDecoder GetDecoder(GlInternalFormat format) {
11+
switch (format) {
12+
case GlInternalFormat.GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
13+
return new Bc1NoAlphaDecoder();
14+
case GlInternalFormat.GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
15+
return new Bc1ADecoder();
16+
default:
17+
return null;
18+
}
19+
}
20+
public Image<Rgba32> Decode(KtxFile file) {
21+
var decoder = GetDecoder(file.Header.GlInternalFormat);
22+
if (decoder == null) {
23+
throw new NotSupportedException($"This format is not supported: {file.Header.GlInternalFormat}");
24+
}
25+
26+
var data = file.MipMaps[0].Faces[0].Data;
27+
var pixelWidth = file.MipMaps[0].Width;
28+
var pixelHeight = file.MipMaps[0].Height;
29+
30+
var blocks = decoder.Decode(data, (int)pixelWidth, (int)pixelHeight, out var blockWidth, out var blockHeight);
31+
32+
return ImageToBlocks.ImageFromRawBlocks(blocks, blockWidth, blockHeight);
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)