CaptureMoment 0.1.0
Free and open-source non-destructive photo editor
Loading...
Searching...
No Matches
working_image_context.cpp
Go to the documentation of this file.
1
13#include <spdlog/spdlog.h>
14#include <expected>
15
17
18bool WorkingImageContext::prepare(std::unique_ptr<Common::ImageRegion> original_tile)
19{
20 if (!original_tile) {
21 return false;
22 }
23
24 // 1. Always recreate the Data Owner to ensure memory addresses are stable
25 // (Prevents dangling pointers if std::vector reallocates internally)
26 try {
27 m_image_data = std::make_unique<WorkingImageData>(std::move(original_tile));
28 } catch (const std::exception& e) {
29 spdlog::error("[WorkingImageContext::prepare]: Exception creating data: {}", e.what());
30 return false;
31 }
32
33 if (!m_image_data->isValid()) {
34 spdlog::error("[WorkingImageContext::prepare]: Data is invalid after initialization.");
35 return false;
36 }
37
38 // 2. Create the Hardware Worker
39 auto new_hardware { WorkingImageFactory::create() };
40
41 if (!new_hardware) {
42 spdlog::error("[WorkingImageContext::prepare]: Failed to create Hardware backend.");
43 return false;
44 }
45
46 // 3. Construct the view from our safely owned data
47 ImageView view_data_image {
48 .working_data = m_image_data->getWorkingDataSpan(),
49 .original_data = m_image_data->getOriginalDataSpan(),
50 .width = m_image_data->getWidth(),
51 .height = m_image_data->getHeight(),
52 .channels = m_image_data->getChannels()
53 };
54
55 // 4. Bind the view to the hardware worker
56 if (!new_hardware->bindView(view_data_image)) {
57 spdlog::error("[WorkingImageContext::prepare]: Failed to bind data to hardware.");
58 return false;
59 }
60
61 m_working_image = std::move(new_hardware);
62 spdlog::debug("[WorkingImageContext::prepare]: Created and bound new WorkingImage.");
63 return true;
64}
65
66
68{
69 if (!m_image_data || !m_image_data->isValid()) {
70 spdlog::warn("[WorkingImageContext::resetToOriginal]: No valid data to reset.");
71 return;
72 }
73
74 // We reset the data at the Context level.
75 // Because the Hardware works on std::span (pointers), the change is instantaneous
76 // for the Hardware worker. No need to call a reset method on m_working_image!
77 m_image_data->restoreOriginalData();
78 spdlog::debug("[WorkingImageContext::resetToOriginal]: Data reset to original.");
79}
80
81std::shared_ptr<IWorkingImageHardware>
83{
84 return m_working_image;
85}
86
87bool WorkingImageContext::isReady() const noexcept
88{
89 return m_working_image && m_working_image->isValid();
90}
91
92std::expected<std::unique_ptr<Common::ImageRegion>, ErrorHandling::CoreError>
94{
95 if (!m_working_image) {
97 }
98 return m_working_image->getFullResImage();
99}
100
101std::expected<std::unique_ptr<Common::ImageRegion>, ErrorHandling::CoreError>
103{
104 if (!m_working_image) {
105 return std::unexpected(ErrorHandling::CoreError::InvalidWorkingImage);
106 }
107
108 return m_working_image->downsample(target_width, target_height);
109}
110
112{
113 m_working_image.reset();
114 m_image_data.reset();
115 spdlog::debug("[WorkingImageContext::release]: Released working image and data.");
116}
117
118} // namespace CaptureMoment::Core::ImageProcessing
119
std::expected< std::unique_ptr< Common::ImageRegion >, ErrorHandling::CoreError > getWorkingImageAsRegion() const
Exports the WorkingImage data to CPU.
std::expected< std::unique_ptr< Common::ImageRegion >, ErrorHandling::CoreError > getDownsampled(Common::ImageDim target_width, Common::ImageDim target_height)
Downsamples the WorkingImage to the specified dimensions.
std::shared_ptr< IWorkingImageHardware > getWorkingImage() noexcept
Gets the WorkingImage for processing.
void release() noexcept
Releases the managed WorkingImage.
bool prepare(std::unique_ptr< Common::ImageRegion > original_tile)
Prepares the context with a new source image.
bool isReady() const noexcept
Checks if a WorkingImage is ready for use.
void resetToOriginal()
Restores the internal working buffer to the original source data.
static std::unique_ptr< IWorkingImageHardware > create()
Creates a working image using the application's global configuration.
Central error handling system for CaptureMoment Core library.
Lightweight, non-owning view over image memory and its geometry.
std::size_t ImageDim
Type for image dimensions (width, height).
Definition image_types.h:20
CoreError
Enumeration of all error codes for CaptureMoment Core.
Definition core_error.h:36
@ InvalidWorkingImage
The working image state is invalid or corrupted.
Represents a zero-copy window over contiguous image memory.
Definition image_view.h:26
Manages the lifecycle of a single WorkingImage instance and its associated data.
Factory for creating IWorkingImageHardware instances using a Registry Pattern.
CaptureMoment — footer