File: cloning.py

package info (click to toggle)
pigment-python 0.3.4-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,856 kB
  • ctags: 2,881
  • sloc: python: 11,567; sh: 9,133; makefile: 227; ansic: 76
file content (136 lines) | stat: -rw-r--r-- 4,593 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- mode: python; coding: utf-8 -*-
#
# Pigment Python binding cloning example
#
# Copyright © 2006, 2007, 2008 Fluendo Embedded S.L.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Author: Loïc Molinari <loic@fluendo.com>

import sys, pgm, gobject, gst
from pgm.timing import controller, modifier, keyframe, ticker

def on_update_pass(viewport, tck):
    tck.tick()

# Terminate the mainloop on a delete event
def on_delete(viewport, event, pipeline):
    pipeline.set_state(gst.STATE_NULL)
    pgm.main_quit()

# Terminate the mainloop on 'Escape' or 'q' key press event
def on_key_press(viewport, event, pipeline):
    if event.keyval == pgm.keysyms.q or event.keyval == pgm.keysyms.Escape:
        pgm.main_quit()

def main(args):
    # Argument check
    if len(args) < 1:
        print 'usage: %s uri' % sys.argv[0]
        return -1

    # OpenGL viewport creation
    gl = pgm.viewport_factory_make('opengl')
    gl.title = 'Video cloning'

    # Canvas and image drawables creation
    cvs = pgm.Canvas()
    img1 = pgm.Image()
    img2 = pgm.Image()
    img3 = pgm.Image()

    # Bind the canvas to the OpenGL viewport
    gl.set_canvas(cvs)

    # Images properties
    img1.position = (0.1, 0.0, 0.0)
    img1.size = (1.8, 2.0)
    img1.fg_color = (255, 100, 100, 255)
    img1.bg_a = 0
    img2.position = (1.1, 0.5, 0.0)
    img2.size = (1.8, 2.0)
    img2.fg_color = (100, 255, 100, 155)
    img2.bg_a = 0
    img3.position = (2.0, 1.0, 0.0)
    img3.size = (1.8, 2.0)
    img3.fg_color = (100, 100, 255, 255)
    img3.bg_a = 0

    # Drawables need to be shown
    img1.show()
    img2.show()
    img3.show()

    # Add them to the middle layer of the canvas
    cvs.add(pgm.DRAWABLE_MIDDLE, img1, img3, img2)

    # GStreamer pipeline setup
    sink = gst.element_factory_make('pgmimagesink')
    pipeline = gst.element_factory_make('playbin')
    pipeline.set_property('uri', sys.argv[1])
    pipeline.set_property('video-sink', sink)
    sink.set_property('image', img1)
    pipeline.set_state(gst.STATE_PLAYING)

    # Cloning from the first image
    img2.set_from_image(img1)
    img3.set_from_image(img1)

    # Create a animation position timeline
    tml = [keyframe.KeyFrame(1.0, 0.0),
           keyframe.KeyFrame(0.0, 1.0)]

    # Create the animation modifiers
    mdf1 = modifier.Modifier([img1], 'y', tml)
    mdf2 = modifier.Modifier([img2], 'y', tml)
    mdf3 = modifier.Modifier([img3], 'y', tml)

    # Set the ticker used by controllers
    tck = ticker.Ticker()
    controller.Controller.set_ticker(tck)

    # Create the animation controllers
    ctrl1 = controller.Controller(duration=700, resolution=7,
                                 repeat_behavior=controller.REVERSE,
                                 repeat_count=controller.INFINITE,
                                 transformation=controller.DECELERATE,
                                 modifiers=[mdf1])
    ctrl2 = controller.Controller(duration=800, resolution=7,
                                 repeat_behavior=controller.REVERSE,
                                 repeat_count=controller.INFINITE,
                                 transformation=controller.DECELERATE,
                                 modifiers=[mdf2])
    ctrl3 = controller.Controller(duration=900, resolution=7,
                                 repeat_behavior=controller.REVERSE,
                                 repeat_count=controller.INFINITE,
                                 transformation=controller.DECELERATE,
                                 modifiers=[mdf3])

    # Start the controllers
    ctrl1.start()
    ctrl2.start()
    ctrl3.start()

    # Let's start the mainloop
    gl.connect('delete-event', on_delete, pipeline)
    gl.connect('key-press-event', on_key_press, pipeline)
    gl.connect('update-pass', on_update_pass, tck)
    gl.show()
    pgm.main()

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))