bazar  1.3.1
ipltexture.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include "ipltexture.h"
3 
5 {
6  if (downsampled) cvReleaseImage(&downsampled);
7 }
8 
10 {
11 #ifdef HAVE_GL
12  if (im==0) return;
13 
14  if (!textureGenerated) {
15  glGenTextures(1,(GLuint*) &texture);
16  textureGenerated = true;
17  }
18  glBindTexture(GL_TEXTURE_2D, texture);
19 
20  for (texWidth=1; texWidth < im->width; texWidth <<=1);
21  for (texHeight=1; texHeight < im->height; texHeight <<=1);
22 
23  if (texWidth > 1024) texWidth = 1024;
24  if (texHeight > 1024) texHeight = 1024;
25  //std::cout << "IplTexture *: "<< this << ": generating a " << texWidth << "x" << texHeight << " texture for a "
26  // << im->width << "x" << im->height << " image.\n";
27 
28  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
29  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
30  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (smooth ? GL_LINEAR : GL_NEAREST));
31  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (smooth ? GL_LINEAR : GL_NEAREST));
32 
33  int sz = texWidth*texHeight*4;
34  char *buffer = (char *) malloc(sz);
35  memset(buffer, 0, sz);
36 
37  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth,texHeight, 0, GL_RGBA,
38  GL_UNSIGNED_BYTE, buffer);
39  free(buffer);
40 #endif
41 }
42 
44 {
45 #ifdef HAVE_GL
46  if (im==0) return;
47  if (!textureGenerated) genTexture();
48 
49  IplImage *im = (downsampled ? downsampled : this->im);
50 
51  if ((im->width > texWidth) || (im->height > texHeight)) {
52  if (downsampled) cvReleaseImage(&downsampled);
53  downsampled = cvCreateImage(cvSize(texWidth, texHeight), this->im->depth, this->im->nChannels);
54  im = downsampled;
55  }
56 
57  glEnable(GL_TEXTURE_2D);
58  glBindTexture(GL_TEXTURE_2D, texture);
59 
60  if (allowCache && !reload) return;
61  reload = false;
62 
63  if (downsampled)
64  cvResize(this->im, downsampled, CV_INTER_AREA);
65 
66  GLenum format;
67  GLenum type;
68  switch (im->depth) {
69  case IPL_DEPTH_8U: type = GL_UNSIGNED_BYTE; break;
70  case IPL_DEPTH_8S: type = GL_BYTE; break;
71  case IPL_DEPTH_16S: type = GL_SHORT; break;
72  case IPL_DEPTH_32F: type = GL_FLOAT; break;
73  default:
74  std::cerr << "IplTexture::loadTexture(): unsupported pixel type.\n";
75  return;
76  }
77  switch (im->nChannels) {
78  case 1: format = GL_LUMINANCE; break;
79  case 3: format = (im->channelSeq[0] == 'B') ? GL_BGR_EXT : GL_RGB; break;
80  case 4: format = GL_RGBA; break;
81  default:
82  std::cerr << "IplTexture::loadTexture(): unsupported number of channels.\n";
83  return;
84  }
85 
86  // pixel storage mode
87  glPixelStorei(GL_UNPACK_ALIGNMENT, im->align);
88 
89  glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, im->width, im->height,
90  format, type, im->imageData);
91 
92  uScale = (double(im->width)/double(this->im->width))/double(texWidth);
93  vScale = (double(im->height)/double(this->im->height))/double(texHeight);
94  vOrigin = 0;
95 
96  if (im->origin) {
97  vScale = -vScale;
98  vOrigin = double(im->height)/double(texHeight);
99  }
100 #endif
101 }
102 
104 {
105 #ifdef HAVE_GL
106  glDisable(GL_TEXTURE_2D);
107 #endif
108 }
109 
111 {
112  refcnt--;
113  if (refcnt <= 0)
114  delete this;
115 }
116 
117 void IplTexture::setImage(IplImage *image)
118 {
119  im = image;
120  update();
121 }
122 
124 {
125  update();
126  textureGenerated = false;
127 }
128