File size: 18,889 Bytes
d5bfab8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
//! Heatmap of where change/nochange are likely to be present.
//!
//! Makes a fingerprint of what colors are present in the rows/columns/diagonals that change/nochange between input and output.
//!
//! Example when the Red color is present, then we know that the column doesn't change between input and output.
//!
//! Example when the Blue or Yellow colors are present, then we know that DiagonalA changes between input and output.
//!
//! Weakness: It considers the entire row/column/diagonal, so it's a very loose hint that something may be going on in this row/column/diagonal.
//! A more narrow hint would be to consider the only pixels BEFORE the Red color, or the pixels AFTER the Red colors.
//! That way it would be more narrow where the change/nochange is likely to be present.
use super::{Histogram, Image, ImageCompare, ImageSize, ImageHistogram, ImageSkew};
use anyhow::Context;
pub struct CompareInputOutput {
image_size: ImageSize,
input: Image,
difference: Image,
}
impl CompareInputOutput {
/// Compare input and output images.
pub fn create(input: &Image, output: &Image) -> anyhow::Result<Self> {
let image_size: ImageSize = input.size();
if image_size != output.size() {
anyhow::bail!("CompareInputOutput failed because input and output have different sizes");
}
if image_size.is_empty() {
anyhow::bail!("CompareInputOutput failed because input and output are empty");
}
let difference: Image = input.diff(output)
.context("CompareInputOutput failed to diff input and output")?;
if image_size != difference.size() {
anyhow::bail!("CompareInputOutput, difference.size inconsistent");
}
let instance = Self {
image_size,
input: input.clone(),
difference,
};
Ok(instance)
}
/// Fingerprint of what colors impacts the rows.
pub fn single_line_row(&self) -> (Histogram, Histogram) {
let histogram_rows: Vec<Histogram> = self.input.histogram_rows();
let mut histogram_change = Histogram::new();
let mut histogram_nochange = Histogram::new();
for y in 0..self.image_size.height {
let mut is_same: bool = true;
for x in 0..self.image_size.width {
let mask: u8 = self.difference.get(x as i32, y as i32).unwrap_or(0);
if mask > 0 {
is_same = false;
break;
}
}
if let Some(histogram) = histogram_rows.get(y as usize) {
if is_same {
histogram_nochange.add_histogram(histogram);
} else {
histogram_change.add_histogram(histogram);
}
}
}
Self::remove_overlap(&histogram_change, &histogram_nochange)
}
/// Fingerprint of what colors impacts the columns.
pub fn single_line_column(&self) -> (Histogram, Histogram) {
let histogram_columns: Vec<Histogram> = self.input.histogram_columns();
let mut histogram_change = Histogram::new();
let mut histogram_nochange = Histogram::new();
for x in 0..self.image_size.width {
let mut is_same: bool = true;
for y in 0..self.image_size.height {
let mask: u8 = self.difference.get(x as i32, y as i32).unwrap_or(0);
if mask > 0 {
is_same = false;
break;
}
}
if let Some(histogram) = histogram_columns.get(x as usize) {
if is_same {
histogram_nochange.add_histogram(histogram);
} else {
histogram_change.add_histogram(histogram);
}
}
}
Self::remove_overlap(&histogram_change, &histogram_nochange)
}
/// Fingerprint of what colors impacts DiagonalA.
#[allow(dead_code)]
pub fn single_line_diagonal_a(&self) -> anyhow::Result<(Histogram, Histogram)> {
self.single_line_diagonal(true)
}
/// Fingerprint of what colors impacts DiagonalB.
#[allow(dead_code)]
pub fn single_line_diagonal_b(&self) -> anyhow::Result<(Histogram, Histogram)> {
self.single_line_diagonal(false)
}
/// Fingerprint of what colors impacts DiagonalA or DiagonalB.
fn single_line_diagonal(&self, skew_reverse: bool) -> anyhow::Result<(Histogram, Histogram)> {
let magic_color: u8 = 255;
let difference_skewed: Image = self.difference.skew_x(magic_color, skew_reverse)?;
let input_skewed: Image = self.input.skew_x(magic_color, skew_reverse)?;
let mut histogram_vec: Vec<Histogram> = input_skewed.histogram_columns();
for histogram in histogram_vec.iter_mut() {
histogram.set_counter_to_zero(magic_color);
}
let image_size: ImageSize = input_skewed.size();
if difference_skewed.size() != image_size || histogram_vec.len() != image_size.width as usize {
anyhow::bail!("CompareInputOutput.single_line_diagonal, size inconsistent");
}
let mut histogram_change = Histogram::new();
let mut histogram_nochange = Histogram::new();
for x in 0..image_size.width {
let mut is_same: bool = true;
for y in 0..image_size.height {
let mask: u8 = difference_skewed.get(x as i32, y as i32).unwrap_or(0);
if mask == magic_color {
continue;
}
if mask > 0 {
is_same = false;
break;
}
}
if let Some(histogram) = histogram_vec.get(x as usize) {
if is_same {
histogram_nochange.add_histogram(histogram);
} else {
histogram_change.add_histogram(histogram);
}
}
}
let (histogram0, histogram1) = Self::remove_overlap(&histogram_change, &histogram_nochange);
Ok((histogram0, histogram1))
}
/// The colors that are in common between the two histograms are removed.
fn remove_overlap(histogram_change: &Histogram, histogram_nochange: &Histogram) -> (Histogram, Histogram) {
let histogram0: Histogram = histogram_change.subtract_clamp01(&histogram_nochange);
let histogram1: Histogram = histogram_nochange.subtract_clamp01(&histogram_change);
(histogram0, histogram1)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::arc::ImageTryCreate;
#[test]
fn test_10000_single_line_row() {
// Arrange
let input_pixels: Vec<u8> = vec![
9, 9, 9,
9, 2, 9,
9, 9, 9,
3, 9, 9,
9, 9, 9,
];
let input: Image = Image::try_create(3, 5, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
9, 9, 9,
9, 9, 2,
9, 9, 9,
9, 9, 3,
9, 9, 9,
];
let output: Image = Image::try_create(3, 5, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_row();
// Assert
let mut expected_change: Histogram = Histogram::new();
expected_change.increment(2);
expected_change.increment(3);
assert_eq!(actual_change, expected_change);
let expected_nochange: Histogram = Histogram::new();
assert_eq!(actual_nochange, expected_nochange);
}
#[test]
fn test_10001_single_line_row() {
// Arrange
let input_pixels: Vec<u8> = vec![
8, 3, 8, 8, 8,
8, 8, 8, 8, 8,
8, 8, 8, 8, 8,
8, 8, 8, 4, 8,
8, 3, 8, 3, 8,
8, 8, 8, 8, 8,
];
let input: Image = Image::try_create(5, 6, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
4, 8, 4, 4, 4,
8, 8, 8, 8, 8,
8, 8, 8, 8, 8,
8, 8, 8, 4, 8,
4, 8, 4, 8, 4,
8, 8, 8, 8, 8,
];
let output: Image = Image::try_create(5, 6, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_row();
// Assert
let mut expected_change: Histogram = Histogram::new();
expected_change.increment(3);
assert_eq!(actual_change, expected_change);
let mut expected_nochange: Histogram = Histogram::new();
expected_nochange.increment(4);
assert_eq!(actual_nochange, expected_nochange);
}
#[test]
fn test_10002_single_line_row() {
// Arrange
let input_pixels: Vec<u8> = vec![
5, 8, 8,
8, 8, 8,
8, 9, 8,
9, 8, 8,
];
let input: Image = Image::try_create(3, 4, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
5, 8, 8,
8, 8, 8,
8, 7, 7,
9, 8, 8,
];
let output: Image = Image::try_create(3, 4, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_row();
// Assert
let expected_change: Histogram = Histogram::new();
assert_eq!(actual_change, expected_change);
let mut expected_nochange: Histogram = Histogram::new();
expected_nochange.increment(5);
assert_eq!(actual_nochange, expected_nochange);
}
#[test]
fn test_20000_single_line_column() {
// Arrange
let input_pixels: Vec<u8> = vec![
9, 9, 9, 3, 9,
9, 2, 9, 9, 9,
9, 9, 9, 9, 9,
];
let input: Image = Image::try_create(5, 3, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
9, 9, 9, 9, 9,
9, 9, 9, 9, 9,
9, 2, 9, 3, 9,
];
let output: Image = Image::try_create(5, 3, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_column();
// Assert
let mut expected_change: Histogram = Histogram::new();
expected_change.increment(2);
expected_change.increment(3);
assert_eq!(actual_change, expected_change);
let expected_nochange: Histogram = Histogram::new();
assert_eq!(actual_nochange, expected_nochange);
}
#[test]
fn test_20001_single_line_column() {
// Arrange
let input_pixels: Vec<u8> = vec![
7, 5, 9, 9, 9,
9, 9, 9, 3, 2,
9, 9, 9, 9, 2,
];
let input: Image = Image::try_create(5, 3, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
7, 5, 9, 9, 9,
9, 7, 9, 7, 2,
9, 9, 9, 9, 2,
];
let output: Image = Image::try_create(5, 3, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_column();
// Assert
let mut expected_change: Histogram = Histogram::new();
expected_change.increment(3);
expected_change.increment(5);
assert_eq!(actual_change, expected_change);
let mut expected_nochange: Histogram = Histogram::new();
expected_nochange.increment(2);
expected_nochange.increment(7);
assert_eq!(actual_nochange, expected_nochange);
}
#[test]
fn test_30000_single_line_diagonal_a() {
// Arrange
let input_pixels: Vec<u8> = vec![
9, 9, 9, 6, 9,
9, 4, 9, 9, 9,
9, 9, 9, 9, 2,
];
let input: Image = Image::try_create(5, 3, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
9, 9, 9, 9, 9,
9, 9, 9, 9, 9,
4, 6, 9, 9, 2,
];
let output: Image = Image::try_create(5, 3, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_diagonal_a().expect("ok");
// Assert
let mut expected_change: Histogram = Histogram::new();
expected_change.increment(4);
expected_change.increment(6);
assert_eq!(actual_change, expected_change);
let mut expected_nochange: Histogram = Histogram::new();
expected_nochange.increment(2);
assert_eq!(actual_nochange, expected_nochange);
}
#[test]
fn test_30001_single_line_diagonal_a() {
// Arrange
let input_pixels: Vec<u8> = vec![
9, 9, 3, 9, 9,
3, 9, 9, 3, 9,
9, 3, 9, 9, 3,
];
let input: Image = Image::try_create(5, 3, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
9, 9, 3, 9, 9,
3, 9, 9, 7, 9,
9, 7, 9, 9, 3,
];
let output: Image = Image::try_create(5, 3, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_diagonal_a().expect("ok");
// Assert
let mut expected_change: Histogram = Histogram::new();
expected_change.increment(3);
assert_eq!(actual_change, expected_change);
let mut expected_nochange: Histogram = Histogram::new();
expected_nochange.increment(9);
assert_eq!(actual_nochange, expected_nochange);
}
#[test]
fn test_30002_single_line_diagonal_a() {
// Arrange
let input_pixels: Vec<u8> = vec![
7, 3, 7, 7, 7, 7,
7, 7, 7, 3, 7, 7,
7, 7, 3, 7, 7, 7,
7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 9, 9,
];
let input: Image = Image::try_create(6, 5, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
7, 5, 7, 7, 7, 7,
5, 7, 7, 3, 7, 7,
7, 7, 3, 7, 7, 7,
7, 5, 7, 7, 7, 7,
7, 7, 7, 7, 9, 9,
];
let output: Image = Image::try_create(6, 5, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_diagonal_a().expect("ok");
// Assert
let expected_change: Histogram = Histogram::new();
assert_eq!(actual_change, expected_change);
let expected_nochange: Histogram = Histogram::new();
assert_eq!(actual_nochange, expected_nochange);
}
#[test]
fn test_40000_single_line_diagonal_b() {
// Arrange
let input_pixels: Vec<u8> = vec![
9, 9, 9, 6, 9,
9, 4, 9, 9, 9,
9, 9, 9, 9, 2,
];
let input: Image = Image::try_create(5, 3, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
7, 9, 9, 9, 9,
9, 9, 9, 9, 7,
9, 9, 9, 9, 2,
];
let output: Image = Image::try_create(5, 3, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_diagonal_b().expect("ok");
// Assert
let mut expected_change: Histogram = Histogram::new();
expected_change.increment(4);
expected_change.increment(6);
assert_eq!(actual_change, expected_change);
let mut expected_nochange: Histogram = Histogram::new();
expected_nochange.increment(2);
assert_eq!(actual_nochange, expected_nochange);
}
#[test]
fn test_40001_single_line_diagonal_b() {
// Arrange
let input_pixels: Vec<u8> = vec![
3, 7, 7, 7, 7, 7,
7, 7, 7, 3, 7, 7,
7, 7, 3, 7, 7, 6,
7, 7, 7, 7, 6, 7,
9, 9, 7, 6, 7, 6,
];
let input: Image = Image::try_create(6, 5, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
5, 7, 5, 7, 7, 7,
7, 5, 7, 3, 7, 7,
7, 7, 5, 7, 5, 6,
7, 7, 7, 5, 6, 5,
9, 9, 7, 6, 5, 6,
];
let output: Image = Image::try_create(6, 5, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_diagonal_b().expect("ok");
// Assert
let mut expected_change: Histogram = Histogram::new();
expected_change.increment(3);
assert_eq!(actual_change, expected_change);
let mut expected_nochange: Histogram = Histogram::new();
expected_nochange.increment(6);
assert_eq!(actual_nochange, expected_nochange);
}
#[test]
fn test_40002_single_line_diagonal_b() {
// Arrange
let input_pixels: Vec<u8> = vec![
7, 7, 5, 7, 7, 4,
7, 5, 7, 7, 4, 7,
5, 7, 7, 4, 7, 7,
];
let input: Image = Image::try_create(6, 3, input_pixels).expect("image");
let output_pixels: Vec<u8> = vec![
7, 7, 9, 7, 7, 9,
7, 9, 7, 7, 9, 7,
9, 7, 7, 9, 7, 7,
];
let output: Image = Image::try_create(6, 3, output_pixels).expect("image");
let instance: CompareInputOutput = CompareInputOutput::create(&input, &output).expect("instance");
// Act
let (actual_change, actual_nochange) = instance.single_line_diagonal_b().expect("ok");
// Assert
let mut expected_change: Histogram = Histogram::new();
expected_change.increment(4);
expected_change.increment(5);
assert_eq!(actual_change, expected_change);
let mut expected_nochange: Histogram = Histogram::new();
expected_nochange.increment(7);
assert_eq!(actual_nochange, expected_nochange);
}
}
|