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.

98 lines
3.6 KiB

8 years ago
8 years ago
  1. #!/usr/bin/env python3
  2. from ctypes import *
  3. from sys import platform, exit
  4. large_image1_path = "../test_images/sample_01_large.jpg".encode(encoding="utf-8")
  5. medium_image1_path = "../test_images/sample_01_medium.jpg".encode(encoding="utf-8")
  6. small_image1_path = "../test_images/sample_01_small.jpg".encode(encoding="utf-8")
  7. large_image2_path = "../test_images/sample_02_large.jpg".encode(encoding="utf-8")
  8. medium_image2_path = "../test_images/sample_02_medium.jpg".encode(encoding="utf-8")
  9. small_image2_path = "../test_images/sample_02_small.jpg".encode(encoding="utf-8")
  10. large_image3_path = "../test_images/sample_03_large.jpg".encode(encoding="utf-8")
  11. medium_image3_path = "../test_images/sample_03_medium.jpg".encode(encoding="utf-8")
  12. small_image3_path = "../test_images/sample_03_small.jpg".encode(encoding="utf-8")
  13. large_image4_path = "../test_images/sample_04_large.jpg".encode(encoding="utf-8")
  14. medium_image4_path = "../test_images/sample_04_medium.jpg".encode(encoding="utf-8")
  15. small_image4_path = "../test_images/sample_04_small.jpg".encode(encoding="utf-8")
  16. test_images = [
  17. large_image1_path, medium_image1_path, small_image1_path,
  18. large_image2_path, medium_image2_path, small_image2_path,
  19. large_image3_path, medium_image3_path, small_image3_path,
  20. large_image4_path, medium_image4_path, small_image4_path
  21. ]
  22. def unsigned64(number):
  23. return (number & 0xfffffffffffffffff)
  24. print("starting ffi test")
  25. # Load the shared library
  26. lib = None
  27. if platform == 'win32':
  28. lib = cdll.pihash
  29. elif platform == 'darwin':
  30. lib = cdll.LoadLibrary("libpihash.dylib")
  31. elif platform == 'linux' or platform == 'linux2':
  32. lib = cdll.LoadLibrary("libpihash.so")
  33. else:
  34. print('Unrecognized platform ', platform)
  35. exit(-1)
  36. class PIHashes(Structure):
  37. _fields_ = [
  38. ("ahash", c_ulonglong),
  39. ("dhash", c_ulonglong),
  40. ("phash", c_ulonglong)]
  41. # Setting the ctypes return type references for the foreign functions
  42. # returns a pointer to the library that we'll need to pass to all function calls
  43. lib.ext_init.restype = c_void_p
  44. lib.ext_init.argtypes = [c_char_p]
  45. # Returns a longlong hash, takes a pointer and a string
  46. lib.ext_get_ahash.restype = c_ulonglong
  47. lib.ext_get_ahash.argtypes = [c_void_p, c_char_p]
  48. lib.ext_get_dhash.restype = c_ulonglong
  49. lib.ext_get_dhash.argtypes = [c_void_p, c_char_p]
  50. lib.ext_get_phash.restype = c_ulonglong
  51. lib.ext_get_phash.argtypes = [c_void_p, c_char_p]
  52. lib.ext_get_pihashes.restype = c_void_p
  53. lib.ext_get_pihashes.argtypes = [c_void_p, c_char_p]
  54. lib.ext_free_pihashes.argtypes = [c_void_p]
  55. # Takes a pointer and frees the struct at that memory location
  56. lib.ext_free.argtypes = [c_void_p]
  57. # initialize the library
  58. lib_struct = lib.ext_init("./.hash_cache".encode('utf-8'))
  59. # print("Pointer to lib_struct: ", lib_struct)
  60. # Print the path and the bytes as hex for debugging
  61. # print(large_image_path)
  62. # print('\\x'+'\\x'.join('{:02x}'.format(x) for x in large_image_path))
  63. for image in test_images:
  64. print("Requesting hashes for: %s" % image)
  65. phashes = lib.ext_get_pihashes(lib_struct, image)
  66. pihashes = PIHashes.from_address(phashes)
  67. lib.ext_free_pihashes(phashes)
  68. print("ahash: %i" % unsigned64(pihashes.ahash))
  69. print("dhash: %i" % unsigned64(pihashes.dhash))
  70. print("phash: %i" % unsigned64(pihashes.phash))
  71. # print("ahash: %i"% unsigned64(lib.ext_get_ahash(lib_struct, image)))
  72. # print("dhash: %i"% unsigned64(lib.ext_get_dhash(lib_struct, image)))
  73. # print("phash: %i"% unsigned64(lib.ext_get_phash(lib_struct, image)))
  74. # Do cleanup
  75. # Makes sure that the heap is cleaned up
  76. lib.ext_free(lib_struct)
  77. print("ffi test finished")