Skip to content

Commit

Permalink
use Grayscale8 QImage format to spare some array copies
Browse files Browse the repository at this point in the history
  • Loading branch information
t20100 committed Oct 26, 2023
1 parent c4f9782 commit 0166a2c
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/silx/gui/_glutils/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def rasterTextQt(text, font, size=-1, weight=-1, italic=False, devicePixelRatio=
font = qt.QFont(font, size, weight, italic)

# get text size
image = qt.QImage(1, 1, qt.QImage.Format_RGB888)
image = qt.QImage(1, 1, qt.QImage.Format_Grayscale8)
painter = qt.QPainter()
painter.begin(image)
painter.setPen(qt.Qt.white)
Expand All @@ -125,11 +125,11 @@ def rasterTextQt(text, font, size=-1, weight=-1, italic=False, devicePixelRatio=
# align line size to 32 bits to ease conversion to numpy array
width = 4 * ((width + 3) // 4)
image = qt.QImage(
int(width), int(bounds.height() * devicePixelRatio + 2), qt.QImage.Format_RGB888
int(width),
int(bounds.height() * devicePixelRatio + 2),
qt.QImage.Format_Grayscale8,
)
image.setDevicePixelRatio(devicePixelRatio)

# TODO if Qt5 use Format_Grayscale8 instead
image.fill(0)

# Raster text
Expand All @@ -142,9 +142,6 @@ def rasterTextQt(text, font, size=-1, weight=-1, italic=False, devicePixelRatio=

array = convertQImageToArray(image)

# RGB to R
array = numpy.ascontiguousarray(array[:, :, 0])

# Remove leading and trailing empty columns/rows but one on each side
filled_rows = numpy.nonzero(numpy.sum(array, axis=1))[0]
filled_columns = numpy.nonzero(numpy.sum(array, axis=0))[0]
Expand Down

0 comments on commit 0166a2c

Please sign in to comment.