mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-17 18:54:46 +03:00
WMF/EMF: Unpack signed integers using unpack()
This commit is contained in:
parent
a568e46899
commit
c7faca7e0f
|
@ -14,6 +14,10 @@
|
||||||
#
|
#
|
||||||
# See the README file for information on usage and redistribution.
|
# See the README file for information on usage and redistribution.
|
||||||
#
|
#
|
||||||
|
# WMF/EMF reference documentation:
|
||||||
|
# https://winprotocoldoc.blob.core.windows.net/productionwindowsarchives/MS-WMF/[MS-WMF].pdf
|
||||||
|
# http://wvware.sourceforge.net/caolan/index.html
|
||||||
|
# http://wvware.sourceforge.net/caolan/ora-wmf.html
|
||||||
|
|
||||||
from PIL import Image, ImageFile, _binary
|
from PIL import Image, ImageFile, _binary
|
||||||
|
|
||||||
|
@ -56,21 +60,9 @@ if hasattr(Image.core, "drawwmf"):
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
|
|
||||||
word = _binary.i16le
|
word = _binary.i16le
|
||||||
|
short = _binary.si16le
|
||||||
|
|
||||||
def short(c, o=0):
|
|
||||||
v = word(c, o)
|
|
||||||
if v >= 32768:
|
|
||||||
v -= 65536
|
|
||||||
return v
|
|
||||||
|
|
||||||
dword = _binary.i32le
|
dword = _binary.i32le
|
||||||
|
_long = _binary.si32le
|
||||||
def _long(c, o=0):
|
|
||||||
v = dword(c, o)
|
|
||||||
if v >= 1<<31:
|
|
||||||
v -= 1<<32
|
|
||||||
return v
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
|
@ -132,7 +124,7 @@ class WmfStubImageFile(ImageFile.StubImageFile):
|
||||||
y1 = _long(s, 20)
|
y1 = _long(s, 20)
|
||||||
|
|
||||||
# get frame (in 0.01 millimeter units)
|
# get frame (in 0.01 millimeter units)
|
||||||
frame = dword(s, 24), dword(s, 28), dword(s, 32), dword(s, 36)
|
frame = _long(s, 24), _long(s, 28), _long(s, 32), _long(s, 36)
|
||||||
|
|
||||||
# normalize size to 72 dots per inch
|
# normalize size to 72 dots per inch
|
||||||
size = x1 - x0, y1 - y0
|
size = x1 - x0, y1 - y0
|
||||||
|
|
|
@ -31,23 +31,41 @@ else:
|
||||||
# TODO: replace with more readable struct.unpack equivalent
|
# TODO: replace with more readable struct.unpack equivalent
|
||||||
def i16le(c, o=0):
|
def i16le(c, o=0):
|
||||||
"""
|
"""
|
||||||
Converts a 2-bytes (16 bits) string to an integer.
|
Converts a 2-bytes (16 bits) string to an unsigned integer.
|
||||||
|
|
||||||
c: string containing bytes to convert
|
c: string containing bytes to convert
|
||||||
o: offset of bytes to convert in string
|
o: offset of bytes to convert in string
|
||||||
"""
|
"""
|
||||||
return unpack("<H", c[o:o+2])[0]
|
return unpack("<H", c[o:o+2])[0]
|
||||||
|
|
||||||
|
def si16le(c, o=0):
|
||||||
|
"""
|
||||||
|
Converts a 2-bytes (16 bits) string to a signed integer.
|
||||||
|
|
||||||
|
c: string containing bytes to convert
|
||||||
|
o: offset of bytes to convert in string
|
||||||
|
"""
|
||||||
|
return unpack("<h", c[o:o+2])[0]
|
||||||
|
|
||||||
|
|
||||||
def i32le(c, o=0):
|
def i32le(c, o=0):
|
||||||
"""
|
"""
|
||||||
Converts a 4-bytes (32 bits) string to an integer.
|
Converts a 4-bytes (32 bits) string to an unsigned integer.
|
||||||
|
|
||||||
c: string containing bytes to convert
|
c: string containing bytes to convert
|
||||||
o: offset of bytes to convert in string
|
o: offset of bytes to convert in string
|
||||||
"""
|
"""
|
||||||
return unpack("<I", c[o:o+4])[0]
|
return unpack("<I", c[o:o+4])[0]
|
||||||
|
|
||||||
|
def si32le(c, o=0):
|
||||||
|
"""
|
||||||
|
Converts a 4-bytes (32 bits) string to a signed integer.
|
||||||
|
|
||||||
|
c: string containing bytes to convert
|
||||||
|
o: offset of bytes to convert in string
|
||||||
|
"""
|
||||||
|
return unpack("<i", c[o:o+4])[0]
|
||||||
|
|
||||||
|
|
||||||
def i16be(c, o=0):
|
def i16be(c, o=0):
|
||||||
return unpack(">H", c[o:o+2])[0]
|
return unpack(">H", c[o:o+2])[0]
|
||||||
|
|
BIN
Tests/images/drawing_emf_ref.png
Normal file
BIN
Tests/images/drawing_emf_ref.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 20 KiB |
BIN
Tests/images/drawing_wmf_ref.png
Normal file
BIN
Tests/images/drawing_wmf_ref.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 531 B |
|
@ -6,11 +6,25 @@ class TestFileWmf(PillowTestCase):
|
||||||
|
|
||||||
def test_load_raw(self):
|
def test_load_raw(self):
|
||||||
|
|
||||||
# Test basic EMF open
|
# Test basic EMF open and rendering
|
||||||
im = Image.open('Tests/images/drawing.emf')
|
im = Image.open('Tests/images/drawing.emf')
|
||||||
|
if hasattr(Image.core, "drawwmf"):
|
||||||
|
# Currently, support for WMF/EMF is Windows-only
|
||||||
|
im.load()
|
||||||
|
# Compare to reference rendering
|
||||||
|
imref = Image.open('Tests/images/drawing_emf_ref.png')
|
||||||
|
imref.load()
|
||||||
|
self.assert_image_equal(im, imref)
|
||||||
|
|
||||||
# Test basic WMF open
|
# Test basic WMF open and rendering
|
||||||
im = Image.open('Tests/images/drawing.wmf')
|
im = Image.open('Tests/images/drawing.wmf')
|
||||||
|
if hasattr(Image.core, "drawwmf"):
|
||||||
|
# Currently, support for WMF/EMF is Windows-only
|
||||||
|
im.load()
|
||||||
|
# Compare to reference rendering
|
||||||
|
imref = Image.open('Tests/images/drawing_wmf_ref.png')
|
||||||
|
imref.load()
|
||||||
|
self.assert_image_equal(im, imref)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in New Issue
Block a user