From: Antonio Valentino <antonio.valentino@tiscali.it>
Date: Tue, 28 Jan 2025 07:25:47 +0000
Subject: Compatibility with numpy2

Forwarded: https://github.com/pearu/pylibtiff/pull/186
---
 libtiff/libtiff_ctypes.py | 16 ++++++++--------
 libtiff/test_bittools.py  |  4 +++-
 libtiff/tiff_image.py     |  2 +-
 libtiff/utils.py          |  1 +
 4 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/libtiff/libtiff_ctypes.py b/libtiff/libtiff_ctypes.py
index 5202747..c776846 100644
--- a/libtiff/libtiff_ctypes.py
+++ b/libtiff/libtiff_ctypes.py
@@ -659,13 +659,13 @@ class TIFF(ctypes.c_void_p):
         compression = self._fix_compression(compression)
 
         arr = np.ascontiguousarray(arr)
-        if arr.dtype in np.sctypes['float']:
+        if np.issubdtype(arr.dtype, np.floating):
             sample_format = SAMPLEFORMAT_IEEEFP
-        elif arr.dtype in np.sctypes['uint'] + [np.bool_]:
+        elif np.issubdtype(arr.dtype, np.unsignedinteger) or np.issubdtype(arr.dtype, np.bool_):
             sample_format = SAMPLEFORMAT_UINT
-        elif arr.dtype in np.sctypes['int']:
+        elif np.issubdtype(arr.dtype, np.signedinteger):
             sample_format = SAMPLEFORMAT_INT
-        elif arr.dtype in np.sctypes['complex']:
+        elif np.issubdtype(arr.dtype, np.complexfloating):
             sample_format = SAMPLEFORMAT_COMPLEXIEEEFP
         else:
             raise NotImplementedError(repr(arr.dtype))
@@ -746,13 +746,13 @@ class TIFF(ctypes.c_void_p):
                     compression=None, write_rgb=False):
         compression = self._fix_compression(compression)
 
-        if arr.dtype in np.sctypes['float']:
+        if np.issubdtype(arr.dtype, np.floating):
             sample_format = SAMPLEFORMAT_IEEEFP
-        elif arr.dtype in np.sctypes['uint'] + [np.bool_]:
+        elif np.issubdtype(arr.dtype, np.unsignedinteger) or np.issubdtype(arr.dtype, np.bool_):
             sample_format = SAMPLEFORMAT_UINT
-        elif arr.dtype in np.sctypes['int']:
+        elif np.issubdtype(arr.dtype, np.signedinteger):
             sample_format = SAMPLEFORMAT_INT
-        elif arr.dtype in np.sctypes['complex']:
+        elif np.issubdtype(arr.dtype, np.complexfloating):
             sample_format = SAMPLEFORMAT_COMPLEXIEEEFP
         else:
             raise NotImplementedError(repr(arr.dtype))
diff --git a/libtiff/test_bittools.py b/libtiff/test_bittools.py
index a173525..c6cba41 100644
--- a/libtiff/test_bittools.py
+++ b/libtiff/test_bittools.py
@@ -19,8 +19,10 @@ def test_setgetbit():
 
 
 def test_setgetword():
+    arange = numpy.arange(-256, 256)
     for dtype in [numpy.ubyte, numpy.int32, numpy.float64]:
-        arr = numpy.array(list(range(-256, 256)), dtype=dtype)
+        # arr = numpy.array(list(range(-256, 256)), dtype=dtype)
+        arr = arange.astype(dtype)
         arr2 = numpy.zeros(arr.shape, dtype=arr.dtype)
         for i in range(arr.nbytes):
             word, next = bittools.getword(arr, i * 8, 8)
diff --git a/libtiff/tiff_image.py b/libtiff/tiff_image.py
index 717370a..8e31d0e 100644
--- a/libtiff/tiff_image.py
+++ b/libtiff/tiff_image.py
@@ -437,7 +437,7 @@ class TIFFimage:
 
         if compressed_data_size != image_data_size:
             sdiff = image_data_size - compressed_data_size
-            total_size -= sdiff
+            total_size = int(total_size) - sdiff
             base = tif._mmap
             if base is None:
                 base = tif.base
diff --git a/libtiff/utils.py b/libtiff/utils.py
index 7fcfde4..d11d785 100644
--- a/libtiff/utils.py
+++ b/libtiff/utils.py
@@ -19,6 +19,7 @@ def isindisk(path):
 
 def bytes2str(bytes):
     lst = []
+    bytes = int(bytes)
     Pbytes = bytes // 1024**5
     if Pbytes:
         lst.append('%sPi' % (Pbytes))
