Tucano  0.1
A library for rapid prototyping with modern OpenGL and GLSL
ppmIO.hpp
Go to the documentation of this file.
1 
23 #ifndef __PPMIMPORTER__
24 #define __PPMIMPORTER__
25 
26 #include <tucano/texture.hpp>
27 
28 namespace Tucano
29 {
30 
31 namespace ImageImporter
32 {
33 
34 static bool loadPPMImage (string filename, Tucano::Texture* tex) __attribute__ ((unused));
35 
46 static bool loadPPMImage (string filename, Tucano::Texture *tex)
47 {
48  ifstream in(filename.c_str(),ios::in);
49  if (!in)
50  {
51  std::cerr << "Cannot open " << filename.c_str() << std::endl;
52  }
53 
54  vector<float> data;
55  string header;
56  in >> header;
57  int w, h;
58  in >> w >> h;
59  float max_value;
60  in >> max_value;
61 
62  float value;
63  while (in >> value)
64  {
65  data.push_back(value/max_value);
66  }
67 
68  if(in.is_open())
69  {
70  in.close();
71  }
72 
73  // flip texture since it will be upside down (invertex y axis)
74  vector<float> flipped;
75  for (int j = h-1; j >= 0; j--)
76  {
77  for (int i = 0; i < w; i++)
78  {
79  flipped.push_back( data[(j*w + i)*3 + 0]);
80  flipped.push_back( data[(j*w + i)*3 + 1]);
81  flipped.push_back( data[(j*w + i)*3 + 2]);
82  }
83  }
84 
85  tex->create (GL_TEXTURE_2D, GL_RGBA32F, w, h, GL_RGB, GL_FLOAT, &flipped[0], 0);
86 
87  #ifdef TUCANODEBUG
88  Misc::errorCheckFunc(__FILE__, __LINE__);
89  #endif
90  return true;
91 }
92 
93 /*
94 static bool writePPMImage (string filename, Tucano::Framebuffer* fbo, int attach = 0)
95 {
96  // compute max value
97  float max_value = 0;
98  int pos;
99  for (int j = size[1]-1; j >= 0; --j)
100  {
101  for (int i = 0 ; i < size[0]; ++i)
102  {
103  pos = (i + size[0]*j)*4;
104  for (int k = 0; k < 3; ++k)
105  {
106  max_value = max(max_value, pixels[pos+k]);
107  }
108  }
109  out_stream << "\n";
110  }
111 
112  ofstream out_stream;
113  out_stream.open(filename.c_str());
114  out_stream << "P3\n";
115  out_stream << size[0] << " " << size[1] << "\n";
116  out_stream << "255\n";
117 
118  bool was_binded = is_binded;
119 
120  GLfloat * pixels = new GLfloat[(int)(size[0]*size[1]*4)];
121  bind();
122  glReadBuffer(GL_COLOR_ATTACHMENT0+attach);
123  glReadPixels(0, 0, size[0], size[1], GL_RGBA, GL_FLOAT, pixels);
124 
125  for (int j = size[1]-1; j >= 0; --j)
126  {
127  for (int i = 0 ; i < size[0]; ++i)
128  {
129  pos = (i + size[0]*j)*4;
130  out_stream << (int)(255*pixels[pos+0]) << " " << (int)(255*pixels[pos+1]) << " " << (int)(255*pixels[pos+2]) << " ";
131  }
132  out_stream << "\n";
133  }
134  out_stream.close();
135 
136  if (!was_binded)
137  {
138  unbindFBO();
139  }
140  delete [] pixels;
141 }
142 */
143 }
144 }
145 #endif
Definition: bufferobject.hpp:34
static bool loadPPMImage(string filename, Tucano::Texture *tex) __attribute__((unused))
Loads a texture from a PPM file. The texture receives data in the range [0,1] to create a FLOAT textu...
Definition: ppmIO.hpp:46
void errorCheckFunc(std::string file, int line, std::string message="")
GL error check method.
Definition: misc.hpp:53
An OpenGL texture. It can be a simple texture or an FBO texture.
Definition: texture.hpp:41
GLuint create(GLenum type, GLenum int_format, int w, int h, GLenum fmt, GLenum pix_type, const GLvoid *data=NULL, int dpt=256)
Creates a texture object and returns its handler.
Definition: texture.hpp:146