Skip to content

Commit

Permalink
Add helper functions to convert bitmap to different formats
Browse files Browse the repository at this point in the history
Trex for now will keep using only 1-byte GRAY8 format as it provides all the information needed to render a glyph.

Emojis are not supported yet, unfortunately. They are more of a system thing rather than font.

closes #4
  • Loading branch information
KyrietS committed Jan 31, 2024
1 parent a0b3e32 commit e688503
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/Trex/Atlas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace Trex

enum class RenderMode { DEFAULT, SDF, /* LCD */ };

using AtlasBitmap = std::vector<uint8_t>;
using AtlasGlyphs = std::map<uint32_t, Glyph>;
using AtlasBitmap = std::vector<uint8_t>; // 1-byte GRAYSCALE
using AtlasGlyphs = std::map<uint32_t, Glyph>; // key: glyph index in font (NOT codepoint!)

class Atlas
{
Expand Down
22 changes: 22 additions & 0 deletions include/Trex/BitmapHelpers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <vector>
#include <span>

// Input to all these functions is always a 1-byte grayscale bitmap
// Which means that each pixel is represented by a single byte where
// value 0 means black and 255 means white.
//
// All conversion functions keep the grayscale value of the pixels.
// Ondly the format of the bitmap is changed.

namespace Trex
{
// Convert 1-byte: GRAY8 to 2-byte: GRAYALPHA88
std::vector<uint8_t> ConvertToGrayAlpha(std::span<const uint8_t> input);

// Convert 1-byte: GRAY8 to 3-byte: RGB888
std::vector<uint8_t> ConvertToRGB(std::span<const uint8_t> input);

// Convert 1-byte: GRAY8 to 4-byte: RGBA8888
std::vector<uint8_t> ConvertToRGBA(std::span<const uint8_t> input);
}
40 changes: 40 additions & 0 deletions src/BitmapHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "Trex/BitmapHelpers.hpp"

namespace Trex
{
std::vector<uint8_t> ConvertToGrayAlpha(const std::span<const uint8_t> input)
{
std::vector<uint8_t> output(input.size() * 2);
for (size_t i = 0; i < input.size(); i++)
{
output[i * 2] = input[i];
output[i * 2 + 1] = 255;
}
return output;
}

std::vector<uint8_t> ConvertToRGB(const std::span<const uint8_t> input)
{
std::vector<uint8_t> output(input.size() * 3);
for (size_t i = 0; i < input.size(); i++)
{
output[i * 3] = input[i];
output[i * 3 + 1] = input[i];
output[i * 3 + 2] = input[i];
}
return output;
}

std::vector<uint8_t> ConvertToRGBA(const std::span<const uint8_t> input)
{
std::vector<uint8_t> output(input.size() * 4);
for (size_t i = 0; i < input.size(); i++)
{
output[i * 4] = input[i];
output[i * 4 + 1] = input[i];
output[i * 4 + 2] = input[i];
output[i * 4 + 3] = 255;
}
return output;
}
}
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ set(CMAKE_CXX_STANDARD 20)

add_executable(${TEST_TARGET}
TestAtlas.cpp
TestBitmapHelpers.cpp
TestFont.cpp
TestTextShaper.cpp
TestCharset.cpp
Expand Down
33 changes: 33 additions & 0 deletions tests/TestBitmapHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include "Trex/BitmapHelpers.hpp"
#include <gtest/gtest.h>
#include <vector>

struct BitmapHelpersTests : public ::testing::Test
{
const std::vector<uint8_t> bitmap = {0, 127,
128, 255};
};

TEST_F(BitmapHelpersTests, ConvertToGrayAlpha)
{
const std::vector<uint8_t> expected = {0, 255, 127, 255,
128, 255, 255, 255};
const auto actual = Trex::ConvertToGrayAlpha(bitmap);
EXPECT_EQ(expected, actual);
}

TEST_F(BitmapHelpersTests, ConvertToRGB)
{
const std::vector<uint8_t> expected = {0, 0, 0, 127, 127, 127,
128, 128, 128, 255, 255, 255};
const auto actual = Trex::ConvertToRGB(bitmap);
EXPECT_EQ(expected, actual);
}

TEST_F(BitmapHelpersTests, ConvertToRGBA)
{
const std::vector<uint8_t> expected = {0, 0, 0, 255, 127, 127, 127, 255,
128, 128, 128, 255, 255, 255, 255, 255};
const auto actual = Trex::ConvertToRGBA(bitmap);
EXPECT_EQ(expected, actual);
}

0 comments on commit e688503

Please sign in to comment.