#include "pixels.h" #include "fuck_intel.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::nullopt; } const auto fileSize = std::filesystem::file_size(filepath); if (fileSize != sizeof(Pixels)) { std::cerr << "wrong file size: " << filepath << std::endl; std::cerr << "sizeof(Pixels): " << sizeof(Pixels) << std::endl; return std::nullopt; } std::ifstream ifs(filepath, std::ios::in | std::ios::binary); if (!ifs) return std::nullopt; Pixels result; ifs.read(reinterpret_cast(&result), sizeof(Pixels)); ifs.close(); if (!ifs) { std::cerr << "cannot read " << filepath << std::endl; return std::nullopt; } result.debugPrintIfInvalid(); 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; } debugPrintIfInvalid(); std::cout << "saved pixels at enc pos " << counters.encoderPosition << std::endl; return true; } Pixels::operator bool() const { bool result = std::find_if(pixels.cbegin(), pixels.cend(), [](const auto& p) { return !qFuzzyIsNull(p) && !std::isnan(p); }) != pixels.cend(); // std::cout << __func__ << ":\t" << (result ? "true" : "false") << std::endl; return result; } bool Pixels::debugPrintIfInvalid() const { // const bool valid{std::find_if(std::execution::par_unseq, // pixels.cbegin(), // pixels.cend(), // [](const auto &p) { return p > 280; }) // == pixels.cend()}; // if (!valid) { // qCritical() << Q_FUNC_INFO << "got invalid profile:" << *this; // } // return valid; return true; } // std::lock_guard Pixels::lock() // { // return std::move(std::lock_guard{m_mtx}); // } QDebug &operator<<(QDebug &debug, const Pixels &pixels) { QDebugStateSaver saver{debug}; const auto &c = pixels.counters; const auto &px = pixels.pixels; QList numbers; for (size_t i{0}; i < 4 && i < px.size(); ++i) { numbers << px.at(i); } for (size_t i{(px.size() - 4) / 2}; i < px.size() / 2 + 2 && i < px.size(); ++i) { numbers << px.at(i); } for (size_t i{px.size() - 4 - 1}; i < px.size(); ++i) { numbers << px.at(i); } debug.nospace().noquote() << QStringLiteral("Pixels { ts: %1 mc: %2 ep: %3 [ %4 ] }") .arg(QString::number(c.timestampUs), QString::number(c.measurementCounter), QString::number(c.encoderPosition)) << qSetRealNumberPrecision(4) << numbers; return debug; }