Skip to content

Commit

Permalink
Merge pull request #6 from Nominom/dev-upgrade-imagesharp
Browse files Browse the repository at this point in the history
Version 1.2.3
  • Loading branch information
Nominom authored Oct 20, 2020
2 parents 6d6aa6f + 281d3c8 commit c5a9e61
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 45 deletions.
6 changes: 3 additions & 3 deletions BCnEnc.Net/BCnEncoder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageLicenseExpression>MIT OR Unlicense</PackageLicenseExpression>
<Copyright />
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.2.2</Version>
<Version>1.2.3</Version>
<Authors>Nominom</Authors>
<Company />
<Product>BCnEncoder.Net</Product>
Expand All @@ -31,7 +31,7 @@ Supported formats are:
<RepositoryType>git</RepositoryType>
<PackageTags>BCn BC BC1 BC2 BC3 BC4 BC5 BC7 BPTC RGTC S3TC DXT1 DXT3 DXT5 ktx dds texture compression encoding decoding decompression image gpu</PackageTags>
<PackageProjectUrl>https://github.com/Nominom/BCnEncoder.NET</PackageProjectUrl>
<PackageReleaseNotes>Changed EncodingQuality to CompressionQuality.</PackageReleaseNotes>
<PackageReleaseNotes>Upgraded ImageSharp dependency from 1.0.0 beta 7 to version 1.0.1</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand All @@ -44,7 +44,7 @@ Supported formats are:
</PropertyGroup>

<ItemGroup>
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.0-beta0007" />
<PackageReference Include="SixLabors.ImageSharp" Version="1.0.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

Expand Down
16 changes: 12 additions & 4 deletions BCnEnc.Net/Decoder/BcDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ public Image<Rgba32> Decode(KtxFile file)

var image = new Image<Rgba32>((int)pixelWidth, (int)pixelHeight);
var output = decoder.Decode(data, (int)pixelWidth, (int)pixelHeight);
var pixels = image.GetPixelSpan();
if (!image.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}

output.CopyTo(pixels);
return image;
Expand Down Expand Up @@ -299,7 +301,9 @@ public Image<Rgba32>[] DecodeAllMipMaps(KtxFile file)

var image = new Image<Rgba32>((int)pixelWidth, (int)pixelHeight);
var output = decoder.Decode(data, (int)pixelWidth, (int)pixelHeight);
var pixels = image.GetPixelSpan();
if (!image.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}

output.CopyTo(pixels);
images[mip] = image;
Expand Down Expand Up @@ -345,7 +349,9 @@ public Image<Rgba32> Decode(DdsFile file)

var image = new Image<Rgba32>((int)pixelWidth, (int)pixelHeight);
var output = decoder.Decode(data, (int)pixelWidth, (int)pixelHeight);
var pixels = image.GetPixelSpan();
if (!image.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}

output.CopyTo(pixels);
return image;
Expand Down Expand Up @@ -393,7 +399,9 @@ public Image<Rgba32>[] DecodeAllMipMaps(DdsFile file)

var image = new Image<Rgba32>((int) pixelWidth, (int) pixelHeight);
var output = decoder.Decode(data, (int) pixelWidth, (int) pixelHeight);
var pixels = image.GetPixelSpan();
if (!image.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}

output.CopyTo(pixels);
images[mip] = image;
Expand Down
30 changes: 24 additions & 6 deletions BCnEnc.Net/Encoder/BcEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ public KtxFile EncodeToKtx(Image<Rgba32> inputImage)
}
else
{
encoded = uncompressedEncoder.Encode(mipChain[i].GetPixelSpan());
if (!mipChain[i].TryGetSinglePixelSpan(out var mipPixels)) {
throw new Exception("Cannot get pixel span.");
}
encoded = uncompressedEncoder.Encode(mipPixels);
}

output.MipMaps.Add(new KtxMipmap((uint)encoded.Length,
Expand Down Expand Up @@ -264,7 +267,10 @@ public DdsFile EncodeToDds(Image<Rgba32> inputImage)
}
else
{
encoded = uncompressedEncoder.Encode(mipChain[mip].GetPixelSpan());
if (!mipChain[mip].TryGetSinglePixelSpan(out var mipPixels)) {
throw new Exception("Cannot get pixel span.");
}
encoded = uncompressedEncoder.Encode(mipPixels);
}

if (mip == 0)
Expand Down Expand Up @@ -334,7 +340,10 @@ public List<byte[]> EncodeToRawBytes(Image<Rgba32> inputImage)
}
else
{
encoded = uncompressedEncoder.Encode(mipChain[i].GetPixelSpan());
if (!mipChain[i].TryGetSinglePixelSpan(out var mipPixels)) {
throw new Exception("Cannot get pixel span.");
}
encoded = uncompressedEncoder.Encode(mipPixels);
}

output.Add(encoded);
Expand Down Expand Up @@ -400,7 +409,10 @@ public byte[] EncodeToRawBytes(Image<Rgba32> inputImage, int mipLevel, out int m
}
else
{
encoded = uncompressedEncoder.Encode(mipChain[mipLevel].GetPixelSpan());
if (!mipChain[mipLevel].TryGetSinglePixelSpan(out var mipPixels)) {
throw new Exception("Cannot get pixel span.");
}
encoded = uncompressedEncoder.Encode(mipPixels);
}

mipWidth = mipChain[mipLevel].Width;
Expand Down Expand Up @@ -506,7 +518,10 @@ public KtxFile EncodeCubeMapToKtx(Image<Rgba32> right, Image<Rgba32> left, Image
}
else
{
encoded = uncompressedEncoder.Encode(mipChain[i].GetPixelSpan());
if (!mipChain[i].TryGetSinglePixelSpan(out var mipPixels)) {
throw new Exception("Cannot get pixel span.");
}
encoded = uncompressedEncoder.Encode(mipPixels);
}

if (f == 0)
Expand Down Expand Up @@ -603,7 +618,10 @@ public DdsFile EncodeCubeMapToDds(Image<Rgba32> right, Image<Rgba32> left, Image
}
else
{
encoded = uncompressedEncoder.Encode(mipChain[mip].GetPixelSpan());
if (!mipChain[mip].TryGetSinglePixelSpan(out var mipPixels)) {
throw new Exception("Cannot get pixel span.");
}
encoded = uncompressedEncoder.Encode(mipPixels);
}

if (mip == 0)
Expand Down
15 changes: 12 additions & 3 deletions BCnEnc.Net/Shared/ImageToBlocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ internal static RawBlock4X4Rgba32[] ImageTo4X4(ImageFrame<Rgba32> image, out int
blocksWidth = (int)MathF.Ceiling(image.Width / 4.0f);
blocksHeight = (int)MathF.Ceiling(image.Height / 4.0f);
RawBlock4X4Rgba32[] output = new RawBlock4X4Rgba32[blocksWidth * blocksHeight];
Span<Rgba32> pixels = image.GetPixelSpan();

if (!image.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}

for (int y = 0; y < image.Height; y++)
{
Expand Down Expand Up @@ -75,7 +78,10 @@ internal static Image<Rgba32> ImageFromRawBlocks(RawBlock4X4Rgba32[,] blocks, in
internal static Image<Rgba32> ImageFromRawBlocks(RawBlock4X4Rgba32[,] blocks, int blocksWidth, int blocksHeight, int pixelWidth, int pixelHeight)
{
Image<Rgba32> output = new Image<Rgba32>(pixelWidth, pixelHeight);
Span<Rgba32> pixels = output.GetPixelSpan();

if (!output.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}

for (int y = 0; y < output.Height; y++)
{
Expand All @@ -102,7 +108,10 @@ internal static Image<Rgba32> ImageFromRawBlocks(RawBlock4X4Rgba32[] blocks, int
internal static Image<Rgba32> ImageFromRawBlocks(RawBlock4X4Rgba32[] blocks, int blocksWidth, int blocksHeight, int pixelWidth, int pixelHeight)
{
Image<Rgba32> output = new Image<Rgba32>(pixelWidth, pixelHeight);
Span<Rgba32> pixels = output.GetPixelSpan();

if (!output.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}

for (int y = 0; y < output.Height; y++)
{
Expand Down
40 changes: 20 additions & 20 deletions BCnEncTests/BC1Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public void Decode() {
block[15] = 0;

var raw = block.Decode(false);
Assert.Equal(Rgba32.Black, raw.p00);
Assert.Equal(Rgba32.Black, raw.p10);
Assert.Equal(Rgba32.Black, raw.p20);
Assert.Equal(Rgba32.Black, raw.p30);
Assert.Equal(new Rgba32(0, 0, 0), raw.p00);
Assert.Equal(new Rgba32(0, 0, 0), raw.p10);
Assert.Equal(new Rgba32(0, 0, 0), raw.p20);
Assert.Equal(new Rgba32(0, 0, 0), raw.p30);

Assert.Equal(new Rgba32(85, 85, 85), raw.p01);
Assert.Equal(new Rgba32(85, 85, 85), raw.p11);
Expand All @@ -50,10 +50,10 @@ public void Decode() {
Assert.Equal(new Rgba32(170, 170, 170), raw.p22);
Assert.Equal(new Rgba32(170, 170, 170), raw.p32);

Assert.Equal(Rgba32.White, raw.p03);
Assert.Equal(Rgba32.White, raw.p13);
Assert.Equal(Rgba32.White, raw.p23);
Assert.Equal(Rgba32.White, raw.p33);
Assert.Equal(new Rgba32(255, 255, 255), raw.p03);
Assert.Equal(new Rgba32(255, 255, 255), raw.p13);
Assert.Equal(new Rgba32(255, 255, 255), raw.p23);
Assert.Equal(new Rgba32(255, 255, 255), raw.p33);
}

[Fact]
Expand Down Expand Up @@ -89,20 +89,20 @@ public void DecodeBlack() {
Assert.Equal(new Rgba32(206, 203, 206), raw.p20);
Assert.Equal(new Rgba32(206, 203, 206), raw.p30);

Assert.Equal(Rgba32.Black, raw.p01);
Assert.Equal(Rgba32.Black, raw.p11);
Assert.Equal(Rgba32.Black, raw.p21);
Assert.Equal(Rgba32.Black, raw.p31);
Assert.Equal(new Rgba32(0, 0, 0), raw.p01);
Assert.Equal(new Rgba32(0, 0, 0), raw.p11);
Assert.Equal(new Rgba32(0, 0, 0), raw.p21);
Assert.Equal(new Rgba32(0, 0, 0), raw.p31);

Assert.Equal(new Rgba32(230, 228, 230), raw.p02);
Assert.Equal(new Rgba32(230, 228, 230), raw.p12);
Assert.Equal(new Rgba32(230, 228, 230), raw.p22);
Assert.Equal(new Rgba32(230, 228, 230), raw.p32);

Assert.Equal(Rgba32.White, raw.p03);
Assert.Equal(Rgba32.White, raw.p13);
Assert.Equal(Rgba32.White, raw.p23);
Assert.Equal(Rgba32.White, raw.p33);
Assert.Equal(new Rgba32(255, 255, 255), raw.p03);
Assert.Equal(new Rgba32(255, 255, 255), raw.p13);
Assert.Equal(new Rgba32(255, 255, 255), raw.p23);
Assert.Equal(new Rgba32(255, 255, 255), raw.p33);
}

[Fact]
Expand Down Expand Up @@ -148,10 +148,10 @@ public void DecodeAlpha() {
Assert.Equal(new Rgba32(230, 228, 230), raw.p22);
Assert.Equal(new Rgba32(230, 228, 230), raw.p32);

Assert.Equal(Rgba32.White, raw.p03);
Assert.Equal(Rgba32.White, raw.p13);
Assert.Equal(Rgba32.White, raw.p23);
Assert.Equal(Rgba32.White, raw.p33);
Assert.Equal(new Rgba32(255, 255, 255), raw.p03);
Assert.Equal(new Rgba32(255, 255, 255), raw.p13);
Assert.Equal(new Rgba32(255, 255, 255), raw.p23);
Assert.Equal(new Rgba32(255, 255, 255), raw.p33);
}

}
Expand Down
17 changes: 12 additions & 5 deletions BCnEncTests/BlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public void PaddingColor()
{
using Image<Rgba32> testImage = new Image<Rgba32>(13, 13);

var pixels = testImage.GetPixelSpan();
if (!testImage.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}
for (int i = 0; i < pixels.Length; i++) {
pixels[i] = Rgba32.Aquamarine;
pixels[i] = new Rgba32(0, 125, 125);
}

var blocks = ImageToBlocks.ImageTo4X4(testImage.Frames[0], out var blocksWidth, out var blocksHeight);
Expand All @@ -52,7 +54,7 @@ public void PaddingColor()
for (int x = 0; x < blocksWidth; x++) {
for (int y = 0; y < blocksHeight; y++) {
foreach (var color in blocks[x + y * blocksWidth].AsSpan) {
Assert.Equal(Rgba32.Aquamarine, color);
Assert.Equal(new Rgba32(0, 125, 125), color);
}
}
}
Expand All @@ -64,7 +66,9 @@ public void BlocksToImage()
Random r = new Random(0);
using Image<Rgba32> testImage = new Image<Rgba32>(16, 16);

var pixels = testImage.GetPixelSpan();
if (!testImage.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}
for (int i = 0; i < pixels.Length; i++) {
pixels[i] = new Rgba32(
(byte)r.Next(255),
Expand All @@ -80,7 +84,10 @@ public void BlocksToImage()
Assert.Equal(4, blocksHeight);

using var output = ImageToBlocks.ImageFromRawBlocks(blocks, blocksWidth, blocksHeight);
var pixels2 = output.GetPixelSpan();

if (!output.TryGetSinglePixelSpan(out var pixels2)) {
throw new Exception("Cannot get pixel span.");
}

Assert.Equal(pixels.Length, pixels2.Length);
for (int i = 0; i < pixels.Length; i++) {
Expand Down
6 changes: 5 additions & 1 deletion BCnEncTests/ClusterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ public class ClusterTests
[Fact]
public void Clusterize() {
using var testImage = ImageLoader.testBlur1.Clone();
var pixels = testImage.GetPixelSpan();

if (!testImage.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}

int numClusters = (testImage.Width / 32) * (testImage.Height / 32);

var clusters = LinearClustering.ClusterPixels(pixels, testImage.Width, testImage.Height, numClusters, 10, 10);
Expand Down
5 changes: 4 additions & 1 deletion BCnEncTests/DdsReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public void ReadBc1a() {
Assert.Equal((uint)image.Width, file.Header.dwWidth);
Assert.Equal((uint)image.Height, file.Header.dwHeight);

Assert.Contains(image.GetPixelSpan().ToArray(), x => x.A == 0);
if (!image.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}
Assert.Contains(pixels.ToArray(), x => x.A == 0);

using FileStream outFs = File.OpenWrite($"decoding_test_dds_bc1a.png");
image.SaveAsPng(outFs);
Expand Down
9 changes: 7 additions & 2 deletions BCnEncTests/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ public static float DecodeCheckPSNR(string filename, Image<Rgba32> original) {
var ktx = KtxFile.Load(fs);
var decoder = new BcDecoder();
using var img = decoder.Decode(ktx);
var pixels = original.GetPixelSpan();
var pixels2 = img.GetPixelSpan();

if (!original.TryGetSinglePixelSpan(out var pixels)) {
throw new Exception("Cannot get pixel span.");
}
if (!img.TryGetSinglePixelSpan(out var pixels2)) {
throw new Exception("Cannot get pixel span.");
}

return ImageQuality.PeakSignalToNoiseRatio(pixels, pixels2, true);
}
Expand Down

0 comments on commit c5a9e61

Please sign in to comment.