Skip to content

Commit

Permalink
Try uploading diff image
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Aug 21, 2024
1 parent 9db0a51 commit e4348de
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,9 @@ jobs:

- name: Run tests
run: cargo test --release

- name: Upload artifacts
if: failure()
uses: actions/upload-artifact@v3
with:
path: ./tests/diff
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target
.idea
out
data
data
./tests/diffs
84 changes: 57 additions & 27 deletions tests/visreg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping};
use fontdb::Source;
use image::{load_from_memory, Rgba};
use image::{load_from_memory, Rgba, RgbaImage};
use krilla::document::Document;
use krilla::rgb::Rgb;
use krilla::serialize::SerializeSettings;
Expand Down Expand Up @@ -36,6 +36,10 @@ pub fn save_refs(name: &str, renderer: &Renderer, document: RenderedDocument) {
.join("tests/refs")
.join(name);

let diffs_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/diffs")
.join(name);

std::fs::create_dir_all(&refs_path).unwrap();

if document.is_empty() {
Expand All @@ -46,33 +50,14 @@ pub fn save_refs(name: &str, renderer: &Renderer, document: RenderedDocument) {
let reference = load_from_memory(&std::fs::read(&ref_path).unwrap()).unwrap().into_rgba8();
let actual = load_from_memory(&document[0]).unwrap().into_rgba8();

let width = max(reference.width(), actual.width());
let height = max(reference.height(), actual.height());

let mut pixel_diff = 0;

for x in 0..width {
for y in 0..height {
let actual_pixel = actual.get_pixel_checked(x, y);
let expected_pixel = reference.get_pixel_checked(x, y);
let (diff_image, pixel_diff) = get_diff(&reference, &actual);



match (actual_pixel, expected_pixel) {
(Some(actual), Some(expected)) => {
if is_pix_diff(expected, actual) {
pixel_diff += 1;
}
}
(Some(_), None) => {
pixel_diff += 1;
}
(None, Some(_)) => {
pixel_diff += 1;
}
_ => unreachable!(),
}
}
if pixel_diff != 0 {
std::fs::create_dir_all(&diffs_path).unwrap();
let diff_path = diffs_path.join(format!("{}.png", renderer.name()));
diff_image
.save_with_format(&diff_path, image::ImageFormat::Png)
.unwrap();
}

assert_eq!(pixel_diff, 0);
Expand All @@ -87,6 +72,51 @@ pub fn save_refs(name: &str, renderer: &Renderer, document: RenderedDocument) {
}
}

pub fn get_diff(
expected_image: &RgbaImage,
actual_image: &RgbaImage,
) -> (RgbaImage, i32) {
let width = max(expected_image.width(), actual_image.width());
let height = max(expected_image.height(), actual_image.height());

let mut diff_image = RgbaImage::new(width * 3, height);

let mut pixel_diff = 0;

for x in 0..width {
for y in 0..height {
let actual_pixel = actual_image.get_pixel_checked(x, y);
let expected_pixel = expected_image.get_pixel_checked(x, y);

match (actual_pixel, expected_pixel) {
(Some(actual), Some(expected)) => {
diff_image.put_pixel(x, y, *expected);
diff_image.put_pixel(x + 2 * width, y, *actual);
if is_pix_diff(expected, actual) {
pixel_diff += 1;
diff_image.put_pixel(x + width, y, Rgba([255, 0, 0, 255]));
} else {
diff_image.put_pixel(x + width, y, Rgba([0, 0, 0, 255]))
}
}
(Some(actual), None) => {
pixel_diff += 1;
diff_image.put_pixel(x + 2 * width, y, *actual);
diff_image.put_pixel(x + width, y, Rgba([255, 0, 0, 255]));
}
(None, Some(expected)) => {
pixel_diff += 1;
diff_image.put_pixel(x, y, *expected);
diff_image.put_pixel(x + width, y, Rgba([255, 0, 0, 255]));
}
_ => unreachable!(),
}
}
}

(diff_image, pixel_diff)
}

fn is_pix_diff(pixel1: &Rgba<u8>, pixel2: &Rgba<u8>) -> bool {
if pixel1.0[3] == 0 && pixel2.0[3] == 0 {
return false;
Expand Down

0 comments on commit e4348de

Please sign in to comment.