10#ifdef MAYAFLUX_ARCH_X64
12#ifdef MAYAFLUX_COMPILER_MSVC
17#ifdef MAYAFLUX_ARCH_ARM64
21#define STBI_NO_FAILURE_STRINGS
22#define STB_IMAGE_IMPLEMENTATION
25#if __has_include("stb/stb_image.h")
26#include "stb/stb_image.h"
27#elif __has_include("stb_image.h")
30#error "stb_image.h not found"
42 std::string extension_of(
const std::filesystem::path& path)
44 auto ext = path.extension().string();
45 if (!ext.empty() && ext[0] ==
'.') {
48 std::ranges::transform(ext, ext.begin(),
49 [](
unsigned char c) { return std::tolower(c); });
67 std::optional<ImageData> load_exr_from_memory(
68 const unsigned char* bytes,
size_t size)
73 if (ParseEXRVersionFromMemory(&
version, bytes,
size) != TINYEXR_SUCCESS) {
75 "EXR: failed to parse version");
80 "EXR: multipart and deep images are not supported");
85 InitEXRHeader(&header);
87 const char* err =
nullptr;
88 if (ParseEXRHeaderFromMemory(&header, &
version, bytes,
size, &err)
91 "EXR: header parse failed: {}", err ? err :
"unknown");
93 FreeEXRErrorMessage(err);
97 for (
int i = 0; i < header.num_channels; ++i) {
98 if (header.pixel_types[i] == TINYEXR_PIXELTYPE_UINT) {
100 "EXR: uint pixel type channel '{}' not supported",
101 header.channels[i].name);
102 FreeEXRHeader(&header);
105 header.requested_pixel_types[i] = TINYEXR_PIXELTYPE_FLOAT;
109 InitEXRImage(&
image);
111 if (LoadEXRImageFromMemory(&
image, &header, bytes,
size, &err)
112 != TINYEXR_SUCCESS) {
114 "EXR: image load failed: {}", err ? err :
"unknown");
116 FreeEXRErrorMessage(err);
117 FreeEXRHeader(&header);
121 const auto width =
static_cast<uint32_t
>(
image.width);
122 const auto height =
static_cast<uint32_t
>(
image.height);
123 const auto src_channels =
static_cast<uint32_t
>(
image.num_channels);
125 if (
width == 0 || height == 0 || src_channels == 0) {
127 "EXR: zero dimensions or channels");
128 FreeEXRImage(&
image);
129 FreeEXRHeader(&header);
133 uint32_t out_channels = src_channels;
135 switch (src_channels) {
151 "EXR: {} channels not supported (1/2/3/4 only)", src_channels);
152 FreeEXRImage(&
image);
153 FreeEXRHeader(&header);
157 const size_t pixel_count =
static_cast<size_t>(
width) * height;
158 const size_t element_count = pixel_count * out_channels;
161 auto& dst = result.pixels.emplace<std::vector<float>>(element_count, 0.0F);
163 auto** planes =
reinterpret_cast<float**
>(
image.images);
165 for (
size_t p = 0; p < pixel_count; ++p) {
166 for (uint32_t c = 0; c < src_channels; ++c) {
167 dst[p * out_channels + c] = planes[c][p];
169 if (out_channels > src_channels) {
170 dst[p * out_channels + (out_channels - 1)] = 1.0F;
174 result.width =
width;
175 result.height = height;
176 result.channels = out_channels;
177 result.format = format;
180 "Loaded EXR: {}x{}, {} channels{}",
181 width, height, out_channels,
182 (src_channels == 3) ?
" [BGR→ABGR, A=1.0]" :
"");
184 FreeEXRImage(&
image);
185 FreeEXRHeader(&header);
195 "STB Image: SSE2 SIMD optimizations enabled (x64)");
196#elif defined(STBI_NEON)
198 "STB Image: NEON SIMD optimizations enabled (ARM64)");
201 "STB Image: No SIMD optimizations (scalar fallback)");
210 const bool has_u8 = std::holds_alternative<std::vector<uint8_t>>(
pixels);
211 const bool has_u16 = std::holds_alternative<std::vector<uint16_t>>(
pixels);
212 const bool has_f32 = std::holds_alternative<std::vector<float>>(
pixels);
253 auto ext = std::filesystem::path(filepath).extension().string();
254 if (!ext.empty() && ext[0] ==
'.') {
258 static const std::vector<std::string> supported = {
259 "png",
"jpg",
"jpeg",
"bmp",
"tga",
"psd",
"gif",
"hdr",
"pic",
"pnm",
263 return std::ranges::find(supported, ext) != supported.end();
291 "Opened image: {} ({}x{}, {} channels)",
341 [](
const auto& vec) -> std::vector<Kakshya::DataVariant> {
369 uint32_t region_width = x_end - x_start;
370 uint32_t region_height = y_end - y_start;
374 const size_t pixel_stride_bytes = bytes_per_elem *
m_image_data->channels;
376 std::vector<uint8_t> region_data(
377 static_cast<size_t>(region_width) * region_height * pixel_stride_bytes);
379 const auto* src =
static_cast<const uint8_t*
>(
m_image_data->data());
381 for (uint32_t y = 0; y < region_height; ++y) {
382 size_t src_offset = (
static_cast<size_t>((y_start + y) *
m_image_data->width + x_start)) * pixel_stride_bytes;
383 size_t dst_offset =
static_cast<size_t>(y * region_width) * pixel_stride_bytes;
384 size_t row_size =
static_cast<size_t>(region_width) * pixel_stride_bytes;
386 region_data.data() + dst_offset,
391 return { region_data };
396 m_last_error =
"Images use direct GPU texture creation, not containers";
402 m_last_error =
"Images cannot be loaded into SignalSourceContainer";
418 return {
"png",
"jpg",
"jpeg",
"bmp",
"tga",
"psd",
"gif",
"hdr",
"pic",
"pnm",
424 return typeid(std::vector<uint8_t>);
464std::optional<ImageData>
ImageReader::load(
const std::filesystem::path& path,
int desired_channels)
466 if (!std::filesystem::exists(path)) {
468 "Image file not found: {}", path.string());
472 std::ifstream file(path, std::ios::binary | std::ios::ate);
473 if (!file.is_open()) {
475 "Failed to open image file: {}", path.string());
479 std::streamsize file_size = file.tellg();
480 file.seekg(0, std::ios::beg);
482 std::vector<unsigned char> file_buffer(file_size);
483 if (!file.read(
reinterpret_cast<char*
>(file_buffer.data()), file_size)) {
485 "Failed to read image file: {}", path.string());
490 if (extension_of(path) ==
"exr") {
491 (void)desired_channels;
492 return load_exr_from_memory(file_buffer.data(),
493 static_cast<size_t>(file_size));
496 int width {}, height {}, channels {};
498 if (desired_channels == 0) {
499 stbi_info_from_memory(file_buffer.data(),
static_cast<int>(file_buffer.size()),
500 &
width, &height, &channels);
502 desired_channels = 4;
506 unsigned char*
pixels = stbi_load_from_memory(
508 static_cast<int>(file_buffer.size()),
509 &
width, &height, &channels,
514 "Failed to decode image: {} - {}",
515 path.string(), stbi_failure_reason());
519 int result_channels = (desired_channels != 0) ? desired_channels : channels;
522 "Loaded image: {} ({}x{}, {} channels{})",
523 path.filename().string(),
width, height, result_channels,
524 (channels == 3 && result_channels == 4) ?
" [RGB→RGBA]" :
"");
527 auto& buf = result.
pixels.emplace<std::vector<uint8_t>>();
528 size_t data_size =
static_cast<size_t>(
width) * height * result_channels;
529 buf.resize(data_size);
530 std::memcpy(buf.data(),
pixels, data_size);
532 result.width =
width;
533 result.height = height;
534 result.channels = result_channels;
536 switch (result_channels) {
548 "Unsupported channel count: {}", result_channels);
559 return load(std::filesystem::path(path), desired_channels);
564 if (!data ||
size == 0) {
566 "Invalid memory buffer for image loading");
570 const auto* bytes =
static_cast<const unsigned char*
>(data);
573 && bytes[0] == 0x76 && bytes[1] == 0x2F
574 && bytes[2] == 0x31 && bytes[3] == 0x01) {
575 return load_exr_from_memory(bytes,
size);
578 int width {}, height {}, channels {};
580 stbi_info_from_memory(bytes,
static_cast<int>(
size),
581 &
width, &height, &channels);
583 int load_as = (channels == 3) ? 4 : 0;
585 unsigned char*
pixels = stbi_load_from_memory(
587 static_cast<int>(
size),
588 &
width, &height, &channels,
593 "Failed to decode image from memory: {}",
594 stbi_failure_reason());
598 int result_channels = (load_as != 0) ? load_as : channels;
601 "Loaded image from memory ({}x{}, {} channels{})",
602 width, height, result_channels,
603 (channels == 3 && result_channels == 4) ?
" [RGB→RGBA]" :
"");
606 auto& buf = result.
pixels.emplace<std::vector<uint8_t>>();
607 size_t data_size =
static_cast<size_t>(
width) * height * result_channels;
608 buf.resize(data_size);
609 std::memcpy(buf.data(),
pixels, data_size);
611 result.width =
width;
612 result.height = height;
613 result.channels = result_channels;
615 switch (result_channels) {
627 "Unsupported channel count: {}", result_channels);
638 auto image_data =
load(path, 4);
644 auto texture = mgr.create_2d(
652 "Created GPU texture from image: {}", path);
670 auto tex_buffer = std::make_shared<Buffers::TextureBuffer>(
677 "Created TextureBuffer from image: {}x{} ({} bytes)",
690 if (!buffer || !buffer->is_initialized()) {
696 if (buffer->get_size_bytes() < required_size) {
707 "Loaded image into VKBuffer: {}x{} ({} bytes)",
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
const std::vector< float > * pixels
static std::string resolve_path(const std::string &filepath)
Resolve a filepath against the project source root if not found as-is.
static std::optional< ImageData > load_from_memory(const void *data, size_t size)
Load image from memory (static utility)
std::type_index get_container_type() const override
Get the container type this reader creates.
std::shared_ptr< Kakshya::SignalSourceContainer > create_container() override
Create and initialize a container from the file.
std::vector< std::string > get_supported_extensions() const override
Get supported file extensions for this reader.
std::type_index get_data_type() const override
Get the data type this reader produces.
static std::optional< ImageData > load(const std::string &path, int desired_channels=4)
Load image from file (static utility)
std::optional< ImageData > m_image_data
bool is_open() const override
Check if a file is currently open.
bool supports_streaming() const override
Check if streaming is supported for the current file.
std::shared_ptr< Buffers::TextureBuffer > create_texture_buffer()
Create a VKBuffer containing the loaded image pixel data.
bool open(const std::string &filepath, FileReadOptions options=FileReadOptions::ALL) override
Open a file for reading.
size_t get_num_dimensions() const override
Get the dimensionality of the file data.
bool load_into_container(std::shared_ptr< Kakshya::SignalSourceContainer > container) override
Load file data into an existing container.
std::vector< Kakshya::DataVariant > read_region(const FileRegion ®ion) override
Read a specific region of data.
std::vector< Kakshya::DataVariant > read_all() override
Read all data from the file into memory.
std::vector< uint64_t > get_dimension_sizes() const override
Get size of each dimension in the file data.
std::optional< FileMetadata > get_metadata() const override
Get metadata from the open file.
std::vector< FileRegion > get_regions() const override
Get semantic regions from the file.
bool load_into_buffer(const std::shared_ptr< Buffers::VKBuffer > &buffer)
Load image directly into an existing VKBuffer.
bool seek(const std::vector< uint64_t > &position) override
Seek to a specific position in the file.
std::vector< uint64_t > get_read_position() const override
Get current read position in primary dimension.
void close() override
Close the currently open file.
bool can_read(const std::string &filepath) const override
Check if a file can be read by this reader.
std::optional< ImageData > get_image_data() const
Get the loaded image data.
static std::shared_ptr< Core::VKImage > load_texture(const std::string &path)
Load image directly into GPU texture (static utility)
std::string get_last_error() const override
Get the last error message.
uint64_t get_preferred_chunk_size() const override
Get the preferred chunk size for streaming.
void upload_to_gpu(const void *data, size_t size, const std::shared_ptr< VKBuffer > &target, const std::shared_ptr< VKBuffer > &staging)
Upload raw data to GPU buffer (auto-detects host-visible vs device-local)
FileReadOptions
Generic options for file reading behavior.
static bool g_stb_simd_logged
@ FileIO
Filesystem I/O operations.
@ Init
Engine/subsystem initialization.
@ IO
Networking, file handling, streaming.
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.
@ IMAGE_COLOR
2D RGB/RGBA image
MAYAFLUX_API TextureLoom & get_texture_manager()
Get the global texture manager instance.
ImageFormat
User-friendly image format enum.
@ RGBA8
Four channel 8-bit.
@ R8
Single channel 8-bit.
std::vector< uint64_t > start_coordinates
N-dimensional start position (e.g., frame, x, y)
std::vector< uint64_t > end_coordinates
N-dimensional end position (inclusive)
Generic region descriptor for any file type.
bool is_consistent() const
Check that the active variant matches the declared format.
Portal::Graphics::ImageFormat format
Raw image data loaded from file.