BlurHash.NET

repository·main·Indexed 41 days ago

https://github.com/markuspalcer/blurhash.net

A .NET implementation of the BlurHash algorithm for generating compact string representations of images to be used as lightweight, blurred placeholders. It provides core functionality via Blurhash.Core and specialized bridges for System.Drawing.Common and SixLabors.ImageSharp, including support for IServiceCollection dependency injection.

Tokens
786
Snippets
0
Records
6
Agent score
2%

What's inside BlurHash.NET

  1. What is BlurHash and how does it work?

    main

    BlurHash is a compact representation of an image placeholder. It converts an image into a short string (typically 20-30 characters) that can be stored in a database (e.g., as a JSON field).

    Workflow:

    1. Backend: Encode the image into a BlurHash string and store it alongside the image URL.
    2. Client: Send both the URL and the BlurHash string to the client.
    3. Client: Decode the string into a low-resolution placeholder image to display while the high-resolution image loads over the network.
  2. Encode an image using Blurhash.Core (Unsupported Libraries)

    main

    If your library is not explicitly supported, you can use Blurhash.Core by converting your image pixels manually. There are two methods:

    Method 1: Full Array Encoding

    1. Convert your image into a two-dimensional array of Blurhash.Pixel (where the first dimension is the X-Axis and the second is the Y-Axis).
    2. Call Blurhash.Core.Encode with this array. Note: This requires holding all pixels in memory at once.

    Method 2: Streamed Encoding (Memory Efficient)

    1. Create a StreamedEncoder instance.
    2. Convert pixels into Span<Blurhash.StreamedPixel> (where StreamedPixel includes X and Y coordinates).
    3. Call StreamedEncoder.Process with these spans.
    4. Call StreamedEncoder.Finish to retrieve the final BlurHash string.
  3. Decode a Blurhash string using Blurhash.Core (Unsupported Libraries)

    main

    To decode a string into an image format your library supports using Blurhash.Core, use one of these two methods:

    Method 1: Full Array Decoding

    1. Create a two-dimensional Pixel array representing the desired width and height.
    2. Call Blurhash.Core.Decode using the array and the input string.
    3. Convert the resulting Pixel array into your library's specific image format. Note: This requires allocating an array for all pixels at once.

    Method 2: Streamed Decoding (Memory Efficient)

    1. Create a StreamedDecoder instance with a callback function.
    2. In the callback, write the reported pixels directly to your image object.
    3. Call StreamedDecoder.Run to begin the decoding process.
  4. Use Blurhash.NET with IServiceCollection

    main

    For dependency injection in .NET applications, install the Microsoft Extensions version of your preferred library package:

    • For System.Drawing.Common: Install Blurhash.Microsoft.Extensions.System.Drawing.
    • For SixLabors.ImageSharp: Install Blurhash.Microsoft.Extensions.ImageSharp.
    • For manual pixel conversion: Install Blurhash.Microsoft.Extensions.Core.
  5. Install Blurhash.NET for supported image libraries

    main

    If you are using a standard image manipulation library, install the corresponding NuGet package to access the Blurhasher.Encode and Blurhasher.Decode methods directly.

    Standard Usage:

    • For System.Drawing.Common: Install Blurhash.System.Drawing.Common.
    • For SixLabors.ImageSharp: Install Blurhash.ImageSharp.
    • For manual pixel conversion: Install Blurhash.Core.
  6. Reference the Blurhash.NET project structure

    main

    The solution consists of the following core projects:

    • Blurhash.Core: The core algorithm (implemented as .NET-Standard for maximum compatibility).
    • Blurhash.System.Drawing.Common: Bridge for System.Drawing.Common.
    • Blurhash.ImageSharp: Bridge for SixLabors.ImageSharp.
    • Blurhash.Microsoft.Extensions.*: Integration packages for IServiceCollection.
    • Benchmarks: Optimization and performance testing.