Skip to content

Commit fdcbd7a

Browse files
authored
Merge pull request #2852 from Damus666/blur-handle-zero-size-surfaces
Using blur on a surface with either width or height equal to 0 won't raise a ValueError
2 parents 4ebed9e + 548ba16 commit fdcbd7a

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

docs/reST/ref/transform.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ Instead, always begin with the original image and scale to the desired size.)
245245
.. versionchanged:: 2.3.0
246246
Passing the calling surface as destination surface raises a ``ValueError``
247247

248+
.. versionchanged:: 2.5.0
249+
A surface with either width or height equal to 0 won't raise a ``ValueError``
250+
248251
.. ## pygame.transform.box_blur ##
249252
250253
.. function:: gaussian_blur
@@ -271,6 +274,9 @@ Instead, always begin with the original image and scale to the desired size.)
271274
Now the standard deviation of the Gaussian kernel is equal to the radius.
272275
Blur results will be slightly different.
273276

277+
.. versionchanged:: 2.5.0
278+
A surface with either width or height equal to 0 won't raise a ``ValueError``
279+
274280
.. ## pygame.transform.gaussian_blur ##
275281
276282
.. function:: average_surfaces

src_c/transform.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,6 +3266,15 @@ blur(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int radius,
32663266
retsurf = pgSurface_AsSurface(dstobj);
32673267
}
32683268

3269+
if ((retsurf->w) != (src->w) || (retsurf->h) != (src->h)) {
3270+
return RAISE(PyExc_ValueError,
3271+
"Destination surface not the same size.");
3272+
}
3273+
3274+
if (retsurf->w == 0 || retsurf->h == 0) {
3275+
return retsurf;
3276+
}
3277+
32693278
Uint8 *ret_start = retsurf->pixels;
32703279
Uint8 *ret_end = ret_start + retsurf->h * retsurf->pitch;
32713280
Uint8 *src_start = src->pixels;
@@ -3279,11 +3288,6 @@ blur(pgSurfaceObject *srcobj, pgSurfaceObject *dstobj, int radius,
32793288
"of them is a subsurface, or they are sharing the same buffer.");
32803289
}
32813290

3282-
if ((retsurf->w) != (src->w) || (retsurf->h) != (src->h)) {
3283-
return RAISE(PyExc_ValueError,
3284-
"Destination surface not the same size.");
3285-
}
3286-
32873291
if (PG_SURF_BytesPerPixel(src) != PG_SURF_BytesPerPixel(retsurf)) {
32883292
return (SDL_Surface *)(RAISE(
32893293
PyExc_ValueError,

test/transform_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,19 @@ def test_gaussian_blur(self):
16001600
for pos in data2:
16011601
self.assertTrue(sf_b2.get_at(pos) == data2[pos])
16021602

1603+
def test_blur_zero_size_surface(self):
1604+
surface = pygame.Surface((0, 0))
1605+
self.assertEqual(pygame.transform.box_blur(surface, 3).get_size(), (0, 0))
1606+
self.assertEqual(pygame.transform.gaussian_blur(surface, 3).get_size(), (0, 0))
1607+
1608+
surface = pygame.Surface((20, 0))
1609+
self.assertEqual(pygame.transform.box_blur(surface, 3).get_size(), (20, 0))
1610+
self.assertEqual(pygame.transform.gaussian_blur(surface, 3).get_size(), (20, 0))
1611+
1612+
surface = pygame.Surface((0, 20))
1613+
self.assertEqual(pygame.transform.box_blur(surface, 3).get_size(), (0, 20))
1614+
self.assertEqual(pygame.transform.gaussian_blur(surface, 3).get_size(), (0, 20))
1615+
16031616
def test_flip(self):
16041617
"""honors the set_color key on the returned surface from flip."""
16051618
image_loaded = pygame.image.load(example_path("data/chimp.png"))

0 commit comments

Comments
 (0)