]> git.sesse.net Git - kdenlive/blob - thumbnailer/westleypreview.cpp
Fix label
[kdenlive] / thumbnailer / westleypreview.cpp
1 /***************************************************************************
2    Copyright (C) 2006-2008
3    by Marco Gulino <marco@kmobiletools.org>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program 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
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the
17    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18     Boston, MA 02110-1301, USA.
19  ***************************************************************************/
20
21 #include "westleypreview.h"
22
23 #include <QFile>
24 #include <QImage>
25 #include <QVarLengthArray>
26
27 #include <kstandarddirs.h>
28 #include <krandomsequence.h>
29 #include <QDateTime>
30 #include <kdebug.h>
31 #include <ktempdir.h>
32 #include <kurl.h>
33 #include <QFileInfo>
34
35 #include <unistd.h>
36
37
38 #define DBG_AREA
39
40 //#include "config.h"
41 extern "C" {
42 KDE_EXPORT ThumbCreator *new_creator() {
43     return new MltPreview;
44 }
45 }
46
47 MltPreview::MltPreview() :
48     QObject(),
49     ThumbCreator()
50 {
51     Mlt::Factory::init();
52 }
53
54 MltPreview::~MltPreview()
55 {
56     Mlt::Factory::close();
57 }
58
59
60 bool MltPreview::create(const QString &path, int width, int height, QImage &img)
61 {
62     Mlt::Profile *profile = new Mlt::Profile("dv_pal");
63     Mlt::Producer *producer = new Mlt::Producer(*profile, path.toUtf8().data());
64
65
66     if (producer->is_blank()) {
67         delete producer;
68         delete profile;
69         return false;
70     }
71     int frame = 75;
72     uint variance = 10;
73     int ct = 1;
74
75     //img = getFrame(producer, frame, width, height);
76
77     while (variance <= 40 && ct < 4) {
78         img = getFrame(producer, frame, width, height);
79         variance = imageVariance(img);
80         frame += 100 * ct;
81         ct++;
82     }
83
84     delete producer;
85     delete profile;
86     return (img.isNull() == false);
87 }
88
89 QImage MltPreview::getFrame(Mlt::Producer *producer, int framepos, int /*width*/, int height)
90 {
91     QImage result;
92     if (producer == NULL) {
93         return result;
94     }
95
96     producer->seek(framepos);
97     Mlt::Frame *frame = producer->get_frame();
98     if (frame == NULL) {
99         return result;
100     }
101
102     mlt_image_format format = mlt_image_rgb24a;
103     height = 200;
104     double ar = frame->get_double("aspect_ratio");
105     if (ar == 0.0) ar = 1.33;
106     int calculated_width = (int)((double) height * ar);
107     uint8_t *data = frame->get_image(format, calculated_width, height, 0);
108     QImage image((uchar *)data, calculated_width, height, QImage::Format_ARGB32);
109
110     if (!image.isNull()) {
111         result = image.rgbSwapped().convertToFormat(QImage::Format_RGB32);
112     }
113
114     delete frame;
115     return result;
116 }
117
118
119 uint MltPreview::imageVariance(const QImage &image)
120 {
121     if (image.isNull()) return 0;
122     uint delta = 0;
123     uint avg = 0;
124     uint bytes = image.numBytes();
125     if (bytes == 0) return 0;
126     uint STEPS = bytes / 2;
127     QVarLengthArray<uchar> pivot(STEPS);
128     kDebug(DBG_AREA) << "Using " << STEPS << " steps\n";
129     const uchar *bits=image.bits();
130     // First pass: get pivots and taking average
131     for( uint i=0; i<STEPS ; i++ ){
132         pivot[i] = bits[2 * i];
133 #if QT_VERSION >= 0x040700
134         avg+=pivot.at(i);
135 #else
136         avg+=pivot[i];
137 #endif
138     }
139     avg=avg/STEPS;
140     // Second Step: calculate delta (average?)
141     for (uint i=0; i<STEPS; i++)
142     {
143 #if QT_VERSION >= 0x040700
144         int curdelta=abs(int(avg - pivot.at(i)));
145 #else
146         int curdelta=abs(int(avg - pivot[i]));
147 #endif
148         delta+=curdelta;
149     }
150     return delta / STEPS;
151 }
152
153 ThumbCreator::Flags MltPreview::flags() const
154 {
155     return None;
156 }
157
158
159
160 #include "westleypreview.moc"