#pragma once
#include <cstdint>
#include <span>
#include <filesystem>
#include <Eigen/Eigen>

#include "sokol_gfx.h"
#include "misc/mapped_file/mapped_file.h"

struct image_heap_t {
	uint32_t         width;        // 图像宽度
	uint32_t         height;       // 图像高度
	sg_pixel_format pixel_format; // 像素格式
	void*           data;         // 图像数据指针

	[[nodiscard]] Eigen::Vector2i get_size() const {
		return { static_cast<int32_t>(width), static_cast<int32_t>(height) };
	}
	[[nodiscard]] size_t get_data_size() const {
		const auto& pixel_info = sg_query_pixelformat(pixel_format);
		return width * height * pixel_info.bytes_per_pixel;
	}
};

std::shared_ptr<image_heap_t> load_image(const std::span<std::byte>& in_raw_data);
inline std::shared_ptr<image_heap_t> load_image(const std::filesystem::path& in_file) {
	auto mapped = mapped_file::create();
	if (!mapped->map_file(in_file))
		return nullptr;
	return load_image(mapped->get_span());
}
