You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.8 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <dlfcn.h>
  5. #include <string.h>
  6. /* function declaration */
  7. void print_ustr_bytes(const char []);
  8. int main() {
  9. void *lib;
  10. // declaration for the external functions used
  11. void (*init)();
  12. void (*teardown)();
  13. uint64_t (*get_ahash)(const char *);
  14. uint64_t (*get_dhash)(const char *);
  15. uint64_t (*get_phash)(const char *);
  16. //test image locations
  17. static const char largePathStr[] = u8"test_images/sample_01_large.jpg";
  18. static const char mediumPathStr[] = u8"test_images/sample_01_medium.jpg";
  19. static const char smallPathStr[] = u8"test_images/sample_01_small.jpg";
  20. static const char image1PathStr[] = u8"test_images/sample_01_";
  21. static const char image2PathStr[] = u8"test_images/sample_02_";
  22. static const char image3PathStr[] = u8"test_images/sample_03_";
  23. // Array of pointers to the base image paths to test
  24. //static const char *imagesSet[] = { image1PathStr, image2PathStr, image3PathStr };
  25. static const char *imagesSet[] = { image1PathStr, image2PathStr, image3PathStr };
  26. static const int imageSetSize = 3;
  27. // designators for the image sizes
  28. static const char largeImageSizeStr[] = u8"large";
  29. static const char mediumImageSizeStr[] = u8"medium";
  30. static const char smallImageSizeStr[] = u8"small";
  31. // Array of pointers to the images sizes
  32. static const char *imageSizesSet[] = { largeImageSizeStr, mediumImageSizeStr, smallImageSizeStr };
  33. static int imageSizesSetSize = 3;
  34. // Image extension
  35. static const char imageExtensionStr[] = u8".jpg";
  36. // Pointers that the external function call need
  37. const char *largePathPtr = &largePathStr[0];
  38. const char *mediumPathPtr = &mediumPathStr[0];
  39. const char *smallPathPtr = &smallPathStr[0];
  40. // Loading the external library
  41. lib = dlopen("./libpihash.so", RTLD_LAZY);
  42. //Registering the external functions
  43. *(void **)(&init) = dlsym(lib,"init");
  44. *(void **)(&teardown) = dlsym(lib,"teardown");
  45. *(void **)(&get_ahash) = dlsym(lib,"ext_get_ahash");
  46. *(void **)(&get_dhash) = dlsym(lib,"ext_get_dhash");
  47. *(void **)(&get_phash) = dlsym(lib,"ext_get_phash");
  48. // Init the shared library
  49. init();
  50. // loop over the images and sizes to test
  51. for (int i = 0; i < imageSetSize; i++) {
  52. for (int j = 0; j < imageSizesSetSize; j++) {
  53. char *imagePath = malloc(100);
  54. // Getting the correct path
  55. strcat(imagePath, imagesSet[i]);
  56. strcat(imagePath, imageSizesSet[j]);
  57. strcat(imagePath, imageExtensionStr);
  58. //printf("Path: %s\n", imagePath);
  59. // Visually proving that the bytes stored are the correct representation
  60. //print_ustr_bytes(imagePath);
  61. printf("Image: %s\n",imagePath);
  62. // Printing information about the hashes of the provided images
  63. uint64_t imageAhash = get_ahash(imagePath);
  64. uint64_t imageDhash = get_dhash(imagePath);
  65. uint64_t imagePhash = get_phash(imagePath);
  66. printf("ahash: %llu \n", imageAhash);
  67. printf("dhash: %llu \n", imageDhash);
  68. printf("phash: %llu \n", imagePhash);
  69. //cleanup
  70. memset(imagePath,0,100);
  71. free(imagePath);
  72. }
  73. }
  74. // Closing the shared library reference
  75. if (lib != NULL ) dlclose(lib);
  76. return EXIT_SUCCESS;
  77. }
  78. void print_ustr_bytes(const char str[]) {
  79. int strLen = strlen(str);
  80. //printf("Length: %u \n",strLen*2);
  81. char *strBuf = malloc(strLen*4);
  82. for(int i = 0; i <= strLen; i++) {
  83. sprintf(&strBuf[i*4], "\\x%02X", str[i]);
  84. }
  85. printf("String: '%s' -> Bytes: '%s'\n" , str, strBuf);
  86. memset(strBuf,0,strLen*4);
  87. free(strBuf);
  88. }