]> git.sesse.net Git - kdenlive/blob - thumbnailer/westleypreview.cpp
const'ref. Fix indent
[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.h>
24 #include <qimage.h>
25 #include <QtCore/QVarLengthArray>
26
27 #include <kstandarddirs.h>
28 #include <krandomsequence.h>
29 #include <qdatetime.h>
30 #include <QColor>
31 #include <kdebug.h>
32 #include <ktempdir.h>
33 #include <kurl.h>
34 #include <qfileinfo.h>
35 #include <KTemporaryFile>
36
37 #include <unistd.h>
38
39
40 #define DBG_AREA
41
42 //#include "config.h"
43 extern "C" {
44 KDE_EXPORT ThumbCreator *new_creator() {
45     return new MltPreview;
46 }
47 }
48
49 MltPreview::MltPreview() :
50     QObject(),
51     ThumbCreator()
52 {
53     Mlt::Factory::init();
54 }
55
56 MltPreview::~MltPreview()
57 {
58     Mlt::Factory::close();
59 }
60
61
62 bool MltPreview::create(const QString &path, int width, int height, QImage &img)
63 {
64     Mlt::Profile *profile = new Mlt::Profile("dv_pal");
65     Mlt::Producer *producer = new Mlt::Producer(*profile, path.toUtf8().data());
66
67
68     if (producer->is_blank()) {
69         delete producer;
70         delete profile;
71         return false;
72     }
73     int frame = 75;
74     uint variance = 10;
75     int ct = 1;
76
77     //img = getFrame(producer, frame, width, height);
78
79     while (variance <= 40 && ct < 4) {
80         img = getFrame(producer, frame, width, height);
81         variance = imageVariance(img);
82         frame += 100 * ct;
83         ct++;
84     }
85
86     delete producer;
87     delete profile;
88     return (img.isNull() == false);
89 }
90
91 QImage MltPreview::getFrame(Mlt::Producer *producer, int framepos, int /*width*/, int height)
92 {
93     QImage result;
94     if (producer == NULL) {
95         return result;
96     }
97
98     producer->seek(framepos);
99     Mlt::Frame *frame = producer->get_frame();
100     if (frame == NULL) {
101         return result;
102     }
103
104     mlt_image_format format = mlt_image_rgb24a;
105     height = 200;
106     double ar = frame->get_double("aspect_ratio");
107     if (ar == 0.0) ar = 1.33;
108     int calculated_width = (int)((double) height * ar);
109     uint8_t *data = frame->get_image(format, calculated_width, height, 0);
110     QImage image((uchar *)data, calculated_width, height, QImage::Format_ARGB32);
111
112     if (!image.isNull()) {
113         result = image.rgbSwapped().convertToFormat(QImage::Format_RGB32);
114     }
115
116     delete frame;
117     return result;
118 }
119
120
121 uint MltPreview::imageVariance(const QImage &image)
122 {
123     if (image.isNull()) return 0;
124     uint delta = 0;
125     uint avg = 0;
126     uint bytes = image.numBytes();
127     if (bytes == 0) return 0;
128     uint STEPS = bytes / 2;
129     QVarLengthArray<uchar> pivot(STEPS);
130     kDebug(DBG_AREA) << "Using " << STEPS << " steps\n";
131     const uchar *bits=image.bits();
132     // First pass: get pivots and taking average
133     for( uint i=0; i<STEPS ; i++ ){
134         pivot[i] = bits[2 * i];
135 #if QT_VERSION >= 0x040700
136         avg+=pivot.at(i);
137 #else
138         avg+=pivot[i];
139 #endif
140     }
141     avg=avg/STEPS;
142     // Second Step: calculate delta (average?)
143     for (uint i=0; i<STEPS; i++)
144     {
145 #if QT_VERSION >= 0x040700
146         int curdelta=abs(int(avg - pivot.at(i)));
147 #else
148         int curdelta=abs(int(avg - pivot[i]));
149 #endif
150         delta+=curdelta;
151     }
152     return delta / STEPS;
153 }
154
155 ThumbCreator::Flags MltPreview::flags() const
156 {
157     return None;
158 }
159
160
161
162 #include "westleypreview.moc"