Merge pull request #7659 from nulano/types-binary

This commit is contained in:
Hugo van Kemenade 2023-12-31 16:56:16 +02:00 committed by GitHub
commit 6282caf3f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 20 deletions

View File

@ -20,26 +20,29 @@ import os
import tempfile
from . import Image, ImageFile
from ._binary import i8, o8
from ._binary import i16be as i16
from ._binary import i32be as i32
COMPRESSION = {1: "raw", 5: "jpeg"}
PAD = o8(0) * 4
PAD = b"\0\0\0\0"
#
# Helpers
def _i8(c: int | bytes) -> int:
return c if isinstance(c, int) else c[0]
def i(c):
return i32((PAD + c)[-4:])
def dump(c):
for i in c:
print("%02x" % i8(i), end=" ")
print("%02x" % _i8(i), end=" ")
print()
@ -103,10 +106,10 @@ class IptcImageFile(ImageFile.ImageFile):
self.info[tag] = tagdata
# mode
layers = i8(self.info[(3, 60)][0])
component = i8(self.info[(3, 60)][1])
layers = self.info[(3, 60)][0]
component = self.info[(3, 60)][1]
if (3, 65) in self.info:
id = i8(self.info[(3, 65)][0]) - 1
id = self.info[(3, 65)][0] - 1
else:
id = 0
if layers == 1 and not component:

View File

@ -18,16 +18,16 @@ from __future__ import annotations
from struct import pack, unpack_from
def i8(c) -> int:
return c if c.__class__ is int else c[0]
def i8(c: bytes) -> int:
return c[0]
def o8(i):
def o8(i: int) -> bytes:
return bytes((i & 255,))
# Input, le = little endian, be = big endian
def i16le(c, o=0):
def i16le(c: bytes, o: int = 0) -> int:
"""
Converts a 2-bytes (16 bits) string to an unsigned integer.
@ -37,7 +37,7 @@ def i16le(c, o=0):
return unpack_from("<H", c, o)[0]
def si16le(c, o=0):
def si16le(c: bytes, o: int = 0) -> int:
"""
Converts a 2-bytes (16 bits) string to a signed integer.
@ -47,7 +47,7 @@ def si16le(c, o=0):
return unpack_from("<h", c, o)[0]
def si16be(c, o=0):
def si16be(c: bytes, o: int = 0) -> int:
"""
Converts a 2-bytes (16 bits) string to a signed integer, big endian.
@ -57,7 +57,7 @@ def si16be(c, o=0):
return unpack_from(">h", c, o)[0]
def i32le(c, o=0) -> int:
def i32le(c: bytes, o: int = 0) -> int:
"""
Converts a 4-bytes (32 bits) string to an unsigned integer.
@ -67,7 +67,7 @@ def i32le(c, o=0) -> int:
return unpack_from("<I", c, o)[0]
def si32le(c, o=0):
def si32le(c: bytes, o: int = 0) -> int:
"""
Converts a 4-bytes (32 bits) string to a signed integer.
@ -77,26 +77,26 @@ def si32le(c, o=0):
return unpack_from("<i", c, o)[0]
def i16be(c, o=0):
def i16be(c: bytes, o: int = 0) -> int:
return unpack_from(">H", c, o)[0]
def i32be(c, o=0):
def i32be(c: bytes, o: int = 0) -> int:
return unpack_from(">I", c, o)[0]
# Output, le = little endian, be = big endian
def o16le(i):
def o16le(i: int) -> bytes:
return pack("<H", i)
def o32le(i):
def o32le(i: int) -> bytes:
return pack("<I", i)
def o16be(i) -> bytes:
def o16be(i: int) -> bytes:
return pack(">H", i)
def o32be(i):
def o32be(i: int) -> bytes:
return pack(">I", i)