49 lines
1.3 KiB
C++
49 lines
1.3 KiB
C++
/**
|
|
* @file Image.cpp
|
|
* @author apio (cloudapio.eu)
|
|
* @brief TGA image loading and rendering.
|
|
*
|
|
* @copyright Copyright (c) 2023, the Luna authors.
|
|
*
|
|
*/
|
|
|
|
#include <os/File.h>
|
|
#include <ui/Alignment.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;
|
|
}
|
|
|
|
Result<OwnedPtr<ImageWidget>> ImageWidget::load(const os::Path& path)
|
|
{
|
|
auto widget = TRY(make_owned<ImageWidget>());
|
|
widget->m_image = TRY(Image::load(path));
|
|
widget->m_rect = { 0, 0, widget->m_image->width(), widget->m_image->height() };
|
|
return widget;
|
|
}
|
|
|
|
Result<void> ImageWidget::draw(Canvas& canvas)
|
|
{
|
|
canvas.subcanvas({ 0, 0, m_image->width(), m_image->height() }).fill(m_image->pixels(), m_image->width());
|
|
return {};
|
|
}
|
|
}
|