Skip to main content

sdl2_alpha - Fast Alpha Blending for SDL2

sdl2_alpha provides mathematically correct alpha blending to fix SDL2's broken alpha compositing behavior. Built with Rust and PyO3 for maximum performance.

Why sdl2_alpha?

SDL2's built-in alpha blending has fundamental flaws:

  • Incorrect destination alpha handling
  • Platform-specific premultiplication inconsistencies
  • Accumulation errors that degrade transparency (the dreaded "persistent haze")

sdl2_alpha implements Porter-Duff "over" compositing with proper premultiplied alpha math, delivering visually correct results for composition-heavy applications.

🚀 Development Setup (Complete Newbie Guide)

Prerequisites

You need Rust and Python. If you don't have Rust:

# Install Rust (this installs rustc, cargo, etc.)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/env

# Verify Rust installation
rustc --version
cargo --version

Install maturin (Rust-Python bridge)

pip install maturin

Development Workflow

cd sdl2_alpha/

# Development build (fast compile, not optimized, includes debug info)
maturin develop

# This installs the package in development mode so you can import it immediately
# Changes to Rust code require rebuilding with maturin develop

Release Builds (for production/benchmarking)

cd sdl2_alpha/

# Release build (slow compile, fully optimized)
maturin build --release

# Install the optimized wheel
pip install target/wheels/*.whl --force-reinstall

# OR do both in one command
maturin develop --release

Testing Your Changes

cd sdl2_alpha/

# Install test dependencies
pip install -e .[test]

# Run the test suite
pytest tests/ -v

# Run just performance tests
pytest tests/test_performance.py -v -s

# Quick functionality test
python -c "
import sdl2_alpha
result = sdl2_alpha.blend_pixel((255,0,0,128), (0,255,0,255))
print(f'Test blend result: {result}')
print('✅ sdl2_alpha working!' if result[0] > 100 and result[1] > 100 else '❌ Something wrong')
"

When to Use Which Build

  • Development build (maturin develop):

    • Use when actively coding/debugging
    • Much faster to compile
    • Good enough for testing functionality
    • ~3-5x slower than release
  • Release build (maturin build --release):

    • Use for performance testing
    • Use for final integration
    • Takes much longer to compile
    • Full SIMD optimizations enabled

Useful Commands

# Clean everything and start over
cd sdl2_alpha/
cargo clean
rm -rf target/

# Check for Rust issues without compiling
cargo check

# See what maturin will build
maturin list-python

# Force rebuild everything
maturin develop --force

# Build specific Python version
maturin develop --python 3.10

Troubleshooting

"maturin not found":

pip install maturin

"Rust compiler errors":

cargo check  # Shows errors without building Python extension

"Import errors after rebuild":

# Sometimes Python caches the old version
python -c "import sys; print([p for p in sys.path if 'sdl2_alpha' in p])"
pip uninstall sdl2_alpha
maturin develop

"Performance is slow":

  • Make sure you're using release build: maturin develop --release
  • Debug builds are much slower

API Usage

import sdl2_alpha

# Blend single pixel (for testing)
result = sdl2_alpha.blend_pixel(
    (255, 128, 64, 128),  # src (r,g,b,a)
    (64, 128, 255, 255)   # dst (r,g,b,a)  
)

# Zero-copy in-place blending (FAST - the high-performance path)
sdl2_alpha.blend_rect_inplace(
    src_ptr,        # Raw pointer to source pixels
    src_width, src_height,
    src_x, src_y, src_w, src_h,  # Source rectangle (can be negative)
    dst_ptr,        # Raw pointer to destination pixels  
    dst_width, dst_height,
    dst_x, dst_y    # Destination position (can be negative)
)

# Copy-based blending (slower but easier)
blended_bytes = sdl2_alpha.blend_surface(
    src_bytes, dst_bytes, width, height
)

blended_bytes = sdl2_alpha.blend_rect(
    src_bytes, src_width, src_height, src_x, src_y, src_w, src_h,
    dst_bytes, dst_width, dst_height, dst_x, dst_y
)

Performance Characteristics

  • ~72 FPS for 100 frames with 20 overlapping 50x50 elements on 800x600 surface
  • ~1440 blits/sec with automatic clipping
  • Zero-copy direct memory access (blend_rect_inplace)
  • Automatic clipping handles negative coordinates and off-screen blits
  • Fast paths for fully opaque (direct copy) and fully transparent (skip) pixels

Integration with SDL2 Applications

sdl2_alpha provides a drop-in replacement for SDL2's broken alpha blending. Use the zero-copy blend_rect_inplace() function for maximum performance in composition-heavy applications.

License

MIT

Metadata

Release files for sdl2-alpha 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for sdl2-alpha 0.2.0
File
sdl2_alpha-0.2.0-cp38-abi3-win_amd64.whl CPython 3.8 abi3 Windows x86-64 Details
sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_x86_64.whl CPython 3.8 abi3 Linux glibc 2.34+ x86-64 Details
sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_aarch64.whl CPython 3.8 abi3 Linux glibc 2.34+ ARM64 Details
sdl2_alpha-0.2.0-cp38-abi3-macosx_11_0_arm64.whl CPython 3.8 abi3 macOS 11.0+ ARM64 Details
sdl2_alpha-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl CPython 3.8 abi3 macOS 10.12+ x86-64 Details

Total release size: 1.0 MB

Release files / sdl2_alpha-0.2.0-cp38-abi3-win_amd64.whl

Download URL sdl2_alpha-0.2.0-cp38-abi3-win_amd64.whl
Size 117.1 kB
Tags CPython 3.8 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
b4fdc303a5697325c40798e8d1e4c0f05f4df0a92ff07a3a8b93f2b5619031f5
BLAKE2b-256 checksum
How to use checksums
e470fd6db0566eda0007398b35826f23fa316b14e6ebe0cf5db6437a3ccee236
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 30, 2026.

Transparency log

Release files / sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_x86_64.whl

Download URL sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_x86_64.whl
Size 244.6 kB
Tags CPython 3.8 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
d47703cce636f3c0c8d032fc32bcf11cd3540c44c38cb9366e3a5f349e608d61
BLAKE2b-256 checksum
How to use checksums
fbafc8026ae340f7084cde81870a4efefbcc6605b041d87d43673184628c8005
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 30, 2026.

Transparency log

Release files / sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_aarch64.whl

Download URL sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_aarch64.whl
Size 241.8 kB
Tags CPython 3.8 Linux glibc 2.34+ ARM64 abi3
SHA-256 checksum
How to use checksums
b5fba09ea14cfc4c03246d58300a4e6e3dfd5104dde8c7dec159674cbd522b2e
BLAKE2b-256 checksum
How to use checksums
32d03ec072d8100179b4a00d6cbc3258cba4e64d7fb304796bc1bd446ce8b5e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 30, 2026.

Transparency log

Release files / sdl2_alpha-0.2.0-cp38-abi3-macosx_11_0_arm64.whl

Download URL sdl2_alpha-0.2.0-cp38-abi3-macosx_11_0_arm64.whl
Size 208.8 kB
Tags CPython 3.8 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e8b34de6ab076acc93028208e1a7fc189aedfa9ee2580971bff8641cf1fe337e
BLAKE2b-256 checksum
How to use checksums
2e6560b5c56ee3648a779cd2250f39c4346db70ff5db97ac99fe7ffda23bbe70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 30, 2026.

Transparency log

Release files / sdl2_alpha-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl

Download URL sdl2_alpha-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl
Size 219.2 kB
Tags CPython 3.8 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
fa958cea91130e41ac80283ce2ee1b0ae915e8981ce4678607d9c133d47a1874
BLAKE2b-256 checksum
How to use checksums
21802698bd7adb956c6f56e361308a72a77af3a12074690487bb8cae78a4a2ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 30, 2026.

Transparency log

Security for sdl2-alpha 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 5 files in this release have been uploaded with provenance metadata.

Select from 5 files

Attestation for sdl2_alpha-0.2.0-cp38-abi3-win_amd64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jan 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page