MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ write()

bool MayaFlux::IO::EXRWriter::write ( const std::string &  filepath,
const ImageData data,
const ImageWriteOptions options = {} 
)
overridevirtual

Write image data to disk.

Parameters
filepathDestination path.
dataImage data. Must satisfy ImageData::is_consistent().
optionsFormat-specific options.
Returns
true on success. On failure call get_last_error().

Implements MayaFlux::IO::ImageWriter.

Definition at line 160 of file EXRWriter.cpp.

164{
165 m_last_error.clear();
166
167 if (!data.is_consistent()) {
168 m_last_error = "ImageData variant does not match declared ImageFormat";
170 return false;
171 }
172
173 if (data.width == 0 || data.height == 0 || data.channels == 0) {
174 m_last_error = "ImageData has zero dimensions";
176 return false;
177 }
178
179 const auto* src = data.as_float();
180 if (!src) {
181 m_last_error = "EXR requires float ImageData (uint8 and uint16 not supported)";
183 return false;
184 }
185
186 const size_t expected_elements = static_cast<size_t>(data.width) * data.height * data.channels;
187 if (src->size() < expected_elements) {
188 m_last_error = "EXR source buffer too small: expected "
189 + std::to_string(expected_elements)
190 + " got " + std::to_string(src->size());
192 return false;
193 }
194
195 const auto names = resolve_channel_names(data, options);
196 auto planes = deinterleave(*src, data.width, data.height, data.channels,
197 options.flip_vertically);
198
199 // ------------------------------------------------------------------------
200 // Populate EXRImage
201 // ------------------------------------------------------------------------
202 EXRImage image;
203 InitEXRImage(&image);
204
205 image.num_channels = static_cast<int>(data.channels);
206 image.width = static_cast<int>(data.width);
207 image.height = static_cast<int>(data.height);
208
209 std::vector<float*> plane_ptrs(data.channels);
210 for (uint32_t c = 0; c < data.channels; ++c) {
211 plane_ptrs[c] = planes[c].data();
212 }
213 image.images = reinterpret_cast<unsigned char**>(plane_ptrs.data());
214
215 // ------------------------------------------------------------------------
216 // Populate EXRHeader
217 // ------------------------------------------------------------------------
218 EXRHeader header;
219 InitEXRHeader(&header);
220
221 header.num_channels = static_cast<int>(data.channels);
222 header.compression_type = resolve_compression(options.compression);
223
224 std::vector<EXRChannelInfo> channel_infos(data.channels);
225 std::vector<int> pixel_types(data.channels, TINYEXR_PIXELTYPE_FLOAT);
226 std::vector<int> requested_pixel_types(data.channels, TINYEXR_PIXELTYPE_FLOAT);
227
228 for (uint32_t c = 0; c < data.channels; ++c) {
229 std::memset(&channel_infos[c], 0, sizeof(EXRChannelInfo));
230 const std::string& n = names[c];
231 const size_t copy_len = std::min<size_t>(n.size(), 255);
232 std::memcpy(channel_infos[c].name, n.data(), copy_len);
233 channel_infos[c].name[copy_len] = '\0';
234 }
235
236 header.channels = channel_infos.data();
237 header.pixel_types = pixel_types.data();
238 header.requested_pixel_types = requested_pixel_types.data();
239
240 // ------------------------------------------------------------------------
241 // Encode to memory, then flush
242 // ------------------------------------------------------------------------
243 unsigned char* encoded = nullptr;
244 const char* err = nullptr;
245
246 const size_t encoded_size = SaveEXRImageToMemory(
247 &image, &header, &encoded, &err);
248
249 if (encoded_size == 0 || encoded == nullptr) {
250 m_last_error = err ? std::string("tinyexr: ") + err
251 : "SaveEXRImageToMemory returned 0 bytes";
252 if (err) {
253 FreeEXRErrorMessage(err);
254 }
256 return false;
257 }
258
259 const bool ok = flush_to_disk(filepath, encoded, encoded_size);
260
261 std::free(encoded);
262
263 if (!ok) {
264 m_last_error = "Failed to write EXR file: " + filepath;
266 return false;
267 }
268
270 "Wrote EXR: {} ({}x{}, {} channels, compression {}, {} bytes)",
271 filepath, data.width, data.height, data.channels,
272 header.compression_type, encoded_size);
273
274 return true;
275}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
IO::ImageData image
Definition Decoder.cpp:57
std::vector< std::string > resolve_channel_names(const ImageData &data, const ImageWriteOptions &options) const
Resolve channel name list based on options and channel count.
Definition EXRWriter.cpp:92
static std::vector< std::vector< float > > deinterleave(const std::vector< float > &src, uint32_t width, uint32_t height, uint32_t channels, bool flip_vertically)
Deinterleave source float buffer into N planar channel buffers.
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.

References MayaFlux::IO::ImageData::as_float(), MayaFlux::IO::ImageData::channels, MayaFlux::IO::ImageWriteOptions::compression, deinterleave(), MayaFlux::Journal::FileIO, MayaFlux::IO::ImageWriteOptions::flip_vertically, MayaFlux::IO::ImageData::height, image, MayaFlux::Journal::IO, MayaFlux::IO::ImageData::is_consistent(), m_last_error, MF_ERROR, MF_INFO, resolve_channel_names(), and MayaFlux::IO::ImageData::width.

+ Here is the call graph for this function: