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.

92 lines
3.3 KiB

8 years ago
8 years ago
  1. #!/usr/bin/env python3
  2. from ctypes import *
  3. import os
  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 os.name == 'nt':
  28. path = os.path.dirname(__file__) + "\\pihash.dll"
  29. print(path)
  30. lib = CDLL(path)
  31. else:
  32. lib = cdll.LoadLibrary("libpihash.so")
  33. class PIHashes(Structure):
  34. _fields_ = [
  35. ("ahash", c_ulonglong),
  36. ("dhash", c_ulonglong),
  37. ("phash", c_ulonglong)]
  38. # Setting the ctypes return type references for the foreign functions
  39. # returns a pointer to the library that we'll need to pass to all function calls
  40. lib.ext_init.restype = c_void_p
  41. # Returns a longlong hash, takes a pointer and a string
  42. lib.ext_get_ahash.restype = c_ulonglong
  43. lib.ext_get_ahash.argtypes = [c_void_p, c_char_p]
  44. lib.ext_get_dhash.restype = c_ulonglong
  45. lib.ext_get_dhash.argtypes = [c_void_p, c_char_p]
  46. lib.ext_get_phash.restype = c_ulonglong
  47. lib.ext_get_phash.argtypes = [c_void_p, c_char_p]
  48. lib.ext_get_phashes.restype = c_void_p
  49. lib.ext_get_phashes.argtypes = [c_void_p, c_char_p]
  50. lib.ext_free_phashes.argtypes = [c_void_p]
  51. # Takes a pointer and frees the struct at that memory location
  52. lib.ext_free.argtypes = [c_void_p]
  53. #initialize the library
  54. lib_struct = lib.ext_init("./.hash_cache".encode(encoding="utf-8"))
  55. #print("Pointer to lib_struct: ", lib_struct)
  56. # Print the path and the bytes as hex for debugging
  57. #print(large_image_path)
  58. #print('\\x'+'\\x'.join('{:02x}'.format(x) for x in large_image_path))
  59. for image in test_images:
  60. print("Requesting hashes for: %s"% image)
  61. phashes = lib.ext_get_phashes(lib_struct, image)
  62. pihashes = PIHashes.from_address(phashes)
  63. lib.ext_free_phashes(phashes)
  64. print("ahash: %i"% unsigned64(pihashes.ahash))
  65. print("dhash: %i"% unsigned64(pihashes.dhash))
  66. print("phash: %i"% unsigned64(pihashes.phash))
  67. # print("ahash: %i"% unsigned64(lib.ext_get_ahash(lib_struct, image)))
  68. # print("dhash: %i"% unsigned64(lib.ext_get_dhash(lib_struct, image)))
  69. # print("phash: %i"% unsigned64(lib.ext_get_phash(lib_struct, image)))
  70. # Do cleanup
  71. # Makes sure that the heap is cleaned up
  72. lib.ext_free(lib_struct)
  73. print("ffi test finished")