]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
Rescale title when they are played with a different profile
[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  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include "kdenlivetitle_wrapper.h"
21
22 #include <QtGui/QImage>
23 #include <QtGui/QPainter>
24 #include <QtCore/QDebug>
25 #include <QtGui/QApplication>
26 #include <QtCore/QMutex>
27 #include <QtGui/QGraphicsScene>
28 #include <QtGui/QGraphicsTextItem>
29 #include <QtGui/QTextCursor>
30
31 static QApplication *app = NULL;
32
33 extern "C"
34 {
35   
36 #include <framework/mlt_producer.h>
37 #include <framework/mlt_cache.h>
38
39         static QMutex g_mutex;
40         void refresh_kdenlivetitle( mlt_producer producer, uint8_t* buffer, int width, int height , double position, int force_refresh )
41         {
42                 if (force_refresh) {
43                         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
44                         loadFromXml( producer, NULL, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
45                 }
46                 drawKdenliveTitle( producer, buffer, width, height, position );
47         }
48
49         static void qscene_delete( void *data )
50         {
51                 QGraphicsScene *scene = ( QGraphicsScene * )data;
52                 delete scene;
53                 scene = NULL;
54         }
55 }
56
57
58
59 void drawKdenliveTitle( mlt_producer producer, uint8_t * buffer, int width, int height, double position )
60 {
61   
62         // restore QGraphicsScene
63         g_mutex.lock();
64         mlt_cache_item qscene_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qscene" );
65         QGraphicsScene *scene = static_cast<QGraphicsScene *>( mlt_cache_item_data( qscene_cache, NULL ) );
66         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
67  
68         if ( scene == NULL )
69         {
70                 int argc = 1;
71                 char* argv[1];
72                 argv[0] = "xxx";
73                 if (qApp) {
74                     app = qApp;
75                 }
76                 else {
77                     app=new QApplication( argc,argv );
78                 }
79                 scene = new QGraphicsScene(0, 0, (qreal) width, (qreal) height, app);
80                 loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
81                 mlt_service_cache_put( MLT_PRODUCER_SERVICE( producer ), "qscene", scene, 0, ( mlt_destructor )qscene_delete );
82         }
83
84         g_mutex.unlock();
85         
86         //must be extracted from kdenlive title
87         QImage img( width, height, QImage::Format_ARGB32 );
88         img.fill( 0 );
89         QPainter p1;
90         p1.begin( &img );
91         p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
92         //| QPainter::SmoothPixmapTransform );
93
94         const QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "startrect" ) ) );
95         const QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "endrect" ) ) );
96         const QRectF source( 0, 0, width, height );
97
98         if (end.isNull()) {
99                 if (start.isNull())
100                         scene->render( &p1 );
101                 else
102                         scene->render( &p1, start, source );
103         }
104         else {
105             QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * position;
106             QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * position;
107             const QRectF r1( topleft, bottomRight );
108             scene->render( &p1, r1, source );
109         }
110         p1.end();
111         uint8_t *pointer=img.bits();
112         QRgb* src = ( QRgb* ) pointer;
113         for ( int i = 0; i < width * height * 4; i += 4 )
114         {
115                 *buffer++=qRed( *src );
116                 *buffer++=qGreen( *src );
117                 *buffer++=qBlue( *src );
118                 *buffer++=qAlpha( *src );
119                 src++;
120         }
121 }
122
123 void loadFromXml( mlt_producer producer, QGraphicsScene *scene, const char *templateXml, const char *templateText )
124 {
125         if (scene == NULL) {
126                 mlt_cache_item qscene_cache = mlt_service_cache_get( MLT_PRODUCER_SERVICE( producer ), "qscene" );
127                 scene = static_cast<QGraphicsScene *>( mlt_cache_item_data( qscene_cache, NULL ) );
128                 if (scene == NULL) return;
129         }
130         scene->clear();
131         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
132         QDomDocument doc;
133         QString data = QString::fromUtf8(templateXml);
134         QString replacementText = QString::fromUtf8(templateText);
135         doc.setContent(data);
136         QDomNodeList titles = doc.elementsByTagName( "kdenlivetitle" );
137         QTransform transform;
138         if ( doc.documentElement().hasAttribute("width") ) {
139             int originalWidth = doc.documentElement().attribute("width").toInt();
140             int originalHeight = doc.documentElement().attribute("height").toInt();
141             if (originalWidth != scene->width() || originalHeight != scene->height()) {
142                     transform = QTransform::fromScale ( scene->width() / originalWidth, scene->height() / originalHeight);
143             }
144         }
145         if ( titles.size() )
146         {
147
148                 QDomNodeList items = titles.item( 0 ).childNodes();
149                 for ( int i = 0; i < items.count(); i++ )
150                 {
151                         QGraphicsItem *gitem = NULL;
152                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
153                         if ( zValue > -1000 )
154                         {
155                                 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
156                                 {
157                                         QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
158                                         QFont font( txtProperties.namedItem( "font" ).nodeValue() );
159
160                                         QDomNode node = txtProperties.namedItem( "font-bold" );
161                                         if ( !node.isNull() )
162                                         {
163                                                 // Old: Bold/Not bold.
164                                                 font.setBold( node.nodeValue().toInt() );
165                                         }
166                                         else
167                                         {
168                                                 // New: Font weight (QFont::)
169                                                 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
170                                         }
171
172                                         //font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
173                                         font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
174                                         font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
175                                         // Older Kdenlive version did not store pixel size but point size
176                                         if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
177                                         {
178                                                 QFont f2;
179                                                 f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
180                                                 font.setPixelSize( QFontInfo( f2 ).pixelSize() );
181                                         }
182                                         else
183                                                 font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
184                                         QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
185                                         QGraphicsTextItem *txt;
186                                         if ( !replacementText.isEmpty() )
187                                         {
188                                                 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
189                                                 text = text.replace( "%s", replacementText );
190                                                 txt = scene->addText( text, font );
191                                         }
192                                         else txt = scene->addText( items.item( i ).namedItem( "content" ).firstChild().nodeValue(), font );
193                                         txt->setDefaultTextColor( col );
194                                         txt->setTextInteractionFlags( Qt::NoTextInteraction );
195                                         if ( txtProperties.namedItem( "alignment" ).isNull() == false )
196                                         {
197                                                 txt->setTextWidth( txt->boundingRect().width() );
198                                                 QTextCursor cur = txt->textCursor();
199                                                 QTextBlockFormat format = cur.blockFormat();
200                                                 format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
201                                                 cur.select( QTextCursor::Document );
202                                                 cur.setBlockFormat( format );
203                                                 txt->setTextCursor( cur );
204                                                 cur.clearSelection();
205                                                 txt->setTextCursor( cur );
206                                         }
207
208                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
209                                         {
210                                                 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
211                                         }
212                                         if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
213                                         {
214                                                 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
215                                         }
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                                         QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
231                                         QPixmap pix( url );
232                                         QGraphicsPixmapItem *rec = scene->addPixmap( pix );
233                                         rec->setData( Qt::UserRole, url );
234                                         gitem = rec;
235                                 }
236                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
237                                 {
238                                         QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
239                                         //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
240                                         //m_scene->addItem(rec);
241                                         //rec->setData(Qt::UserRole, url);
242                                         //gitem = rec;
243                                 }
244                         }
245                         //pos and transform
246                         if ( gitem )
247                         {
248                                 QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
249                                            items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
250                                 if ( transform != QTransform() ) p = QPointF(p.x() * transform.m11(), p.y() * transform.m22());
251                                 gitem->setPos( p );
252                                 gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
253                                 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
254                                 gitem->setZValue( zValue );
255                                 if ( transform != QTransform() ) gitem->setTransform( transform, true );
256                                 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
257                         }
258                         if ( items.item( i ).nodeName() == "background" )
259                         {
260                                 QColor color = QColor( stringToColor( items.item( i ).attributes().namedItem( "color" ).nodeValue() ) );
261                                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
262                                 QList<QGraphicsItem *> items = scene->items();
263                                 for ( int i = 0; i < items.size(); i++ )
264                                 {
265                                         if ( items.at( i )->zValue() == -1100 )
266                                         {
267                                                 (( QGraphicsRectItem * )items.at( i ) )->setBrush( QBrush( color ) );
268                                                 break;
269                                         }
270                                 }
271                         }
272                         else if ( items.item( i ).nodeName() == "startviewport" )
273                         {
274                                 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
275                                 if ( transform != QTransform() ) {
276                                     rect = rectTransform( rect, transform );
277                                 }
278                                 mlt_properties_set( producer_props, "startrect", rect.toUtf8().data() );
279                         }
280                         else if ( items.item( i ).nodeName() == "endviewport" )
281                         {
282                                 QString rect = items.item( i ).attributes().namedItem( "rect" ).nodeValue();
283                                 if ( transform != QTransform() ) {
284                                     rect = rectTransform( rect, transform );
285                                 }
286                                 mlt_properties_set( producer_props, "endrect", rect.toUtf8().data() );
287                         }
288                 }
289         }
290         if ( mlt_properties_get( producer_props, "startrect") == mlt_properties_get( producer_props, "endrect") )
291                 mlt_properties_set( producer_props, "endrect", "" );
292         return;
293 }
294
295 QString rectTransform( QString s, QTransform t )
296 {
297         QStringList l = s.split( ',' );
298         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());
299 }
300
301 QString colorToString( const QColor& c )
302 {
303         QString ret = "%1,%2,%3,%4";
304         ret = ret.arg( c.red() ).arg( c.green() ).arg( c.blue() ).arg( c.alpha() );
305         return ret;
306 }
307
308 QString rectFToString( const QRectF& c )
309 {
310         QString ret = "%1,%2,%3,%4";
311         ret = ret.arg( c.top() ).arg( c.left() ).arg( c.width() ).arg( c.height() );
312         return ret;
313 }
314
315 QRectF stringToRect( const QString & s )
316 {
317
318         QStringList l = s.split( ',' );
319         if ( l.size() < 4 )
320                 return QRectF();
321         return QRectF( l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(), l.at( 3 ).toDouble() ).normalized();
322 }
323
324 QColor stringToColor( const QString & s )
325 {
326         QStringList l = s.split( ',' );
327         if ( l.size() < 4 )
328                 return QColor();
329         return QColor( l.at( 0 ).toInt(), l.at( 1 ).toInt(), l.at( 2 ).toInt(), l.at( 3 ).toInt() );
330         ;
331 }
332 QTransform stringToTransform( const QString& s )
333 {
334         QStringList l = s.split( ',' );
335         if ( l.size() < 9 )
336                 return QTransform();
337         return QTransform(
338                    l.at( 0 ).toDouble(), l.at( 1 ).toDouble(), l.at( 2 ).toDouble(),
339                    l.at( 3 ).toDouble(), l.at( 4 ).toDouble(), l.at( 5 ).toDouble(),
340                    l.at( 6 ).toDouble(), l.at( 7 ).toDouble(), l.at( 8 ).toDouble()
341                );
342 }