openmedialibrary_platform/Darwin/lib/python3.5/site-packages/PIL/XVThumbImagePlugin.py

76 lines
1.8 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00: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)
#
2014-09-30 16:15:32 +00:00
from PIL import Image, ImageFile, ImagePalette, _binary
2015-11-25 01:25:01 +00:00
__version__ = "0.1"
2014-09-30 16:15:32 +00:00
o8 = _binary.o8
2013-10-11 17:28:32 +00:00
# standard color palette for thumbnails (RGB332)
2014-09-30 16:15:32 +00:00
PALETTE = b""
2013-10-11 17:28:32 +00:00
for r in range(8):
for g in range(8):
for b in range(4):
2014-09-30 16:15:32 +00:00
PALETTE = PALETTE + (o8((r*255)//7)+o8((g*255)//7)+o8((b*255)//3))
2013-10-11 17:28:32 +00:00
2015-11-25 01:25:01 +00:00
2013-10-11 17:28:32 +00:00
##
# Image plugin for XV thumbnail images.
class XVThumbImageFile(ImageFile.ImageFile):
format = "XVThumb"
format_description = "XV thumbnail image"
def _open(self):
# check magic
s = self.fp.read(6)
2014-09-30 16:15:32 +00:00
if s != b"P7 332":
raise SyntaxError("not an XV thumbnail file")
2013-10-11 17:28:32 +00:00
# Skip to beginning of next line
self.fp.readline()
# skip info comments
2014-09-30 16:15:32 +00:00
while True:
2013-10-11 17:28:32 +00:00
s = self.fp.readline()
if not s:
2014-09-30 16:15:32 +00:00
raise SyntaxError("Unexpected EOF reading XV thumbnail file")
if s[0] != b'#':
2013-10-11 17:28:32 +00:00
break
# parse header line (already read)
2014-09-30 16:15:32 +00:00
s = s.strip().split()
2013-10-11 17:28:32 +00:00
self.mode = "P"
2014-09-30 16:15:32 +00:00
self.size = int(s[0:1]), int(s[1:2])
2013-10-11 17:28:32 +00:00
self.palette = ImagePalette.raw("RGB", PALETTE)
self.tile = [
("raw", (0, 0)+self.size,
self.fp.tell(), (self.mode, 0, 1)
)]
# --------------------------------------------------------------------
Image.register_open("XVThumb", XVThumbImageFile)