FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
matplotlib/src/_image_wrapper.cpp at main · commit-0/matplotlib · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
commit-0
/
matplotlib
Public
forked from
matplotlib/matplotlib
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
matplotlib
/
src
/
_image_wrapper.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
235 lines (193 loc) · 8.2 KB
Breadcrumbs
matplotlib
/
src
/
_image_wrapper.cpp
Copy path
File metadata and controls
235 lines (193 loc) · 8.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#
include
<
pybind11/pybind11.h
>
#
include
<
pybind11/numpy.h
>
#
include
"
_image_resample.h
"
#
include
"
py_converters.h
"
namespace
py
=
pybind11;
using
namespace
pybind11
::literals
;
/*
*********************************************************************
* Free functions
*
*/
const
char
* image_resample__doc__ =
R"""(
Resample input_array, blending it in-place into output_array, using an affine transform.
Parameters
----------
input_array : 2-d or 3-d NumPy array of float, double or `numpy.uint8`
If 2-d, the image is grayscale. If 3-d, the image must be of size 4 in the last
dimension and represents RGBA data.
output_array : 2-d or 3-d NumPy array of float, double or `numpy.uint8`
The dtype and number of dimensions must match `input_array`.
transform : matplotlib.transforms.Transform instance
The transformation from the input array to the output array.
interpolation : int, default: NEAREST
The interpolation method. Must be one of the following constants defined in this
module:
NEAREST, BILINEAR, BICUBIC, SPLINE16, SPLINE36, HANNING, HAMMING, HERMITE, KAISER,
QUADRIC, CATROM, GAUSSIAN, BESSEL, MITCHELL, SINC, LANCZOS, BLACKMAN
resample : bool, optional
When `True`, use a full resampling method. When `False`, only resample when the
output image is larger than the input image.
alpha : float, default: 1
The transparency level, from 0 (transparent) to 1 (opaque).
norm : bool, default: False
Whether to norm the interpolation function.
radius: float, default: 1
The radius of the kernel, if method is SINC, LANCZOS or BLACKMAN.
)"""
;
static
py::
array_t
<
double
>
_get_transform_mesh
(
const
py::object& transform,
const
py::
ssize_t
*dims)
{
/*
TODO: Could we get away with float, rather than double, arrays here?
*/
/*
Given a non-affine transform object, create a mesh that maps
every pixel in the output image to the input image. This is used
as a lookup table during the actual resampling.
*/
//
If attribute doesn't exist, raises Python AttributeError
auto
inverse = transform.
attr
(
"
inverted
"
)();
py::
ssize_t
mesh_dims[
2
] = {dims[
0
]*dims[
1
],
2
};
py::
array_t
<
double
>
input_mesh
(mesh_dims);
auto
p = input_mesh.
mutable_data
();
for
(
auto
y =
0
; y < dims[
0
]; ++y) {
for
(
auto
x =
0
; x < dims[
1
]; ++x) {
*p++ = (
double
)x;
*p++ = (
double
)y;
}
}
auto
output_mesh = inverse.
attr
(
"
transform
"
)(input_mesh);
auto
output_mesh_array =
py::
array_t
<
double
, py::array::c_style | py::array::forcecast>(output_mesh);
if
(output_mesh_array.
ndim
() !=
2
) {
throw
std::runtime_error
(
"
Inverse transformed mesh array should be 2D not {}D
"
_s.
format
(
output_mesh_array.
ndim
()));
}
return
output_mesh_array;
}
//
Using generic py::array for input and output arrays rather than the more usual
//
py::array_t<type> as this function supports multiple array dtypes.
static
void
image_resample
(py::array input_array,
py::array& output_array,
const
py::object& transform,
interpolation_e interpolation,
bool
resample_,
//
Avoid name clash with resample() function
float
alpha,
bool
norm,
float
radius)
{
//
Validate input_array
auto
dtype = input_array.
dtype
();
//
Validated when determine resampler below
auto
ndim = input_array.
ndim
();
if
(ndim !=
2
&& ndim !=
3
) {
throw
std::invalid_argument
(
"
Input array must be a 2D or 3D array
"
);
}
if
(ndim ==
3
&& input_array.
shape
(
2
) !=
4
) {
throw
std::invalid_argument
(
"
3D input array must be RGBA with shape (M, N, 4), has trailing dimension of {}
"
_s.
format
(
input_array.
shape
(
2
)));
}
//
Ensure input array is contiguous, regardless of dtype
input_array =
py::array::ensure
(input_array, py::array::c_style);
//
Validate output array
auto
out_ndim = output_array.
ndim
();
if
(out_ndim != ndim) {
throw
std::invalid_argument
(
"
Input ({}D) and output ({}D) arrays have different dimensionalities
"
_s.
format
(
ndim, out_ndim));
}
if
(out_ndim ==
3
&& output_array.
shape
(
2
) !=
4
) {
throw
std::invalid_argument
(
"
3D output array must be RGBA with shape (M, N, 4), has trailing dimension of {}
"
_s.
format
(
output_array.
shape
(
2
)));
}
if
(!output_array.
dtype
().
is
(dtype)) {
throw
std::invalid_argument
(
"
Input and output arrays have mismatched types
"
);
}
if
((output_array.
flags
() & py::array::c_style) ==
0
) {
throw
std::invalid_argument
(
"
Output array must be C-contiguous
"
);
}
if
(!output_array.
writeable
()) {
throw
std::invalid_argument
(
"
Output array must be writeable
"
);
}
resample_params_t
params;
params.
interpolation
= interpolation;
params.
transform_mesh
=
nullptr
;
params.
resample
= resample_;
params.
norm
= norm;
params.
radius
= radius;
params.
alpha
= alpha;
//
Only used if transform is not affine.
//
Need to keep it in scope for the duration of this function.
py::
array_t
<
double
> transform_mesh;
//
Validate transform
if
(transform.
is_none
()) {
params.
is_affine
=
true
;
}
else
{
//
Raises Python AttributeError if no such attribute or TypeError if cast fails
bool
is_affine = py::cast<
bool
>(transform.
attr
(
"
is_affine
"
));
if
(is_affine) {
convert_trans_affine
(transform, params.
affine
);
params.
is_affine
=
true
;
}
else
{
transform_mesh =
_get_transform_mesh
(transform, output_array.
shape
());
params.
transform_mesh
= transform_mesh.
data
();
params.
is_affine
=
false
;
}
}
if
(
auto
resampler =
(ndim ==
2
) ? (
(dtype.
equal
(py::dtype::of<std::
uint8_t
>())) ? resample<agg::gray8> :
(dtype.
equal
(py::dtype::of<std::
int8_t
>())) ? resample<agg::gray8> :
(dtype.
equal
(py::dtype::of<std::
uint16_t
>())) ? resample<agg::gray16> :
(dtype.
equal
(py::dtype::of<std::
int16_t
>())) ? resample<agg::gray16> :
(dtype.
equal
(py::dtype::of<
float
>())) ? resample<agg::gray32> :
(dtype.
equal
(py::dtype::of<
double
>())) ? resample<agg::gray64> :
nullptr
) : (
//
ndim == 3
(dtype.
equal
(py::dtype::of<std::
uint8_t
>())) ? resample<agg::rgba8> :
(dtype.
equal
(py::dtype::of<std::
int8_t
>())) ? resample<agg::rgba8> :
(dtype.
equal
(py::dtype::of<std::
uint16_t
>())) ? resample<agg::rgba16> :
(dtype.
equal
(py::dtype::of<std::
int16_t
>())) ? resample<agg::rgba16> :
(dtype.
equal
(py::dtype::of<
float
>())) ? resample<agg::rgba32> :
(dtype.
equal
(py::dtype::of<
double
>())) ? resample<agg::rgba64> :
nullptr
)) {
Py_BEGIN_ALLOW_THREADS
resampler
(
input_array.
data
(), input_array.
shape
(
1
), input_array.
shape
(
0
),
output_array.
mutable_data
(), output_array.
shape
(
1
), output_array.
shape
(
0
),
params);
Py_END_ALLOW_THREADS
}
else
{
throw
std::invalid_argument
(
"
arrays must be of dtype byte, short, float32 or float64
"
);
}
}
PYBIND11_MODULE
(_image, m, py::mod_gil_not_used())
{
py::enum_<interpolation_e>(m,
"
_InterpolationType
"
)
.
value
(
"
NEAREST
"
,
NEAREST
)
.
value
(
"
BILINEAR
"
,
BILINEAR
)
.
value
(
"
BICUBIC
"
,
BICUBIC
)
.
value
(
"
SPLINE16
"
,
SPLINE16
)
.
value
(
"
SPLINE36
"
,
SPLINE36
)
.
value
(
"
HANNING
"
,
HANNING
)
.
value
(
"
HAMMING
"
,
HAMMING
)
.
value
(
"
HERMITE
"
,
HERMITE
)
.
value
(
"
KAISER
"
,
KAISER
)
.
value
(
"
QUADRIC
"
,
QUADRIC
)
.
value
(
"
CATROM
"
,
CATROM
)
.
value
(
"
GAUSSIAN
"
,
GAUSSIAN
)
.
value
(
"
BESSEL
"
,
BESSEL
)
.
value
(
"
MITCHELL
"
,
MITCHELL
)
.
value
(
"
SINC
"
,
SINC
)
.
value
(
"
LANCZOS
"
,
LANCZOS
)
.
value
(
"
BLACKMAN
"
,
BLACKMAN
)
.
export_values
();
m.
def
(
"
resample
"
, &image_resample,
"
input_array
"
_a,
"
output_array
"
_a,
"
transform
"
_a,
"
interpolation
"
_a = interpolation_e::
NEAREST
,
"
resample
"
_a =
false
,
"
alpha
"
_a =
1
,
"
norm
"
_a =
false
,
"
radius
"
_a =
1
,
image_resample__doc__);
}
Back
|
FazBrowse Home
|
New Git URL