19 #include "visiontransfer/imagepair.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), disparityPair(false),
34 referenceCounter(NULL) {
44 copyData(*
this, other,
true);
50 copyData(*
this, other,
true);
55 ImagePair::~ImagePair() {
60 dest.width = src.width;
61 dest.height = src.height;
63 for(
int i=0; i<2; i++) {
64 dest.rowStride[i] = src.rowStride[i];
65 dest.formats[i] = src.formats[i];
66 dest.data[i] = src.data[i];
69 dest.qMatrix = src.qMatrix;
70 dest.timeSec = src.timeSec;
71 dest.timeMicrosec = src.timeMicrosec;
72 dest.seqNum = src.seqNum;
73 dest.minDisparity = src.minDisparity;
74 dest.maxDisparity = src.maxDisparity;
75 dest.subpixelFactor = src.subpixelFactor;
76 dest.disparityPair = src.disparityPair;
77 dest.referenceCounter = src.referenceCounter;
79 if(dest.referenceCounter !=
nullptr && countRef) {
80 (*dest.referenceCounter)++;
84 void ImagePair::decrementReference() {
85 if(referenceCounter !=
nullptr && --(*referenceCounter) == 0) {
89 delete referenceCounter;
94 referenceCounter =
nullptr;
99 if(imageNumber < 0 || imageNumber >1) {
100 throw std::runtime_error(
"Illegal image number!");
103 std::fstream strm(fileName, std::ios::out | std::ios::binary);
106 int type, maxVal, bytesPerChannel, channels;
107 switch(formats[imageNumber]) {
127 throw std::runtime_error(
"Illegal pixel format!");
130 strm <<
"P" << type <<
" " << width <<
" " << height <<
" " << maxVal << std::endl;
133 for(
int y = 0; y < height; y++) {
134 for(
int x = 0; x < width*channels; x++) {
135 unsigned char* pixel = &data[imageNumber][y*rowStride[imageNumber] + x*bytesPerChannel];
136 if(bytesPerChannel == 2) {
138 unsigned short swapped = htons(*reinterpret_cast<unsigned short*>(pixel));
139 strm.write(reinterpret_cast<char*>(&swapped),
sizeof(swapped));
141 strm.write(reinterpret_cast<char*>(pixel), 1);
148 dest.decrementReference();
149 copyData(dest, *
this,
false);
151 dest.qMatrix =
new float[16];
152 memcpy(const_cast<float*>(dest.qMatrix), qMatrix,
sizeof(
float)*16);
154 for(
int i=0; i<2; i++) {
157 dest.rowStride[i] = width*bytesPixel;
158 dest.data[i] =
new unsigned char[height*dest.rowStride[i]];
161 for(
int y = 0; y < height; y++) {
162 memcpy(&dest.data[i][y*dest.rowStride[i]], &data[i][y*rowStride[i]],
167 dest.referenceCounter =
new int;
168 (*dest.referenceCounter) = 1;
176 default:
throw std::runtime_error(
"Invalid image format!");
void copyTo(ImagePair &dest)
Makes a deep copy of this image pair.
ImagePair()
Default constructor creating an image pair with no pixel data.
void writePgmFile(int imageNumber, const char *fileName) const
Writes one image of the pair to a PGM or PPM file.
ImageFormat
Image formats that can be transferred.
A set of two images, which are usually the left camera image and the disparity map.
int getBytesPerPixel(int imageNumber) const
Returns the number of bytes that are required to store one image pixel.