Skip to main content

Fast, correct alpha blending for SDL2 surfaces

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sdl2_alpha-0.2.0-cp38-abi3-win_amd64.whl (117.1 kB view details)

Uploaded CPython 3.8+Windows x86-64

sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_x86_64.whl (244.6 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ x86-64

sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_aarch64.whl (241.8 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ ARM64

sdl2_alpha-0.2.0-cp38-abi3-macosx_11_0_arm64.whl (208.8 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

sdl2_alpha-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl (219.2 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

Details for the file sdl2_alpha-0.2.0-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: sdl2_alpha-0.2.0-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 117.1 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sdl2_alpha-0.2.0-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b4fdc303a5697325c40798e8d1e4c0f05f4df0a92ff07a3a8b93f2b5619031f5
MD5 8a2df2f8cc5609c70913fbc804fa2a8e
BLAKE2b-256 e470fd6db0566eda0007398b35826f23fa316b14e6ebe0cf5db6437a3ccee236

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdl2_alpha-0.2.0-cp38-abi3-win_amd64.whl:

Publisher: publish.yml on justindujardin/sdl2_alpha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 d47703cce636f3c0c8d032fc32bcf11cd3540c44c38cb9366e3a5f349e608d61
MD5 400e952df11993185211fa02e02b8262
BLAKE2b-256 fbafc8026ae340f7084cde81870a4efefbcc6605b041d87d43673184628c8005

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_x86_64.whl:

Publisher: publish.yml on justindujardin/sdl2_alpha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 b5fba09ea14cfc4c03246d58300a4e6e3dfd5104dde8c7dec159674cbd522b2e
MD5 485a3c54535f9c5791ceb76df5029f03
BLAKE2b-256 32d03ec072d8100179b4a00d6cbc3258cba4e64d7fb304796bc1bd446ce8b5e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdl2_alpha-0.2.0-cp38-abi3-manylinux_2_34_aarch64.whl:

Publisher: publish.yml on justindujardin/sdl2_alpha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sdl2_alpha-0.2.0-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sdl2_alpha-0.2.0-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e8b34de6ab076acc93028208e1a7fc189aedfa9ee2580971bff8641cf1fe337e
MD5 532e98be4d150cd29c8c26781b3d2ad8
BLAKE2b-256 2e6560b5c56ee3648a779cd2250f39c4346db70ff5db97ac99fe7ffda23bbe70

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdl2_alpha-0.2.0-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: publish.yml on justindujardin/sdl2_alpha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sdl2_alpha-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for sdl2_alpha-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 fa958cea91130e41ac80283ce2ee1b0ae915e8981ce4678607d9c133d47a1874
MD5 393dd2ce91d8d9a86e1683786b619f20
BLAKE2b-256 21802698bd7adb956c6f56e361308a72a77af3a12074690487bb8cae78a4a2ec

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdl2_alpha-0.2.0-cp38-abi3-macosx_10_12_x86_64.whl:

Publisher: publish.yml on justindujardin/sdl2_alpha

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page