libvisiontransfer  4.1.5
imagepair.cpp
1 /*******************************************************************************
2  * Copyright (c) 2017 Nerian Vision Technologies
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include <iostream>
16 #include <fstream>
17 #include <stdexcept>
18 #include "visiontransfer/imagepair.h"
19 
20 #ifdef _WIN32
21 #include <winsock2.h>
22 #else
23 #include <arpa/inet.h>
24 #endif
25 
26 void ImagePair::writePgmFile(int imageNumber, const char* fileName) {
27  if(imageNumber < 0 || imageNumber >1) {
28  throw std::runtime_error("Illegal image number!");
29  }
30 
31  std::fstream strm(fileName, std::ios::out);
32 
33  // Write PGM header
34  int maxVal, bytesPixel;
35  if(formats[imageNumber] == FORMAT_8_BIT) {
36  maxVal = 255;
37  bytesPixel = 1;
38  } else { // 12 bit
39  maxVal = 4095;
40  bytesPixel = 2;
41  }
42 
43  strm << "P5 " << width << " " << height << " " << maxVal << std::endl;
44 
45  // Write image data
46  for(int y = 0; y < height; y++) {
47  for(int x = 0; x < width; x++) {
48  unsigned char* pixel = &data[imageNumber][y*rowStride[imageNumber] + x*bytesPixel];
49  if(bytesPixel == 2) {
50  // Swap endianess
51  *reinterpret_cast<unsigned short*>(pixel)
52  = htons(*reinterpret_cast<unsigned short*>(pixel));
53  }
54  strm.write(reinterpret_cast<char*>(pixel), bytesPixel);
55  }
56  }
57 }
void writePgmFile(int imageNumber, const char *fileName)
Writes one image of the pair to a PGM file.
Definition: imagepair.cpp:26
8-bit greyscale format
Definition: imagepair.h:37
Nerian Vision Technologies