Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
glfTexture2D.cpp
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2008, 2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include "glfTexture2D.hh"
26 #include "glfException.hh"
27 #include <Base/Image/ImageIO.hh>
28 #include "glfFormatDB.hh"
29 
30 #include <Gui/biasgl.h>
31 using namespace BIAS;
32 
33 // border is currently not used.
34 const int BORDER_WIDTH = 0;
35 
37  glfTexture(GL_TEXTURE_2D)
38 {
39  //in the dll case several glew instances might exist.
40  //some of them aren't initialized and will deliver false
41  //information about the gpu capabilities.
42  //until we've found a fix we must initialize glew for every dll
43 }
44 
46 glfTexture2D::operator=(const glfTexture2D& b)
47 {
48  return (*this);
49 }
50 
52  glfTexture(GL_TEXTURE_2D)
53 {
54 }
55 
56 void
57 glfTexture2D::Set(GLenum minFilter, GLenum magFilter, GLenum wrapS,
58  GLenum wrapT, GLint textureNr)
59 {
60  SetMinFilter(minFilter);
61  SetMagFilter(magFilter);
62  SetWrapS(wrapS);
63  SetWrapT(wrapT);
64  SetTextureNr(textureNr);
65 }
66 
67 void
68 glfTexture2D::UploadImage(const BIAS::ImageBase& image, GLenum internalFormat,
69  int mipmap)
70 {
71  if (enlargeToPot_)
72  {
73  int potWidth = NextPowerOfTwo(image.GetWidth());
74  int potHeight = NextPowerOfTwo(image.GetHeight());
75  if ((int) image.GetWidth() != potWidth || (int) image.GetHeight()
76  != potHeight)
77  {
78  ImageBase paddedImage;
79  image.Pad(paddedImage, potWidth, potHeight);
80  UploadImage(paddedImage, internalFormat, mipmap);
81  return;
82  }
83  }
84 
85  // choose internal format if none is given
86  if (internalFormat == 0)
87  {
88  internalFormat = glfFormatDB::ProposeInternalFormat(image.GetStorageType(),
89  image.GetColorModel());
90  }
91 
92  // get pixel format and pixel type of given image
93  GLenum format = glfFormatDB::GetFormatForColorModel(image.GetColorModel());
94  GLenum pixelType = glfFormatDB::GetPixelTypeForStorageType(image.GetStorageType());
95 
96  // upload data.
97  Bind();
98  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
99  glTexImage2D(target_, mipmap, internalFormat, image.GetWidth(),
100  image.GetHeight(), BORDER_WIDTH, format, pixelType, image.GetImageData());
101 
102  GLF_THROW_ON_OPENGL_ERROR;
103 }
104 
105 void
106 glfTexture2D::UploadImageFromFile(const std::string& fileName,
107  GLenum internalFormat, int mipmap)
108 {
109  ImageBase image;
110  if (ImageIO::Load(fileName, image) != 0)
111  {
112  GLF_THROW_EXCEPTION("Could not load image: " << fileName);
113  }
114  UploadImage(image, internalFormat, mipmap);
115 
116  GLF_THROW_ON_OPENGL_ERROR;
117 }
118 
119 void
120 glfTexture2D::Allocate(int width, int height, GLenum internalFormat, int mipmap)
121 {
122  if (enlargeToPot_)
123  {
124  width = NextPowerOfTwo(width);
125  height = NextPowerOfTwo(height);
126  }
127 
128  GLenum format;
129  int numChannels;
130  glfFormatDB::GetFormatForInternalFormat(internalFormat, format, numChannels);
131 
132  Bind();
133 
134  GLenum type = GL_UNSIGNED_BYTE;
135  if (internalFormat == GL_DEPTH24_STENCIL8_EXT)
136  type = GL_UNSIGNED_INT_24_8_EXT;
137 
138  glTexImage2D(target_, mipmap, internalFormat, width, height, BORDER_WIDTH,
139  format, type, NULL);
140 
141  GLF_THROW_ON_OPENGL_ERROR;
142 }
143 
144 void
145 glfTexture2D::Allocate(int width, int height,
146  ImageBase::EStorageType storageType, ImageBase::EColorModel colorModel,
147  int mipmap)
148 {
149  GLenum internalFormat = glfFormatDB::ProposeInternalFormat(storageType, colorModel);
150  Allocate(width, height, internalFormat, mipmap);
151 }
152 
153 void
155 {
156  Bind();
157 
158  // get internal format of texture.
159  GLenum internalFormat;
160  glGetTexLevelParameteriv(target_, mipmap, GL_TEXTURE_INTERNAL_FORMAT,
161  (GLint*) &internalFormat);
162 
163  // get size
164  // BIASERR("copying texture");
165  int width = GetWidth(mipmap);
166  int height = GetHeight(mipmap);
167 
168  // get format and number of channels
169  GLenum format;
170  int numChannels;
171  glfFormatDB::GetFormatForInternalFormat(internalFormat, format, numChannels);
172 
173  // init image with size of texture and appropriate number of channels
174  ImageBase::EStorageType storageType = image.GetStorageType();
175  if (long(image.GetWidth()) != width || long(image.GetHeight()) != height ||
176  long(image.GetChannelCount()) != numChannels)
177  {
178  image.Init(width, height, numChannels, storageType);
179  }
180 
181  // get OpenGL pixel type
183 
184  glPixelStorei(GL_PACK_ALIGNMENT, 1);
185  // copy texture to image
186  glGetTexImage(target_, mipmap, format, type, image.GetImageData());
187 
188  GLF_THROW_ON_OPENGL_ERROR;
189 }
190 
191 void
193  int mipmap)
194 {
195  Bind();
196 
197  // get size
198  int width = GetWidth(mipmap);
199  int height = GetHeight(mipmap);
200 
201  // get number of channels
202  int numChannels = glfFormatDB::ChannelsFromGLFormat(format);
203 
204  // init image with size of texture and appropriate number of channels
205  ImageBase::EStorageType storageType = image.GetStorageType();
206  if (!image.IsEmpty())
207  {
208  image.Release(); // ImageBase doesn't like re-Init ...
209  }
210  image.Init(width, height, numChannels, storageType);
211 
212  // get OpenGL pixel type
214 
215  glPixelStorei(GL_PACK_ALIGNMENT, 1);
216  // copy texture to image
217  glGetTexImage(target_, mipmap, format, type, image.GetImageData());
218 
219  GLF_THROW_ON_OPENGL_ERROR;
220 }
221 
222 int
223 glfTexture2D::GetWidth(int mipmap) const
224 {
225  GLint width;
226  Bind();
227  glGetTexLevelParameteriv(target_, mipmap, GL_TEXTURE_WIDTH, &width);
228  GLF_THROW_ON_OPENGL_ERROR;
229  return width;
230 }
231 
232 int
233 glfTexture2D::GetHeight(int mipmap) const
234 {
235  GLint height;
236  Bind();
237  glGetTexLevelParameteriv(target_, mipmap, GL_TEXTURE_HEIGHT, &height);
238  GLF_THROW_ON_OPENGL_ERROR;
239  return height;
240 }
241 
242 void
243 glfTexture2D::DumpUC_RGBA(const std::string& fileName)
244 {
246  CopyToImage(dump, 0);
247  //ImageIO::Save(fileName, dump);
248  ImageIO::Save(fileName, dump);
249 }
250 
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
void DumpUC_RGBA(const std::string &fileName)
Uses CopyToImage to dump a Unsigned Char image comprising RGBA channels to file.
void Allocate(int width, int height, GLenum internalFormat, int mipmap=0)
Creates a texture with undefined content.
static int NextPowerOfTwo(int i)
Returns the smallest power of two that is greater than i.
Definition: glfTexture.cpp:269
int GetWidth(int mipmap=0) const
Returns the width of a mipmap of the texture.
A 2D texture.
Definition: glfTexture2D.hh:40
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
Definition: ImageBase.hh:245
void Bind() const
Binds the texture.
Definition: glfTexture.cpp:242
static int ChannelsFromGLFormat(GLenum textureFormat)
Returns the number of channels required to store an image of specified texture format, e.g.
void SetWrapS(GLenum wrapS)
Sets the wrapping mode for the 1st texture coordinate.
Definition: glfTexture.cpp:105
static void GetFormatForInternalFormat(GLenum internalFormat, GLenum &format, int &numChannels)
Computes a pixel format that can be used with glGetTexImage and glReadPixels for an internal format o...
Definition: glfFormatDB.cpp:32
void UploadImage(const BIAS::ImageBase &image, GLenum internalFormat=0, int mipmap=0)
Uploads a BIAS::Image to a mipmap of the texture.
void CopyToImage(ImageBase &image, int mipmap=0)
Copies the pixels of a mipmap of the texture to a BIAS::ImageBase.
unsigned int GetWidth() const
Definition: ImageBase.hh:312
void SetMinFilter(GLenum minFilter)
Sets the minifying function.
Definition: glfTexture.cpp:89
const void * GetImageData() const
Definition: ImageBase.hh:280
void UploadImageFromFile(const std::string &fileName, GLenum internalFormat=0, int mipmap=0)
Uploads an image loaded from a file to a mipmap of the texture.
int Pad(BIAS::ImageBase &dest, const unsigned int &newwidth, const unsigned int &newheight, const int &padVal=0) const
Definition: ImageBase.cpp:1207
int GetHeight(int mipmap=0) const
Returns the height of a mipmap of the texture.
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
Definition: ImageBase.hh:382
void SetWrapT(GLenum wrapT)
Sets the wrapping mode for the 2nd texture coordinate.
Definition: glfTexture.cpp:113
unsigned int GetHeight() const
Definition: ImageBase.hh:319
static int Save(const std::string &filename, const ImageBase &img, const enum TFileFormat FileFormat=FF_auto, const bool sync=BIAS_DEFAULT_SYNC, const int c_jpeg_quality=BIAS_DEFAULT_IMAGE_QUALITY, const bool forceNewID=BIAS_DEFAULT_FORCENEWID, const bool &writeMetaData=true)
Export image as file using extrnal libs.
Definition: ImageIO.cpp:725
static GLenum ProposeInternalFormat(ImageBase::EStorageType storageType, ImageBase::EColorModel colorModel)
Returns an OpenGL internal pixel format for a BIAS storage type and color model.
void Release(const bool reset_storage_type=false)
Free the allocated data structures Hands off: Do !!NOT!! change the default of reset_storage_type: Im...
Definition: ImageBase.cpp:350
enum EColorModel GetColorModel() const
Definition: ImageBase.hh:407
static GLenum GetFormatForColorModel(ImageBase::EColorModel colorModel)
Returns the OpenGL pixel format (GL_RGB etc.) for a BIAS color model.
void Init(unsigned int width, unsigned int height, unsigned int nChannels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
Initialize image size and channels.
Definition: ImageBase.cpp:229
static GLenum GetPixelTypeForStorageType(ImageBase::EStorageType storageType)
Returns the OpenGL pixel type (GL_UNSIGNED_BYTE etc.) for a BIAS storage type.
static int Load(const std::string &FileName, ImageBase &img)
first tries a call to Read MIP image and if that fails, tries to Import Image with all other availabl...
Definition: ImageIO.cpp:141
Base class for textures.
Definition: glfTexture.hh:42
void Set(GLenum minFilter=GL_NEAREST, GLenum magFilter=GL_NEAREST, GLenum wrapS=GL_CLAMP, GLenum wrapT=GL_CLAMP, GLint textureNr=GL_TEXTURE0)
Convenience wrapper.
enum EStorageType GetStorageType() const
Definition: ImageBase.hh:414
void SetTextureNr(GLint textureNr)
Sets the OpenGL texture number (GL_TEXTURE0 - GL_TEXTURE[MaxTextureUnit-1]).
Definition: glfTexture.cpp:207
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
void CopyChannelsToImage(ImageBase &image, GLenum format, int mipmap=0)
Copies the pixels of a mipmap of the texture to a BIAS::ImageBase and interprets the texture using th...
void SetMagFilter(GLenum magFilter)
Sets the magnification function.
Definition: glfTexture.cpp:97