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

◆ load_from_memory()

std::optional< ImageData > MayaFlux::IO::ImageReader::load_from_memory ( const void *  data,
size_t  size 
)
static

Load image from memory (static utility)

Parameters
dataPointer to image data in memory
sizeSize of the image data in bytes
Returns
Image data or nullopt on failure

Definition at line 314 of file ImageReader.cpp.

315{
316
317 if (!data || size == 0) {
319 "Invalid memory buffer for image loading");
320 return std::nullopt;
321 }
322
323 int width {}, height {}, channels {};
324 unsigned char* pixels = stbi_load_from_memory(
325 static_cast<const unsigned char*>(data),
326 static_cast<int>(size),
327 &width, &height, &channels,
328 0);
329
330 if (!pixels) {
332 "Failed to decode image from memory: {}",
333 stbi_failure_reason());
334 return std::nullopt;
335 }
336
338 switch (channels) {
339 case 1:
341 break;
342 case 2:
344 break;
345 case 3:
347 break;
348 case 4:
350 break;
351 default:
353 "Unsupported channel count: {}", channels);
354 stbi_image_free(pixels);
355 return std::nullopt;
356 }
357
358 ImageData result;
359 size_t data_size = static_cast<long>(width * height) * channels;
360 result.pixels.resize(data_size);
361 std::memcpy(result.pixels.data(), pixels, data_size);
362
363 result.width = width;
364 result.height = height;
365 result.format = format;
366
367 stbi_image_free(pixels);
368
370 "Loaded image from memory ({}x{}, {} channels)",
371 width, height, channels);
372
373 return result;
374}
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
@ FileIO
Filesystem I/O operations.
std::string format(format_string< std::remove_cvref_t< Args >... > fmt_str, Args &&... args)
Definition Format.hpp:30
@ IO
Networking, file handling, streaming.
ImageFormat
User-friendly image format enum.

References MayaFlux::Journal::FileIO, MayaFlux::IO::ImageData::format, MayaFlux::IO::ImageData::height, MayaFlux::Journal::IO, MF_ERROR, MF_INFO, MayaFlux::IO::ImageData::pixels, MayaFlux::Portal::Graphics::R8, MayaFlux::Portal::Graphics::RG8, MayaFlux::Portal::Graphics::RGB8, MayaFlux::Portal::Graphics::RGBA8, and MayaFlux::IO::ImageData::width.