Add other cases to unit tests

This commit is contained in:
544146
2023-03-20 15:44:47 +00:00
parent c4d2e7ec9e
commit 90baa20c8b
2 changed files with 45 additions and 23 deletions

View File

@@ -29,29 +29,31 @@ def _scale_by_percentage(width, height, pct):
step = (pct - 1.0)
new_width = int(round(width * (1.0 + step)))
new_height = int(round(new_width / aspect_ratio))
if new_width > _MAX_DIMENSION:
new_width = _MAX_DIMENSION
new_height = int(round(new_width / aspect_ratio))
if new_height > _MAX_DIMENSION:
new_height = _MAX_DIMENSION
new_width = int(round(new_height * aspect_ratio))
if new_width < _MIN_DIMENSION:
new_width = _MIN_DIMENSION
new_height = int(round(new_width / aspect_ratio))
if new_height < _MIN_DIMENSION:
new_height = _MIN_DIMENSION
new_width = int(round(new_height * aspect_ratio))
return new_width, new_height
return _clamp_to_boundaries(new_width, new_height, aspect_ratio)
def _scale_dimensions_to_max_dimension(width, height, max_dim):
if max_dim < _MIN_DIMENSION:
max_dim = _MIN_DIMENSION
elif max_dim > _MAX_DIMENSION:
max_dim = _MAX_DIMENSION
if max_dim == max(width, height):
return width, height
aspect_ratio = float(width) / float(height)
if width > height:
return max_dim, max(int(round(max_dim / aspect_ratio)), 1)
return max(int(round(max_dim * aspect_ratio)), 1), max_dim
new_width = max_dim
new_height = max(int(round(max_dim / aspect_ratio)), 1)
else:
new_height = max_dim
new_width = max(int(round(max_dim * aspect_ratio)), 1)
return _clamp_to_boundaries(new_width, new_height, aspect_ratio)
def _clamp_to_boundaries(width, height, aspect_ratio):
if width > _MAX_DIMENSION:
width = _MAX_DIMENSION
height = int(round(width / aspect_ratio))
if height > _MAX_DIMENSION:
height = _MAX_DIMENSION
width = int(round(height * aspect_ratio))
if width < _MIN_DIMENSION:
width = _MIN_DIMENSION
height = int(round(width / aspect_ratio))
if height < _MIN_DIMENSION:
height = _MIN_DIMENSION
width = int(round(height * aspect_ratio))
return width, height

View File

@@ -111,15 +111,35 @@ def test_scale_by_percentage(
(_MIN_DIMENSION, _MIN_DIMENSION),
id='scale_from_max_to_min',
),
pytest.param(
_MIN_DIMENSION, 32, _MIN_DIMENSION,
(128, _MIN_DIMENSION),
id='scale_below_min_height_dimension_clamps_retains_ar',
),
pytest.param(
32, _MIN_DIMENSION, _MIN_DIMENSION,
(_MIN_DIMENSION, 128),
id='scale_below_min_width_dimension_clamps_retains_ar',
),
pytest.param(
_MAX_DIMENSION, 4096, _MAX_DIMENSION,
(1024, _MAX_DIMENSION),
id='scale_above_max_height_dimension_clamps_retains_ar',
),
pytest.param(
4096, _MAX_DIMENSION, _MAX_DIMENSION,
(_MAX_DIMENSION, 1024),
id='scale_above_max_width_dimension_clamps_retains_ar',
),
pytest.param(
64, 64, _MIN_DIMENSION - 1,
(_MIN_DIMENSION, _MIN_DIMENSION),
id='scale_below_min_dimension_clamps_retains_ar',
id='scale_dimension_below_min_dimension_clamps_retains_ar',
),
pytest.param(
64, 64, _MAX_DIMENSION + 1,
(_MAX_DIMENSION, _MAX_DIMENSION),
id='scale_above_max_dimension_clamps_retains_ar',
id='scale_dimension_above_max_dimension_clamps_retains_ar',
),
],
)