]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
Don't crash when running kdenlivetitle module from a console,
[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         QTransform transform;
136         if ( title.hasAttribute("width") ) {
137             int originalWidth = title.attribute("width").toInt();
138             mlt_properties_set_int( producer_props, "_original_width", originalWidth );
139             int originalHeight = title.attribute("height").toInt();
140             mlt_properties_set_int( producer_props, "_original_height", originalHeight );
141             scene->setSceneRect(0, 0, originalWidth, originalHeight);
142         }
143
144         QDomNodeList items = title.elementsByTagName("item");
145         for ( int i = 0; i < items.count(); i++ )
146         {
147                 QGraphicsItem *gitem = NULL;
148                 int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
149                 if ( zValue > -1000 )
150                 {
151                         if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
152                         {
153                                 QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
154                                 QFont font( txtProperties.namedItem( "font" ).nodeValue() );
155                                         QDomNode node = txtProperties.namedItem( "font-bold" );
156                                 if ( !node.isNull() )
157                                 {
158                                 // Old: Bold/Not bold.
159                                         font.setBold( node.nodeValue().toInt() );
160                                 }
161                                 else
162                                 {
163                                         // New: Font weight (QFont::)
164                                         font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
165                                 }
166                                         font.setItalic( txtProperties.namedItem( "font-italic" ).nodeValue().toInt() );
167                                 font.setUnderline( txtProperties.namedItem( "font-underline" ).nodeValue().toInt() );
168                                 // Older Kdenlive version did not store pixel size but point size
169                                 if ( txtProperties.namedItem( "font-pixel-size" ).isNull() )
170                                 {
171                                         QFont f2;
172                                         f2.setPointSize( txtProperties.namedItem( "font-size" ).nodeValue().toInt() );
173                                         font.setPixelSize( QFontInfo( f2 ).pixelSize() );
174                                 }
175                                 else
176                                         font.setPixelSize( txtProperties.namedItem( "font-pixel-size" ).nodeValue().toInt() );
177                                 QColor col( stringToColor( txtProperties.namedItem( "font-color" ).nodeValue() ) );
178                                 QString text = items.item( i ).namedItem( "content" ).firstChild().nodeValue();
179                                 if ( !replacementText.isEmpty() )
180                                 {
181                                         text = text.replace( "%s", replacementText );
182                                 }
183                                 QGraphicsTextItem *txt = scene->addText(text, font);
184                                 txt->setDefaultTextColor( col );
185                                 if ( txtProperties.namedItem( "alignment" ).isNull() == false )
186                                 {
187                                         txt->setTextWidth( txt->boundingRect().width() );
188                                         QTextCursor cur = txt->textCursor();
189                                         QTextBlockFormat format = cur.blockFormat();
190                                         format.setAlignment(( Qt::Alignment ) txtProperties.namedItem( "alignment" ).nodeValue().toInt() );
191                                         cur.select( QTextCursor::Document );
192                                         cur.setBlockFormat( format );
193                                         txt->setTextCursor( cur );
194                                         cur.clearSelection();
195                                         txt->setTextCursor( cur );
196                                 }
197                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
198                                 {
199                                         //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
200                                 }
201                                 if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
202                                 {
203                                         //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
204                                 }
205                                         gitem = txt;
206                         }
207                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
208                         {
209                                 QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
210                                 QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
211                                 QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
212                                 double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth") .nodeValue().toDouble();
213                                 QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
214                                 gitem = rec;
215                         }
216                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
217                         {
218                                 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
219                                 QImage img( url );
220                                 ImageItem *rec = new ImageItem(img);
221                                 scene->addItem( rec );
222                                 gitem = rec;
223                         }
224                         else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
225                         {
226                                 const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
227                                 QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
228                                 scene->addItem(rec);
229                                 gitem = rec;
230                         }
231                 }
232                 //pos and transform
233                 if ( gitem )
234                 {
235                         QPointF p( items.item( i ).namedItem( "position" ).attributes().namedItem( "x" ).nodeValue().toDouble(),
236                                    items.item( i ).namedItem( "position" ).attributes().namedItem( "y" ).nodeValue().toDouble() );
237                         gitem->setPos( p );
238                         gitem->setTransform( stringToTransform( items.item( i ).namedItem( "position" ).firstChild().firstChild().nodeValue() ) );
239                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
240                         gitem->setZValue( zValue );
241                 }
242         }
243
244         QDomNode n = doc.documentElement().firstChildElement("background");
245         if (!n.isNull()) {
246                 QColor color = QColor( stringToColor( n.attributes().namedItem( "color" ).nodeValue() ) );
247                 if (color.alpha() > 0) {
248                         QGraphicsRectItem *rec = scene->addRect(0, 0, scene->width(), scene->height() , QPen( Qt::NoPen ), QBrush( color ) );
249                         rec->setZValue(-1100);
250                 }
251           
252         }
253
254         QString startRect;
255         n = doc.documentElement().firstChildElement( "startviewport" );
256         if (!n.isNull())
257         {
258                 startRect = n.attributes().namedItem( "rect" ).nodeValue();
259         }
260         n = doc.documentElement().firstChildElement( "endviewport" );
261         if (!n.isNull())
262         {
263                 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
264                 if (startRect != rect)
265                         mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
266         }
267         if (!startRect.isEmpty()) {
268                 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
269         }
270         return;
271 }
272
273
274 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
275 {
276         // Obtain the producer 
277         mlt_producer producer = &self->parent;
278         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
279
280         // Obtain properties of frame
281         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
282         
283         pthread_mutex_lock( &self->mutex );
284         
285         // Check if user wants us to reload the image
286         if ( force_refresh == 1 || width != self->current_width || height != self->current_height || mlt_properties_get( producer_props, "_endrect" ) != NULL )
287         {
288                 //mlt_cache_item_close( self->image_cache );
289                 self->current_image = NULL;
290                 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
291                 mlt_properties_set_int( producer_props, "force_reload", 0 );
292         }
293         
294         if (self->current_image == NULL) {
295                 // restore QGraphicsScene
296                 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
297
298                 if ( force_refresh == 1 && scene )
299                 {
300                         scene = NULL;
301                         mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
302                 }
303
304                 if ( scene == NULL )
305                 {
306                         int argc = 1;
307                         char* argv[1];
308                         argv[0] = (char*) "xxx";
309                         
310                         // Warning: all Qt graphic objects (QRect, ...) must be initialized AFTER 
311                         // the QApplication is created, otherwise their will be NULL
312                         
313                         if (qApp) {
314                                 app = qApp;
315                         }
316                         else {
317 #ifdef linux
318                                 if ( getenv("DISPLAY") == 0 )
319                                 {
320                                         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" );
321                                         pthread_mutex_unlock( &self->mutex );
322                                         exit(1);
323                                         return;
324                                 }
325 #endif
326                                 app = new QApplication( argc, argv );
327                         }
328                         scene = new QGraphicsScene();
329                         loadFromXml( producer, scene, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ) );
330                         mlt_properties_set_data( producer_props, "qscene", scene, 0, ( mlt_destructor )qscene_delete, NULL );
331                 }
332                 
333                 QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
334                 QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
335         
336                 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
337                 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
338                 const QRectF source( 0, 0, width, height );
339                 if (start.isNull()) {
340                     start = QRectF( 0, 0, originalWidth, originalHeight );
341                 }
342         
343                 //must be extracted from kdenlive title
344                 QImage img( width, height, QImage::Format_ARGB32 );
345                 img.fill( 0 );
346                 QPainter p1;
347                 p1.begin( &img );
348                 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
349                 //| QPainter::SmoothPixmapTransform );
350                 if (end.isNull())
351                 {
352                         scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
353                 }
354                 else
355                 {
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, source, r1, Qt::IgnoreAspectRatio );
360                 }
361                 p1.end();
362
363                 int size = width * height * 4;
364                 uint8_t *pointer=img.bits();
365                 QRgb* src = ( QRgb* ) pointer;
366                 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
367                 uint8_t *dst = self->current_image;
368         
369                 for ( int i = 0; i < width * height * 4; i += 4 )
370                 {
371                         *dst++=qRed( *src );
372                         *dst++=qGreen( *src );
373                         *dst++=qBlue( *src );
374                         *dst++=qAlpha( *src );
375                         src++;
376                 }
377
378                 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
379                 self->current_width = width;
380                 self->current_height = height;
381         }
382
383         pthread_mutex_unlock( &self->mutex );
384         mlt_properties_set_int( properties, "width", self->current_width );
385         mlt_properties_set_int( properties, "height", self->current_height );
386 }
387
388