4
4
#include " _image_resample.h"
5
5
#include " py_converters_11.h"
6
6
7
+ namespace py = pybind11;
8
+ using namespace pybind11 ::literals;
7
9
8
10
/* *********************************************************************
9
11
* Free functions
@@ -46,8 +48,8 @@ radius: float, default: 1
46
48
)""" ;
47
49
48
50
49
- static pybind11 ::array_t <double >
50
- _get_transform_mesh (const pybind11 ::object& transform, const pybind11 ::ssize_t *dims)
51
+ static py ::array_t <double >
52
+ _get_transform_mesh (const py ::object& transform, const py ::ssize_t *dims)
51
53
{
52
54
/* TODO: Could we get away with float, rather than double, arrays here? */
53
55
@@ -58,8 +60,8 @@ _get_transform_mesh(const pybind11::object& transform, const pybind11::ssize_t *
58
60
// If attribute doesn't exist, raises Python AttributeError
59
61
auto inverse = transform.attr (" inverted" )();
60
62
61
- pybind11 ::ssize_t mesh_dims[2 ] = {dims[0 ]*dims[1 ], 2 };
62
- pybind11 ::array_t <double > input_mesh (mesh_dims);
63
+ py ::ssize_t mesh_dims[2 ] = {dims[0 ]*dims[1 ], 2 };
64
+ py ::array_t <double > input_mesh (mesh_dims);
63
65
auto p = input_mesh.mutable_data ();
64
66
65
67
for (auto y = 0 ; y < dims[0 ]; ++y) {
@@ -72,7 +74,7 @@ _get_transform_mesh(const pybind11::object& transform, const pybind11::ssize_t *
72
74
auto output_mesh = inverse.attr (" transform" )(input_mesh);
73
75
74
76
auto output_mesh_array =
75
- pybind11 ::array_t <double , pybind11 ::array::c_style | pybind11 ::array::forcecast>(output_mesh);
77
+ py ::array_t <double , py ::array::c_style | py ::array::forcecast>(output_mesh);
76
78
77
79
if (output_mesh_array.ndim () != 2 ) {
78
80
throw std::runtime_error (
@@ -84,12 +86,12 @@ _get_transform_mesh(const pybind11::object& transform, const pybind11::ssize_t *
84
86
}
85
87
86
88
87
- // Using generic pybind ::array for input and output arrays rather than the more usual
88
- // pybind ::array_t<type> as function supports multiple array dtypes.
89
+ // Using generic py ::array for input and output arrays rather than the more usual
90
+ // py ::array_t<type> as this function supports multiple array dtypes.
89
91
static void
90
- image_resample (pybind11 ::array input_array,
91
- pybind11 ::array& output_array,
92
- const pybind11 ::object& transform,
92
+ image_resample (py ::array input_array,
93
+ py ::array& output_array,
94
+ const py ::object& transform,
93
95
interpolation_e interpolation,
94
96
bool resample_, // Avoid name clash with resample() function
95
97
float alpha,
@@ -111,7 +113,7 @@ image_resample(pybind11::array input_array,
111
113
}
112
114
113
115
// Ensure input array is contiguous, regardless of dtype
114
- input_array = pybind11 ::array::ensure (input_array, pybind11 ::array::c_style);
116
+ input_array = py ::array::ensure (input_array, py ::array::c_style);
115
117
116
118
// Validate output array
117
119
auto out_ndim = output_array.ndim ();
@@ -132,7 +134,7 @@ image_resample(pybind11::array input_array,
132
134
throw std::invalid_argument (" Input and output arrays have mismatched types" );
133
135
}
134
136
135
- if ((output_array.flags () & pybind11 ::array::c_style) == 0 ) {
137
+ if ((output_array.flags () & py ::array::c_style) == 0 ) {
136
138
throw std::invalid_argument (" Output array must be C-contiguous" );
137
139
}
138
140
@@ -150,14 +152,14 @@ image_resample(pybind11::array input_array,
150
152
151
153
// Only used if transform is not affine.
152
154
// Need to keep it in scope for the duration of this function.
153
- pybind11 ::array_t <double > transform_mesh;
155
+ py ::array_t <double > transform_mesh;
154
156
155
157
// Validate transform
156
158
if (transform.is_none ()) {
157
159
params.is_affine = true ;
158
160
} else {
159
161
// Raises Python AttributeError if no such attribute or TypeError if cast fails
160
- bool is_affine = pybind11 ::cast<bool >(transform.attr (" is_affine" ));
162
+ bool is_affine = py ::cast<bool >(transform.attr (" is_affine" ));
161
163
162
164
if (is_affine) {
163
165
convert_trans_affine (transform, params.affine );
@@ -171,20 +173,20 @@ image_resample(pybind11::array input_array,
171
173
172
174
if (auto resampler =
173
175
(ndim == 2 ) ? (
174
- (dtype.is (pybind11 ::dtype::of<std::uint8_t >())) ? resample<agg::gray8> :
175
- (dtype.is (pybind11 ::dtype::of<std::int8_t >())) ? resample<agg::gray8> :
176
- (dtype.is (pybind11 ::dtype::of<std::uint16_t >())) ? resample<agg::gray16> :
177
- (dtype.is (pybind11 ::dtype::of<std::int16_t >())) ? resample<agg::gray16> :
178
- (dtype.is (pybind11 ::dtype::of<float >())) ? resample<agg::gray32> :
179
- (dtype.is (pybind11 ::dtype::of<double >())) ? resample<agg::gray64> :
176
+ (dtype.is (py ::dtype::of<std::uint8_t >())) ? resample<agg::gray8> :
177
+ (dtype.is (py ::dtype::of<std::int8_t >())) ? resample<agg::gray8> :
178
+ (dtype.is (py ::dtype::of<std::uint16_t >())) ? resample<agg::gray16> :
179
+ (dtype.is (py ::dtype::of<std::int16_t >())) ? resample<agg::gray16> :
180
+ (dtype.is (py ::dtype::of<float >())) ? resample<agg::gray32> :
181
+ (dtype.is (py ::dtype::of<double >())) ? resample<agg::gray64> :
180
182
nullptr ) : (
181
183
// ndim == 3
182
- (dtype.is (pybind11 ::dtype::of<std::uint8_t >())) ? resample<agg::rgba8> :
183
- (dtype.is (pybind11 ::dtype::of<std::int8_t >())) ? resample<agg::rgba8> :
184
- (dtype.is (pybind11 ::dtype::of<std::uint16_t >())) ? resample<agg::rgba16> :
185
- (dtype.is (pybind11 ::dtype::of<std::int16_t >())) ? resample<agg::rgba16> :
186
- (dtype.is (pybind11 ::dtype::of<float >())) ? resample<agg::rgba32> :
187
- (dtype.is (pybind11 ::dtype::of<double >())) ? resample<agg::rgba64> :
184
+ (dtype.is (py ::dtype::of<std::uint8_t >())) ? resample<agg::rgba8> :
185
+ (dtype.is (py ::dtype::of<std::int8_t >())) ? resample<agg::rgba8> :
186
+ (dtype.is (py ::dtype::of<std::uint16_t >())) ? resample<agg::rgba16> :
187
+ (dtype.is (py ::dtype::of<std::int16_t >())) ? resample<agg::rgba16> :
188
+ (dtype.is (py ::dtype::of<float >())) ? resample<agg::rgba32> :
189
+ (dtype.is (py ::dtype::of<double >())) ? resample<agg::rgba64> :
188
190
nullptr )) {
189
191
Py_BEGIN_ALLOW_THREADS
190
192
resampler (
@@ -199,7 +201,7 @@ image_resample(pybind11::array input_array,
199
201
200
202
201
203
PYBIND11_MODULE (_image, m) {
202
- pybind11 ::enum_<interpolation_e>(m, " _InterpolationType" )
204
+ py ::enum_<interpolation_e>(m, " _InterpolationType" )
203
205
.value (" NEAREST" , NEAREST)
204
206
.value (" BILINEAR" , BILINEAR)
205
207
.value (" BICUBIC" , BICUBIC)
@@ -220,13 +222,13 @@ PYBIND11_MODULE(_image, m) {
220
222
.export_values ();
221
223
222
224
m.def (" resample" , &image_resample,
223
- pybind11::arg ( " input_array" ) ,
224
- pybind11::arg ( " output_array" ) ,
225
- pybind11::arg ( " transform" ) ,
226
- pybind11::arg ( " interpolation" ) = interpolation_e::NEAREST,
227
- pybind11::arg ( " resample" ) = false ,
228
- pybind11::arg ( " alpha" ) = 1 ,
229
- pybind11::arg ( " norm" ) = false ,
230
- pybind11::arg ( " radius" ) = 1 ,
225
+ " input_array" _a ,
226
+ " output_array" _a ,
227
+ " transform" _a ,
228
+ " interpolation" _a = interpolation_e::NEAREST,
229
+ " resample" _a = false ,
230
+ " alpha" _a = 1 ,
231
+ " norm" _a = false ,
232
+ " radius" _a = 1 ,
231
233
image_resample__doc__);
232
234
}
0 commit comments