Compare commits

...

4 Commits

Author SHA1 Message Date
Bananymous 157e05f57c image: Implement --scale argument to scale image to framebuffer
Also fix bug where red and blue channels were flipped
2024-06-15 17:24:01 +03:00
Bananymous 96efd1e8b9 LibImage: Implement image resize using nearest or bilinear filters 2024-06-15 17:23:24 +03:00
Bananymous 672ce40618 LibImage: Move userspace image parsing to its own library
The image utility now uses this tool instead of parsing images on its
own.
2024-06-14 11:05:54 +03:00
Bananymous 05e9d76c77 BAN: Implement will_{addition,multiplication}_overflow 2024-06-14 11:04:29 +03:00
14 changed files with 485 additions and 330 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <BAN/Limits.h>
#include <BAN/Traits.h>
#include <stddef.h>
@ -65,6 +66,27 @@ namespace BAN::Math
return (value & (value - 1)) == 0;
}
template<BAN::integral T>
static constexpr bool will_multiplication_overflow(T a, T b)
{
if (a == 0 || b == 0)
return false;
if ((a > 0) == (b > 0))
return a > BAN::numeric_limits<T>::max() / b;
else
return a < BAN::numeric_limits<T>::min() / b;
}
template<BAN::integral T>
static constexpr bool will_addition_overflow(T a, T b)
{
if (a > 0 && b > 0)
return a > BAN::numeric_limits<T>::max() - b;
if (a < 0 && b < 0)
return a < BAN::numeric_limits<T>::min() - b;
return false;
}
template<typename T>
requires is_same_v<T, unsigned int> || is_same_v<T, unsigned long> || is_same_v<T, unsigned long long>
inline constexpr T ilog2(T value)

View File

@ -23,6 +23,7 @@ add_subdirectory(libc)
add_subdirectory(LibELF)
add_subdirectory(LibFont)
add_subdirectory(LibGUI)
add_subdirectory(LibImage)
add_subdirectory(LibInput)
add_subdirectory(userspace)
@ -38,6 +39,7 @@ add_custom_target(headers
DEPENDS libelf-headers
DEPENDS libfont-headers
DEPENDS libgui-headers
DEPENDS libimage-headers
DEPENDS libinput-headers
)
@ -49,6 +51,7 @@ add_custom_target(install-sysroot
DEPENDS libelf-install
DEPENDS libfont-install
DEPENDS libgui-install
DEPENDS libimage-install
DEPENDS libinput-install
)

25
LibImage/CMakeLists.txt Normal file
View File

@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.26)
project(libimage CXX)
set(LIBIMAGE_SOURCES
Image.cpp
Netbpm.cpp
)
add_custom_target(libimage-headers
COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different ${CMAKE_CURRENT_SOURCE_DIR}/include/ ${BANAN_INCLUDE}/
DEPENDS sysroot
)
add_library(libimage ${LIBIMAGE_SOURCES})
add_dependencies(libimage headers libc-install)
target_link_libraries(libimage PUBLIC libc)
add_custom_target(libimage-install
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/libimage.a ${BANAN_LIB}/
DEPENDS libimage
BYPRODUCTS ${BANAN_LIB}/libimage.a
)
set(CMAKE_STATIC_LIBRARY_PREFIX "")

139
LibImage/Image.cpp Normal file
View File

@ -0,0 +1,139 @@
#include <BAN/ScopeGuard.h>
#include <BAN/String.h>
#include <LibImage/Image.h>
#include <LibImage/Netbpm.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
namespace LibImage
{
BAN::ErrorOr<BAN::UniqPtr<Image>> Image::load_from_file(BAN::StringView path)
{
int fd = -1;
if (path.data()[path.size()] == '\0')
{
fd = open(path.data(), O_RDONLY);
}
else
{
BAN::String path_str;
TRY(path_str.append(path));
fd = open(path_str.data(), O_RDONLY);
}
if (fd == -1)
{
fprintf(stddbg, "open: %s\n", strerror(errno));
return BAN::Error::from_errno(errno);
}
BAN::ScopeGuard guard_file_close([fd] { close(fd); });
struct stat st;
if (fstat(fd, &st) == -1)
{
fprintf(stddbg, "fstat: %s\n", strerror(errno));
return BAN::Error::from_errno(errno);
}
if (st.st_size < 2)
{
fprintf(stddbg, "invalid image (too small)\n");
return BAN::Error::from_errno(EINVAL);
}
void* addr = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED)
{
fprintf(stddbg, "mmap: %s\n", strerror(errno));
return BAN::Error::from_errno(errno);
}
BAN::ScopeGuard guard_munmap([&] { munmap(addr, st.st_size); });
auto image_data_span = BAN::ConstByteSpan(reinterpret_cast<uint8_t*>(addr), st.st_size);
uint16_t u16_signature = image_data_span.as<const uint16_t>();
switch (u16_signature)
{
case 0x3650:
case 0x3550:
case 0x3450:
case 0x3350:
case 0x3250:
case 0x3150:
return TRY(load_netbpm(image_data_span));
default:
fprintf(stderr, "unrecognized image format\n");
break;
}
return BAN::Error::from_errno(ENOTSUP);
}
BAN::ErrorOr<BAN::UniqPtr<Image>> Image::resize(uint64_t new_width, uint64_t new_height, ResizeAlgorithm algorithm)
{
if (!validate_size(new_width, new_height))
return BAN::Error::from_errno(EOVERFLOW);
const double ratio_x = (double)width() / new_width;
const double ratio_y = (double)height() / new_height;
switch (algorithm)
{
case ResizeAlgorithm::Nearest:
{
BAN::Vector<Color> nearest_bitmap;
TRY(nearest_bitmap.resize(new_width * new_height));
for (uint64_t y = 0; y < new_height; y++)
{
for (uint64_t x = 0; x < new_width; x++)
{
const uint64_t nearest_x = BAN::Math::clamp<uint64_t>(x * ratio_x, 0, width() - 1);
const uint64_t nearest_y = BAN::Math::clamp<uint64_t>(y * ratio_y, 0, height() - 1);
nearest_bitmap[y * new_width + x] = get_color(nearest_x, nearest_y);
}
}
return TRY(BAN::UniqPtr<Image>::create(new_width, new_height, BAN::move(nearest_bitmap)));
}
case ResizeAlgorithm::Bilinear:
{
BAN::Vector<Color> bilinear_bitmap;
TRY(bilinear_bitmap.resize(new_width * new_height));
for (uint64_t y = 0; y < new_height; y++)
{
for (uint64_t x = 0; x < new_width; x++)
{
const double src_x_float = x * ratio_x;
const double src_y_float = y * ratio_y;
const double weight_x = src_x_float - floor(src_x_float);
const double weight_y = src_y_float - floor(src_y_float);
const uint64_t src_l = BAN::Math::clamp<uint64_t>(src_x_float, 0, width() - 1);
const uint64_t src_t = BAN::Math::clamp<uint64_t>(src_y_float, 0, height() - 1);
const uint64_t src_r = BAN::Math::clamp<uint64_t>(src_l + 1, 0, width() - 1);
const uint64_t src_b = BAN::Math::clamp<uint64_t>(src_t + 1, 0, height() - 1);
const Color avg_t = Color::average(get_color(src_l, src_t), get_color(src_r, src_t), weight_x);
const Color avg_b = Color::average(get_color(src_l, src_b), get_color(src_r, src_b), weight_x);
bilinear_bitmap[y * new_width + x] = Color::average(avg_t, avg_b, weight_y);
}
}
return TRY(BAN::UniqPtr<Image>::create(new_width, new_height, BAN::move(bilinear_bitmap)));
}
}
return BAN::Error::from_errno(EINVAL);
}
}

120
LibImage/Netbpm.cpp Normal file
View File

@ -0,0 +1,120 @@
#include <BAN/Optional.h>
#include <LibImage/Netbpm.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
namespace LibImage
{
static BAN::Optional<uint64_t> parse_u64(BAN::ConstByteSpan& data)
{
size_t digit_count = 0;
while (digit_count < data.size() && isdigit(data[digit_count]))
digit_count++;
if (digit_count == 0)
return {};
uint64_t result = 0;
for (size_t i = 0; i < digit_count; i++)
{
if (BAN::Math::will_multiplication_overflow<uint64_t>(result, 10))
return {};
result *= 10;
if (BAN::Math::will_addition_overflow<uint64_t>(result, data[i] - '0'))
return {};
result += data[i] - '0';
}
data = data.slice(digit_count);
return result;
}
BAN::ErrorOr<BAN::UniqPtr<Image>> load_netbpm(BAN::ConstByteSpan image_data)
{
if (image_data.size() < 11)
{
fprintf(stddbg, "invalid Netbpm image (too small)\n");
return BAN::Error::from_errno(EINVAL);
}
if (image_data[0] != 'P')
{
fprintf(stddbg, "not Netbpm image\n");
return BAN::Error::from_errno(EINVAL);
}
if (image_data[1] != '6')
{
fprintf(stddbg, "unsupported Netbpm image\n");
return BAN::Error::from_errno(EINVAL);
}
if (image_data[2] != '\n')
{
fprintf(stddbg, "invalid Netbpm image (invalid header)\n");
return BAN::Error::from_errno(EINVAL);
}
image_data = image_data.slice(3);
auto opt_width = parse_u64(image_data);
if (!opt_width.has_value() || image_data[0] != ' ')
{
fprintf(stddbg, "invalid Netbpm image (invalid width)\n");
return BAN::Error::from_errno(EINVAL);
}
image_data = image_data.slice(1);
auto width = opt_width.value();
auto opt_height = parse_u64(image_data);
if (!opt_height.has_value() || image_data[0] != '\n')
{
fprintf(stddbg, "invalid Netbpm image (invalid height)\n");
return BAN::Error::from_errno(EINVAL);
}
image_data = image_data.slice(1);
auto height = opt_height.value();
if (!Image::validate_size(width, height))
{
fprintf(stddbg, "invalid Netbpm image size\n");
return BAN::Error::from_errno(EINVAL);
}
auto header_end = parse_u64(image_data);
if (!header_end.has_value() || *header_end != 255 || image_data[0] != '\n')
{
fprintf(stddbg, "invalid Netbpm image (invalid header end)\n");
return BAN::Error::from_errno(EINVAL);
}
image_data = image_data.slice(1);
if (image_data.size() < width * height * 3)
{
fprintf(stddbg, "invalid Netbpm image (too small file size)\n");
return BAN::Error::from_errno(EINVAL);
}
BAN::Vector<Image::Color> bitmap;
TRY(bitmap.resize(width * height));
// Fill bitmap
for (uint64_t y = 0; y < height; y++)
{
for (uint64_t x = 0; x < width; x++)
{
const uint64_t index = y * width + x;
auto& pixel = bitmap[index];
pixel.r = image_data[index * 3 + 0];
pixel.g = image_data[index * 3 + 1];
pixel.b = image_data[index * 3 + 2];
pixel.a = 0xFF;
}
}
return TRY(BAN::UniqPtr<Image>::create(width, height, BAN::move(bitmap)));
}
}

View File

@ -0,0 +1,85 @@
#pragma once
#include <BAN/Limits.h>
#include <BAN/StringView.h>
#include <BAN/UniqPtr.h>
#include <BAN/Vector.h>
namespace LibImage
{
class Image
{
public:
struct Color
{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
// Calculate weighted average of colors
// weight of 0.0 returns a and weight of 1.0 returns b
static Color average(Color a, Color b, double weight)
{
const double b_mult = BAN::Math::clamp(weight, 0.0, 1.0);
const double a_mult = 1.0 - b_mult;
return Color {
.r = static_cast<uint8_t>(a.r * a_mult + b.r * b_mult),
.g = static_cast<uint8_t>(a.g * a_mult + b.g * b_mult),
.b = static_cast<uint8_t>(a.b * a_mult + b.b * b_mult),
.a = static_cast<uint8_t>(a.a * a_mult + b.a * b_mult),
};
}
};
enum class ResizeAlgorithm
{
Nearest,
Bilinear,
};
public:
static BAN::ErrorOr<BAN::UniqPtr<Image>> load_from_file(BAN::StringView path);
BAN::ErrorOr<BAN::UniqPtr<Image>> resize(uint64_t new_width, uint64_t new_height, ResizeAlgorithm = ResizeAlgorithm::Bilinear);
Color get_color(uint64_t x, uint64_t y) const { return m_bitmap[y * width() + x]; }
const BAN::Vector<Color> bitmap() const { return m_bitmap; }
uint64_t width() const { return m_width; }
uint64_t height() const { return m_height; }
static constexpr bool validate_size(uint64_t width, uint64_t height)
{
// width and height must fit in int64_t and width * height * sizeof(Color) has to not overflow
if (width > static_cast<uint64_t>(BAN::numeric_limits<int64_t>::max()))
return false;
if (height > static_cast<uint64_t>(BAN::numeric_limits<int64_t>::max()))
return false;
if (BAN::Math::will_multiplication_overflow(width, height))
return false;
if (BAN::Math::will_multiplication_overflow(width * height, sizeof(Color)))
return false;
return true;
}
private:
Image(uint64_t width, uint64_t height, BAN::Vector<Color>&& bitmap)
: m_width(width)
, m_height(height)
, m_bitmap(BAN::move(bitmap))
{
ASSERT(validate_size(m_width, m_height));
ASSERT(m_bitmap.size() >= m_width * m_height);
}
private:
const uint64_t m_width;
const uint64_t m_height;
const BAN::Vector<Color> m_bitmap;
friend class BAN::UniqPtr<Image>;
};
}

View File

@ -0,0 +1,10 @@
#include <BAN/ByteSpan.h>
#include <LibImage/Image.h>
namespace LibImage
{
BAN::ErrorOr<BAN::UniqPtr<Image>> load_netbpm(BAN::ConstByteSpan);
}

View File

@ -64,35 +64,14 @@ static constexpr int get_base_digit(char c, int base)
return -1;
}
template<BAN::integral T>
static constexpr bool will_multiplication_overflow(T a, T b)
{
if (a == 0 || b == 0)
return false;
if ((a > 0) == (b > 0))
return a > BAN::numeric_limits<T>::max() / b;
else
return a < BAN::numeric_limits<T>::min() / b;
}
template<BAN::integral T>
static constexpr bool will_addition_overflow(T a, T b)
{
if (a > 0 && b > 0)
return a > BAN::numeric_limits<T>::max() - b;
if (a < 0 && b < 0)
return a < BAN::numeric_limits<T>::min() - b;
return false;
}
template<BAN::integral T>
static constexpr bool will_digit_append_overflow(bool negative, T current, int digit, int base)
{
if (BAN::is_unsigned_v<T> && negative && digit)
return true;
if (will_multiplication_overflow<T>(current, base))
if (BAN::Math::will_multiplication_overflow<T>(current, base))
return true;
if (will_addition_overflow<T>(current * base, current < 0 ? -digit : digit))
if (BAN::Math::will_addition_overflow<T>(current * base, current < 0 ? -digit : digit))
return true;
return false;
}
@ -286,7 +265,7 @@ static T strtoT(const char* str, char** endp, int& error)
int extra_exponent = strtoT<int>(str + 1, &maybe_end, 10, exp_error);
if (exp_error != EINVAL)
{
if (exp_error == ERANGE || will_addition_overflow(exponent, extra_exponent))
if (exp_error == ERANGE || BAN::Math::will_addition_overflow(exponent, extra_exponent))
exponent = negative ? BAN::numeric_limits<int>::min() : BAN::numeric_limits<int>::max();
else
exponent += extra_exponent;

View File

@ -4,13 +4,11 @@ project(image CXX)
set(SOURCES
main.cpp
Image.cpp
Netbpm.cpp
)
add_executable(image ${SOURCES})
target_compile_options(image PUBLIC -O2 -g)
target_link_libraries(image PUBLIC libc ban)
target_link_libraries(image PUBLIC libc ban libimage)
add_custom_target(image-install
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/image ${BANAN_BIN}/

View File

@ -1,144 +0,0 @@
#include "Image.h"
#include "Netbpm.h"
#include <fcntl.h>
#include <stdlib.h>
#include <sys/framebuffer.h>
#include <sys/mman.h>
BAN::UniqPtr<Image> Image::load_from_file(BAN::StringView path)
{
int fd = -1;
if (path.data()[path.size()] == '\0')
{
fd = open(path.data(), O_RDONLY);
}
else
{
char* buffer = (char*)malloc(path.size() + 1);
if (!buffer)
{
perror("malloc");
return {};
}
memcpy(buffer, path.data(), path.size());
buffer[path.size()] = '\0';
fd = open(path.data(), O_RDONLY);
free(buffer);
}
if (fd == -1)
{
perror("open");
return {};
}
struct stat st;
if (fstat(fd, &st) == -1)
{
perror("fstat");
close(fd);
return {};
}
if (st.st_size < 2)
{
fprintf(stderr, "invalid image (too small)\n");
close(fd);
return {};
}
void* addr = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED)
{
perror("mmap");
close(fd);
return {};
}
BAN::UniqPtr<Image> image;
uint16_t u16_signature = *reinterpret_cast<uint16_t*>(addr);
switch (u16_signature)
{
case 0x3650:
case 0x3550:
case 0x3450:
case 0x3350:
case 0x3250:
case 0x3150:
if (auto res = load_netbpm(addr, st.st_size); res.is_error())
fprintf(stderr, "%s\n", strerror(res.error().get_error_code()));
else
image = res.release_value();
break;
default:
fprintf(stderr, "unrecognized image format\n");
break;
}
munmap(addr, st.st_size);
close(fd);
return image;
}
bool Image::render_to_framebuffer()
{
int fd = open("/dev/fb0", O_RDWR);
if (fd == -1)
{
perror("open");
return false;
}
framebuffer_info_t fb_info;
if (pread(fd, &fb_info, sizeof(fb_info), -1) == -1)
{
perror("pread");
close(fd);
return false;
}
ASSERT(BANAN_FB_BPP == 24 || BANAN_FB_BPP == 32);
size_t mmap_size = fb_info.height * fb_info.width * BANAN_FB_BPP / 8;
void* mmap_addr = mmap(nullptr, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mmap_addr == MAP_FAILED)
{
perror("mmap");
close(fd);
return false;
}
uint8_t* u8_fb = reinterpret_cast<uint8_t*>(mmap_addr);
for (uint64_t y = 0; y < BAN::Math::min<uint64_t>(height(), fb_info.height); y++)
{
for (uint64_t x = 0; x < BAN::Math::min<uint64_t>(width(), fb_info.width); x++)
{
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 0] = m_bitmap[y * width() + x].r;
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 1] = m_bitmap[y * width() + x].g;
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 2] = m_bitmap[y * width() + x].b;
if constexpr(BANAN_FB_BPP == 32)
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 3] = m_bitmap[y * width() + x].a;
}
}
if (msync(mmap_addr, mmap_size, MS_SYNC) == -1)
{
perror("msync");
munmap(mmap_addr, mmap_size);
close(fd);
return false;
}
munmap(mmap_addr, mmap_size);
close(fd);
return true;
}

View File

@ -1,39 +0,0 @@
#pragma once
#include <BAN/StringView.h>
#include <BAN/UniqPtr.h>
#include <BAN/Vector.h>
class Image
{
public:
struct Color
{
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
};
public:
static BAN::UniqPtr<Image> load_from_file(BAN::StringView path);
uint64_t width() const { return m_width; }
uint64_t height() const { return m_height; }
bool render_to_framebuffer();
private:
Image(uint64_t width, uint64_t height, BAN::Vector<Color>&& bitmap)
: m_width(width)
, m_height(height)
, m_bitmap(BAN::move(bitmap))
{ }
private:
const uint64_t m_width;
const uint64_t m_height;
const BAN::Vector<Color> m_bitmap;
friend class BAN::UniqPtr<Image>;
};

View File

@ -1,109 +0,0 @@
#include "Netbpm.h"
#include <BAN/Optional.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdio.h>
BAN::Optional<uint64_t> parse_u64(const uint8_t*& data, size_t data_size)
{
uint64_t result = 0;
// max supported size 10^20 - 1
for (size_t i = 0; i < 19; i++)
{
if (i >= data_size)
{
if (isdigit(*data))
return {};
return result;
}
if (!isdigit(*data))
return result;
result = (result * 10) + (*data - '0');
data++;
}
return {};
}
BAN::ErrorOr<BAN::UniqPtr<Image>> load_netbpm(const void* mmap_addr, size_t size)
{
if (size < 11)
{
fprintf(stderr, "invalid Netbpm image (too small)\n");
return BAN::Error::from_errno(EINVAL);
}
const uint8_t* u8_ptr = reinterpret_cast<const uint8_t*>(mmap_addr);
if (u8_ptr[0] != 'P')
{
fprintf(stderr, "not Netbpm image\n");
return BAN::Error::from_errno(EINVAL);
}
if (u8_ptr[1] != '6')
{
fprintf(stderr, "unsupported Netbpm image\n");
return BAN::Error::from_errno(EINVAL);
}
if (u8_ptr[2] != '\n')
{
fprintf(stderr, "invalid Netbpm image (invalid header)\n");
return BAN::Error::from_errno(EINVAL);
}
u8_ptr += 3;
auto width = parse_u64(u8_ptr, size - (u8_ptr - reinterpret_cast<const uint8_t*>(mmap_addr)));
if (!width.has_value() || *u8_ptr != ' ')
{
fprintf(stderr, "invalid Netbpm image (invalid width)\n");
return BAN::Error::from_errno(EINVAL);
}
u8_ptr++;
auto height = parse_u64(u8_ptr, size - (u8_ptr - reinterpret_cast<const uint8_t*>(mmap_addr)));
if (!height.has_value() || *u8_ptr != '\n')
{
fprintf(stderr, "invalid Netbpm image (invalid height)\n");
return BAN::Error::from_errno(EINVAL);
}
u8_ptr++;
auto header_end = parse_u64(u8_ptr, size - (u8_ptr - reinterpret_cast<const uint8_t*>(mmap_addr)));
if (!header_end.has_value() || *header_end != 255 || *u8_ptr != '\n')
{
fprintf(stderr, "invalid Netbpm image (invalid header end)\n");
return BAN::Error::from_errno(EINVAL);
}
u8_ptr++;
if (size - (u8_ptr - reinterpret_cast<const uint8_t*>(mmap_addr)) < *width * *height * 3)
{
fprintf(stderr, "invalid Netbpm image (too small file size)\n");
return BAN::Error::from_errno(EINVAL);
}
printf("Netbpm image %" PRIu64 "x%" PRIu64 "\n", *width, *height);
BAN::Vector<Image::Color> bitmap;
TRY(bitmap.resize(*width * *height));
// Fill bitmap
for (uint64_t y = 0; y < *height; y++)
{
for (uint64_t x = 0; x < *width; x++)
{
auto& pixel = bitmap[y * *width + x];
pixel.r = *u8_ptr++;
pixel.g = *u8_ptr++;
pixel.b = *u8_ptr++;
pixel.a = 0xFF;
}
}
return TRY(BAN::UniqPtr<Image>::create(*width, *height, BAN::move(bitmap)));
}

View File

@ -1,3 +0,0 @@
#include "Image.h"
BAN::ErrorOr<BAN::UniqPtr<Image>> load_netbpm(const void* mmap_addr, size_t size);

View File

@ -1,26 +1,95 @@
#include "Image.h"
#include <LibImage/Image.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/framebuffer.h>
#include <sys/mman.h>
#include <unistd.h>
void render_to_framebuffer(BAN::UniqPtr<LibImage::Image> image, bool scale)
{
int fd = open("/dev/fb0", O_RDWR);
if (fd == -1)
{
perror("open");
exit(1);
}
framebuffer_info_t fb_info;
if (pread(fd, &fb_info, sizeof(fb_info), -1) == -1)
{
perror("pread");
exit(1);
}
if (scale)
image = MUST(image->resize(fb_info.width, fb_info.height));
ASSERT(BANAN_FB_BPP == 24 || BANAN_FB_BPP == 32);
size_t mmap_size = fb_info.height * fb_info.width * BANAN_FB_BPP / 8;
void* mmap_addr = mmap(nullptr, mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (mmap_addr == MAP_FAILED)
{
perror("mmap");
exit(1);
}
uint8_t* u8_fb = reinterpret_cast<uint8_t*>(mmap_addr);
const auto& bitmap = image->bitmap();
for (uint64_t y = 0; y < BAN::Math::min<uint64_t>(image->height(), fb_info.height); y++)
{
for (uint64_t x = 0; x < BAN::Math::min<uint64_t>(image->width(), fb_info.width); x++)
{
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 0] = bitmap[y * image->width() + x].b;
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 1] = bitmap[y * image->width() + x].g;
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 2] = bitmap[y * image->width() + x].r;
if constexpr(BANAN_FB_BPP == 32)
u8_fb[(y * fb_info.width + x) * BANAN_FB_BPP / 8 + 3] = bitmap[y * image->width() + x].a;
}
}
if (msync(mmap_addr, mmap_size, MS_SYNC) == -1)
{
perror("msync");
exit(1);
}
munmap(mmap_addr, mmap_size);
close(fd);
}
int usage(char* arg0, int ret)
{
FILE* out = (ret == 0) ? stdout : stderr;
fprintf(out, "usage: %s IMAGE_PATH\n", arg0);
fprintf(out, "usage: %s [options]... IMAGE_PATH\n", arg0);
fprintf(out, "options:\n");
fprintf(out, " -h, --help: show this message and exit\n");
fprintf(out, " -s, --scale: scale image to framebuffer size\n");
return ret;
}
int main(int argc, char** argv)
{
if (argc != 2)
if (argc < 2)
return usage(argv[0], 1);
auto image = Image::load_from_file(argv[1]);
if (!image)
return 1;
bool scale = false;
for (int i = 1; i < argc - 1; i++)
{
if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--scale") == 0)
scale = true;
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
return usage(argv[0], 0);
else
return usage(argv[0], 1);
}
if (!image->render_to_framebuffer())
return 1;
auto image = MUST(LibImage::Image::load_from_file(argv[argc - 1]));
render_to_framebuffer(BAN::move(image), scale);
for (;;)
sleep(1);