Skip to main content

Crate hqx

Crate hqx 

Source
Expand description

High-quality pixel-art magnification by 2x, 3x, and 4x.

hqx enlarges a raster of 32-bit pixels while keeping sharp edges sharp and smoothing diagonals. It reads a 3x3 neighborhood around each source pixel, classifies the eight neighbors as close or distant in an approximate YUV space, and fills the output block from a 256-entry rule table. The result suits sprites, tiles, and other pixel art far better than bilinear scaling.

§Pixel format

Each pixel is a u32 read as 0xAARRGGBB: alpha in bits 24..31, red in 16..23, green in 8..15, blue in 0..7. Color comparison ignores alpha. Interpolation carries alpha through. Byte order in memory is the caller’s concern; the functions operate on u32 values directly.

§Buffers

The source holds width * height pixels in row-major order. The caller allocates the destination. A scale of n writes (width * n) * (height * n) pixels. The plain functions assume tightly packed rows. The _rb variants take explicit row strides in pixels for padded buffers.

§Example

let src = [
    0x00FF_0000, 0x0000_FF00,
    0x0000_00FF, 0x00FF_FFFF,
];
let mut dst = vec![0u32; 2 * 2 * src.len()];
hqx::hq2x(&src, &mut dst, 2, 2);
assert_eq!(dst.len(), 16);

§Errors and bounds

A zero width or height writes nothing. A destination shorter than the required size panics up front, before any write, so the buffer is never left partly filled. The panic message names the pixel count the buffer needs. A source shorter than the pixels the scale reads panics the same way before any write.

Functions§

hq2x
Scale a raster by 2x into a tightly packed destination.
hq2x_rb
Scale a raster by 2x with explicit row strides in pixels.
hq3x
Scale a raster by 3x into a tightly packed destination.
hq3x_rb
Scale a raster by 3x with explicit row strides in pixels.
hq4x
Scale a raster by 4x into a tightly packed destination.
hq4x_rb
Scale a raster by 4x with explicit row strides in pixels.