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

33 statements  

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# 

13 

14 

15"""Binary input/output support routines.""" 

16 

17from __future__ import annotations 

18 

19__lazy_modules__ = {"struct"} 

20 

21from struct import unpack_from 

22 

23 

24def i8(c: bytes) -> int: 

25 return c[0] 

26 

27 

28def o8(i: int) -> bytes: 

29 return (i & 255).to_bytes() 

30 

31 

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. 

36 

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] 

41 

42 

43def si16le(c: bytes, o: int = 0) -> int: 

44 """ 

45 Converts a 2-bytes (16 bits) string to a signed integer. 

46 

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] 

51 

52 

53def si16be(c: bytes, o: int = 0) -> int: 

54 """ 

55 Converts a 2-bytes (16 bits) string to a signed integer, big endian. 

56 

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] 

61 

62 

63def i32le(c: bytes, o: int = 0) -> int: 

64 """ 

65 Converts a 4-bytes (32 bits) string to an unsigned integer. 

66 

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] 

71 

72 

73def si32le(c: bytes, o: int = 0) -> int: 

74 """ 

75 Converts a 4-bytes (32 bits) string to a signed integer. 

76 

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] 

81 

82 

83def si32be(c: bytes, o: int = 0) -> int: 

84 """ 

85 Converts a 4-bytes (32 bits) string to a signed integer, big endian. 

86 

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] 

91 

92 

93def i16be(c: bytes, o: int = 0) -> int: 

94 return unpack_from(">H", c, o)[0] 

95 

96 

97def i32be(c: bytes, o: int = 0) -> int: 

98 return unpack_from(">I", c, o)[0] 

99 

100 

101# Output, le = little endian, be = big endian 

102def o16le(i: int) -> bytes: 

103 return i.to_bytes(2, "little") 

104 

105 

106def o32le(i: int) -> bytes: 

107 return i.to_bytes(4, "little") 

108 

109 

110def o16be(i: int) -> bytes: 

111 return i.to_bytes(2, "big") 

112 

113 

114def o32be(i: int) -> bytes: 

115 return i.to_bytes(4, "big")