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"
41 "STB Image: SSE2 SIMD optimizations enabled (x64)");
42#elif defined(STBI_NEON)
44 "STB Image: NEON SIMD optimizations enabled (ARM64)");
47 "STB Image: No SIMD optimizations (scalar fallback)");
64 auto ext = std::filesystem::path(filepath).extension().string();
65 if (!ext.empty() && ext[0] ==
'.') {
69 static const std::vector<std::string> supported = {
70 "png",
"jpg",
"jpeg",
"bmp",
"tga",
"psd",
"gif",
"hdr",
"pic",
"pnm"
73 return std::ranges::find(supported, ext) != supported.end();
99 "Opened image: {} ({}x{}, {} channels)",
173 uint32_t region_width = x_end - x_start;
174 uint32_t region_height = y_end - y_start;
175 size_t region_size =
static_cast<size_t>(region_width * region_height) *
m_image_data->channels;
176 std::vector<uint8_t> region_data(region_size);
178 for (uint32_t y = 0; y < region_height; ++y) {
180 size_t dst_offset =
static_cast<size_t>(y * region_width) *
m_image_data->channels;
181 size_t row_size =
static_cast<size_t>(region_width) *
m_image_data->channels;
183 region_data.data() + dst_offset,
188 return { region_data };
194 m_last_error =
"Images use direct GPU texture creation, not containers";
201 m_last_error =
"Images cannot be loaded into SignalSourceContainer";
217 return {
"png",
"jpg",
"jpeg",
"bmp",
"tga",
"psd",
"gif",
"hdr",
"pic",
"pnm" };
222 return typeid(std::vector<uint8_t>);
262std::optional<ImageData>
ImageReader::load(
const std::filesystem::path& path,
int desired_channels)
264 if (!std::filesystem::exists(path)) {
266 "Image file not found: {}", path.string());
270 std::ifstream file(path, std::ios::binary | std::ios::ate);
271 if (!file.is_open()) {
273 "Failed to open image file: {}", path.string());
277 std::streamsize file_size = file.tellg();
278 file.seekg(0, std::ios::beg);
280 std::vector<unsigned char> file_buffer(file_size);
281 if (!file.read(
reinterpret_cast<char*
>(file_buffer.data()), file_size)) {
283 "Failed to read image file: {}", path.string());
288 int width {}, height {}, channels {};
290 if (desired_channels == 0) {
291 stbi_info_from_memory(file_buffer.data(),
static_cast<int>(file_buffer.size()),
292 &width, &height, &channels);
294 desired_channels = 4;
298 unsigned char* pixels = stbi_load_from_memory(
300 static_cast<int>(file_buffer.size()),
301 &width, &height, &channels,
306 "Failed to decode image: {} - {}",
307 path.string(), stbi_failure_reason());
311 int result_channels = (desired_channels != 0) ? desired_channels : channels;
314 "Loaded image: {} ({}x{}, {} channels{})",
315 path.filename().string(), width, height, result_channels,
316 (channels == 3 && result_channels == 4) ?
" [RGB→RGBA]" :
"");
319 size_t data_size =
static_cast<size_t>(width) * height * result_channels;
320 result.
pixels.resize(data_size);
321 std::memcpy(result.pixels.data(), pixels, data_size);
323 result.width = width;
324 result.height = height;
325 result.channels = result_channels;
327 switch (result_channels) {
339 "Unsupported channel count: {}", result_channels);
340 stbi_image_free(pixels);
344 stbi_image_free(pixels);
350 return load(std::filesystem::path(path), desired_channels);
355 if (!data || size == 0) {
357 "Invalid memory buffer for image loading");
361 int width {}, height {}, channels {};
363 stbi_info_from_memory(
364 static_cast<const unsigned char*
>(data),
365 static_cast<int>(size),
366 &width, &height, &channels);
368 int load_as = (channels == 3) ? 4 : 0;
370 unsigned char* pixels = stbi_load_from_memory(
371 static_cast<const unsigned char*
>(data),
372 static_cast<int>(size),
373 &width, &height, &channels,
378 "Failed to decode image from memory: {}",
379 stbi_failure_reason());
383 int result_channels = (load_as != 0) ? load_as : channels;
386 "Loaded image from memory ({}x{}, {} channels{})",
387 width, height, result_channels,
388 (channels == 3 && result_channels == 4) ?
" [RGB→RGBA]" :
"");
391 size_t data_size =
static_cast<size_t>(width) * height * result_channels;
392 result.
pixels.resize(data_size);
393 std::memcpy(result.pixels.data(), pixels, data_size);
395 result.width = width;
396 result.height = height;
397 result.channels = result_channels;
399 switch (result_channels) {
411 "Unsupported channel count: {}", result_channels);
412 stbi_image_free(pixels);
416 stbi_image_free(pixels);
422 auto image_data =
load(path, 4);
428 auto texture = mgr.create_2d(
432 image_data->pixels.data());
436 "Created GPU texture from image: {}", path);
454 auto tex_buffer = std::make_shared<Buffers::TextureBuffer>(
461 "Created TextureBuffer from image: {}x{} ({} bytes)",
474 if (!buffer || !buffer->is_initialized()) {
480 if (buffer->get_size_bytes() < required_size) {
491 "Loaded image into VKBuffer: {}x{} ({} bytes)",
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
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.
@ IMAGE_COLOR
2D RGB/RGBA image
MAYAFLUX_API TextureLoom & get_texture_manager()
Get the global texture manager instance.
@ 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.
std::vector< uint8_t > pixels
Raw image data loaded from file.