Skip to content

Commit

Permalink
Add kernel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gin committed Nov 20, 2023
1 parent 32ba77a commit 473baee
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 7 deletions.
162 changes: 160 additions & 2 deletions SerialPrograms/Source/Tests/Kernels_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,46 @@


#include "Common/Compiler.h"
#include "Common/Cpp/Color.h"
#include "Common/Cpp/CpuId/CpuId.h"
#include "Common/Cpp/Time.h"
#include "CommonFramework/ImageTypes/BinaryImage.h"
#include "CommonFramework/ImageTools/ImageBoxes.h"
#include "CommonFramework/ImageTypes/ImageRGB32.h"
#include "CommonFramework/ImageTools/ImageFilter.h"
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
#include "Kernels/BinaryMatrix/Kernels_BinaryMatrix.h"
#include "Kernels/BinaryImageFilters/Kernels_BinaryImage_BasicFilters.h"
#include "Kernels/ImageScaleBrightness/Kernels_ImageScaleBrightness.h"
#include "Kernels_Tests.h"

#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
using std::flush;

namespace PokemonAutomation{

using namespace Kernels;

// using namespace NintendoSwitch::PokemonLA;
namespace Kernels{

std::unique_ptr<PackedBinaryMatrix_IB> make_PackedBinaryMatrix_64x4_Default(size_t width, size_t height);

void compress_rgb32_to_binary_range_64x4_Default(
const uint32_t* image, size_t bytes_per_row,
PackedBinaryMatrix_IB& matrix0, uint32_t mins0, uint32_t maxs0
);


}


int test_kernels_ImageScaleBrightness(const ImageViewRGB32& image){
ImageRGB32 new_image = image.copy();

int num_iterations = 5000;
int num_iterations = 500;
auto time_start = current_time();
for(int i = 0; i < num_iterations; i++){
scale_brightness(new_image.width(), new_image.height(), new_image.data(), new_image.bytes_per_row(), 1.2f, 1.3f, 0.5f);
Expand All @@ -40,4 +60,142 @@ int test_kernels_ImageScaleBrightness(const ImageViewRGB32& image){
return 0;
}


int test_kernels_BinaryMatrix(const ImageViewRGB32& image){
const size_t width = image.width();
const size_t height = image.height();

const uint32_t mins = combine_rgb(0, 0, 0);
// uint32_t maxs = combine_rgb(255, 255, 255);
const uint32_t maxs = combine_rgb(63, 63, 63);

bool have_error = false;

auto matrix_default = make_PackedBinaryMatrix_64x4_Default(width, height);

compress_rgb32_to_binary_range_64x4_Default(
image.data(), image.bytes_per_row(), *matrix_default, mins, maxs
);

const size_t num_iter = 3000;

{
auto time_start = current_time();
for(size_t i = 0; i < num_iter; i++){
compress_rgb32_to_binary_range_64x4_Default(
image.data(), image.bytes_per_row(), *matrix_default, mins, maxs
);
}
auto time_end = current_time();
const auto ms = std::chrono::duration_cast<Milliseconds>(time_end - time_start).count();
cout << "Default impl. time: " << ms << " ms" << endl;
}

// cout << matrix_default->dump() << flush;

cout << "Testing current binary matrix construction from image" << endl;
auto matrix_type = get_BinaryMatrixType();
auto matrix_current = make_PackedBinaryMatrix(matrix_type, width, height);

compress_rgb32_to_binary_range(
image.data(), image.bytes_per_row(), *matrix_current, mins, maxs
);

for (size_t r = 0; r < height; r++){
for (size_t c = 0; c < width; c++){
const bool v_default = matrix_default->get(c, r);
const bool v_m1 = matrix_current->get(c, r);
if (v_default != v_m1){
cout << "Error: matrix (" << c << ", " << r << ") not same: default: "
<< v_default << ", M1: " << v_m1 << endl;
have_error = true;
}
}
}
if (have_error){
return 1;
}

{
auto time_start = current_time();
for(size_t i = 0; i < num_iter; i++){
compress_rgb32_to_binary_range(
image.data(), image.bytes_per_row(), *matrix_current, mins, maxs
);
}
auto time_end = current_time();
const auto ms = std::chrono::duration_cast<Milliseconds>(time_end - time_start).count();
cout << "Cur impl. time: " << ms << " ms" << endl;
}

return 0;
}

int test_kernels_FilterRGB32(const ImageViewRGB32& image){

const size_t width = image.width();
const size_t height = image.height();

const uint32_t mins = combine_rgb(0, 0, 0);
// uint32_t maxs = combine_rgb(255, 255, 255);
const uint32_t maxs = combine_rgb(63, 63, 63);

bool have_error = false;

const bool replace_color_within_range = true;
auto new_image = filter_rgb32_range(image, mins, maxs, COLOR_WHITE, replace_color_within_range);

for (size_t r = 0; r < height; r++){
for (size_t c = 0; c < width; c++){
const Color color(image.pixel(c, r));
const Color new_color(new_image.pixel(c, r));
bool in_range = (color.red() <= 63 && color.green() <= 63 && color.blue() <= 63);
if (in_range && uint32_t(new_color) != uint32_t(COLOR_WHITE)){
cout << "Error: wrong filter result: old color " << color.to_string() << ", (x,y) = "
<< c << ", " << r << endl;
have_error = true;
return 1;
}
}
}

// const size_t num_iter = 3000;
// auto time_start = current_time();
// for(size_t i = 0; i < num_iter; i++){
// filter_rgb32(
// matrix, new_image.data(), new_image.bytes_per_row(), uint32_t(COLOR_WHITE), replace_if_zero
// );
// }
// auto time_end = current_time();
// const auto ms = std::chrono::duration_cast<Milliseconds>(time_end - time_start).count();
// cout << "Filter time: " << ms << " ms" << endl;



if (have_error){
return 1;
}

return 0;
}

int test_kernels_Waterfill(const ImageViewRGB32& image){

ImagePixelBox box(0, 0, image.width(), image.height());
ImageViewRGB32 sub_image = extract_box_reference(image, box);

PackedBinaryMatrix matrix(sub_image.width(), sub_image.height());
uint32_t mins = combine_rgb(0, 0, 0);
// uint32_t maxs = combine_rgb(255, 255, 255);
uint32_t maxs = combine_rgb(63, 63, 63);
Kernels::compress_rgb32_to_binary_range(
sub_image.data(), sub_image.bytes_per_row(),
matrix, mins, maxs
);

cout << matrix.dump() << flush;

return 0;
}

}
8 changes: 8 additions & 0 deletions SerialPrograms/Source/Tests/Kernels_Tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class ImageViewRGB32;

int test_kernels_ImageScaleBrightness(const ImageViewRGB32& image);

int test_kernels_BinaryMatrix(const ImageViewRGB32& image);

int test_kernels_FilterRGB32(const ImageViewRGB32& image);


int test_kernels_Waterfill(const ImageViewRGB32& image);


}

#endif
4 changes: 3 additions & 1 deletion SerialPrograms/Source/Tests/PokemonLA_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,10 +449,12 @@ int test_pokemonLA_MapMissionTabReader(const ImageViewRGB32& image, bool target)
return 0;
}

void test_pokemonLA_BerryTreeDetector(const ImageViewRGB32& image){
int test_pokemonLA_BerryTreeDetector(const ImageViewRGB32& image){
BerryTreeDetector detector;

detector.process_frame(image, current_time());

return 0;
}

int test_pokemonLA_SaveScreenDetector(const ImageViewRGB32& image, const std::vector<std::string>& keywords){
Expand Down
2 changes: 1 addition & 1 deletion SerialPrograms/Source/Tests/PokemonLA_Tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int test_pokemonLA_BattleSpriteArrowDetector(const ImageViewRGB32& image, int ta

int test_pokemonLA_MapMissionTabReader(const ImageViewRGB32& image, bool target);

void test_pokemonLA_BerryTreeDetector(const ImageViewRGB32& image);
int test_pokemonLA_BerryTreeDetector(const ImageViewRGB32& image);

int test_pokemonLA_shinySoundDetector(const std::vector<AudioSpectrum>& spectrums, bool target);

Expand Down
8 changes: 5 additions & 3 deletions SerialPrograms/Source/Tests/TestMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ using ImageIntDetectorFunction = std::function<int(const ImageViewRGB32& image,

using ImageWordsDetectorFunction = std::function<int(const ImageViewRGB32& image, const std::vector<std::string>& words)>;

using ImageVoidDetectorFunction = std::function<void(const ImageViewRGB32& image)>;
using ImageVoidDetectorFunction = std::function<int(const ImageViewRGB32& image)>;

using SoundBoolDetectorFunction = std::function<int(const std::vector<AudioSpectrum>& spectrums, bool target)>;

Expand Down Expand Up @@ -159,8 +159,7 @@ int image_int_detector_helper(ImageIntDetectorFunction test_func, const std::str
// debugging output. So no need to get target values from the test framework.
int image_void_detector_helper(ImageVoidDetectorFunction test_func, const std::string& test_path){
auto run_test = [&](const ImageViewRGB32& image, const std::string&) -> int{
test_func(image);
return 0;
return test_func(image);
};

return image_filename_detector_helper(run_test, test_path);
Expand Down Expand Up @@ -221,6 +220,9 @@ int sound_bool_detector_helper(SoundBoolDetectorFunction test_func, const std::s

const std::map<std::string, TestFunction> TEST_MAP = {
{"Kernels_ImageScaleBrightness", std::bind(image_void_detector_helper, test_kernels_ImageScaleBrightness, _1)},
{"Kernels_BinaryMatrix", std::bind(image_void_detector_helper, test_kernels_BinaryMatrix, _1)},
{"Kernels_FilterRGB32", std::bind(image_void_detector_helper, test_kernels_FilterRGB32, _1)},
{"Kernels_Waterfill", std::bind(image_void_detector_helper, test_kernels_Waterfill, _1)},
{"CommonFramework_BlackBorderDetector", std::bind(image_bool_detector_helper, test_CommonFramework_BlackBorderDetector, _1)},
{"NintendoSwitch_UpdateMenuDetector", std::bind(image_bool_detector_helper, test_NintendoSwitch_UpdateMenuDetector, _1)},
{"PokemonSwSh_YCommMenuDetector", std::bind(image_bool_detector_helper, test_pokemonSwSh_YCommMenuDetector, _1)},
Expand Down

0 comments on commit 473baee

Please sign in to comment.