#include "monoformat_schema.hpp" #include namespace monoformat { MemoryOneBitBuffer::MemoryOneBitBuffer(std::span buffer, std::uint16_t width, std::uint16_t height) : m_width{width} , m_height{height} , m_buffer{buffer} { std::size_t expectedSize = (m_width * m_height + 8 - 1) / 8; if (m_buffer.size() < expectedSize) { m_width = 0; m_height = 0; } } MemoryOneBitBuffer::~MemoryOneBitBuffer() { } bool MemoryOneBitBuffer::isPixelSet(std::uint16_t x, std::uint16_t y) const { if (x >= m_width || y >= m_height) { return false; } std::size_t pxIndex = static_cast(y) * m_width + x; std::size_t byteIndex = pxIndex / 8; std::size_t bitIndex = pxIndex % 8; return ((static_cast(m_buffer[byteIndex]) >> bitIndex) & 0x01) == 0x01; } void MemoryOneBitBuffer::setPixel(std::uint16_t x, std::uint16_t y, bool value) { if (x >= m_width || y >= m_height) { return; } std::size_t pxIndex = static_cast(y) * m_width + x; std::size_t byteIndex = pxIndex / 8; std::size_t bitIndex = pxIndex % 8; if (value) { m_buffer[byteIndex] = static_cast(static_cast(m_buffer[byteIndex]) | (1u << bitIndex)); } else { m_buffer[byteIndex] = static_cast(static_cast(m_buffer[byteIndex]) & ~(1u << bitIndex)); } } ConstMemoryOneBitBuffer::ConstMemoryOneBitBuffer(std::span buffer, std::uint16_t width, std::uint16_t height) : m_width{width} , m_height{height} , m_buffer{buffer} { std::size_t expectedSize = (m_width * m_height + 8 - 1) / 8; if (m_buffer.size() < expectedSize) { m_width = 0; m_height = 0; } } ConstMemoryOneBitBuffer::~ConstMemoryOneBitBuffer() { } bool ConstMemoryOneBitBuffer::isPixelSet(std::uint16_t x, std::uint16_t y) const { if (x >= m_width || y >= m_height) { return false; } std::size_t pxIndex = static_cast(y) * m_width + x; std::size_t byteIndex = pxIndex / 8; std::size_t bitIndex = pxIndex % 8; return ((static_cast(m_buffer[byteIndex]) >> bitIndex) & 0x01) == 0x01; } void ConstMemoryOneBitBuffer::setPixel(std::uint16_t x, std::uint16_t y, bool value) { std::ignore = x; std::ignore = y; std::ignore = value; } ClippedImage::ClippedImage(OneBitBufferInterface* underlying, std::uint16_t x, std::uint16_t y, std::uint16_t width, std::uint16_t height) : m_underlying{underlying} , m_x{x} , m_y{y} , m_width{width} , m_height{height} { } ClippedImage::~ClippedImage() { } bool ClippedImage::isPixelSet(std::uint16_t x, std::uint16_t y) const { if (x >= m_width || y >= m_height) { return false; } return m_underlying->isPixelSet(x + m_x, y + m_y); } void ClippedImage::setPixel(std::uint16_t x, std::uint16_t y, bool value) { if (x >= m_width || y >= m_height) { return; } m_underlying->setPixel(x + m_x, y + m_y, value); } } // namespace monoformat