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.1.1-cp38-abi3-win_amd64.whl (115.9 kB view details)

Uploaded CPython 3.8+Windows x86-64

sdl2_alpha-0.1.1-cp38-abi3-manylinux_2_34_x86_64.whl (244.9 kB view details)

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

sdl2_alpha-0.1.1-cp38-abi3-manylinux_2_34_aarch64.whl (240.2 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ ARM64

sdl2_alpha-0.1.1-cp38-abi3-macosx_11_0_arm64.whl (205.5 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

sdl2_alpha-0.1.1-cp38-abi3-macosx_10_12_x86_64.whl (219.0 kB view details)

Uploaded CPython 3.8+macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for sdl2_alpha-0.1.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f27245fd619fe3fd279c17c789e2182b9a551f727ce2c16059e2e87b14b8caff
MD5 dbab574977a84c50be981a99713cee97
BLAKE2b-256 7d28e5340b4153323c908f8a37cd7748bb95570b2b43157768a5466144680098

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdl2_alpha-0.1.1-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.1.1-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for sdl2_alpha-0.1.1-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 f6e1630e6e2a1b2a166a20df7d7b1340d007fdc54a355c5f74f53b4608fe45ee
MD5 51c71c920b92f8cb4caa10a32202c651
BLAKE2b-256 451b7e188442f21014d2b44945ef9b0dda58737077e04e185fa1ad05b5cd0e14

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdl2_alpha-0.1.1-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.1.1-cp38-abi3-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for sdl2_alpha-0.1.1-cp38-abi3-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 ba2100bfdd0ab5e3d28959d459333f22ee742f0cd77ea81d702c1811123046b3
MD5 7c41aca07bd3f8ead378318609e8eda4
BLAKE2b-256 6130b7b7ab0cefd874d225cfee5f3b4c786fab4dbd79244af7be9365ab5df3ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdl2_alpha-0.1.1-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.1.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for sdl2_alpha-0.1.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 4f9d59fa315e74dfe236042da1b0e40e849602854b00a38dfc24f91492e34c78
MD5 a868351ac46c2d47ad8b66198db5e272
BLAKE2b-256 88406952ac089bbc4f1c776a0d7100d428a0a7d0294daa881aaba05ad058fb03

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdl2_alpha-0.1.1-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.1.1-cp38-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for sdl2_alpha-0.1.1-cp38-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 5a12e1ed7831d33d7874d19234164412640c77f68367c084b30c8806a841d8a5
MD5 0ff6bf8c07f750da52adb9f227a431c5
BLAKE2b-256 56038b38a7d334fdc4cec9388f365473c79bac58a43ac971f0bdcf04a75125d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdl2_alpha-0.1.1-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