From 57841a0a34f9e54acde01bcc7d4672561c430a89 Mon Sep 17 00:00:00 2001 From: Oneric Date: Thu, 16 Jul 2020 23:21:25 +0200 Subject: [PATCH] AlignToVideo: Handle tolerance = 0 correctly Checking if the diff is '>' instead of '>=' will always fail if tolerance=0, even if the colours are identical. This will cause the line to get a startime greter than its end time, which is not desireable. Rounding errors and limits of floating type precision might still affect the comparison. An additional sanity check before calculation is added to ensure the selected position and colour match within tolerance. This allows us to refactor the search code to never check the starting frame and guanrantees valid timings with start @@ -290,28 +290,30 @@ namespace { auto view = interleaved_view(frame->width, frame->height, reinterpret_cast(frame->data.data()), frame->pitch); if (frame->flipped) y = frame->height - y; + + // Ensure selected color and position match + if(!check_point(*view.at(x,y), lab, tolerance)) + { + wxMessageBox(_("Selected position and color are not within tolerance!")); + return; + } + int lrud[4]; calculate_point(view, x, y, lab, tolerance, lrud); // find forward #define CHECK_EXISTS_POS check_exists(pos, x, y, lrud, lab, tolerance) - while (pos >= 0) - { - if (CHECK_EXISTS_POS) - pos -= 2; - else break; - } + do { + pos -= 2; + } while (pos >= 0 && !CHECK_EXISTS_POS) pos++; pos = std::max(0, pos); auto left = CHECK_EXISTS_POS ? pos : pos + 1; pos = current_n_frame; - while (pos < n_frames) - { - if (CHECK_EXISTS_POS) - pos += 2; - else break; - } + do { + pos += 2; + } while (pos < n_frames && !CHECK_EXISTS_POS) pos--; pos = std::min(pos, n_frames - 1); auto right = CHECK_EXISTS_POS ? pos : pos - 1;