textures.h 8.87 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                  *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23 24 25 26 27
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef CGOGN_TEXTURES_H_
#define CGOGN_TEXTURES_H_

28
#include "Utils/gl_def.h"
Sylvain Thery's avatar
Sylvain Thery committed
29 30
#include "Geometry/vector_gen.h"
#include <string>
Sylvain Thery's avatar
Sylvain Thery committed
31
#include <QImage>
Sylvain Thery's avatar
Sylvain Thery committed
32

Sylvain Thery's avatar
Sylvain Thery committed
33 34 35 36 37 38
#ifndef WITH_QT
#error "Texture class need Qt for loading images, please recompile CGoGN with Qt support"
#endif

// TODO externaliser le chargement d'image pour enlever la dependance Qt ??

Sylvain Thery's avatar
Sylvain Thery committed
39 40 41 42 43 44 45 46 47

namespace CGoGN
{
namespace Utils
{

class GTexture
{
public:
Thery Sylvain's avatar
Thery Sylvain committed
48
	virtual ~GTexture() {}
Sylvain Thery's avatar
Sylvain Thery committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	virtual void bind() {}
};

/**
* Basic class for image management
*/
template < unsigned int DIM, typename TYPE >
class ImageData: public GTexture
{
public:
	typedef Geom::Vector<DIM,unsigned int> COORD;

protected:
	/// pointer on data
	TYPE* m_data_ptr;

	/// size of image
//	unsigned int m_size[DIM];
	COORD m_size;

	/// size of sub dimension (line/slice/...)
//	unsigned int m_sizeSub[DIM];
	COORD m_sizeSub;

	/// local memory allocation ?
	bool m_localAlloc;

	void computeSub();

public:
	/// constructor 
	ImageData();

	/// copy constructor
	ImageData(const ImageData<DIM,TYPE>& img);

	/**
	* create with memory reservation for given size
	* @param size vector with size values
	*/
	ImageData(const COORD& size);

	/**
	 * swap two images
	 */
	virtual void swap(ImageData<DIM,TYPE>& img);


	/**
	 * destructor
	 */
	~ImageData();


	/**
	* create from existing data (no copy)
	* @param data pointer on image data
	* @param size vector with size values
	*/
	void create(TYPE* data, const COORD& size);

	/**
	* create from nothing (mem allocation)
	* @param size vector with size values
	*/
	void create(const COORD& size);

	/**
	* get the size
	* @return vector with size values
	*/
	const COORD& size() const { return m_size;}

	/**
	 * get direct acces to data
	 */
	TYPE* getDataPtr() const  { return m_data_ptr;}
	
	/**
	* get a pointer on data
	*/
	TYPE* getPtrData();

	/**
	* get pixel value
	*/
	TYPE& operator()(unsigned int i);

	/**
	* get pixel value
	*/
	TYPE& operator()(unsigned int i, unsigned int j);

	/**
	* get pixel value
	*/
	TYPE& operator()(unsigned int i, unsigned int j, unsigned int k);

	/**
	* get pixel value
	* @param coord coordinates stored in vector
	*/
	TYPE& texel(const COORD& coord);
	const TYPE& texel(const COORD& coord) const;
	
	template < typename TYPE2 >
	void convert(ImageData<DIM,TYPE2>& output, TYPE2 (*convertFunc)(const TYPE&));

	///texel access without assertion test (internal use, but not protected because of template)
	TYPE& texel(unsigned int i);

	///texel access without assertion test (internal use)
	TYPE& texel(unsigned int i, unsigned int j);

	///texel access without assertion test (internal use)
	TYPE& texel(unsigned int i, unsigned int j, unsigned int k);

	///texel access without assertion test (internal use, but not protected because of template)
	const TYPE& texel(unsigned int i) const;

	///texel access without assertion test (internal use)
	const TYPE& texel(unsigned int i, unsigned int j) const;

	///texel access without assertion test (internal use)
	const TYPE& texel(unsigned int i, unsigned int j, unsigned int k) const;


};

/**
* class for convolution filter creation
*/
template <unsigned int DIM>
class Filter: public ImageData<DIM,double>
{
public:
	typedef Geom::Vector<DIM,unsigned int> COORD;
	static Filter<DIM>* createGaussian(int radius, double sigma);
	static Filter<DIM>* createAverage(int radius);
};

/**
* class image with some high level function
*/
template < unsigned int DIM, typename TYPE >
class Image: public ImageData<DIM,TYPE>
{
public:
	typedef Geom::Vector<DIM,unsigned int> COORD;

protected:

	template <typename TYPEDOUBLE>
	TYPE applyFilterOneTexel(const Filter<DIM>& filter, const COORD& t) const;

	/// swap two texel in dim 1 image
	void swapTexels(unsigned int x0, unsigned int x1);

	/// swap two texel in dim 2 image
	void swapTexels(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1);

	/// swap two texel in dim 3 image
	void swapTexels(unsigned int x0, unsigned int y0, unsigned int z0, unsigned int x1, unsigned int y1, unsigned int z1);


public:
	/// constructor
	Image();


	/// constructor with given size
	Image(const COORD& size);

	/// destructor
	~Image();


Sylvain Thery's avatar
Sylvain Thery committed
226 227 228 229 230 231 232 233
	/**
	* Load from memory ptr,
	* a copy if performed (ptr could be destructed just after calling
	* @param ptr a pointer on memory source image (allocation must be coherent with other parameters)
	* @param w width of image
	* @param h heighy of image
	* @param bpp byte per pixel of image 
	*/
unknown's avatar
unknown committed
234
	bool load(const unsigned char *ptr, unsigned int w, unsigned int h, unsigned int bpp);
Sylvain Thery's avatar
Sylvain Thery committed
235 236
	
#ifdef WITH_QT
Sylvain Thery's avatar
Sylvain Thery committed
237 238 239 240 241
	/// load from file
	bool load(const std::string& filename);

	/// load from file
	void save(const std::string& filename);
Sylvain Thery's avatar
Sylvain Thery committed
242
#endif
Sylvain Thery's avatar
Sylvain Thery committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

	/**
	* crop image
	*/
	void crop(const COORD& origin, const COORD& sz);

	/**
	* create a subimage
	*/
	Image<DIM,TYPE>* subImage(const COORD& origin, const COORD& sz);


	template <typename TYPEDOUBLE>
	Image<DIM,TYPE>* subSampleToNewImage2();

	/**
	 * scale image by 1/2
	 */
	template <typename TYPEDOUBLE>
	void subSample2();

	/**
	* scale image
	*/
	void scaleNearest(const COORD& newSize);

	/**
	 * scale image to new one
	 */
	Image<DIM,TYPE>* scaleNearestToNewImage(const COORD& newSize);

	/**
	* apply convultion filter
	* Q? what about the borders
	*/
	template <typename TYPEDOUBLE>
	Image<DIM,TYPE>* applyFilter(const Filter<DIM>& filter);

	/**
	* flip image along one axis
	* @param axis 1=X, 2=Y, 3=Z
	*/
	void flip(unsigned int axis);
	

	Image<DIM,TYPE>* rotate90ToNewImage(int axis);

	/**
	* rotation of 90 degrees in counterclockwise direction around axis 
	* @param axis 1=X, 2=Y, 3=Z (negative values : opposite directions)
	* @warning sizes are permutted
	*/
	void rotate90(int axis);
	
};




/**
* Texture class
*/
template < unsigned int DIM, typename TYPE >
class Texture: public Image<DIM,TYPE>
{
protected:
	/**
	* texture id
	*/
312
	CGoGNGLuint m_id;
Sylvain Thery's avatar
Sylvain Thery committed
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
	
	/**
	 * dimension of texture
	 */
	GLenum m_target;

	/**
	* DIM_TEXEL
	*	1			LUMINANCE
	*	2
	*	3			RGB
	*	4			RGBA
	*/		
	int m_compo;

	/**
	* GL_UNSIGNED_BYTE,
	* GL_BYTE, 
	* GL_BITMAP,
	* GL_UNSIGNED_SHORT,
	* GL_SHORT,
	* GL_UNSIGNED_INT,
	* GL_INT,
	* GL_FLOAT
	* Extract type from TYPE with dynamic_cast 
	*/
	GLenum m_type;

	/**
	 * give GL format from m_compo
	 */
	GLenum format();

	GLenum internalFormat();

	/// check alignment of texel in memory for optimized transfer
	void checkAlignment();

public:
	typedef Geom::Vector<DIM,unsigned int> COORD;

	/**
	* constructor (gen id)
	* @param type of data in texel (GL_UNSIGNED_BYE../GL_INT.../GL_FLOAT..)
	*/
	Texture(GLenum type = GL_UNSIGNED_BYTE);


	/**
	* bind the texture	
	*/
	void bind();

	/**
	* update texture on graphic memory
	*/
	void update();

	/**
	* update a part of the texture
	* @param origin (lower left corner)
	* @param sz size of sub image to send to GPU
	*/
	void update(const COORD& origin, const COORD& sz);
	
	/**
	* set filtering texture param
	* @param GL_NEAREST orGL_LINEAR
	*/
	void setFiltering(GLint param);

	/**
	* set filtering texture param
	* @param param GL_CLAMP, GL_CLAMP_TO_BORDER, GL_CLAMP_TO_EDGE, GL_MIRRORED_REPEAT, or GL_REPEAT
	*/
	void setWrapping(GLint param);
};


} //endnamespace
} //endnamespace

#include "Utils/textures.hpp"

#endif