CaptureMoment 0.1.0
Free and open-source non-destructive photo editor
Loading...
Searching...
No Matches
working_image_data.cpp
Go to the documentation of this file.
1
9#include <spdlog/spdlog.h>
10#include <algorithm>
11
13
14
15WorkingImageData::WorkingImageData(std::unique_ptr<Common::ImageRegion> source)
16{
17 if (!source || !source->isValid()) {
18 spdlog::warn("[WorkingImageData]: Invalid source image provided");
19 return; // m_valid stays false, derived can check
20 }
21
22 auto result { initializeData(std::move(*source)) };
23 if (!result.has_value()) {
24 spdlog::error("[WorkingImageData]: Init failed: {}",
25 ErrorHandling::to_string(result.error()));
26 } else {
27 spdlog::debug("[WorkingImageData]: Initialized {}x{} ({} ch)",
28 m_width, m_height, m_channels);
29 }
30}
31
32std::expected<void, ErrorHandling::CoreError> WorkingImageData::initializeData(Common::ImageRegion&& source_image)
33{
34 if (!source_image.isValid()) {
35 spdlog::warn("[WorkingImageData::initializeData]: Input ImageRegion is invalid");
37 }
38
39 try {
40 // Cache dimensions and validity
41 m_data = std::move(source_image.m_data);
42
43 m_original_data = m_data;
44
45 m_width = source_image.width();
46 m_height = source_image.height();
47 m_channels = source_image.channels();
48 m_valid = true;
49
50 spdlog::debug("[WorkingImageData::initializeData]: Copied elements ({}x{}, {} ch)",
51 m_width, m_height, m_channels);
52
53 return {};
54
55 } catch (const std::bad_alloc& e) {
56 spdlog::critical("[WorkingImageData::initializeData]: Allocation failed: {}", e.what());
57 return std::unexpected(ErrorHandling::CoreError::AllocationFailed);
58 }
59}
60
62{
63 if (!m_valid || m_original_data.empty() || m_data.empty()) {
64 return;
65 }
66
67 if (m_original_data.size() != m_data.size()) {
68 spdlog::error("[WorkingImageData::restoreOriginalData]: Size mismatch! Original: {}, Working: {}",
69 m_original_data.size(), m_data.size());
70 return;
71 }
72
73 std::ranges::copy(m_original_data.begin(), m_original_data.end(), m_data.begin());
74}
75
76} // namespace CaptureMoment::Core::ImageProcessing
std::expected< void, ErrorHandling::CoreError > initializeData(Common::ImageRegion &&source_image)
Re-initializes the internal buffer with a new ImageRegion.
WorkingImageData(std::unique_ptr< Common::ImageRegion > source)
Initializes the working image data from an ImageRegion.
void restoreOriginalData()
Restores the original image data.
constexpr std::string_view to_string(CoreError code)
Converts a CoreError code to a human-readable string.
Definition core_error.h:197
@ InvalidImageRegion
The provided ImageRegion dimensions or data are invalid.
@ AllocationFailed
Memory allocation failed.
Represents a rectangular region of an image and its pixel data.
Base class providing raw buffer storage and metadata for all working image implementations.
CaptureMoment — footer