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