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

32 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.""" 

16from __future__ import annotations 

17 

18from struct import pack, unpack_from 

19 

20 

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

22 return c[0] 

23 

24 

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

26 return bytes((i & 255,)) 

27 

28 

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. 

33 

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] 

38 

39 

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

41 """ 

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

43 

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] 

48 

49 

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

51 """ 

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

53 

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] 

58 

59 

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

61 """ 

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

63 

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] 

68 

69 

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

71 """ 

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

73 

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] 

78 

79 

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

81 """ 

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

83 

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] 

88 

89 

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

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

92 

93 

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

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

96 

97 

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

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

100 return pack("<H", i) 

101 

102 

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

104 return pack("<I", i) 

105 

106 

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

108 return pack(">H", i) 

109 

110 

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

112 return pack(">I", i)