From 07fcaa5c4ee747bfc9599c193dad210a2f526e0a Mon Sep 17 00:00:00 2001 From: Josh Klontz Date: Wed, 25 Jun 2014 10:19:51 -0400 Subject: [PATCH] better handling of bad URLs in DownloadTransform and utGallery, for #207 --- openbr/plugins/gallery.cpp | 10 ++++------ openbr/plugins/misc.cpp | 37 ++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/openbr/plugins/gallery.cpp b/openbr/plugins/gallery.cpp index ea3cbd6..59abd15 100644 --- a/openbr/plugins/gallery.cpp +++ b/openbr/plugins/gallery.cpp @@ -238,20 +238,18 @@ class utGallery : public BinaryGallery void writeTemplate(const Template &t) { - if (t.empty()) - return; - - const QByteArray imageID = QByteArray::fromHex(t.file.get("ImageID")); + const QByteArray imageID = QByteArray::fromHex(t.file.get("ImageID", QByteArray(32, '0'))); if (imageID.size() != 16) qFatal("Expected 16-byte ImageID, got: %d bytes.", imageID.size()); - const int32_t algorithmID = t.file.get("AlgorithmID"); + const int32_t algorithmID = t.isEmpty() ? 0 : t.file.get("AlgorithmID"); QByteArray data; if (algorithmID == 5) { QDataStream stream(&data, QIODevice::WriteOnly); stream << t; } else { - data = QByteArray((const char*) t.m().data, t.m().rows * t.m().cols * t.m().elemSize()); + if (!t.empty()) + data = QByteArray((const char*) t.m().data, t.m().rows * t.m().cols * t.m().elemSize()); if (algorithmID == -1) { const QRectF frontalFace = t.file.get("FrontalFace"); diff --git a/openbr/plugins/misc.cpp b/openbr/plugins/misc.cpp index c7d14e9..96d218f 100644 --- a/openbr/plugins/misc.cpp +++ b/openbr/plugins/misc.cpp @@ -106,9 +106,6 @@ private: { dst.file = src.file; QString url = src.file.get("URL", src.file.name).simplified(); - if (url.isEmpty()) - return; - if (url.startsWith("file://")) url = url.mid(7); @@ -136,25 +133,27 @@ private: } } - if (!device) - return; + QByteArray data; + if (device) { + data = device->readAll(); + delete device; + device = NULL; + } - const QByteArray data = device->readAll(); - delete device; - device = NULL; + if (!data.isEmpty()) { + Mat encoded(1, data.size(), CV_8UC1, (void*)data.data()); + encoded = encoded.clone(); + if (mode == Permissive) { + dst += encoded; + } else { + Mat decoded = imdecode(encoded, IMREAD_UNCHANGED); + if (!decoded.empty()) + dst += (mode == Encoded) ? encoded : decoded; + } - Mat encoded(1, data.size(), CV_8UC1, (void*)data.data()); - encoded = encoded.clone(); - if (mode == Permissive) { - dst += encoded; - } else { - Mat decoded = imdecode(encoded, IMREAD_UNCHANGED); - if (!decoded.empty()) - dst += (mode == Encoded) ? encoded : decoded; + dst.file.set("ImageID", QVariant(QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex())); + dst.file.set("AlgorithmID", data.isEmpty() ? 0 : (mode == Decoded ? 5 : 3)); } - - dst.file.set("ImageID", QVariant(QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex())); - dst.file.set("AlgorithmID", mode == Decoded ? 5 : 3); } }; -- libgit2 0.21.4