63namespace pixel_constants {
157 return {255, 255, 255};
159 return {170, 170, 170};
167 return {255, 255, 0};
186[[nodiscard]]
constexpr auto rgb_to_grayscale(std::uint8_t r, std::uint8_t g, std::uint8_t b)
noexcept -> std::uint8_t {
189 return static_cast<std::uint8_t
>((299U * r + 587U * g + 114U * b) / 1000U);
202[[nodiscard]]
constexpr auto rgb_to_color_bw(std::uint8_t r, std::uint8_t g, std::uint8_t b)
noexcept ->
Color {
277 -> std::pair<std::size_t, std::uint8_t> {
278 const auto width_bytes = (width + 7) / 8;
279 const auto byte_index = (x / 8) + (y * width_bytes);
280 const auto bit_offset =
static_cast<std::uint8_t
>(x % 8);
282 return {byte_index, bit_mask};
311 -> std::pair<std::size_t, std::uint8_t> {
312 const auto width_bytes = (width + 3) / 4;
313 const auto byte_index = (x / 4) + (y * width_bytes);
314 const auto pixel_offset =
static_cast<std::uint8_t
>((x % 4) * 2);
315 return {byte_index, pixel_offset};
344 -> std::pair<std::size_t, std::size_t> {
345 const std::size_t pixel_index = (y * width) + x;
346 const std::size_t byte_index = (pixel_index * 3) / 8;
347 const std::size_t bit_offset = (pixel_index * 3) % 8;
348 return {byte_index, bit_offset};
374[[nodiscard]]
inline auto get_pixel_bw(std::span<const std::byte> buffer, std::size_t width, std::size_t x,
375 std::size_t y)
noexcept ->
Color {
377 if (byte_index >= buffer.size()) {
380 const auto byte_val =
static_cast<std::uint8_t
>(buffer[byte_index]);
407[[nodiscard]]
inline auto get_pixel_grayscale4(std::span<const std::byte> buffer, std::size_t width, std::size_t x,
408 std::size_t y)
noexcept ->
Color {
410 if (byte_index >= buffer.size()) {
413 const auto byte_val =
static_cast<std::uint8_t
>(buffer[byte_index]);
414 const auto gray_level = (byte_val >> (6 - pixel_shift)) & 0x03;
416 switch (gray_level) {
461[[nodiscard]]
inline auto get_pixel_bwr_bwy(std::span<const std::byte> buffer, std::size_t width, std::size_t height,
462 std::size_t x, std::size_t y,
bool is_bwr)
noexcept ->
Color {
463 const std::size_t plane_size = (width * height + 7) / 8;
466 if (byte_index >= buffer.size()) {
471 const auto bw_byte =
static_cast<std::uint8_t
>(buffer[byte_index]);
472 const bool is_white = (bw_byte & bit_mask) != 0;
475 if (byte_index + plane_size < buffer.size()) {
476 const auto color_byte =
static_cast<std::uint8_t
>(buffer[byte_index + plane_size]);
480 const bool is_color = (color_byte & bit_mask) == 0;
524[[nodiscard]]
inline auto get_pixel_spectra6(std::span<const std::byte> buffer, std::size_t width, std::size_t x,
525 std::size_t y)
noexcept ->
Color {
528 if (byte_index >= buffer.size()) {
532 std::uint8_t color_value = 0;
534 if (bit_offset <= 5) {
536 const auto shift =
static_cast<std::uint8_t
>(5 - bit_offset);
537 const auto byte_val =
static_cast<std::uint8_t
>(buffer[byte_index]);
541 const auto high_bits =
static_cast<std::uint8_t
>(8 - bit_offset);
542 const auto low_bits =
static_cast<std::uint8_t
>(3 - high_bits);
544 const auto high_byte =
static_cast<std::uint8_t
>(buffer[byte_index]);
545 color_value =
static_cast<std::uint8_t
>((high_byte & ((1U << high_bits) - 1)) << low_bits);
547 if (byte_index + 1 < buffer.size()) {
548 const auto low_byte =
static_cast<std::uint8_t
>(buffer[byte_index + 1]);
549 color_value =
static_cast<std::uint8_t
>(color_value | ((low_byte >> (8 - low_bits)) & ((1U << low_bits) - 1)));
570 std::size_t height, std::size_t x, std::size_t y)
noexcept ->
Color {
614inline auto set_pixel_bw(std::span<std::byte> buffer, std::size_t width, std::size_t x, std::size_t y,
615 Color color)
noexcept ->
void {
617 if (byte_index >= buffer.size()) {
621 auto &byte_val = buffer[byte_index];
623 byte_val =
static_cast<std::byte
>(
static_cast<std::uint8_t
>(byte_val) | bit_mask);
625 byte_val =
static_cast<std::byte
>(
static_cast<std::uint8_t
>(byte_val) & ~bit_mask);
658 Color color)
noexcept ->
void {
660 if (byte_index >= buffer.size()) {
664 auto &byte_val = buffer[byte_index];
669 byte_val =
static_cast<std::byte
>((
static_cast<std::uint8_t
>(byte_val) & ~mask) | (color_bits & mask));
706inline auto set_pixel_bwr_bwy(std::span<std::byte> buffer, std::size_t width, std::size_t height, std::size_t x,
707 std::size_t y,
Color color,
bool is_bwr)
noexcept ->
void {
708 const std::size_t plane_size = (width * height + 7) / 8;
711 if (byte_index >= buffer.size() || byte_index + plane_size >= buffer.size()) {
716 bool is_bw_white =
false;
717 bool is_color =
false;
723 }
else if (color == target_color) {
733 auto &bw_byte = buffer[byte_index];
735 bw_byte =
static_cast<std::byte
>(
static_cast<std::uint8_t
>(bw_byte) | bit_mask);
737 bw_byte =
static_cast<std::byte
>(
static_cast<std::uint8_t
>(bw_byte) & ~bit_mask);
741 auto &color_byte = buffer[byte_index + plane_size];
743 color_byte =
static_cast<std::byte
>(
static_cast<std::uint8_t
>(color_byte) | bit_mask);
745 color_byte =
static_cast<std::byte
>(
static_cast<std::uint8_t
>(color_byte) & ~bit_mask);
758inline auto set_pixel_spectra6(std::span<std::byte> buffer, std::size_t width, std::size_t x, std::size_t y,
759 Color color)
noexcept ->
void {
762 if (byte_index >= buffer.size()) {
768 if (bit_offset <= 5) {
770 const auto shift =
static_cast<std::uint8_t
>(5 - bit_offset);
772 auto &byte_val = buffer[byte_index];
773 byte_val =
static_cast<std::byte
>((
static_cast<std::uint8_t
>(byte_val) & ~mask) | ((color_value & 0x07) << shift));
776 const auto high_bits =
static_cast<std::uint8_t
>(8 - bit_offset);
777 const auto low_bits =
static_cast<std::uint8_t
>(3 - high_bits);
780 const auto high_mask =
static_cast<std::uint8_t
>((1U << high_bits) - 1);
781 auto &high_byte = buffer[byte_index];
782 high_byte =
static_cast<std::byte
>((
static_cast<std::uint8_t
>(high_byte) & ~high_mask) |
783 ((color_value >> low_bits) & high_mask));
786 if (byte_index + 1 < buffer.size()) {
787 const auto low_mask =
static_cast<std::uint8_t
>(((1U << low_bits) - 1) << (8 - low_bits));
788 auto &low_byte = buffer[byte_index + 1];
789 low_byte =
static_cast<std::byte
>((
static_cast<std::uint8_t
>(low_byte) & ~low_mask) |
790 ((color_value & ((1U << low_bits) - 1)) << (8 - low_bits)));
809 std::size_t x, std::size_t y,
Color color)
noexcept ->
void {
constexpr std::uint8_t GRAY_THRESHOLD_DARK
Definition pixel_codec.hpp:88
constexpr std::uint8_t GRAY_PIXEL_MASK
Definition pixel_codec.hpp:75
constexpr std::uint8_t GRAY_THRESHOLD_LIGHT
Definition pixel_codec.hpp:87
constexpr std::uint8_t GRAY_PIXELS_PER_BYTE
Definition pixel_codec.hpp:73
constexpr std::uint8_t BW_PIXELS_PER_BYTE
Definition pixel_codec.hpp:67
constexpr std::uint8_t GRAY_BITS_PER_PIXEL
Definition pixel_codec.hpp:74
constexpr std::uint8_t BW_MSB_MASK
Definition pixel_codec.hpp:68
constexpr std::uint8_t SPECTRA6_BITS_PER_PIXEL
Definition pixel_codec.hpp:80
constexpr std::uint8_t SPECTRA6_COLOR_MASK
Definition pixel_codec.hpp:81
constexpr std::uint8_t GRAY_THRESHOLD_WHITE
Definition pixel_codec.hpp:86
auto get_pixel_bw(std::span< const std::byte > buffer, std::size_t width, std::size_t x, std::size_t y) noexcept -> Color
Read a pixel from a B/W mode buffer.
Definition pixel_codec.hpp:374
auto set_pixel_grayscale4(std::span< std::byte > buffer, std::size_t width, std::size_t x, std::size_t y, Color color) noexcept -> void
Write a pixel to a Grayscale4 mode buffer.
Definition pixel_codec.hpp:657
constexpr auto spectra6_value_to_color(std::uint8_t value) noexcept -> Color
Convert a 3-bit Spectra6 value to Color.
Definition pixel_codec.hpp:140
constexpr auto rgb_to_color(DisplayMode mode, std::uint8_t r, std::uint8_t g, std::uint8_t b) noexcept -> Color
Convert RGB to Color based on display mode.
Definition pixel_codec.hpp:240
auto set_pixel_bw(std::span< std::byte > buffer, std::size_t width, std::size_t x, std::size_t y, Color color) noexcept -> void
Write a pixel to a B/W mode buffer.
Definition pixel_codec.hpp:614
auto set_pixel_spectra6(std::span< std::byte > buffer, std::size_t width, std::size_t x, std::size_t y, Color color) noexcept -> void
Write a pixel to a Spectra6 mode buffer.
Definition pixel_codec.hpp:758
constexpr std::array< Color, 8 > SPECTRA6_VALUE_TO_COLOR
Mapping from 3-bit Spectra6 value to Color enum.
Definition pixel_codec.hpp:98
constexpr auto calculate_bw_position(std::size_t width, std::size_t x, std::size_t y) noexcept -> std::pair< std::size_t, std::uint8_t >
Calculate byte index and bit mask for B/W mode pixel.
Definition pixel_codec.hpp:276
auto set_pixel_in_buffer(DisplayMode mode, std::span< std::byte > buffer, std::size_t width, std::size_t height, std::size_t x, std::size_t y, Color color) noexcept -> void
Write a pixel to a buffer based on display mode.
Definition pixel_codec.hpp:808
constexpr auto rgb_to_color_grayscale4(std::uint8_t r, std::uint8_t g, std::uint8_t b) noexcept -> Color
Convert RGB to Color for 4-level grayscale mode.
Definition pixel_codec.hpp:217
constexpr auto color_to_rgb(Color color) noexcept -> RGB
Convert a Color enum to RGB values.
Definition pixel_codec.hpp:154
constexpr auto spectra6_color_to_value(Color color) noexcept -> std::uint8_t
Convert a Color to its 3-bit Spectra6 value.
Definition pixel_codec.hpp:115
constexpr auto rgb_to_color_bw(std::uint8_t r, std::uint8_t g, std::uint8_t b) noexcept -> Color
Convert RGB to Color for black/white mode.
Definition pixel_codec.hpp:202
constexpr auto rgb_to_grayscale(std::uint8_t r, std::uint8_t g, std::uint8_t b) noexcept -> std::uint8_t
Convert RGB values to grayscale using standard luminance formula.
Definition pixel_codec.hpp:186
auto get_pixel_spectra6(std::span< const std::byte > buffer, std::size_t width, std::size_t x, std::size_t y) noexcept -> Color
Read a pixel from a Spectra6 mode buffer.
Definition pixel_codec.hpp:524
auto get_pixel_grayscale4(std::span< const std::byte > buffer, std::size_t width, std::size_t x, std::size_t y) noexcept -> Color
Read a pixel from a Grayscale4 mode buffer.
Definition pixel_codec.hpp:407
constexpr auto calculate_gray_position(std::size_t width, std::size_t x, std::size_t y) noexcept -> std::pair< std::size_t, std::uint8_t >
Calculate byte index and pixel shift for Grayscale4 mode pixel.
Definition pixel_codec.hpp:310
Color
Definition types.hpp:32
@ Gray1
First gray level - lighter (Grayscale4 mode only)
@ White
White (or lightest gray in grayscale modes)
@ Yellow
Yellow (BWY and Spectra6 modes)
@ Blue
Blue (Spectra6 mode only)
@ Green
Green (Spectra6 mode only)
@ Gray2
Second gray level - darker (Grayscale4 mode only)
@ Black
Black (or darkest gray in grayscale modes)
@ Red
Red (BWR and Spectra6 modes)
constexpr auto calculate_spectra6_position(std::size_t width, std::size_t x, std::size_t y) noexcept -> std::pair< std::size_t, std::size_t >
Calculate byte index and bit offset for Spectra6 (3-bit) mode pixel.
Definition pixel_codec.hpp:343
DisplayMode
Definition driver.hpp:46
@ BWY
Black, White, Yellow (3 colors, typically 2-bit)
@ Spectra6
6-color: Black, White, Red, Yellow, Blue, Green (3-bit)
@ BlackWhite
1-bit black and white (2 colors)
@ Grayscale4
2-bit 4-level grayscale
@ BWR
Black, White, Red (3 colors, typically 2-bit)
auto set_pixel_bwr_bwy(std::span< std::byte > buffer, std::size_t width, std::size_t height, std::size_t x, std::size_t y, Color color, bool is_bwr) noexcept -> void
Write a pixel to a BWR or BWY mode buffer.
Definition pixel_codec.hpp:706
auto get_pixel_from_buffer(DisplayMode mode, std::span< const std::byte > buffer, std::size_t width, std::size_t height, std::size_t x, std::size_t y) noexcept -> Color
Read a pixel from a buffer based on display mode.
Definition pixel_codec.hpp:569
auto get_pixel_bwr_bwy(std::span< const std::byte > buffer, std::size_t width, std::size_t height, std::size_t x, std::size_t y, bool is_bwr) noexcept -> Color
Read a pixel from a BWR or BWY mode buffer.
Definition pixel_codec.hpp:461