Merge branch 'master' into fli

This commit is contained in:
wiredfool 2017-08-16 11:46:55 +01:00 committed by GitHub
commit 680a7ac43b
15 changed files with 115 additions and 36 deletions

View File

@ -59,7 +59,7 @@ class FliImageFile(ImageFile.ImageFile):
# animation speed
duration = i32(s[16:20])
if magic == 0xAF11:
duration = (duration * 1000) / 70
duration = (duration * 1000) // 70
self.info["duration"] = duration
# look for palette
@ -156,6 +156,7 @@ class FliImageFile(ImageFile.ImageFile):
def tell(self):
return self.__frame
#
# registry

View File

@ -56,8 +56,9 @@ try:
from . import _imaging as core
if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
raise ImportError("The _imaging extension was built for another "
"version of Pillow or PIL: Core Version: %s"
"Pillow Version: %s" %
"version of Pillow or PIL:\n"
"Core version: %s\n"
"Pillow version: %s" %
(getattr(core, 'PILLOW_VERSION', None),
PILLOW_VERSION))

View File

@ -36,6 +36,7 @@ class _imagingft_not_installed(object):
def __getattr__(self, id):
raise ImportError("The _imagingft C module is not installed")
try:
from . import _imagingft as core
except ImportError:
@ -113,7 +114,6 @@ class ImageFont(object):
return self.font.getmask(text, mode)
##
# Wrapper for FreeType fonts. Application code should use the
# <b>truetype</b> factory function to create font objects.
@ -162,7 +162,7 @@ class FreeTypeFont(object):
def getmask(self, text, mode="", direction=None, features=None):
return self.getmask2(text, mode, direction=direction, features=features)[0]
def getmask2(self, text, mode="", fill=Image.core.fill, direction=None, features=None):
def getmask2(self, text, mode="", fill=Image.core.fill, direction=None, features=None, *args, **kwargs):
size, offset = self.font.getsize(text, direction, features)
im = fill("L", size, 0)
self.font.render(text, im.id, mode == "1", direction, features)
@ -250,7 +250,7 @@ def truetype(font=None, size=10, index=0, encoding="",
and "armn" (Apple Roman). See the FreeType documentation
for more information.
:param layout_engine: Which layout engine to use, if available:
`ImageFont.LAYOUT_BASIC` or `ImageFont.LAYOUT_RAQM`.
`ImageFont.LAYOUT_BASIC` or `ImageFont.LAYOUT_RAQM`.
:return: A font object.
:exception IOError: If the file could not be read.
"""

View File

@ -104,6 +104,11 @@ def _save(im, fp, filename):
z = len(im.mode)
if dim == 1 or dim == 2:
z = 1
# assert we've got the right number of bands.
if len(im.getbands()) != z:
raise ValueError("incorrect number of bands in SGI write: %s vs %s" %
(z, len(im.getbands())))
# Minimum Byte value
pinmin = 0
# Maximum Byte value (255 = 8bits per pixel)
@ -131,11 +136,6 @@ def _save(im, fp, filename):
fp.write(struct.pack('404s', b'')) # dummy
# assert we've got the right number of bands.
if len(im.getbands()) != z:
raise ValueError("incorrect number of bands in SGI write: %s vs %s" %
(z, len(im.getbands())))
for channel in im.split():
fp.write(channel.tobytes())

BIN
Tests/images/a.fli Normal file

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View File

@ -4,17 +4,28 @@ from PIL import Image, FliImagePlugin
# created as an export of a palette image from Gimp2.6
# save as...-> hopper.fli, default options.
test_file = "Tests/images/hopper.fli"
static_test_file = "Tests/images/hopper.fli"
# From https://samples.libav.org/fli-flc/
animated_test_file = "Tests/images/a.fli"
class TestFileFli(PillowTestCase):
def test_sanity(self):
im = Image.open(test_file)
im = Image.open(static_test_file)
im.load()
self.assertEqual(im.mode, "P")
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "FLI")
self.assertFalse(im.is_animated)
im = Image.open(animated_test_file)
self.assertEqual(im.mode, "P")
self.assertEqual(im.size, (320, 200))
self.assertEqual(im.format, "FLI")
self.assertEqual(im.info["duration"], 71)
self.assertTrue(im.is_animated)
def test_tell(self):
# Arrange
@ -33,12 +44,16 @@ class TestFileFli(PillowTestCase):
lambda: FliImagePlugin.FliImageFile(invalid_file))
def test_n_frames(self):
im = Image.open(test_file)
im = Image.open(static_test_file)
self.assertEqual(im.n_frames, 1)
self.assertFalse(im.is_animated)
im = Image.open(animated_test_file)
self.assertEqual(im.n_frames, 385)
self.assertTrue(im.is_animated)
def test_eoferror(self):
im = Image.open(test_file)
im = Image.open(animated_test_file)
n_frames = im.n_frames
while True:
@ -58,6 +73,24 @@ class TestFileFli(PillowTestCase):
# Test seek past end of file
self.assertRaises(EOFError, lambda: im.seek(2))
def test_seek_tell(self):
im = Image.open(animated_test_file)
layer_number = im.tell()
self.assertEqual(layer_number, 0)
im.seek(0)
layer_number = im.tell()
self.assertEqual(layer_number, 0)
im.seek(1)
layer_number = im.tell()
self.assertEqual(layer_number, 1)
im.seek(2)
layer_number = im.tell()
self.assertEqual(layer_number, 2)
if __name__ == '__main__':
unittest.main()

View File

@ -94,6 +94,28 @@ class TestImageFilter(PillowTestCase):
self.assertEqual(rankfilter.size, 1)
self.assertEqual(rankfilter.rank, 2)
def test_consistency_3x3(self):
im = Image.open("Tests/images/hopper.bmp")
emboss = im.filter(ImageFilter.Kernel((3, 3),
(-1, -1, 0,
-1, 0, 1,
0, 1, 1), .3))
self.assert_image_equal(
emboss, Image.open("Tests/images/hopper_emboss.bmp"))
def test_consistency_5x5(self):
im = Image.open("Tests/images/hopper.bmp")
emboss = im.filter(ImageFilter.Kernel((5, 5),
(-1, -1, -1, -1, 0,
-1, -1, -1, 0, 1,
-1, -1, 0, 1, 1,
-1, 0, 1, 1, 1,
0, 1, 1, 1, 1), 0.3))
self.assert_image_equal(
emboss, Image.open("Tests/images/hopper_emboss_more.bmp"))
if __name__ == '__main__':
unittest.main()

View File

@ -40,13 +40,15 @@ class SimplePatcher(object):
else:
delattr(self._parent_obj, self._attr_name)
@unittest.skipUnless(HAS_FREETYPE, "ImageFont not Available")
class TestImageFont(PillowTestCase):
LAYOUT_ENGINE = ImageFont.LAYOUT_BASIC
# Freetype has different metrics depending on the version.
# (and, other things, but first things first)
METRICS = { ('2', '3'): {'multiline': 30,
METRICS = {
('2', '3'): {'multiline': 30,
'textsize': 12,
'getters': (13, 16)},
('2', '7'): {'multiline': 6.2,
@ -213,6 +215,14 @@ class TestImageFont(PillowTestCase):
font=ttf,
align="unknown"))
def test_draw_align(self):
im = Image.new('RGB', (300, 100), 'white')
draw = ImageDraw.Draw(im)
ttf = self.get_font()
line = "some text"
draw.text((100, 40), line, (0, 0, 0), font=ttf, align='left')
del draw
def test_multiline_size(self):
ttf = self.get_font()
im = Image.new(mode='RGB', size=(300, 100))
@ -395,7 +405,7 @@ class TestImageFont(PillowTestCase):
def test_getsize_empty(self):
font = self.get_font()
# should not crash.
# should not crash.
self.assertEqual((0, 0), font.getsize(''))
def _test_fake_loading_font(self, path_to_fake, fontname):
@ -494,5 +504,6 @@ class TestImageFont(PillowTestCase):
class TestImageFont_RaqmLayout(TestImageFont):
LAYOUT_ENGINE = ImageFont.LAYOUT_RAQM
if __name__ == '__main__':
unittest.main()

View File

@ -1,4 +1,4 @@
<h3>Need help?</h3>
<p>
You can get help via IRC at <a href="irc://irc.freenode.net#pil">irc://irc.freenode.net#pil</a> or Stack Overflow <a href="https://stackoverflow.com/questions/tagged/pillow">here</a> and <a href="https://stackoverflow.com/questions/tagged/pil">here</a>. Please <a href="https://github.com/python-pillow/Pillow/issues/new">report issues on GitHub</a>.
You can get help via IRC at <a href="irc://irc.freenode.net#pil">irc://irc.freenode.net#pil</a> or Stack Overflow <a href="https://stackoverflow.com/questions/tagged/pillow">here</a> and <a href="https://stackoverflow.com/questions/tagged/python-imaging-library">here</a>. Please <a href="https://github.com/python-pillow/Pillow/issues/new">report issues on GitHub</a>.
</p>

View File

@ -815,6 +815,7 @@ static int decode_bcn(Imaging im, ImagingCodecState state, const UINT8* src, int
case NN: \
while (bytes >= SZ) { \
TY col[16]; \
memset(col, 0, 16 * sizeof(col[0])); \
decode_bc##NN##_block(col, ptr); \
put_block(im, state, (const char *)col, sizeof(col[0]), C); \
ptr += SZ; \

View File

@ -31,6 +31,7 @@ ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode)
{
Imaging imOut;
int x, y;
ImagingSectionCookie cookie;
if (xmargin < 0 && ymargin < 0)
return (Imaging) ImagingError_ValueError("bad kernel size");
@ -60,11 +61,13 @@ ImagingExpand(Imaging imIn, int xmargin, int ymargin, int mode)
EXPAND_LINE(type, image, imIn->ysize-1, ymargin+imIn->ysize+y);\
}
ImagingSectionEnter(&cookie);
if (imIn->image8) {
EXPAND(UINT8, image8);
} else {
EXPAND(INT32, image32);
}
ImagingSectionLeave(&cookie);
ImagingCopyInfo(imOut, imIn);
@ -78,6 +81,7 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel,
Imaging imOut;
int x, y;
FLOAT32 sum;
ImagingSectionCookie cookie;
if (!im || strcmp(im->mode, "L") != 0)
return (Imaging) ImagingError_ModeError();
@ -92,6 +96,9 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel,
if (!imOut)
return NULL;
// Add one time for rounding
offset += 0.5;
/* brute force kernel implementations */
#define KERNEL3x3(image, kernel, d) ( \
(int) image[y+1][x-d] * kernel[0] + \
@ -131,6 +138,7 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel,
(int) image[y-2][x+d] * kernel[23] + \
(int) image[y-2][x+d+d] * kernel[24])
ImagingSectionEnter(&cookie);
if (xsize == 3) {
/* 3x3 kernel. */
for (x = 0; x < im->xsize; x++)
@ -174,6 +182,7 @@ ImagingFilter(Imaging im, int xsize, int ysize, const FLOAT32* kernel,
for (x = 0; x < im->xsize; x++)
imOut->image8[y][x] = im->image8[y][x];
}
ImagingSectionLeave(&cookie);
return imOut;
}

View File

@ -5,6 +5,13 @@
#define ROUND_UP(f) ((int) ((f) >= 0.0 ? (f) + 0.5F : (f) - 0.5F))
#ifdef WORDS_BIGENDIAN
#define MAKE_UINT32(u0, u1, u2, u3) (u3 | (u2<<8) | (u1<<16) | (u0<<24))
#else
#define MAKE_UINT32(u0, u1, u2, u3) (u0 | (u1<<8) | (u2<<16) | (u3<<24))
#endif
struct filter {
double (*filter)(double x);
double support;
@ -281,8 +288,8 @@ ImagingResampleHorizontal_8bpc(Imaging imIn, int xsize, struct filter *filterp)
ss0 += ((UINT8) imIn->image[yy][(x + xmin)*4 + 0]) * k[x];
ss3 += ((UINT8) imIn->image[yy][(x + xmin)*4 + 3]) * k[x];
}
imOut->image[yy][xx*4 + 0] = clip8(ss0);
imOut->image[yy][xx*4 + 3] = clip8(ss3);
((UINT32 *) imOut->image[yy])[xx] = MAKE_UINT32(
clip8(ss0), 0, 0, clip8(ss3));
}
}
} else if (imIn->bands == 3) {
@ -297,9 +304,8 @@ ImagingResampleHorizontal_8bpc(Imaging imIn, int xsize, struct filter *filterp)
ss1 += ((UINT8) imIn->image[yy][(x + xmin)*4 + 1]) * k[x];
ss2 += ((UINT8) imIn->image[yy][(x + xmin)*4 + 2]) * k[x];
}
imOut->image[yy][xx*4 + 0] = clip8(ss0);
imOut->image[yy][xx*4 + 1] = clip8(ss1);
imOut->image[yy][xx*4 + 2] = clip8(ss2);
((UINT32 *) imOut->image[yy])[xx] = MAKE_UINT32(
clip8(ss0), clip8(ss1), clip8(ss2), 0);
}
}
} else {
@ -315,10 +321,8 @@ ImagingResampleHorizontal_8bpc(Imaging imIn, int xsize, struct filter *filterp)
ss2 += ((UINT8) imIn->image[yy][(x + xmin)*4 + 2]) * k[x];
ss3 += ((UINT8) imIn->image[yy][(x + xmin)*4 + 3]) * k[x];
}
imOut->image[yy][xx*4 + 0] = clip8(ss0);
imOut->image[yy][xx*4 + 1] = clip8(ss1);
imOut->image[yy][xx*4 + 2] = clip8(ss2);
imOut->image[yy][xx*4 + 3] = clip8(ss3);
((UINT32 *) imOut->image[yy])[xx] = MAKE_UINT32(
clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3));
}
}
}
@ -386,8 +390,8 @@ ImagingResampleVertical_8bpc(Imaging imIn, int ysize, struct filter *filterp)
ss0 += ((UINT8) imIn->image[y + ymin][xx*4 + 0]) * k[y];
ss3 += ((UINT8) imIn->image[y + ymin][xx*4 + 3]) * k[y];
}
imOut->image[yy][xx*4 + 0] = clip8(ss0);
imOut->image[yy][xx*4 + 3] = clip8(ss3);
((UINT32 *) imOut->image[yy])[xx] = MAKE_UINT32(
clip8(ss0), 0, 0, clip8(ss3));
}
}
} else if (imIn->bands == 3) {
@ -402,9 +406,8 @@ ImagingResampleVertical_8bpc(Imaging imIn, int ysize, struct filter *filterp)
ss1 += ((UINT8) imIn->image[y + ymin][xx*4 + 1]) * k[y];
ss2 += ((UINT8) imIn->image[y + ymin][xx*4 + 2]) * k[y];
}
imOut->image[yy][xx*4 + 0] = clip8(ss0);
imOut->image[yy][xx*4 + 1] = clip8(ss1);
imOut->image[yy][xx*4 + 2] = clip8(ss2);
((UINT32 *) imOut->image[yy])[xx] = MAKE_UINT32(
clip8(ss0), clip8(ss1), clip8(ss2), 0);
}
}
} else {
@ -420,10 +423,8 @@ ImagingResampleVertical_8bpc(Imaging imIn, int ysize, struct filter *filterp)
ss2 += ((UINT8) imIn->image[y + ymin][xx*4 + 2]) * k[y];
ss3 += ((UINT8) imIn->image[y + ymin][xx*4 + 3]) * k[y];
}
imOut->image[yy][xx*4 + 0] = clip8(ss0);
imOut->image[yy][xx*4 + 1] = clip8(ss1);
imOut->image[yy][xx*4 + 2] = clip8(ss2);
imOut->image[yy][xx*4 + 3] = clip8(ss3);
((UINT32 *) imOut->image[yy])[xx] = MAKE_UINT32(
clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3));
}
}
}