发布于 2015-09-11 16:19:10 | 201 次阅读 | 评论: 0 | 来源: 网络整理

# -*- coding: iso-8859-1 -*-
#
# The Python Imaging Library.
# $Id$
#
# WAL file handling
#
# History:
# 2003-04-23 fl   created
#
# Copyright (c) 2003 by Fredrik Lundh.
#
# See the README file for information on usage and redistribution.
#

# NOTE: This format cannot be automatically recognized, so the reader
# is not registered for use with Image.open().  To open a WEL file, use
# the WalImageFile.open() function instead.

# This reader is based on the specification available from:
#    http://www.flipcode.com/tutorials/tut_q2levels.shtml
# and has been tested with a few sample files found using google.

from __future__ import print_function

from PIL import Image, _binary

try:
    import builtins
except ImportError:
    import __builtin__
    builtins = __builtin__

i32 = _binary.i32le

##
# Load texture from a Quake2 WAL texture file.
# <p>
# By default, a Quake2 standard palette is attached to the texture.
# To override the palette, use the <b>putpalette</b> method.
#
# @param filename WAL file name, or an opened file handle.
# @return An image instance.

[docs]def open(filename): # FIXME: modify to return a WalImageFile instance instead of # plain Image object ? if hasattr(filename, "read"): fp = filename else: fp = builtins.open(filename, "rb") # read header fields header = fp.read(32+24+32+12) size = i32(header, 32), i32(header, 36) offset = i32(header, 40) # load pixel data fp.seek(offset) im = Image.frombytes("P", size, fp.read(size[0] * size[1])) im.putpalette(quake2palette) im.format = "WAL" im.format_description = "Quake2 Texture" # strings are null-terminated im.info["name"] = header[:32].split(b"", 1)[0] next_name = header[56:56+32].split(b"", 1)[0] if next_name: im.info["next_name"] = next_name return im
quake2palette = ( # default palette taken from piffo 0.93 by Hans Häggström b"x01x01x01x0bx0bx0bx12x12x12x17x17x17x1bx1bx1bx1e" b"x1ex1ex22x22x22x26x26x26x29x29x29x2cx2cx2cx2fx2f" b"x2fx32x32x32x35x35x35x37x37x37x3ax3ax3ax3cx3cx3c" b"x24x1ex13x22x1cx12x20x1bx12x1fx1ax10x1dx19x10x1b" b"x17x0fx1ax16x0fx18x14x0dx17x13x0dx16x12x0dx14x10" b"x0bx13x0fx0bx10x0dx0ax0fx0bx0ax0dx0bx07x0bx0ax07" b"x23x23x26x22x22x25x22x20x23x21x1fx22x20x1ex20x1f" b"x1dx1ex1dx1bx1cx1bx1ax1ax1ax19x19x18x17x17x17x16" b"x16x14x14x14x13x13x13x10x10x10x0fx0fx0fx0dx0dx0d" b"x2dx28x20x29x24x1cx27x22x1ax25x1fx17x38x2ex1ex31" b"x29x1ax2cx25x17x26x20x14x3cx30x14x37x2cx13x33x28" b"x12x2dx24x10x28x1fx0fx22x1ax0bx1bx14x0ax13x0fx07" b"x31x1ax16x30x17x13x2ex16x10x2cx14x0dx2ax12x0bx27" b"x0fx0ax25x0fx07x21x0dx01x1ex0bx01x1cx0bx01x1ax0b" b"x01x18x0ax01x16x0ax01x13x0ax01x10x07x01x0dx07x01" b"x29x23x1ex27x21x1cx26x20x1bx25x1fx1ax23x1dx19x21" b"x1cx18x20x1bx17x1ex19x16x1cx18x14x1bx17x13x19x14" b"x10x17x13x0fx14x10x0dx12x0fx0bx0fx0bx0ax0bx0ax07" b"x26x1ax0fx23x19x0fx20x17x0fx1cx16x0fx19x13x0dx14" b"x10x0bx10x0dx0ax0bx0ax07x33x22x1fx35x29x26x37x2f" b"x2dx39x35x34x37x39x3ax33x37x39x30x34x36x2bx31x34" b"x27x2ex31x22x2bx2fx1dx28x2cx17x25x2ax0fx20x26x0d" b"x1ex25x0bx1cx22x0ax1bx20x07x19x1ex07x17x1bx07x14" b"x18x01x12x16x01x0fx12x01x0bx0dx01x07x0ax01x01x01" b"x2cx21x21x2ax1fx1fx29x1dx1dx27x1cx1cx26x1ax1ax24" b"x18x18x22x17x17x21x16x16x1ex13x13x1bx12x12x18x10" b"x10x16x0dx0dx12x0bx0bx0dx0ax0ax0ax07x07x01x01x01" b"x2ex30x29x2dx2ex27x2bx2cx26x2ax2ax24x28x29x23x27" b"x27x21x26x26x1fx24x24x1dx22x22x1cx1fx1fx1ax1cx1c" b"x18x19x19x16x17x17x13x13x13x10x0fx0fx0dx0bx0bx0a" b"x30x1ex1bx2dx1cx19x2cx1ax17x2ax19x14x28x17x13x26" b"x16x10x24x13x0fx21x12x0dx1fx10x0bx1cx0fx0ax19x0d" b"x0ax16x0bx07x12x0ax07x0fx07x01x0ax01x01x01x01x01" b"x28x29x38x26x27x36x25x26x34x24x24x31x22x22x2fx20" b"x21x2dx1ex1fx2ax1dx1dx27x1bx1bx25x19x19x21x17x17" b"x1ex14x14x1bx13x12x17x10x0fx13x0dx0bx0fx0ax07x07" b"x2fx32x29x2dx30x26x2bx2ex24x29x2cx21x27x2ax1ex25" b"x28x1cx23x26x1ax21x25x18x1ex22x14x1bx1fx10x19x1c" b"x0dx17x1ax0ax13x17x07x10x13x01x0dx0fx01x0ax0bx01" b"x01x3fx01x13x3cx0bx1bx39x10x20x35x14x23x31x17x23" b"x2dx18x23x29x18x3fx3fx3fx3fx3fx39x3fx3fx31x3fx3f" b"x2ax3fx3fx20x3fx3fx14x3fx3cx12x3fx39x0fx3fx35x0b" b"x3fx32x07x3fx2dx01x3dx2ax01x3bx26x01x39x21x01x37" b"x1dx01x34x1ax01x32x16x01x2fx12x01x2dx0fx01x2ax0b" b"x01x27x07x01x23x01x01x1dx01x01x17x01x01x10x01x01" b"x3dx01x01x19x19x3fx3fx01x01x01x01x3fx16x16x13x10" b"x10x0fx0dx0dx0bx3cx2ex2ax36x27x20x30x21x18x29x1b" b"x10x3cx39x37x37x32x2fx31x2cx28x2bx26x21x30x22x20" ) if __name__ == "__main__": im = open("../hacks/sample.wal") print(im.info, im.mode, im.size) im.save("../out.png")
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务