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.

76 lines
2.8 KiB

9 years ago
9 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. # Setting the ctypes return type references for the foreign functions
  34. # returns a pointer to the library that we'll need to pass to all function calls
  35. lib.ext_init.restype = c_void_p
  36. # Returns a longlong hash, takes a pointer and a string
  37. lib.ext_get_ahash.restype = c_ulonglong
  38. lib.ext_get_ahash.argtypes = [c_void_p, c_char_p]
  39. lib.ext_get_dhash.restype = c_ulonglong
  40. lib.ext_get_dhash.argtypes = [c_void_p, c_char_p]
  41. lib.ext_get_phash.restype = c_ulonglong
  42. lib.ext_get_phash.argtypes = [c_void_p, c_char_p]
  43. # Takes a pointer and frees the struct at that memory location
  44. lib.ext_free.argtypes = [c_void_p]
  45. #initialize the library
  46. lib_struct = lib.ext_init("./.hash_cache".encode(encoding="utf-8"))
  47. #print("Pointer to lib_struct: ", lib_struct)
  48. # Print the path and the bytes as hex for debugging
  49. #print(large_image_path)
  50. #print('\\x'+'\\x'.join('{:02x}'.format(x) for x in large_image_path))
  51. for image in test_images:
  52. print("Requesting hashes for: %s"% image)
  53. print("ahash: %i"% unsigned64(lib.ext_get_ahash(lib_struct, image)))
  54. print("dhash: %i"% unsigned64(lib.ext_get_dhash(lib_struct, image)))
  55. print("phash: %i"% unsigned64(lib.ext_get_phash(lib_struct, image)))
  56. # Do cleanup
  57. # Makes sure that the heap is cleaned up
  58. lib.ext_free(lib_struct)
  59. print("ffi test finished")