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/>.
27 #include "ipltexture.h"
29 IplTexture::~IplTexture()
31 if (downsampled
) cvReleaseImage(&downsampled
);
34 void IplTexture::genTexture()
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
);
68 void IplTexture::loadTexture()
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
);
82 glEnable(GL_TEXTURE_2D
);
83 glBindTexture(GL_TEXTURE_2D
, texture
);
85 if (allowCache
&& !reload
) return;
89 cvResize(this->im
, downsampled
, CV_INTER_AREA
);
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;
99 std::cerr
<< "IplTexture::loadTexture(): unsupported pixel type.\n";
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;
107 std::cerr
<< "IplTexture::loadTexture(): unsupported number of channels.\n";
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
);
123 vOrigin
= double(im
->height
)/double(texHeight
);
128 void IplTexture::disableTexture()
131 glDisable(GL_TEXTURE_2D
);
135 void IplTexture::unref()
142 void IplTexture::setImage(IplImage
*image
)
148 void IplTexture::regen()
151 textureGenerated
= false;