BCnEncoder.NET

repository·master·Indexed 41 days ago

https://github.com/nominom/bcnencoder.net

A cross-platform .NET Standard 2.0 and 2.1 library for compressing and decompressing RGBA images into block-compressed (BCn/DXT) formats. Supports BC1 through BC7, including experimental BC6H HDR support, and outputs to KTX and DDS file formats. Provides optional extension packages for SixLabors.ImageSharp integration.

Tokens
1.4K
Snippets
3
Records
6
Agent score
50%

What's inside BCnEncoder.NET

  1. Overview of BCnEncoder.NET

    master

    BCnEncoder.NET is a cross-platform library for compressing RGBA images into various block-compressed formats. It is compatible with .NET Standard 2.0 and 2.1 and has no native dependencies.

    Supported Formats:

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

    Output File Formats:

    • KTX
    • DDS
  2. Install BCnEncoder.NET and ImageSharp extensions

    master

    To use the library with standard .NET projects, install the core package. If you wish to work with SixLabors.ImageSharp types directly using extension methods, you must also install the BCnEncoder.Net.ImageSharp extension package.

    Dependencies:

    • CommunityToolkit.HighPerformance (required for Span2D and Memory2D types).
    • For .NET Standard 2.0 support, PolySharp and Microsoft.Bcl.Numerics are used.
  3. Migrating from 1.x to 2.0

    master
    In version 2.0, ImageSharp was removed as a core dependency. Existing code relying on ImageSharp types will break. To restore this functionality, you must explicitly install the BCnEncoder.Net.ImageSharp extension package.
  4. Encode and decode HDR images with BC6H

    master

    The library provides experimental support for HDR images using the HdrImage class (which handles Radiance HDR files) and the Bc6U format.

    Encoding HDR: Use encoder.EncodeToStreamHdr passing the PixelMemory from an HdrImage. Decoding HDR: Use decoder.DecodeHdr2D to get a Memory2D<ColorRgbFloat>, which can then be wrapped in a new HdrImage object.

    // Encoding HDR
    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;
    
    using FileStream fs = File.OpenWrite("example.ktx");
    encoder.EncodeToStreamHdr(image.PixelMemory, fs);
    
    // Decoding HDR
    using FileStream fsIn = File.OpenRead("compressed_bc6.ktx");
    BcDecoder decoder = new BcDecoder();
    Memory2D<ColorRgbFloat> pixels = decoder.DecodeHdr2D(fsIn);
    
    HdrImage decodedImage = new HdrImage(pixels.Span);
    using FileStream outFs = File.OpenWrite("decoded.hdr");
    decodedImage.Write(outFs);
  5. Decode a compressed KTX image to PNG

    master

    To decode a compressed image (e.g., BC1) from a KTX file back into a standard RGBA32 ImageSharp image, use the BcDecoder class.

    using BCnEncoder.Encoder;
    using BCnEncoder.Decoder;
    using BCnEncoder.Shared;
    using BCnEncoder.ImageSharp;
    using SixLabors.ImageSharp;
    using SixLabors.ImageSharp.PixelFormats;
    
    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);
  6. Encode an image to BC1 using ImageSharp

    master

    To encode a PNG image to the BC1 format and save it as a KTX file, use the BcEncoder class along with the BCnEncoder.ImageSharp extension methods. You can configure GenerateMipMaps, Quality, Format, and FileFormat via encoder.OutputOptions.

    using BCnEncoder.Encoder;
    using BCnEncoder.Decoder;
    using BCnEncoder.Shared;
    using BCnEncoder.ImageSharp;
    using SixLabors.ImageSharp;
    using SixLabors.ImageSharp.PixelFormats;
    
    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 OutputFileFormat.Dds for a dds file.
    
    using FileStream fs = File.OpenWrite("example.ktx");
    encoder.EncodeToStream(image, fs);