Merge pull request #980 from wiredfool/pr_938

4 bit TIFF support
This commit is contained in:
Hugo 2014-10-29 21:41:17 +02:00
commit 4d74b81a15
4 changed files with 47 additions and 20 deletions

View File

@ -149,6 +149,7 @@ OPEN_INFO = {
(II, 0, 1, 2, (8,), ()): ("L", "L;IR"), (II, 0, 1, 2, (8,), ()): ("L", "L;IR"),
(II, 0, 3, 1, (32,), ()): ("F", "F;32F"), (II, 0, 3, 1, (32,), ()): ("F", "F;32F"),
(II, 1, 1, 1, (1,), ()): ("1", "1"), (II, 1, 1, 1, (1,), ()): ("1", "1"),
(II, 1, 1, 1, (4,), ()): ("L", "L;4"),
(II, 1, 1, 2, (1,), ()): ("1", "1;R"), (II, 1, 1, 2, (1,), ()): ("1", "1;R"),
(II, 1, 1, 1, (8,), ()): ("L", "L"), (II, 1, 1, 1, (8,), ()): ("L", "L"),
(II, 1, 1, 1, (8, 8), (2,)): ("LA", "LA"), (II, 1, 1, 1, (8, 8), (2,)): ("LA", "LA"),
@ -449,10 +450,10 @@ class ImageFileDirectory(collections.MutableMapping):
if size > 4: if size > 4:
here = fp.tell() here = fp.tell()
if Image.DEBUG: if Image.DEBUG:
print ("Tag Location: %s" %here) print("Tag Location: %s" % here)
fp.seek(i32(ifd, 8)) fp.seek(i32(ifd, 8))
if Image.DEBUG: if Image.DEBUG:
print ("Data Location: %s" %fp.tell()) print("Data Location: %s" % fp.tell())
data = ImageFile._safe_read(fp, size) data = ImageFile._safe_read(fp, size)
fp.seek(here) fp.seek(here)
else: else:
@ -659,14 +660,14 @@ class TiffImageFile(ImageFile.ImageFile):
if not self.__next: if not self.__next:
raise EOFError("no more images in TIFF file") raise EOFError("no more images in TIFF file")
if Image.DEBUG: if Image.DEBUG:
print("Seeking to frame %s, on frame %s, __next %s, location: %s"% print("Seeking to frame %s, on frame %s, __next %s, location: %s" %
(frame, self.__frame, self.__next, self.fp.tell())) (frame, self.__frame, self.__next, self.fp.tell()))
# reset python3 buffered io handle in case fp # reset python3 buffered io handle in case fp
# was passed to libtiff, invalidating the buffer # was passed to libtiff, invalidating the buffer
self.fp.tell() self.fp.tell()
self.fp.seek(self.__next) self.fp.seek(self.__next)
if Image.DEBUG: if Image.DEBUG:
print("Loading tags, location: %s"%self.fp.tell()) print("Loading tags, location: %s" % self.fp.tell())
self.tag.load(self.fp) self.tag.load(self.fp)
self.__next = self.tag.next self.__next = self.tag.next
self.__frame += 1 self.__frame += 1

Binary file not shown.

View File

@ -342,6 +342,24 @@ class TestFileLibTiff(LibTiffTestCase):
im.load() im.load()
self.assertFalse(im.tag.next) self.assertFalse(im.tag.next)
def test_4bit(self):
# Arrange
test_file = "Tests/images/hopper_gray_4bpp.tif"
original = hopper("L")
# Act
TiffImagePlugin.READ_LIBTIFF = True
im = Image.open(test_file)
TiffImagePlugin.READ_LIBTIFF = False
# Assert
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.mode, "L")
self.assert_image_similar(im, original, 7.3)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -144,28 +144,27 @@ class TestFileTiff(PillowTestCase):
def test_multipage(self): def test_multipage(self):
# issue #862 # issue #862
im = Image.open('Tests/images/multipage.tiff') im = Image.open('Tests/images/multipage.tiff')
# file is a multipage tiff, 10x10 green, 10x10 red, 20x20 blue # file is a multipage tiff: 10x10 green, 10x10 red, 20x20 blue
im.seek(0) im.seek(0)
self.assertEqual(im.size, (10,10)) self.assertEqual(im.size, (10, 10))
self.assertEqual(im.convert('RGB').getpixel((0,0)), (0,128,0)) self.assertEqual(im.convert('RGB').getpixel((0, 0)), (0, 128, 0))
im.seek(1) im.seek(1)
im.load() im.load()
self.assertEqual(im.size, (10,10)) self.assertEqual(im.size, (10, 10))
self.assertEqual(im.convert('RGB').getpixel((0,0)), (255,0,0)) self.assertEqual(im.convert('RGB').getpixel((0, 0)), (255, 0, 0))
im.seek(2) im.seek(2)
im.load() im.load()
self.assertEqual(im.size, (20,20)) self.assertEqual(im.size, (20, 20))
self.assertEqual(im.convert('RGB').getpixel((0,0)), (0,0,255)) self.assertEqual(im.convert('RGB').getpixel((0, 0)), (0, 0, 255))
def test_multipage_last_frame(self): def test_multipage_last_frame(self):
im = Image.open('Tests/images/multipage-lastframe.tif') im = Image.open('Tests/images/multipage-lastframe.tif')
im.load() im.load()
self.assertEqual(im.size, (20,20)) self.assertEqual(im.size, (20, 20))
self.assertEqual(im.convert('RGB').getpixel((0,0)), (0,0,255)) self.assertEqual(im.convert('RGB').getpixel((0, 0)), (0, 0, 255))
def test___str__(self): def test___str__(self):
# Arrange # Arrange
@ -199,7 +198,6 @@ class TestFileTiff(PillowTestCase):
def test_load_byte(self): def test_load_byte(self):
# Arrange # Arrange
from PIL import TiffImagePlugin
ifd = TiffImagePlugin.ImageFileDirectory() ifd = TiffImagePlugin.ImageFileDirectory()
data = b"abc" data = b"abc"
@ -211,7 +209,6 @@ class TestFileTiff(PillowTestCase):
def test_load_string(self): def test_load_string(self):
# Arrange # Arrange
from PIL import TiffImagePlugin
ifd = TiffImagePlugin.ImageFileDirectory() ifd = TiffImagePlugin.ImageFileDirectory()
data = b"abc\0" data = b"abc\0"
@ -223,7 +220,6 @@ class TestFileTiff(PillowTestCase):
def test_load_float(self): def test_load_float(self):
# Arrange # Arrange
from PIL import TiffImagePlugin
ifd = TiffImagePlugin.ImageFileDirectory() ifd = TiffImagePlugin.ImageFileDirectory()
data = b"abcdabcd" data = b"abcdabcd"
@ -235,7 +231,6 @@ class TestFileTiff(PillowTestCase):
def test_load_double(self): def test_load_double(self):
# Arrange # Arrange
from PIL import TiffImagePlugin
ifd = TiffImagePlugin.ImageFileDirectory() ifd = TiffImagePlugin.ImageFileDirectory()
data = b"abcdefghabcdefgh" data = b"abcdefghabcdefgh"
@ -298,6 +293,19 @@ class TestFileTiff(PillowTestCase):
# Assert # Assert
self.assertEqual(ret, [0, 1]) self.assertEqual(ret, [0, 1])
def test_4bit(self):
# Arrange
test_file = "Tests/images/hopper_gray_4bpp.tif"
original = hopper("L")
# Act
im = Image.open(test_file)
# Assert
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.mode, "L")
self.assert_image_similar(im, original, 7.3)
def test_page_number_x_0(self): def test_page_number_x_0(self):
# Issue 973 # Issue 973