Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/_binary.py: 68%

28 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

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 

17 

18from struct import pack, unpack_from 

19 

20 

21def i8(c): 

22 return c if c.__class__ is int else c[0] 

23 

24 

25def o8(i): 

26 return bytes((i & 255,)) 

27 

28 

29# Input, le = little endian, be = big endian 

30def i16le(c, o=0): 

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, o=0): 

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, o=0): 

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, o=0): 

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, o=0): 

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 i16be(c, o=0): 

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

82 

83 

84def i32be(c, o=0): 

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

86 

87 

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

89def o16le(i): 

90 return pack("<H", i) 

91 

92 

93def o32le(i): 

94 return pack("<I", i) 

95 

96 

97def o16be(i): 

98 return pack(">H", i) 

99 

100 

101def o32be(i): 

102 return pack(">I", i)