MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ImageWriter.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace MayaFlux::IO {
6
7/**
8 * @struct ImageWriteOptions
9 * @brief Configuration for image writing.
10 *
11 * Format-specific knobs are interpreted by the concrete writer; unsupported
12 * options are silently ignored. Compression maps differently per format:
13 * PNG : deflate level [0..9], default 6
14 * JPG : quality [1..100], default 90
15 * EXR : compression code (zip/piz/rle), default zip
16 * TGA : RLE flag, default false
17 */
19 int compression { -1 };
20 int quality { 90 };
21 bool flip_vertically { false };
22
23 std::vector<std::string> channel_names;
24};
25
26/**
27 * @class ImageWriter
28 * @brief Abstract base for image format writers.
29 *
30 * Parallels ImageReader. Each concrete writer handles one or more file
31 * extensions and validates that the supplied ImageData matches the format's
32 * expected pixel variant (uint8 for PNG/JPG/BMP/TGA, float for EXR/HDR,
33 * uint16 for 16-bit PNG).
34 *
35 * Writers are single-shot: one call to write() produces one file. No open/
36 * close lifecycle because image formats are whole-file atomic.
37 */
38class MAYAFLUX_API ImageWriter {
39public:
40 virtual ~ImageWriter() = default;
41
42 /**
43 * @brief Check whether this writer handles the given filepath.
44 */
45 [[nodiscard]] virtual bool can_write(const std::string& filepath) const = 0;
46
47 /**
48 * @brief Write image data to disk.
49 * @param filepath Destination path.
50 * @param data Image data. Must satisfy ImageData::is_consistent().
51 * @param options Format-specific options.
52 * @return true on success. On failure call get_last_error().
53 */
54 virtual bool write(
55 const std::string& filepath,
56 const ImageData& data,
57 const ImageWriteOptions& options = {}) = 0;
58
59 /**
60 * @brief File extensions handled by this writer (without dot).
61 */
62 [[nodiscard]] virtual std::vector<std::string> get_supported_extensions() const = 0;
63
64 /**
65 * @brief Last error message or empty string.
66 */
67 [[nodiscard]] virtual std::string get_last_error() const = 0;
68};
69
70using ImageWriterFactory = std::function<std::unique_ptr<ImageWriter>()>;
71
72/**
73 * @class ImageWriterRegistry
74 * @brief Singleton registry dispatching image writes by file extension.
75 *
76 * Mirrors FileReaderRegistry. Concrete writers register themselves on static
77 * initialization. create_writer(path) looks up the extension and returns a
78 * fresh instance of the appropriate writer, or nullptr if none is registered.
79 */
80class MAYAFLUX_API ImageWriterRegistry {
81public:
83 {
84 static ImageWriterRegistry registry;
85 return registry;
86 }
87
89 const std::vector<std::string>& extensions,
90 const ImageWriterFactory& factory)
91 {
92 for (const auto& ext : extensions) {
93 m_factories[ext] = factory;
94 }
95 }
96
97 [[nodiscard]] std::unique_ptr<ImageWriter> create_writer(const std::string& filepath) const
98 {
99 auto ext = std::filesystem::path(filepath).extension().string();
100 if (!ext.empty() && ext[0] == '.') {
101 ext = ext.substr(1);
102 }
103
104 auto it = m_factories.find(ext);
105 if (it != m_factories.end()) {
106 return it->second();
107 }
108 return nullptr;
109 }
110
111 [[nodiscard]] std::vector<std::string> get_registered_extensions() const
112 {
113 std::vector<std::string> exts;
114 exts.reserve(m_factories.size());
115 for (const auto& [ext, _] : m_factories) {
116 exts.push_back(ext);
117 }
118 return exts;
119 }
120
121private:
122 std::unordered_map<std::string, ImageWriterFactory> m_factories;
123};
124
125} // namespace MayaFlux::IO
std::unique_ptr< ImageWriter > create_writer(const std::string &filepath) const
static ImageWriterRegistry & instance()
std::vector< std::string > get_registered_extensions() const
void register_writer(const std::vector< std::string > &extensions, const ImageWriterFactory &factory)
std::unordered_map< std::string, ImageWriterFactory > m_factories
Singleton registry dispatching image writes by file extension.
virtual ~ImageWriter()=default
virtual std::string get_last_error() const =0
Last error message or empty string.
virtual bool write(const std::string &filepath, const ImageData &data, const ImageWriteOptions &options={})=0
Write image data to disk.
virtual bool can_write(const std::string &filepath) const =0
Check whether this writer handles the given filepath.
virtual std::vector< std::string > get_supported_extensions() const =0
File extensions handled by this writer (without dot).
Abstract base for image format writers.
std::function< std::unique_ptr< ImageWriter >()> ImageWriterFactory
Raw image data loaded from file.
std::vector< std::string > channel_names
Configuration for image writing.