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.

101 lines
3.5 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 image1PathStr[] = u8"../test_images/sample_01_";
  18. static const char image2PathStr[] = u8"../test_images/sample_02_";
  19. static const char image3PathStr[] = u8"../test_images/sample_03_";
  20. // Array of pointers to the base image paths to test
  21. //static const char *imagesSet[] = { image1PathStr, image2PathStr, image3PathStr };
  22. static const char *imagesSet[] = { image1PathStr, image2PathStr, image3PathStr };
  23. static const int imageSetSize = 3;
  24. // designators for the image sizes
  25. static const char largeImageSizeStr[] = u8"large";
  26. static const char mediumImageSizeStr[] = u8"medium";
  27. static const char smallImageSizeStr[] = u8"small";
  28. // Array of pointers to the images sizes
  29. static const char *imageSizesSet[] = { largeImageSizeStr, mediumImageSizeStr, smallImageSizeStr };
  30. static int imageSizesSetSize = 3;
  31. // Image extension
  32. static const char imageExtensionStr[] = u8".jpg";
  33. // Loading the external library
  34. lib = dlopen("./libpihash.so", RTLD_LAZY);
  35. //Registering the external functions
  36. *(void **)(&init) = dlsym(lib,"init");
  37. *(void **)(&teardown) = dlsym(lib,"teardown");
  38. *(void **)(&get_ahash) = dlsym(lib,"ext_get_ahash");
  39. *(void **)(&get_dhash) = dlsym(lib,"ext_get_dhash");
  40. *(void **)(&get_phash) = dlsym(lib,"ext_get_phash");
  41. // Init the shared library
  42. init();
  43. //temp buffer for the path
  44. char *imagePathBuffer = malloc(100);
  45. // loop over the images and sizes to test
  46. for (int i = 0; i < imageSetSize; i++) {
  47. for (int j = 0; j < imageSizesSetSize; j++) {
  48. // Make sure the buffer is clean before using it
  49. memset(imagePathBuffer,0,100);
  50. // Getting the correct path
  51. strcat(imagePathBuffer, imagesSet[i]);
  52. strcat(imagePathBuffer, imageSizesSet[j]);
  53. strcat(imagePathBuffer, imageExtensionStr);
  54. //printf("Path: %s\n", imagePath);
  55. // Visually proving that the bytes stored are the correct representation
  56. //print_ustr_bytes(imagePath);
  57. printf("Image: %s\n",imagePathBuffer);
  58. // Printing information about the hashes of the provided images
  59. uint64_t imageAhash = get_ahash(imagePathBuffer);
  60. uint64_t imageDhash = get_dhash(imagePathBuffer);
  61. uint64_t imagePhash = get_phash(imagePathBuffer);
  62. printf("ahash: %llu \n", imageAhash);
  63. printf("dhash: %llu \n", imageDhash);
  64. printf("phash: %llu \n", imagePhash);
  65. }
  66. }
  67. //cleanup and close the buffer
  68. memset(imagePathBuffer,0,100);
  69. free(imagePathBuffer);
  70. // Closing the shared library reference
  71. if (lib != NULL ) dlclose(lib);
  72. return EXIT_SUCCESS;
  73. }
  74. void print_ustr_bytes(const char str[]) {
  75. int strLen = strlen(str);
  76. //printf("Length: %u \n",strLen*2);
  77. char *strBuf = malloc(strLen*4);
  78. for(int i = 0; i <= strLen; i++) {
  79. sprintf(&strBuf[i*4], "\\x%02X", str[i]);
  80. }
  81. printf("String: '%s' -> Bytes: '%s'\n" , str, strBuf);
  82. memset(strBuf,0,strLen*4);
  83. free(strBuf);
  84. }