Commit b423c5321d1417bd027ba079d6540487f2759baa

Authored by Josh Klontz
1 parent e9ff5ce4

remove download transform

Showing 1 changed file with 0 additions and 126 deletions
openbr/plugins/io/download.cpp deleted
1   -/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2   - * Copyright 2012 The MITRE Corporation *
3   - * *
4   - * Licensed under the Apache License, Version 2.0 (the "License"); *
5   - * you may not use this file except in compliance with the License. *
6   - * You may obtain a copy of the License at *
7   - * *
8   - * http://www.apache.org/licenses/LICENSE-2.0 *
9   - * *
10   - * Unless required by applicable law or agreed to in writing, software *
11   - * distributed under the License is distributed on an "AS IS" BASIS, *
12   - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13   - * See the License for the specific language governing permissions and *
14   - * limitations under the License. *
15   - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
16   -
17   -#include <QtNetwork>
18   -#include <opencv2/highgui/highgui.hpp>
19   -
20   -#include <openbr/plugins/openbr_internal.h>
21   -
22   -using namespace cv;
23   -
24   -namespace br
25   -{
26   -
27   -/*!
28   - * \ingroup transforms
29   - * \brief Downloads an image from a URL
30   - * \author Josh Klontz \cite jklontz
31   - */
32   -class DownloadTransform : public UntrainableMetaTransform
33   -{
34   - Q_OBJECT
35   - Q_ENUMS(Mode)
36   - Q_PROPERTY(Mode mode READ get_mode WRITE set_mode RESET reset_mode STORED false)
37   -
38   -public:
39   - enum Mode { Permissive,
40   - Encoded,
41   - Decoded };
42   -private:
43   - BR_PROPERTY(Mode, mode, Encoded)
44   -
45   - // The reasons for this data structure are as follows:
46   - // 1) The QNetworkAccessManager must be used in the thread that _created_ it,
47   - // hence the use of `QThreadStorage`.
48   - // 2) The QThreadStorage must be deleted _after_ the threads that added QNetworkAccessManager
49   - // to it are deleted, hence the `static` ensuring that `nam` is deleted at program termination,
50   - // long after the threads that created QNetworkAccessManager are deleted.
51   - static QThreadStorage<QNetworkAccessManager*> nam;
52   -
53   - void project(const Template &src, Template &dst) const
54   - {
55   - dst.file = src.file;
56   - QString url = src.file.get<QString>("URL", src.file.name).simplified();
57   - if (!url.contains("://"))
58   - url = "file://" + url;
59   - dst.file.set("URL", url);
60   -
61   - static const QRegularExpression regExp("file:///[A-Z]:/");
62   -
63   - if (url.contains(regExp))
64   - url = url.mid(8);
65   - else if (url.startsWith("file://"))
66   - url = url.mid(7);
67   -
68   - QIODevice *device = NULL;
69   - if (QFileInfo(url).exists()) {
70   - device = new QFile(url);
71   - device->open(QIODevice::ReadOnly);
72   - } else {
73   - if (!nam.hasLocalData())
74   - nam.setLocalData(new QNetworkAccessManager());
75   - const QUrl qURL(url, QUrl::StrictMode);
76   - if (qURL.isValid() && !qURL.isRelative()) {
77   - QNetworkRequest req = QNetworkRequest(qURL);
78   - req.setRawHeader("User-Agent", "br");
79   - QNetworkReply *reply = nam.localData()->get(req);
80   -
81   - reply->waitForReadyRead(-1);
82   - while (!reply->isFinished())
83   - QCoreApplication::processEvents();
84   -
85   - if (reply->error() != QNetworkReply::NoError) {
86   - qDebug() << reply->errorString() << url;
87   - reply->deleteLater();
88   - } else {
89   - device = reply;
90   - }
91   - }
92   - }
93   -
94   - QByteArray data;
95   - if (device) {
96   - data = device->readAll();
97   - delete device;
98   - device = NULL;
99   - }
100   -
101   - if (!data.isEmpty()) {
102   - Mat encoded(1, data.size(), CV_8UC1, (void*)data.data());
103   - encoded = encoded.clone();
104   - if (mode == Permissive) {
105   - dst += encoded;
106   - } else {
107   - Mat decoded = imdecode(encoded, IMREAD_UNCHANGED);
108   - if (!decoded.empty())
109   - dst += (mode == Encoded) ? encoded : decoded;
110   - }
111   -
112   - dst.file.set("ImageID", QVariant(QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()));
113   - dst.file.set("AlgorithmID", data.isEmpty() ? 0 : (mode == Decoded ? 5 : 3));
114   - } else {
115   - dst.file.fte = true;
116   - qWarning("Error opening %s", qPrintable(url));
117   - }
118   - }
119   -};
120   -QThreadStorage<QNetworkAccessManager*> DownloadTransform::nam;
121   -
122   -BR_REGISTER(Transform, DownloadTransform)
123   -
124   -} // namespace br
125   -
126   -#include "io/download.moc"