New repo for repo.or.cz
[The-Artvertiser.git] / garfeild / lightcalib / ipltexture.cpp
blob3aeb6244feb7d766228d31678ca2a53a85db4488
1 /*
2 Copyright 2005, 2006 Computer Vision Lab,
3 Ecole Polytechnique Federale de Lausanne (EPFL), Switzerland.
4 Modified by Damian Stewart <damian@frey.co.nz> 2009-2010;
5 modifications Copyright 2009, 2010 Damian Stewart <damian@frey.co.nz>.
7 Distributed under the terms of the GNU General Public License v3.
9 This file is part of The Artvertiser.
11 The Artvertiser is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 The Artvertiser is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with The Artvertiser. If not, see <http://www.gnu.org/licenses/>.
26 #include <iostream>
27 #include "ipltexture.h"
29 IplTexture::~IplTexture()
31 if (downsampled) cvReleaseImage(&downsampled);
34 void IplTexture::genTexture()
36 #ifdef HAVE_GL
37 if (im==0) return;
39 if (!textureGenerated) {
40 glGenTextures(1,(GLuint*) &texture);
41 textureGenerated = true;
43 glBindTexture(GL_TEXTURE_2D, texture);
45 for (texWidth=1; texWidth < im->width; texWidth <<=1);
46 for (texHeight=1; texHeight < im->height; texHeight <<=1);
48 if (texWidth > 1024) texWidth = 1024;
49 if (texHeight > 1024) texHeight = 1024;
50 //std::cout << "IplTexture *: "<< this << ": generating a " << texWidth << "x" << texHeight << " texture for a "
51 // << im->width << "x" << im->height << " image.\n";
53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (smooth ? GL_LINEAR : GL_NEAREST));
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, (smooth ? GL_LINEAR : GL_NEAREST));
58 int sz = texWidth*texHeight*4;
59 char *buffer = (char *) malloc(sz);
60 memset(buffer, 0, sz);
62 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth,texHeight, 0, GL_RGBA,
63 GL_UNSIGNED_BYTE, buffer);
64 free(buffer);
65 #endif
68 void IplTexture::loadTexture()
70 #ifdef HAVE_GL
71 if (im==0) return;
72 if (!textureGenerated) genTexture();
74 IplImage *im = (downsampled ? downsampled : this->im);
76 if ((im->width > texWidth) || (im->height > texHeight)) {
77 if (downsampled) cvReleaseImage(&downsampled);
78 downsampled = cvCreateImage(cvSize(texWidth, texHeight), this->im->depth, this->im->nChannels);
79 im = downsampled;
82 glEnable(GL_TEXTURE_2D);
83 glBindTexture(GL_TEXTURE_2D, texture);
85 if (allowCache && !reload) return;
86 reload = false;
88 if (downsampled)
89 cvResize(this->im, downsampled, CV_INTER_AREA);
91 GLenum format;
92 GLenum type;
93 switch (im->depth) {
94 case IPL_DEPTH_8U: type = GL_UNSIGNED_BYTE; break;
95 case IPL_DEPTH_8S: type = GL_BYTE; break;
96 case IPL_DEPTH_16S: type = GL_SHORT; break;
97 case IPL_DEPTH_32F: type = GL_FLOAT; break;
98 default:
99 std::cerr << "IplTexture::loadTexture(): unsupported pixel type.\n";
100 return;
102 switch (im->nChannels) {
103 case 1: format = GL_LUMINANCE; break;
104 case 3: format = (im->channelSeq[0] == 'B') ? GL_BGR_EXT : GL_RGB; break;
105 case 4: format = GL_RGBA; break;
106 default:
107 std::cerr << "IplTexture::loadTexture(): unsupported number of channels.\n";
108 return;
111 // pixel storage mode
112 glPixelStorei(GL_UNPACK_ALIGNMENT, im->align);
114 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, im->width, im->height,
115 format, type, im->imageData);
117 uScale = (double(im->width)/double(this->im->width))/double(texWidth);
118 vScale = (double(im->height)/double(this->im->height))/double(texHeight);
119 vOrigin = 0;
121 if (im->origin) {
122 vScale = -vScale;
123 vOrigin = double(im->height)/double(texHeight);
125 #endif
128 void IplTexture::disableTexture()
130 #ifdef HAVE_GL
131 glDisable(GL_TEXTURE_2D);
132 #endif
135 void IplTexture::unref()
137 refcnt--;
138 if (refcnt <= 0)
139 delete this;
142 void IplTexture::setImage(IplImage *image)
144 im = image;
145 update();
148 void IplTexture::regen()
150 update();
151 textureGenerated = false;