Skip to content

Commit 9062269

Browse files
authored
TextureImageが拡張子jpg,tifには対応していてもjpeg,tiffには対応していない問題を修正。そのテストを記述。 (Synesthesias#188)
1 parent 4f45638 commit 9062269

File tree

10 files changed

+38
-20
lines changed

10 files changed

+38
-20
lines changed
192 KB
Binary file not shown.
15.2 KB
Loading
15.2 KB
Loading
5.78 KB
Loading
192 KB
Binary file not shown.
192 KB
Binary file not shown.

include/plateau/texture/texture_image.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
#include <plateau/texture/jpeg_texture_image.h>
88
#include <plateau/texture/png_texture_image.h>
99
#include <plateau/texture/tiff_texture_image.h>
10+
#include <libplateau_api.h>
1011

1112
namespace plateau::texture {
12-
class TextureImage {
13+
class LIBPLATEAU_EXPORT TextureImage {
1314
public:
1415
enum class TextureType {
1516
None, Jpeg, Png, Tiff
@@ -64,11 +65,5 @@ namespace plateau::texture {
6465
size_t image_height_{};
6566
unsigned char image_color_{};
6667
std::string image_file_path_;
67-
68-
const std::string png_extension_ = ".png";
69-
const std::string jpeg_extension_ = ".jpg";
70-
const std::string tiff_extension_ = ".tif";
71-
72-
static bool checkFileExtension(const std::string& file_name, const std::string& file_extension);
7368
};
7469
}

src/texture/texture_image.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@
22
#include <plateau/texture/texture_image.h>
33

44
#include <string>
5+
#include <filesystem>
56

67
namespace plateau::texture {
8+
namespace fs = std::filesystem;
79
TextureImage::TextureImage(const std::string& file_name, const size_t height_limit) {
810
reset();
911

1012
image_file_path_ = file_name;
13+
auto extension = fs::u8path(file_name).extension().u8string();
1114

12-
if (checkFileExtension(file_name, jpeg_extension_)) {
15+
if (extension == ".jpg" || extension == ".jpeg") {
1316
texture_type_ = TextureType::Jpeg;
1417
jpeg_image_.init(file_name, height_limit);
1518

1619
image_width_ = jpeg_image_.getWidth();
1720
image_height_ = jpeg_image_.getHeight();
18-
} else if (checkFileExtension(file_name, tiff_extension_)) {
21+
} else if (extension == ".tif" || extension == ".tiff") {
1922
texture_type_ = TextureType::Tiff;
2023
tiff_image_.init(file_name);
2124

2225
image_width_ = tiff_image_.getWidth();
2326
image_height_ = tiff_image_.getHeight();
24-
} else if (checkFileExtension(file_name, png_extension_)) {
27+
} else if (extension == ".png") {
2528
texture_type_ = TextureType::Png;
2629
png_image_.init(file_name);
2730

@@ -50,16 +53,6 @@ namespace plateau::texture {
5053
texture_type_ = TextureType::None;
5154
}
5255

53-
bool TextureImage::checkFileExtension(const std::string& file_name, const std::string& file_extension) {
54-
auto is_valid = false;
55-
56-
const size_t last_pos = file_name.find(file_extension);
57-
if (file_name.length() == (last_pos + file_extension.length())) {
58-
is_valid = true;
59-
}
60-
return is_valid;
61-
}
62-
6356
// 指定されたファイル名で、jpegファイルを保存
6457
void TextureImage::save(const std::string& file_name) {
6558
jpeg_image_.save(file_name);

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ add_executable(plateau_test
4343
"test_server_dataset_accessor.cpp"
4444
"test_fbx_writer.cpp"
4545
"test_lod_searcher.cpp"
46+
"test_texture_image.cpp"
4647
)
4748

4849
target_link_libraries(plateau_test gtest gtest_main plateau citygml)

test/test_texture_image.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <gtest/gtest.h>
2+
#include <plateau/texture/texture_image.h>
3+
4+
using namespace plateau::texture;
5+
namespace fs = std::filesystem;
6+
class TextureImageTest : public ::testing::Test {
7+
8+
protected:
9+
void SetUp() override {
10+
}
11+
public:
12+
static constexpr std::string_view test_data_dir = "../data/日本語パステスト/test_images/";
13+
};
14+
15+
namespace {
16+
TextureImage::TextureType getTextureTypeOf(const std::string& file_name) {
17+
auto dir = fs::absolute(fs::u8path(TextureImageTest::test_data_dir));
18+
auto texture = TextureImage((fs::path(dir) / file_name).u8string(), 256);
19+
return texture.getTextureType();
20+
}
21+
}
22+
23+
TEST_F(TextureImageTest, getTextureType_Returns_Its_Extension) { // NOLINT
24+
ASSERT_EQ(getTextureTypeOf("test_image.tif"), TextureImage::TextureType::Tiff);
25+
ASSERT_EQ(getTextureTypeOf("test_image.tiff"), TextureImage::TextureType::Tiff);
26+
ASSERT_EQ(getTextureTypeOf("test_image.jpg"), TextureImage::TextureType::Jpeg);
27+
ASSERT_EQ(getTextureTypeOf("test_image.jpeg"), TextureImage::TextureType::Jpeg);
28+
ASSERT_EQ(getTextureTypeOf("test_image.png"), TextureImage::TextureType::Png);
29+
}

0 commit comments

Comments
 (0)