]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
memhandling changed
[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  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include <QtGui/QImage>
21 #include <QtGui/QPainter>
22 #include <QtCore/QCoreApplication>
23 #include <QtGui/QApplication>
24 #include <QtCore/QDebug>
25 #include <QtCore/QFile>
26 #include <QtGui/QGraphicsView>
27 #include <QtGui/QGraphicsScene>
28 #include <QtGui/QGraphicsTextItem>
29 #include <QtGui/QGraphicsPolygonItem>
30 #include <QtGui/QTextCursor>
31 #include "kdenlivetitle_wrapper.h"
32 #include <framework/mlt_producer.h>
33 extern "C" {
34     void init_qt (const char* c){
35         titleclass=new Title(QString(c));
36     }
37     uint8_t* refresh_kdenlivetitle( uint8_t* buffer, int width, int height , double position){
38         void * cpy=malloc( width*height*4);
39         uint8_t* ret=titleclass->drawKdenliveTitle(buffer,width,height,position);
40         memcpy(cpy,ret,width*height*4);
41         return (uint8_t*)cpy;
42     }
43 }
44 Title::Title(const QString& filename):m_filename(filename),m_scene(NULL){
45     /*int argc=0;
46     char* argv[1];
47     argv[0]="xxx"; 
48     if (! QApplication::activeWindow())
49         app=new QApplication(argc,argv);
50     */
51     //must be extracted from kdenlive title
52     start =new QGraphicsPolygonItem(QPolygonF(QRectF(100, 100, 600, 600)));;
53     end=new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 300, 300)));;
54     //m_scene=new QGraphicsScene;
55     //loadDocument(filename,start,end);
56 }
57 uint8_t* Title::drawKdenliveTitle(uint8_t * buffer ,int width,int height,double position){
58     if (!m_scene){
59         int argc=0;
60         char* argv[1];
61         argv[0]="xxx"; 
62         //if (! QApplication::activeWindow())
63         if (!app)
64             app=new QApplication(argc,argv);
65         m_scene=new QGraphicsScene;
66         loadDocument(m_filename,start,end);
67     }
68     //must be extracted from kdenlive title
69     
70     //qDebug() << width << height;
71     QImage *img=new QImage(width,height,QImage::Format_ARGB32);
72     //QImage img((uchar*)buffer,width,height,width*4,QImage::Format_ARGB32);
73     img->fill(Qt::transparent);
74     //img.fill(66);
75     //memset(buffer,127,width*height+4);
76     QRectF rstart=start->boundingRect();
77     QRectF rend=end->boundingRect();
78     QPointF topleft=rstart.topLeft()+(rend.topLeft()-rstart.topLeft())*position;
79     QPointF bottomRight=rstart.bottomRight()+(rend.bottomRight()-rstart.bottomRight())*position;
80     QPainter p1;
81     p1.begin(img);
82     p1.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing|QPainter::SmoothPixmapTransform );
83     m_scene->render(&p1,QRect(0,0,width,height),QRectF(topleft,bottomRight));
84     p1.end();
85     uint8_t* pointer=img->bits();
86      for (int i=0;i<width*height*4;i+=4){
87             pointer=buffer+i;
88             uint8_t a=pointer[0],r=pointer[1],g=pointer[2],b=pointer[3];
89             pointer[0]=g;//g
90             pointer[1]=r;//r
91             pointer[2]=b;//b
92             pointer[3]=a;//a
93        }
94     return img->bits();
95     //qDebug() << img.hasAlphaChannel();
96     //img.save("test.png");
97 }
98 int Title::loadDocument(const QString& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
99 {
100     QDomDocument doc;
101     if (!m_scene)
102         return -1;
103
104         QFile file(url);
105         if (file.open(QIODevice::ReadOnly)) {
106             doc.setContent(&file, false);
107             file.close();
108         } 
109         return loadFromXml(doc, startv, endv);
110 }
111 int Title::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* /*startv*/, QGraphicsPolygonItem* /*endv*/)
112 {
113     QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
114     int maxZValue = 0;
115     if (titles.size()) {
116
117         QDomNodeList items = titles.item(0).childNodes();
118         for (int i = 0; i < items.count(); i++) {
119             QGraphicsItem *gitem = NULL;
120             int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
121             if (zValue > -1000) {
122                 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
123                     QDomNamedNodeMap txtProperties = items.item(i).namedItem("content").attributes();
124                     QFont font(txtProperties.namedItem("font").nodeValue());
125                     font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
126                     font.setItalic(txtProperties.namedItem("font-italic").nodeValue().toInt());
127                     font.setUnderline(txtProperties.namedItem("font-underline").nodeValue().toInt());
128                     // Older Kdenlive version did not store pixel size but point size
129                     if (txtProperties.namedItem("font-pixel-size").isNull()) {
130                         QFont f2;
131                         f2.setPointSize(txtProperties.namedItem("font-size").nodeValue().toInt());
132                         font.setPixelSize(QFontInfo(f2).pixelSize());
133                     } else
134                         font.setPixelSize(txtProperties.namedItem("font-pixel-size").nodeValue().toInt());
135                     QColor col(stringToColor(txtProperties.namedItem("font-color").nodeValue()));
136                     QGraphicsTextItem *txt = m_scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
137                     txt->setDefaultTextColor(col);
138                     txt->setTextInteractionFlags(Qt::NoTextInteraction);
139                     if (txtProperties.namedItem("alignment").isNull() == false) {
140                         txt->setTextWidth(txt->boundingRect().width());
141                         QTextCursor cur = txt->textCursor();
142                         QTextBlockFormat format = cur.blockFormat();
143                         format.setAlignment((Qt::Alignment) txtProperties.namedItem("alignment").nodeValue().toInt());
144                         cur.select(QTextCursor::Document);
145                         cur.setBlockFormat(format);
146                         txt->setTextCursor(cur);
147                         cur.clearSelection();
148                         txt->setTextCursor(cur);
149                     }
150
151                     if (!txtProperties.namedItem("kdenlive-axis-x-inverted").isNull()) {
152                         //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
153                     }
154                     if (!txtProperties.namedItem("kdenlive-axis-y-inverted").isNull()) {
155                         //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
156                     }
157
158                     gitem = txt;
159                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
160                     QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
161                     QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
162                     QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
163                     double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
164                     QGraphicsRectItem *rec = m_scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
165                     gitem = rec;
166                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
167                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
168                     QPixmap pix(url);
169                     QGraphicsPixmapItem *rec = m_scene->addPixmap(pix);
170                     rec->setData(Qt::UserRole, url);
171                     gitem = rec;
172                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
173                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
174                     //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
175                     //m_scene->addItem(rec);
176                     //rec->setData(Qt::UserRole, url);
177                     //gitem = rec;
178                 }
179             }
180             //pos and transform
181             if (gitem) {
182                 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
183                           items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
184                 gitem->setPos(p);
185                 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
186                 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
187                 if (zValue > maxZValue) maxZValue = zValue;
188                 gitem->setZValue(zValue);
189                 gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
190             }
191             if (items.item(i).nodeName() == "background") {
192                 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
193                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
194                 QList<QGraphicsItem *> items = m_scene->items();
195                 for (int i = 0; i < items.size(); i++) {
196                     if (items.at(i)->zValue() == -1100) {
197                         ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
198                         break;
199                     }
200                 }
201             } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
202                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
203                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
204                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
205                     kDebug() << width << rect;
206                     startv->setPolygon(rect);
207                     startv->setPos(p);
208                 } else if (items.item(i).nodeName() == "endviewport" && endv) {
209                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
210                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
211                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
212                     kDebug() << width << rect;
213                     endv->setPolygon(rect);
214                     endv->setPos(p);
215                 }*/
216         }
217     }
218     return maxZValue;
219 }
220
221 QString Title::colorToString(const QColor& c)
222 {
223     QString ret = "%1,%2,%3,%4";
224     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
225     return ret;
226 }
227
228 QString Title::rectFToString(const QRectF& c)
229 {
230     QString ret = "%1,%2,%3,%4";
231     ret = ret.arg(c.top()).arg(c.left()).arg(c.width()).arg(c.height());
232     return ret;
233 }
234
235 QRectF Title::stringToRect(const QString & s)
236 {
237
238     QStringList l = s.split(',');
239     if (l.size() < 4)
240         return QRectF();
241     return QRectF(l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(), l.at(3).toDouble()).normalized();
242 }
243
244 QColor Title::stringToColor(const QString & s)
245 {
246     QStringList l = s.split(',');
247     if (l.size() < 4)
248         return QColor();
249     return QColor(l.at(0).toInt(), l.at(1).toInt(), l.at(2).toInt(), l.at(3).toInt());;
250 }
251 QTransform Title::stringToTransform(const QString& s)
252 {
253     QStringList l = s.split(',');
254     if (l.size() < 9)
255         return QTransform();
256     return QTransform(
257                l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(),
258                l.at(3).toDouble(), l.at(4).toDouble(), l.at(5).toDouble(),
259                l.at(6).toDouble(), l.at(7).toDouble(), l.at(8).toDouble()
260            );
261 }