19 #include "visiontransfer/imageset.h"
24 #include <arpa/inet.h>
27 using namespace visiontransfer;
29 namespace visiontransfer {
32 : width(0), height(0), qMatrix(NULL), timeSec(0), timeMicrosec(0),
33 seqNum(0), minDisparity(0), maxDisparity(0), subpixelFactor(16),
34 referenceCounter(NULL), numberOfImages(2), indexLeftImage(0), indexRightImage(1), indexDisparityImage(-1),
35 indexColorImage(-1), exposureTime(0), lastSyncPulseSec(0), lastSyncPulseMicrosec(0) {
36 for (
int i=0; i<MAX_SUPPORTED_IMAGES; ++i) {
44 copyData(*
this, other,
true);
50 copyData(*
this, other,
true);
55 ImageSet::~ImageSet() {
59 void ImageSet::copyData(
ImageSet& dest,
const ImageSet& src,
bool countRef) {
60 dest.width = src.width;
61 dest.height = src.height;
63 dest.numberOfImages = src.numberOfImages;
64 for(
int i=0; i<MAX_SUPPORTED_IMAGES; i++) {
65 dest.rowStride[i] = src.rowStride[i];
66 dest.formats[i] = src.formats[i];
67 dest.data[i] = src.data[i];
70 dest.qMatrix = src.qMatrix;
71 dest.timeSec = src.timeSec;
72 dest.timeMicrosec = src.timeMicrosec;
73 dest.seqNum = src.seqNum;
74 dest.minDisparity = src.minDisparity;
75 dest.maxDisparity = src.maxDisparity;
76 dest.subpixelFactor = src.subpixelFactor;
77 dest.referenceCounter = src.referenceCounter;
78 dest.numberOfImages = src.numberOfImages;
79 dest.indexLeftImage = src.indexLeftImage;
80 dest.indexRightImage = src.indexRightImage;
81 dest.indexDisparityImage = src.indexDisparityImage;
82 dest.indexColorImage = src.indexColorImage;
83 dest.exposureTime = src.exposureTime;
84 dest.lastSyncPulseSec = src.lastSyncPulseSec;
85 dest.lastSyncPulseMicrosec = src.lastSyncPulseMicrosec;
87 if(dest.referenceCounter !=
nullptr && countRef) {
88 (*dest.referenceCounter)++;
92 void ImageSet::decrementReference() {
93 if(referenceCounter !=
nullptr && --(*referenceCounter) == 0) {
99 delete referenceCounter;
102 referenceCounter =
nullptr;
108 throw std::runtime_error(
"Illegal image number!");
111 std::fstream strm(fileName, std::ios::out | std::ios::binary);
114 int type, maxVal, bytesPerChannel, channels;
115 switch(formats[imageNumber]) {
135 throw std::runtime_error(
"Illegal pixel format!");
138 strm <<
"P" << type <<
" " << width <<
" " << height <<
" " << maxVal << std::endl;
141 for(
int y = 0; y < height; y++) {
142 for(
int x = 0; x < width*channels; x++) {
143 unsigned char* pixel = &data[imageNumber][y*rowStride[imageNumber] + x*bytesPerChannel];
144 if(bytesPerChannel == 2) {
146 unsigned short swapped = htons(*
reinterpret_cast<unsigned short*
>(pixel));
147 strm.write(
reinterpret_cast<char*
>(&swapped),
sizeof(swapped));
149 strm.write(
reinterpret_cast<char*
>(pixel), 1);
160 default:
throw std::runtime_error(
"Invalid image format!");
165 dest.decrementReference();
166 copyData(dest, *
this,
false);
168 dest.qMatrix =
new float[16];
169 memcpy(
const_cast<float*
>(dest.qMatrix), qMatrix,
sizeof(
float)*16);
174 dest.rowStride[i] = width*bytesPixel;
175 dest.data[i] =
new unsigned char[height*dest.rowStride[i]];
178 for(
int y = 0; y < height; y++) {
179 memcpy(&dest.data[i][y*dest.rowStride[i]], &data[i][y*rowStride[i]],
184 dest.referenceCounter =
new int;
185 (*dest.referenceCounter) = 1;
193 default:
throw std::runtime_error(
"Invalid image format!");
199 if (imageNumber ==
getIndexOf(ImageSet::ImageType::IMAGE_LEFT))
return ImageSet::ImageType::IMAGE_LEFT;
200 if (imageNumber ==
getIndexOf(ImageSet::ImageType::IMAGE_RIGHT))
return ImageSet::ImageType::IMAGE_RIGHT;
201 if (imageNumber ==
getIndexOf(ImageSet::ImageType::IMAGE_DISPARITY))
return ImageSet::ImageType::IMAGE_DISPARITY;
202 if (imageNumber ==
getIndexOf(ImageSet::ImageType::IMAGE_COLOR))
return ImageSet::ImageType::IMAGE_COLOR;
203 throw std::runtime_error(
"Invalid image number for getImageType!");
206 void ImageSet::setImageDisparityPair(
bool dispPair) {
207 if (
getNumberOfImages() != 2)
throw std::runtime_error(
"setImageDisparityPair is only supported for two-image sets");
210 indexRightImage = dispPair ? -1 : 1;
211 indexDisparityImage = dispPair ? 1 : -1;
218 idx = indexLeftImage;
222 idx = indexRightImage;
225 case IMAGE_DISPARITY: {
226 idx = indexDisparityImage;
230 idx = indexColorImage;
234 throw std::runtime_error(
"Invalid ImageType for query!");
236 if (throwIfNotFound && (idx==-1))
throw std::runtime_error(
"ImageSet does not contain the queried ImageType");
243 indexLeftImage = idx;
247 indexRightImage = idx;
250 case IMAGE_DISPARITY: {
251 indexDisparityImage = idx;
255 indexColorImage = idx;
259 throw std::runtime_error(
"Invalid ImageType for setIndexOf!");