1
# -*- coding: utf-8 -*-
3
# This file is part of emesene.
5
# Emesene is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# Emesene is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with emesene; if not, write to the Free Software
17
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
'''Converts a gtk.gdk.Pixbuf into a GifEncoder instance'''
27
def convert(pixbuf, enc=None):
28
'''Parses gtk.gdk.Pixbuf pixels and returns a GifEncoder instance'''
31
enc = pygif.GifEncoder()
33
enc.ls_width = pixbuf.get_width()
34
enc.ls_height = pixbuf.get_height()
35
bpp = pixbuf.get_n_channels()
36
assert pixbuf.get_bits_per_sample() == 8
38
# we have two palletes
39
# the "definitive", tuple based pallete
40
# and a quick search string pallete
44
raw_pixels = StringIO.StringIO(pixbuf.get_pixels())
48
rgb = raw_pixels.read(bpp)
52
red, green, blue = ord(rgb[0]), ord(rgb[1]), ord(rgb[2])
53
# uncomment the following to have ASCII art debug :P
54
#sys.stdout.write(hex(red)[2])
56
if rgb not in string_pallete:
57
index = len(enc.pallete)
58
string_pallete.append(rgb)
59
enc.pallete.append((red, green, blue))
61
index = string_pallete.index(rgb)
63
out_pixels.append(index)
66
enc.global_color_table_size = len(enc.pallete)
67
enc.color_table_flag = True
68
enc.color_resolution = 7 # 256 colors
71
image = enc.new_image()
72
image.codesize, image.lzwcode = enc.lzw_encode(out_pixels)
76
'''runs a simple test with vampire.gif'''
77
pixbuf = gtk.gdk.pixbuf_new_from_file("vampire.gif")
79
enc.write("vampire_pixbuf.gif")
81
if __name__ == '__main__':