Skip to content

Commit

Permalink
#4110 fix 'flags' argument to 'PyMemoryView_FromMemory'
Browse files Browse the repository at this point in the history
  • Loading branch information
totaam committed Jan 30, 2024
1 parent 6ceb529 commit 6b0c177
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
3 changes: 0 additions & 3 deletions xpra/codecs/avif/decoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ from libc.stdlib cimport free
cdef extern from *:
ctypedef unsigned long size_t

cdef extern from "Python.h":
object PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)


AVIF_PIXEL_FORMAT = {
AVIF_PIXEL_FORMAT_NONE : "NONE",
Expand Down
5 changes: 3 additions & 2 deletions xpra/codecs/csc_cython/colorspace_converter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cdef extern from "Python.h":
int PyObject_GetBuffer(object obj, Py_buffer *view, int flags)
void PyBuffer_Release(Py_buffer *view)
int PyBUF_ANY_CONTIGUOUS
int PyBUF_WRITE

cdef extern from "stdlib.h":
void free(void *ptr)
Expand Down Expand Up @@ -625,7 +626,7 @@ cdef class ColorspaceConverter:
cdef unsigned char i
for i in range(3):
strides.append(self.dst_strides[i])
planes.append(PyMemoryView_FromMemory(<char *> ((<uintptr_t> buf) + self.offsets[i]), self.dst_sizes[i], True))
planes.append(PyMemoryView_FromMemory(<char *> ((<uintptr_t> buf) + self.offsets[i]), self.dst_sizes[i], PyBUF_WRITE))
out_image = CythonImageWrapper(0, 0, self.dst_width, self.dst_height,
planes, self.dst_format, bpp, strides,
planes=ImageWrapper.PLANAR_3)
Expand Down Expand Up @@ -740,7 +741,7 @@ cdef class ColorspaceConverter:
return self.packed_image_wrapper(<char *> bgr48, 48)

cdef packed_image_wrapper(self, char *buf, unsigned char bpp=24):
pybuf = PyMemoryView_FromMemory(buf, self.dst_sizes[0], True)
pybuf = PyMemoryView_FromMemory(buf, self.dst_sizes[0], PyBUF_WRITE)
out_image = CythonImageWrapper(0, 0, self.dst_width, self.dst_height, pybuf, self.dst_format, bpp, self.dst_strides[0], planes=ImageWrapper.PACKED)
out_image.cython_buffer = <uintptr_t> buf
return out_image
Expand Down
5 changes: 3 additions & 2 deletions xpra/codecs/libyuv/colorspace_converter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ from libc.stdlib cimport free

cdef extern from "Python.h":
object PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)
int PyBUF_WRITE

cdef extern from "libyuv/convert_from_argb.h" namespace "libyuv":
#int BGRAToI420(const uint8_t* src_frame, ...
Expand Down Expand Up @@ -575,15 +576,15 @@ cdef class ColorspaceConverter:
log("libyuv.ScalePlane %i times, took %.1fms", self.planes, 1000.0*elapsed)
for i in range(self.planes):
strides.append(self.scaled_stride[i])
planes.append(PyMemoryView_FromMemory(<char *> scaled_planes[i], self.scaled_size[i], True))
planes.append(PyMemoryView_FromMemory(<char *> scaled_planes[i], self.scaled_size[i], PyBUF_WRITE))
self.frames += 1
out_image = YUVImageWrapper(0, 0, self.dst_width, self.dst_height, planes, self.dst_format, 24, strides, 1, self.planes)
out_image.cython_buffer = <uintptr_t> scaled_buffer
else:
#use output buffer directly:
for i in range(self.planes):
strides.append(self.out_stride[i])
planes.append(PyMemoryView_FromMemory(<char *> out_planes[i], self.out_size[i], True))
planes.append(PyMemoryView_FromMemory(<char *> out_planes[i], self.out_size[i], PyBUF_WRITE))
self.frames += 1
out_image = YUVImageWrapper(0, 0, self.dst_width, self.dst_height, planes, self.dst_format, 24, strides, 1, self.planes)
out_image.cython_buffer = <uintptr_t> output_buffer
Expand Down
17 changes: 9 additions & 8 deletions xpra/codecs/webp/decoder.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ cdef extern from *:

cdef extern from "Python.h":
object PyMemoryView_FromMemory(char *mem, Py_ssize_t size, int flags)
int PyBUF_WRITE

cdef extern from "webp/decode.h":

Expand Down Expand Up @@ -181,7 +182,7 @@ cdef class WebpBufferWrapper:

def get_pixels(self):
assert self.buffer_ptr>0, "WebpBufferWrapper has already been freed!"
return PyMemoryView_FromMemory(<char *> self.buffer_ptr, self.size, True)
return PyMemoryView_FromMemory(<char *> self.buffer_ptr, self.size, PyBUF_WRITE)

def free(self):
if self.buffer_ptr!=0:
Expand Down Expand Up @@ -308,16 +309,16 @@ def decompress_yuv(data, has_alpha=False):
webp_check(ret)
if alpha:
planes = (
PyMemoryView_FromMemory(<char *> YUVA.y, y_size, True),
PyMemoryView_FromMemory(<char *> YUVA.u, u_size, True),
PyMemoryView_FromMemory(<char *> YUVA.v, v_size, True),
PyMemoryView_FromMemory(<char *> YUVA.a, a_size, True),
PyMemoryView_FromMemory(<char *> YUVA.y, y_size, PyBUF_WRITE),
PyMemoryView_FromMemory(<char *> YUVA.u, u_size, PyBUF_WRITE),
PyMemoryView_FromMemory(<char *> YUVA.v, v_size, PyBUF_WRITE),
PyMemoryView_FromMemory(<char *> YUVA.a, a_size, PyBUF_WRITE),
)
else:
planes = (
PyMemoryView_FromMemory(<char *> YUVA.y, y_size, True),
PyMemoryView_FromMemory(<char *> YUVA.u, u_size, True),
PyMemoryView_FromMemory(<char *> YUVA.v, v_size, True),
PyMemoryView_FromMemory(<char *> YUVA.y, y_size, PyBUF_WRITE),
PyMemoryView_FromMemory(<char *> YUVA.u, u_size, PyBUF_WRITE),
PyMemoryView_FromMemory(<char *> YUVA.v, v_size, PyBUF_WRITE),
)
img = YUVImageWrapper(0, 0, w, h, planes, "YUV420P", (3+alpha)*8, strides, 3+alpha, ImageWrapper.PLANAR_3+alpha)
img.cython_buffer = <uintptr_t> buf
Expand Down
3 changes: 2 additions & 1 deletion xpra/x11/bindings/ximage.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ cdef extern from "Python.h":
int PyObject_GetBuffer(object obj, Py_buffer *view, int flags)
void PyBuffer_Release(Py_buffer *view)
int PyBUF_ANY_CONTIGUOUS
int PyBUF_READ

cdef extern from "stdlib.h":
int posix_memalign(void **memptr, size_t alignment, size_t size)
Expand Down Expand Up @@ -312,7 +313,7 @@ cdef class XImageWrapper:
cdef void *pix_ptr = self.get_pixels_ptr()
if pix_ptr==NULL:
return None
return PyMemoryView_FromMemory(<char *> pix_ptr, self.get_size(), False)
return PyMemoryView_FromMemory(<char *> pix_ptr, self.get_size(), PyBUF_READ)

def get_sub_image(self, unsigned int x, unsigned int y, unsigned int w, unsigned int h):
if w<=0 or h<=0:
Expand Down

0 comments on commit 6b0c177

Please sign in to comment.