Commit 645574023915fd84adcbc6663a84c168aab1f712
1 parent
816a598d
improved matrix deserialization for streams
Showing
1 changed file
with
10 additions
and
5 deletions
openbr/core/opencvutils.cpp
| @@ -362,13 +362,18 @@ QDataStream &operator>>(QDataStream &stream, Mat &m) | @@ -362,13 +362,18 @@ QDataStream &operator>>(QDataStream &stream, Mat &m) | ||
| 362 | stream >> rows >> cols >> type; | 362 | stream >> rows >> cols >> type; |
| 363 | m.create(rows, cols, type); | 363 | m.create(rows, cols, type); |
| 364 | 364 | ||
| 365 | - // Read data | ||
| 366 | int len; | 365 | int len; |
| 367 | stream >> len; | 366 | stream >> len; |
| 368 | - if (len > 0) { | ||
| 369 | - if (!m.isContinuous()) qFatal("Can't deserialize non-continuous matrices."); | ||
| 370 | - const int read = stream.readRawData((char*)m.data, len); | ||
| 371 | - if (read != len) qFatal("Mat deserialization failure, exptected: %d bytes, read: %d bytes.", len, read); | 367 | + char *data = (char*) m.data; |
| 368 | + | ||
| 369 | + // In certain circumstances, like reading from stdin or sockets, we may not | ||
| 370 | + // be given all the data we need at once because it isn't available yet. | ||
| 371 | + // So we loop until it we get it. | ||
| 372 | + while (len > 0) { | ||
| 373 | + const int read = stream.readRawData(data, len); | ||
| 374 | + if (read == -1) qFatal("Mat deserialization failure, exptected %d more bytes.", len); | ||
| 375 | + data += read; | ||
| 376 | + len -= read; | ||
| 372 | } | 377 | } |
| 373 | return stream; | 378 | return stream; |
| 374 | } | 379 | } |