pathanimator/c++/main.cpp

80 lines
2.0 KiB
C++

/// \file main.cpp
/// \copyright 2022 Sascha Nitsch
/// Licenced under MIT
/// \brief definition of the main class of the application
// system includes
#include <stdio.h>
#include <string.h>
#include <OpenImageIO/imageio.h>
// own includes
#include "main.h"
// algorithms
#include "algorithm/distancepath.h"
Main::Main() {
m_algorithm = NULL;
}
Main::~Main() {
delete m_algorithm;
}
bool Main::init(int argc, const char* argv[]) {
if (argc < 3) {
printUsage(argv[0]);
return false;
}
m_inputFilename = argv[1];
m_outputFilename = argv[2];
const char* algorithmName = argv[3];
if (!strcmp(algorithmName, "distancepath")) {
m_algorithm = new Algorithm::DistancePath(argc - 4, &argv[4]);
}
if (m_algorithm) {
return true;
}
printf("algorithm \"%s\" not found\n", algorithmName);
return false;
}
void Main::printUsage(const char* programName) {
printf("Usage: %s input.png output.png algorithm <algoritm arguments>\n", programName);
}
bool Main::run() {
if (!m_algorithm) {
return false;
}
// load input image
auto inp = OIIO::ImageInput::open(m_inputFilename);
if (!inp) {
printf("Loading of input image \"%s\" failed %s\n", m_inputFilename.c_str(), OIIO::geterror().c_str());
return false;
}
const OIIO::ImageSpec &spec = inp->spec();
Image input(spec.width, spec.height, spec.nchannels, 8);
input.allocateMemory();
inp->read_image(0, 0, 0, 3, OIIO::TypeDesc::UINT8, input.getPixels8());
inp->close();
// create output image
Image output(spec.width, spec.height, 3, 16);
output.allocateMemory();
int ret = m_algorithm->process(&input, &output);
if (ret == 0) {
OIIO::ImageSpec ospec;
ospec.width = spec.width;
ospec.height = spec.height;
ospec.nchannels = 3;
ospec.format = OIIO::TypeDesc::UINT16;
auto outp = OIIO::ImageOutput::create(m_outputFilename);
if (outp) {
outp->open(m_outputFilename, ospec);
outp->write_image(OIIO::TypeDesc::UINT16, output.getPixels16());
outp->close();
}
}
return ret == 0;
}