]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
Fix regression introduced by last commit
[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
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 )
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             mlt_properties_set_int( producer_props, "_original_width", originalWidth );
134             int originalHeight = doc.documentElement().attribute("height").toInt();
135             mlt_properties_set_int( producer_props, "_original_height", originalHeight );
136             scene->setSceneRect(0, 0, originalWidth, originalHeight);
137         }
138
139         if ( titles.size() )
140         {
141
142                 QDomNodeList items = titles.item( 0 ).childNodes();
143                 for ( int i = 0; i < items.count(); i++ )
144                 {
145                         QGraphicsItem *gitem = NULL;
146                         int zValue = items.item( i ).attributes().namedItem( "z-index" ).nodeValue().toInt();
147                         if ( zValue > -1000 )
148                         {
149                                 if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsTextItem" )
150                                 {
151                                         QDomNamedNodeMap txtProperties = items.item( i ).namedItem( "content" ).attributes();
152                                         QFont font( txtProperties.namedItem( "font" ).nodeValue() );
153
154                                         QDomNode node = txtProperties.namedItem( "font-bold" );
155                                         if ( !node.isNull() )
156                                         {
157                                                 // Old: Bold/Not bold.
158                                                 font.setBold( node.nodeValue().toInt() );
159                                         }
160                                         else
161                                         {
162                                                 // New: Font weight (QFont::)
163                                                 font.setWeight( txtProperties.namedItem( "font-weight" ).nodeValue().toInt() );
164                                         }
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
198                                         if ( !txtProperties.namedItem( "kdenlive-axis-x-inverted" ).isNull() )
199                                         {
200                                                 //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
201                                         }
202                                         if ( !txtProperties.namedItem( "kdenlive-axis-y-inverted" ).isNull() )
203                                         {
204                                                 //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
205                                         }
206
207                                         gitem = txt;
208                                 }
209                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsRectItem" )
210                                 {
211                                         QString rect = items.item( i ).namedItem( "content" ).attributes().namedItem( "rect" ).nodeValue();
212                                         QString br_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "brushcolor" ).nodeValue();
213                                         QString pen_str = items.item( i ).namedItem( "content" ).attributes().namedItem( "pencolor" ).nodeValue();
214                                         double penwidth = items.item( i ).namedItem( "content" ).attributes().namedItem( "penwidth" ).nodeValue().toDouble();
215                                         QGraphicsRectItem *rec = scene->addRect( stringToRect( rect ), QPen( QBrush( stringToColor( pen_str ) ), penwidth ), QBrush( stringToColor( br_str ) ) );
216                                         gitem = rec;
217                                 }
218                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsPixmapItem" )
219                                 {
220                                         const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
221                                         QImage img( url );
222                                         ImageItem *rec = new ImageItem(img);
223                                         scene->addItem( rec );
224                                         gitem = rec;
225                                 }
226                                 else if ( items.item( i ).attributes().namedItem( "type" ).nodeValue() == "QGraphicsSvgItem" )
227                                 {
228                                         const QString url = items.item( i ).namedItem( "content" ).attributes().namedItem( "url" ).nodeValue();
229                                         QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
230                                         scene->addItem(rec);
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         n = doc.documentElement().firstChildElement( "startviewport" );
264         if (!n.isNull())
265         {
266                 startRect = n.attributes().namedItem( "rect" ).nodeValue();
267         }
268         n = doc.documentElement().firstChildElement( "endviewport" );
269         if (!n.isNull())
270         {
271                 QString rect = n.attributes().namedItem( "rect" ).nodeValue();
272                 if (startRect != rect)
273                         mlt_properties_set( producer_props, "_endrect", rect.toUtf8().data() );
274         }
275         if (!startRect.isEmpty()) {
276                 mlt_properties_set( producer_props, "_startrect", startRect.toUtf8().data() );
277         }
278
279         return;
280 }
281
282
283 void drawKdenliveTitle( producer_ktitle self, mlt_frame frame, int width, int height, double position, int force_refresh )
284 {
285         // Obtain the producer 
286         mlt_producer producer = &self->parent;
287         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
288
289         // Obtain properties of frame
290         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
291
292         pthread_mutex_lock( &self->mutex );
293
294         QRectF start = stringToRect( QString( mlt_properties_get( producer_props, "_startrect" ) ) );
295         QRectF end = stringToRect( QString( mlt_properties_get( producer_props, "_endrect" ) ) );
296         
297         // Check if user wants us to reload the image
298         if ( force_refresh == 1 || width != self->current_width || height != self->current_height || !end.isNull())
299         {
300                 //mlt_cache_item_close( self->image_cache );
301                 
302                 self->current_image = NULL;
303                 mlt_properties_set_data( producer_props, "cached_image", NULL, 0, NULL, NULL );
304                 mlt_properties_set_int( producer_props, "force_reload", 0 );
305         }
306         
307         if (self->current_image == NULL) {
308                 // restore QGraphicsScene
309                 QGraphicsScene *scene = static_cast<QGraphicsScene *> (mlt_properties_get_data( producer_props, "qscene", NULL ));
310
311                 if ( force_refresh == 1 && scene )
312                 {
313                         scene = NULL;
314                         mlt_properties_set_data( producer_props, "qscene", NULL, 0, NULL, NULL );
315                 }
316  
317                 if ( scene == NULL )
318                 {
319                         int argc = 1;
320                         char* argv[1];
321                         argv[0] = "xxx";
322                         if (qApp) {
323                                 app = qApp;
324                         }
325                         else {
326                                 app=new QApplication( argc,argv ); //, QApplication::Tty );
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                 int originalWidth = mlt_properties_get_int( producer_props, "_original_width" );
334                 int originalHeight= mlt_properties_get_int( producer_props, "_original_height" );
335                 const QRectF source( 0, 0, width, height );
336                 if (start.isNull()) {
337                     start = QRectF( 0, 0, originalWidth, originalHeight );
338                 }
339         
340                 //must be extracted from kdenlive title
341                 QImage img( width, height, QImage::Format_ARGB32 );
342                 img.fill( 0 );
343                 QPainter p1;
344                 p1.begin( &img );
345                 p1.setRenderHints( QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing );
346                 //| QPainter::SmoothPixmapTransform );
347                 if (end.isNull())
348                 {
349                         scene->render( &p1, source, start, Qt::IgnoreAspectRatio );
350                 }
351                 else
352                 {
353                         QPointF topleft = start.topLeft() + ( end.topLeft() - start.topLeft() ) * position;
354                         QPointF bottomRight = start.bottomRight() + ( end.bottomRight() - start.bottomRight() ) * position;
355                         const QRectF r1( topleft, bottomRight );
356                         scene->render( &p1, source, r1, Qt::IgnoreAspectRatio );
357                 }
358                 p1.end();
359
360                 int size = width * height * 4;
361                 uint8_t *pointer=img.bits();
362                 QRgb* src = ( QRgb* ) pointer;
363                 self->current_image = ( uint8_t * )mlt_pool_alloc( size );
364                 uint8_t *dst = self->current_image;
365         
366                 for ( int i = 0; i < width * height * 4; i += 4 )
367                 {
368                         *dst++=qRed( *src );
369                         *dst++=qGreen( *src );
370                         *dst++=qBlue( *src );
371                         *dst++=qAlpha( *src );
372                         src++;
373                 }
374
375                 mlt_properties_set_data( producer_props, "cached_image", self->current_image, size, mlt_pool_release, NULL );
376                 self->current_width = width;
377                 self->current_height = height;
378         }
379
380         pthread_mutex_unlock( &self->mutex );
381         mlt_properties_set_int( properties, "width", self->current_width );
382         mlt_properties_set_int( properties, "height", self->current_height );
383 }
384
385