Skip to content

Commit

Permalink
morph: prefer bitwise NOT over bitwise XOR
Browse files Browse the repository at this point in the history
`~p` and `p ^ 255` produce the same result on uchar images, as XOR
affects only the lowest 8 bits.
  • Loading branch information
kleisauke committed Nov 3, 2024
1 parent 302689a commit 4e0b1b2
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
2 changes: 1 addition & 1 deletion libvips/morphology/morph.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ vips_dilate_gen(VipsRegion *out_region,
*/
result = 0;
for (i = 0; i < seq->nn128; i++)
result |= !coeff[i] ? p[off[i]] ^ 255 : p[off[i]];
result |= !coeff[i] ? ~p[off[i]] : p[off[i]];

*q = result;
}
Expand Down
8 changes: 3 additions & 5 deletions libvips/morphology/morph_hwy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ vips_dilate_uchar_hwy(VipsRegion *out_region, VipsRegion *ir, VipsRect *r,
*/
auto pix = LoadU(du8, p + offsets[i]);

pix = IfThenElse(Ne(mmk, one), Xor(pix, one), pix);
pix = IfThenElse(Ne(mmk, one), AndNot(pix, one), pix);
sum = Or(sum, pix);
}

Expand All @@ -109,7 +109,7 @@ vips_dilate_uchar_hwy(VipsRegion *out_region, VipsRegion *ir, VipsRect *r,
auto pix = LoadU(du8, p + offsets[i]);

if (!coeff[i])
pix = Xor(pix, one);
pix = AndNot(pix, one);
sum = Or(sum, pix);
}

Expand Down Expand Up @@ -146,9 +146,7 @@ vips_erode_uchar_hwy(VipsRegion *out_region, VipsRegion *ir, VipsRect *r,
*/
auto pix = LoadU(du8, p + offsets[i]);

pix = IfThenElse(Ne(mmk, one),
AndNot(pix, one),
pix);
pix = IfThenElse(Ne(mmk, one), AndNot(pix, one), pix);
sum = And(sum, pix);
}

Expand Down

0 comments on commit 4e0b1b2

Please sign in to comment.