libui: Add support for TGA image loading
This commit is contained in:
parent
747c720159
commit
6138e8ef66
@ -7,6 +7,7 @@ set(SOURCES
|
||||
src/Canvas.cpp
|
||||
src/Rect.cpp
|
||||
src/Font.cpp
|
||||
src/Image.cpp
|
||||
)
|
||||
|
||||
add_library(ui ${SOURCES})
|
||||
|
71
libui/include/ui/Image.h
Normal file
71
libui/include/ui/Image.h
Normal file
@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
#include <luna/Buffer.h>
|
||||
#include <luna/SharedPtr.h>
|
||||
#include <os/Path.h>
|
||||
|
||||
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<SharedPtr<Image>> An error, or a new Image object.
|
||||
*/
|
||||
static Result<SharedPtr<Image>> 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;
|
||||
};
|
||||
}
|
24
libui/src/Image.cpp
Normal file
24
libui/src/Image.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include <os/File.h>
|
||||
#include <ui/Image.h>
|
||||
|
||||
namespace ui
|
||||
{
|
||||
Result<SharedPtr<Image>> Image::load(const os::Path& path)
|
||||
{
|
||||
auto image = TRY(make_shared<Image>());
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user