Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
matplotlib/src/_image_wrapper.cpp at fedora-patches · fedora-python/matplotlib · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
fedora-python
/
matplotlib
Public
forked from
matplotlib/matplotlib
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
6
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
fedora-patches
Breadcrumbs
matplotlib
/
src
/
_image_wrapper.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
235 lines (193 loc) · 8.2 KB
fedora-patches
Breadcrumbs
matplotlib
/
src
/
_image_wrapper.cpp
Copy path
Top
File metadata and controls
Code
Blame
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__);
}
You can’t perform that action at this time.