mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-03-03 11:35:52 +03:00
Merge pull request #1121 from radarhere/endian
Let Python do the endian stuff + tests
This commit is contained in:
commit
061c9f41d8
|
@ -11,6 +11,8 @@
|
||||||
# See the README file for information on usage and redistribution.
|
# See the README file for information on usage and redistribution.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
from struct import unpack, pack
|
||||||
|
|
||||||
if bytes is str:
|
if bytes is str:
|
||||||
def i8(c):
|
def i8(c):
|
||||||
return ord(c)
|
return ord(c)
|
||||||
|
@ -34,7 +36,7 @@ def i16le(c, o=0):
|
||||||
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 i8(c[o]) | (i8(c[o+1]) << 8)
|
return unpack("<H", c[o:o+2])[0]
|
||||||
|
|
||||||
|
|
||||||
def i32le(c, o=0):
|
def i32le(c, o=0):
|
||||||
|
@ -44,33 +46,31 @@ def i32le(c, o=0):
|
||||||
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 (i8(c[o]) | (i8(c[o+1]) << 8) | (i8(c[o+2]) << 16) |
|
return unpack("<I", c[o:o+4])[0]
|
||||||
(i8(c[o+3]) << 24))
|
|
||||||
|
|
||||||
|
|
||||||
def i16be(c, o=0):
|
def i16be(c, o=0):
|
||||||
return (i8(c[o]) << 8) | i8(c[o+1])
|
return unpack(">H", c[o:o+2])[0]
|
||||||
|
|
||||||
|
|
||||||
def i32be(c, o=0):
|
def i32be(c, o=0):
|
||||||
return ((i8(c[o]) << 24) | (i8(c[o+1]) << 16) |
|
return unpack(">I", c[o:o+4])[0]
|
||||||
(i8(c[o+2]) << 8) | i8(c[o+3]))
|
|
||||||
|
|
||||||
|
|
||||||
# Output, le = little endian, be = big endian
|
# Output, le = little endian, be = big endian
|
||||||
def o16le(i):
|
def o16le(i):
|
||||||
return o8(i) + o8(i >> 8)
|
return pack("<H", i)
|
||||||
|
|
||||||
|
|
||||||
def o32le(i):
|
def o32le(i):
|
||||||
return o8(i) + o8(i >> 8) + o8(i >> 16) + o8(i >> 24)
|
return pack("<I", i)
|
||||||
|
|
||||||
|
|
||||||
def o16be(i):
|
def o16be(i):
|
||||||
return o8(i >> 8) + o8(i)
|
return pack(">H", i)
|
||||||
|
|
||||||
|
|
||||||
def o32be(i):
|
def o32be(i):
|
||||||
return o8(i >> 24) + o8(i >> 16) + o8(i >> 8) + o8(i)
|
return pack(">I", i)
|
||||||
|
|
||||||
# End of file
|
# End of file
|
||||||
|
|
28
Tests/test_binary.py
Normal file
28
Tests/test_binary.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
from helper import unittest, PillowTestCase
|
||||||
|
|
||||||
|
from PIL import _binary
|
||||||
|
|
||||||
|
class TestBinary(PillowTestCase):
|
||||||
|
|
||||||
|
def test_standard(self):
|
||||||
|
self.assertEqual(_binary.i8(b'*'), 42)
|
||||||
|
self.assertEqual(_binary.o8(42), b'*')
|
||||||
|
|
||||||
|
def test_little_endian(self):
|
||||||
|
self.assertEqual(_binary.i16le(b'\xff\xff\x00\x00'), 65535)
|
||||||
|
self.assertEqual(_binary.i32le(b'\xff\xff\x00\x00'), 65535)
|
||||||
|
|
||||||
|
self.assertEqual(_binary.o16le(65535), b'\xff\xff')
|
||||||
|
self.assertEqual(_binary.o32le(65535), b'\xff\xff\x00\x00')
|
||||||
|
|
||||||
|
def test_big_endian(self):
|
||||||
|
self.assertEqual(_binary.i16be(b'\x00\x00\xff\xff'), 0)
|
||||||
|
self.assertEqual(_binary.i32be(b'\x00\x00\xff\xff'), 65535)
|
||||||
|
|
||||||
|
self.assertEqual(_binary.o16be(65535), b'\xff\xff')
|
||||||
|
self.assertEqual(_binary.o32be(65535), b'\x00\x00\xff\xff')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
||||||
|
|
||||||
|
# End of file
|
Loading…
Reference in New Issue
Block a user