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