90 lines
2.8 KiB
C++
90 lines
2.8 KiB
C++
#include <iostream>
|
|
#include <string>
|
|
#include <random>
|
|
#include <vector>
|
|
//Command-line parsing
|
|
#include <CLI11.hpp>
|
|
|
|
//Image filtering and I/O
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
#include <stb_image.h>
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
#include <stb_image_write.h>
|
|
|
|
//Global flag to silent verbose messages
|
|
bool silent;
|
|
bool test_energy;
|
|
|
|
|
|
/** e_1 energy */
|
|
std::vector<unsigned char> energy_e1(unsigned char* source, int width, int height, int nbChannels) {
|
|
std::vector<unsigned char> energy(width*height);
|
|
|
|
for (auto i=0; i < width*height; i++) { energy[i] = 0; }
|
|
|
|
for(auto i = 0 ; i < width ; ++i) {
|
|
for(auto j = 0; j < height; ++j) {
|
|
auto indexPixel = nbChannels*(width*j+i);
|
|
auto indexPixel_up = (j-1 > 0) ? nbChannels*(width*(j-1)+i) : indexPixel;
|
|
auto indexPixel_down = (j+1 < height) ? nbChannels*(width*(j+1)+i) : indexPixel;
|
|
auto indexPixel_left = (i-1 > 0) ? nbChannels*(width*j+(i-1)) : indexPixel;
|
|
auto indexPixel_right = (i+1 < width) ? nbChannels*(width*j+(i+1)) : indexPixel;
|
|
|
|
for (auto ch=0; ch < nbChannels; ch++) {
|
|
energy[width*j+i] += (
|
|
fabs((float)source[indexPixel_up+ch] - source[indexPixel+ch])
|
|
+fabs((float)source[indexPixel_down+ch] - source[indexPixel+ch])
|
|
+fabs((float)source[indexPixel_left+ch] - source[indexPixel+ch])
|
|
+fabs((float)source[indexPixel_right+ch] - source[indexPixel+ch])
|
|
)/(nbChannels*4);
|
|
}
|
|
}
|
|
}
|
|
|
|
return energy;
|
|
}
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
CLI::App app{"seam-carving"};
|
|
std::string sourceImage;
|
|
app.add_option("-s,--source", sourceImage, "Source image")->required()->check(CLI::ExistingFile);;
|
|
std::string outputImage= "output.png";
|
|
app.add_option("-o,--output", outputImage, "Output image")->required();
|
|
silent = false;
|
|
app.add_flag("--silent", silent, "No verbose messages");
|
|
test_energy = false;
|
|
app.add_flag("--test-energy", test_energy, "Don't resize image, just try the specified energy function");
|
|
CLI11_PARSE(app, argc, argv);
|
|
|
|
//Image loading
|
|
int width, height, nbChannels;
|
|
unsigned char *source = stbi_load(sourceImage.c_str(), &width, &height, &nbChannels, 0);
|
|
|
|
if (nbChannels < 3) { // TODO : really ?
|
|
std::cout<< "Input images must be RGB images."<<std::endl;
|
|
exit(1);
|
|
}
|
|
|
|
int outChannels = nbChannels;
|
|
std::vector<unsigned char> output;
|
|
|
|
if (test_energy) {
|
|
outChannels = 1;
|
|
output = energy_e1(source, width, height, nbChannels);
|
|
} else {
|
|
std::cout <<"Not implemented" << std::endl;
|
|
exit(1);
|
|
}
|
|
|
|
//Final export
|
|
if (!silent) std::cout<<"Exporting.."<<std::endl;
|
|
int errcode = stbi_write_png(outputImage.c_str(), width, height, outChannels, output.data(), outChannels*width);
|
|
if (!errcode) {
|
|
std::cout<<"Error while exporting the resulting image."<<std::endl;
|
|
exit(errcode);
|
|
}
|
|
|
|
stbi_image_free(source);
|
|
exit(0);
|
|
} |