Skip to content

Instantly share code, notes, and snippets.

@x100ex
Last active June 19, 2017 07:15
Show Gist options
  • Save x100ex/bc431c0fdf2648438a44ff246027b7f3 to your computer and use it in GitHub Desktop.
Save x100ex/bc431c0fdf2648438a44ff246027b7f3 to your computer and use it in GitHub Desktop.
вычисление габаритов корректных пикселей
// Copyright (C) 2017, Aleksander A. Popov.
//
// Вычисление габаритов корректных пикселей
// одновременно с расчётом этих пикселей
//
#include <QRect>
std::pair<int, int> find_nonzero_range(const std::vector<int>& v) {
int s = static_cast<int>(v.size());
int imin = 0;
while (imin < s && v[imin] == 0) {
imin++;
}
int imax = s - 1;
while (imax >= 0 && v[imax] == 0) {
imax--;
}
return std::pair<int, int>(imin, imax);
}
void calc(int sx, int sy, QRect* output_valid_rect) {
DCHECK_GE(sx, 1);
DCHECK_GE(sy, 1);
DCHECK(output_valid_rect);
std::vector<int> count_valid_x, count_valid_y;
count_valid_x.clear();
count_valid_y.clear();
count_valid_x.resize(sx, sy);
count_valid_y.resize(sy, sx);
for (int y = 0; y < sy; y++) {
for (int x = 0; x < sx; x++) {
bool is_valid_value = false;
// ... calc value
if (is_valid_value) {
// ...
} else {
// ...
count_valid_x[x]--;
count_valid_y[y]--;
}
}
}
std::pair<int, int> x_range = find_nonzero_range(count_valid_x);
std::pair<int, int> y_range = find_nonzero_range(count_valid_y);
*output_valid_rect = QRect(QPoint(x_range.first, y_range.first),
QPoint(x_range.second, y_range.second));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment