ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
board_view_pixels.c
Go to the documentation of this file.
1
12
13#include <string.h>
14#include <sys/stat.h>
15
16#include "board_view.h"
18#include "ra8_attributes.h"
19
28
44RA8_INTERNAL static size_t internal_available_bytes(int fd, off_t position, size_t count)
45{
46 struct stat metadata = {};
47 if ((fstat(fd, &metadata) != 0) || (metadata.st_size < 0) ||
48 (((uint64_t)metadata.st_size % sizeof(uint16_t)) != 0U)) {
49 return 0U;
50 }
51 const uint64_t pixels = (uint64_t)metadata.st_size / sizeof(uint16_t);
52 const uint64_t output_size = pixels * sizeof(uint32_t);
53 if ((uint64_t)position >= output_size) {
54 return 0U;
55 }
56 const size_t available = (size_t)(output_size - (uint64_t)position);
57 return (count < available) ? count : available;
58}
59
72RA8_INTERNAL static void
73internal_expand_chunk(const uint16_t* rgb565, uint32_t* rgb888, size_t pixel_count)
74{
75 for (size_t index = 0U; index < pixel_count; index++) {
76 const uint32_t pixel = rgb565[index];
77 const uint32_t red5 = (pixel >> k_rgb565_red_shift) & k_rgb565_five_bit_max;
78 const uint32_t green6 = (pixel >> k_rgb565_green_shift) & k_rgb565_six_bit_max;
79 const uint32_t blue5 = pixel & k_rgb565_five_bit_max;
80 const uint32_t red8 = (red5 << 3U) | (red5 >> 2U);
81 const uint32_t green8 = (green6 << 2U) | (green6 >> 4U);
82 const uint32_t blue8 = (blue5 << 3U) | (blue5 >> 2U);
83 rgb888[index] = (red8 << 16U) | (green8 << 8U) | blue8;
84 }
85}
86
87size_t board_view_read_rgb888_fd(int fd, off_t position, void* buffer, size_t count)
88{
89 if ((fd < 0) || (position < 0) || (buffer == nullptr) || (count == 0U)) {
90 return 0U;
91 }
92 const size_t available = internal_available_bytes(fd, position, count);
93 uint8_t* destination = (uint8_t*)buffer;
94 size_t copied = 0U;
95 while (copied < available) {
96 const uint64_t stream_offset = (uint64_t)position + copied;
97 const uint64_t first_pixel = stream_offset / sizeof(uint32_t);
98 const size_t byte_skip = (size_t)(stream_offset % sizeof(uint32_t));
99 const size_t wanted = available - copied;
100 size_t pixel_count = (byte_skip + wanted + sizeof(uint32_t) - 1U) / sizeof(uint32_t);
101 if (pixel_count > k_view_pixel_chunk) {
102 pixel_count = k_view_pixel_chunk;
103 }
104 uint16_t rgb565[k_view_pixel_chunk];
105 uint32_t rgb888[k_view_pixel_chunk];
107 rgb565,
108 pixel_count * sizeof(uint16_t),
109 (off_t)(first_pixel * sizeof(uint16_t)))
110 .status != k_emu_io_ok) {
111 return 0U;
112 }
113 internal_expand_chunk(rgb565, rgb888, pixel_count);
114 const size_t generated = (pixel_count * sizeof(uint32_t)) - byte_skip;
115 const size_t take = ((available - copied) < generated) ? (available - copied) : generated;
116 (void)memcpy(&destination[copied], &((const uint8_t*)rgb888)[byte_skip], take);
117 copied += take;
118 }
119 return copied;
120}
Minimal self-contained macOS window for the board emulator.
size_t board_view_read_rgb888_fd(int fd, off_t position, void *buffer, size_t count)
Expand an arbitrary byte range from a sealed RGB565 descriptor.
view_pixel_geometry_t
Bounded conversion chunk and RGB565 channel geometry.
@ k_rgb565_five_bit_max
Five-bit channel mask.
@ k_rgb565_red_shift
Red-field least-significant bit.
@ k_rgb565_six_bit_max
Six-bit channel mask.
@ k_rgb565_green_shift
Green-field least-significant bit.
@ k_view_pixel_chunk
Fixed source/conversion chunk capacity.
static size_t internal_available_bytes(int fd, off_t position, size_t count)
Clamp one provider request to the bytes the surface can still supply.
static void internal_expand_chunk(const uint16_t *rgb565, uint32_t *rgb888, size_t pixel_count)
Expand one bounded RGB565 chunk into packed 32-bit RGB888 words.
Bounded raw-descriptor I/O seam for the RA8 emulator.
@ k_emu_io_ok
The complete operation succeeded.
emu_io_result_t priv_emu_io_pread_exact(int fd, void *buf, size_t count, off_t offset)
Read exactly count bytes at offset without changing the cursor.
Annotation-attribute framework macros for ra8-firmware.
#define RA8_INTERNAL
Marker that a function is intended to be static (file-local).
void * memcpy(void *dst, const void *src, size_t n)
Copy memory area between non-overlapping regions.