Io.hh
Go to the documentation of this file.
1 
35 #ifndef IO_HH
36 #define IO_HH
37 
38 #include <string>
39 
40 namespace io
41 {
42 
43 bool savePpm(const std::string& fileName,
44  uint32_t width,
45  uint32_t height,
46  const void* dataP)
47 {
48  std::ofstream outputStream(fileName.c_str(), std::ios::out | std::ios::binary);
49 
50  if (false == outputStream.good()) {
51  std::cerr << "Failed to open \"" << fileName << "\"" << std::endl;
52  return false;
53  }
54 
55  const uint32_t imageSize = height * width * 3;
56 
57  outputStream << "P6\n"
58  << width << " " << height << "\n"
59  << 0xFF << "\n";
60 
61  outputStream.write(reinterpret_cast<const char*>(dataP), imageSize);
62 
63  outputStream.close();
64  return true;
65 }
66 
67 bool savePgm(const std::string& fileName,
68  uint32_t width,
69  uint32_t height,
70  uint32_t bitsPerPixel,
71  const std::string& comment,
72  const void *dataP)
73 {
74  std::ofstream outputStream(fileName.c_str(), std::ios::binary | std::ios::out);
75 
76  if (false == outputStream.good()) {
77  std::cerr << "Failed to open \"" << fileName << "\"" << std::endl;
78  return false;
79  }
80 
81  const uint32_t imageSize = height * width;
82 
83  switch(bitsPerPixel) {
84  case 8:
85  {
86 
87  outputStream << "P5\n"
88  << "#" << comment << "\n"
89  << width << " " << height << "\n"
90  << 0xFF << "\n";
91 
92  outputStream.write(reinterpret_cast<const char*>(dataP), imageSize);
93 
94  break;
95  }
96  case 16:
97  {
98  outputStream << "P5\n"
99  << "#" << comment << "\n"
100  << width << " " << height << "\n"
101  << 0xFFFF << "\n";
102 
103  const uint16_t *imageP = reinterpret_cast<const uint16_t*>(dataP);
104 
105  for (uint32_t i=0; i<imageSize; ++i) {
106  uint16_t o = htons(imageP[i]);
107  outputStream.write(reinterpret_cast<const char*>(&o), sizeof(uint16_t));
108  }
109 
110  break;
111  }
112  }
113 
114  outputStream.close();
115  return true;
116 }
117 
118 }
119 
120 #endif //IO_HH
121 
io::savePgm
bool savePgm(const std::string &fileName, uint32_t width, uint32_t height, uint32_t bitsPerPixel, const std::string &comment, const void *dataP)
Definition: Io.hh:67
io::savePpm
bool savePpm(const std::string &fileName, uint32_t width, uint32_t height, const void *dataP)
Definition: Io.hh:43
io
Definition: Io.hh:40


multisense_lib
Author(s):
autogenerated on Thu Apr 17 2025 02:49:09