File: test_qimage_writethru.py

package info (click to toggle)
python-pyqtgraph 0.13.7-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,072 kB
  • sloc: python: 54,043; makefile: 127; ansic: 40; sh: 2
file content (30 lines) | stat: -rw-r--r-- 873 bytes parent folder | download | duplicates (3)
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
import numpy as np

import pyqtgraph as pg


def test_qimage_writethrough():
    w, h = 256, 256
    backstore = np.ones((h, w), dtype=np.uint8)
    ptr0 = backstore.ctypes.data
    fmt = pg.Qt.QtGui.QImage.Format.Format_Grayscale8
    qimg = pg.functions.ndarray_to_qimage(backstore, fmt)

    def get_pointer(obj):
        if hasattr(obj, 'setsize'):
            return int(obj)
        else:
            return np.frombuffer(obj, dtype=np.uint8).ctypes.data

    # test that QImage is using the provided buffer (i.e. zero-copy)
    ptr1 = get_pointer(qimg.constBits())
    assert ptr0 == ptr1

    # test that QImage is not const (i.e. no COW)
    # if QImage is const, then bits() returns a copy
    ptr2 = get_pointer(qimg.bits())
    assert ptr1 == ptr2

    # test that data gets written through to provided buffer
    qimg.fill(0)
    assert np.all(backstore == 0)