Blyx

AspectFit

Library · 0.1.0

Fit an image inside a box while preserving aspect ratio.

Framework using this library

Install with Conda

Create an environment containing AspectFit 0.1.0, its Blyx source and a runnable example. Run the second command with Blyx 0.2.1, Rust and a host linker installed.

conda create -y --prefix ./blyx-env --override-channels -c https://rahulchaube1.github.io/blyx-packages blyx-aspectfit=0.1.0=0
blyxc native-run ./blyx-env/share/blyx/aspectfit/main.blyx

To use this package in your application, copy share/blyx/aspectfit/vendor/aspectfit from the environment into your project's vendor directory.

Install the full collection with Homebrew or Scoop

Import into a Blyx project

Install Blyx 0.2.1, Rust 1.85 or newer and a host linker. From the directory containing your entry file, clone this tagged release.

git clone --depth 1 --branch v0.1.0 https://github.com/Rahulchaube1/blyx-aspectfit.git vendor/aspectfit

You can also download AspectFit as a ZIP. Extract its contents into vendor/aspectfit.

Save this as main.blyx beside vendor and run blyxc native-run main.blyx.

use vendor::aspectfit::aspectfit;

fn main() {
    let width = 400.0;
    let height = 200.0;
    let box_w = 100.0;
    let box_h = 100.0;
    let upscale = 0.0;
    let result = aspectfit_contain(width, height, box_w, box_h, upscale);
    if len(result) == 0 { println!("invalid"); return; }
    let mut i = 0;
    while i < len(result) {
        println!(at(result, i));
        i += 1;
    }
}

Expected output

100
50
0.25

API and input requirements

Returns [integer output width, height, scale]. Downward rounding and a one-pixel minimum preserve a nonempty raster. Set upscale to 0 to keep small images at native size. Invalid arguments and nonfinite results return an empty vector.

APIUse
aspectfit_contain(width, height, box_w, box_h, upscale)Returns [integer output width, height, scale]. Downward rounding and a one-pixel minimum preserve a nonempty raster. Set upscale to 0 to keep small images at native size.

Develop and contribute

Open the tagged source to read the implementation and examples. Include the smallest reproducible Blyx program, compiler version and expected result when reporting an issue.