-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper functions to convert bitmap to different formats
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
Showing
5 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |