104 lines
2.3 KiB
C++
104 lines
2.3 KiB
C++
/**
|
|
* @file Image.h
|
|
* @author apio (cloudapio.eu)
|
|
* @brief TGA image loading and rendering.
|
|
*
|
|
* @copyright Copyright (c) 2023, the Luna authors.
|
|
*
|
|
*/
|
|
|
|
#pragma once
|
|
#include <luna/Buffer.h>
|
|
#include <luna/SharedPtr.h>
|
|
#include <os/File.h>
|
|
#include <os/Path.h>
|
|
#include <ui/Widget.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;
|
|
};
|
|
|
|
class ImageWidget final : public Widget
|
|
{
|
|
public:
|
|
ImageWidget(ui::Window* window, ui::Widget* parent);
|
|
|
|
Result<void> load(const os::Path& path);
|
|
|
|
Result<void> draw(Canvas& canvas) override;
|
|
|
|
void recalculate_pseudo_rects() override;
|
|
|
|
void show_tree(int indent) override
|
|
{
|
|
os::println("%*s- ui::Image %dx%d (%d,%d,%d,%d)", indent, "", m_image->width(), m_image->height(),
|
|
m_rect.pos.x, m_rect.pos.y, m_rect.width, m_rect.height);
|
|
}
|
|
|
|
private:
|
|
SharedPtr<Image> m_image;
|
|
};
|
|
}
|