pixelhog 1.3.0__tar.gz → 1.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -389,7 +389,7 @@ checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
389
389
 
390
390
  [[package]]
391
391
  name = "pixelhog"
392
- version = "1.3.0"
392
+ version = "1.3.2"
393
393
  dependencies = [
394
394
  "approx",
395
395
  "criterion",
@@ -400,7 +400,7 @@ dependencies = [
400
400
 
401
401
  [[package]]
402
402
  name = "pixelhog-python"
403
- version = "1.3.0"
403
+ version = "1.3.2"
404
404
  dependencies = [
405
405
  "pixelhog",
406
406
  "pyo3",
@@ -1,11 +1,12 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pixelhog
3
- Version: 1.3.0
3
+ Version: 1.3.2
4
4
  Classifier: Development Status :: 5 - Production/Stable
5
5
  Classifier: Programming Language :: Python :: 3
6
6
  Classifier: Programming Language :: Python :: 3.12
7
7
  Classifier: Programming Language :: Python :: 3.13
8
8
  Classifier: Programming Language :: Python :: 3.14
9
+ Classifier: Programming Language :: Python :: 3.15
9
10
  Classifier: Programming Language :: Rust
10
11
  Classifier: Operating System :: OS Independent
11
12
  Classifier: License :: OSI Approved :: MIT License
@@ -78,7 +79,7 @@ thumb = thumbnail(current_png, width=200, height=150)
78
79
  | `.ssim()` | `float` | Structural similarity |
79
80
  | `.clusters(dilation, merge_gap, ...)` | `ClustersResult` | Spatial regions of change |
80
81
  | `.row_alignment(...)` | `RowAlignment` | Separate a vertical shift from real changes |
81
- | `.aligned_clusters(alignment, ...)` | `ClustersResult` | Clusters of the residual only |
82
+ | `.aligned_clusters(alignment, ...)` | `ClustersResult` | Clusters of the changed content, shift left out |
82
83
  | `.aligned_ssim(alignment)` | `float` | SSIM over the matched rows only |
83
84
  | `.aligned_diff_image(alignment, ...)` | `bytes` (PNG) | Shift-aware diff visualization |
84
85
  | `.diff_image(...)` | `bytes` (PNG) | Diff visualization |
@@ -112,7 +113,7 @@ if alignment.aligned:
112
113
  for band in alignment.bands:
113
114
  print(band.kind, band.y, band.rows) # "inserted" / "deleted", current-image rows
114
115
 
115
- clusters = cmp.aligned_clusters(alignment) # clusters of the residual only
116
+ clusters = cmp.aligned_clusters(alignment) # clusters of the changed content
116
117
  score = cmp.aligned_ssim(alignment) # SSIM over the matched rows only
117
118
  png = cmp.aligned_diff_image(alignment) # diff image in current-image coordinates
118
119
  ```
@@ -123,10 +124,15 @@ if alignment.aligned:
123
124
  | `inserted_rows` / `deleted_rows` | Rows the current image gained or lost — the shift itself. |
124
125
  | `changed_rows` | Rows present in both images whose content differs. |
125
126
  | `residual_count` | Differing pixels inside those changed rows. This is the number to threshold on: it excludes the shift. |
126
- | `bands` | Where the shift happened, in current-image coordinates. A deleted band is the seam row the removed rows left behind. |
127
+ | `bands` | Where the content below moved, in current-image coordinates, as the diff image draws it. A deleted band is the seam row the removed rows left behind. Band rows can differ from the counts when a region was replaced. |
127
128
 
128
- The cluster mask holds the residual only. Shift bands are the other half of the answer, so read
129
- `alignment.bands` to decide whether to absorb a shift or flag it. An alignment belongs to the pair
129
+ The cluster mask holds the changed content only. Shift bands are the other half of the answer, so read
130
+ `alignment.bands` to decide whether to absorb a shift or flag it.
131
+
132
+ A region replaced by content of a different height, such as a tall chart swapped for a short one, keeps blank rows that both versions share.
133
+ The Myers diff matches those blank rows inside the region, so the fields above report the change as separate inserted and deleted rows.
134
+ `aligned_diff_image()`, `aligned_clusters()` and `bands` show such a region as one changed region instead, followed by the rows one side has over the other.
135
+ The counts and `residual_count` do not change, so thresholds that read them behave as before. An alignment belongs to the pair
130
136
  it was computed from; passing it to another `Comparison` raises. Tune the bail-out with
131
137
  `max_edit_ratio` (default 0.25) and `max_edit_rows` (default 2048).
132
138
 
@@ -57,7 +57,7 @@ thumb = thumbnail(current_png, width=200, height=150)
57
57
  | `.ssim()` | `float` | Structural similarity |
58
58
  | `.clusters(dilation, merge_gap, ...)` | `ClustersResult` | Spatial regions of change |
59
59
  | `.row_alignment(...)` | `RowAlignment` | Separate a vertical shift from real changes |
60
- | `.aligned_clusters(alignment, ...)` | `ClustersResult` | Clusters of the residual only |
60
+ | `.aligned_clusters(alignment, ...)` | `ClustersResult` | Clusters of the changed content, shift left out |
61
61
  | `.aligned_ssim(alignment)` | `float` | SSIM over the matched rows only |
62
62
  | `.aligned_diff_image(alignment, ...)` | `bytes` (PNG) | Shift-aware diff visualization |
63
63
  | `.diff_image(...)` | `bytes` (PNG) | Diff visualization |
@@ -91,7 +91,7 @@ if alignment.aligned:
91
91
  for band in alignment.bands:
92
92
  print(band.kind, band.y, band.rows) # "inserted" / "deleted", current-image rows
93
93
 
94
- clusters = cmp.aligned_clusters(alignment) # clusters of the residual only
94
+ clusters = cmp.aligned_clusters(alignment) # clusters of the changed content
95
95
  score = cmp.aligned_ssim(alignment) # SSIM over the matched rows only
96
96
  png = cmp.aligned_diff_image(alignment) # diff image in current-image coordinates
97
97
  ```
@@ -102,10 +102,15 @@ if alignment.aligned:
102
102
  | `inserted_rows` / `deleted_rows` | Rows the current image gained or lost — the shift itself. |
103
103
  | `changed_rows` | Rows present in both images whose content differs. |
104
104
  | `residual_count` | Differing pixels inside those changed rows. This is the number to threshold on: it excludes the shift. |
105
- | `bands` | Where the shift happened, in current-image coordinates. A deleted band is the seam row the removed rows left behind. |
105
+ | `bands` | Where the content below moved, in current-image coordinates, as the diff image draws it. A deleted band is the seam row the removed rows left behind. Band rows can differ from the counts when a region was replaced. |
106
106
 
107
- The cluster mask holds the residual only. Shift bands are the other half of the answer, so read
108
- `alignment.bands` to decide whether to absorb a shift or flag it. An alignment belongs to the pair
107
+ The cluster mask holds the changed content only. Shift bands are the other half of the answer, so read
108
+ `alignment.bands` to decide whether to absorb a shift or flag it.
109
+
110
+ A region replaced by content of a different height, such as a tall chart swapped for a short one, keeps blank rows that both versions share.
111
+ The Myers diff matches those blank rows inside the region, so the fields above report the change as separate inserted and deleted rows.
112
+ `aligned_diff_image()`, `aligned_clusters()` and `bands` show such a region as one changed region instead, followed by the rows one side has over the other.
113
+ The counts and `residual_count` do not change, so thresholds that read them behave as before. An alignment belongs to the pair
109
114
  it was computed from; passing it to another `Comparison` raises. Tune the bail-out with
110
115
  `max_edit_ratio` (default 0.25) and `max_edit_rows` (default 2048).
111
116
 
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "pixelhog"
3
- version = "1.3.0"
3
+ version = "1.3.2"
4
4
  edition = "2021"
5
5
  rust-version = "1.70"
6
6
  description = "Fast pixelmatch and SSIM image comparison"
@@ -57,7 +57,7 @@ thumb = thumbnail(current_png, width=200, height=150)
57
57
  | `.ssim()` | `float` | Structural similarity |
58
58
  | `.clusters(dilation, merge_gap, ...)` | `ClustersResult` | Spatial regions of change |
59
59
  | `.row_alignment(...)` | `RowAlignment` | Separate a vertical shift from real changes |
60
- | `.aligned_clusters(alignment, ...)` | `ClustersResult` | Clusters of the residual only |
60
+ | `.aligned_clusters(alignment, ...)` | `ClustersResult` | Clusters of the changed content, shift left out |
61
61
  | `.aligned_ssim(alignment)` | `float` | SSIM over the matched rows only |
62
62
  | `.aligned_diff_image(alignment, ...)` | `bytes` (PNG) | Shift-aware diff visualization |
63
63
  | `.diff_image(...)` | `bytes` (PNG) | Diff visualization |
@@ -91,7 +91,7 @@ if alignment.aligned:
91
91
  for band in alignment.bands:
92
92
  print(band.kind, band.y, band.rows) # "inserted" / "deleted", current-image rows
93
93
 
94
- clusters = cmp.aligned_clusters(alignment) # clusters of the residual only
94
+ clusters = cmp.aligned_clusters(alignment) # clusters of the changed content
95
95
  score = cmp.aligned_ssim(alignment) # SSIM over the matched rows only
96
96
  png = cmp.aligned_diff_image(alignment) # diff image in current-image coordinates
97
97
  ```
@@ -102,10 +102,15 @@ if alignment.aligned:
102
102
  | `inserted_rows` / `deleted_rows` | Rows the current image gained or lost — the shift itself. |
103
103
  | `changed_rows` | Rows present in both images whose content differs. |
104
104
  | `residual_count` | Differing pixels inside those changed rows. This is the number to threshold on: it excludes the shift. |
105
- | `bands` | Where the shift happened, in current-image coordinates. A deleted band is the seam row the removed rows left behind. |
105
+ | `bands` | Where the content below moved, in current-image coordinates, as the diff image draws it. A deleted band is the seam row the removed rows left behind. Band rows can differ from the counts when a region was replaced. |
106
106
 
107
- The cluster mask holds the residual only. Shift bands are the other half of the answer, so read
108
- `alignment.bands` to decide whether to absorb a shift or flag it. An alignment belongs to the pair
107
+ The cluster mask holds the changed content only. Shift bands are the other half of the answer, so read
108
+ `alignment.bands` to decide whether to absorb a shift or flag it.
109
+
110
+ A region replaced by content of a different height, such as a tall chart swapped for a short one, keeps blank rows that both versions share.
111
+ The Myers diff matches those blank rows inside the region, so the fields above report the change as separate inserted and deleted rows.
112
+ `aligned_diff_image()`, `aligned_clusters()` and `bands` show such a region as one changed region instead, followed by the rows one side has over the other.
113
+ The counts and `residual_count` do not change, so thresholds that read them behave as before. An alignment belongs to the pair
109
114
  it was computed from; passing it to another `Comparison` raises. Tune the bail-out with
110
115
  `max_edit_ratio` (default 0.25) and `max_edit_rows` (default 2048).
111
116
 
@@ -11,10 +11,11 @@
11
11
  //! pairing turns an adjacent delete and insert back into a content change, so
12
12
  //! anti-aliasing jitter on a text row is not reported as a shift.
13
13
  //!
14
- //! Rows repeated across the page (blank background, rules, spacers) need no
15
- //! special handling. Myers minimizes edits, so it matches them by position:
16
- //! pairing the nth blank row with the nth blank row costs nothing, while any
17
- //! other pairing costs a delete and an insert.
14
+ //! Rows repeated across the page (blank background, rules, spacers) match by
15
+ //! position, because Myers minimizes edits: pairing the nth blank row with the
16
+ //! nth blank row costs nothing, while any other pairing costs a delete and an
17
+ //! insert. The same freedom lets Myers match blank rows inside a region whose
18
+ //! content was replaced; `picture_segments` covers how the diff image draws it.
18
19
  //!
19
20
  //! Alignment is vertical only. Two images of different widths have no row in
20
21
  //! common by construction, so a width change disqualifies the pair.
@@ -29,6 +30,7 @@ use crate::ssim::compute_ssim_rgba;
29
30
  use crate::Error;
30
31
  use rayon::prelude::*;
31
32
  use std::collections::hash_map::DefaultHasher;
33
+ use std::collections::HashSet;
32
34
  use std::hash::Hasher;
33
35
 
34
36
  /// Options for row alignment.
@@ -102,7 +104,11 @@ pub struct RowAlignment {
102
104
  /// Differing pixels inside `Replace` segments, per the pixelmatch options.
103
105
  pub residual_count: usize,
104
106
  pub segments: Vec<RowSegment>,
107
+ /// Where the content below moved, as the diff image draws it. Band rows can
108
+ /// differ from `inserted_rows` and `deleted_rows`, which count `segments`.
105
109
  pub bands: Vec<ShiftBand>,
110
+ // What the diff image, the clusters and the bands draw; see `picture_segments`.
111
+ picture: Vec<RowSegment>,
106
112
  // Row indices only mean something for the pair they were computed from, so
107
113
  // the aligned_* calls refuse an alignment that came from another pair.
108
114
  width: usize,
@@ -122,6 +128,7 @@ impl RowAlignment {
122
128
  residual_count: 0,
123
129
  segments: Vec::new(),
124
130
  bands: Vec::new(),
131
+ picture: Vec::new(),
125
132
  width: images.width,
126
133
  baseline_height: images.baseline_height,
127
134
  current_height: images.current_height,
@@ -427,6 +434,37 @@ fn segment(
427
434
  }
428
435
  }
429
436
 
437
+ /// Push a `Replace` over the rows both sides cover, then the surplus of the longer side.
438
+ fn push_paired(
439
+ segments: &mut Vec<RowSegment>,
440
+ baseline_start: usize,
441
+ current_start: usize,
442
+ baseline_len: usize,
443
+ current_len: usize,
444
+ ) {
445
+ let paired = baseline_len.min(current_len);
446
+ segments.push(segment(
447
+ RowSegmentKind::Replace,
448
+ baseline_start,
449
+ current_start,
450
+ paired,
451
+ ));
452
+
453
+ let (kind, surplus) = if baseline_len > paired {
454
+ (RowSegmentKind::Delete, baseline_len - paired)
455
+ } else {
456
+ (RowSegmentKind::Insert, current_len - paired)
457
+ };
458
+ if surplus > 0 {
459
+ segments.push(segment(
460
+ kind,
461
+ baseline_start + paired,
462
+ current_start + paired,
463
+ surplus,
464
+ ));
465
+ }
466
+ }
467
+
430
468
  /// Pair an adjacent delete/insert couple into a `Replace`, leaving the surplus.
431
469
  ///
432
470
  /// Text that only jitters by an anti-aliased pixel deletes one row and inserts
@@ -465,37 +503,140 @@ fn pair_replacements(runs: &[EditRun]) -> Vec<RowSegment> {
465
503
  continue;
466
504
  };
467
505
 
468
- let baseline_start = run.baseline_start.min(next.baseline_start);
469
- let current_start = run.current_start.min(next.current_start);
470
- let paired = delete_len.min(insert_len);
506
+ push_paired(
507
+ &mut segments,
508
+ run.baseline_start.min(next.baseline_start),
509
+ run.current_start.min(next.current_start),
510
+ delete_len,
511
+ insert_len,
512
+ );
471
513
 
472
- segments.push(segment(
473
- RowSegmentKind::Replace,
474
- baseline_start,
475
- current_start,
476
- paired,
477
- ));
514
+ index += 2;
515
+ }
478
516
 
479
- if delete_len > paired {
480
- segments.push(segment(
481
- RowSegmentKind::Delete,
482
- baseline_start + paired,
483
- current_start + paired,
484
- delete_len - paired,
485
- ));
486
- } else if insert_len > paired {
487
- segments.push(segment(
488
- RowSegmentKind::Insert,
489
- baseline_start + paired,
490
- current_start + paired,
491
- insert_len - paired,
492
- ));
517
+ segments
518
+ }
519
+
520
+ /// The row hashes of both images, with the rows that repeat in either one.
521
+ struct RowHashes<'a> {
522
+ baseline: &'a [u64],
523
+ current: &'a [u64],
524
+ repeated: HashSet<u64>,
525
+ }
526
+
527
+ impl<'a> RowHashes<'a> {
528
+ fn new(baseline: &'a [u64], current: &'a [u64]) -> Self {
529
+ let mut repeated = HashSet::new();
530
+ for hashes in [baseline, current] {
531
+ let mut seen = HashSet::new();
532
+ for hash in hashes {
533
+ if !seen.insert(*hash) {
534
+ repeated.insert(*hash);
535
+ }
536
+ }
493
537
  }
494
538
 
495
- index += 2;
539
+ Self {
540
+ baseline,
541
+ current,
542
+ repeated,
543
+ }
496
544
  }
497
545
 
498
- segments
546
+ /// True for an equal segment that says nothing about where content went.
547
+ ///
548
+ /// A run of one repeated row (blank background, a spacer) can match any of
549
+ /// its copies at the same cost, so it does not show that the rows around it
550
+ /// stayed in place. Varied rows do, even when a list repeats them elsewhere.
551
+ fn is_filler(&self, seg: &RowSegment) -> bool {
552
+ seg.kind == RowSegmentKind::Equal
553
+ && uniform_hash(self.baseline, seg.baseline_start, seg.len)
554
+ .is_some_and(|hash| self.repeated.contains(&hash))
555
+ }
556
+
557
+ /// True when a row the hunk inserts equals a row it deletes: content that moved.
558
+ fn moves_rows(&self, hunk: &[RowSegment]) -> bool {
559
+ let deleted: HashSet<u64> = hunk
560
+ .iter()
561
+ .filter(|seg| seg.kind == RowSegmentKind::Delete)
562
+ .flat_map(|seg| &self.baseline[seg.baseline_start..seg.baseline_start + seg.len])
563
+ .copied()
564
+ .collect();
565
+
566
+ hunk.iter()
567
+ .filter(|seg| seg.kind == RowSegmentKind::Insert)
568
+ .flat_map(|seg| &self.current[seg.current_start..seg.current_start + seg.len])
569
+ .any(|hash| deleted.contains(hash))
570
+ }
571
+ }
572
+
573
+ /// Where a segment ends in the baseline and in the current image.
574
+ fn segment_end(seg: &RowSegment) -> (usize, usize) {
575
+ let (baseline, current) = (seg.baseline_start, seg.current_start);
576
+ match seg.kind {
577
+ RowSegmentKind::Insert => (baseline, current + seg.len),
578
+ RowSegmentKind::Delete => (baseline + seg.len, current),
579
+ RowSegmentKind::Equal | RowSegmentKind::Replace => (baseline + seg.len, current + seg.len),
580
+ }
581
+ }
582
+
583
+ /// One past the last edit of the hunk that starts with the edit at `start`.
584
+ ///
585
+ /// A hunk is a group of edits with only filler rows between them.
586
+ fn hunk_end(segments: &[RowSegment], start: usize, hashes: &RowHashes) -> usize {
587
+ let mut end = start + 1;
588
+ for (index, seg) in segments.iter().enumerate().skip(start + 1) {
589
+ if seg.kind != RowSegmentKind::Equal {
590
+ end = index + 1;
591
+ } else if !hashes.is_filler(seg) {
592
+ break;
593
+ }
594
+ }
595
+ end
596
+ }
597
+
598
+ /// The segments the diff image, the clusters and the bands draw.
599
+ ///
600
+ /// A region replaced by content of another height is one change to a reader.
601
+ /// Myers threads the blank rows both versions have through that region, so its
602
+ /// segments show separate inserts and deletes, and a diff image drawn from them
603
+ /// shows bands where the content changed. The picture draws such a hunk as one
604
+ /// changed region, then the rows one side has over the other, unless the hunk
605
+ /// moves rows. The segments stay as they are, because the counts, the residual
606
+ /// and the SSIM come from them.
607
+ fn picture_segments(segments: &[RowSegment], hashes: &RowHashes) -> Vec<RowSegment> {
608
+ let mut picture = Vec::with_capacity(segments.len());
609
+ let mut index = 0;
610
+
611
+ while index < segments.len() {
612
+ if segments[index].kind == RowSegmentKind::Equal {
613
+ picture.push(segments[index]);
614
+ index += 1;
615
+ continue;
616
+ }
617
+
618
+ let end = hunk_end(segments, index, hashes);
619
+ let hunk = &segments[index..end];
620
+ index = end;
621
+
622
+ let has_insert = hunk.iter().any(|seg| seg.kind == RowSegmentKind::Insert);
623
+ let has_delete = hunk.iter().any(|seg| seg.kind == RowSegmentKind::Delete);
624
+ if has_insert && has_delete && !hashes.moves_rows(hunk) {
625
+ let first = hunk[0];
626
+ let (baseline_end, current_end) = segment_end(&hunk[hunk.len() - 1]);
627
+ push_paired(
628
+ &mut picture,
629
+ first.baseline_start,
630
+ first.current_start,
631
+ baseline_end - first.baseline_start,
632
+ current_end - first.current_start,
633
+ );
634
+ } else {
635
+ picture.extend_from_slice(hunk);
636
+ }
637
+ }
638
+
639
+ picture
499
640
  }
500
641
 
501
642
  /// Bands sit in current-image coordinates so they overlay the current screenshot.
@@ -644,7 +785,11 @@ pub(crate) fn compute_row_alignment(
644
785
 
645
786
  slide_edits_together(&mut runs, &baseline_hashes, &current_hashes);
646
787
  let segments = pair_replacements(&runs);
647
- let bands = shift_bands(&segments);
788
+ let picture = picture_segments(
789
+ &segments,
790
+ &RowHashes::new(&baseline_hashes, &current_hashes),
791
+ );
792
+ let bands = shift_bands(&picture);
648
793
 
649
794
  let rows_of = |kind: RowSegmentKind| -> usize {
650
795
  segments
@@ -663,6 +808,7 @@ pub(crate) fn compute_row_alignment(
663
808
  residual_count: residual_count(images, &segments, pixel_options)?,
664
809
  segments,
665
810
  bands,
811
+ picture,
666
812
  width: images.width,
667
813
  baseline_height: images.baseline_height,
668
814
  current_height: images.current_height,
@@ -695,11 +841,11 @@ fn check_alignment(
695
841
  Ok(())
696
842
  }
697
843
 
698
- /// Cluster the residual — the pixels that differ inside `Replace` segments.
844
+ /// Cluster the pixels that differ inside the changed regions the diff image draws.
699
845
  ///
700
846
  /// The mask is in current-image coordinates. Shift bands are not in it: the
701
- /// bands are the shift signal and the mask is the residual, and a caller decides
702
- /// separately whether to absorb a shift or flag it, reading
847
+ /// bands are the shift signal and the mask is the changed content, and a caller
848
+ /// decides separately whether to absorb a shift or flag it, reading
703
849
  /// [`RowAlignment::bands`] for that.
704
850
  pub(crate) fn aligned_clusters(
705
851
  images: &RowImages,
@@ -714,7 +860,7 @@ pub(crate) fn aligned_clusters(
714
860
  let width = images.width;
715
861
  let mut mask = vec![false; width * images.current_height];
716
862
 
717
- for seg in replace_segments(&alignment.segments) {
863
+ for seg in replace_segments(&alignment.picture) {
718
864
  let segment_mask = segment_mask(images, seg, pixel_options)?;
719
865
  let start = seg.current_start * width;
720
866
  mask[start..start + seg.len * width].copy_from_slice(&segment_mask);
@@ -746,9 +892,13 @@ fn draw_band(diff: &mut [u8], width: usize, height: usize, band: &ShiftBand, col
746
892
 
747
893
  /// Build the diff image in current-image coordinates.
748
894
  ///
749
- /// Equal rows are grayed like pixelmatch does, `Replace` rows carry the usual
895
+ /// Equal rows are grayed like pixelmatch does, changed rows carry the usual
750
896
  /// pixelmatch coloring, inserted bands are filled with `diff_color`, and a
751
897
  /// deleted band is one seam row in `diff_color_alt` when set.
898
+ ///
899
+ /// The image draws the picture segments, not [`RowAlignment::segments`]; see
900
+ /// `picture_segments`. `diff_count` counts the changed pixels drawn, so it can
901
+ /// be larger than `residual_count`.
752
902
  pub(crate) fn aligned_diff_image_rgba(
753
903
  images: &RowImages,
754
904
  alignment: &RowAlignment,
@@ -773,7 +923,7 @@ pub(crate) fn aligned_diff_image_rgba(
773
923
  }
774
924
 
775
925
  let mut diff_count = 0;
776
- for seg in replace_segments(&alignment.segments) {
926
+ for seg in replace_segments(&alignment.picture) {
777
927
  let window = segment_window(images, seg);
778
928
  let output = pixelmatch_rgba(
779
929
  window.baseline,
@@ -794,7 +944,7 @@ pub(crate) fn aligned_diff_image_rgba(
794
944
  let deleted_color = pixel_options
795
945
  .diff_color_alt
796
946
  .unwrap_or(pixel_options.diff_color);
797
- for band in &alignment.bands {
947
+ for band in &shift_bands(&alignment.picture) {
798
948
  let color = match band.kind {
799
949
  ShiftBandKind::Inserted => pixel_options.diff_color,
800
950
  ShiftBandKind::Deleted => deleted_color,
@@ -882,6 +1032,53 @@ mod tests {
882
1032
  (edit_distance, pair_replacements(&runs))
883
1033
  }
884
1034
 
1035
+ fn picture_of(baseline: &[u64], current: &[u64]) -> (Vec<RowSegment>, Vec<RowSegment>) {
1036
+ let (_, segments) = segments_of(baseline, current, 32);
1037
+ let picture = picture_segments(&segments, &RowHashes::new(baseline, current));
1038
+ (segments, picture)
1039
+ }
1040
+
1041
+ #[test]
1042
+ fn content_swapped_across_blank_rows_draws_as_one_change() {
1043
+ // A tall chart (20..=23) swapped for a short one (30, 31), with the blank
1044
+ // row 0 above, below and between them. Myers matches the blank rows
1045
+ // inside the region, so the segments read as a shift.
1046
+ let baseline = [1u64, 0, 0, 20, 21, 22, 23, 0, 0, 9];
1047
+ let current = [1u64, 0, 30, 0, 31, 0, 9];
1048
+
1049
+ let (segments, picture) = picture_of(&baseline, &current);
1050
+
1051
+ assert!(segments.iter().any(|s| s.kind == RowSegmentKind::Insert));
1052
+ assert!(segments.iter().any(|s| s.kind == RowSegmentKind::Delete));
1053
+ assert_eq!(
1054
+ picture,
1055
+ vec![
1056
+ segment(RowSegmentKind::Equal, 0, 0, 2),
1057
+ segment(RowSegmentKind::Replace, 2, 2, 3),
1058
+ segment(RowSegmentKind::Delete, 5, 5, 3),
1059
+ segment(RowSegmentKind::Equal, 8, 5, 1),
1060
+ segment(RowSegmentKind::Equal, 9, 6, 1),
1061
+ ]
1062
+ );
1063
+ }
1064
+
1065
+ #[test]
1066
+ fn moved_rows_and_varied_gaps_draw_as_their_segments() {
1067
+ let cases: [(&[u64], &[u64]); 2] = [
1068
+ // A line that moved down through blank rows.
1069
+ (&[1, 5, 0, 0, 0, 9], &[1, 0, 0, 0, 5, 9]),
1070
+ // Rows 2 and 3 repeat at the end, the way a list repeats its items,
1071
+ // but they differ from each other and so keep the edits apart.
1072
+ (&[1, 7, 2, 3, 4, 5, 2, 3], &[1, 2, 3, 8, 4, 5, 2, 3]),
1073
+ ];
1074
+
1075
+ for (baseline, current) in cases {
1076
+ let (segments, picture) = picture_of(baseline, current);
1077
+ assert!(segments.iter().any(|s| s.kind == RowSegmentKind::Insert));
1078
+ assert_eq!(picture, segments);
1079
+ }
1080
+ }
1081
+
885
1082
  #[test]
886
1083
  fn identical_sequences_have_no_edits() {
887
1084
  let rows = [1u64, 2, 3, 4];
@@ -235,10 +235,10 @@ impl Comparison {
235
235
  Ok(alignment)
236
236
  }
237
237
 
238
- /// Cluster the residual differences inside `Replace` segments.
238
+ /// Cluster the changed content the aligned diff image draws.
239
239
  ///
240
- /// The mask is in current-image coordinates and holds the residual only. The
241
- /// shift bands are the other half of the answer: read
240
+ /// The mask is in current-image coordinates and holds the changed content
241
+ /// only. The shift bands are the other half of the answer: read
242
242
  /// [`RowAlignment::bands`] to decide whether to absorb a shift or flag it.
243
243
  pub fn aligned_clusters(
244
244
  &self,
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "pixelhog-python"
3
- version = "1.3.0"
3
+ version = "1.3.2"
4
4
  edition = "2021"
5
5
  publish = false
6
6
 
@@ -730,11 +730,12 @@ impl ComparisonPy {
730
730
  Py::new(py, row_alignment_py(py, alignment)?)
731
731
  }
732
732
 
733
- /// Cluster the residual differences, ignoring the shifted rows.
733
+ /// Cluster the changed content, ignoring the shifted rows.
734
734
  ///
735
- /// The mask holds the residual only. The shift bands are the other half of
736
- /// the answer: read `alignment.bands` to decide whether to absorb a shift or
737
- /// flag it. Raises if the alignment failed or came from another image pair.
735
+ /// The mask holds the changed content only, drawn like `aligned_diff_image`.
736
+ /// The shift bands are the other half of the answer: read `alignment.bands`
737
+ /// to decide whether to absorb a shift or flag it. Raises if the alignment
738
+ /// failed or came from another image pair.
738
739
  #[pyo3(signature = (alignment, threshold = 0.1, include_aa = false, min_pixels = 16, min_side = 0, dilation = 4, max_clusters = None, merge_gap = 0, merge_overlap = 0.5))]
739
740
  fn aligned_clusters(
740
741
  &self,
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "pixelhog"
7
- version = "1.3.0"
7
+ version = "1.3.2"
8
8
  description = "Rust-accelerated pixelmatch and SSIM for PNG bytes"
9
9
  readme = "README.md"
10
10
  license = { file = "LICENSE" }
@@ -16,6 +16,7 @@ classifiers = [
16
16
  "Programming Language :: Python :: 3.12",
17
17
  "Programming Language :: Python :: 3.13",
18
18
  "Programming Language :: Python :: 3.14",
19
+ "Programming Language :: Python :: 3.15",
19
20
  "Programming Language :: Rust",
20
21
  "Operating System :: OS Independent",
21
22
  "License :: OSI Approved :: MIT License",
File without changes
File without changes