Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/skia/Surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ py::class_<SkSurfaceProps> surfaceprops(m, "SurfaceProps", R"docstring(
)docstring");

py::enum_<SkSurfaceProps::Flags>(surfaceprops, "Flags", py::arithmetic())
.value("kDefault_Flag",
SkSurfaceProps::Flags::kDefault_Flag)
.value("kUseDeviceIndependentFonts_Flag",
SkSurfaceProps::Flags::kUseDeviceIndependentFonts_Flag)
.value("kDynamicMSAA_Flag",
Expand Down Expand Up @@ -246,6 +248,24 @@ surface
See :py:meth:`~MakeRasterN32Premul`
)docstring",
py::arg("width"), py::arg("height"), py::arg("surfaceProps") = nullptr)
.def(py::init(
[] (int width, int height, SkColorType colortype, const SkSurfaceProps* surfaceProps) {
auto info = SkImageInfo::MakeN32Premul(width, height).makeColorType(colortype);
return SkSurfaces::Raster(info, surfaceProps);
}),
R"docstring(
See :py:meth:`~MakeRasterN32Premul` plus :py:class:`ImageInfo`'s :py:meth:`~makeColorType`
)docstring",
py::arg("width"), py::arg("height"), py::arg("colortype"), py::arg("surfaceProps") = nullptr)
.def(py::init(
[] (const SkImageInfo& imageinfo, size_t rowBytes, const SkSurfaceProps* surfaceProps) {
return SkSurfaces::Raster(imageinfo, rowBytes, surfaceProps);
}),
R"docstring(
See :py:meth:`~MakeRaster`
)docstring",
py::arg("imageInfo"), py::arg("rowBytes") = 0,
py::arg("surfaceProps") = nullptr)
.def(py::init(
[] (py::array array, SkColorType ct, SkAlphaType at,
const SkColorSpace* cs, const SkSurfaceProps *surfaceProps) {
Expand Down Expand Up @@ -1010,6 +1030,15 @@ surface
Internally, sets :py:class:`ImageInfo` to width, height, native color
type, and :py:attr:`AlphaType.kPremul`.

Note that the :py:class:`ImageInfo` native color type might not be what
you desire, if you intent to interact with other software expecting
specific color type order. e.g. package `wx`'s `wx.Bitmap.FromBufferRGBA`.
For that purpose, you need one of the other :py:class:`Surface`
constructors which takes an explicit appropriate matching
:py:class:`ImageInfo` input, or one that takes an explicit :py:class:`ColorType`
input. You should check the output of `surface.imageInfo().colorType()`
afterwards, in that usage.

:py:class:`Surface` is returned if width and height are greater than
zero.

Expand Down
17 changes: 17 additions & 0 deletions tests/test_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,29 @@ def check_surface(x):

@pytest.mark.parametrize('args', [
(240, 320),
(240, 320, skia.SurfaceProps()),
(240, 320, skia.ColorType.kRGBA_8888_ColorType),
(240, 320, skia.ColorType.kBGRA_8888_ColorType, skia.SurfaceProps()),
(skia.ImageInfo.MakeN32Premul(240, 320),),
(skia.ImageInfo.MakeN32Premul(240, 320).makeColorType(skia.ColorType.kRGBA_8888_ColorType),),
(skia.ImageInfo.MakeN32Premul(240, 320).makeColorType(skia.ColorType.kBGRA_8888_ColorType), 0),
(skia.ImageInfo.MakeN32Premul(240, 320).makeColorType(skia.ColorType.kBGRA_8888_ColorType), 0, skia.SurfaceProps()),
(skia.ImageInfo.MakeN32Premul(240, 320), 0),
(skia.ImageInfo.MakeN32Premul(240, 320), 0, skia.SurfaceProps()),
(np.zeros((240, 320, 4), dtype=np.uint8),),
])
def test_Surface_init(args):
check_surface(skia.Surface(*args))


@pytest.mark.parametrize('width, height, colortype', [
(240, 320, skia.ColorType.kRGBA_8888_ColorType),
(240, 320, skia.ColorType.kBGRA_8888_ColorType),
])
def test_Surface_color_init(width, height, colortype):
assert skia.Surface(width, height, colortype).imageInfo().colorType() == colortype


def test_Surface_enter_exit(surface):
with surface as canvas:
assert isinstance(canvas, skia.Canvas)
Expand Down
Loading