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;
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)
@ IO
Networking, file handling, streaming.
ImageFormat
User-friendly image format enum.
@ RGB8
Three channel 8-bit.
@ RGBA8
Four channel 8-bit.
@ R8
Single channel 8-bit.