~jconti/ubuntu/precise/emesene/fix-956422

« back to all changes in this revision

Viewing changes to pygif/pixbuf.py

  • Committer: Bazaar Package Importer
  • Author(s): Devid Antonio Filoni
  • Date: 2010-04-14 01:33:51 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20100414013351-r2icbt5gs4ai71j8
Tags: 1.6.1-0ubuntu1
* New upstream release (LP: #562646).
* Fix missing-debian-source-format lintian warning.
* Refresh 20_dont_build_own_libmimic.patch patch.
* Bump Standards-Version to 3.8.4.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
#   This file is part of emesene.
 
4
#
 
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.
 
9
#
 
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.
 
14
#
 
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
 
18
#
 
19
 
 
20
'''Converts a gtk.gdk.Pixbuf into a GifEncoder instance'''
 
21
 
 
22
import pygif
 
23
 
 
24
import StringIO
 
25
import gtk
 
26
 
 
27
def convert(pixbuf, enc=None):
 
28
    '''Parses gtk.gdk.Pixbuf pixels and returns a GifEncoder instance'''
 
29
 
 
30
    if enc is None:
 
31
        enc = pygif.GifEncoder()
 
32
 
 
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
 
37
    
 
38
    # we have two palletes
 
39
    # the "definitive", tuple based pallete
 
40
    # and a quick search string pallete
 
41
    enc.pallete = []
 
42
    string_pallete = []
 
43
 
 
44
    raw_pixels = StringIO.StringIO(pixbuf.get_pixels())
 
45
    out_pixels = []
 
46
 
 
47
    while True:
 
48
        rgb = raw_pixels.read(bpp)
 
49
        if len(rgb) != bpp:
 
50
            break
 
51
        
 
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])
 
55
        
 
56
        if rgb not in string_pallete:
 
57
            index = len(enc.pallete)
 
58
            string_pallete.append(rgb)
 
59
            enc.pallete.append((red, green, blue))
 
60
        else:
 
61
            index = string_pallete.index(rgb)
 
62
 
 
63
        out_pixels.append(index)
 
64
    del string_pallete
 
65
 
 
66
    enc.global_color_table_size = len(enc.pallete)
 
67
    enc.color_table_flag = True
 
68
    enc.color_resolution = 7 # 256 colors
 
69
    enc.build_flags()
 
70
    
 
71
    image = enc.new_image()
 
72
    image.codesize, image.lzwcode = enc.lzw_encode(out_pixels)
 
73
    return enc
 
74
 
 
75
def main():
 
76
    '''runs a simple test with vampire.gif'''
 
77
    pixbuf = gtk.gdk.pixbuf_new_from_file("vampire.gif")
 
78
    enc = convert(pixbuf)
 
79
    enc.write("vampire_pixbuf.gif")
 
80
 
 
81
if __name__ == '__main__':
 
82
    main()