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