c# - Byte comparison uses so much CPU -
well have function compare red, blue, green, , alpha pixels in image comparison loop comparing 2 images
code:
var o = 255 - c1.a; var t = tolerance < o ? o : tolerance; var b = aresimiliar(ref c1.b, ref c2.b, ref t); var g = aresimiliar(ref c1.g, ref c2.g, ref t); var r = aresimiliar(ref c1.r, ref c2.r, ref t); if (b && g && r) continue; [methodimpl(methodimploptions.aggressiveinlining)] private static bool aresimiliar(ref byte v1, ref byte v2, ref double tolerance) { var z = v1 - v2; var t = z > 0 ? z : -z; return t <= tolerance; }
the problem uses cpu although no external code being used, , testing narrowed down code causes load.
try instead
var o = 255 - c1.a; var t = tolerance < o ? o : tolerance; if (aresimiliar(c1.b, c2.b, t) && aresimiliar(c1.g, c2.g, t) && aresimiliar(c1.r, c2.r, t)) continue; [methodimpl(methodimploptions.aggressiveinlining)] private static bool aresimiliar(byte v1, byte v2, double tolerance) { var z = v1 - v2; var t = z > 0 ? z : -z; return t <= tolerance; }
this might better
var o = 255 - c1.a; var t = tolerance < o ? o : tolerance; if (abs(c1.b - c2.b) <= t) & //or && abs(c1.g - c2.g) <= t) & //or && abs(c1.r - c2.r) <= t)) continue; [methodimpl(methodimploptions.aggressiveinlining)] private static int abs(int d) { int y = (d >> 31); return (d ^ y) - y; }
Comments
Post a Comment