19 #include "visiontransfer/imagepair.h" 24 #include <arpa/inet.h> 28 : width(0), height(0), qMatrix(NULL), timeSec(0), timeMicrosec(0),
29 seqNum(0), minDisparity(0), maxDisparity(0), disparityPair(false),
30 referenceCounter(NULL) {
40 copyData(*
this, other,
true);
46 copyData(*
this, other,
true);
51 ImagePair::~ImagePair() {
56 dest.width = src.width;
57 dest.height = src.height;
59 for(
int i=0; i<2; i++) {
60 dest.rowStride[i] = src.rowStride[i];
61 dest.formats[i] = src.formats[i];
62 dest.data[i] = src.data[i];
65 dest.qMatrix = src.qMatrix;
66 dest.timeSec = src.timeSec;
67 dest.timeMicrosec = src.timeMicrosec;
68 dest.seqNum = src.seqNum;
69 dest.minDisparity = src.minDisparity;
70 dest.maxDisparity = src.maxDisparity;
71 dest.disparityPair = src.disparityPair;
72 dest.referenceCounter = src.referenceCounter;
74 if(dest.referenceCounter !=
nullptr && countRef) {
75 (*dest.referenceCounter)++;
79 void ImagePair::decrementReference() {
80 if(referenceCounter !=
nullptr && --(*referenceCounter) == 0) {
84 delete referenceCounter;
89 referenceCounter =
nullptr;
94 if(imageNumber < 0 || imageNumber >1) {
95 throw std::runtime_error(
"Illegal image number!");
98 std::fstream strm(fileName, std::ios::out | std::ios::binary);
101 int type, maxVal, bytesPerChannel, channels;
102 switch(formats[imageNumber]) {
122 throw std::runtime_error(
"Illegal pixel format!");
125 strm <<
"P" << type <<
" " << width <<
" " << height <<
" " << maxVal << std::endl;
128 for(
int y = 0; y < height; y++) {
129 for(
int x = 0; x < width*channels; x++) {
130 unsigned char* pixel = &data[imageNumber][y*rowStride[imageNumber] + x*bytesPerChannel];
131 if(bytesPerChannel == 2) {
133 unsigned short swapped = htons(*reinterpret_cast<unsigned short*>(pixel));
134 strm.write(reinterpret_cast<char*>(&swapped),
sizeof(swapped));
136 strm.write(reinterpret_cast<char*>(pixel), 1);
143 dest.decrementReference();
144 copyData(dest, *
this,
false);
146 dest.qMatrix =
new float[16];
147 memcpy(const_cast<float*>(dest.qMatrix), qMatrix,
sizeof(
float)*16);
149 for(
int i=0; i<2; i++) {
152 dest.rowStride[i] = width*bytesPixel;
153 dest.data[i] =
new unsigned char[height*dest.rowStride[i]];
156 for(
int y = 0; y < height; y++) {
157 memcpy(&dest.data[i][y*dest.rowStride[i]], &data[i][y*rowStride[i]],
162 dest.referenceCounter =
new int;
163 (*dest.referenceCounter) = 1;
171 default:
throw std::runtime_error(
"Invalid image format!");
void copyTo(ImagePair &dest)
Makes a deep copy of this image pair.
int getBytesPerPixel(int imageNumber) const
Returns the number of bytes that are required to store one image pixel.
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.
A set of two images, which are usually the left camera image and the disparity map.
ImageFormat
Image formats that can be transferred.