Pillow/src/PIL/XVThumbImagePlugin.py

81 lines
1.9 KiB
Python
Raw Normal View History

2010-07-31 06:52:47 +04:00
#
# The Python Imaging Library.
# $Id$
#
# XV Thumbnail file handler by Charles E. "Gene" Cash
# (gcash@magicnet.net)
#
# see xvcolor.c and xvbrowse.c in the sources to John Bradley's XV,
# available from ftp://ftp.cis.upenn.edu/pub/xv/
#
# history:
# 98-08-15 cec created (b/w only)
# 98-12-09 cec added color palette
# 98-12-28 fl added to PIL (with only a few very minor modifications)
#
# To do:
# FIXME: make save work (this requires quantization support)
#
from . import Image, ImageFile, ImagePalette
from ._binary import o8
_MAGIC = b"P7 332"
2010-07-31 06:52:47 +04:00
# standard color palette for thumbnails (RGB332)
PALETTE = b""
2010-07-31 06:52:47 +04:00
for r in range(8):
for g in range(8):
for b in range(4):
2019-03-21 16:28:20 +03:00
PALETTE = PALETTE + (
o8((r * 255) // 7) + o8((g * 255) // 7) + o8((b * 255) // 3)
)
2010-07-31 06:52:47 +04:00
2016-09-03 05:17:22 +03:00
def _accept(prefix):
return prefix[:6] == _MAGIC
2014-08-28 15:44:19 +04:00
2010-07-31 06:52:47 +04:00
##
# Image plugin for XV thumbnail images.
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
class XVThumbImageFile(ImageFile.ImageFile):
format = "XVThumb"
format_description = "XV thumbnail image"
def _open(self):
# check magic
2017-03-04 21:03:34 +03:00
if not _accept(self.fp.read(6)):
msg = "not an XV thumbnail file"
raise SyntaxError(msg)
2010-07-31 06:52:47 +04:00
# Skip to beginning of next line
self.fp.readline()
# skip info comments
while True:
2010-07-31 06:52:47 +04:00
s = self.fp.readline()
if not s:
msg = "Unexpected EOF reading XV thumbnail file"
raise SyntaxError(msg)
if s[0] != 35: # ie. when not a comment: '#'
2010-07-31 06:52:47 +04:00
break
# parse header line (already read)
s = s.strip().split()
2010-07-31 06:52:47 +04:00
self.mode = "P"
self._size = int(s[0]), int(s[1])
2010-07-31 06:52:47 +04:00
self.palette = ImagePalette.raw("RGB", PALETTE)
2019-03-21 16:28:20 +03:00
self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), (self.mode, 0, 1))]
2010-07-31 06:52:47 +04:00
2010-07-31 06:52:47 +04:00
# --------------------------------------------------------------------
Image.register_open(XVThumbImageFile.format, XVThumbImageFile, _accept)