monarchsolutions - BCnEncoder.NetStd 2.1.0-CI00002

BCnEncoder.NET is a library for compressing rgba images to different block-compressed formats. Both ktx and dds output file-formats are supported. It has no native dependencies and is .NET Standard 2.1 compatible.

Supported formats are: Raw unsigned byte R, RG, RGB and RGBA formats BC1 (S3TC DXT1) BC2 (S3TC DXT3) BC3 (S3TC DXT5) BC4 (RGTC1) BC5 (RGTC2) BC6 (BPTC-FLOAT) BC7 (BPTC)

PM> Install-Package BCnEncoder.NetStd -Version 2.1.0-CI00002 -Source https://www.myget.org/F/monarchsolutions/api/v3/index.json

Copy to clipboard

> nuget.exe install BCnEncoder.NetStd -Version 2.1.0-CI00002 -Source https://www.myget.org/F/monarchsolutions/api/v3/index.json

Copy to clipboard

> dotnet add package BCnEncoder.NetStd --version 2.1.0-CI00002 --source https://www.myget.org/F/monarchsolutions/api/v3/index.json

Copy to clipboard
<PackageReference Include="BCnEncoder.NetStd" Version="2.1.0-CI00002" />
Copy to clipboard
source https://www.myget.org/F/monarchsolutions/api/v3/index.json

nuget BCnEncoder.NetStd  ~> 2.1.0-CI00002
Copy to clipboard

> choco install BCnEncoder.NetStd --version 2.1.0-CI00002 --source https://www.myget.org/F/monarchsolutions/api/v2

Copy to clipboard
Import-Module PowerShellGet
Register-PSRepository -Name "monarchsolutions" -SourceLocation "https://www.myget.org/F/monarchsolutions/api/v2"
Install-Module -Name "BCnEncoder.NetStd" -RequiredVersion "2.1.0-CI00002" -Repository "monarchsolutions" -AllowPreRelease
Copy to clipboard

Nuget Tests

BCnEncoder.NET

A Cross-platform BCn / DXT encoding libary for .NET

What is it?

BCnEncoder.NET is a library for compressing rgba images to different block-compressed formats. It has no native dependencies and is .NET Standard 2.1 compatible.

Supported formats are:

  • Raw unsigned byte R, RG, RGB and RGBA formats
  • BC1 (S3TC DXT1)
  • BC2 (S3TC DXT3)
  • BC3 (S3TC DXT5)
  • BC4 (RGTC1)
  • BC5 (RGTC2)
  • BC6 (BPTC_FLOAT)
  • BC7 (BPTC)

Current state

The current state of this library is in development but quite usable. I'm planning on implementing support for more codecs and different algorithms. The current version is capable of encoding and decoding BC1-BC7 images in both KTX or DDS formats.

Please note, that the API might change between versions.

Dependencies

Current dependencies are:

Image library extensions

This library has extension packages available for the following image libraries:

ImageSharp

The extension packages provide extension methods for ease of use with the image library.

Upgrading to 2.0

If you're upgrading from 1.X.X to version 2, expect some of your exsting code to be broken. ImageSharp was removed as a core dependency in version 2.0, so the code will no longer work with ImageSharp's Image types by default. You can install the extension package for ImageSharp to continue using this library easily with ImageSharp apis.

API

The below examples are using the ImageSharp extension package. For more detailed usage examples, you can go look at the unit tests.

Remember add the following usings to the top of the file:

using BCnEncoder.Encoder;
using BCnEncoder.Decoder;
using BCnEncoder.Shared;
using BCnEncoder.ImageSharp;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;

Here's an example on how to encode a png image to BC1 without alpha, and save it to a file.

using Image<Rgba32> image = Image.Load<Rgba32>("example.png");

BcEncoder encoder = new BcEncoder();

encoder.OutputOptions.GenerateMipMaps = true;
encoder.OutputOptions.Quality = CompressionQuality.Balanced;
encoder.OutputOptions.Format = CompressionFormat.Bc1;
encoder.OutputOptions.FileFormat = OutputFileFormat.Ktx; //Change to Dds for a dds file.

using FileStream fs = File.OpenWrite("example.ktx");
encoder.EncodeToStream(image, fs);

And how to decode a compressed image from a KTX file and save it to png format.

using FileStream fs = File.OpenRead("compressed_bc1.ktx");

BcDecoder decoder = new BcDecoder();
using Image<Rgba32> image = decoder.DecodeToImageRgba32(fs);

using FileStream outFs = File.OpenWrite("decoding_test_bc1.png");
image.SaveAsPng(outFs);

How to encode an HDR image with BC6H. (HdrImage class reads and writes Radiance HDR files. This class is experimental and subject to be removed)

HdrImage image = HdrImage.Read("example.hdr");
			
BcEncoder encoder = new BcEncoder();

encoder.OutputOptions.GenerateMipMaps = true;
encoder.OutputOptions.Quality = CompressionQuality.Balanced;
encoder.OutputOptions.Format = CompressionFormat.Bc6U;
encoder.OutputOptions.FileFormat = OutputFileFormat.Ktx; //Change to Dds for a dds file.

using FileStream fs = File.OpenWrite("example.ktx");
encoder.EncodeToStreamHdr(image.PixelMemory, fs);

How to decode a BC6H encoded file.

using FileStream fs = File.OpenRead("compressed_bc6.ktx");

BcDecoder decoder = new BcDecoder();
Memory2D<ColorRgbFloat> pixels = decoder.DecodeHdr2D(fs);

HdrImage image = new HdrImage(pixels.Span);

using FileStream outFs = File.OpenWrite("decoded.hdr");
image.Write(outFs);

TO-DO

  • BC1 / DXT1 Encoding Without Alpha
  • BC1 / DXT1 Encoding With 1bit of alpha
  • BC2 / DXT3 Encoding
  • BC3 / DXT5 Encoding
  • BC4 Encoding
  • BC5 Encoding
  • BC7 / BPTC Encoding
  • DDS file support
  • Implement PCA to remove Accord.Statistics dependency
  • BC6H HDR Encoding
  • Performance improvements
  • ETC / ETC2 Encoding?

Contributing

All contributions are welcome. I'll try to respond to bug reports and feature requests as fast as possible, but you can also fix things yourself and submit a pull request. Please note, that by submitting a pull request you accept that your code will be dual licensed under MIT and public domain Unlicense.

License

This library is dual-licensed under the Unlicense and MIT licenses.

You may use this code under the terms of either license.

Please note, that any dependencies of this project are licensed under their own respective licenses.

2.0.0 - Removed ImageSharp dependency, added async api methods, added ATC and BGRA support, added new raw api methods, improved dds file handling and fixed some bugs. See the github page for more information about 2.0.

2.1.0 - BC6H Support

  • .NETStandard 2.0
    • Microsoft.Toolkit.HighPerformance (>= 7.1.2)
  • .NETStandard 2.0: 2.0.0.0

Owners

UlyssesWu

Authors

Nominom

Project URL

https://github.com/Nominom/BCnEncoder.NET

License

Unknown

Tags

BCn BC BC1 BC2 BC3 BC4 BC5 BC6 BC6H BC7 BPTC RGTC S3TC DXT1 DXT3 DXT5 ktx dds texture compression encoding decoding decompression image gpu

Info

161 total downloads
60 downloads for version 2.1.0-CI00002
Download (110.84 KB)
Found on the current feed only

Package history

Version Size Last updated Downloads Mirrored?
2.1.0-CI00009 219.09 KB Fri, 18 Jul 2025 19:42:36 GMT 49
2.1.0-CI00008 219.1 KB Fri, 18 Jul 2025 19:29:01 GMT 52
2.1.0-CI00002 110.84 KB Sun, 03 Jul 2022 17:59:23 GMT 60