Skip to content

Commit eafbb39

Browse files
authored
Merge pull request #1817 from radarhere/gd
Added GD tests
2 parents 9e5494e + 6b224be commit eafbb39

File tree

4 files changed

+44
-24
lines changed

4 files changed

+44
-24
lines changed

Tests/images/hopper.gd

17 KB
Binary file not shown.

Tests/test_file_gd.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from helper import unittest, PillowTestCase
2+
3+
from PIL import GdImageFile
4+
5+
import io
6+
7+
TEST_GD_FILE = "Tests/images/hopper.gd"
8+
9+
10+
class TestFileGd(PillowTestCase):
11+
12+
def test_sanity(self):
13+
im = GdImageFile.open(TEST_GD_FILE)
14+
self.assertEqual(im.size, (128, 128))
15+
self.assertEqual(im.format, "GD")
16+
17+
def test_bad_mode(self):
18+
self.assertRaises(ValueError,
19+
GdImageFile.open, TEST_GD_FILE, 'bad mode')
20+
21+
def test_invalid_file(self):
22+
invalid_file = "Tests/images/flower.jpg"
23+
24+
self.assertRaises(IOError, GdImageFile.open, invalid_file)
25+
26+
27+
if __name__ == '__main__':
28+
unittest.main()

docs/handbook/image-file-formats.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,8 @@ The :py:meth:`~PIL.Image.Image.open` method sets the following
863863
GD
864864
^^
865865

866-
PIL reads uncompressed GD files. Note that this file format cannot be
867-
automatically identified, so you must use :py:func:`PIL.GdImageFile.open` to
868-
read such a file.
866+
PIL reads uncompressed GD2 files. Note that you must use
867+
:py:func:`PIL.GdImageFile.open` to read such a file.
869868

870869
The :py:meth:`~PIL.Image.Image.open` method sets the following
871870
:py:attr:`~PIL.Image.Image.info` properties:

src/PIL/GdImageFile.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,10 @@
2424

2525

2626
from . import ImageFile, ImagePalette
27-
from ._binary import i16be as i16
28-
from ._util import isPath
27+
from ._binary import i8, i16be as i16, i32be as i32
2928

3029
__version__ = "0.1"
3130

32-
try:
33-
import builtins
34-
except ImportError:
35-
import __builtin__
36-
builtins = __builtin__
37-
3831

3932
##
4033
# Image plugin for the GD uncompressed format. Note that this format
@@ -50,19 +43,25 @@ class GdImageFile(ImageFile.ImageFile):
5043
def _open(self):
5144

5245
# Header
53-
s = self.fp.read(775)
46+
s = self.fp.read(1037)
47+
48+
if not i16(s[:2]) in [65534, 65535]:
49+
raise SyntaxError("Not a valid GD 2.x .gd file")
5450

5551
self.mode = "L" # FIXME: "P"
56-
self.size = i16(s[0:2]), i16(s[2:4])
52+
self.size = i16(s[2:4]), i16(s[4:6])
53+
54+
trueColor = i8(s[6])
55+
trueColorOffset = 2 if trueColor else 0
5756

5857
# transparency index
59-
tindex = i16(s[5:7])
58+
tindex = i32(s[7+trueColorOffset:7+trueColorOffset+4])
6059
if tindex < 256:
61-
self.info["transparent"] = tindex
60+
self.info["transparency"] = tindex
6261

63-
self.palette = ImagePalette.raw("RGB", s[7:])
62+
self.palette = ImagePalette.raw("XBGR", s[7+trueColorOffset+4:7+trueColorOffset+4+256*4])
6463

65-
self.tile = [("raw", (0, 0)+self.size, 775, ("L", 0, -1))]
64+
self.tile = [("raw", (0, 0)+self.size, 7+trueColorOffset+4+256*4, ("L", 0, 1))]
6665

6766

6867
def open(fp, mode="r"):
@@ -78,13 +77,7 @@ def open(fp, mode="r"):
7877
if mode != "r":
7978
raise ValueError("bad mode")
8079

81-
if isPath(fp):
82-
filename = fp
83-
fp = builtins.open(fp, "rb")
84-
else:
85-
filename = ""
86-
8780
try:
88-
return GdImageFile(fp, filename)
81+
return GdImageFile(fp)
8982
except SyntaxError:
9083
raise IOError("cannot identify this image file")

0 commit comments

Comments
 (0)