Pillow/src/PIL/_binary.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
2.2 KiB
Python
Raw Normal View History

#
# The Python Imaging Library.
# $Id$
#
# Binary input/output support routines.
#
# Copyright (c) 1997-2003 by Secret Labs AB
# Copyright (c) 1995-2003 by Fredrik Lundh
# Copyright (c) 2012 by Brian Crowell
#
# See the README file for information on usage and redistribution.
#
"""Binary input/output support routines."""
from __future__ import annotations
from struct import pack, unpack_from
2015-01-12 01:29:47 +03:00
2023-12-30 01:18:08 +03:00
def i8(c: bytes) -> int:
return c[0]
2019-03-21 16:28:20 +03:00
2023-12-30 01:15:41 +03:00
def o8(i: int) -> bytes:
2019-09-26 15:12:28 +03:00
return bytes((i & 255,))
2014-08-26 17:47:10 +04:00
# Input, le = little endian, be = big endian
2023-12-30 01:15:41 +03:00
def i16le(c: bytes, o: int = 0) -> int:
"""
Converts a 2-bytes (16 bits) string to an unsigned integer.
:param c: string containing bytes to convert
:param o: offset of bytes to convert in string
"""
return unpack_from("<H", c, o)[0]
2014-08-26 17:47:10 +04:00
2017-04-20 14:14:23 +03:00
2023-12-30 01:15:41 +03:00
def si16le(c: bytes, o: int = 0) -> int:
"""
Converts a 2-bytes (16 bits) string to a signed integer.
:param c: string containing bytes to convert
:param o: offset of bytes to convert in string
"""
return unpack_from("<h", c, o)[0]
2023-12-30 01:15:41 +03:00
def si16be(c: bytes, o: int = 0) -> int:
2021-07-15 12:38:26 +03:00
"""
Converts a 2-bytes (16 bits) string to a signed integer, big endian.
:param c: string containing bytes to convert
:param o: offset of bytes to convert in string
"""
return unpack_from(">h", c, o)[0]
2023-12-30 01:15:41 +03:00
def i32le(c: bytes, o: int = 0) -> int:
"""
Converts a 4-bytes (32 bits) string to an unsigned integer.
:param c: string containing bytes to convert
:param o: offset of bytes to convert in string
"""
return unpack_from("<I", c, o)[0]
2014-08-26 17:47:10 +04:00
2017-04-20 14:14:23 +03:00
2023-12-30 01:15:41 +03:00
def si32le(c: bytes, o: int = 0) -> int:
"""
Converts a 4-bytes (32 bits) string to a signed integer.
:param c: string containing bytes to convert
:param o: offset of bytes to convert in string
"""
return unpack_from("<i", c, o)[0]
2023-12-30 01:15:41 +03:00
def i16be(c: bytes, o: int = 0) -> int:
return unpack_from(">H", c, o)[0]
2014-08-26 17:47:10 +04:00
2023-12-30 01:15:41 +03:00
def i32be(c: bytes, o: int = 0) -> int:
return unpack_from(">I", c, o)[0]
2014-08-26 17:47:10 +04:00
# Output, le = little endian, be = big endian
2023-12-30 01:15:41 +03:00
def o16le(i: int) -> bytes:
2015-01-12 01:29:47 +03:00
return pack("<H", i)
2014-08-26 17:47:10 +04:00
2023-12-30 01:15:41 +03:00
def o32le(i: int) -> bytes:
2015-01-12 01:29:47 +03:00
return pack("<I", i)
2014-08-26 17:47:10 +04:00
2023-12-30 01:15:41 +03:00
def o16be(i: int) -> bytes:
2015-01-12 01:29:47 +03:00
return pack(">H", i)
2014-08-26 17:47:10 +04:00
2023-12-30 01:15:41 +03:00
def o32be(i: int) -> bytes:
2015-01-12 01:29:47 +03:00
return pack(">I", i)