From dc91d047de40a36c72daa7cbe227ccc55fe7f525 Mon Sep 17 00:00:00 2001 From: apio Date: Fri, 4 Aug 2023 21:16:31 +0200 Subject: [PATCH] libui: Add support for TGA image loading --- libui/CMakeLists.txt | 1 + libui/include/ui/Image.h | 71 ++++++++++++++++++++++++++++++++++++++++ libui/src/Image.cpp | 24 ++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 libui/include/ui/Image.h create mode 100644 libui/src/Image.cpp diff --git a/libui/CMakeLists.txt b/libui/CMakeLists.txt index c28eb3e2..1a0fa74b 100644 --- a/libui/CMakeLists.txt +++ b/libui/CMakeLists.txt @@ -7,6 +7,7 @@ set(SOURCES src/Canvas.cpp src/Rect.cpp src/Font.cpp + src/Image.cpp ) add_library(ui ${SOURCES}) diff --git a/libui/include/ui/Image.h b/libui/include/ui/Image.h new file mode 100644 index 00000000..fa1cdb39 --- /dev/null +++ b/libui/include/ui/Image.h @@ -0,0 +1,71 @@ +#pragma once +#include +#include +#include + +namespace ui +{ + /** + * @brief An image in the TGA file format. + */ + class Image : public Shareable + { + public: + /** + * @brief Load a new TGA image from a file. + * + * @param path The path to open. + * @return Result> An error, or a new Image object. + */ + static Result> load(const os::Path& path); + + /** + * @brief Return the array of pixels contained in the image. + * + * @return u32* The array of pixels. + */ + u32* pixels() + { + return (u32*)m_image_data.data(); + } + + /** + * @brief Return the width of the image. + * + * @return u16 The width. + */ + u16 width() + { + return m_tga_header.w; + } + + /** + * @brief Return the height of the image. + * + * @return u16 The height. + */ + u16 height() + { + return m_tga_header.h; + } + + private: + struct [[gnu::packed]] TGAHeader + { + u8 idlen; + u8 colormap; + u8 encoding; + u16 cmaporig, cmaplen; + u8 cmapent; + u16 x; + u16 y; + u16 w; + u16 h; + u8 bpp; + u8 pixeltype; + }; + + TGAHeader m_tga_header; + Buffer m_image_data; + }; +} diff --git a/libui/src/Image.cpp b/libui/src/Image.cpp new file mode 100644 index 00000000..de6051ad --- /dev/null +++ b/libui/src/Image.cpp @@ -0,0 +1,24 @@ +#include +#include + +namespace ui +{ + Result> Image::load(const os::Path& path) + { + auto image = TRY(make_shared()); + auto file = TRY(os::File::open(path, os::File::ReadOnly)); + + TRY(file->read_typed(image->m_tga_header)); + + if (image->m_tga_header.encoding != 2) todo(); + if (image->m_tga_header.bpp != 32) todo(); + + Buffer image_id; + TRY(file->read(image_id, image->m_tga_header.idlen)); + + TRY(file->read(image->m_image_data, + image->m_tga_header.w * image->m_tga_header.h * (image->m_tga_header.bpp / 8))); + + return image; + } +}