From 1d56ddeca84eb16b307408648d6fb1391ed30f94 Mon Sep 17 00:00:00 2001 From: simon987 Date: Sat, 28 Dec 2019 10:10:15 -0500 Subject: [PATCH] API cleanup --- benchmark.cpp | 13 ++++++------- fastimagehash.cpp | 29 +++++++++++++++++------------ fastimagehash.h | 16 ++++++++-------- imhash.c | 2 +- 4 files changed, 32 insertions(+), 28 deletions(-) diff --git a/benchmark.cpp b/benchmark.cpp index d249155..dc61db4 100644 --- a/benchmark.cpp +++ b/benchmark.cpp @@ -24,7 +24,7 @@ static void BM_phash(benchmark::State &state) { void *buf = load_test_file(&size); for (auto _ : state) { - phash_mem(buf, tmp, size, state.range(), 4); + phash_mem(buf, size, tmp, state.range(), 4); } free(buf); @@ -35,7 +35,7 @@ static void BM_whash(benchmark::State &state) { void *buf = load_test_file(&size); for (auto _ : state) { - whash_mem(buf, tmp, size, state.range(), 0, "haar"); + whash_mem(buf, size, tmp, state.range(), 0, "haar"); } free(buf); @@ -46,7 +46,7 @@ static void BM_dhash(benchmark::State &state) { void *buf = load_test_file(&size); for (auto _ : state) { - dhash_mem(buf, tmp, size, state.range()); + dhash_mem(buf, size, tmp, state.range()); } free(buf); @@ -57,7 +57,7 @@ static void BM_ahash(benchmark::State &state) { void *buf = load_test_file(&size); for (auto _ : state) { - ahash_mem(buf, tmp, size, state.range()); + ahash_mem(buf, size, tmp, state.range()); } free(buf); @@ -68,7 +68,7 @@ static void BM_mhash(benchmark::State &state) { void *buf = load_test_file(&size); for (auto _ : state) { - mhash_mem(buf, tmp, size, state.range()); + mhash_mem(buf, size, tmp, state.range()); } free(buf); @@ -81,7 +81,7 @@ static void BM_multi(benchmark::State &state) { multi_hash_t *m = multi_hash_create(state.range()); for (auto _ : state) { - multi_hash_file(filepath, m, state.range(), 4, 0); + multi_hash_file(filepath, m, state.range(), 4, 0, "haar"); } multi_hash_destroy(m); @@ -89,7 +89,6 @@ static void BM_multi(benchmark::State &state) { free(buf); } - BENCHMARK(BM_phash)->ArgName("size")->Arg(8); BENCHMARK(BM_whash)->ArgName("size")->Arg(8); BENCHMARK(BM_dhash)->ArgName("size")->Arg(8); diff --git a/fastimagehash.cpp b/fastimagehash.cpp index 5d3760b..f4cdbad 100644 --- a/fastimagehash.cpp +++ b/fastimagehash.cpp @@ -108,12 +108,12 @@ int ahash_file(const char *filepath, uchar *out, int hash_size) { return FASTIMAGEHASH_READ_ERR; } - int ret = ahash_mem(buf, out, size, hash_size); + int ret = ahash_mem(buf, size, out, hash_size); free(buf); return ret; } -int ahash_mem(void *buf, uchar *out, size_t buf_len, int hash_size) { +int ahash_mem(void *buf, size_t buf_len, uchar *out, int hash_size) { Mat im; try { im = imdecode(Mat(1, buf_len, CV_8UC1, buf), IMREAD_GRAYSCALE); @@ -140,12 +140,12 @@ int mhash_file(const char *filepath, uchar *out, int hash_size) { return FASTIMAGEHASH_READ_ERR; } - int ret = mhash_mem(buf, out, size, hash_size); + int ret = mhash_mem(buf, size, out, hash_size); free(buf); return ret; } -int mhash_mem(void *buf, uchar *out, size_t buf_len, int hash_size) { +int mhash_mem(void *buf, size_t buf_len, uchar *out, int hash_size) { Mat im; try { im = imdecode(Mat(1, buf_len, CV_8UC1, buf), IMREAD_GRAYSCALE); @@ -175,12 +175,12 @@ int dhash_file(const char *filepath, uchar *out, int hash_size) { return FASTIMAGEHASH_READ_ERR; } - int ret = dhash_mem(buf, out, size, hash_size); + int ret = dhash_mem(buf, size, out, hash_size); free(buf); return ret; } -int dhash_mem(void *buf, uchar *out, size_t buf_len, int hash_size) { +int dhash_mem(void *buf, size_t buf_len, uchar *out, int hash_size) { Mat im; try { im = imdecode(Mat(1, buf_len, CV_8UC1, buf), IMREAD_GRAYSCALE); @@ -207,12 +207,12 @@ int whash_file(const char *filepath, uchar *out, int hash_size, int img_scale, c return FASTIMAGEHASH_READ_ERR; } - int ret = whash_mem(buf, out, size, hash_size, img_scale, wname); + int ret = whash_mem(buf, size, out, hash_size, img_scale, wname); free(buf); return ret; } -int whash_mem(void *buf, uchar *out, size_t buf_len, const int hash_size, int img_scale, const char *wname) { +int whash_mem(void *buf, size_t buf_len, uchar *out, const int hash_size, int img_scale, const char *wname) { Mat im; try { im = imdecode(Mat(1, buf_len, CV_8UC1, buf), IMREAD_GRAYSCALE); @@ -290,12 +290,12 @@ int phash_file(const char *filepath, uchar *out, const int hash_size, int highfr return FASTIMAGEHASH_READ_ERR; } - int ret = phash_mem(buf, out, size, hash_size, highfreq_factor); + int ret = phash_mem(buf, size, out, hash_size, highfreq_factor); free(buf); return ret; } -int phash_mem(void *buf, uchar *out, size_t buf_len, const int hash_size, int highfreq_factor) { +int phash_mem(void *buf, size_t buf_len, uchar *out, const int hash_size, int highfreq_factor) { int img_size = hash_size * highfreq_factor; Mat im; @@ -374,14 +374,19 @@ int multi_hash_file(const char *filepath, multi_hash_t *out, int hash_size, return FASTIMAGEHASH_READ_ERR; } - int ret = multi_hash_mem(buf, out, size, hash_size, ph_highfreq_factor, wh_img_scale, wname); + int ret = multi_hash_mem(buf, size, out, hash_size, ph_highfreq_factor, wh_img_scale, wname); free(buf); return ret; } -int multi_hash_mem(void *buf, multi_hash_t *out, size_t buf_len, +int multi_hash_mem(void *buf, size_t buf_len, multi_hash_t *out, int hash_size, int ph_highfreq_factor, int wh_img_scale, const char*wname) { + + if (strcmp(wname, "haar") != 0 && strcmp(wname, "db4") != 0) { + throw std::invalid_argument("wname must be either of 'haar' or 'db4'"); + } + Mat im; try { im = imdecode(Mat(1, buf_len, CV_8UC1, buf), IMREAD_GRAYSCALE); diff --git a/fastimagehash.h b/fastimagehash.h index 10df548..11d69b6 100644 --- a/fastimagehash.h +++ b/fastimagehash.h @@ -1,7 +1,7 @@ #ifndef FASTIMAGEHASH_FASTIMAGEHASH_H #define FASTIMAGEHASH_FASTIMAGEHASH_H -#define FASTIMAGEHASH_VERSION "2.0" +#define FASTIMAGEHASH_VERSION "3.0" #include @@ -26,31 +26,31 @@ void multi_hash_destroy(multi_hash_t *h); int multi_hash_file(const char *filepath, multi_hash_t *out, int hash_size, int ph_highfreq_factor, int wh_img_scale, const char*wname); -int multi_hash_mem(void *buf, multi_hash_t *out, size_t buf_len, int hash_size, int ph_highfreq_factor, int wh_img_scale, const char* wname); - void hash_to_hex_string_reversed(const uchar *h, char *out, int hash_size); void hash_to_hex_string(const uchar *h, char *out, int hash_size); -int mhash_mem(void *buf, uchar *out, size_t buf_len, int hash_size); +int multi_hash_mem(void *buf, size_t buf_len, multi_hash_t *out, int hash_size, int ph_highfreq_factor, int wh_img_scale, const char* wname); + +int mhash_mem(void *buf, size_t buf_len,uchar *out, int hash_size); int mhash_file(const char *filepath, uchar *out, int hash_size); -int ahash_mem(void *buf, uchar *out, size_t buf_len, int hash_size); +int ahash_mem(void *buf, size_t buf_len, uchar *out, int hash_size); int ahash_file(const char *filepath, uchar *out, int hash_size); int dhash_file(const char *filepath, uchar *out, int hash_size); -int dhash_mem(void *buf, uchar *out, size_t buf_len, int hash_size); +int dhash_mem(void *buf, size_t buf_len, uchar *out, int hash_size); int whash_file(const char *filepath, uchar *out, int hash_size, int img_scale, const char* wname); -int whash_mem(void *buf, uchar *out, size_t buf_len, int hash_size, int img_scale, const char*wname); +int whash_mem(void *buf, size_t buf_len, uchar *out, int hash_size, int img_scale, const char*wname); int phash_file(const char *buf, uchar *out, int hash_size, int highfreq_factor); -int phash_mem(void *buf, uchar *out, size_t buf_len, int hash_size, int highfreq_factor); +int phash_mem(void *buf, size_t buf_len, uchar *out, int hash_size, int highfreq_factor); #ifdef __cplusplus }; diff --git a/imhash.c b/imhash.c index cabb403..8f43de2 100644 --- a/imhash.c +++ b/imhash.c @@ -64,7 +64,7 @@ int main(int argc, char *argv[]) { } multi_hash_t *m = multi_hash_create(8); - multi_hash_file(argv[i], m, 8, 4, 0); + multi_hash_file(argv[i], m, 8, 4, 0, "haar"); hash_to_hex_string_reversed(m->phash, hashstr, 8); printf("%s\tmp:%s\n", argv[i], hashstr);