]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
Fix behaviour of title clips when in and out points are given
[mlt] / src / modules / qimage / kdenlivetitle_wrapper.cpp
1 /*
2  * kdenlivetitle_wrapper.cpp -- kdenlivetitle wrapper
3  * Copyright (c) 2009 Marco Gittler <g.marco@freenet.de>
4  * Copyright (c) 2009 Jean-Baptiste Mardelle <jb@kdenlive.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "kdenlivetitle_wrapper.h"
22
23 #include <QtGui/QImage>
24 #include <QtGui/QPainter>
25 #include <QtCore/QDebug>
26 #include <QtGui/QApplication>
27 #include <QtCore/QMutex>
28 #include <QtGui/QGraphicsScene>
29 #include <QtGui/QGraphicsTextItem>
30 #include <QtSvg/QGraphicsSvgItem>
31 #include <QtGui/QTextCursor>
32 #include <QtGui/QStyleOptionGraphicsItem>
33
34 #include <QtCore/QString>
35
36 #include <QtXml/QDomElement>
37 #include <QtCore/QRectF>
38 #include <QtGui/QColor>
39 #include <QtGui/QWidget>
40 #include <framework/mlt_log.h>
41
42 static QApplication *app = NULL;
43
44 class ImageItem: public QGraphicsRectItem
45 {
46 public:
47     ImageItem(QImage img)
48     {
49         m_img = img;
50     }
51     QImage m_img;
52
53
54 protected:
55 virtual void paint( QPainter *painter,
56                        const QStyleOptionGraphicsItem * /*option*/,
57                        QWidget* )
58
59     painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
60     painter->drawImage(QPoint(), m_img);
61 }
62 };
63
64
65 QString rectTransform( QString s, QTransform t )
66 {
67         QStringList l = s.split( ',' );
68         return QString::number(l.at(0).toDouble() * t.m11()) + ',' + QString::number(l.at(1).toDouble() * t.m22()) + ',' + QString::number(l.at(2).toDouble() * t.m11()) + ',' + QString::number(l.at(3).toDouble() * t.m22());
69 }
70
71 QString colorToString( const QColor& c )
72 {
73         QString ret = "%1,%2,%3,%4";
74         ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
75         return ret;
76 }
77
78 QString rectFToString( const QRectF& c )
79 {
80         QString ret = "%1,%2,%3,%4";
81         ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
82         return ret;
83 }
84
85 QRectF stringToRect( const QString & s )
86 {
87
88         QStringList l = s.split( ',' );
89         if ( l.size() < 4 )
90                 return QRectF();
91         return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
92 }
93
94 QColor stringToColor( const QString & s )
95 {
96         QStringList l = s.split( ',' );
97         if ( l.size() < 4 )
98                 return QColor();
99         return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
100         ;
101 }
102 QTransform stringToTransform( const QString& s )
103 {
104         QStringList l = s.split( ',' );
105         if ( l.size() < 9 )
106                 return QTransform();
107         return QTransform(
108                    l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
109                    l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
110                    l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
111                );
112 }
113
114 static void qscene_delete( void *data )
115 {
116         QGraphicsScene *scene = ( QGraphicsScene * )data;
117         if (scene) delete scene;
118         scene = NULL;
119 }
120
121
122 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
123 {
124         scene->clear();
125         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
126         QDomDocument doc;
127         QString data = QString::fromUtf8(templateXml);
128         QString replacementText = QString::fromUtf8(templateText);
129         doc.setContent(data);
130         QDomElement title = doc.documentElement();
131         
132         // Check for invalid title
133         if ( title.isNull() || title.tagName() != "kdenlivetitle" ) return;
134         
135         int originalWidth;
136         int originalHeight;
137         if ( title.hasAttribute("width") ) {
138             originalWidth = title.attribute("width").toInt();
139             originalHeight = title.attribute("height").toInt();
140             scene->setSceneRect(0, 0, originalWidth, originalHeight);
141         }
142         else {
143             originalWidth = scene->sceneRect().width();
144             originalHeight = scene->sceneRect().height();
145         }
146         if ( title.hasAttribute( "out" ) ) {
147             mlt_properties_set_position( producer_props, "_animation_out", title.attribute( "out" ).toDouble() );
148         }
149         else {
150             mlt_properties_set_position( producer_props, "_animation_out", mlt_producer_get_out( producer ) );
151         }
152         
153         mlt_properties_set_int( producer_props, "_original_width", originalWidth );
154         mlt_properties_set_int( producer_props, "_original_height", originalHeight );
155
156         QDomNodeList items = title.elementsByTagName("item");
157         for ( int i = 0; i < items.count(); i++ )
158         {
159                 QGraphicsItem *gitem = NULL;
160                 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
161                 if ( zValue > -1000 )
162                 {
163                         if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
164                         {
165                                 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
166                                 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
167                                         QDomNode node = txtProperties.namedItem( "font-bold" );
168                                 if ( !node.isNull() )
169                                 {
170                                 // Old: Bold/Not bold.
171                                         font.setBold( node.nodeValue().toInt() );
172                                 }
173                                 else
174                                 {
175                                         // New: Font weight (QFont::)
176                                         font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
177                                 }
178                                         font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
179                                 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
180                                 // Older Kdenlive version did not store pixel size but point size
181                                 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
182                                 {
183                                         QFont f2;
184                                         f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
185                                         font.setPixelSize( QFontInfo( f2 ).pixelSize() );
186                                 }
187                                 else
188                                         font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
189                                 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
190                                 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
191                                 if ( !replacementText.isEmpty() )
192                                 {
193                                         text = text.replace( "%s", replacementText );
194                                 }
195                                 QGraphicsTextItem *txt = scene->addText(text, font);
196                                 txt->setDefaultTextColor( col );
197                                 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
198                                 {
199                                         txt->setTextWidth( txt->boundingRect().width() );
200                                         QTextCursor cur = txt->textCursor();
201                                         QTextBlockFormat format = cur.blockFormat();
202                                         format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
203                                         cur.select( QTextCursor::Document );
204                                         cur.setBlockFormat( format );
205                                         txt->setTextCursor( cur );
206                                         cur.clearSelection();
207                                         txt->setTextCursor( cur );
208                                 }
209                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
210                                 {
211                                         //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
212                                 }
213                                 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
214                                 {
215                                         //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
216                                 }
217                                         gitem = txt;
218                         }
219                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
220                         {
221                                 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
222                                 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
223                                 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
224                                 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth") .nodeValue().toDouble();
225                                 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
226                                 gitem = rec;
227                         }
228                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
229                         {
230                                 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
231                                 QImage img( url );
232                                 ImageItem *rec = new ImageItem(img);
233                                 scene->addItem( rec );
234                                 gitem = rec;
235                         }
236                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
237                         {
238                                 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
239                                 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
240                                 scene->addItem(rec);
241                                 gitem = rec;
242                         }
243                 }
244                 //pos and transform
245                 if ( gitem )
246                 {
247                         QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
248                                    items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
249                         gitem->setPos( p );
250                         gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
251                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
252                         gitem->setZValue( zValue );
253                 }
254         }
255
256         QDomNode n = title.firstChildElement("background");
257         if (!n.isNull()) {
258                 QColor color = QColor( stringToColor( n.attributes().namedItem( "color" ).nodeValue() ) );
259                 if (color.alpha() > 0) {
260                         QGraphicsRectItem *rec = scene->addRect(0, 0, scene->width(), scene->height() , QPen( Qt::NoPen ), QBrush( color ) );
261                         rec->setZValue(-1100);
262                 }
263           
264         }
265
266         QString startRect;
267         n = title.firstChildElement( "startviewport" );
268         // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
269         if (!n.isNull() && !n.toElement().hasAttribute("x"))
270         {
271                 startRect = n.attributes().namedItem( "rect" ).nodeValue();
272         }
273         n = title.firstChildElement( "endviewport" );
274         // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
275         if (!n.isNull() && !n.toElement().hasAttribute("x"))
276         {
277                 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
278                 if (startRect != rect)
279                         mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
280         }
281         if (!startRect.isEmpty()) {
282                 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
283         }
284         return;
285 }
286
287
288 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
289 {
290         // Obtain the producer 
291         mlt_producer producer = &self->parent;
292         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
293
294         // Obtain properties of frame
295         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
296         
297         pthread_mutex_lock( &self->mutex );
298         
299         // Check if user wants us to reload the image
300         if ( force_refresh == 1 || width != self->current_width || height != self->current_height || mlt_properties_get( producer_props, "_endrect" ) != NULL )
301         {
302                 //mlt_cache_item_close( self->image_cache );
303                 self->current_image = NULL;
304                 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
305                 mlt_properties_set_int( producer_props, "force_reload", 0 );
306         }
307         
308         if (self->current_image == NULL) {
309                 // restore QGraphicsScene
310                 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
311
312                 if ( force_refresh == 1 && scene )
313                 {
314                         scene = NULL;
315                         mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
316                 }
317
318                 if ( scene == NULL )
319                 {
320                         int argc = 1;
321                         char* argv[1];
322                         argv[0] = (char*) "xxx";
323                         
324                         // Warning: all Qt graphic objects (QRect, ...) must be initialized AFTER 
325                         // the QApplication is created, otherwise their will be NULL
326                         
327                         if (qApp) {
328                                 app = qApp;
329                         }
330                         else {
331 #ifdef linux
332                                 if ( getenv("DISPLAY") == 0 )
333                                 {
334                                         mlt_log_panic( MLT_PRODUCER_SERVICE( producer ), "Error, cannot render titles without an X11 environment.\nPlease either run melt from an X session or use a fake X server like xvfb:\nxvfb-run -a melt (...)\n" );
335                                         pthread_mutex_unlock( &self->mutex );
336                                         exit(1);
337                                         return;
338                                 }
339 #endif
340                                 app = new QApplication( argc, argv );
341                 //fix to let the decimal point for every locale be: "."
342                 setlocale(LC_NUMERIC,"POSIX");
343                         }
344                         scene = new QGraphicsScene();
345                         scene->setSceneRect(0, 0, mlt_properties_get_int( properties, "width" ), mlt_properties_get_int( properties, "height" ));
346                         loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
347                         mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
348                 }
349                 
350                 QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
351                 QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
352         
353                 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
354                 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
355                 const QRectF source( 0, 0, width, height );
356                 if (start.isNull()) {
357                     start = QRectF( 0, 0, originalWidth, originalHeight );
358                 }
359         
360                 //must be extracted from kdenlive title
361                 QImage img( width, height, QImage::Format_ARGB32 );
362                 img.fill( 0 );
363                 QPainter p1;
364                 p1.begin( &img );
365                 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
366                 //| QPainter::SmoothPixmapTransform );
367                 mlt_position anim_out = mlt_properties_get_position( producer_props, "_animation_out" );
368                 
369                 if (end.isNull())
370                 {
371                         scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
372                 }
373                 else if ( position > anim_out ) {
374                         scene->render( &p1, source, end, Qt::IgnoreAspectRatio );
375                 }
376                 else {
377                         double percentage = position / anim_out;
378                         QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * percentage;
379                         QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * percentage;
380                         const QRectF r1( topleft, bottomRight );
381                         scene->render( &p1, source, r1, Qt::IgnoreAspectRatio );
382                 }
383                 p1.end();
384
385                 int size = width * height * 4;
386                 uint8_t *pointer=img.bits();
387                 QRgb* src = ( QRgb* ) pointer;
388                 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
389                 uint8_t *dst = self->current_image;
390         
391                 for ( int i = 0; i < width * height * 4; i += 4 )
392                 {
393                         *dst++=qRed( *src );
394                         *dst++=qGreen( *src );
395                         *dst++=qBlue( *src );
396                         *dst++=qAlpha( *src );
397                         src++;
398                 }
399
400                 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
401                 self->current_width = width;
402                 self->current_height = height;
403         }
404
405         pthread_mutex_unlock( &self->mutex );
406         mlt_properties_set_int( properties, "width", self->current_width );
407         mlt_properties_set_int( properties, "height", self->current_height );
408 }
409
410