Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various Fixes/Improvements #55

Merged
merged 7 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ jobs:
echo $LD_LIBRARY_PATH
cmake --build ./build

- name: Test
run: |
ctest --test-dir build

- name: Upload a Build Artifact
uses: actions/upload-artifact@v4
with:
Expand Down Expand Up @@ -66,6 +70,10 @@ jobs:
- name: Build
run: msbuild /m /p:Configuration=Release build\vtex2.sln

- name: Test
run: |
ctest --test-dir build

- name: Upload a Build Artifact
uses: actions/upload-artifact@v4
with:
Expand Down
45 changes: 45 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(CMAKE_CXX_STANDARD 20)

# Project settings
option(BUILD_GUI "Build the VTFViewer GUI" ON)
option(BUILD_TESTS "Build test binaries" OFF)

# Global flags, mainly for UNIX. Use $ORIGIN rpath & -fPIC
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
Expand All @@ -21,6 +22,20 @@ add_subdirectory(external/fmtlib)

add_definitions(-DVTFLIB_STATIC=1)

##############################
# Setup gtest
##############################
if (BUILD_TESTS)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.15.2
)
FetchContent_MakeAvailable(googletest)
enable_testing()
endif()

##############################
# Common code
##############################
Expand Down Expand Up @@ -99,6 +114,36 @@ if (BUILD_GUI)
)
endif()

##############################
# Tests
##############################

if (BUILD_TESTS)
include(GoogleTest)

add_executable(
vtex2_tests

src/tests/image_tests.cpp
)

target_link_libraries(
vtex2_tests PRIVATE

gtest_main
vtflib_static
com
)

target_include_directories(
vtex2_tests PRIVATE

src
)

gtest_discover_tests(vtex2_tests)
endif()

##############################
# Version header
##############################
Expand Down
2 changes: 1 addition & 1 deletion external/fmtlib
Submodule fmtlib updated 199 files
42 changes: 31 additions & 11 deletions src/cli/action_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace opts
static int width, height;
static int nomips;
static int toDX;
static int quiet;
} // namespace opts

static bool get_version_from_str(const std::string& str, int& major, int& minor);
Expand Down Expand Up @@ -226,6 +227,15 @@ const OptionList& ActionConvert::get_options() const {
.value(false)
.type(OptType::Bool)
.help("Treat the incoming normal map as an OpenGL normal map"));

opts::quiet = opts.add(
ActionOption()
.long_opt("--quiet")
.short_opt("-q")
.value(false)
.type(OptType::Bool)
.help("Silence output messages that aren't errors")
);
};
return opts;
}
Expand Down Expand Up @@ -303,7 +313,7 @@ bool ActionConvert::process_file(
outFile = srcFile.parent_path() / srcFile.filename().replace_extension(".vtf");
}
else {
outFile = srcFile.parent_path() / userOutputFile;
outFile = userOutputFile;
}

auto format = ImageFormatFromUserString(formatStr.c_str());
Expand All @@ -319,15 +329,15 @@ bool ActionConvert::process_file(
std::max(formatInfo.uiRedBitsPerPixel, formatInfo.uiGreenBitsPerPixel),
std::max(formatInfo.uiBlueBitsPerPixel, formatInfo.uiAlphaBitsPerPixel));
if (maxBpp > 16) {
procChanType = imglib::Float;
procChanType = imglib::ChannelType::Float;
return IMAGE_FORMAT_RGBA32323232F;
}
else if (maxBpp > 8) {
procChanType = imglib::UInt16;
return IMAGE_FORMAT_RGBA16161616F;
procChanType = imglib::ChannelType::UInt16;
return IMAGE_FORMAT_RGBA16161616;
}
else {
procChanType = imglib::UInt8;
procChanType = imglib::ChannelType::UInt8;
return IMAGE_FORMAT_RGBA8888;
}
}();
Expand Down Expand Up @@ -398,12 +408,14 @@ bool ActionConvert::process_file(
}

// Report file sizes
if (initialSize != 0) {
fmt::print(
"{} ({} bytes) -> {} ({} bytes)\n", srcFile.string(), initialSize, outFile.string(), vtfFile->GetSize());
}
else {
fmt::print("{} -> {} ({} bytes)\n", srcFile.string(), outFile.string(), vtfFile->GetSize());
if (!opts.get<bool>(opts::quiet)) {
if (initialSize != 0) {
fmt::print(
"{} ({} KiB) -> {} ({} KiB)\n", srcFile.string(), initialSize / 1024, outFile.string(), vtfFile->GetSize() / 1024);
}
else {
fmt::print("{} -> {} ({} KiB)\n", srcFile.string(), outFile.string(), vtfFile->GetSize() / 1024);
}
}

return true;
Expand Down Expand Up @@ -521,6 +533,14 @@ bool ActionConvert::add_image_data(
return false;
}

// Hack for VTFLib; Ensure we have an alpha channel because that's well supported in that horrible code
if (image->channels() < 4 && image->type() != imglib::ChannelType::UInt8) {
if (!image->convert(image->type(), 4)) {
std::cerr << fmt::format("Failed to convert {}\n", imageSrc.string());
return false;
}
}

// Add the raw image data
return add_image_data_raw(
file, image->data(), format, image->vtf_format(), image->width(), image->height(), create);
Expand Down
15 changes: 13 additions & 2 deletions src/cli/action_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace opts
static int mip;
static int recursive;
static int noalpha;
static int quiet;
} // namespace opts

std::string ActionExtract::get_help() const {
Expand Down Expand Up @@ -82,6 +83,15 @@ const OptionList& ActionExtract::get_options() const {
.type(OptType::Bool)
.value(false)
.help("Exclude alpha channel from converted image"));

opts::quiet = opts.add(
ActionOption()
.short_opt("-q")
.long_opt("--quiet")
.type(OptType::Bool)
.value(false)
.help("Silence output messages that aren't errors")
);
};
return opts;
}
Expand Down Expand Up @@ -152,7 +162,8 @@ bool ActionExtract::extract_file(
outFile = vtfPath.parent_path() / vtfPath.filename().replace_extension(ext);
}

fmt::print("{} -> {}\n", vtfPath.string(), outFile.string());
if (!opts.get<bool>(opts::quiet))
fmt::print("{} -> {}\n", vtfPath.string(), outFile.string());

// Validate mipmap selection
if (mip > file_->GetMipmapCount()) {
Expand Down Expand Up @@ -217,7 +228,7 @@ bool ActionExtract::extract_file(
return false;
}

imglib::Image image(imageData, destIsFloat ? imglib::Float : imglib::UInt8, comps, w, h, true);
imglib::Image image(imageData, destIsFloat ? imglib::ChannelType::Float : imglib::ChannelType::UInt8, comps, w, h, true);
if (!image.save(outFile.string().c_str(), targetFmt)) {
std::cerr << fmt::format("Could not save image to '{}'!\n", outFile.string());
return false;
Expand Down
Loading
Loading