f-office 0.3.0

Renders Office Open XML documents the way Microsoft Office does.
Documentation
use std::num::NonZeroU32;

use f_office_paint::{LinearLimit, Resampler};

use super::{
    area_weights, centre_pixels, level, linear_weights, placed_weights, resample, resample_by,
    resample_part_at_scale, scaled_count,
};

/// The limit of 2^17 source pixels, which the tests below the side's test were written for.
const COUNT: LinearLimit = LinearLimit::Pixels(NonZeroU32::new(1 << 17).unwrap());

#[test]
fn takes_the_pixel_under_each_new_pixels_centre() {
    // 5 pixels across to 2: centres at 1.25 and 3.75, so pixels 1 and 3. 3 rows to 2: centres
    // at 0.75 and 2.25, so rows 0 and 2. Two samples each.
    let samples: Vec<u8> = (0..30).collect();
    let rows = [vec![2, 3, 6, 7], vec![22, 23, 26, 27]].concat();
    assert_eq!(centre_pixels(&samples, (5, 3), 2, (2, 2)), rows);
    let by = |resampler| resample_by(resampler, &samples, (5, 3), 2, (2, 2));
    assert_eq!(by(Resampler::CentrePixel), rows);
    assert_eq!(
        by(Resampler::Filter(COUNT)),
        resample(&samples, (5, 3), 2, (2, 2), COUNT)
    );
    // Never a mix of two pixels: a 1-bit stripe of 3 pixels to 2 keeps black and white.
    let stripe = [0, 255, 0];
    assert_eq!(centre_pixels(&stripe, (3, 1), 1, (2, 1)), vec![0, 0]);
}

#[test]
fn interpolates_small_pictures_linearly_at_the_left_edge_of_each_pixel() {
    // 4 pixels to 3: places 0, 4/3 and 8/3.
    let taps = linear_weights(4, 3);
    assert_eq!(taps[0], vec![(0, 1.0), (1, 0.0)]);
    assert_eq!(taps[1][0].0, 1);
    assert!((taps[1][0].1 - 2.0 / 3.0).abs() < 1e-12);
    assert_eq!(taps[2][1].0, 3);
    let gray = [0, 30, 60, 90];
    assert_eq!(resample(&gray, (4, 1), 1, (3, 1), COUNT), vec![0, 40, 80]);
}

#[test]
fn takes_the_mean_of_the_covered_area_for_large_pictures() {
    // 5 pixels to 2: the first covers 0 to 2.5, the second 2.5 to 5.
    let taps = area_weights(5, 2);
    assert_eq!(taps[0], vec![(0, 0.4), (1, 0.4), (2, 0.2)]);
    assert_eq!(taps[1], vec![(2, 0.2), (3, 0.4), (4, 0.4)]);
    // A picture above the linear limit: 400 by 330 pixels of one gray, and its halves.
    let mut samples = vec![100; 400 * 330];
    samples[..200 * 330].fill(0);
    let small = resample(&samples, (330, 400), 1, (165, 200), COUNT);
    assert!(small[..100 * 165].iter().all(|&v| v == 0));
    assert!(small[100 * 165..].iter().all(|&v| v == 100));
}

#[test]
fn rounds_half_to_even() {
    assert_eq!(level(2.5), 2);
    assert_eq!(level(3.5), 4);
    assert_eq!(level(-3.0), 0);
    assert_eq!(level(300.0), 255);
}

#[test]
fn keeps_each_channel_apart() {
    let rgb = [10, 20, 30, 50, 60, 70];
    assert_eq!(resample(&rgb, (2, 1), 3, (1, 1), COUNT), vec![10, 20, 30]);
}

/// A weighted sum of the samples, which changes when any sample changes.
fn digest(samples: &[u8]) -> u64 {
    samples
        .iter()
        .zip(1..)
        .map(|(&v, i)| u64::from(v) * i)
        .sum()
}

#[test]
fn gives_the_samples_of_one_pass_over_the_whole_picture() {
    // The digests that the resampling gave before it kept only the open rows of the pass
    // down: linear interpolation of RGB, the mean of the area of a large gray picture, and a
    // picture brought down to one row.
    let sample = |value: usize| u8::try_from(value % 256).unwrap();
    let rgb: Vec<u8> = (0..9 * 7 * 3).map(|i| sample(i * 37 + 11)).collect();
    let gray: Vec<u8> = (0..400 * 330)
        .map(|i| sample((i % 330) * 7 + (i / 330) * 13))
        .collect();
    let tall: Vec<u8> = (0..50 * 40).map(|i| sample(i * 5)).collect();
    let digests = [
        digest(&resample(&rgb, (9, 7), 3, (4, 3), COUNT)),
        digest(&resample(&gray, (330, 400), 1, (13, 11), COUNT)),
        digest(&resample(&tall, (50, 40), 1, (50, 1), COUNT)),
    ];
    assert_eq!(digests, [93_630, 1_312_349, 208_250]);
}

#[test]
fn takes_the_mean_of_the_covered_area_when_a_side_passes_the_limit() {
    // A ramp of 420 pixels across, 1 down, to 140: the side limit of 419 takes the area mean,
    // one of 420 the linear interpolation, whatever the count.
    let ramp: Vec<u8> = (0..420u32)
        .map(|x| u8::try_from(x * 255 / 419).unwrap())
        .collect();
    let side = |count| LinearLimit::Side(NonZeroU32::new(count).unwrap());
    let area = resample(&ramp, (420, 1), 1, (140, 1), side(419));
    let linear = resample(&ramp, (420, 1), 1, (140, 1), side(420));
    assert_ne!(area, linear);
    assert_eq!(
        area,
        resample(
            &ramp,
            (420, 1),
            1,
            (140, 1),
            LinearLimit::Pixels(NonZeroU32::new(1).unwrap())
        )
    );
    assert_eq!(linear, resample(&ramp, (420, 1), 1, (140, 1), COUNT));
}

#[test]
fn counts_the_whole_picture_at_the_parts_scale_rounded_half_up() {
    // 708 rows, a part of 140 written as 48: 242.74 rows, so 243; written as 55: 278.14, so 278.
    assert_eq!(scaled_count(708, 48, 140), 243);
    assert_eq!(scaled_count(708, 55, 140), 278);
    // 3 rows at the scale 1/2: 1.5, a half up to 2; never below 1.
    assert_eq!(scaled_count(3, 1, 2), 2);
    assert_eq!(scaled_count(1, 1, 40), 1);
}

#[test]
fn places_each_written_pixel_at_the_parts_first_pixel_times_the_scale() {
    // The first pixel 270 at the scale 48/140: 92.571..., then one pixel apart.
    let taps = placed_weights(270, 48, 140, 243);
    let first = 270.0 * 48.0 / 140.0;
    assert_eq!(taps[0][0].0, 92);
    assert!((taps[0][1].1 - (first - 92.0)).abs() < 1e-12);
    assert_eq!(taps[47][0].0, 139);
    // A place beyond the last sample is held at it.
    assert_eq!(placed_weights(10, 3, 2, 12)[2], vec![(11, 1.0), (11, 0.0)]);
}

#[test]
fn resamples_a_part_as_its_own_area_mean_when_the_grids_meet() {
    // 40 rows, the part from row 10 to 30 written as 10 rows: the whole picture goes to 20 rows
    // and the part starts at row 5 of them, so each written row is the mean of two part rows.
    let samples: Vec<u8> = (0..40u32 * 3)
        .map(|i| u8::try_from((i * 37) % 251).unwrap())
        .collect();
    let part = &samples[10 * 3..30 * 3];
    let at_scale = resample_part_at_scale(&samples, (3, 40), 1, [0, 10, 3, 30], (3, 10));
    let own = resample(
        part,
        (3, 20),
        1,
        (3, 10),
        LinearLimit::Pixels(NonZeroU32::new(1).unwrap()),
    );
    assert_eq!(at_scale, own);
}

#[test]
fn blends_two_rows_of_the_whole_picture_at_the_parts_scale() {
    // 6 rows of 0, 60, 120, 180, 240, 240 and the part of rows 1 to 5 written as 2: the whole
    // picture goes to 3 rows (30, 150, 240), and written row y takes the place 0.5 + y, so the
    // rows are 90 and 195.
    let samples = [0, 60, 120, 180, 240, 240];
    let at_scale = resample_part_at_scale(&samples, (1, 6), 1, [0, 1, 1, 5], (1, 2));
    assert_eq!(at_scale, vec![90, 195]);
}