Commit 258c9f03a8fe9c435313c501a051d5fc0f474944
1 parent
93c7bb2f
Implemented vecGallery read
Showing
1 changed file
with
77 additions
and
0 deletions
openbr/plugins/gallery/vec.cpp
0 → 100644
| 1 | +#include <openbr/plugins/openbr_internal.h> | ||
| 2 | + | ||
| 3 | +namespace br | ||
| 4 | +{ | ||
| 5 | + | ||
| 6 | +class vecGallery : public Gallery | ||
| 7 | +{ | ||
| 8 | + Q_OBJECT | ||
| 9 | + | ||
| 10 | + // Property for image width/height | ||
| 11 | + Q_PROPERTY(int width READ get_width WRITE set_width RESET reset_width STORED false) | ||
| 12 | + Q_PROPERTY(int height READ get_height WRITE set_height RESET reset_height STORED false) | ||
| 13 | + BR_PROPERTY(int, width, 24) | ||
| 14 | + BR_PROPERTY(int, height, 24) | ||
| 15 | + | ||
| 16 | + TemplateList readBlock(bool *done) | ||
| 17 | + { | ||
| 18 | + *done = true; | ||
| 19 | + | ||
| 20 | + QFile gallery; | ||
| 21 | + gallery.setFileName(file); | ||
| 22 | + if (!gallery.exists()) | ||
| 23 | + qFatal("File %s does not exist", qPrintable(gallery.fileName())); | ||
| 24 | + | ||
| 25 | + QFile::OpenMode mode = QFile::ReadOnly; | ||
| 26 | + if (!gallery.open(mode)) | ||
| 27 | + qFatal("Can't open gallery: %s for reading", qPrintable(gallery.fileName())); | ||
| 28 | + | ||
| 29 | + // Read header | ||
| 30 | + int count, size; | ||
| 31 | + short temp; | ||
| 32 | + | ||
| 33 | + const size_t read1 = gallery.read((char*)&count,sizeof(count)); | ||
| 34 | + const size_t read2 = gallery.read((char*)&size,sizeof(size)); | ||
| 35 | + const size_t read3 = gallery.read((char*)&temp,sizeof(temp)); | ||
| 36 | + const size_t read4 = gallery.read((char*)&temp,sizeof(temp)); | ||
| 37 | + | ||
| 38 | + if (read1 != sizeof(count) || read2 != sizeof(size) || read3 != sizeof(temp) || read4 != sizeof(temp)) | ||
| 39 | + qFatal("Failed to read header."); | ||
| 40 | + | ||
| 41 | + if (size != width*height) | ||
| 42 | + qFatal("Width*height != vector size."); | ||
| 43 | + | ||
| 44 | + // Read content | ||
| 45 | + short *vec = new short[size]; | ||
| 46 | + | ||
| 47 | + TemplateList templates; | ||
| 48 | + for (int i=0; i<count; i++) { | ||
| 49 | + uchar tmp = 0; | ||
| 50 | + const size_t read5 = gallery.read((char*)&tmp,sizeof(tmp)); | ||
| 51 | + const size_t read6 = gallery.read((char*)vec,size*sizeof(short)); | ||
| 52 | + | ||
| 53 | + if (read5 != sizeof(tmp) || read6 != size*sizeof(short)) | ||
| 54 | + qFatal("Unable to read vector."); | ||
| 55 | + | ||
| 56 | + cv::Mat m(height, width, CV_8UC1); | ||
| 57 | + for (int r = 0; r < height; r++) | ||
| 58 | + for (int c = 0; c < width; c++) | ||
| 59 | + m.ptr(r)[c] = (uchar)vec[r*width+c]; | ||
| 60 | + templates.append(Template(m)); | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + return templates; | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + void write(const Template &t) | ||
| 67 | + { | ||
| 68 | + Q_UNUSED(t); | ||
| 69 | + } | ||
| 70 | +}; | ||
| 71 | + | ||
| 72 | +BR_REGISTER(Gallery, vecGallery) | ||
| 73 | + | ||
| 74 | +} // namespace br | ||
| 75 | + | ||
| 76 | +#include "gallery/vec.moc" | ||
| 77 | + |