#include <QImage>
#include <QApplication>
#include <QtConcurrentRun>
-
+#include <QVarLengthArray>
KThumb::KThumb(ClipManager *clipManager, KUrl url, const QString &id, const QString &hash, QObject * parent, const char */*name*/) :
QObject(parent),
delete frame;
return p;
}
+
+
+//static
+uint KThumb::imageVariance(QImage image )
+{
+ uint delta=0;
+ uint avg=0;
+ uint bytes=image.numBytes();
+ uint STEPS=bytes/2;
+ QVarLengthArray<uchar> pivot(STEPS);
+ uchar *bits=image.bits();
+ // First pass: get pivots and taking average
+ for( uint i=0; i<STEPS ; i++ ){
+ pivot[i]=bits[i*(bytes/STEPS)];
+ avg+=pivot[i];
+ }
+ avg=avg/STEPS;
+ // Second Step: calculate delta (average?)
+ for (uint i=0; i<STEPS; i++)
+ {
+ int curdelta=abs(int(avg-pivot[i]));
+ delta+=curdelta;
+ }
+ return delta/STEPS;
+}
+
/*
void KThumb::getImage(KUrl url, int frame, int width, int height)
{
void getAudioThumbs(int channel, double frame, double frameLength, int arrayWidth);
static QPixmap getImage(KUrl url, int frame, int width, int height);
static QImage getFrame(Mlt::Producer *producer, int framepos, int width, int height);
+ /** @brief Calculates image variance, useful to know if a thumbnail is interesting.
+ * @return an integer between 0 and 100. 0 means no variance, eg. black image while bigger values mean contrasted image
+ * */
+ static uint imageVariance(QImage image);
private slots:
void slotAudioThumbOver();
else
filePropertyMap["type"] = "video";
+ int variance;
mlt_image_format format = mlt_image_rgb24a;
int frame_width = width;
int frame_height = imageHeight;
- uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
- QImage image((uchar *)data, frame_width, frame_height, QImage::Format_ARGB32_Premultiplied);
QPixmap pix;
-
- if (!image.isNull()) {
- if (frame_width > (2 * width)) {
- // there was a scaling problem, do it manually
- QImage scaled = image.scaled(width, imageHeight);
- pix = QPixmap::fromImage(scaled.rgbSwapped());
- } else pix = QPixmap::fromImage(image.rgbSwapped());
- } else
- pix.fill(Qt::black);
-
+ do {
+ variance = 100;
+ uint8_t *data = frame->get_image(format, frame_width, frame_height, 0);
+ QImage image((uchar *)data, frame_width, frame_height, QImage::Format_ARGB32_Premultiplied);
+
+ if (!image.isNull()) {
+ if (frame_width > (2 * width)) {
+ // there was a scaling problem, do it manually
+ QImage scaled = image.scaled(width, imageHeight);
+ pix = QPixmap::fromImage(scaled.rgbSwapped());
+ } else pix = QPixmap::fromImage(image.rgbSwapped());
+ variance = KThumb::imageVariance(image);
+ } else
+ pix.fill(Qt::black);
+
+ if (frameNumber == 0 && variance < 6) {
+ // Thumbnail is not interesting (for example all black, seek to fetch better thumb
+ frameNumber = 100;
+ producer->seek(frameNumber);
+ delete frame;
+ frame = producer->get_frame();
+ variance = -1;
+ }
+ } while (variance == -1);
emit replyGetImage(clipId, pix);
} else if (frame->get_int("test_audio") == 0) {