#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; } 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; } // std::lock_guard Pixels::lock() // { // return std::move(std::lock_guard{m_mtx}); // }