summaryrefslogtreecommitdiff
path: root/src/pixels.cpp
diff options
context:
space:
mode:
authorNikita Kostovsky <nikita@kostovsky.me>2025-01-12 11:50:34 +0100
committerNikita Kostovsky <nikita@kostovsky.me>2025-01-12 11:50:34 +0100
commit9dde2ab53c8e2c97647164fce89cf149260fbc8f (patch)
treef428169ce67a93d0532d91883e18892736bb26b4 /src/pixels.cpp
parent8630381c7e1fa1527026b9c823790dc3f92c6321 (diff)
implement calibration
Diffstat (limited to 'src/pixels.cpp')
-rw-r--r--src/pixels.cpp95
1 files changed, 95 insertions, 0 deletions
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 <execution>
+#include <filesystem>
+#include <fstream>
+#include <iostream>
+
+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> 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<char*>(&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<const char*>(this), sizeof(Pixels));
+ ofs.close();
+
+ if (!ofs)
+ {
+ std::cerr << "cannot write " << filepath << std::endl;
+
+ return false;
+ }
+
+ return true;
+}