seam-carving/src/utils.hpp
2025-04-04 12:10:59 +02:00

62 lines
1.8 KiB
C++

#include <random>
// Get index for any table indexed by [width*(i : height) + (j : width)], but a
// : dim_long, b : dim_large
#define im_index(a, b) (vertical ? (width * a + b) : (width * b + a))
// Check if two floats are nearly equal
bool nearly_equal(float a, float b);
bool does_seam_remove_mask(std::vector<std::pair<int, float>> energy, int width, int height, int nbChannels,
std::vector<int> opt_seam, bool vertical);
std::vector<std::pair<int, float>> mask_energy(std::vector<float> energy,
int width, int height, unsigned char* mask);
//* Operators overwrites
std::pair<int, float> operator+(std::pair<int, float>& p1, std::pair<int, float>& p2);
void operator+=(std::pair<int, float>& p1, std::pair<int, float>& p2);
bool operator==(std::pair<int, float>& p1, std::pair<int, float>& p2);
// Unfortunately, std::cout << (std::pair<>) does not work and can't be rewritten
std::string str_of_e(std::pair<int, float> energy);
std::string str_of_e(float energy);
namespace limits {
struct max_energy {
template<class T> operator T() {
return std::numeric_limits<T>::max();
}
operator std::pair<int, float>() {
return {3, std::numeric_limits<float>::max()};
}
};
struct null_energy {
template<class T> operator T() {
return 0.;
}
operator std::pair<int, float>() {
return {0, limits::null_energy()};
}
};
}
template <typename T>
void compute_seam_tot(std::vector<int> opt_seam, std::vector<T> energy, int width, int height, bool vertical) {
int dim_long = vertical ? height : width;
T sum = limits::null_energy();
for (int i=0; i < dim_long; i++) {
sum += energy[im_index(i, opt_seam[i])];
// std::cout << str_of_e(sum);
}
std::cout << "Computed sum: " << str_of_e(sum) << std::endl;
}