]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
Merge branch 'master' of xtremedia:git/mltframework.org/mlt
[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 #if QT_VERSION >= 0x040600
43 #include <QtGui/QGraphicsEffect>
44 #include <QtGui/QGraphicsBlurEffect>
45 #include <QtGui/QGraphicsDropShadowEffect>
46 #endif
47
48 static QApplication *app = NULL;
49
50 class ImageItem: public QGraphicsItem
51 {
52 public:
53     ImageItem(QImage img)
54     {
55         m_img = img;
56     }
57     QImage m_img;
58
59
60 protected:
61
62 virtual QRectF boundingRect() const
63 {
64     return QRectF(0, 0, m_img.width(), m_img.height());
65 }
66
67 virtual void paint( QPainter *painter,
68                        const QStyleOptionGraphicsItem * /*option*/,
69                        QWidget* )
70
71     painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
72     painter->drawImage(QPoint(), m_img);
73 }
74 };
75
76
77 QString rectTransform( QString s, QTransform t )
78 {
79         QStringList l = s.split( ',' );
80         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());
81 }
82
83 QString colorToString( const QColor& c )
84 {
85         QString ret = "%1,%2,%3,%4";
86         ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
87         return ret;
88 }
89
90 QString rectFToString( const QRectF& c )
91 {
92         QString ret = "%1,%2,%3,%4";
93         ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
94         return ret;
95 }
96
97 QRectF stringToRect( const QString & s )
98 {
99
100         QStringList l = s.split( ',' );
101         if ( l.size() < 4 )
102                 return QRectF();
103         return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
104 }
105
106 QColor stringToColor( const QString & s )
107 {
108         QStringList l = s.split( ',' );
109         if ( l.size() < 4 )
110                 return QColor();
111         return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
112         ;
113 }
114 QTransform stringToTransform( const QString& s )
115 {
116         QStringList l = s.split( ',' );
117         if ( l.size() < 9 )
118                 return QTransform();
119         return QTransform(
120                    l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
121                    l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
122                    l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
123                );
124 }
125
126 static void qscene_delete( void *data )
127 {
128         QGraphicsScene *scene = ( QGraphicsScene * )data;
129         if (scene) delete scene;
130         scene = NULL;
131 }
132
133
134 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
135 {
136         scene->clear();
137         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
138         QDomDocument doc;
139         QString data = QString::fromUtf8(templateXml);
140         QString replacementText = QString::fromUtf8(templateText);
141         doc.setContent(data);
142         QDomElement title = doc.documentElement();
143         
144         // Check for invalid title
145         if ( title.isNull() || title.tagName() != "kdenlivetitle" ) return;
146         
147         int originalWidth;
148         int originalHeight;
149         if ( title.hasAttribute("width") ) {
150             originalWidth = title.attribute("width").toInt();
151             originalHeight = title.attribute("height").toInt();
152             scene->setSceneRect(0, 0, originalWidth, originalHeight);
153         }
154         else {
155             originalWidth = scene->sceneRect().width();
156             originalHeight = scene->sceneRect().height();
157         }
158         if ( title.hasAttribute( "out" ) ) {
159             mlt_properties_set_position( producer_props, "_animation_out", title.attribute( "out" ).toDouble() );
160         }
161         else {
162             mlt_properties_set_position( producer_props, "_animation_out", mlt_producer_get_out( producer ) );
163         }
164         
165         mlt_properties_set_int( producer_props, "_original_width", originalWidth );
166         mlt_properties_set_int( producer_props, "_original_height", originalHeight );
167
168         QDomNodeList items = title.elementsByTagName("item");
169         for ( int i = 0; i < items.count(); i++ )
170         {
171                 QGraphicsItem *gitem = NULL;
172                 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
173                 if ( zValue > -1000 )
174                 {
175                         if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
176                         {
177                                 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
178                                 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
179                                         QDomNode node = txtProperties.namedItem( "font-bold" );
180                                 if ( !node.isNull() )
181                                 {
182                                 // Old: Bold/Not bold.
183                                         font.setBold( node.nodeValue().toInt() );
184                                 }
185                                 else
186                                 {
187                                         // New: Font weight (QFont::)
188                                         font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
189                                 }
190                                         font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
191                                 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
192                                 // Older Kdenlive version did not store pixel size but point size
193                                 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
194                                 {
195                                         QFont f2;
196                                         f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
197                                         font.setPixelSize( QFontInfo( f2 ).pixelSize() );
198                                 }
199                                 else
200                                         font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
201                                 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
202                                 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
203                                 if ( !replacementText.isEmpty() )
204                                 {
205                                         text = text.replace( "%s", replacementText );
206                                 }
207                                 QGraphicsTextItem *txt = scene->addText(text, font);
208                                 txt->setDefaultTextColor( col );
209                                 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
210                                 {
211                                         txt->setTextWidth( txt->boundingRect().width() );
212                                         QTextCursor cur = txt->textCursor();
213                                         QTextBlockFormat format = cur.blockFormat();
214                                         format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
215                                         cur.select( QTextCursor::Document );
216                                         cur.setBlockFormat( format );
217                                         txt->setTextCursor( cur );
218                                         cur.clearSelection();
219                                         txt->setTextCursor( cur );
220                                 }
221                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
222                                 {
223                                         //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
224                                 }
225                                 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
226                                 {
227                                         //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
228                                 }
229                                         gitem = txt;
230                         }
231                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
232                         {
233                                 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
234                                 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
235                                 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
236                                 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth") .nodeValue().toDouble();
237                                 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
238                                 gitem = rec;
239                         }
240                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
241                         {
242                                 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
243                                 QImage img( url );
244                                 ImageItem *rec = new ImageItem(img);
245                                 scene->addItem( rec );
246                                 gitem = rec;
247                         }
248                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
249                         {
250                                 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
251                                 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
252                                 scene->addItem(rec);
253                                 gitem = rec;
254                         }
255                 }
256                 //pos and transform
257                 if ( gitem )
258                 {
259                         QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
260                                    items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
261                         gitem->setPos( p );
262                         gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
263                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
264                         gitem->setZValue( zValue );
265
266 #if QT_VERSION >= 0x040600
267                         // effects
268                         QDomNode eff = items.item(i).namedItem("effect");
269                         if (!eff.isNull()) {
270                                 QDomElement e = eff.toElement();
271                                 if (e.attribute("type") == "blur") {
272                                         QGraphicsBlurEffect *blur = new QGraphicsBlurEffect();
273                                         blur->setBlurRadius(e.attribute("blurradius").toInt());
274                                         gitem->setGraphicsEffect(blur);
275                                 }
276                                 else if (e.attribute("type") == "shadow") {
277                                         QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect();
278                                         shadow->setBlurRadius(e.attribute("blurradius").toInt());
279                                         shadow->setOffset(e.attribute("xoffset").toInt(), e.attribute("yoffset").toInt());
280                                         gitem->setGraphicsEffect(shadow);
281                                 }
282                         }
283 #endif
284                 }
285         }
286
287         QDomNode n = title.firstChildElement("background");
288         if (!n.isNull()) {
289                 QColor color = QColor( stringToColor( n.attributes().namedItem( "color" ).nodeValue() ) );
290                 if (color.alpha() > 0) {
291                         QGraphicsRectItem *rec = scene->addRect(0, 0, scene->width(), scene->height() , QPen( Qt::NoPen ), QBrush( color ) );
292                         rec->setZValue(-1100);
293                 }
294           
295         }
296
297         QString startRect;
298         n = title.firstChildElement( "startviewport" );
299         // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
300         if (!n.isNull() && !n.toElement().hasAttribute("x"))
301         {
302                 startRect = n.attributes().namedItem( "rect" ).nodeValue();
303         }
304         n = title.firstChildElement( "endviewport" );
305         // Check if node exists, if it has an x attribute, it is an old version title, don't use viewport
306         if (!n.isNull() && !n.toElement().hasAttribute("x"))
307         {
308                 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
309                 if (startRect != rect)
310                         mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
311         }
312         if (!startRect.isEmpty()) {
313                 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
314         }
315         return;
316 }
317
318
319 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
320 {
321         // Obtain the producer 
322         mlt_producer producer = &self->parent;
323         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
324
325         // Obtain properties of frame
326         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
327         
328         pthread_mutex_lock( &self->mutex );
329         
330         // Check if user wants us to reload the image
331         if ( force_refresh == 1 || width != self->current_width || height != self->current_height || mlt_properties_get( producer_props, "_endrect" ) != NULL )
332         {
333                 //mlt_cache_item_close( self->image_cache );
334                 self->current_image = NULL;
335                 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
336                 mlt_properties_set_int( producer_props, "force_reload", 0 );
337         }
338         
339         if (self->current_image == NULL) {
340                 // restore QGraphicsScene
341                 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
342
343                 if ( force_refresh == 1 && scene )
344                 {
345                         scene = NULL;
346                         mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
347                 }
348
349                 if ( scene == NULL )
350                 {
351                         int argc = 1;
352                         char* argv[1];
353                         argv[0] = (char*) "xxx";
354                         
355                         // Warning: all Qt graphic objects (QRect, ...) must be initialized AFTER 
356                         // the QApplication is created, otherwise their will be NULL
357                         
358                         if (qApp) {
359                                 app = qApp;
360                         }
361                         else {
362 #ifdef linux
363                                 if ( getenv("DISPLAY") == 0 )
364                                 {
365                                         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" );
366                                         pthread_mutex_unlock( &self->mutex );
367                                         exit(1);
368                                         return;
369                                 }
370 #endif
371                                 app = new QApplication( argc, argv );
372                 //fix to let the decimal point for every locale be: "."
373                 setlocale(LC_NUMERIC,"POSIX");
374                         }
375                         scene = new QGraphicsScene();
376                         scene->setSceneRect(0, 0, mlt_properties_get_int( properties, "width" ), mlt_properties_get_int( properties, "height" ));
377                         loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
378                         mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
379                 }
380                 
381                 QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
382                 QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
383         
384                 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
385                 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
386                 const QRectF source( 0, 0, width, height );
387                 if (start.isNull()) {
388                     start = QRectF( 0, 0, originalWidth, originalHeight );
389                 }
390         
391                 //must be extracted from kdenlive title
392                 QImage img( width, height, QImage::Format_ARGB32 );
393                 img.fill( 0 );
394                 QPainter p1;
395                 p1.begin( &img );
396                 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
397                 //| QPainter::SmoothPixmapTransform );
398                 mlt_position anim_out = mlt_properties_get_position( producer_props, "_animation_out" );
399
400                 if (end.isNull())
401                 {
402                         scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
403                 }
404                 else if ( position > anim_out ) {
405                         scene->render( &p1, source, end, Qt::IgnoreAspectRatio );
406                 }
407                 else {
408                         double percentage = position / anim_out;
409                         QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * percentage;
410                         QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * percentage;
411                         const QRectF r1( topleft, bottomRight );
412                         scene->render( &p1, source, r1, Qt::IgnoreAspectRatio );
413                 }
414                 p1.end();
415
416                 int size = width * height * 4;
417                 uint8_t *pointer=img.bits();
418                 QRgb* src = ( QRgb* ) pointer;
419                 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
420                 uint8_t *dst = self->current_image;
421         
422                 for ( int i = 0; i < width * height * 4; i += 4 )
423                 {
424                         *dst++=qRed( *src );
425                         *dst++=qGreen( *src );
426                         *dst++=qBlue( *src );
427                         *dst++=qAlpha( *src );
428                         src++;
429                 }
430
431                 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
432                 self->current_width = width;
433                 self->current_height = height;
434         }
435
436         pthread_mutex_unlock( &self->mutex );
437         mlt_properties_set_int( properties, "width", self->current_width );
438         mlt_properties_set_int( properties, "height", self->current_height );
439 }
440
441