Skip to content

htkzr80s/WebpEncoder.NET

Repository files navigation

WebpEncoder.dll

A lightweight and simple native DLL wrapper for Google's libwebp, allowing basic WebP encoding from C# and other .NET environments.

Note

This project (including the wrapper code and C# examples) was automatically generated by AI. It is highly recommended to perform thorough testing and implement proper error handling before using it in a production environment.

Key Features

  • libwebp Wrapper: Statically links libwebp 1.6.0.
  • Self-Contained: No additional external runtime dependencies required.
  • Designed for .NET: Supports raw BGRA/RGBA pixel data input (suitable for WriteableBitmap, etc.).
  • Minimal API: Provides straightforward encoding functions without complex configurations.

Usage (C# Example)

Below is an example of P/Invoke definitions and usage for integrating this DLL into a C# project.

internal static partial class WebpEncoder
{
    private const string DllName = "WebpEncoder.dll";

    // WebP Encode (BGRA Input)
    [LibraryImport(DllName)]
    [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
    public static partial int EncodeBGRAtoWebP(
        IntPtr bgra, // Pointer to raw BGRA pixel data
        int width,
        int height,
        int stride,
        float quality,
        int lossless,
        out IntPtr output,
        out nuint outputSize
    );

    // WebP Encode (RGBA Input)
    [LibraryImport(DllName)]
    [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
    public static partial int EncodeRGBAtoWebP(
        IntPtr rgba, // Pointer to raw RGBA pixel data
        int width,
        int height,
        int stride,
        float quality,
        int lossless,
        out IntPtr output,
        out nuint outputSize
    );

    // Free allocated memory on the native side
    [LibraryImport(DllName)]
    [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
    public static partial void FreeWebP(IntPtr data);
}

// Usage Example
// 1. Prepare your raw RGBA pixel data as a byte array (or pointer)
byte[] rgba = GetRawRgbaPixels(); 
int width = 1920;
int height = 1080;
int stride = width * 4;
float quality = 90.0f;
int lossless = 0; // 0 for lossy, 1 for lossless

// 2. Call the native WebP encoder
int ok = WebpEncoder.EncodeRGBAtoWebP(
    rgba, width, height, stride, quality, lossless,
    out IntPtr nativePtr, out nuint outputSize
);

if (ok == 1 && outputSize > 0)
{
    // 3. Copy the encoded native bytes back to managed C# memory
    byte[] webpData = new byte[(int)outputSize];
    Marshal.Copy(nativePtr, webpData, 0, (int)outputSize);

    // 4. CRITICAL: Always free the native memory to prevent memory leaks!
    WebpEncoder.FreeWebP(nativePtr);

    // 5. Save or use your WebP data
    File.WriteAllBytes("output.webp", webpData);
}

Important Notes

  • The output parameter is a pointer-to-pointer (e.g., out IntPtr in C#) used to receive the allocated memory address.
  • The output buffer is allocated internally by the libwebp library.
  • Ensure FreeWebP is always executed after usage to properly release memory.
  • The stride parameter represents the data width of a single row in bytes.
  • For 4-channel (RGBA/BGRA) images, the stride is typically calculated as width * 4.
  • Providing an incorrect or mismatched pixel format will result in a corrupted output image.

Common Use Cases

  • Instantly saving screenshots as WebP files.
  • Speeding up basic image conversion tools.
  • Calling lightweight native processing directly from .NET applications.

Documentation

For more comprehensive details, please refer to the following documents:

Limitations

As this is a very thin wrapper, advanced features such as metadata handling or detailed encoding configurations are currently unsupported.


Third-Party Software Notices & Licenses

This project statically links the official libwebp library provided by Google.

License

This project is licensed under the MIT License - see the LICENSE file for details. Copyright (c) 2026 htkz80s

================================================================================

JAPANESE README SECTION / 日本語ドキュメント

Googleの libwebp をC#などの.NET環境から呼び出すための、シンプルなネイティブDLLラッパー。

Note

このプロジェクト(ラッパー部分、およびC#のサンプルコード)は、AIによって自動生成されたものです。実際の利用環境に合わせた検証やエラーハンドリングの追加を推奨します。

主な機能

  • libwebpのラップ: libwebp 1.6.0 を静的リンク。
  • 依存関係の集約: ランタイム時の追加外部依存は不要。
  • .NET向けの設計: BGRA/RGBAピクセルデータ(WriteableBitmap など)の処理に対応。
  • 最小限のAPI: 複雑な設定を省いた単純なエンコード機能。

使い方 (C#の実装例)

このDLLをC#プロジェクトで利用するためのP/Invoke定義と実装例。

internal static partial class WebpEncoder
{
    private const string DllName = "WebpEncoder.dll";

    // WebP Encode (BGRA Input)
    [LibraryImport(DllName)]
    [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
    public static partial int EncodeBGRAtoWebP(
        IntPtr bgra, // Pointer to raw BGRA pixel data
        int width,
        int height,
        int stride,
        float quality,
        int lossless,
        out IntPtr output,
        out nuint outputSize
    );

    // WebP Encode (RGBA Input)
    [LibraryImport(DllName)]
    [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
    public static partial int EncodeRGBAtoWebP(
        IntPtr rgba, // Pointer to raw RGBA pixel data
        int width,
        int height,
        int stride,
        float quality,
        int lossless,
        out IntPtr output,
        out nuint outputSize
    );

    // Free allocated memory in native side
    [LibraryImport(DllName)]
    [UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
    public static partial void FreeWebP(IntPtr data);
}

// 実装例
// 1. 変換元のRGBAピクセルデータをバイト配列等で用意
byte[] rgba = GetRawRgbaPixels(); 
int width = 1920;
int height = 1080;
int stride = width * 4;
float quality = 90.0f;
int lossless = 0; // 0: ロッシー圧縮 / 1: ロスレス圧縮

// 2. ネイティブのWebPエンコーダを呼び出す
int ok = WebpEncoder.EncodeRGBAtoWebP(
    rgba, width, height, stride, quality, lossless,
    out IntPtr nativePtr, out nuint outputSize
);

if (ok == 1 && outputSize > 0)
{
    // 3. エンコードされたネイティブ側のバイトデータをC#のメモリにコピー
    byte[] webpData = new byte[(int)outputSize];
    Marshal.Copy(nativePtr, webpData, 0, (int)outputSize);

    // 4. 重要: メモリリークを防ぐため、必ずネイティブメモリを解放してください!
    WebpEncoder.FreeWebP(nativePtr);

    // 5. 取得したWebPデータを保存、または利用
    File.WriteAllBytes("output.webp", webpData);
}

注意点

  • output はアドレスを受け取るためのポインタのポインタ(C#では out IntPtr 等)
  • output は libwebp 側で確保される
  • 使用後は必ず FreeWebP で解放すること
  • stride は「1行あたりのバイト数」
  • 4チャンネル(RGBA/BGRA)画像の場合、基本的には stride = width * 4 となる
  • ピクセルフォーマットが一致していないと壊れた画像になる

使いどころ

  • スクリーンショットの即時WebP保存
  • 画像変換ツールの高速化
  • .NETアプリからの軽量ネイティブ処理呼び出し

ドキュメント

詳細は下記ドキュメントを参照してください:

メモ

かなり薄いラッパーなので、高度な設定(メタデータ保持・詳細なエンコードパラメータチューニングなど)は未対応です。


サードパーティ製ソフトウェアの通知とライセンス

本プロジェクトは、Googleが提供する公式の libwebp ライブラリを静的リンクしています。

ライセンス

本プロジェクトはMITライセンスのもとで公開されています。詳細は同梱の LICENSE ファイルをご確認ください。 Copyright (c) 2026 htkzr80s

About

​A lightweight native DLL wrapper for Google's libwebp, enabling fast WebP encoding in C# / .NET.

Topics

Resources

License

Stars

Watchers

Forks

Contributors