Commit 22db0c4b636f61a763435c6552a0ccdb6e8c0ffd

Authored by Charles Otto
1 parent c04a5c97

Use BlockCompression in loadstores and algorithm serialization

openbr/core/core.cpp
... ... @@ -110,9 +110,11 @@ struct AlgorithmCore
110 110  
111 111 void store(const QString &model) const
112 112 {
113   - // Create stream
114   - QByteArray data;
115   - QDataStream out(&data, QFile::WriteOnly);
  113 + QtUtils::BlockCompression compressedWrite;
  114 + QFile outFile(model);
  115 + compressedWrite.setBasis(&outFile);
  116 + QDataStream out(&compressedWrite);
  117 + compressedWrite.open(QFile::WriteOnly);
116 118  
117 119 // Serialize algorithm to stream
118 120 transform->serialize(out);
... ... @@ -131,18 +133,16 @@ struct AlgorithmCore
131 133 if (mode == TransformCompare)
132 134 comparison->serialize(out);
133 135  
134   - // Compress and save to file
135   - QtUtils::writeFile(model, data, -1);
  136 + compressedWrite.close();
136 137 }
137 138  
138 139 void load(const QString &model)
139 140 {
140   - // Load from file and decompress
141   - QByteArray data;
142   - QtUtils::readFile(model, data, true);
143   -
144   - // Create stream
145   - QDataStream in(&data, QFile::ReadOnly);
  141 + QtUtils::BlockCompression compressedRead;
  142 + QFile inFile(model);
  143 + compressedRead.setBasis(&inFile);
  144 + QDataStream in(&compressedRead);
  145 + compressedRead.open(QFile::ReadOnly);
146 146  
147 147 // Load algorithm
148 148 transform = QSharedPointer<Transform>(Transform::deserialize(in));
... ...
openbr/plugins/meta.cpp
... ... @@ -17,6 +17,8 @@
17 17 #include <QFutureSynchronizer>
18 18 #include <QRegularExpression>
19 19 #include <QtConcurrentRun>
  20 +#include <qbuffer.h>
  21 +
20 22 #include "openbr_internal.h"
21 23 #include "openbr/core/common.h"
22 24 #include "openbr/core/opencvutils.h"
... ... @@ -553,7 +555,7 @@ private:
553 555 return transform->timeVarying();
554 556 }
555 557  
556   - void train(const TemplateList &data)
  558 + void train(const QList<TemplateList> &data)
557 559 {
558 560 if (QFileInfo(getFileName()).exists())
559 561 return;
... ... @@ -561,11 +563,20 @@ private:
561 563 transform->train(data);
562 564  
563 565 qDebug("Storing %s", qPrintable(baseName));
564   - QByteArray byteArray;
565   - QDataStream stream(&byteArray, QFile::WriteOnly);
566   - stream << transform->description();
  566 + QtUtils::BlockCompression compressedOut;
  567 + QFile fout(baseName);
  568 + QtUtils::touchDir(fout);
  569 + compressedOut.setBasis(&fout);
  570 +
  571 + QDataStream stream(&compressedOut);
  572 + QString desc = transform->description();
  573 +
  574 + if (!compressedOut.open(QFile::WriteOnly))
  575 + qFatal("Failed to open %s for writing.", qPrintable(file));
  576 +
  577 + stream << desc;
567 578 transform->store(stream);
568   - QtUtils::writeFile(baseName, byteArray, -1);
  579 + compressedOut.close();
569 580 }
570 581  
571 582 void project(const Template &src, Template &dst) const
... ... @@ -606,12 +617,19 @@ private:
606 617 if (file.isEmpty()) return false;
607 618  
608 619 qDebug("Loading %s", qPrintable(file));
609   - QByteArray data;
610   - QtUtils::readFile(file, data, true);
611   - QDataStream stream(&data, QFile::ReadOnly);
  620 + QFile fin(file);
  621 + QtUtils::BlockCompression reader(&fin);
  622 + if (!reader.open(QIODevice::ReadOnly)) {
  623 + if (QFileInfo(file).exists()) qFatal("Unable to open %s for reading. Check file permissions.", qPrintable(file));
  624 + else qFatal("Unable to open %s for reading. File does not exist.", qPrintable(file));
  625 + }
  626 +
  627 + QDataStream stream(&reader);
612 628 stream >> transformString;
  629 +
613 630 transform = Transform::make(transformString);
614 631 transform->load(stream);
  632 +
615 633 return true;
616 634 }
617 635 };
... ...