From 318e194dc6f1fd8277c2bd4f8fdea9c21f582cd3 Mon Sep 17 00:00:00 2001 From: Josh Klontz Date: Fri, 13 Jun 2014 11:43:21 -0400 Subject: [PATCH] first draft of DownloadTransform --- openbr/plugins/misc.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+), 0 deletions(-) diff --git a/openbr/plugins/misc.cpp b/openbr/plugins/misc.cpp index bae5b5e..e78b2a7 100644 --- a/openbr/plugins/misc.cpp +++ b/openbr/plugins/misc.cpp @@ -14,6 +14,7 @@ * limitations under the License. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +#include #include #include #include @@ -83,6 +84,77 @@ BR_REGISTER(Transform, DecodeTransform) /*! * \ingroup transforms + * \brief Downloads an image from a URL + * \author Josh Klontz \cite jklontz + */ +class DownloadTransform : public UntrainableMetaTransform +{ + Q_OBJECT + Q_ENUMS(Mode) + +public: + enum Mode { Permissive, + Encoded, + Decoded }; +private: + BR_PROPERTY(Mode, mode, Encoded) + + mutable QNetworkAccessManager nam; + mutable QMutex namLock; + + void project(const Template &src, Template &dst) const + { + dst.file = src.file; + QString url = src.file.get("URL").simplified(); + if (url.isEmpty()) + return; + + if (url.startsWith("file://")) + url = url.mid(7); + + QIODevice *device = NULL; + if (QFileInfo(url).exists()) { + device = new QFile(url); + device->open(QIODevice::ReadOnly); + } else { + namLock.lock(); + QNetworkReply *reply = nam.get(QNetworkRequest(url)); + namLock.unlock(); + + reply->waitForReadyRead(-1); + while (!reply->isFinished()) + QCoreApplication::processEvents(); + + if (reply->error() != QNetworkReply::NoError) { + qDebug() << reply->errorString() << url; + reply->deleteLater(); + } else { + device = reply; + } + } + + if (!device) + return; + + const QByteArray data = device->readAll(); + delete device; + device = NULL; + + Mat encoded(1, data.size(), CV_8UC1, (void*)data.data()); + if (mode == Permissive) { + dst += encoded; + } else { + Mat decoded = imdecode(encoded, IMREAD_UNCHANGED); + if (!decoded.empty()) + dst += (mode == Encoded) ? encoded : decoded; + } + } +}; + +BR_REGISTER(Transform, DownloadTransform) + +/*! + * \ingroup transforms * \brief Prints the template's file to stdout or stderr. * \author Josh Klontz \cite jklontz */ -- libgit2 0.21.4