Coverage for /usr/lib/python3/dist-packages/PIL/_binary.py: 55%
29 statements
« prev ^ index » next coverage.py v7.4.4, created at 2025-06-14 15:25 +0200
« prev ^ index » next coverage.py v7.4.4, created at 2025-06-14 15:25 +0200
1#
2# The Python Imaging Library.
3# $Id$
4#
5# Binary input/output support routines.
6#
7# Copyright (c) 1997-2003 by Secret Labs AB
8# Copyright (c) 1995-2003 by Fredrik Lundh
9# Copyright (c) 2012 by Brian Crowell
10#
11# See the README file for information on usage and redistribution.
12#
15"""Binary input/output support routines."""
16from __future__ import annotations
18from struct import pack, unpack_from
21def i8(c: bytes) -> int:
22 return c[0]
25def o8(i: int) -> bytes:
26 return bytes((i & 255,))
29# Input, le = little endian, be = big endian
30def i16le(c: bytes, o: int = 0) -> int:
31 """
32 Converts a 2-bytes (16 bits) string to an unsigned integer.
34 :param c: string containing bytes to convert
35 :param o: offset of bytes to convert in string
36 """
37 return unpack_from("<H", c, o)[0]
40def si16le(c: bytes, o: int = 0) -> int:
41 """
42 Converts a 2-bytes (16 bits) string to a signed integer.
44 :param c: string containing bytes to convert
45 :param o: offset of bytes to convert in string
46 """
47 return unpack_from("<h", c, o)[0]
50def si16be(c: bytes, o: int = 0) -> int:
51 """
52 Converts a 2-bytes (16 bits) string to a signed integer, big endian.
54 :param c: string containing bytes to convert
55 :param o: offset of bytes to convert in string
56 """
57 return unpack_from(">h", c, o)[0]
60def i32le(c: bytes, o: int = 0) -> int:
61 """
62 Converts a 4-bytes (32 bits) string to an unsigned integer.
64 :param c: string containing bytes to convert
65 :param o: offset of bytes to convert in string
66 """
67 return unpack_from("<I", c, o)[0]
70def si32le(c: bytes, o: int = 0) -> int:
71 """
72 Converts a 4-bytes (32 bits) string to a signed integer.
74 :param c: string containing bytes to convert
75 :param o: offset of bytes to convert in string
76 """
77 return unpack_from("<i", c, o)[0]
80def i16be(c: bytes, o: int = 0) -> int:
81 return unpack_from(">H", c, o)[0]
84def i32be(c: bytes, o: int = 0) -> int:
85 return unpack_from(">I", c, o)[0]
88# Output, le = little endian, be = big endian
89def o16le(i: int) -> bytes:
90 return pack("<H", i)
93def o32le(i: int) -> bytes:
94 return pack("<I", i)
97def o16be(i: int) -> bytes:
98 return pack(">H", i)
101def o32be(i: int) -> bytes:
102 return pack(">I", i)