PPMIO.cpp
Go to the documentation of this file.
1 
28 /*
29  * PPMIO.cpp
30  *
31  * Created on: 08.09.2011
32  * Author: Kim Rinnewitz ( krinnewitz@uos.de )
33  * Modified on: 11.12.2011
34  * Author: Thomas Wiemann ( twiemann@uos.de )
35  * Modified on: 15.02.2011
36  * Author: Denis Meyer ( denmeyer@uos.de )
37  */
38 
39 #include "lvr2/io/PPMIO.hpp"
40 
41 #include <iostream>
42 #include <fstream>
43 #include <string.h>
44 
45 using namespace std;
46 
47 namespace lvr2
48 {
49 
50 PPMIO::PPMIO()
51 {
52  m_pixels = 0;
53  m_width = 0;
54  m_height = 0;
55 }
56 
57 PPMIO::PPMIO( string filename ) : m_width(0), m_height(0), m_pixels(0)
58 {
59 
60  // Try to open file
61  ifstream in(filename.c_str());
62 
63  // Parse file
64  if(in.good())
65  {
66  // Line buffer
67  char buffer[1024];
68 
69  // Read file tag
70  readLine(in, buffer);
71 
72  // Check tag
73  string tag(buffer);
74  if(tag == "P3")
75  {
76  // Read width, height and color information
77  stringstream ss;
78  readLine(in, buffer);
79  ss << buffer << " ";
80  readLine(in, buffer);
81  ss << buffer << " ";
82 
83  // Read formatted data
84  ss >> m_width >> m_height;
85 
86  // Alloc data
87  m_pixels = new unsigned char[m_width * m_height * 3];
88 
89  // Read pixels
90  int p;
91  for(int i = 0; i < m_width * m_height * 3; i++)
92  {
93  in >> p;
94  m_pixels[i] = (unsigned char)p;
95  }
96  }
97  else
98  {
99  in.close();
100  in.open(filename.c_str(), ios::binary);
101  // readLine(in, buffer);
102  // char tmp[3];
103  // sscanf(buffer, "%s %d %d 255", tmp, &m_width, &m_height);
104 
105  string tag;
106  in >> tag;
107 
108 
109  if(tag == "P6") // TODO: hacked in for our output
110  {
111  int n_colors;
112  in >> m_width >> m_height >> n_colors;
113  in.getline(0,0);
114  m_pixels = new unsigned char[m_width * m_height * 3];
115  in.read((char *)m_pixels, m_width * m_height * 3);
116  }
117  else
118  {
119  cerr << "Unsupported tag, only P3 or P6 possible." << endl;
120  }
121  }
122  }
123  else
124  {
125  cout << "ReadPPM: Unable to open file " << filename << "." << endl;
126  }
127 }
128 
129 void PPMIO::write( string filename )
130 {
131  ofstream out(filename.c_str());
132 
133  if(out.good())
134  {
135  out<<"P6"<<" "<<m_width<<" "<<m_height<<" "<<"255"<<endl;
136  out.write((char*) m_pixels, m_width * m_height * 3);
137  }
138 
139  out.close();
140 
141 }
142 
143 void PPMIO::setDataArray( unsigned char* array, int width, int height )
144 {
145  m_pixels = array;
146  m_width = width;
147  m_height = height;
148 }
149 
150 void PPMIO::readLine( ifstream & in, char* buffer )
151 {
152  // Read lines until no comment line was found
153  do
154  {
155  in.getline(buffer, 256);
156  }
157  while(buffer[0] == '#' && in.good() );
158 }
159 
160 } // namespace lvr2
unsigned char * m_pixels
Definition: PPMIO.hpp:76
void readLine(ifstream &in, char *buffer)
Definition: PPMIO.cpp:150
void write(string filename)
Definition: PPMIO.cpp:129
void setDataArray(unsigned char *array, int width, int height)
Definition: PPMIO.cpp:143
int m_height
Definition: PPMIO.hpp:75
SharedPointer p
int m_width
Definition: PPMIO.hpp:74


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:08