Skip to content

Commit 7827507

Browse files
authored
Deprecate ImageQt align8to32() in favour of raw encoder stride (#9940)
1 parent 83f23d7 commit 7827507

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

Tests/test_imageqt.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,9 @@ def test_image(mode: str) -> None:
5353
def test_closed_file() -> None:
5454
with warnings.catch_warnings(action="error"):
5555
ImageQt.ImageQt("Tests/images/hopper.gif")
56+
57+
58+
def test_align8to32_deprecation() -> None:
59+
im = hopper("1")
60+
with pytest.warns(DeprecationWarning, match="ImageQt.align8to32"):
61+
ImageQt.align8to32(im.tobytes(), im.width, im.mode)

docs/deprecations.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ExifTags.IFD.Makernote
2121
``ExifTags.IFD.MakerNote``.
2222

2323
Image getdata()
24-
~~~~~~~~~~~~~~~
24+
^^^^^^^^^^^^^^^
2525

2626
.. deprecated:: 12.1.0
2727

@@ -30,9 +30,16 @@ Image getdata()
3030
identical, except that it returns a tuple of pixel values, instead of an internal
3131
Pillow data type.
3232

33+
ImageQt align8to32()
34+
^^^^^^^^^^^^^^^^^^^^
35+
36+
.. deprecated:: 13.0.0
37+
38+
``ImageQt.align8to32()`` has been deprecated. This was an undocumented helper function
39+
intended for internal use, so there is no replacement.
3340

3441
JpegImageFile.load_djpeg
35-
~~~~~~~~~~~~~~~~~~~~~~~~
42+
^^^^^^^^^^^^^^^^^^^^^^^^
3643

3744
.. deprecated:: 13.0.0
3845

docs/releasenotes/13.0.0.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ JpegImageFile.load_djpeg
7878
Use the built-in JPEG decoder instead, or call ``djpeg`` directly and decode the
7979
resulting image with Pillow.
8080

81+
ImageQt align8to32()
82+
^^^^^^^^^^^^^^^^^^^^
83+
84+
``ImageQt.align8to32()`` has been deprecated. This was an undocumented helper function
85+
intended for internal use, so there is no replacement.
86+
8187
API changes
8288
===========
8389

src/PIL/ImageQt.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from io import BytesIO
2222

2323
from . import Image
24+
from ._deprecate import deprecate
2425
from ._util import is_path
2526

2627
TYPE_CHECKING = False
@@ -107,6 +108,7 @@ def align8to32(bytes: bytes, width: int, mode: str) -> bytes:
107108
"""
108109
converts each scanline of data from 8 bit to 32 bit aligned
109110
"""
111+
deprecate("ImageQt.align8to32", 14)
110112

111113
bits_per_pixel = {"1": 1, "L": 8, "P": 8, "I;16": 16}[mode]
112114

@@ -174,10 +176,16 @@ def _toqclass_helper(im: Image.Image | str | QByteArray) -> dict[str, Any]:
174176
raise ValueError(msg)
175177

176178
size = im.size
177-
__data = data or align8to32(im.tobytes(), size[0], im.mode)
179+
if data is None:
180+
# Compute the stride (scanline size) in bytes when aligned
181+
# to Qt's requirement that scanlines be aligned to 32 bits.
182+
bpp = {"1": 1, "L": 8, "P": 8, "I;16": 16}[im.mode]
183+
stride = (bpp * size[0] + 31) // 32 * (32 // 8)
184+
185+
data = im.tobytes("raw", im.mode, stride)
178186
if exclusive_fp:
179187
im.close()
180-
return {"data": __data, "size": size, "format": format, "colortable": colortable}
188+
return {"data": data, "size": size, "format": format, "colortable": colortable}
181189

182190

183191
if qt_is_installed:

0 commit comments

Comments
 (0)