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

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# XV Thumbnail file handler by Charles E. "Gene" Cash 

6# (gcash@magicnet.net) 

7# 

8# see xvcolor.c and xvbrowse.c in the sources to John Bradley's XV, 

9# available from ftp://ftp.cis.upenn.edu/pub/xv/ 

10# 

11# history: 

12# 98-08-15 cec created (b/w only) 

13# 98-12-09 cec added color palette 

14# 98-12-28 fl added to PIL (with only a few very minor modifications) 

15# 

16# To do: 

17# FIXME: make save work (this requires quantization support) 

18# 

19 

20from . import Image, ImageFile, ImagePalette 

21from ._binary import o8 

22 

23_MAGIC = b"P7 332" 

24 

25# standard color palette for thumbnails (RGB332) 

26PALETTE = b"" 

27for r in range(8): 

28 for g in range(8): 

29 for b in range(4): 

30 PALETTE = PALETTE + ( 

31 o8((r * 255) // 7) + o8((g * 255) // 7) + o8((b * 255) // 3) 

32 ) 

33 

34 

35def _accept(prefix): 

36 return prefix[:6] == _MAGIC 

37 

38 

39## 

40# Image plugin for XV thumbnail images. 

41 

42 

43class XVThumbImageFile(ImageFile.ImageFile): 

44 

45 format = "XVThumb" 

46 format_description = "XV thumbnail image" 

47 

48 def _open(self): 

49 

50 # check magic 

51 if not _accept(self.fp.read(6)): 

52 raise SyntaxError("not an XV thumbnail file") 

53 

54 # Skip to beginning of next line 

55 self.fp.readline() 

56 

57 # skip info comments 

58 while True: 

59 s = self.fp.readline() 

60 if not s: 

61 raise SyntaxError("Unexpected EOF reading XV thumbnail file") 

62 if s[0] != 35: # ie. when not a comment: '#' 

63 break 

64 

65 # parse header line (already read) 

66 s = s.strip().split() 

67 

68 self.mode = "P" 

69 self._size = int(s[0]), int(s[1]) 

70 

71 self.palette = ImagePalette.raw("RGB", PALETTE) 

72 

73 self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), (self.mode, 0, 1))] 

74 

75 

76# -------------------------------------------------------------------- 

77 

78Image.register_open(XVThumbImageFile.format, XVThumbImageFile, _accept)