13 std::string extension_of(
const std::string& filepath)
15 auto ext = std::filesystem::path(filepath).extension().string();
16 if (!ext.empty() && ext[0] ==
'.') {
19 std::ranges::transform(ext, ext.begin(),
20 [](
unsigned char c) { return std::tolower(c); });
24 bool flush_to_disk(
const std::string& filepath,
const unsigned char* bytes,
size_t size)
26 std::ofstream out(filepath, std::ios::binary | std::ios::trunc);
30 out.write(
reinterpret_cast<const char*
>(bytes),
31 static_cast<std::streamsize
>(
size));
35 int resolve_compression(
int requested)
38 return TINYEXR_COMPRESSIONTYPE_ZIP;
41 case TINYEXR_COMPRESSIONTYPE_NONE:
42 case TINYEXR_COMPRESSIONTYPE_RLE:
43 case TINYEXR_COMPRESSIONTYPE_ZIPS:
44 case TINYEXR_COMPRESSIONTYPE_ZIP:
45 case TINYEXR_COMPRESSIONTYPE_PIZ:
48 return TINYEXR_COMPRESSIONTYPE_ZIP;
63 []() -> std::unique_ptr<ImageWriter> {
64 return std::make_unique<EXRWriter>();
68 "EXRWriter registered for: exr");
77 return extension_of(filepath) ==
"exr";
95 "EXRWriter: channel_names size ({}) does not match channels ({}); "
96 "falling back to defaults",
109 return {
"B",
"G",
"R" };
111 return {
"A",
"B",
"G",
"R" };
113 std::vector<std::string> names;
115 for (uint32_t i = 0; i < data.
channels; ++i) {
116 names.emplace_back(
"C" + std::to_string(i));
128 const std::vector<float>& src,
129 uint32_t
width, uint32_t height, uint32_t channels,
130 bool flip_vertically)
132 const size_t pixel_count =
static_cast<size_t>(
width) * height;
133 std::vector<std::vector<float>> planes(channels, std::vector<float>(pixel_count));
135 for (uint32_t y = 0; y < height; ++y) {
136 const uint32_t src_row = flip_vertically ? (height - 1 - y) : y;
137 const size_t src_row_offset =
static_cast<size_t>(src_row) *
width * channels;
138 const size_t dst_row_offset =
static_cast<size_t>(y) *
width;
140 for (uint32_t x = 0; x <
width; ++x) {
141 const size_t src_pixel = src_row_offset +
static_cast<size_t>(x) * channels;
142 const size_t dst_pixel = dst_row_offset + x;
144 for (uint32_t c = 0; c < channels; ++c) {
145 planes[c][dst_pixel] = src[src_pixel + c];
158 const std::string& filepath,
165 m_last_error =
"ImageData variant does not match declared ImageFormat";
178 m_last_error =
"EXR requires float ImageData (uint8 and uint16 not supported)";
183 const size_t expected_elements =
static_cast<size_t>(data.
width) * data.
height * data.
channels;
184 if (src->size() < expected_elements) {
186 + std::to_string(expected_elements)
187 +
" got " + std::to_string(src->size());
200 InitEXRImage(&
image);
206 std::vector<float*> plane_ptrs(data.
channels);
207 for (uint32_t c = 0; c < data.
channels; ++c) {
208 plane_ptrs[c] = planes[c].data();
210 image.images =
reinterpret_cast<unsigned char**
>(plane_ptrs.data());
216 InitEXRHeader(&header);
218 header.num_channels =
static_cast<int>(data.
channels);
219 header.compression_type = resolve_compression(options.
compression);
221 std::vector<EXRChannelInfo> channel_infos(data.
channels);
222 std::vector<int> pixel_types(data.
channels, TINYEXR_PIXELTYPE_FLOAT);
223 std::vector<int> requested_pixel_types(data.
channels, TINYEXR_PIXELTYPE_FLOAT);
225 for (uint32_t c = 0; c < data.
channels; ++c) {
226 std::memset(&channel_infos[c], 0,
sizeof(EXRChannelInfo));
227 const std::string& n = names[c];
228 const size_t copy_len = std::min<size_t>(n.size(), 255);
229 std::memcpy(channel_infos[c].name, n.data(), copy_len);
230 channel_infos[c].name[copy_len] =
'\0';
233 header.channels = channel_infos.data();
234 header.pixel_types = pixel_types.data();
235 header.requested_pixel_types = requested_pixel_types.data();
240 unsigned char* encoded =
nullptr;
241 const char* err =
nullptr;
243 const size_t encoded_size = SaveEXRImageToMemory(
244 &
image, &header, &encoded, &err);
246 if (encoded_size == 0 || encoded ==
nullptr) {
248 :
"SaveEXRImageToMemory returned 0 bytes";
250 FreeEXRErrorMessage(err);
256 const bool ok = flush_to_disk(filepath, encoded, encoded_size);
267 "Wrote EXR: {} ({}x{}, {} channels, compression {}, {} bytes)",
269 header.compression_type, encoded_size);
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
std::vector< std::string > resolve_channel_names(const ImageData &data, const ImageWriteOptions &options) const
Resolve channel name list based on options and channel count.
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.
bool write(const std::string &filepath, const ImageData &data, const ImageWriteOptions &options={}) override
Write image data to disk.
std::vector< std::string > get_supported_extensions() const override
File extensions handled by this writer (without dot).
bool can_write(const std::string &filepath) const override
Check whether this writer handles the given filepath.
static void register_with_registry()
Register this writer with the ImageWriterRegistry.
static ImageWriterRegistry & instance()
@ FileIO
Filesystem I/O operations.
@ Init
Engine/subsystem initialization.
@ IO
Networking, file handling, streaming.
bool is_consistent() const
Check that the active variant matches the declared format.
const std::vector< float > * as_float() const
Raw image data loaded from file.
std::vector< std::string > channel_names
Configuration for image writing.