From 9dde2ab53c8e2c97647164fce89cf149260fbc8f Mon Sep 17 00:00:00 2001 From: Nikita Kostovsky Date: Sun, 12 Jan 2025 11:50:34 +0100 Subject: implement calibration --- src/pixels.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/pixels.cpp (limited to 'src/pixels.cpp') diff --git a/src/pixels.cpp b/src/pixels.cpp new file mode 100644 index 0000000..b314e6a --- /dev/null +++ b/src/pixels.cpp @@ -0,0 +1,95 @@ +#include "pixels.h" + +#include +#include +#include +#include + +Pixels& Pixels::operator+=(const Pixels& other) +{ + std::transform( + std::execution::par, + pixels.begin(), + pixels.end(), + other.pixels.begin(), + pixels.begin(), + // [](auto& toAdd) { return dst += src; }); + std::plus<>() + ); + + return *this; +} + +Pixels& Pixels::operator/=(const float divider) +{ + std::for_each( + std::execution::par_unseq, + pixels.begin(), + pixels.end(), + [divider](auto& pixel) { pixel /= divider; } + ); + + return *this; +} + +std::optional Pixels::load(const QString& filename) +{ + const std::filesystem::path filepath{filename.toStdString()}; + + if (!std::filesystem::exists(filepath)) + { + std::cerr << "no such file: " << filepath << std::endl; + + return {}; + } + + std::ifstream ifs(filepath, std::ios::in | std::ios::binary); + + if (!ifs) + return {}; + + Pixels result; + ifs.read(reinterpret_cast(&result), sizeof(Pixels)); + ifs.close(); + + if (!ifs) + { + std::cerr << "cannot read " << filepath << std::endl; + + return {}; + } + + return result; +} + +bool Pixels::save(const QString& filename) +{ + const std::filesystem::path filepath{filename.toStdString()}; + const auto parent_path = filepath.parent_path(); + + if (!std::filesystem::exists(parent_path) && + !std::filesystem::create_directories(parent_path)) + { + std::cerr << "cannot create parent directory for file " << filepath + << std::endl; + + return false; + } + + std::ofstream ofs(filepath, std::ios::out | std::ios::binary); + + if (!ofs) + return false; + + ofs.write(reinterpret_cast(this), sizeof(Pixels)); + ofs.close(); + + if (!ofs) + { + std::cerr << "cannot write " << filepath << std::endl; + + return false; + } + + return true; +} -- cgit v1.2.3-70-g09d2