diff options
Diffstat (limited to 'meowpp.test/inc/test_utility.h')
-rw-r--r-- | meowpp.test/inc/test_utility.h | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/meowpp.test/inc/test_utility.h b/meowpp.test/inc/test_utility.h new file mode 100644 index 0000000..8875445 --- /dev/null +++ b/meowpp.test/inc/test_utility.h @@ -0,0 +1,103 @@ +#ifndef TEST_UTILITY_H__ +#define TEST_UTILITY_H__ + +#include <opencv/cv.h> +#include <opencv/highgui.h> + +#include <cstdarg> +#include <queue> + +extern "C" { +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <dirent.h> +} + +#include "meowpp/colors/RGB_Space.h" +#include "meowpp/gra/Bitmap.h" +#include "meowpp/utility.h" + +#include <cstdio> +#include <string> + +template<class T> +inline bool readBitmap(std::string const& name, meow::Bitmap<T>* bmp) { + cv::Mat img = cv::imread(name, CV_LOAD_IMAGE_COLOR); + if (!img.data) + return false; + size_t height = img.size().height; + size_t width = img.size().width ; + bmp->size(height, width, T(0)); + for (size_t y = 0; y < height; ++y) + for (size_t x = 0; x < width; ++x) { + meow::RGBi_Space tmp; + for (size_t i = 0; i < 3; ++i) + tmp.rgb(i, img.at<cv::Vec3b>(y, x)[2 - i]); + T p; + meow::colorTransformate(tmp, &p); + bmp->pixel(y, x, p); + } + return true; +} + +template<class T> +inline bool writeBitmap(std::string const& name, meow::Bitmap<T> const& bmp) { + size_t height = bmp.height(); + size_t width = bmp.width (); + cv::Mat img(height, width, CV_8UC3); + for (size_t y = 0; y < height; y++) + for (size_t x = 0; x < width; x++) { + meow::RGBi_Space tmp; + meow::colorTransformate(bmp.pixel(y, x), &tmp); + for (size_t i = 0; i < 3; ++i) + img.at<cv::Vec3b>(y, x)[i] = tmp.rgb(2 - i); + } + return imwrite(name, img); +} + +inline std::vector<std::string> cgetFiles(char const* dn, bool recur, int num, ...) { + va_list args; + va_start(args, num); + std::vector<char const*> filter(num); + for (int i = 0; i < num; ++i) + filter[i] = va_arg(args, char const*); + + std::string name(dn); + if (dn[0] != '\0' && dn[strlen(dn) - 1] != '/') + name = name + "/"; + + std::queue<std::string> dirs; + std::vector<std::string> ret; + struct stat st; + + for (dirs.push(name); !dirs.empty(); dirs.pop()) { + DIR* dir = opendir(dirs.front().c_str()); + if (dir == NULL) + continue; + for (dirent* ent; (ent = readdir(dir)) != NULL; ) { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) + continue; + std::string fname(dirs.front() + ent->d_name); + if (stat(fname.c_str(), &st) < 0) + continue; + if (S_ISDIR(st.st_mode) && recur) { + dirs.push(fname + "/"); + continue; + } + if (S_ISREG(st.st_mode)) { + bool ok = false; + for (int i = 0; i < num && !ok; ++i) + ok = meow::cstringEndWith(fname.c_str(), 1, filter[i]); + if (ok) + ret.push_back(fname); + } + } + closedir(dir); + } + + va_end(args); + return ret; +} + +#endif // TEST_UTILITY_H__ |