Skip to main content

uppercut-retro-pixels

The pixel arithmetic that console ports keep retyping, as pure functions on bytes, integers and tuples: 16-bit colour formats, red/blue swap, the PSP block swizzle and its inverse, run-length coding, palette ramps and padded palettes, and a glyph-atlas packer that writes the metrics as a C header. Standard library only; no Pillow, no numpy. Turning a PNG into RGBA bytes (and running ImageMagick or a texture converter) stays with the caller.

pip install uppercut-retro-pixels

Requires Python 3.10 or newer.

Example

from uppercut_retro_pixels import ARGB4444, RGB565, encode, pack, swap_rb, swizzle

# One pixel, or a whole RGBA image, into a console format
value = encode(RGB565, 255, 128, 0)                       # 0xFC00
data = pack(ARGB4444, rgba_bytes, endian="little")        # two bytes a pixel

# PSP wants BGR565, laid out in 16-byte by 8-row blocks
psp = swizzle(pack(RGB565, rgba_bytes), row_bytes=64 * 2, height=64)
bgr = swap_rb(RGB565, value)                              # the same pixel as BGR565

Formats

RGB565, BGR565, ARGB4444, ABGR4444, ARGB1555, ABGR1555, RGBA5551 and BGR555 are PixelFormat values; FORMATS maps names to them. A format lists where each channel sits in the 16-bit value, so other layouts are a one-line PixelFormat(name, {"r": (shift, bits), ...}).

Function What it does
encode(fmt, r, g, b, a=255, *, rounding="truncate") Packs 8-bit channels into a 16-bit integer. "truncate" is a right shift; "nearest" picks the closest level. A 1-bit alpha is on from 128 in both modes.
encode_unit(fmt, r, g, b, a=1.0) The same for channels given as floats in 0..1: clamped, scaled, rounded with Python's round and masked.
decode(fmt, value, *, expand="replicate") Unpacks to (r, g, b, a). "replicate" repeats the high bits into the low ones (v << 3 | v >> 2 for 5 bits); "scale" is v * 255 // max. A format with no alpha gives 255.
swap_rb(fmt, value) Exchanges the red and blue fields. swap_rb(RGB565, v) is v read as BGR565, and swap_rb(ARGB4444, v) is v read as ABGR4444.
pack(fmt, rgba, *, endian="little", rounding="truncate") RGBA bytes to packed pixels, two bytes each.
unpack(fmt, data, *, endian="little", expand="replicate") Packed pixels to RGBA bytes.

Block layouts

Function What it does
swizzle(data, row_bytes, height) Reorders a linear image into 16-byte by 8-row blocks, the PSP texture layout. row_bytes is a multiple of 16 and height a multiple of 8. Works for any pixel size, 4-bit included, because it counts bytes.
unswizzle(data, row_bytes, height) The inverse.
swizzle_items(items, width, height, *, block_width=8, block_height=8) The same reordering for a list of whole pixels. The default block fits 16-bit pixels; use block_width=16 for 8-bit ones.
pack_nibbles(indices) / unpack_nibbles(data) 4-bit indices, two to a byte, the first in the low nibble.
morton_index(x, y, bits) The interleaved (twiddled) index Dreamcast textures use.

A byte at (x, y) lands at ((y // 8) * (row_bytes // 16) + x // 16) * 128 + (y % 8) * 16 + x % 16.

Run-length coding

A record is a one-byte count and a value of one or two bytes.

Function What it does
rle_runs(values, *, max_run=255) (count, value) runs.
rle_pack(values, *, value_bytes=1, endian="big", max_run=255) Bytes of count, value records.
rle_unpack(data, *, value_bytes=1, endian="big", limit=None) The values back. A record cut off at the end is dropped; limit stops at that many values, so a dump with spare runs past the end of the image is trimmed.
rle_unpack_hex(text, *, value_bytes=2, limit=None) The same for records logged as hexadecimal text (two digits of count, then the value), whitespace ignored.
flatten(runs) Expands (count, value) runs.

Palettes

Function What it does
ramp(stops, steps) steps colours evenly spaced through a list of (r, g, b) stops, ends included, rounded to the nearest integer.
pad_palette(colors, size=256, fill=None) Exactly size colours; missing ones come from fill(index), or black.
palette_bytes(colors, size=256) Flat r, g, b bytes, zero-padded to size entries.

Glyph atlases

Name What it does
Glyph(char, width, height, y_offset, advance, pixels) One glyph bitmap, RGBA bytes. A blank glyph has width and height 0.
pack_glyphs(sets, atlas_width, atlas_height, *, margin=1) Shelf-packs the glyphs of one or more sets into an RGBA atlas and returns it with a GlyphRect (x, y, width, height, y_offset, advance) for each glyph. The sets share a cursor. Raises ValueError when the atlas is full.
metrics_header(tables, *, atlas_width, atlas_height, first_char, last_char, ...) The C header: atlas size and character range as #defines, a glyph struct and one static const rectangle table per (name, rects). Names, guard, integer types and include line are parameters.
outlined_cell_atlas(rows_for, *, first=32, last=126, columns=16, cell_width=8, cell_height=12, ...) A texture of equal cells holding 1-bit glyph rows with a one-pixel outline, as pixel values the caller has already packed (ink, outline, blank).

Fonts render from outside: the caller draws glyphs with ImageMagick, Pillow or a bitmap table and hands the RGBA bytes to pack_glyphs; a 5x7 table for outlined_cell_atlas comes from bitmap-font or uppercut-pixel-art-sheets.

Errors

Every function raises ValueError for input it cannot use: a channel outside 0..255, an odd byte count, a size that is not a whole number of blocks, a palette over its size, an atlas that is full.

Licence

MIT

Metadata

Release files for uppercut-retro-pixels 0.1.0

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

Source distribution (sdist)

Source distribution for uppercut-retro-pixels 0.1.0
File Size Uploaded Provenance Actions
uppercut_retro_pixels-0.1.0.tar.gz 17.9 kB Without provenance Details for uppercut_retro_pixels-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for uppercut-retro-pixels 0.1.0
File Interpreter ABI Platform Actions
uppercut_retro_pixels-0.1.0-py3-none-any.whl Python 3 none any Details for uppercut_retro_pixels-0.1.0-py3-none-any.whl

Total release size: 32.9 kB

Release files / uppercut_retro_pixels-0.1.0.tar.gz

Download URL uppercut_retro_pixels-0.1.0.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
c8619885e34760ca55a6a546be0b343e99534355a1d19c0a8d00eeb971bfef1b
BLAKE2b-256 checksum
How to use checksums
d5c5552368a699dad49461b656cb5dcc14d22b1a73107f9f95fac97504e93896
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / uppercut_retro_pixels-0.1.0-py3-none-any.whl

Download URL uppercut_retro_pixels-0.1.0-py3-none-any.whl
Size 14.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a58f821d44ab144bb48277cae786d23f37bb72bcd204a0e620d9097f101541fb
BLAKE2b-256 checksum
How to use checksums
3256aeade038e8918995f8a9315cbac1b369d7c7f19a8d7f0d20fd4f2f2391c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for uppercut-retro-pixels 0.1.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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