Add base files
This commit is contained in:
parent
6371a7921a
commit
c3010b112e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
build/
|
18
CMakeLists.txt
Normal file
18
CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#* From https://github.com/ttroy50/cmake-examples this falls under MIT License
|
||||||
|
# Set the minimum version of CMake that can be used
|
||||||
|
# To find the cmake version run
|
||||||
|
# $ cmake --version
|
||||||
|
cmake_minimum_required(VERSION 3.30)
|
||||||
|
|
||||||
|
# Set the project name
|
||||||
|
project (seam-carving)
|
||||||
|
|
||||||
|
# Add an executable with the above sources
|
||||||
|
add_executable(seam-carving src/seam-carving.cpp)
|
||||||
|
|
||||||
|
# Set the directories that should be included in the build command for this target
|
||||||
|
# when running g++ these will be included as -I/directory/path/
|
||||||
|
target_include_directories(seam-carving
|
||||||
|
PRIVATE
|
||||||
|
${PROJECT_SOURCE_DIR}/deps
|
||||||
|
)
|
12
README.md
12
README.md
@ -1,5 +1,15 @@
|
|||||||
# Seam Carving
|
# Seam Carving
|
||||||
|
|
||||||
## Refrences:
|
## References:
|
||||||
- [Seam carving for content-aware image resizing](https://perso.crans.org/frenoy/matlab2012/seamcarving.pdf)
|
- [Seam carving for content-aware image resizing](https://perso.crans.org/frenoy/matlab2012/seamcarving.pdf)
|
||||||
- [By-example Synthesis of Architectural Textures](https://inria.hal.science/inria-00547754v1/file/paper.pdf)
|
- [By-example Synthesis of Architectural Textures](https://inria.hal.science/inria-00547754v1/file/paper.pdf)
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mkdir -p build # Create build dir if needed
|
||||||
|
cd build
|
||||||
|
cmake .. # Update build/Makefile
|
||||||
|
make # Compile
|
||||||
|
cd ..
|
||||||
|
```
|
11527
deps/CLI11.hpp
vendored
Normal file
11527
deps/CLI11.hpp
vendored
Normal file
File diff suppressed because it is too large
Load Diff
7988
deps/stb_image.h
vendored
Normal file
7988
deps/stb_image.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
1724
deps/stb_image_write.h
vendored
Normal file
1724
deps/stb_image_write.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
67
src/seam-carving.cpp
Normal file
67
src/seam-carving.cpp
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#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;
|
||||||
|
|
||||||
|
|
||||||
|
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");
|
||||||
|
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) {
|
||||||
|
std::cout<< "Input images must be RGB images."<<std::endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Main computation
|
||||||
|
std::vector<unsigned char> output(width*height*nbChannels);
|
||||||
|
|
||||||
|
//As an example, we just scan the pixels of the source image
|
||||||
|
//and swap the color channels.
|
||||||
|
for(auto i = 0 ; i < width ; ++i) {
|
||||||
|
for(auto j = 0; j < height; ++j) {
|
||||||
|
auto indexPixel = nbChannels*(width*j+i);
|
||||||
|
unsigned char r = source[ indexPixel ];
|
||||||
|
unsigned char g = source[ indexPixel + 1];
|
||||||
|
unsigned char b = source[ indexPixel + 2];
|
||||||
|
//Swapping the channels
|
||||||
|
output[ indexPixel ] = b;
|
||||||
|
output[ indexPixel + 1 ] = g;
|
||||||
|
output[ indexPixel + 2 ] = r;
|
||||||
|
if (nbChannels == 4) // just copying the alpha value if any
|
||||||
|
output[ indexPixel + 3] = source[ indexPixel + 3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Final export
|
||||||
|
if (!silent) std::cout<<"Exporting.."<<std::endl;
|
||||||
|
int errcode = stbi_write_png(outputImage.c_str(), width, height, nbChannels, output.data(), nbChannels*width);
|
||||||
|
if (!errcode) {
|
||||||
|
std::cout<<"Error while exporting the resulting image."<<std::endl;
|
||||||
|
exit(errcode);
|
||||||
|
}
|
||||||
|
|
||||||
|
stbi_image_free(source);
|
||||||
|
exit(0);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user