SubtitleEdit/subtitleedit · error · InvalidOperationException

Unable to access bitmap pixel data.

Error message

Unable to access bitmap pixel data.

What it means

Thrown when an SkiaSharp SKBitmap, freshly allocated with dimensions w×h and Rgba8888/Unpremul colortype, returns a zero pointer from GetPixels(). The BluRaySupParser decodes PGS subtitle fragments straight into the bitmap's native pixel buffer via an unsafe Span, so a null pointer means there is no writable backing store to write decoded RGBA into.

Source

Thrown at src/libse/BluRaySup/BluRaySupParser.cs:188

                {
                    return new SKBitmap(1, 1);
                }

                var w = data[0].Width;
                var h = data[0].Height;

                if (w <= 0 || h <= 0 || data[0].Fragment?.ImageBuffer?.Length == 0)
                {
                    return new SKBitmap(1, 1);
                }

                // Unpremul (not Premul) as FillPixels/PutPixelFast below write straight, non-premultiplied RGBA.
                // Opaque here would make Skia drop the alpha channel when encoding (transparent areas turn black).
                var bm = new SKBitmap(w, h, SKColorType.Rgba8888, SKAlphaType.Unpremul);
                var pixelPtr = bm.GetPixels(); // Writable pixel buffer
                if (pixelPtr == IntPtr.Zero)
                {
                    throw new InvalidOperationException("Unable to access bitmap pixel data.");
                }

                unsafe
                {
                    var buf = data[0].Fragment.ImageBuffer;
                    var pixelData = new Span<byte>(pixelPtr.ToPointer(), bm.ByteCount); // Create writable span
                    var pal = DecodePalette(palettes);

                    var ofs = 0;
                    var xpos = 0;

                    for (var index = 0; index < buf.Length;)
                    {
                        var b = buf[index++] & 0xff;

                        if (b == 0 && index < buf.Length)
                        {
                            b = buf[index++] & 0xff;

View on GitHub (pinned to 17a9f07487)

Solutions

  1. Validate w and h against a sane upper bound before allocating the bitmap and return the 1×1 fallback instead of throwing.
  2. Ensure SKBitmap is disposed after use to release native memory (use using/finally around each decode).
  3. Upgrade/downgrade SkiaSharp to a version known to allocate pixel buffers for the target runtime.
  4. Catch InvalidOperationException at the caller and surface a user-friendly 'cannot render this subtitle' message rather than crashing the import.

Example fix

// before
var bm = new SKBitmap(w, h, SKColorType.Rgba8888, SKAlphaType.Unpremul);
var pixelPtr = bm.GetPixels();
if (pixelPtr == IntPtr.Zero)
{
    throw new InvalidOperationException("Unable to access bitmap pixel data.");
}

// after
const int MaxDim = 4096;
if (w > MaxDim || h > MaxDim)
{
    return new SKBitmap(1, 1);
}
using var bm = new SKBitmap(w, h, SKColorType.Rgba8888, SKAlphaType.Unpremul);
var pixelPtr = bm.GetPixels();
if (pixelPtr == IntPtr.Zero)
{
    bm.Dispose();
    return new SKBitmap(1, 1);
}
Defensive patterns

Strategy: validation

Validate before calling

const int MaxDim = 4096;
if (w <= 0 || h <= 0 || w > MaxDim || h > MaxDim) { /* skip decode */ }

Try / catch

try { var bm = parser.Decode(...); }
catch (InvalidOperationException ex) when (ex.Message.Contains("bitmap pixel data")) { /* log + skip subtitle */ }

Prevention

When it happens

Trigger: Constructing a very large SKBitmap that exhausts native memory; running on a platform where SkiaSharp cannot pin/allocate the pixel buffer (e.g. constrained GPU/SkiaSharp build); w/h coming from a malformed PGS packet that passes the >0 guard but yields an allocation failure.

Common situations: Corrupt or hand-edited Blu-ray SUP files with oversized dimension fields; low-memory containers; mismatched SkiaSharp version where GetPixels() behavior differs; processing many subtitles in a batch without disposing bitmaps (native heap pressure).

Related errors


AI-assisted analysis of SubtitleEdit/subtitleedit@17a9f07487 (2026-08-13). Data as JSON: /api/errors/af773a61f20f66b7. Report an issue: GitHub.