home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
tusportal.tus.k12.pa.us
/
tusportal.tus.k12.pa.us.tar
/
tusportal.tus.k12.pa.us
/
Wyse
/
latest-image.raw
/
0.img
/
usr
/
lib
/
pygtk
/
2.0
/
demos
/
pixbufs.pyo
(
.txt
)
< prev
next >
Wrap
Python Compiled Bytecode
|
2010-05-11
|
6KB
|
167 lines
# Source Generated with Decompyle++
# File: in.pyo (Python 2.6)
'''Pixbufs
A GdkPixbuf represents an image, normally in RGB or RGBA format.
Pixbufs are normally used to load files from disk and perform image scaling.
This demo is not all that educational, but looks cool. It was written by
Extreme Pixbuf Hacker Federico Mena Quintero. It also shows off how to use
GtkDrawingArea to do a simple animation.
Look at the Image demo for additional pixbuf usage examples.'''
import os
import math
import gobject
import gtk
FRAME_DELAY = 50
CYCLE_LEN = 60
IMAGE_DIR = os.path.join(os.path.dirname(__file__), 'images')
BACKGROUND_NAME = 'background.jpg'
image_names = [
'apple-red.png',
'gnome-applets.png',
'gnome-calendar.png',
'gnome-foot.png',
'gnome-gmush.png',
'gnome-gimp.png',
'gnome-gsame.png',
'gnu-keys.png']
class PixbufsDemo(gtk.Window):
frame = None
background = None
images = []
back_width = 0
back_height = 0
timeout_id = 0
frame_num = 0
timeout_id = None
def __init__(self, parent = None):
gtk.Window.__init__(self)
try:
self.set_screen(parent.get_screen())
except AttributeError:
self.connect('destroy', (lambda : gtk.main_quit()))
self.connect('destroy', self.cleanup_callback)
self.set_title(self.__class__.__name__)
self.set_resizable(False)
if not self.load_pixbufs():
dialog = gtk.MessageDialog(self, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, 'Failed to load an image')
dialog.connect('response', (lambda d, r: d.destroy()))
dialog.show()
else:
self.set_size_request(self.back_width, self.back_height)
self.frame = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, self.back_width, self.back_height)
da = gtk.DrawingArea()
da.connect('expose_event', self.expose_cb)
self.add(da)
self.timeout_id = gobject.timeout_add(FRAME_DELAY, self.timeout)
self.show_all()
def load_pixbufs(self):
''' Loads the images for the demo and returns whether the
operation succeeded.
'''
if self.background is not None:
return True
try:
self.background = gtk.gdk.pixbuf_new_from_file(os.path.join(IMAGE_DIR, BACKGROUND_NAME))
except gobject.GError:
self.background is not None
error = self.background is not None
return False
self.back_width = self.background.get_width()
self.back_height = self.background.get_height()
for filename in image_names:
try:
self.images.append(gtk.gdk.pixbuf_new_from_file(os.path.join(IMAGE_DIR, filename)))
continue
except gobject.GError:
self.background is not None
error = self.background is not None
return False
return True
def expose_cb(self, draw_area, event):
''' Expose callback for the drawing area. '''
rowstride = self.frame.get_rowstride()
pixels = self.frame.get_pixels()
draw_area.window.draw_rgb_image(draw_area.style.black_gc, event.area.x, event.area.y, event.area.width, event.area.height, 'normal', pixels, rowstride, event.area.x, event.area.y)
return True
def cleanup_callback(self, win):
if self.timeout_id is not None:
gobject.source_remove(self.timeout_id)
self.timeout_id = None
def timeout(self):
''' Timeout handler to regenerate the frame. '''
self.background.copy_area(0, 0, self.back_width, self.back_height, self.frame, 0, 0)
f = float(self.frame_num % CYCLE_LEN) / float(CYCLE_LEN)
xmid = self.back_width / 2
ymid = self.back_height / 2
radius = min(xmid, ymid) / 2
N_IMAGES = len(image_names)
for i_name in image_names:
i = image_names.index(i_name)
ang = 2 * math.pi * i / N_IMAGES - f * 2 * math.pi
iw = self.images[i].get_width()
ih = self.images[i].get_height()
r = radius + (radius / 3) * math.sin(f * 2 * math.pi)
xpos = math.floor((xmid + r * math.cos(ang) - iw / 2) + 0.5)
ypos = math.floor((ymid + r * math.sin(ang) - ih / 2) + 0.5)
if i % 2 == 0:
k = math.cos(f * 2 * math.pi)
else:
k = math.sin(f * 2 * math.pi)
k = 2 * k * k
k = max(0.25, k)
r1 = gtk.gdk.Rectangle()
r1.x = int(xpos)
r1.y = int(ypos)
r1.width = iw * k
r1.height = ih * k
r2 = gtk.gdk.Rectangle()
r2.x = 0
r2.y = 0
r2.width = self.back_width
r2.height = self.back_height
dest = r1.intersect(r2)
if dest is not None:
if i % 2 == 0:
alpha = int(max(127, math.fabs(255 * math.cos(f * 2 * math.pi))))
else:
alpha = int(max(127, math.fabs(255 * math.sin(f * 2 * math.pi))))
self.images[i].composite(self.frame, dest.x, dest.y, dest.width, dest.height, xpos, ypos, k, k, gtk.gdk.INTERP_NEAREST, alpha)
continue
if self is not None:
self.queue_draw()
self.frame_num += 1
return True
def main():
PixbufsDemo()
gtk.main()
if __name__ == '__main__':
main()