]> git.sesse.net Git - mlt/blob - src/modules/qimage/kdenlivetitle_wrapper.cpp
memcpy works now
[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     void refresh_kdenlivetitle( uint8_t* buffer, int width, int height , double position){
38         QImage* ret=titleclass->drawKdenliveTitle(buffer,width,height,position);
39         memcpy(buffer,ret->bits(),width*height*4);
40         delete(ret);
41         //return (uint8_t*)cpy;
42     }
43 }
44 Title::Title(const QString& filename):m_filename(filename),m_scene(NULL){
45     //must be extracted from kdenlive title
46     start =new QGraphicsPolygonItem(QPolygonF(QRectF(100, 100, 600, 600)));;
47     end=new QGraphicsPolygonItem(QPolygonF(QRectF(0, 0, 300, 300)));;
48 }
49 QImage* Title::drawKdenliveTitle(uint8_t * buffer ,int width,int height,double position){
50     if (!m_scene){
51         int argc=0;
52         char* argv[1];
53         argv[0]="xxx"; 
54         if (! QApplication::activeWindow())
55         //if (!app)
56             app=new QApplication(argc,argv);
57         m_scene=new QGraphicsScene;
58         loadDocument(m_filename,start,end);
59     }
60     //must be extracted from kdenlive title
61     
62     //qDebug() << width << height;
63     QImage *img=new QImage(width,height,QImage::Format_ARGB32);
64     //QImage img((uchar*)buffer,width,height,width*4,QImage::Format_ARGB32);
65     //img->fill(Qt::transparent);
66     //memset(buffer,127,width*height+4);
67     QRectF rstart=start->boundingRect();
68     QRectF rend=end->boundingRect();
69     QPointF topleft=rstart.topLeft()+(rend.topLeft()-rstart.topLeft())*position;
70     QPointF bottomRight=rstart.bottomRight()+(rend.bottomRight()-rstart.bottomRight())*position;
71     QPainter p1;
72     p1.begin(img);
73     p1.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::HighQualityAntialiasing);//|QPainter::SmoothPixmapTransform );
74     m_scene->render(&p1,QRect(0,0,width,height),QRectF(topleft,bottomRight));
75     p1.end();
76     uint8_t* pointer=img->bits();
77      for (int i=0;i<width*height*4;i+=4){
78             uint8_t v=i>11000 && i< 35000 ?255:0; 
79             uint8_t a=pointer[0],r=pointer[1],g=pointer[2],b=pointer[3];
80             pointer[0]=g;//g
81             pointer[1]=r;//r
82             pointer[2]=b;//b
83             pointer[3]=a;//a
84             //qDebug() << a;
85             pointer+=4;
86        }
87     img->save("test.png");
88     return img;
89     //qDebug() << img.hasAlphaChannel();
90 }
91 int Title::loadDocument(const QString& url, QGraphicsPolygonItem* startv, QGraphicsPolygonItem* endv)
92 {
93     QDomDocument doc;
94     if (!m_scene)
95         return -1;
96
97         QFile file(url);
98         if (file.open(QIODevice::ReadOnly)) {
99             doc.setContent(&file, false);
100             file.close();
101         } 
102         return loadFromXml(doc, startv, endv);
103 }
104 int Title::loadFromXml(QDomDocument doc, QGraphicsPolygonItem* /*startv*/, QGraphicsPolygonItem* /*endv*/)
105 {
106     QDomNodeList titles = doc.elementsByTagName("kdenlivetitle");
107     int maxZValue = 0;
108     if (titles.size()) {
109
110         QDomNodeList items = titles.item(0).childNodes();
111         for (int i = 0; i < items.count(); i++) {
112             QGraphicsItem *gitem = NULL;
113             int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
114             if (zValue > -1000) {
115                 if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsTextItem") {
116                     QDomNamedNodeMap txtProperties = items.item(i).namedItem("content").attributes();
117                     QFont font(txtProperties.namedItem("font").nodeValue());
118                     font.setBold(txtProperties.namedItem("font-bold").nodeValue().toInt());
119                     font.setItalic(txtProperties.namedItem("font-italic").nodeValue().toInt());
120                     font.setUnderline(txtProperties.namedItem("font-underline").nodeValue().toInt());
121                     // Older Kdenlive version did not store pixel size but point size
122                     if (txtProperties.namedItem("font-pixel-size").isNull()) {
123                         QFont f2;
124                         f2.setPointSize(txtProperties.namedItem("font-size").nodeValue().toInt());
125                         font.setPixelSize(QFontInfo(f2).pixelSize());
126                     } else
127                         font.setPixelSize(txtProperties.namedItem("font-pixel-size").nodeValue().toInt());
128                     QColor col(stringToColor(txtProperties.namedItem("font-color").nodeValue()));
129                     QGraphicsTextItem *txt = m_scene->addText(items.item(i).namedItem("content").firstChild().nodeValue(), font);
130                     txt->setDefaultTextColor(col);
131                     txt->setTextInteractionFlags(Qt::NoTextInteraction);
132                     if (txtProperties.namedItem("alignment").isNull() == false) {
133                         txt->setTextWidth(txt->boundingRect().width());
134                         QTextCursor cur = txt->textCursor();
135                         QTextBlockFormat format = cur.blockFormat();
136                         format.setAlignment((Qt::Alignment) txtProperties.namedItem("alignment").nodeValue().toInt());
137                         cur.select(QTextCursor::Document);
138                         cur.setBlockFormat(format);
139                         txt->setTextCursor(cur);
140                         cur.clearSelection();
141                         txt->setTextCursor(cur);
142                     }
143
144                     if (!txtProperties.namedItem("kdenlive-axis-x-inverted").isNull()) {
145                         //txt->setData(OriginXLeft, txtProperties.namedItem("kdenlive-axis-x-inverted").nodeValue().toInt());
146                     }
147                     if (!txtProperties.namedItem("kdenlive-axis-y-inverted").isNull()) {
148                         //txt->setData(OriginYTop, txtProperties.namedItem("kdenlive-axis-y-inverted").nodeValue().toInt());
149                     }
150
151                     gitem = txt;
152                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsRectItem") {
153                     QString rect = items.item(i).namedItem("content").attributes().namedItem("rect").nodeValue();
154                     QString br_str = items.item(i).namedItem("content").attributes().namedItem("brushcolor").nodeValue();
155                     QString pen_str = items.item(i).namedItem("content").attributes().namedItem("pencolor").nodeValue();
156                     double penwidth = items.item(i).namedItem("content").attributes().namedItem("penwidth").nodeValue().toDouble();
157                     QGraphicsRectItem *rec = m_scene->addRect(stringToRect(rect), QPen(QBrush(stringToColor(pen_str)), penwidth), QBrush(stringToColor(br_str)));
158                     gitem = rec;
159                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsPixmapItem") {
160                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
161                     QPixmap pix(url);
162                     QGraphicsPixmapItem *rec = m_scene->addPixmap(pix);
163                     rec->setData(Qt::UserRole, url);
164                     gitem = rec;
165                 } else if (items.item(i).attributes().namedItem("type").nodeValue() == "QGraphicsSvgItem") {
166                     QString url = items.item(i).namedItem("content").attributes().namedItem("url").nodeValue();
167                     //QGraphicsSvgItem *rec = new QGraphicsSvgItem(url);
168                     //m_scene->addItem(rec);
169                     //rec->setData(Qt::UserRole, url);
170                     //gitem = rec;
171                 }
172             }
173             //pos and transform
174             if (gitem) {
175                 QPointF p(items.item(i).namedItem("position").attributes().namedItem("x").nodeValue().toDouble(),
176                           items.item(i).namedItem("position").attributes().namedItem("y").nodeValue().toDouble());
177                 gitem->setPos(p);
178                 gitem->setTransform(stringToTransform(items.item(i).namedItem("position").firstChild().firstChild().nodeValue()));
179                 int zValue = items.item(i).attributes().namedItem("z-index").nodeValue().toInt();
180                 if (zValue > maxZValue) maxZValue = zValue;
181                 gitem->setZValue(zValue);
182                 //gitem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
183             }
184             if (items.item(i).nodeName() == "background") {
185                 QColor color = QColor(stringToColor(items.item(i).attributes().namedItem("color").nodeValue()));
186                 //color.setAlpha(items.item(i).attributes().namedItem("alpha").nodeValue().toInt());
187                 QList<QGraphicsItem *> items = m_scene->items();
188                 for (int i = 0; i < items.size(); i++) {
189                     if (items.at(i)->zValue() == -1100) {
190                         ((QGraphicsRectItem *)items.at(i))->setBrush(QBrush(color));
191                         break;
192                     }
193                 }
194             } /*else if (items.item(i).nodeName() == "startviewport" && startv) {
195                     QPointF p(items.item(i).attributes().namedItem("x").nodeValue().toDouble(), items.item(i).attributes().namedItem("y").nodeValue().toDouble());
196                     double width = items.item(i).attributes().namedItem("size").nodeValue().toDouble();
197                     QRectF rect(-width, -width / aspect_ratio, width*2.0, width / aspect_ratio*2.0);
198                     kDebug() << width << rect;
199                     startv->setPolygon(rect);
200                     startv->setPos(p);
201                 } else if (items.item(i).nodeName() == "endviewport" && endv) {
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                     endv->setPolygon(rect);
207                     endv->setPos(p);
208                 }*/
209         }
210     }
211     return maxZValue;
212 }
213
214 QString Title::colorToString(const QColor& c)
215 {
216     QString ret = "%1,%2,%3,%4";
217     ret = ret.arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
218     return ret;
219 }
220
221 QString Title::rectFToString(const QRectF& c)
222 {
223     QString ret = "%1,%2,%3,%4";
224     ret = ret.arg(c.top()).arg(c.left()).arg(c.width()).arg(c.height());
225     return ret;
226 }
227
228 QRectF Title::stringToRect(const QString & s)
229 {
230
231     QStringList l = s.split(',');
232     if (l.size() < 4)
233         return QRectF();
234     return QRectF(l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(), l.at(3).toDouble()).normalized();
235 }
236
237 QColor Title::stringToColor(const QString & s)
238 {
239     QStringList l = s.split(',');
240     if (l.size() < 4)
241         return QColor();
242     return QColor(l.at(0).toInt(), l.at(1).toInt(), l.at(2).toInt(), l.at(3).toInt());;
243 }
244 QTransform Title::stringToTransform(const QString& s)
245 {
246     QStringList l = s.split(',');
247     if (l.size() < 9)
248         return QTransform();
249     return QTransform(
250                l.at(0).toDouble(), l.at(1).toDouble(), l.at(2).toDouble(),
251                l.at(3).toDouble(), l.at(4).toDouble(), l.at(5).toDouble(),
252                l.at(6).toDouble(), l.at(7).toDouble(), l.at(8).toDouble()
253            );
254 }