Add access functions for ImagingEffectMandelbrot with a test

This commit is contained in:
hugovk 2014-09-02 15:14:00 +03:00
parent 76ef83263b
commit 8c96d38ce9
4 changed files with 46 additions and 21 deletions

View File

@ -2424,9 +2424,21 @@ def _showxv(image, title=None, **options):
# --------------------------------------------------------------------
# Effects
def effect_mandelbrot(size, extent, quality):
"""
Generate a Mandelbrot set covering the given extent.
:param size: The requested size in pixels, as a 2-tuple:
(width, height).
:param extent: The extent to cover, as a 4-tuple:
(x0, y0, x1, y2).
:param quality: Quality.
"""
return Image()._new(core.effect_mandelbrot(size, extent, quality))
def effect_noise(size, sigma):
"""
Generate Gaussian noise centered around 128
Generate Gaussian noise centered around 128.
:param size: The requested size in pixels, as a 2-tuple:
(width, height).

BIN
Tests/images/mandelbrot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -141,6 +141,20 @@ class TestImage(PillowTestCase):
img_colors = sorted(img.getcolors())
self.assertEqual(img_colors, expected_colors)
def test_effect_mandelbrot(self):
# Arrange
size = (512, 512)
extent = (-3, -2.5, 2, 2.5)
quality = 100
# Act
im = Image.effect_mandelbrot(size, extent, quality)
# Assert
self.assertEqual(im.size, (512, 512))
im2 = Image.open('Tests/images/mandelbrot.png')
self.assert_image_equal(im, im2)
@unittest.skipUnless(sys.platform.startswith('win32'),
"Stalls on Travis CI, passes on Windows")
def test_effect_noise(self):
@ -156,7 +170,6 @@ class TestImage(PillowTestCase):
self.assertEqual(im.getpixel((0, 0)), 60)
self.assertEqual(im.getpixel((0, 1)), 28)
if __name__ == '__main__':
unittest.main()

View File

@ -48,25 +48,25 @@ ImagingEffectMandelbrot(int xsize, int ysize, double extent[4], int quality)
for (y = 0; y < ysize; y++) {
UINT8* buf = im->image8[y];
for (x = 0; x < xsize; x++) {
x1 = y1 = xi2 = yi2 = 0.0;
cr = x*dr + extent[0];
ci = y*di + extent[1];
for (k = 1;; k++) {
y1 = 2*x1*y1 + ci;
x1 = xi2 - yi2 + cr;
xi2 = x1*x1;
yi2 = y1*y1;
if ((xi2 + yi2) > radius) {
buf[x] = k*255/quality;
break;
}
if (k > quality) {
buf[x] = 0;
break;
}
}
}
for (x = 0; x < xsize; x++) {
x1 = y1 = xi2 = yi2 = 0.0;
cr = x*dr + extent[0];
ci = y*di + extent[1];
for (k = 1;; k++) {
y1 = 2*x1*y1 + ci;
x1 = xi2 - yi2 + cr;
xi2 = x1*x1;
yi2 = y1*y1;
if ((xi2 + yi2) > radius) {
buf[x] = k*255/quality;
break;
}
if (k > quality) {
buf[x] = 0;
break;
}
}
}
}
return im;
}