Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/_binary.py: 55%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
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."""
17from __future__ import annotations
19__lazy_modules__ = {"struct"}
21from struct import pack, unpack_from
24def i8(c: bytes) -> int:
25 return c[0]
28def o8(i: int) -> bytes:
29 return bytes((i & 255,))
32# Input, le = little endian, be = big endian
33def i16le(c: bytes, o: int = 0) -> int:
34 """
35 Converts a 2-bytes (16 bits) string to an unsigned integer.
37 :param c: string containing bytes to convert
38 :param o: offset of bytes to convert in string
39 """
40 return unpack_from("<H", c, o)[0]
43def si16le(c: bytes, o: int = 0) -> int:
44 """
45 Converts a 2-bytes (16 bits) string to a signed integer.
47 :param c: string containing bytes to convert
48 :param o: offset of bytes to convert in string
49 """
50 return unpack_from("<h", c, o)[0]
53def si16be(c: bytes, o: int = 0) -> int:
54 """
55 Converts a 2-bytes (16 bits) string to a signed integer, big endian.
57 :param c: string containing bytes to convert
58 :param o: offset of bytes to convert in string
59 """
60 return unpack_from(">h", c, o)[0]
63def i32le(c: bytes, o: int = 0) -> int:
64 """
65 Converts a 4-bytes (32 bits) string to an unsigned integer.
67 :param c: string containing bytes to convert
68 :param o: offset of bytes to convert in string
69 """
70 return unpack_from("<I", c, o)[0]
73def si32le(c: bytes, o: int = 0) -> int:
74 """
75 Converts a 4-bytes (32 bits) string to a signed integer.
77 :param c: string containing bytes to convert
78 :param o: offset of bytes to convert in string
79 """
80 return unpack_from("<i", c, o)[0]
83def si32be(c: bytes, o: int = 0) -> int:
84 """
85 Converts a 4-bytes (32 bits) string to a signed integer, big endian.
87 :param c: string containing bytes to convert
88 :param o: offset of bytes to convert in string
89 """
90 return unpack_from(">i", c, o)[0]
93def i16be(c: bytes, o: int = 0) -> int:
94 return unpack_from(">H", c, o)[0]
97def i32be(c: bytes, o: int = 0) -> int:
98 return unpack_from(">I", c, o)[0]
101# Output, le = little endian, be = big endian
102def o16le(i: int) -> bytes:
103 return pack("<H", i)
106def o32le(i: int) -> bytes:
107 return pack("<I", i)
110def o16be(i: int) -> bytes:
111 return pack(">H", i)
114def o32be(i: int) -> bytes:
115 return pack(">I", i)