1 /***************************************************************************
2 Copyright (C) 2006-2008
3 by Marco Gulino <marco@kmobiletools.org>
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.
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.
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 ***************************************************************************/
21 #include "westleypreview.h"
25 #include <QtCore/QVarLengthArray>
27 #include <kstandarddirs.h>
28 #include <krandomsequence.h>
29 #include <qdatetime.h>
34 #include <qfileinfo.h>
35 #include <KTemporaryFile>
44 KDE_EXPORT ThumbCreator *new_creator() {
45 return new MltPreview;
49 MltPreview::MltPreview() :
56 MltPreview::~MltPreview()
58 Mlt::Factory::close();
62 bool MltPreview::create(const QString &path, int width, int height, QImage &img)
64 Mlt::Profile *profile = new Mlt::Profile("dv_pal");
65 Mlt::Producer *producer = new Mlt::Producer(*profile, path.toUtf8().data());
68 if (producer->is_blank()) {
76 //img = getFrame(producer, frame, width, height);
78 while (variance <= 40 && ct < 4) {
79 img = getFrame(producer, frame, width, height);
80 variance = imageVariance(img);
87 return (img.isNull() == false);
90 QImage MltPreview::getFrame(Mlt::Producer *producer, int framepos, int /*width*/, int height)
93 if (producer == NULL) {
97 producer->seek(framepos);
98 Mlt::Frame *frame = producer->get_frame();
103 mlt_image_format format = mlt_image_rgb24a;
105 double ar = frame->get_double("aspect_ratio");
106 if (ar == 0.0) ar = 1.33;
107 int calculated_width = (int)((double) height * ar);
108 uint8_t *data = frame->get_image(format, calculated_width, height, 0);
109 QImage image((uchar *)data, calculated_width, height, QImage::Format_ARGB32);
111 if (!image.isNull()) {
112 result = image.rgbSwapped().convertToFormat(QImage::Format_RGB32);
120 uint MltPreview::imageVariance(QImage image)
122 if (image.isNull()) return 0;
125 uint bytes = image.numBytes();
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
140 // Second Step: calculate delta (average?)
141 for (uint i=0; i<STEPS; i++)
143 #if QT_VERSION >= 0x040700
144 int curdelta=abs(int(avg - pivot.at(i)));
146 int curdelta=abs(int(avg - pivot[i]));
150 return delta / STEPS;
153 ThumbCreator::Flags MltPreview::flags() const