]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
Merge commit 'jbm/kdenlivetitle' into kdenlivetitle
[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 <QtGui/QTextCursor>
31 #include <QtGui/QStyleOptionGraphicsItem>
32
33 #include <QtCore/QString>
34
35 #include <QtXml/QDomElement>
36 #include <QtCore/QRectF>
37 #include <QtGui/QColor>
38 #include <QtGui/QWidget>
39
40 static QMutex g_mutex;
41 static QApplication *app = NULL;
42
43 class ImageItem: public QGraphicsRectItem
44 {
45 public:
46     ImageItem(QImage img)
47     {
48         m_img = img;
49     }
50     QImage m_img;
51
52
53 protected:
54 virtual void paint( QPainter *painter,
55                        const QStyleOptionGraphicsItem * /*option*/,
56                        QWidget* )
57
58     painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
59     painter->drawImage(QPoint(), m_img);
60 }
61 };
62
63
64 QString rectTransform( QString s, QTransform t )
65 {
66         QStringList l = s.split( ',' );
67         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());
68 }
69
70 QString colorToString( const QColor& c )
71 {
72         QString ret = "%1,%2,%3,%4";
73         ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
74         return ret;
75 }
76
77 QString rectFToString( const QRectF& c )
78 {
79         QString ret = "%1,%2,%3,%4";
80         ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
81         return ret;
82 }
83
84 QRectF stringToRect( const QString & s )
85 {
86
87         QStringList l = s.split( ',' );
88         if ( l.size() < 4 )
89                 return QRectF();
90         return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
91 }
92
93 QColor stringToColor( const QString & s )
94 {
95         QStringList l = s.split( ',' );
96         if ( l.size() < 4 )
97                 return QColor();
98         return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
99         ;
100 }
101 QTransform stringToTransform( const QString& s )
102 {
103         QStringList l = s.split( ',' );
104         if ( l.size() < 9 )
105                 return QTransform();
106         return QTransform(
107                    l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
108                    l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
109                    l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
110                );
111 }
112
113 static void qscene_delete( void *data )
114 {
115         QGraphicsScene *scene = ( QGraphicsScene * )data;
116         delete scene;
117         scene = NULL;
118 }
119
120
121 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText, int width, int height  )
122 {
123         scene->clear();
124         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
125         QDomDocument doc;
126         QString data = QString::fromUtf8(templateXml);
127         QString replacementText = QString::fromUtf8(templateText);
128         doc.setContent(data);
129         QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
130         QTransform transform;
131         if ( doc.documentElement().hasAttribute("width") ) {
132             int originalWidth = doc.documentElement().attribute("width").toInt();
133             int originalHeight = doc.documentElement().attribute("height").toInt();
134             if (originalWidth != width || originalHeight != height) {
135 #if QT_VERSION < 0x40500
136                     transform = QTransform().scale(  (double) width / originalWidth, (double) height / originalHeight );
137 #else
138                     transform = QTransform::fromScale ( (double) width / originalWidth, (double) height / originalHeight);
139 #endif
140             }
141         }
142         if ( titles.size() )
143         {
144
145                 QDomNodeList items = titles.item( 0 ).childNodes();
146                 for ( int i = 0; i < items.count(); i++ )
147                 {
148                         QGraphicsItem *gitem = NULL;
149                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
150                         if ( zValue > -1000 )
151                         {
152                                 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
153                                 {
154                                         QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
155                                         QFont font( txtProperties.namedItem( "font" ).nodeValue() );
156
157                                         QDomNode node = txtProperties.namedItem( "font-bold" );
158                                         if ( !node.isNull() )
159                                         {
160                                                 // Old: Bold/Not bold.
161                                                 font.setBold( node.nodeValue().toInt() );
162                                         }
163                                         else
164                                         {
165                                                 // New: Font weight (QFont::)
166                                                 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
167                                         }
168
169                                         //font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
170                                         font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
171                                         font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
172                                         // Older Kdenlive version did not store pixel size but point size
173                                         if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
174                                         {
175                                                 QFont f2;
176                                                 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
177                                                 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
178                                         }
179                                         else
180                                                 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
181                                         QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
182                                         QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
183                                         if ( !replacementText.isEmpty() )
184                                         {
185                                                 text = text.replace( "%s", replacementText );
186                                         }
187                                         QGraphicsTextItem *txt = new QGraphicsTextItem( text );
188                                         txt->setFont(font);
189                                         scene->addItem(txt);
190                                         txt->setDefaultTextColor( col );
191                                         if ( txtProperties.namedItem( "alignment" ).isNull() == false )
192                                         {
193                                                 txt->setTextWidth( txt->boundingRect().width() );
194                                                 QTextCursor cur = txt->textCursor();
195                                                 QTextBlockFormat format = cur.blockFormat();
196                                                 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
197                                                 cur.select( QTextCursor::Document );
198                                                 cur.setBlockFormat( format );
199                                                 txt->setTextCursor( cur );
200                                                 cur.clearSelection();
201                                                 txt->setTextCursor( cur );
202                                         }
203
204                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
205                                         {
206                                                 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
207                                         }
208                                         if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
209                                         {
210                                                 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
211                                         }
212
213                                         gitem = txt;
214                                 }
215                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
216                                 {
217                                         QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
218                                         QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
219                                         QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
220                                         double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
221                                         QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
222                                         gitem = rec;
223                                 }
224                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
225                                 {
226                                         QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
227                                         QImage img( url );
228                                         ImageItem *rec = new ImageItem(img);
229                                         scene->addItem( rec );
230                                         rec->setData( Qt::UserRole, url );
231                                         gitem = rec;
232                                 }
233                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
234                                 {
235                                         QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
236                                         //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
237                                         //m_scene->addItem(rec);
238                                         //rec->setData(Qt::UserRole, url);
239                                         //gitem = rec;
240                                 }
241                         }
242                         //pos and transform
243                         if ( gitem )
244                         {
245                                 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
246                                            items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
247                                 if ( transform != QTransform() ) p = QPointF(p.x() * transform.m11(), p.y() * transform.m22());
248                                 gitem->setPos( p );
249                                 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
250                                 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
251                                 gitem->setZValue( zValue );
252                                 if ( transform != QTransform() ) gitem->setTransform( transform, true );
253                                 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
254                         }
255                         if ( items.item( i ).nodeName() == "background" )
256                         {
257                                 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
258                                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
259                                 QList<QGraphicsItem *> items = scene->items();
260                                 for ( int i = 0; i < items.size(); i++ )
261                                 {
262                                         if ( items.at( i )->zValue() == -1100 )
263                                         {
264                                                 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
265                                                 break;
266                                         }
267                                 }
268                         }
269                         else if ( items.item( i ).nodeName() == "startviewport" )
270                         {
271                                 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
272                                 if ( transform != QTransform() ) {
273                                     rect = rectTransform( rect, transform );
274                                 }
275                                 mlt_properties_set( producer_props, "_startrect", rect.toUtf8().data() );
276                         }
277                         else if ( items.item( i ).nodeName() == "endviewport" )
278                         {
279                                 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
280                                 if ( transform != QTransform() ) {
281                                     rect = rectTransform( rect, transform );
282                                 }
283                                 mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
284                         }
285                 }
286         }
287         if ( !strcmp( mlt_properties_get( producer_props, "_startrect" ), mlt_properties_get( producer_props, "_endrect" ) ) )
288                 mlt_properties_set( producer_props, "_endrect", "" );
289         return;
290 }
291
292
293 void drawKdenliveTitle( producer_ktitle self, uint8_t * buffer, int width, int height, double position, int force_refresh )
294 {
295   
296         // Obtain the producer 
297         mlt_producer producer = &self->parent;
298         
299         // restore cached image if any
300         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
301
302         /*if ( force_refresh == 0 )
303         {
304                 uint8_t * cache = ( uint8_t * )mlt_properties_get_data( producer_props, "image", NULL );
305                 if (cache) {
306                     buffer = cache;
307                     return;
308                 }
309         }*/
310         
311         // restore QGraphicsScene
312         QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
313
314         if ( force_refresh == 1 && scene )
315         {
316                 scene = NULL;
317                 mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
318         }
319  
320         if ( scene == NULL )
321         {
322                 int argc = 1;
323                 char* argv[1];
324                 argv[0] = "xxx";
325                 if (qApp) {
326                     app = qApp;
327                 }
328                 else {
329                     app=new QApplication( argc,argv ); //, QApplication::Tty );
330                 }
331                 scene = new QGraphicsScene( 0, 0, width, height, app );
332                 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ), width, height );
333                 mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
334         }
335
336
337         //must be extracted from kdenlive title
338         QImage img( width, height, QImage::Format_ARGB32 );
339         img.fill( 0 );
340         QPainter p1;
341         p1.begin( &img );
342         p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
343         //| QPainter::SmoothPixmapTransform );
344
345         const QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
346         const QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
347         const QRectF source( 0, 0, width, height );
348
349         if (end.isNull()) {
350                 if (start.isNull())
351                         scene->render( &p1, source, source );
352                 else
353                         scene->render( &p1, start, source );
354         }
355         else {
356             QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * position;
357             QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * position;
358             const QRectF r1( topleft, bottomRight );
359             scene->render( &p1, r1, source );
360         }
361         p1.end();
362         g_mutex.lock();
363         uint8_t *pointer=img.bits();
364         QRgb* src = ( QRgb* ) pointer;
365         int size = width * height * 4;
366         for ( int i = 0; i < width * height * 4; i += 4 )
367         {
368                 *buffer++=qRed( *src );
369                 *buffer++=qGreen( *src );
370                 *buffer++=qBlue( *src );
371                 *buffer++=qAlpha( *src );
372                 src++;
373         }
374
375         if ( end.isNull() )
376         {
377             // No animation, cache image for further requests
378             self->current_image = (uint8_t *)mlt_pool_alloc( size );
379             self->current_image = buffer;
380             mlt_properties_set_data( producer_props, "image", self->current_image, width * height * 4, NULL, NULL );
381             
382             // Clear scene, we don't need it anymore
383             mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
384         }
385         g_mutex.unlock();
386 }
387
388
389
390