19 #include "visiontransfer/imageset.h" 24 #include <arpa/inet.h> 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 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<src.numberOfImages; 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.exposureTime = src.exposureTime;
83 dest.lastSyncPulseSec = src.lastSyncPulseSec;
84 dest.lastSyncPulseMicrosec = src.lastSyncPulseMicrosec;
86 if(dest.referenceCounter !=
nullptr && countRef) {
87 (*dest.referenceCounter)++;
91 void ImageSet::decrementReference() {
92 if(referenceCounter !=
nullptr && --(*referenceCounter) == 0) {
98 delete referenceCounter;
101 referenceCounter =
nullptr;
107 throw std::runtime_error(
"Illegal image number!");
110 std::fstream strm(fileName, std::ios::out | std::ios::binary);
113 int type, maxVal, bytesPerChannel, channels;
114 switch(formats[imageNumber]) {
134 throw std::runtime_error(
"Illegal pixel format!");
137 strm <<
"P" << type <<
" " << width <<
" " << height <<
" " << maxVal << std::endl;
140 for(
int y = 0; y < height; y++) {
141 for(
int x = 0; x < width*channels; x++) {
142 unsigned char* pixel = &data[imageNumber][y*rowStride[imageNumber] + x*bytesPerChannel];
143 if(bytesPerChannel == 2) {
145 unsigned short swapped = htons(*reinterpret_cast<unsigned short*>(pixel));
146 strm.write(reinterpret_cast<char*>(&swapped),
sizeof(swapped));
148 strm.write(reinterpret_cast<char*>(pixel), 1);
159 default:
throw std::runtime_error(
"Invalid image format!");
164 dest.decrementReference();
165 copyData(dest, *
this,
false);
167 dest.qMatrix =
new float[16];
168 memcpy(const_cast<float*>(dest.qMatrix), qMatrix,
sizeof(
float)*16);
173 dest.rowStride[i] = width*bytesPixel;
174 dest.data[i] =
new unsigned char[height*dest.rowStride[i]];
177 for(
int y = 0; y < height; y++) {
178 memcpy(&dest.data[i][y*dest.rowStride[i]], &data[i][y*rowStride[i]],
183 dest.referenceCounter =
new int;
184 (*dest.referenceCounter) = 1;
192 default:
throw std::runtime_error(
"Invalid image format!");
198 if (imageNumber ==
getIndexOf(ImageSet::ImageType::IMAGE_LEFT))
return ImageSet::ImageType::IMAGE_LEFT;
199 if (imageNumber ==
getIndexOf(ImageSet::ImageType::IMAGE_RIGHT))
return ImageSet::ImageType::IMAGE_RIGHT;
200 if (imageNumber ==
getIndexOf(ImageSet::ImageType::IMAGE_DISPARITY))
return ImageSet::ImageType::IMAGE_DISPARITY;
201 throw std::runtime_error(
"Invalid image number for getImageType!");
204 void ImageSet::setImageDisparityPair(
bool dispPair) {
205 if (
getNumberOfImages() != 2)
throw std::runtime_error(
"setImageDisparityPair is only supported for two-image sets");
208 indexRightImage = dispPair ? -1 : 1;
209 indexDisparityImage = dispPair ? 1 : -1;
216 idx = indexLeftImage;
220 idx = indexRightImage;
223 case IMAGE_DISPARITY: {
224 idx = indexDisparityImage;
228 throw std::runtime_error(
"Invalid ImageType for query!");
230 if (throwIfNotFound && (idx==-1))
throw std::runtime_error(
"ImageSet does not contain the queried ImageType");
237 indexLeftImage = idx;
241 indexRightImage = idx;
244 case IMAGE_DISPARITY: {
245 indexDisparityImage = idx;
249 std::cout <<
"what=" << what << std::endl;
250 throw std::runtime_error(
"Invalid ImageType for setIndexOf!");
int getBitsPerPixel(int imageNumber) const
Returns the number of bits that are required to store one image pixel.
void writePgmFile(int imageNumber, const char *fileName) const
Writes one image of the set to a PGM or PPM file.
int getIndexOf(ImageType what, bool throwIfNotFound=false) const
Returns the index of a specific image type.
int getNumberOfImages() const
Returns the number of images in this set.
int getBytesPerPixel(int imageNumber) const
Returns the number of bytes that are required to store one image pixel.
ImageSet()
Default constructor creating an image set with no pixel data.
void copyTo(ImageSet &dest)
Makes a deep copy of this image set.
ImageType
Supported image types.
ImageType getImageType(int imageNumber) const
Returns the ImageType of the specified channel.
ImageFormat
Image formats that can be transferred.
A set of one to three images, but usually two (the left camera image and the disparity map)...
void setIndexOf(ImageType what, int idx)
Assign an image index to a specified ImageType, -1 to disable.