seam-carving/src/utils.cpp

63 lines
1.8 KiB
C++

#include <iostream>
#include <sstream>
#include <random>
#include <vector>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
#include "utils.hpp"
bool nearly_equal(float a, float b) {
return std::nextafter(a, std::numeric_limits<float>::lowest()) <= b &&
std::nextafter(a, std::numeric_limits<float>::max()) >= 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)
{
int dim_large = vertical ? width : height;
int dim_long = vertical ? height : width;
for (int i=0; i < dim_long; i++) {
if (std::get<0>(energy[im_index(i, opt_seam[i])]) == 0) return true;
}
return false;
}
std::vector<std::pair<int, float>> mask_energy(std::vector<float> energy,
int width, int height, unsigned char* mask) {
std::vector<std::pair<int, float>> output(width*height);
for (auto i=0; i < width*height; i++) {
output[i] = {mask[i], energy[i]};
}
return output;
}
std::pair<int, float> operator+(std::pair<int, float>& p1, std::pair<int, float>& p2) {
return {
// If one of the two pixels is "protected" (ie 2), we want to prevent this line removing
// Else, we want to keep the information of "is there a pixel to remove in this seam" (ie 0)
(p1.first==2) || (p2.first==2) ? 2 : std::min(p1.first, p2.first),
p1.second+p2.second
};
}
void operator+=(std::pair<int, float>& p1, std::pair<int, float>& p2) {
p1 = p1+p2;
}
std::string str_of_e(std::pair<int, float> energy) {
std::stringstream ss;
ss << "(" << energy.first << ", " << energy.second << ")";
return ss.str();
}
std::string str_of_e(float energy) {
std::stringstream ss;
ss << "(" << energy << ")";
return ss.str();
}