FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
skia-python/src/skia/utils.cpp at main · skia-python/skia-python · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
skia-python
/
skia-python
Public
Notifications
You must be signed in to change notification settings
Fork
52
Star
322
Code
Issues
44
Pull requests
6
Discussions
Actions
Projects
Security and quality
1
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
skia-python
/
src
/
skia
/
utils.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
366 lines (316 loc) · 14 KB
Breadcrumbs
skia-python
/
src
/
skia
/
utils.cpp
Copy path
File metadata and controls
366 lines (316 loc) · 14 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#
include
"
common.h
"
#
include
<
include/encode/SkPngEncoder.h
>
#
include
<
pybind11/numpy.h
>
template
<>
sk_sp<SkShader>
CloneFlattenable
(
const
SkShader& shader) {
auto
data = shader.
serialize
();
auto
flat =
SkShader::Deserialize
(
shader.
getFlattenableType
(), data->
data
(), data->
size
());
return
sk_sp<SkShader>(
reinterpret_cast
<SkShader*>(flat.
release
()));
}
template
<>
sk_sp<SkColorFilter>
CloneFlattenable
(
const
SkColorFilter& colorFilter) {
auto
data = colorFilter.
serialize
();
auto
flat = colorFilter.
Deserialize
(data->
data
(), data->
size
());
return
sk_sp<SkColorFilter>(
reinterpret_cast
<SkColorFilter*>(flat.
release
()));
}
sk_sp<SkColorSpace>
CloneColorSpace
(
const
SkColorSpace* cs) {
return
(cs) ?
CloneFlattenable
(*cs) : sk_sp<SkColorSpace>(
nullptr
);
}
sk_sp<SkImage>
CloneImage
(
const
SkImage& image) {
SkPixmap pixmap;
if
(image.
peekPixels
(&pixmap))
return
SkImages::RasterFromPixmapCopy
(pixmap);
return
SkImages::DeferredFromEncodedData
(
SkPngEncoder::Encode
(
nullptr
, &image, {}));
}
size_t
ValidateBufferToImageInfo
(
const
SkImageInfo& imageInfo,
const
py::buffer_info& buffer,
size_t
rowBytes) {
if
(buffer.
ndim
==
0
)
throw
py::value_error
(
"
Buffer does not have dimensions.
"
);
if
(buffer.
ndim
==
1
&& rowBytes ==
0
)
rowBytes = imageInfo.
minRowBytes
();
else
if
(buffer.
ndim
>
1
)
rowBytes = buffer.
strides
[
0
];
if
(!imageInfo.
validRowBytes
(rowBytes))
throw
py::value_error
(
py::str
(
"
Row bytes is smaller than required (expected {}, given {})
"
).
format
(imageInfo.
minRowBytes
(), rowBytes));
size_t
byteSize = buffer.
strides
[
0
] * buffer.
shape
[
0
];
if
(byteSize < imageInfo.
computeByteSize
(rowBytes))
throw
py::value_error
(
py::str
(
"
Byte size is smaller than required (expected {}, given {})
"
).
format
(imageInfo.
computeByteSize
(rowBytes), byteSize));
return
rowBytes;
}
SkImageInfo
NumPyToImageInfo
(py::array array, SkColorType ct, SkAlphaType at,
const
SkColorSpace* cs) {
if
(!(array.
flags
() & py::array::c_style))
throw
py::value_error
(
"
Array must be C-style contiguous.
"
);
if
(array.
ndim
() <=
1
)
throw
py::value_error
(
"
Number of dimensions must be 2 or more.
"
);
if
(array.
shape
(
0
) ==
0
|| array.
shape
(
1
) ==
0
)
throw
py::value_error
(
py::str
(
"
Width and height must be greater than 0.
"
"
(width={}, height={})
"
).
format
(array.
shape
(
1
), array.
shape
(
0
)));
//
TODO: automatic colortype inference.
auto
imageInfo =
SkImageInfo::Make
(
array.
shape
(
1
), array.
shape
(
0
), ct, at,
CloneColorSpace
(cs));
auto
pixelSize = (array.
ndim
() ==
2
) ?
array.
strides
(
1
) : array.
strides
(
2
) * array.
shape
(
2
);
if
(pixelSize != imageInfo.
bytesPerPixel
())
throw
py::value_error
(
py::str
(
"
Incorrect number of color channels (expected {} bytes per pixel,
"
"
given {} bytes per pixel).
"
).
format
(
imageInfo.
bytesPerPixel
(), pixelSize));
return
imageInfo;
}
py::buffer_info
ImageInfoToBufferInfo
(
const
SkImageInfo& imageInfo,
void
* data, py::
ssize_t
rowBytes,
bool
readonly) {
py::
ssize_t
width = imageInfo.
width
();
py::
ssize_t
height = imageInfo.
height
();
py::
ssize_t
bytesPerPixel = imageInfo.
bytesPerPixel
();
if
(!rowBytes)
rowBytes = imageInfo.
minRowBytes
();
switch
(imageInfo.
colorType
()) {
case
kAlpha_8_SkColorType
:
case
kGray_8_SkColorType
:
return
py::buffer_info
(
data, bytesPerPixel,
"
B
"
,
2
, { height, width },
{ rowBytes, bytesPerPixel }, readonly);
case
kRGB_565_SkColorType
:
case
kARGB_4444_SkColorType
:
return
py::buffer_info
(
data, bytesPerPixel,
"
H
"
,
2
, { height, width },
{ rowBytes, bytesPerPixel }, readonly);
case
kRGBA_8888_SkColorType
:
case
kRGB_888x_SkColorType
:
case
kBGRA_8888_SkColorType
:
return
py::buffer_info
(
data,
1
,
"
B
"
,
3
, { height, width,
py::ssize_t
(
4
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
1
) }, readonly);
case
kRGBA_1010102_SkColorType
:
case
kBGRA_1010102_SkColorType
:
case
kRGB_101010x_SkColorType
:
case
kBGR_101010x_SkColorType
:
return
py::buffer_info
(
data, bytesPerPixel,
"
I
"
,
2
, { height, width },
{ rowBytes, bytesPerPixel }, readonly);
case
kRGBA_F16Norm_SkColorType
:
case
kRGBA_F16_SkColorType
:
return
py::buffer_info
(
data,
2
,
"
e
"
,
3
, { height, width,
py::ssize_t
(
4
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
2
) }, readonly);
case
kRGBA_F32_SkColorType
:
return
py::buffer_info
(
data,
4
,
"
f
"
,
3
, { height, width,
py::ssize_t
(
4
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
4
) }, readonly);
case
kR8G8_unorm_SkColorType
:
return
py::buffer_info
(
data,
1
,
"
B
"
,
3
, { height, width,
py::ssize_t
(
2
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
1
) }, readonly);
case
kA16_float_SkColorType
:
return
py::buffer_info
(
data,
2
,
"
e
"
,
2
, { height, width },
{ rowBytes, bytesPerPixel }, readonly);
case
kR16G16_float_SkColorType
:
return
py::buffer_info
(
data,
2
,
"
e
"
,
3
, { height, width,
py::ssize_t
(
2
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
2
) }, readonly);
case
kA16_unorm_SkColorType
:
return
py::buffer_info
(
data,
2
,
"
<H
"
,
2
, { height, width },
{ rowBytes, bytesPerPixel }, readonly);
case
kR16G16_unorm_SkColorType
:
return
py::buffer_info
(
data,
2
,
"
<H
"
,
3
, { height, width,
py::ssize_t
(
2
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
2
) }, readonly);
case
kR16G16B16A16_unorm_SkColorType
:
return
py::buffer_info
(
data,
2
,
"
<H
"
,
3
, { height, width,
py::ssize_t
(
4
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
2
) }, readonly);
case
kUnknown_SkColorType
:
default
:
throw
std::runtime_error
(
"
Unsupported color type
"
);
}
}
py::memoryview
ImageInfoToMemoryView
(
const
SkImageInfo& imageInfo,
void
* data, py::
ssize_t
rowBytes,
bool
readonly) {
py::
ssize_t
width = imageInfo.
width
();
py::
ssize_t
height = imageInfo.
height
();
py::
ssize_t
bytesPerPixel = imageInfo.
bytesPerPixel
();
if
(!rowBytes)
rowBytes = imageInfo.
minRowBytes
();
switch
(imageInfo.
colorType
()) {
case
kAlpha_8_SkColorType
:
case
kGray_8_SkColorType
:
return
py::memoryview::from_buffer
(
data, bytesPerPixel,
"
B
"
, { height, width },
{ rowBytes, bytesPerPixel }, readonly);
case
kRGB_565_SkColorType
:
case
kARGB_4444_SkColorType
:
return
py::memoryview::from_buffer
(
data, bytesPerPixel,
"
H
"
, { height, width },
{ rowBytes, bytesPerPixel }, readonly);
case
kRGBA_8888_SkColorType
:
case
kRGB_888x_SkColorType
:
case
kBGRA_8888_SkColorType
:
return
py::memoryview::from_buffer
(
data,
1
,
"
B
"
, { height, width,
py::ssize_t
(
4
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
1
) }, readonly);
case
kRGBA_1010102_SkColorType
:
case
kBGRA_1010102_SkColorType
:
case
kRGB_101010x_SkColorType
:
case
kBGR_101010x_SkColorType
:
return
py::memoryview::from_buffer
(
data, bytesPerPixel,
"
I
"
, { height, width },
{ rowBytes, bytesPerPixel }, readonly);
case
kRGBA_F16Norm_SkColorType
:
case
kRGBA_F16_SkColorType
:
return
py::memoryview::from_buffer
(
data,
2
,
"
e
"
, { height, width,
py::ssize_t
(
4
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
2
) }, readonly);
case
kRGBA_F32_SkColorType
:
return
py::memoryview::from_buffer
(
data,
4
,
"
f
"
, { height, width,
py::ssize_t
(
4
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
4
) }, readonly);
case
kR8G8_unorm_SkColorType
:
return
py::memoryview::from_buffer
(
data,
1
,
"
B
"
, { height, width,
py::ssize_t
(
2
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
1
) }, readonly);
case
kA16_float_SkColorType
:
return
py::memoryview::from_buffer
(
data,
2
,
"
e
"
, { height, width },
{ rowBytes, bytesPerPixel }, readonly);
case
kR16G16_float_SkColorType
:
return
py::memoryview::from_buffer
(
data,
2
,
"
e
"
, { height, width,
py::ssize_t
(
2
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
2
) }, readonly);
case
kA16_unorm_SkColorType
:
return
py::memoryview::from_buffer
(
data,
2
,
"
<H
"
, { height, width },
{ rowBytes, bytesPerPixel }, readonly);
case
kR16G16_unorm_SkColorType
:
return
py::memoryview::from_buffer
(
data,
2
,
"
<H
"
, { height, width,
py::ssize_t
(
2
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
2
) }, readonly);
case
kR16G16B16A16_unorm_SkColorType
:
return
py::memoryview::from_buffer
(
data,
2
,
"
<H
"
, { height, width,
py::ssize_t
(
4
) },
{ rowBytes, bytesPerPixel,
py::ssize_t
(
2
) }, readonly);
case
kUnknown_SkColorType
:
default
:
throw
std::runtime_error
(
"
Unsupported color type
"
);
}
}
py::dict
ImageInfoToArrayInterface
(
const
SkImageInfo& imageInfo,
size_t
rowBytes) {
using
namespace
pybind11
::literals
;
auto
byteorder =
py::module::import
(
"
sys
"
)
.
attr
(
"
byteorder
"
).
cast
<std::string>();
std::string
bytemark
((byteorder ==
"
little
"
) ?
"
<
"
:
"
>
"
);
int
width = imageInfo.
width
();
int
height = imageInfo.
height
();
int
bytesPerPixel = imageInfo.
bytesPerPixel
();
if
(!rowBytes)
rowBytes = imageInfo.
minRowBytes
();
switch
(imageInfo.
colorType
()) {
case
kAlpha_8_SkColorType
:
case
kGray_8_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width),
"
typestr
"
_a =
py::str
(
"
|u1
"
),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel),
"
version
"
_a =
3
);
case
kRGB_565_SkColorType
:
case
kARGB_4444_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width),
"
typestr
"
_a =
py::str
(
"
{}u2
"
).
format
(bytemark),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel),
"
version
"
_a =
3
);
case
kRGBA_8888_SkColorType
:
case
kRGB_888x_SkColorType
:
case
kBGRA_8888_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width,
4
),
"
typestr
"
_a =
py::str
(
"
|u1
"
),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel,
1
),
"
version
"
_a =
3
);
case
kRGBA_1010102_SkColorType
:
case
kBGRA_1010102_SkColorType
:
case
kRGB_101010x_SkColorType
:
case
kBGR_101010x_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width),
"
typestr
"
_a =
py::str
(
"
{}u4
"
).
format
(bytemark),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel),
"
version
"
_a =
3
);
case
kRGBA_F16Norm_SkColorType
:
case
kRGBA_F16_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width,
4
),
"
typestr
"
_a =
py::str
(
"
{}f2
"
).
format
(bytemark),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel,
2
),
"
version
"
_a =
3
);
case
kRGBA_F32_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width,
4
),
"
typestr
"
_a =
py::str
(
"
{}f4
"
).
format
(bytemark),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel,
4
),
"
version
"
_a =
3
);
case
kR8G8_unorm_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width,
2
),
"
typestr
"
_a =
py::str
(
"
|u1
"
),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel,
1
),
"
version
"
_a =
3
);
case
kA16_float_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width),
"
typestr
"
_a =
py::str
(
"
{}f2
"
).
format
(bytemark),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel),
"
version
"
_a =
3
);
case
kR16G16_float_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width,
2
),
"
typestr
"
_a =
py::str
(
"
{}f2
"
).
format
(bytemark),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel,
2
),
"
version
"
_a =
3
);
case
kA16_unorm_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width),
"
typestr
"
_a =
py::str
(
"
<u2
"
),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel),
"
version
"
_a =
3
);
case
kR16G16_unorm_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width,
2
),
"
typestr
"
_a =
py::str
(
"
<u2
"
),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel,
2
),
"
version
"
_a =
3
);
case
kR16G16B16A16_unorm_SkColorType
:
return
py::dict
(
"
shape
"
_a =
py::make_tuple
(height, width,
4
),
"
typestr
"
_a =
py::str
(
"
<f2
"
),
"
strides
"
_a =
py::make_tuple
(rowBytes, bytesPerPixel,
2
),
"
version
"
_a =
3
);
case
kUnknown_SkColorType
:
default
:
throw
std::runtime_error
(
"
Unsupported color type
"
);
}
}
Back
|
FazBrowse Home
|
New Git URL