|
| 1 | +//////////////////////////////////////////////////////////////////////////////// |
| 2 | +#include <CLI/CLI.hpp> |
| 3 | +#include <YImage.hpp> |
| 4 | +#include <vector> |
| 5 | +#include <vor2d/CompressedImage.h> |
| 6 | +#include <vor2d/Dexelize.h> |
| 7 | +#include <vor2d/DoubleCompressedImage.h> |
| 8 | +//////////////////////////////////////////////////////////////////////////////// |
| 9 | + |
| 10 | +#define WHITE YImage::YPixel({255, 255, 255, 255}) |
| 11 | +#define BLACK YImage::YPixel({0, 0, 0, 255}) |
| 12 | + |
| 13 | +bool pixel_is_white(YImage::YPixel p) { |
| 14 | + return p.r > 128 || p.g > 128 || p.b > 128 || p.a > 128; |
| 15 | +} |
| 16 | + |
| 17 | +int main(int argc, char *argv[]) { |
| 18 | + // Default arguments |
| 19 | + struct { |
| 20 | + std::string input; |
| 21 | + std::string output = "out.png"; |
| 22 | + double radius = 0; |
| 23 | + bool erode = false; |
| 24 | + bool force = false; |
| 25 | + bool transpose = false; |
| 26 | + bool negate = false; |
| 27 | + } args; |
| 28 | + |
| 29 | + // Parse arguments |
| 30 | + CLI::App app("Offset2D"); |
| 31 | + app.add_option("input,-i,--input", args.input, "Input image.") |
| 32 | + ->required() |
| 33 | + ->check(CLI::ExistingFile); |
| 34 | + app.add_option("output,-o,--output", args.output, "Output image."); |
| 35 | + app.add_option("-r,--radius", args.radius, "Dilation/erosion radius."); |
| 36 | + app.add_flag("-e,--erode", args.erode, "Erode instead of dilate."); |
| 37 | + app.add_flag("-f,--force", args.force, "Overwrite output file."); |
| 38 | + app.add_flag("-t,--transpose", args.transpose, "Transpose input image."); |
| 39 | + app.add_flag("-n,--negate", args.negate, "Negate input image."); |
| 40 | + try { |
| 41 | + app.parse(argc, argv); |
| 42 | + } catch (const CLI::ParseError &e) { |
| 43 | + return app.exit(e); |
| 44 | + } |
| 45 | + |
| 46 | + // Load SVG |
| 47 | + vor::DoubleCompressedImage dexels = vor::create_dexels(args.input.c_str()); |
| 48 | + |
| 49 | + // Pre-processing operations |
| 50 | + if (args.transpose) { |
| 51 | + dexels.transposeInPlace(); |
| 52 | + } |
| 53 | + if (args.negate) { |
| 54 | + dexels.negate(); |
| 55 | + } |
| 56 | + |
| 57 | + // Offset |
| 58 | + if (args.radius > 0) { |
| 59 | + std::cout << "-- Performing offset by radius r = " << args.radius |
| 60 | + << std::endl; |
| 61 | + if (args.erode) { |
| 62 | + dexels.erode(args.radius); |
| 63 | + } else { |
| 64 | + dexels.dilate(args.radius); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Save output image |
| 69 | + if (std::ifstream(args.output)) { |
| 70 | + // Output file exists! |
| 71 | + if (args.force) { |
| 72 | + std::cout << "-- Overwriting output file: " << args.output << std::endl; |
| 73 | + vor::dexel_dump(args.output.c_str(), dexels); |
| 74 | + } else { |
| 75 | + std::cerr << "-- Output file already exists. Please use -f to force overwriting." |
| 76 | + << std::endl; |
| 77 | + } |
| 78 | + } else { |
| 79 | + std::cout << "-- Saving" << std::endl; |
| 80 | + vor::dexel_dump(args.output.c_str(), dexels); |
| 81 | + } |
| 82 | + return 0; |
| 83 | +} |
0 commit comments