15 using Portal::Graphics::TextureLoom;
23 bool emplace_variant_for_format(ImageData& data, ImageFormat format,
24 size_t pixel_count, uint32_t channels)
26 const size_t element_count = pixel_count * channels;
27 const size_t bpp = TextureLoom::get_bytes_per_pixel(format);
28 const size_t bpe = channels > 0 ? (bpp / channels) : 0;
32 data.pixels.emplace<std::vector<uint8_t>>(element_count);
35 data.pixels.emplace<std::vector<uint16_t>>(element_count);
38 data.pixels.emplace<std::vector<float>>(element_count);
52 const std::shared_ptr<Core::VKImage>&
image)
56 "download_image: null image");
60 const vk::Format vk_format =
image->get_format();
64 "download_image: vk::Format {} has no ImageFormat mapping",
65 vk::to_string(vk_format));
69 const ImageFormat format = *format_opt;
73 "download_image: zero channels for format {}",
74 static_cast<int>(format));
79 const uint32_t height =
image->get_height();
80 if (
width == 0 || height == 0) {
82 "download_image: zero dimensions {}x{}",
width, height);
86 const size_t pixel_count =
static_cast<size_t>(
width) * height;
87 const size_t mip0_bytes = pixel_count * TextureLoom::get_bytes_per_pixel(format);
95 if (!emplace_variant_for_format(result, format, pixel_count, channels)) {
97 "download_image: unsupported bytes-per-element for format {}",
98 static_cast<int>(format));
102 TextureLoom::instance().download_data_async(
104 const_cast<void*
>(result.
data()),
109 "download_image: resulting ImageData failed is_consistent()");
114 "download_image: {}x{}, {} channels, {} bytes",
115 width, height, channels, mip0_bytes);
125 const std::shared_ptr<Buffers::TextureBuffer>& buffer)
129 "download_texture_buffer: null buffer");
133 auto image = buffer->get_gpu_texture();
136 "download_texture_buffer: buffer has no GPU texture yet "
137 "(has the buffer been processed at least once?)");
149 const std::shared_ptr<Core::VKImage>&
image,
150 const std::string& filepath,
161 "save_image: no writer registered for extension of '{}'", filepath);
165 const bool ok = writer->write(filepath, *data, options);
168 "save_image: writer failed: {}", writer->get_last_error());
174 const std::shared_ptr<Buffers::TextureBuffer>& buffer,
175 const std::string& filepath,
180 "save_texture_buffer: null buffer");
183 auto image = buffer->get_gpu_texture();
186 "save_texture_buffer: buffer has no GPU texture");
193 const std::shared_ptr<Buffers::TextBuffer>& buffer,
194 const std::string& filepath,
#define MF_ERROR(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
std::unique_ptr< ImageWriter > create_writer(const std::string &filepath) const
static ImageWriterRegistry & instance()
static uint32_t get_channel_count(ImageFormat format)
Get the number of color channels for a given format.
static std::optional< ImageFormat > from_vulkan_format(vk::Format vk_format)
Convert Vulkan format to Portal ImageFormat.
bool save_texture_buffer(const std::shared_ptr< Buffers::TextureBuffer > &buffer, const std::string &filepath, const ImageWriteOptions &options)
Save a TextureBuffer's current GPU state to disk.
bool save_image(const std::shared_ptr< Core::VKImage > &image, const std::string &filepath, const ImageWriteOptions &options)
Save a VKImage directly to disk via the ImageWriter registry.
bool save_text_buffer(const std::shared_ptr< Buffers::TextBuffer > &buffer, const std::string &filepath, const ImageWriteOptions &options)
Save a TextBuffer's rendered glyph texture to disk.
std::optional< ImageData > download_texture_buffer(const std::shared_ptr< Buffers::TextureBuffer > &buffer)
Download a TextureBuffer's GPU texture into host ImageData.
std::optional< ImageData > download_image(const std::shared_ptr< Core::VKImage > &image)
Download pixel data from a GPU-resident VKImage into host ImageData.
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.
ImageFormat
User-friendly image format enum.
bool is_consistent() const
Check that the active variant matches the declared format.
Portal::Graphics::ImageFormat format
const void * data() const
Raw data pointer, dispatched on variant.
Raw image data loaded from file.
Configuration for image writing.