Skip to content

Commit

Permalink
Remove the GenerateImages target and create baseline images while upd…
Browse files Browse the repository at this point in the history
…ating the local baseline config. (#427)
  • Loading branch information
domchen authored Jan 9, 2025
1 parent 2d04317 commit 398adb9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 54 deletions.
19 changes: 7 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -524,26 +524,21 @@ if (TGFX_BUILD_TESTS)
list(REMOVE_ITEM TGFX_TEST_FILES ${TGFX_TEST_EXCLUDE_FILES})
endif ()

# used to update the local md5 data for baseline testing.
list(APPEND TGFX_BASELINE_DEFINES ${TGFX_TEST_DEFINES} UPDATE_BASELINE)
if (NOT TGFX_SKIP_GENERATE_BASELINE_IMAGES)
list(APPEND TGFX_BASELINE_DEFINES GENERATE_BASELINE_IMAGES)
endif ()
# used to update the local md5 data for baseline testing and generate baseline images to the
# out/ directory. each image has a "_base" suffix.
add_executable(UpdateBaseline ${TGFX_TEST_FILES})
add_dependencies(UpdateBaseline test-vendor)
target_include_directories(UpdateBaseline PUBLIC ${TGFX_TEST_INCLUDES})
target_include_directories(UpdateBaseline SYSTEM PRIVATE ${TGFX_INCLUDES})
target_compile_definitions(UpdateBaseline PUBLIC ${TGFX_TEST_DEFINES} UPDATE_BASELINE)
target_compile_definitions(UpdateBaseline PUBLIC ${TGFX_BASELINE_DEFINES})
target_compile_options(UpdateBaseline PUBLIC ${TGFX_TEST_COMPILE_OPTIONS})
target_link_options(UpdateBaseline PUBLIC ${TGFX_TEST_LINK_OPTIONS})
target_link_libraries(UpdateBaseline ${TGFX_TEST_LIBS})

# used to generate baseline images to the out/ directory. each image has a "_base" suffix.
add_executable(GenerateImages ${TGFX_TEST_FILES})
add_dependencies(GenerateImages test-vendor)
target_include_directories(GenerateImages PUBLIC ${TGFX_TEST_INCLUDES})
target_include_directories(GenerateImages SYSTEM PRIVATE ${TGFX_INCLUDES})
target_compile_definitions(GenerateImages PUBLIC ${TGFX_TEST_DEFINES} GENERATOR_BASELINE_IMAGES)
target_compile_options(GenerateImages PUBLIC ${TGFX_TEST_COMPILE_OPTIONS})
target_link_options(GenerateImages PUBLIC ${TGFX_TEST_LINK_OPTIONS})
target_link_libraries(GenerateImages ${TGFX_TEST_LIBS})

add_executable(TGFXUnitTest ${TGFX_TEST_FILES})
add_dependencies(TGFXUnitTest test-vendor)
target_include_directories(TGFXUnitTest PUBLIC ${TGFX_TEST_INCLUDES})
Expand Down
3 changes: 3 additions & 0 deletions resources/apitest/imageReplacement.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 20 additions & 20 deletions test/baseline/version.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,18 @@
"rasterize_path_texture": "b062b9a"
},
"ReadPixelsTest": {
"JpegCodec_Decode_Alpha8": "d010fb8",
"JpegCodec_Decode_Gray8": "d010fb8",
"JpegCodec_Decode_RGB565": "d010fb8",
"JpegCodec_Decode_RGBA": "d010fb8",
"JpegCodec_Encode_Alpha8": "d010fb8",
"JpegCodec_Encode_Gray8": "d010fb8",
"JpegCodec_Encode_RGB565": "d010fb8",
"JpegCodec_Encode_RGBA": "d010fb8",
"NativeCodec_Decode_Alpha8": "840f02f",
"NativeCodec_Decode_RGBA": "840f02f",
"NativeCodec_Encode_Alpha8": "840f02f",
"NativeCodec_Encode_RGBA": "840f02f",
"JpegCodec_Decode_Alpha8": "afd80b4",
"JpegCodec_Decode_Gray8": "afd80b4",
"JpegCodec_Decode_RGB565": "afd80b4",
"JpegCodec_Decode_RGBA": "afd80b4",
"JpegCodec_Encode_Alpha8": "afd80b4",
"JpegCodec_Encode_Gray8": "afd80b4",
"JpegCodec_Encode_RGB565": "afd80b4",
"JpegCodec_Encode_RGBA": "afd80b4",
"NativeCodec_Decode_Alpha8": "afd80b4",
"NativeCodec_Decode_RGBA": "afd80b4",
"NativeCodec_Encode_Alpha8": "afd80b4",
"NativeCodec_Encode_RGBA": "afd80b4",
"PixelMap_BGRA_to_BGRA": "d010fb8",
"PixelMap_BGRA_to_RGBA": "d010fb8",
"PixelMap_BGRA_to_rgb_A": "d010fb8",
Expand All @@ -164,14 +164,14 @@
"PixelMap_rgb_A_to_RGBA": "d010fb8",
"PixelMap_rgb_A_to_alpha": "d010fb8",
"PixelMap_rgb_A_to_bgr_A": "d010fb8",
"PngCodec_Decode_Alpha8": "d010fb8",
"PngCodec_Decode_Gray8": "d010fb8",
"PngCodec_Decode_RGB565": "d010fb8",
"PngCodec_Decode_RGBA": "d010fb8",
"PngCodec_Encode_Alpha8": "d010fb8",
"PngCodec_Encode_Gray8": "d010fb8",
"PngCodec_Encode_RGB565": "d010fb8",
"PngCodec_Encode_RGBA": "d010fb8",
"PngCodec_Decode_Alpha8": "afd80b4",
"PngCodec_Decode_Gray8": "afd80b4",
"PngCodec_Decode_RGB565": "afd80b4",
"PngCodec_Decode_RGBA": "afd80b4",
"PngCodec_Encode_Alpha8": "afd80b4",
"PngCodec_Encode_Gray8": "afd80b4",
"PngCodec_Encode_RGB565": "afd80b4",
"PngCodec_Encode_RGBA": "afd80b4",
"Surface_BL_rgb_A_to_rgb_A": "d010fb8",
"Surface_BL_rgb_A_to_rgb_A_-100_-100": "d010fb8",
"Surface_BL_rgb_A_to_rgb_A_100_-100": "d010fb8",
Expand Down
34 changes: 19 additions & 15 deletions test/src/ReadPixelsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ TGFX_TEST(ReadPixelsTest, Surface) {
}

TGFX_TEST(ReadPixelsTest, PngCodec) {
auto rgbaCodec = MakeImageCodec("resources/apitest/test_timestretch.png");
auto rgbaCodec = MakeImageCodec("resources/apitest/imageReplacement.png");
ASSERT_TRUE(rgbaCodec != nullptr);
EXPECT_EQ(rgbaCodec->width(), 1280);
EXPECT_EQ(rgbaCodec->height(), 720);
EXPECT_EQ(rgbaCodec->width(), 110);
EXPECT_EQ(rgbaCodec->height(), 110);
EXPECT_EQ(rgbaCodec->orientation(), Orientation::TopLeft);
auto rowBytes = static_cast<size_t>(rgbaCodec->width()) * 4;
Buffer buffer(rowBytes * static_cast<size_t>(rgbaCodec->height()));
Expand All @@ -270,12 +270,16 @@ TGFX_TEST(ReadPixelsTest, PngCodec) {
auto RGBAInfo = ImageInfo::Make(rgbaCodec->width(), rgbaCodec->height(), ColorType::RGBA_8888,
AlphaType::Premultiplied);
EXPECT_TRUE(rgbaCodec->readPixels(RGBAInfo, pixels));
// set each pixel's alpha to 255
for (size_t i = 3; i < rowBytes * static_cast<size_t>(rgbaCodec->height()); i += 4) {
static_cast<uint8_t*>(pixels)[i] = 255;
}
CHECK_PIXELS(RGBAInfo, pixels, "PngCodec_Decode_RGBA");
auto bytes = ImageCodec::Encode(Pixmap(RGBAInfo, pixels), EncodedFormat::PNG, 100);
auto codec = ImageCodec::MakeFrom(bytes);
ASSERT_TRUE(codec != nullptr);
EXPECT_EQ(codec->width(), 1280);
EXPECT_EQ(codec->height(), 720);
EXPECT_EQ(codec->width(), 110);
EXPECT_EQ(codec->height(), 110);
EXPECT_EQ(codec->orientation(), Orientation::TopLeft);
buffer.clear();
EXPECT_TRUE(codec->readPixels(RGBAInfo, pixels));
Expand Down Expand Up @@ -378,10 +382,10 @@ TGFX_TEST(ReadPixelsTest, WebpCodec) {
}

TGFX_TEST(ReadPixelsTest, JpegCodec) {
auto rgbaCodec = MakeImageCodec("resources/apitest/rotation.jpg");
auto rgbaCodec = MakeImageCodec("resources/apitest/imageReplacement.jpg");
ASSERT_TRUE(rgbaCodec != nullptr);
EXPECT_EQ(rgbaCodec->width(), 4032);
EXPECT_EQ(rgbaCodec->height(), 3024);
EXPECT_EQ(rgbaCodec->width(), 110);
EXPECT_EQ(rgbaCodec->height(), 110);
EXPECT_EQ(rgbaCodec->orientation(), Orientation::RightTop);
auto RGBAInfo = ImageInfo::Make(rgbaCodec->width(), rgbaCodec->height(), ColorType::RGBA_8888,
AlphaType::Premultiplied);
Expand All @@ -393,8 +397,8 @@ TGFX_TEST(ReadPixelsTest, JpegCodec) {
auto bytes = ImageCodec::Encode(Pixmap(RGBAInfo, pixels), EncodedFormat::JPEG, 20);
auto codec = ImageCodec::MakeFrom(bytes);
ASSERT_TRUE(codec != nullptr);
EXPECT_EQ(codec->width(), 4032);
EXPECT_EQ(codec->height(), 3024);
EXPECT_EQ(codec->width(), 110);
EXPECT_EQ(codec->height(), 110);
EXPECT_EQ(codec->orientation(), Orientation::TopLeft);
buffer.clear();
EXPECT_TRUE(codec->readPixels(RGBAInfo, pixels));
Expand Down Expand Up @@ -435,10 +439,10 @@ TGFX_TEST(ReadPixelsTest, JpegCodec) {
}

TGFX_TEST(ReadPixelsTest, NativeCodec) {
auto rgbaCodec = MakeNativeCodec("resources/apitest/test_timestretch.png");
auto rgbaCodec = MakeNativeCodec("resources/apitest/imageReplacement.png");
ASSERT_TRUE(rgbaCodec != nullptr);
EXPECT_EQ(rgbaCodec->width(), 1280);
EXPECT_EQ(rgbaCodec->height(), 720);
EXPECT_EQ(rgbaCodec->width(), 110);
EXPECT_EQ(rgbaCodec->height(), 110);
EXPECT_EQ(rgbaCodec->orientation(), Orientation::TopLeft);
auto RGBAInfo = ImageInfo::Make(rgbaCodec->width(), rgbaCodec->height(), ColorType::RGBA_8888,
AlphaType::Premultiplied);
Expand All @@ -450,8 +454,8 @@ TGFX_TEST(ReadPixelsTest, NativeCodec) {
auto bytes = ImageCodec::Encode(Pixmap(RGBAInfo, pixels), EncodedFormat::PNG, 100);
auto codec = ImageCodec::MakeNativeCodec(bytes);
ASSERT_TRUE(codec != nullptr);
ASSERT_EQ(codec->width(), 1280);
ASSERT_EQ(codec->height(), 720);
ASSERT_EQ(codec->width(), 110);
ASSERT_EQ(codec->height(), 110);
ASSERT_EQ(codec->orientation(), Orientation::TopLeft);
buffer.clear();
EXPECT_TRUE(codec->readPixels(RGBAInfo, pixels));
Expand Down
25 changes: 20 additions & 5 deletions test/src/utils/Baseline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <iostream>
#include <unordered_set>
#include "base/TGFXTest.h"
#include "core/utils/USE.h"
#include "nlohmann/json.hpp"
#include "tgfx/core/Data.h"
#include "tgfx/core/ImageCodec.h"
Expand All @@ -36,10 +37,14 @@ static const std::string BASELINE_ROOT = ProjectPath::Absolute("test/baseline/")
static const std::string BASELINE_VERSION_PATH = BASELINE_ROOT + "/version.json";
static const std::string CACHE_MD5_PATH = BASELINE_ROOT + "/.cache/md5.json";
static const std::string CACHE_VERSION_PATH = BASELINE_ROOT + "/.cache/version.json";
static const std::string GIT_HEAD_PATH = BASELINE_ROOT + "/.cache/HEAD";
#ifdef GENERATE_BASELINE_IMAGES
static const std::string OUT_ROOT = ProjectPath::Absolute("test/baseline-out/");
#else
static const std::string OUT_ROOT = ProjectPath::Absolute("test/out/");
#endif
static const std::string OUT_MD5_PATH = OUT_ROOT + "/md5.json";
static const std::string OUT_VERSION_PATH = OUT_ROOT + "/version.json";
static const std::string GIT_HEAD_PATH = BASELINE_ROOT + "/.cache/HEAD";

static nlohmann::json BaselineVersion = {};
static nlohmann::json CacheVersion = {};
Expand Down Expand Up @@ -172,15 +177,15 @@ bool Baseline::Compare(const Pixmap& pixmap, const std::string& key) {
}
md5 = DumpMD5(newPixmap.pixels(), newPixmap.byteSize());
}
#ifdef GENERATE_BASELINE_IMAGES
SaveImage(pixmap, key + "_base");
#endif
return CompareVersionAndMd5(md5, key, [key, pixmap](bool result) {
if (result) {
RemoveImage(key);
} else {
SaveImage(pixmap, key);
}
#ifdef GENERATOR_BASELINE_IMAGES
SaveImage(pixmap, key + "_base");
#endif
});
}

Expand Down Expand Up @@ -238,14 +243,24 @@ static void CreateFolder(const std::string& path) {
void Baseline::TearDown() {
#ifdef UPDATE_BASELINE
if (!TGFXTest::HasFailure()) {
#ifdef GENERATE_BASELINE_IMAGES
auto outPath = ProjectPath::Absolute("test/out/");
std::filesystem::remove_all(outPath);
if (std::filesystem::exists(OUT_ROOT)) {
std::filesystem::rename(OUT_ROOT, outPath);
}
#endif
CreateFolder(CACHE_MD5_PATH);
std::ofstream outMD5File(CACHE_MD5_PATH);
outMD5File << std::setw(4) << OutputMD5 << std::endl;
outMD5File.close();
CreateFolder(CACHE_VERSION_PATH);
std::filesystem::copy(BASELINE_VERSION_PATH, CACHE_VERSION_PATH,
std::filesystem::copy_options::overwrite_existing);
} else {
std::filesystem::remove_all(OUT_ROOT);
}
USE(RemoveEmptyFolder);
#else
std::filesystem::remove(OUT_MD5_PATH);
if (!OutputMD5.empty()) {
Expand All @@ -258,7 +273,7 @@ void Baseline::TearDown() {
std::ofstream versionFile(OUT_VERSION_PATH);
versionFile << std::setw(4) << OutputVersion << std::endl;
versionFile.close();
#endif
RemoveEmptyFolder(OUT_ROOT);
#endif
}
} // namespace tgfx
6 changes: 5 additions & 1 deletion test/src/utils/TestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
#include "tgfx/gpu/opengl/GLFunctions.h"

namespace tgfx {
static const std::string OUT_ROOT = ProjectPath::Absolute("test/out");
#ifdef GENERATE_BASELINE_IMAGES
static const std::string OUT_ROOT = ProjectPath::Absolute("test/baseline-out/");
#else
static const std::string OUT_ROOT = ProjectPath::Absolute("test/out/");
#endif
static const std::string WEBP_FILE_EXT = ".webp";

bool CreateGLTexture(Context* context, int width, int height, GLTextureInfo* texture) {
Expand Down
2 changes: 1 addition & 1 deletion update_baseline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
echo $CMAKE_COMMAND

if [[ $1 == "1" ]]; then
$CMAKE_COMMAND -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage -g -O0" -DTGFX_USE_SWIFTSHADER=ON -DTGFX_BUILD_TESTS=ON -DTGFX_SKIP_BASELINE_CHECK=ON -DCMAKE_BUILD_TYPE=Debug ../
$CMAKE_COMMAND -DCMAKE_CXX_FLAGS="-fprofile-arcs -ftest-coverage -g -O0" -DTGFX_USE_SWIFTSHADER=ON -DTGFX_SKIP_GENERATE_BASELINE_IMAGES=ON -DTGFX_BUILD_TESTS=ON -DTGFX_SKIP_BASELINE_CHECK=ON -DCMAKE_BUILD_TYPE=Debug ../
else
$CMAKE_COMMAND -DTGFX_BUILD_TESTS=ON -DTGFX_SKIP_BASELINE_CHECK=ON -DCMAKE_BUILD_TYPE=Debug ../
fi
Expand Down

0 comments on commit 398adb9

Please sign in to comment.