80 lines
2.4 KiB
C++
80 lines
2.4 KiB
C++
// Copyright (2019-2023) Paul Scherrer Institute
|
|
|
|
#ifndef JUNGFRAUJOCH_ROIFILTER_H
|
|
#define JUNGFRAUJOCH_ROIFILTER_H
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <vector>
|
|
#include "JFJochException.h"
|
|
|
|
class ROIFilter {
|
|
int32_t width, height;
|
|
std::vector<uint8_t> mask;
|
|
public:
|
|
ROIFilter(int32_t in_width, int32_t in_height, uint8_t fill_value = 0)
|
|
: width(in_width), height(in_height) {
|
|
if ((width < 0) || (height < 0))
|
|
throw JFJochException(JFJochExceptionCategory::InputParameterInvalid, "Negative dimensions are wrong");
|
|
mask = std::vector<uint8_t>(in_width * in_height, fill_value);
|
|
}
|
|
|
|
void SetRectangle(int32_t x0, int32_t y0, int32_t in_width, int32_t in_height, uint8_t mask_value = 1) {
|
|
if (x0 < 0) { in_width += x0; x0 = 0; }
|
|
if (in_width <= 0) return;
|
|
|
|
if (x0 >= width) return;
|
|
if (x0 + in_width >= width) in_width = width - x0;
|
|
|
|
if (y0 < 0) { in_height += y0; y0 = 0; }
|
|
if (in_height <= 0) return;
|
|
|
|
if (y0 >= height) return;
|
|
if (y0 + in_height >= height) in_height = height - y0;
|
|
|
|
for (size_t y = y0; y < y0 + in_height; y++) {
|
|
for (size_t x = x0; x < x0 + in_width; x++) {
|
|
mask[y * width + x] |= mask_value;
|
|
}
|
|
}
|
|
}
|
|
|
|
void ClearRectangle(int32_t x0, int32_t y0, int32_t in_width, int32_t in_height, uint8_t mask_value = 1) {
|
|
if (x0 < 0) { in_width += x0; x0 = 0; }
|
|
if (in_width <= 0) return;
|
|
|
|
if (x0 >= width) return;
|
|
if (x0 + in_width >= width) in_width = width - x0;
|
|
|
|
if (y0 < 0) { in_height += y0; y0 = 0; }
|
|
if (in_height <= 0) return;
|
|
|
|
if (y0 >= height) return;
|
|
if (y0 + in_height >= height) in_height = height - y0;
|
|
|
|
for (size_t y = y0; y < y0 + in_height; y++) {
|
|
for (size_t x = x0; x < x0 + in_width; x++) {
|
|
mask[y * width + x] &= ~mask_value;
|
|
}
|
|
}
|
|
}
|
|
|
|
template <class T>
|
|
void Apply(T* data, T fill_value) const {
|
|
for (size_t i = 0; i < mask.size(); i++) {
|
|
if (mask[i] == 0)
|
|
data[i] = fill_value;
|
|
}
|
|
}
|
|
|
|
template <class T>
|
|
void Apply(std::vector<T> &data, T fill_value) const {
|
|
if (data.size() != mask.size())
|
|
throw JFJochException(JFJochExceptionCategory::ArrayOutOfBounds, "Mismatch in array size");
|
|
Apply(data.data(), fill_value);
|
|
}
|
|
};
|
|
|
|
|
|
#endif //JUNGFRAUJOCH_ROIFILTER_H
|