hqx 0.1.1

high-quality pixel-art image magnification (2x/3x/4x upscaling)
Documentation
//! Shared loaders for the conformance fixtures.
//!
//! Inputs are hex grids: a `width height` header, then rows of 8-digit hex
//! pixels. Goldens are raw little-endian `u32` buffers, one per input and
//! scale. Both formats are produced once and read back here verbatim.
//!
//! Each test binary compiles this module on its own and uses a subset of the
//! helpers, so unused-item warnings here are expected and silenced.
#![allow(dead_code)]

use std::fs;
use std::path::PathBuf;

/// A decoded source image.
pub struct Image {
    /// Width in pixels.
    pub w: usize,
    /// Height in pixels.
    pub h: usize,
    /// Row-major pixels as `0xAARRGGBB`.
    pub px: Vec<u32>,
}

/// Directory holding this crate's tests.
fn tests_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests")
}

/// Load an input grid by case name.
pub fn load_input(name: &str) -> Image {
    let path = tests_dir().join("inputs").join(format!("{name}.txt"));
    let text = fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {path:?}: {e}"));
    let mut tokens = text.split_whitespace();
    let w: usize = tokens.next().unwrap().parse().unwrap();
    let h: usize = tokens.next().unwrap().parse().unwrap();
    let mut px = Vec::with_capacity(w * h);
    for tok in tokens {
        px.push(u32::from_str_radix(tok, 16).unwrap());
    }
    assert_eq!(px.len(), w * h, "pixel count mismatch in {name}");
    Image { w, h, px }
}

/// Load a golden buffer by case name and scale suffix (`hq2x`/`hq3x`/`hq4x`).
pub fn load_golden(name: &str, ext: &str) -> Vec<u32> {
    let path = tests_dir().join("golden").join(format!("{name}.{ext}.bin"));
    let bytes = fs::read(&path).unwrap_or_else(|e| panic!("read {path:?}: {e}"));
    assert_eq!(bytes.len() % 4, 0, "golden not u32-aligned: {name}.{ext}");
    bytes
        .chunks_exact(4)
        .map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]]))
        .collect()
}

/// Every case name with a committed golden.
pub const CASES: &[&str] = &[
    "solid_1x1",
    "solid_4x4",
    "two_color_vert",
    "two_color_horiz",
    "diagonal",
    "checkerboard",
    "four_corners",
    "single_dot",
    "cross",
    "alpha_varied",
    "near_threshold",
    "gradient_row",
    "strip_1x6",
    "strip_6x1",
    "rng_8x8",
    "rng_16x12",
    "rng_24x24",
    "bands_20x20",
    "all_patterns",
    "all_patterns_multicolor",
    "alpha_pattern",
];