Example #1
0
    def _from_lumps(self, texture1, pnames):
        # util.Unpack PNAMES
        numdefs = util.unpack16(pnames.data[0:2])
        pnames = [util.zstrip(pnames.data[ptr:ptr+8]) \
            for ptr in xrange(4, 8*numdefs+4, 8)]

        # util.Unpack TEXTURE1
        data = texture1.data
        numtextures = util.unpack('<l', data[0:4])[0]
        pointers = util.unpack('<'+('l'*numtextures), data[4:4+numtextures*4])
        for ptr in pointers:
            texture = TextureDef(bytes=data[ptr:ptr+22])
            for pptr in range(ptr+22, ptr+22+10*texture.npatches, 10):
                x, y, idn = util.unpack('<hhh', data[pptr:pptr+6])
                texture.patches.append(PatchDef(x, y, name=pnames[idn]))
            self[texture.name] = texture
Example #2
0
    def _from_lumps(self, texture1, pnames):
        # util.Unpack PNAMES
        numdefs = util.unpack16(pnames.data[0:2])
        pnames = [util.zstrip(pnames.data[ptr:ptr+8]) \
            for ptr in xrange(4, 8*numdefs+4, 8)]

        # util.Unpack TEXTURE1
        data = texture1.data
        numtextures = util.unpack('<l', data[0:4])[0]
        pointers = util.unpack('<' + ('l' * numtextures),
                               data[4:4 + numtextures * 4])
        for ptr in pointers:
            texture = TextureDef(bytes=data[ptr:ptr + 22])
            for pptr in range(ptr + 22, ptr + 22 + 10 * texture.npatches, 10):
                x, y, idn = util.unpack('<hhh', data[pptr:pptr + 6])
                texture.patches.append(PatchDef(x, y, name=pnames[idn]))
            self[texture.name] = texture
Example #3
0
    def from_Image(self, im, translate=False):
        """Load from a PIL Image instance

        If the input image is 24-bit, the colors will be looked up
        in the current palette.

        If the input image is 8-bit, indices will simply be copied
        from the input image. To properly translate colors between
        palettes, set the `translate` parameter."""

        pixels = im.tostring()
        width, height = im.size
        # High resolution graphics not supported yet, so truncate
        height = min(254, height)
        xoff, yoff = (width // 2)-1, height-5
        if im.mode == "RGB":
            pixels = "".join([chr(self.palette.match(util.unpack('<BBB', \
                pixels[i*3:(i+1)*3]))) for i in range(width*height)])
        elif im.mode == 'P':
            srcpal = im.palette.tostring()
            if translate:
                R = [ord(c) for c in srcpal[0::3]]
                G = [ord(c) for c in srcpal[1::3]]
                B = [ord(c) for c in srcpal[2::3]]
                # Work around PIL bug: "RGB" loads as "BGR" from bmps (?)
                if util.filename[-4:].lower() == '.bmp':
                    srcpal = zip(B, G, R)
                else:
                    srcpal = zip(R, G, B)
                lexicon = [chr(self.palette.match(c)) for c in srcpal]
                pixels = "".join([lexicon[ord(b)] for b in pixels])
            else:
                # Simply copy pixels. However, make sure to translate
                # all colors matching the transparency color to the
                # right index. This is necessary because programs
                # aren't consistent in choice of position for the
                # transparent entry.
                packed_color = util.pack("BBB", *util.pal.tran_color)
                ri = 0
                while ri != -1:
                    ri = srcpal.find(packed_color, ri+3)
                    if not ri % 3 and ri//3 != self.palette.tran_index:
                        pixels = pixels.replace(chr(ri//3),
                            chr(self.palette.tran_index))
        else:
            raise TypeError, "image mode must be 'P' or 'RGB'"

        self.from_raw(pixels, width, height, xoff, yoff, self.palette)
Example #4
0
    def from_Image(self, im, translate=False):
        """Load from a PIL Image instance

        If the input image is 24-bit, the colors will be looked up
        in the current palette.

        If the input image is 8-bit, indices will simply be copied
        from the input image. To properly translate colors between
        palettes, set the `translate` parameter."""

        pixels = im.tostring()
        width, height = im.size
        # High resolution graphics not supported yet, so truncate
        height = min(254, height)
        xoff, yoff = (width // 2) - 1, height - 5
        if im.mode == "RGB":
            pixels = "".join([chr(self.palette.match(util.unpack('<BBB', \
                pixels[i*3:(i+1)*3]))) for i in range(width*height)])
        elif im.mode == 'P':
            srcpal = im.palette.tostring()
            if translate:
                R = [ord(c) for c in srcpal[0::3]]
                G = [ord(c) for c in srcpal[1::3]]
                B = [ord(c) for c in srcpal[2::3]]
                # Work around PIL bug: "RGB" loads as "BGR" from bmps (?)
                if util.filename[-4:].lower() == '.bmp':
                    srcpal = zip(B, G, R)
                else:
                    srcpal = zip(R, G, B)
                lexicon = [chr(self.palette.match(c)) for c in srcpal]
                pixels = "".join([lexicon[ord(b)] for b in pixels])
            else:
                # Simply copy pixels. However, make sure to translate
                # all colors matching the transparency color to the
                # right index. This is necessary because programs
                # aren't consistent in choice of position for the
                # transparent entry.
                packed_color = util.pack("BBB", *util.pal.tran_color)
                ri = 0
                while ri != -1:
                    ri = srcpal.find(packed_color, ri + 3)
                    if not ri % 3 and ri // 3 != self.palette.tran_index:
                        pixels = pixels.replace(chr(ri // 3),
                                                chr(self.palette.tran_index))
        else:
            raise TypeError, "image mode must be 'P' or 'RGB'"

        self.from_raw(pixels, width, height, xoff, yoff, self.palette)
Example #5
0
    def to_raw(self, tran_index=None):
        """Returns self converted to a raw (8-bpp) image.

        `tran_index` specifies the palette index to use for
        transparent pixels. The value defaults to that of the
        Graphic object's palette instance."""
        data = self.data
        width, height = self.dimensions
        tran_index = tran_index or self.palette.tran_index
        output = [chr(tran_index)] * (width*height)
        pointers = util.unpack('<%il'%width, data[8 : 8 + width*4])
        for x in xrange(width):
            pointer = pointers[x]
            while data[pointer] != '\xff':
                post_length = ord(data[pointer+1])
                op = ord(data[pointer])*width + x
                for p in range(pointer + 3, pointer + post_length + 3):
                    output[op] = data[p]
                    op += width
                pointer += post_length + 4
        return "".join(output)
Example #6
0
    def to_raw(self, tran_index=None):
        """Returns self converted to a raw (8-bpp) image.

        `tran_index` specifies the palette index to use for
        transparent pixels. The value defaults to that of the
        Graphic object's palette instance."""
        data = self.data
        width, height = self.dimensions
        tran_index = tran_index or self.palette.tran_index
        output = [chr(tran_index)] * (width * height)
        pointers = util.unpack('<%il' % width, data[8:8 + width * 4])
        for x in xrange(width):
            pointer = pointers[x]
            while data[pointer] != '\xff':
                post_length = ord(data[pointer + 1])
                op = ord(data[pointer]) * width + x
                for p in range(pointer + 3, pointer + post_length + 3):
                    output[op] = data[p]
                    op += width
                pointer += post_length + 4
        return "".join(output)
Example #7
0
 def get_dimensions(self):
     """Retrieve the (width, height) dimensions of the graphic."""
     return util.unpack('<hh', self.data[0:4])
Example #8
0
 def get_offsets(self):
     """Retrieve the (x, y) offsets of the graphic."""
     return util.unpack('<hh', self.data[4:8])
Example #9
0
 def get_dimensions(self):
     """Retrieve the (width, height) dimensions of the graphic."""
     return util.unpack('<hh', self.data[0:4])
Example #10
0
 def get_offsets(self):
     """Retrieve the (x, y) offsets of the graphic."""
     return util.unpack('<hh', self.data[4:8])