#include #include #include #include //Command-line parsing #include //Image filtering and I/O #define STB_IMAGE_IMPLEMENTATION #include #define STB_IMAGE_WRITE_IMPLEMENTATION #include //Global flag to silent verbose messages bool silent; bool test_energy; /** e_1 energy */ std::vector energy_e1(unsigned char* source, int width, int height, int nbChannels) { std::vector 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."< 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.."<