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.

110 lines
3.9 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. //temp buffer for the path
  51. char *imagePathBuffer = malloc(100);
  52. // loop over the images and sizes to test
  53. for (int i = 0; i < imageSetSize; i++) {
  54. for (int j = 0; j < imageSizesSetSize; j++) {
  55. // Make sure the buffer is clean before using it
  56. memset(imagePathBuffer,0,100);
  57. // Getting the correct path
  58. strcat(imagePathBuffer, imagesSet[i]);
  59. strcat(imagePathBuffer, imageSizesSet[j]);
  60. strcat(imagePathBuffer, imageExtensionStr);
  61. //printf("Path: %s\n", imagePath);
  62. // Visually proving that the bytes stored are the correct representation
  63. //print_ustr_bytes(imagePath);
  64. printf("Image: %s\n",imagePathBuffer);
  65. // Printing information about the hashes of the provided images
  66. uint64_t imageAhash = get_ahash(imagePathBuffer);
  67. uint64_t imageDhash = get_dhash(imagePathBuffer);
  68. uint64_t imagePhash = get_phash(imagePathBuffer);
  69. printf("ahash: %llu \n", imageAhash);
  70. printf("dhash: %llu \n", imageDhash);
  71. printf("phash: %llu \n", imagePhash);
  72. }
  73. }
  74. //cleanup and close the buffer
  75. memset(imagePathBuffer,0,100);
  76. free(imagePathBuffer);
  77. // Closing the shared library reference
  78. if (lib != NULL ) dlclose(lib);
  79. return EXIT_SUCCESS;
  80. }
  81. void print_ustr_bytes(const char str[]) {
  82. int strLen = strlen(str);
  83. //printf("Length: %u \n",strLen*2);
  84. char *strBuf = malloc(strLen*4);
  85. for(int i = 0; i <= strLen; i++) {
  86. sprintf(&strBuf[i*4], "\\x%02X", str[i]);
  87. }
  88. printf("String: '%s' -> Bytes: '%s'\n" , str, strBuf);
  89. memset(strBuf,0,strLen*4);
  90. free(strBuf);
  91. }