31 namespace ImageImporter
50 ifstream in(filename.c_str(), std::ios::in | std::ios::binary);
53 cerr <<
"Cannot open " << filename.c_str() << endl; exit(1);
58 bool magic_number =
false;
59 int w, h, d, max_value;;
62 while (header.compare(
"ENDHDR") != 0)
64 if (header.compare(
"WIDTH") == 0)
66 else if (header.compare(
"HEIGHT") == 0)
68 else if (header.compare(
"DEPTH") == 0)
70 else if (header.compare(
"MAXVAL") == 0)
72 else if (header.compare(
"TUPLTYPE") == 0)
74 else if (header.compare(
"P7") == 0)
82 std::cerr <<
"Error opening : " << filename.c_str() <<
" : Invalid Magic Number, should be P7" << std::endl;
85 if (w < 1 || h < 1 || d < 1)
87 std::cerr <<
"Error opening : " << filename.c_str() <<
"Invalid dimension, WIDTH HEIGHT and DEPTH should be at least 1" << std::endl;
93 unsigned char byte_value = 0;
94 unsigned int value = 0;
95 while (in.read(reinterpret_cast< char* >(&byte_value),
sizeof(byte_value)) )
98 data.push_back(value/(
float)max_value);
101 if (data.size() != (
unsigned int)(w*h*d))
103 std::cerr <<
"possible error reading PAM file:\n read " << data.size()*
sizeof(byte_value) <<
" bytes, should have read " << w*h*d*
sizeof(byte_value) << std::endl;
112 bool convertGAToRGBA = !tuple.compare(
"GRAYSCALE_ALPHA");
115 vector<float> flipped;
116 for (
int j = h-1; j >= 0; j--)
118 for (
int i = 0; i < w; i++)
120 for (
int k = 0; k < d; ++k)
122 flipped.push_back( data[(j*w + i)*d + k]);
124 if (convertGAToRGBA && k == 0)
126 flipped.push_back( data[(j*w + i)*d + k]);
127 flipped.push_back( data[(j*w + i)*d + k]);
134 tex->create (GL_TEXTURE_2D, GL_RGBA32F, w, h, GL_RGB, GL_FLOAT, &flipped[0], 0);
135 else if (d == 4 || convertGAToRGBA)
136 tex->create (GL_TEXTURE_2D, GL_RGBA32F, w, h, GL_RGBA, GL_FLOAT, &flipped[0], 0);
162 out_stream.open(filename.c_str(), ios::out);
163 out_stream <<
"P7\n";
164 out_stream <<
"WIDTH " << size[0] <<
"\n " <<
"HEIGHT " << size[1] <<
"\n";
165 out_stream <<
"DEPTH 4\n";
166 out_stream <<
"MAXVAL " << max_value <<
"\n";
167 out_stream <<
"TUPLTYPE RGB_ALPHA\n";
168 out_stream <<
"ENDHDR\n";
170 GLfloat * pixels =
new GLfloat[(int)(size[0]*size[1]*4)];
172 glReadBuffer(GL_COLOR_ATTACHMENT0+attach);
173 glReadPixels(0, 0, size[0], size[1], GL_RGBA, GL_FLOAT, pixels);
176 out_stream.open(filename.c_str(), ios::out | ios::app | ios::binary);
179 for (
int j = size[1]-1; j >= 0; --j)
181 for (
int i = 0 ; i < size[0]; ++i)
183 pos = (i + size[0]*j)*4;
184 for (
int k = 0; k < 4; ++k)
186 value = (
unsigned int) (max_value*pixels[pos+k]);
187 out_stream.write(reinterpret_cast< char* >(&value),
sizeof(value)) ;
Eigen::Vector2i getDimensions(void)
Returns the dimensions of the FBO.
Definition: framebuffer.hpp:875
Definition: bufferobject.hpp:34
void errorCheckFunc(std::string file, int line, std::string message="")
GL error check method.
Definition: misc.hpp:53
virtual void bind(void)
Binds framebuffer object.
Definition: framebuffer.hpp:245
void unbind(void)
Unbinds fbo and all texture units in use.
Definition: framebuffer.hpp:408
A wrapper class for creating and using FBOs.
Definition: framebuffer.hpp:44
static bool loadPAMImage(string filename, Tucano::Texture *tex) __attribute__((unused))
Definition: pamIO.hpp:47
static bool writePAMImage(string filename, Tucano::Framebuffer *fbo, int attach=0) __attribute__((unused))
Saves a framebuffer attachment to an image file Note that since the input is a float image...
Definition: pamIO.hpp:157
An OpenGL texture. It can be a simple texture or an FBO texture.
Definition: texture.hpp:41