cv::Mat uncompressData(const unsigned char * bytes, unsigned long size) { cv::Mat data; if(bytes && size>=3*sizeof(int)) { //last 3 int elements are matrix size and type int height = *((int*)&bytes[size-3*sizeof(int)]); int width = *((int*)&bytes[size-2*sizeof(int)]); int type = *((int*)&bytes[size-1*sizeof(int)]); data = cv::Mat(height, width, type); uLongf totalUncompressed = uLongf(data.total())*uLongf(data.elemSize()); int errCode = uncompress( (Bytef*)data.data, &totalUncompressed, (const Bytef*)bytes, uLong(size)); if(errCode == Z_MEM_ERROR) { UERROR("Z_MEM_ERROR : Insufficient memory."); } else if(errCode == Z_BUF_ERROR) { UERROR("Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data."); } else if(errCode == Z_DATA_ERROR) { UERROR("Z_DATA_ERROR : The compressed data (referenced by source) was corrupted."); } } return data; }
cv::Mat uncompressData(const unsigned char * bytes, unsigned long size) { cv::Mat data; if(bytes && size>=3*sizeof(int)) { //last 3 int elements are matrix size and type int height = *((int*)&bytes[size-3*sizeof(int)]); int width = *((int*)&bytes[size-2*sizeof(int)]); int type = *((int*)&bytes[size-1*sizeof(int)]); // If the size is higher, it may be a wrong data format. UASSERT_MSG(height>=0 && height<10000 && width>=0 && width<10000, uFormat("size=%d, height=%d width=%d type=%d", size, height, width, type).c_str()); data = cv::Mat(height, width, type); uLongf totalUncompressed = uLongf(data.total())*uLongf(data.elemSize()); int errCode = uncompress( (Bytef*)data.data, &totalUncompressed, (const Bytef*)bytes, uLong(size)); if(errCode == Z_MEM_ERROR) { UERROR("Z_MEM_ERROR : Insufficient memory."); } else if(errCode == Z_BUF_ERROR) { UERROR("Z_BUF_ERROR : The buffer dest was not large enough to hold the uncompressed data."); } else if(errCode == Z_DATA_ERROR) { UERROR("Z_DATA_ERROR : The compressed data (referenced by source) was corrupted."); } } return data; }