Commit 2967908e3e0176df3b16c083ff91e05020fef5f8
1 parent
ccc5d38c
vecGallery write done
Showing
1 changed file
with
49 additions
and
17 deletions
openbr/plugins/gallery/vec.cpp
| ... | ... | @@ -9,7 +9,7 @@ namespace br |
| 9 | 9 | * \author Scott Klum \cite sklum |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | -class vecGallery : public Gallery | |
| 12 | +class vecGallery : public FileGallery | |
| 13 | 13 | { |
| 14 | 14 | Q_OBJECT |
| 15 | 15 | |
| ... | ... | @@ -18,27 +18,56 @@ class vecGallery : public Gallery |
| 18 | 18 | BR_PROPERTY(int, width, 24) |
| 19 | 19 | BR_PROPERTY(int, height, 24) |
| 20 | 20 | |
| 21 | - TemplateList readBlock(bool *done) | |
| 21 | + QList<cv::Mat> mats; | |
| 22 | + | |
| 23 | + ~vecGallery() | |
| 22 | 24 | { |
| 23 | - *done = true; | |
| 25 | + if (mats.isEmpty()) | |
| 26 | + return; | |
| 27 | + | |
| 28 | + writeOpen(); | |
| 29 | + | |
| 30 | + // Write header | |
| 31 | + int count = mats.size(); | |
| 32 | + int size = width*height; | |
| 33 | + short temp = 0; | |
| 34 | + | |
| 35 | + const size_t write1 = f.write((char*)&count,sizeof(count)); | |
| 36 | + const size_t write2 = f.write((char*)&size,sizeof(size)); | |
| 37 | + const size_t write3 = f.write((char*)&temp,sizeof(temp)); | |
| 38 | + const size_t write4 = f.write((char*)&temp,sizeof(temp)); | |
| 39 | + | |
| 40 | + if (write1 != sizeof(count) || write2 != sizeof(size) || write3 != sizeof(temp) || write4 != sizeof(temp)) | |
| 41 | + qFatal("Failed to write header."); | |
| 42 | + | |
| 43 | + for (int i=0; i<count; i++) { | |
| 44 | + uchar tmp = 0; | |
| 45 | + const size_t write5 = f.write((char*)&tmp,sizeof(tmp)); | |
| 24 | 46 | |
| 25 | - QFile gallery; | |
| 26 | - gallery.setFileName(file); | |
| 27 | - if (!gallery.exists()) | |
| 28 | - qFatal("File %s does not exist", qPrintable(gallery.fileName())); | |
| 47 | + for (int r = 0; r < height; r++) | |
| 48 | + for (int c = 0; c < width; c++) { | |
| 49 | + short buffer = mats[i].ptr(r)[c]; | |
| 50 | + f.write((char*)&buffer, sizeof(buffer)); | |
| 51 | + } | |
| 52 | + } | |
| 53 | + | |
| 54 | + f.close(); | |
| 55 | + } | |
| 29 | 56 | |
| 30 | - QFile::OpenMode mode = QFile::ReadOnly; | |
| 31 | - if (!gallery.open(mode)) | |
| 32 | - qFatal("Can't open gallery: %s for reading", qPrintable(gallery.fileName())); | |
| 57 | + TemplateList readBlock(bool *done) | |
| 58 | + { | |
| 59 | + readOpen(); | |
| 60 | + | |
| 61 | + *done = true; | |
| 33 | 62 | |
| 34 | 63 | // Read header |
| 35 | 64 | int count, size; |
| 36 | 65 | short temp; |
| 37 | 66 | |
| 38 | - const size_t read1 = gallery.read((char*)&count,sizeof(count)); | |
| 39 | - const size_t read2 = gallery.read((char*)&size,sizeof(size)); | |
| 40 | - const size_t read3 = gallery.read((char*)&temp,sizeof(temp)); | |
| 41 | - const size_t read4 = gallery.read((char*)&temp,sizeof(temp)); | |
| 67 | + const size_t read1 = f.read((char*)&count,sizeof(count)); | |
| 68 | + const size_t read2 = f.read((char*)&size,sizeof(size)); | |
| 69 | + const size_t read3 = f.read((char*)&temp,sizeof(temp)); | |
| 70 | + const size_t read4 = f.read((char*)&temp,sizeof(temp)); | |
| 42 | 71 | |
| 43 | 72 | if (read1 != sizeof(count) || read2 != sizeof(size) || read3 != sizeof(temp) || read4 != sizeof(temp)) |
| 44 | 73 | qFatal("Failed to read header."); |
| ... | ... | @@ -52,8 +81,8 @@ class vecGallery : public Gallery |
| 52 | 81 | TemplateList templates; |
| 53 | 82 | for (int i=0; i<count; i++) { |
| 54 | 83 | uchar tmp = 0; |
| 55 | - const size_t read5 = gallery.read((char*)&tmp,sizeof(tmp)); | |
| 56 | - const size_t read6 = gallery.read((char*)vec,size*sizeof(short)); | |
| 84 | + const size_t read5 = f.read((char*)&tmp,sizeof(tmp)); | |
| 85 | + const size_t read6 = f.read((char*)vec,size*sizeof(short)); | |
| 57 | 86 | |
| 58 | 87 | if (read5 != sizeof(tmp) || read6 != size*sizeof(short)) |
| 59 | 88 | qFatal("Unable to read vector."); |
| ... | ... | @@ -72,7 +101,10 @@ class vecGallery : public Gallery |
| 72 | 101 | |
| 73 | 102 | void write(const Template &t) |
| 74 | 103 | { |
| 75 | - Q_UNUSED(t); | |
| 104 | + if (t.m().rows == height && t.m().cols == width) | |
| 105 | + mats.append(t); | |
| 106 | + else | |
| 107 | + qFatal("Matrix has incorrect width/height."); | |
| 76 | 108 | } |
| 77 | 109 | }; |
| 78 | 110 | ... | ... |