Skip to content

Commit 86afe21

Browse files
committed
Refresh image dimensions after quarter-turn rotations
1 parent 432932a commit 86afe21

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version
88

99
### Fixed
1010

11+
- `tcod.image.Image.rotate90` updates the width and height of rectangular images, keeping NumPy array conversions consistent with the rotated pixels.
1112
- Adding a root to an already resolved `tcod.path.Pathfinder` resets its predecessor only when its value improves the current distance. This prevents cyclic paths while preserving routes to cheaper roots.
1213
- Lazy pathfinder queries reconsider previously visited goals after adding a root or lowering a root's distance.
1314

‎tcod/image.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ def rotate90(self, rotations: int = 1) -> None:
110110
rotations (int): Number of 90 degree clockwise rotations.
111111
"""
112112
lib.TCOD_image_rotate90(self.image_c, rotations)
113+
self.width, self.height = self._get_size()
113114

114115
def vflip(self) -> None:
115116
"""Vertically flip this Image."""

‎tests/test_image.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""Tests for the image object and its NumPy interface."""
2+
3+
import numpy as np
4+
import pytest
5+
6+
import tcod.image
7+
8+
9+
@pytest.mark.parametrize("shape", [(4, 6), (6, 4), (4, 4)])
10+
@pytest.mark.parametrize("rotations", [-5, -2, -1, 0, 1, 2, 3, 4, 5])
11+
def test_rotate90_dimensions(shape: tuple[int, int], rotations: int) -> None:
12+
pixels = np.arange(shape[0] * shape[1] * 3, dtype=np.uint8).reshape(*shape, 3)
13+
image = tcod.image.Image.from_array(pixels)
14+
image.rotate90(rotations)
15+
expected = np.rot90(pixels, -rotations)
16+
assert (image.height, image.width) == expected.shape[:2]
17+
np.testing.assert_array_equal(np.asarray(image), expected)
18+
19+
20+
def test_rotate90_sequence() -> None:
21+
pixels = np.arange(4 * 6 * 3, dtype=np.uint8).reshape(4, 6, 3)
22+
image = tcod.image.Image.from_array(pixels)
23+
for _ in range(4):
24+
image.rotate90()
25+
pixels = np.rot90(pixels, -1)
26+
assert (image.height, image.width) == pixels.shape[:2]
27+
np.testing.assert_array_equal(np.asarray(image), pixels)

0 commit comments

Comments
 (0)