Skip to content

Commit

Permalink
Add tile matrix emitter utility
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Jul 23, 2024
1 parent 9215230 commit 13f90ec
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
2 changes: 1 addition & 1 deletion source/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ target_sources(tactile-base
"inc/tactile/base/io/byte_stream.hpp"
"inc/tactile/base/io/color_parser.hpp"
"inc/tactile/base/io/int_parser.hpp"
"inc/tactile/base/io/tile_parser.hpp"
"inc/tactile/base/io/tile_io.hpp"
"inc/tactile/base/layer/layer_type.hpp"
"inc/tactile/base/layer/object_type.hpp"
"inc/tactile/base/layer/tile_encoding.hpp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include <bit> // endian, byteswap
#include <concepts> // same_as
#include <cstring> // memcpy
#include <utility> // cmp_not_equal

#include "tactile/base/container/maybe.hpp"
#include "tactile/base/container/vector.hpp"
#include "tactile/base/int.hpp"
#include "tactile/base/io/byte_stream.hpp"
#include "tactile/base/platform/bits.hpp"
#include "tactile/base/prelude.hpp"
#include "tactile/base/util/matrix_extent.hpp"
#include "tactile/base/util/matrix_index.hpp"
Expand Down Expand Up @@ -58,10 +58,11 @@ constexpr auto parse_raw_tile_matrix(const ByteStream& byte_stream,
{
auto tile_matrix = make_tile_matrix(extent);

const auto expected_byte_count = extent.rows * extent.cols * sizeof(TileID);
const auto expected_byte_count =
saturate_cast<usize>(extent.rows * extent.cols) * sizeof(TileID);
const auto real_byte_count = byte_stream.size();

if (std::cmp_not_equal(expected_byte_count, real_byte_count)) {
if (expected_byte_count != real_byte_count) {
return kNone;
}

Expand Down Expand Up @@ -89,4 +90,31 @@ constexpr auto parse_raw_tile_matrix(const ByteStream& byte_stream,
return tile_matrix;
}

[[nodiscard]]
inline auto to_byte_stream(const TileMatrix& tile_matrix) -> ByteStream
{
ByteStream bytes {};

const auto row_count = tile_matrix.size();
if (row_count < 1) {
return bytes;
}

const auto col_count = tile_matrix.front().size();
if (col_count < 1) {
return bytes;
}

bytes.reserve(row_count * col_count * sizeof(TileID));

for (usize row = 0; row < row_count; ++row) {
for (usize col = 0; col < col_count; ++col) {
const auto tile_id = to_little_endian(tile_matrix[row][col]);
each_byte(tile_id, [&](const uint8 byte) { bytes.push_back(byte); });
}
}

return bytes;
}

} // namespace tactile
2 changes: 1 addition & 1 deletion source/base/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ target_sources(tactile-base-test
PRIVATE
"src/io/color_parser_test.cpp"
"src/io/int_parser_test.cpp"
"src/io/tile_parser_test.cpp"
"src/io/tile_io_test.cpp"
"src/meta/attribute_test.cpp"
"src/numeric/saturate_cast_test.cpp"
"src/numeric/vec_common_test.cpp"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright (C) 2024 Albin Johansson (GNU General Public License v3.0)

#include "tactile/base/io/tile_parser.hpp"
#include "tactile/base/io/tile_io.hpp"

#include <gtest/gtest.h>

namespace tactile::test {

// tactile::parse_raw_tile_matrix
TEST(TileParser, ParseRawTileMatrix)
TEST(TileIO, ParseRawTileMatrix)
{
const ByteStream byte_stream {
// clang-format off
Expand Down Expand Up @@ -41,7 +41,7 @@ TEST(TileParser, ParseRawTileMatrix)
}

// tactile::parse_raw_tile_matrix
TEST(TileParser, ParseRawTileMatrixWithInsufficientData)
TEST(TileIO, ParseRawTileMatrixWithInsufficientData)
{
const ByteStream byte_stream {
// clang-format off
Expand All @@ -61,7 +61,7 @@ TEST(TileParser, ParseRawTileMatrixWithInsufficientData)
}

// tactile::parse_raw_tile_matrix
TEST(TileParser, ParseRawTileMatrixWithTooMuchData)
TEST(TileIO, ParseRawTileMatrixWithTooMuchData)
{
const ByteStream byte_stream {
// clang-format off
Expand All @@ -81,4 +81,42 @@ TEST(TileParser, ParseRawTileMatrixWithTooMuchData)
EXPECT_FALSE(tile_matrix.has_value());
}

// tactile::to_byte_stream [TileMatrix]
// tactile::parse_raw_tile_matrix
TEST(TileIO, TileMatrixToByteStreamAndBack)
{
const TileMatrix original_tile_matrix {
TileRow {10, 11, 12, 13},
TileRow {20, 21, 22, 23},
TileRow {30, 31, 32, 33},
};

const auto bytes = to_byte_stream(original_tile_matrix);
EXPECT_EQ(bytes.size(), 12 * sizeof(TileID));

const auto new_tile_matrix =
parse_raw_tile_matrix(bytes, MatrixExtent {3, 4}, TileIdFormat::kTactile);
ASSERT_TRUE(new_tile_matrix.has_value());

ASSERT_EQ(new_tile_matrix->size(), 3);
ASSERT_EQ(new_tile_matrix->at(0).size(), 4);
ASSERT_EQ(new_tile_matrix->at(1).size(), 4);
ASSERT_EQ(new_tile_matrix->at(2).size(), 4);

EXPECT_EQ(new_tile_matrix->at(0).at(0), TileID {10});
EXPECT_EQ(new_tile_matrix->at(0).at(1), TileID {11});
EXPECT_EQ(new_tile_matrix->at(0).at(2), TileID {12});
EXPECT_EQ(new_tile_matrix->at(0).at(3), TileID {13});

EXPECT_EQ(new_tile_matrix->at(1).at(0), TileID {20});
EXPECT_EQ(new_tile_matrix->at(1).at(1), TileID {21});
EXPECT_EQ(new_tile_matrix->at(1).at(2), TileID {22});
EXPECT_EQ(new_tile_matrix->at(1).at(3), TileID {23});

EXPECT_EQ(new_tile_matrix->at(2).at(0), TileID {30});
EXPECT_EQ(new_tile_matrix->at(2).at(1), TileID {31});
EXPECT_EQ(new_tile_matrix->at(2).at(2), TileID {32});
EXPECT_EQ(new_tile_matrix->at(2).at(3), TileID {33});
}

} // namespace tactile::test
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <cppcodec/base64_default_rfc4648.hpp>

#include "tactile/base/io/compress/compressor.hpp"
#include "tactile/base/io/tile_parser.hpp"
#include "tactile/base/io/tile_io.hpp"
#include "tactile/base/numeric/saturate_cast.hpp"
#include "tactile/base/runtime.hpp"
#include "tactile/base/util/matrix_index.hpp"
Expand Down

0 comments on commit 13f90ec

Please sign in to comment.