TgaForPIL

From X-Plugins

Jump to: navigation, search

TgaImagePlugin.py

Sytanx Highlight by GeSHI! Media Wiki extension: http://www.mediawiki.org/wiki/Extension:SyntaxHighlight_GeSHi

#
# The Python Imaging Library.
# $Id: TgaImagePlugin.py 2134 2004-10-06 08:55:20Z fredrik $
#
# TGA file handling
#
# History:
# 95-09-01 fl   created (reads 24-bit files only)
# 97-01-04 fl   support more TGA versions, including compressed images
# 98-07-04 fl   fixed orientation and alpha layer bugs
# 98-09-11 fl   fixed orientation for runlength decoder
#
# 09-04-25         added basic support for SAVING tga files, supports unencoded 24 or 32bit - Ben Russell, br@x-plugins.com
#
# Copyright (c) Secret Labs AB 1997-98.
# Copyright (c) Fredrik Lundh 1995-97.
#
# See the README file for information on usage and redistribution.
#
 
 
__version__ = "0.3"
 
import Image, ImageFile, ImagePalette
 
 
def i16(c):
    return ord(c[0]) + (ord(c[1])<<8)
 
def i32(c):
    return ord(c[0]) + (ord(c[1])<<8) + (ord(c[2])<<16) + (ord(c[3])<<24)
 
 
MODES = {
    # map imagetype/depth to rawmode
    (1, 8):  "P",
    (3, 1):  "1",
    (3, 8):  "L",
    (2, 16): "BGR;5",
    (2, 24): "BGR",
    (2, 32): "BGRA",
}
 
 
def _accept(prefix):
    return prefix[0] == "\0"
 
##
# Image plugin for Targa files.
 
class TgaImageFile(ImageFile.ImageFile):
 
    format = "TGA"
    format_description = "Targa"
 
    def _open(self):
 
        # process header
        s = self.fp.read(18)
 
        id = ord(s[0])
 
        colormaptype = ord(s[1])
        imagetype = ord(s[2])
 
        depth = ord(s[16])
 
        flags = ord(s[17])
 
        self.size = i16(s[12:]), i16(s[14:])
 
        # validate header fields
        if id != 0 or colormaptype not in (0, 1) or\
            self.size[0] <= 0 or self.size[1] <= 0 or\
            depth not in (8, 16, 24, 32):
            raise SyntaxError, "not a TGA file"
 
        # image mode
        if imagetype in (3, 11):
            self.mode = "L"
            if depth == 1:
                self.mode = "1" # ???
        elif imagetype in (1, 9):
            self.mode = "P"
        elif imagetype in (2, 10):
            self.mode = "RGB"
            if depth == 32:
                self.mode = "RGBA"
        else:
            raise SyntaxError, "unknown TGA mode"
 
        # orientation
        orientation = flags & 0x30
        if orientation == 0x20:
            orientation = 1
        elif not orientation:
            orientation = -1
        else:
            raise SyntaxError, "unknown TGA orientation"
 
        if imagetype & 8:
            self.info["compression"] = "tga_rle"
 
        if colormaptype:
            # read palette
            start, size, mapdepth = i16(s[3:]), i16(s[5:]), i16(s[7:])
            if mapdepth == 16:
                self.palette = ImagePalette.raw("BGR;16",
                    "\0"*2*start + self.fp.read(2*size))
            elif mapdepth == 24:
                self.palette = ImagePalette.raw("BGR",
                    "\0"*3*start + self.fp.read(3*size))
            elif mapdepth == 32:
                self.palette = ImagePalette.raw("BGRA",
                    "\0"*4*start + self.fp.read(4*size))
 
        # setup tile descriptor
        try:
            rawmode = MODES[(imagetype&7, depth)]
            if imagetype & 8:
                # compressed
                self.tile = [("tga_rle", (0, 0)+self.size,
                              self.fp.tell(), (rawmode, orientation, depth))]
            else:
                self.tile = [("raw", (0, 0)+self.size,
                              self.fp.tell(), (rawmode, 0, orientation))]
        except KeyError:
            pass # cannot decode
 
 
 
def _save(im, fp, filename):
    import struct
 
    #--2 in position 3 denotes "uncompressed RGB image". Also need support for RGBA eventually..
    header_flags = (0,0,2, 0,0,0, 0,0,0, 0,0,0)
    fp.write( struct.pack( "<12B", *(header_flags) ) )
 
 
    #--write image dimensions chunk to header
    imageDimensionBytes =     (
                            (im.size[0] & 0x00FF),(im.size[0] & 0xFF00)/256,
                            (im.size[1] & 0x00FF),(im.size[1] & 0xFF00)/256
                            )
    fp.write( struct.pack( "<4B", *( imageDimensionBytes ) ) )
 
 
    # -- asess options passed from calling code
    #FIXME; writeAlpha simply forces a value of 0xFF into the alpha byte, support for the Alpha data from PIL would be better.
    writeAlpha = im.encoderinfo.get('writeAlpha', False)
 
    if writeAlpha :
        #--32 bit bitmap (BGRA) flag, and then a padding byte to indicate the end of the header
        fp.write( struct.pack( "<2B", 32, 0x08 ) )
    else:
        #--24 bit bitmap (BGR) flag, and then a padding byte to indicate the end of the header
        fp.write( struct.pack( "<2B", 24, 0x00 ) )
 
    # ----- end header
 
 
    #these loops write the body of the data to disk.
    #there has got to be a faster way, it takes about 5 seconds on a CureDue 2ghz Intel to write 1024x704 TGA files at the moment.
    idata = im.load() #aquire pixel data
    for y in reversed(range( im.size[1] )): #start at the bottom left corner, this is most common format from image editors
        for x in range( im.size[0] ):
            if writeAlpha: #FIXME; respect any alpha channel data coming from the rest of PIL
                fp.write( struct.pack("<4B", idata[x,y][2], idata[x,y][1], idata[x,y][0], 0xFF ))
            else:
                fp.write( struct.pack("<3B", idata[x,y][2], idata[x,y][1], idata[x,y][0] ))
 
 
    try:
        fp.flush()
    except:
        pass
 
 
 
 
 
#
# registry
 
Image.register_open("TGA", TgaImageFile, _accept)
Image.register_save("TGA", _save)
 
Image.register_extension("TGA", ".tga")