Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/_binary.py: 88%
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."""
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 si32be(c: bytes, o: int = 0) -> int:
81 """
82 Converts a 4-bytes (32 bits) string to a signed integer, big endian.
84 :param c: string containing bytes to convert
85 :param o: offset of bytes to convert in string
86 """
87 return unpack_from(">i", c, o)[0]
90def i16be(c: bytes, o: int = 0) -> int:
91 return unpack_from(">H", c, o)[0]
94def i32be(c: bytes, o: int = 0) -> int:
95 return unpack_from(">I", c, o)[0]
98# Output, le = little endian, be = big endian
99def o16le(i: int) -> bytes:
100 return pack("<H", i)
103def o32le(i: int) -> bytes:
104 return pack("<I", i)
107def o16be(i: int) -> bytes:
108 return pack(">H", i)
111def o32be(i: int) -> bytes:
112 return pack(">I", i)