textures.hpp 24.5 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 20 21 22
/*******************************************************************************
* 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.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
Sylvain Thery's avatar
Sylvain Thery committed
23
*******************************************************************************/
Sylvain Thery's avatar
Sylvain Thery committed
24 25 26

#include "Utils/cgognStream.h"

Sylvain Thery's avatar
Sylvain Thery committed
27 28 29 30 31 32 33 34 35 36

namespace CGoGN
{
namespace Utils
{

// implementation class ImageData

template < unsigned int DIM, typename TYPE >
ImageData<DIM,TYPE>::ImageData():
Sylvain Thery's avatar
Sylvain Thery committed
37 38
m_data_ptr(NULL),
m_size(0),
Sylvain Thery's avatar
Sylvain Thery committed
39 40
m_sizeSub(0),
m_localAlloc(false)
Sylvain Thery's avatar
Sylvain Thery committed
41
{
Sylvain Thery's avatar
Sylvain Thery committed
42 43 44
}

template < unsigned int DIM, typename TYPE >
Sylvain Thery's avatar
Sylvain Thery committed
45 46 47
ImageData<DIM,TYPE>::ImageData(const ImageData<DIM,TYPE>& img):
m_size(img.m_size),
m_sizeSub(img.m_sizeSub),
Sylvain Thery's avatar
Sylvain Thery committed
48
m_localAlloc(true)
Sylvain Thery's avatar
Sylvain Thery committed
49
{
Sylvain Thery's avatar
Sylvain Thery committed
50 51 52 53 54 55 56 57 58 59 60
	if (img.m_qimg == NULL)
	{
		if (img.m_data_ptr != NULL)
		{
			m_data_ptr = new TYPE[m_sizeSub[DIM-1]];
			memcpy(m_data_ptr, img.m_data_ptr, sizeof(TYPE)*m_sizeSub[DIM-1]);
			this->m_qimg = NULL;
		}
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
61 62 63 64 65 66 67 68

template < unsigned int DIM, typename TYPE >
ImageData<DIM,TYPE>::ImageData(const COORD& size):
m_size(size),
m_localAlloc(true)
{
	computeSub();
	m_data_ptr = new TYPE[m_sizeSub[DIM-1]];
Sylvain Thery's avatar
Sylvain Thery committed
69 70
}

Sylvain Thery's avatar
Sylvain Thery committed
71

Sylvain Thery's avatar
Sylvain Thery committed
72 73 74 75 76 77 78 79

template < unsigned int DIM, typename TYPE >
ImageData<DIM,TYPE>::~ImageData()
{
	if (m_data_ptr!=NULL)
	{
		delete[] m_data_ptr;
	}
Sylvain Thery's avatar
Sylvain Thery committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}


template < unsigned int DIM, typename TYPE >
void ImageData<DIM,TYPE>::swap(ImageData<DIM,TYPE>& img)
{
	COORD tmp = m_size;
	m_size = img.m_size;
	img.m_size = tmp;

	tmp = m_sizeSub;
	m_sizeSub = img.m_sizeSub;
	img.m_sizeSub = tmp;

	TYPE* prt_temp= m_data_ptr;
	m_data_ptr = img.m_data_ptr;
	img.m_data_ptr = prt_temp;
Sylvain Thery's avatar
Sylvain Thery committed
97 98
}

Sylvain Thery's avatar
Sylvain Thery committed
99 100


Sylvain Thery's avatar
Sylvain Thery committed
101 102 103 104 105 106 107 108 109
template < unsigned int DIM, typename TYPE >
void ImageData<DIM,TYPE>::computeSub()
{
	unsigned int sub=1;
	for (unsigned int i=0; i<DIM; ++i)
	{
		sub *= m_size[i];
		m_sizeSub[i] = sub;
	}
Sylvain Thery's avatar
Sylvain Thery committed
110 111
}

Sylvain Thery's avatar
Sylvain Thery committed
112 113 114 115

template < unsigned int DIM, typename TYPE >
void ImageData<DIM,TYPE>::create(TYPE* data, const COORD& size)
{
Sylvain Thery's avatar
Sylvain Thery committed
116
	// first free old image if exist
Sylvain Thery's avatar
Sylvain Thery committed
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	if (this->m_qimg != NULL)
	{
		delete this->m_qmg;
	}
	else if (m_data_ptr!=NULL)
	{
		delete[] m_data_ptr;
	}

	m_size = size;
	computeSub();
	m_data_ptr = data;
	this->m_qimg = NULL;
}

template < unsigned int DIM, typename TYPE >
void ImageData<DIM,TYPE>::create(const COORD& size)
Sylvain Thery's avatar
Sylvain Thery committed
134 135
{
	// first free old image if exist
Sylvain Thery's avatar
Sylvain Thery committed
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
	if (m_data_ptr!=NULL)
	{
		delete[] m_data_ptr;
	}

	m_size = size;
	computeSub();
	m_data_ptr = new TYPE[m_sizeSub[DIM-1]];
}

	
template < unsigned int DIM, typename TYPE >
TYPE* ImageData<DIM,TYPE>::getPtrData()
{
	return m_data_ptr;
}

template < unsigned int DIM, typename TYPE >
TYPE& ImageData<DIM,TYPE>::operator()(unsigned int i)
{
	CGoGN_STATIC_ASSERT(DIM==1, incompatible_image_dimension) ;
	assert(i<m_size[0]);
	return m_data_ptr[i];
}

template < unsigned int DIM, typename TYPE >
TYPE& ImageData<DIM,TYPE>::operator()(unsigned int i, unsigned int j)
{
	CGoGN_STATIC_ASSERT(DIM==2, incompatible_image_dimension) ;
	assert(i<m_size[0]);
	assert(j<m_size[1]);
	return m_data_ptr[ i + j*m_sizeSub[0] ];
}


template < unsigned int DIM, typename TYPE >
TYPE& ImageData<DIM,TYPE>::operator()(unsigned int i, unsigned int j, unsigned int k)
{
	CGoGN_STATIC_ASSERT(DIM==3, incompatible_image_dimension) ;
	assert(i<m_size[0]);
	assert(j<m_size[1]);
	assert(k<m_size[2]);
	return m_data_ptr[ i + j*m_sizeSub[0] + k*m_sizeSub[1] ];
}
Sylvain Thery's avatar
Sylvain Thery committed
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 226 227 228 229


template < unsigned int DIM, typename TYPE >
TYPE& ImageData<DIM,TYPE>::texel(unsigned int i)
{
	return m_data_ptr[i];
}

template < unsigned int DIM, typename TYPE >
TYPE& ImageData<DIM,TYPE>::texel(unsigned int i, unsigned int j)
{
	return m_data_ptr[ i + j*m_sizeSub[0] ];
}


template < unsigned int DIM, typename TYPE >
TYPE& ImageData<DIM,TYPE>::texel(unsigned int i, unsigned int j, unsigned int k)
{
	return m_data_ptr[ i + j*m_sizeSub[0] + k*m_sizeSub[1] ];
}

template < unsigned int DIM, typename TYPE >
const TYPE& ImageData<DIM,TYPE>::texel(unsigned int i) const
{
	return m_data_ptr[i];
}

template < unsigned int DIM, typename TYPE >
const TYPE& ImageData<DIM,TYPE>::texel(unsigned int i, unsigned int j) const
{
	return m_data_ptr[ i + j*m_sizeSub[0] ];
}


template < unsigned int DIM, typename TYPE >
const TYPE& ImageData<DIM,TYPE>::texel(unsigned int i, unsigned int j, unsigned int k) const
{
	return m_data_ptr[ i + j*m_sizeSub[0] + k*m_sizeSub[1] ];
}



template < unsigned int DIM, typename TYPE >
TYPE& ImageData<DIM,TYPE>::texel(const COORD& coord)
{
	TYPE* ptr = m_data_ptr;
	for (unsigned int i=0; i<DIM; ++i)
		ptr += coord[i]*m_sizeSub[i];
	return *ptr;
}
Sylvain Thery's avatar
Sylvain Thery committed
230 231 232 233 234 235 236 237 238 239


template < unsigned int DIM, typename TYPE >
const TYPE& ImageData<DIM,TYPE>::texel(const COORD& coord) const
{
	TYPE* ptr = m_data_ptr;
	for (unsigned int i=0; i<DIM; ++i)
		ptr += coord[i]*m_sizeSub[i];
	return *ptr;
}
Sylvain Thery's avatar
Sylvain Thery committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

template < unsigned int DIM, typename TYPE >
template < typename TYPE2 >
void ImageData<DIM,TYPE>::convert(ImageData<DIM,TYPE2>& output, TYPE2 (*convertFunc)(const TYPE&))
{
	output.create(m_size);

	TYPE*  ptrSrc = m_data_ptr;
	TYPE2* ptrDst = output.getDataPtr();

	if (convertFunc)
		for (unsigned int i = 0; i<m_sizeSub[DIM-1]; ++i)
			*ptrDst++ = (*convertFunc)(*ptrSrc++);
}


Sylvain Thery's avatar
Sylvain Thery committed
256 257 258
// implementation class Image

template < unsigned int DIM, typename TYPE >
Sylvain Thery's avatar
Sylvain Thery committed
259
Image<DIM,TYPE>::Image()
Sylvain Thery's avatar
Sylvain Thery committed
260
{
Sylvain Thery's avatar
Sylvain Thery committed
261 262 263 264
}

template < unsigned int DIM, typename TYPE >
Image<DIM,TYPE>::Image(const COORD& size):
Sylvain Thery's avatar
Sylvain Thery committed
265
ImageData<DIM,TYPE>(size)
Sylvain Thery's avatar
Sylvain Thery committed
266 267 268 269 270 271 272
{
}


template < unsigned int DIM, typename TYPE >
Image<DIM,TYPE>::~Image()
{
Sylvain Thery's avatar
Sylvain Thery committed
273
	delete[] m_data_ptr;
Sylvain Thery's avatar
Sylvain Thery committed
274 275
	this->m_data_ptr = NULL;
}
Sylvain Thery's avatar
Sylvain Thery committed
276

Sylvain Thery's avatar
Sylvain Thery committed
277
/*
Sylvain Thery's avatar
Sylvain Thery committed
278 279 280
template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::swap(Image<DIM,TYPE>& img)
{
Sylvain Thery's avatar
Sylvain Thery committed
281 282 283 284 285 286 287
	if ((this->m_qimg != NULL) && (img.m_qimg != NULL))
	{
		QImage* ptr = this->m_qimg;
		this->m_qimg = img.m_qimg;
		img.m_qimg = ptr;
	}
	
Sylvain Thery's avatar
Sylvain Thery committed
288 289
	ImageData<DIM,TYPE>::swap(img);
}
Sylvain Thery's avatar
Sylvain Thery committed
290
*/
Sylvain Thery's avatar
Sylvain Thery committed
291

Sylvain Thery's avatar
Sylvain Thery committed
292
template < unsigned int DIM, typename TYPE >
Sylvain Thery's avatar
Sylvain Thery committed
293
bool Image<DIM,TYPE>::load(const unsigned char *ptr, unsigned int w, unsigned int h, unsigned int bpp)
Sylvain Thery's avatar
Sylvain Thery committed
294 295 296
{
	CGoGN_STATIC_ASSERT(DIM==2, incompatible_Vector_constructor_dimension);
	
Sylvain Thery's avatar
Sylvain Thery committed
297
	// compatible TYPE
Sylvain Thery's avatar
Sylvain Thery committed
298
	if (bpp != sizeof(TYPE))
Sylvain Thery's avatar
Sylvain Thery committed
299
	{
Sylvain Thery's avatar
Sylvain Thery committed
300
		CGoGNout << "Image::load incompatible type: bbp=" << bpp << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
301
		CGoGNout << "sizeof(TYPE)="<<sizeof(TYPE)<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
302
		delete ptr;
Sylvain Thery's avatar
Sylvain Thery committed
303 304 305
		return false;
	}

Sylvain Thery's avatar
Sylvain Thery committed
306
	// destroy old data	
Sylvain Thery's avatar
Sylvain Thery committed
307
	 if (this->m_data_ptr != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
308
		delete[] this->m_data_ptr;
Sylvain Thery's avatar
Sylvain Thery committed
309

Sylvain Thery's avatar
Sylvain Thery committed
310 311 312 313
	this->m_data_ptr = new T[sizeof(TYPE)*w*h];
	memcpy(this->m_data_ptr, img.bits(), sizeof(TYPE)*w*h);
	this->m_size[0] = w;
	this->m_size[1] = h;
Sylvain Thery's avatar
Sylvain Thery committed
314

Sylvain Thery's avatar
Sylvain Thery committed
315 316
	this->computeSub();

Sylvain Thery's avatar
Sylvain Thery committed
317
	return true;
Sylvain Thery's avatar
Sylvain Thery committed
318 319 320
}


Sylvain Thery's avatar
Sylvain Thery committed
321 322

#ifdef WITH_QT
Sylvain Thery's avatar
Sylvain Thery committed
323
template < unsigned int DIM, typename TYPE >
Sylvain Thery's avatar
Sylvain Thery committed
324
bool Image<DIM,TYPE>::load(const std::string& filename)
Sylvain Thery's avatar
Sylvain Thery committed
325 326 327
{
	CGoGN_STATIC_ASSERT(DIM==2, incompatible_Vector_constructor_dimension);

Sylvain Thery's avatar
Sylvain Thery committed
328
	QImage* ptr = new QImage(filename.c_str());
Sylvain Thery's avatar
Sylvain Thery committed
329

Sylvain Thery's avatar
Sylvain Thery committed
330
	if (ptr == NULL)
Sylvain Thery's avatar
Sylvain Thery committed
331
	{
unknown's avatar
unknown committed
332
		CGoGNout << "Impossible to load "<< filename << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
333 334 335
		return false;
	}
	
Sylvain Thery's avatar
Sylvain Thery committed
336 337
	//	get the info of images
	unsigned int bpp = ptr->depth() / 8;
Sylvain Thery's avatar
Sylvain Thery committed
338 339

	// compatible TYPE
Sylvain Thery's avatar
Sylvain Thery committed
340
	if (bpp < sizeof(TYPE))
Sylvain Thery's avatar
Sylvain Thery committed
341
	{
Sylvain Thery's avatar
Sylvain Thery committed
342
		CGoGNout << "Image::load incompatible type: bbp=" << bpp << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
343 344
		CGoGNout << "sizeof(TYPE)="<<sizeof(TYPE)<< CGoGNendl;
		delete ptr;
Sylvain Thery's avatar
Sylvain Thery committed
345 346 347
		return false;
	}

Sylvain Thery's avatar
Sylvain Thery committed
348
	// destroy old data	
Sylvain Thery's avatar
Sylvain Thery committed
349
	 if (this->m_data_ptr != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
350 351 352 353 354 355 356
		delete[] this->m_data_ptr;

	if (bpp > sizeof(TYPE))
	{
		if (sizeof(TYPE) == 3)
		{
			QImage img = ptr->convertToFormat(QImage::Format_RGB888);
unknown's avatar
unknown committed
357
			this->m_data_ptr = new TYPE[3*img.width()*img.height()];
Sylvain Thery's avatar
Sylvain Thery committed
358
			memcpy(this->m_data_ptr, img.bits(), 3*img.width()*img.height());
Sylvain Thery's avatar
Sylvain Thery committed
359 360 361 362
		}
		else if (sizeof(TYPE) == 1)
		{
			QImage img = ptr->convertToFormat(QImage::Format_Indexed8);
unknown's avatar
unknown committed
363
			this->m_data_ptr = new TYPE[img.width()*img.height()];
Sylvain Thery's avatar
Sylvain Thery committed
364
			memcpy(this->m_data_ptr, img.bits(), img.width()*img.height());
Sylvain Thery's avatar
Sylvain Thery committed
365
		}
Sylvain Thery's avatar
Sylvain Thery committed
366 367 368
	}
	else
	{
unknown's avatar
unknown committed
369
		this->m_data_ptr = new TYPE[ptr->width()*ptr->height()];
Sylvain Thery's avatar
Sylvain Thery committed
370 371
		memcpy(this->m_data_ptr, ptr->bits(), ptr->width()*ptr->height());
	}
Sylvain Thery's avatar
Sylvain Thery committed
372

Sylvain Thery's avatar
Sylvain Thery committed
373 374
	this->m_size[0] = ptr->width();
	this->m_size[1] = ptr->height();
Sylvain Thery's avatar
Sylvain Thery committed
375 376
	this->computeSub();

Sylvain Thery's avatar
Sylvain Thery committed
377
	delete ptr;
Sylvain Thery's avatar
Sylvain Thery committed
378 379 380 381 382 383 384 385 386
	return true;
}


template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::save(const std::string& filename)
{
	CGoGN_STATIC_ASSERT(DIM==2, incompatible_Vector_constructor_dimension);

Sylvain Thery's avatar
Sylvain Thery committed
387
	if (this->m_data_ptr != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
388
	{
unknown's avatar
unknown committed
389
		QImage* ptrIm = NULL;
Sylvain Thery's avatar
Sylvain Thery committed
390 391 392
		switch(sizeof(TYPE))
		{
		case 1:
unknown's avatar
unknown committed
393
			ptrIm = new QImage((uchar *)(this->m_data_ptr), this->m_size[0],this->m_size[1], QImage::Format_Indexed8);
Sylvain Thery's avatar
Sylvain Thery committed
394 395
			break;
		case 3:
unknown's avatar
unknown committed
396
			ptrIm = new QImage((uchar *)(this->m_data_ptr), this->m_size[0],this->m_size[1], QImage::Format_RGB888);
Sylvain Thery's avatar
Sylvain Thery committed
397 398
			break;
		case 4:
unknown's avatar
unknown committed
399
			ptrIm = new QImage((uchar *)(this->m_data_ptr), this->m_size[0],this->m_size[1], QImage::Format_ARGB32);
Sylvain Thery's avatar
Sylvain Thery committed
400 401 402 403
			break;
		default:
			break;
		}
Sylvain Thery's avatar
Sylvain Thery committed
404
	}
unknown's avatar
unknown committed
405 406
	ptrIm->save(QString(filename.c_str()));
	delete ptrIm;
Sylvain Thery's avatar
Sylvain Thery committed
407
}
Sylvain Thery's avatar
Sylvain Thery committed
408
#endif
Sylvain Thery's avatar
Sylvain Thery committed
409 410


Sylvain Thery's avatar
Sylvain Thery committed
411 412
template < unsigned int DIM, typename TYPE >
Image<DIM,TYPE>* Image<DIM,TYPE>::subImage(const COORD& origin, const COORD& sz)
Sylvain Thery's avatar
Sylvain Thery committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
{
	COORD szz;
	unsigned int nbt=1;

	for (unsigned int i=0; i<DIM; ++i)
	{
		if ((origin[i] + sz[i]) > this->m_size[i])
			szz[i] = this->m_size[i] - origin[i];
		else
			szz[i] = sz[i];
		nbt *= szz[i];
	}

	Image<DIM,TYPE>* newImg = new Image<DIM,TYPE>;
	newImg->create(szz);

	switch(DIM)
	{
	case 1:
		for (unsigned int i=0; i< szz[0]; ++i)
			newImg->texel(i) = this->texel(origin[0]+i);
		break;
	case 2:
			for (unsigned int j=0; j< szz[1]; ++j)
				for (unsigned int i=0; i< szz[0]; ++i)
					newImg->texel(i,j) = this->texel(origin[0]+i,origin[1]+j);
			break;
	case 3:
			for (unsigned int k=0; k< szz[2]; ++k)
				for (unsigned int j=0; j< szz[1]; ++j)
					for (unsigned int i=0; i< szz[0]; ++i)
						newImg->texel(i,j,k) = this->texel(origin[0]+i,origin[1]+j,origin[2]+k);
		break;
	}

Sylvain Thery's avatar
Sylvain Thery committed
448 449
	return newImg;
}
Sylvain Thery's avatar
Sylvain Thery committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::crop(const COORD& origin, const COORD& sz)
{
	Image<DIM,TYPE>* newImg = subImage(origin,sz);
	swap(*newImg);
	delete newImg;
}




template < unsigned int DIM, typename TYPE >
Image<DIM,TYPE>* Image<DIM,TYPE>::scaleNearestToNewImage(const COORD& newSize)
{
	Image<DIM,TYPE>* newImg = new Image<DIM,TYPE>;
	newImg->create(newSize);

	double inc0 = double(this->m_size[0])/double(newSize[0]);

	switch(DIM)
	{
	case 1:
		{
			double p0 = inc0/2.0 - 0.5;
			for (unsigned int i=0; i< newSize[0]; ++i)
			{
				newImg->texel(i) = this->texel( (unsigned int)(rint(p0)) );
				p0 += inc0;
			}
		}
		break;
	case 2:
		{
			double inc1 = double(this->m_size[1])/double(newSize[1]);
			double p1 = inc1/2.0 - 0.5;

			for (unsigned int j=0; j< newSize[1]; ++j)
			{
				double p0 = inc0/2.0 - 0.5;
				for (unsigned int i=0; i< newSize[0]; ++i)
				{
					newImg->texel(i,j) = this->texel( (unsigned int)(rint(p0)), (unsigned int)(rint(p1)) );
					p0 += inc0;
				}
				p1 += inc1;
			}
		}
		break;
	case 3:
		{
			double inc1 = double(this->m_size[1])/double(newSize[1]);
			double inc2 = double(this->m_size[2])/double(newSize[2]);
			double p2 = inc2/2.0 - 0.5;

			for (unsigned int k=0; k< newSize[2]; ++k)
			{
				double p1 = inc1/2.0 - 0.5;
				for (unsigned int j=0; j< newSize[1]; ++j)
				{
					double p0 = inc0/2.0 - 0.5;
					for (unsigned int i=0; i< newSize[0]; ++i)
					{
						newImg->texel(i,j,k) = this->texel( (unsigned int)(rint(p0)), (unsigned int)(rint(p1)), (unsigned int)(rint(p2)) );
						p0 += inc0;
					}
					p1 += inc1;
				}
				p2 += inc2;
			}
		}
		break;
	}

	return newImg;
Sylvain Thery's avatar
Sylvain Thery committed
525
}
Sylvain Thery's avatar
Sylvain Thery committed
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617



template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::scaleNearest(const COORD& newSize)
{
	Image<DIM,TYPE>* newImg = scaleNearestToNewImage(newSize);
	swap(*newImg);
	delete newImg;
}


template < unsigned int DIM, typename TYPE >
template <typename TYPEDOUBLE>
Image<DIM,TYPE>* Image<DIM,TYPE>::subSampleToNewImage2()
{
	COORD newSize = this->m_size/2;
	Image<DIM,TYPE>* newImg = new Image<DIM,TYPE>(newSize);
//	newImg->create(newSize);

	switch(DIM)
	{
	case 1:
			for (unsigned int i=0; i< newSize[0]; ++i)
				newImg->texel(i) =  TYPE(TYPEDOUBLE(this->texel(2*i)) +   TYPEDOUBLE(this->texel(2*i+1)) /2.0);
		break;
	case 2:
			for (unsigned int j=0; j< newSize[1]; ++j)
				for (unsigned int i=0; i< newSize[0]; ++i)
				{
					TYPEDOUBLE sum =  TYPEDOUBLE(this->texel(2*i,2*j));
					sum +=  TYPEDOUBLE(this->texel(2*i+1,2*j));
					sum +=  TYPEDOUBLE(this->texel(2*i+1,2*j+1));
					sum +=  TYPEDOUBLE(this->texel(2*i  ,2*j+1));
					newImg->texel(i,j) = TYPE (sum/4.0);
				}
		break;
	case 3:
			for (unsigned int k=0; k< newSize[2]; ++k)
				for (unsigned int j=0; j< newSize[1]; ++j)
					for (unsigned int i=0; i< newSize[0]; ++i)
					{
						TYPEDOUBLE sum =  TYPEDOUBLE(this->texel(2*i,2*j,2*k));
						sum +=  TYPEDOUBLE(this->texel(2*i+1,2*j  ,2*k));
						sum +=  TYPEDOUBLE(this->texel(2*i+1,2*j+1,2*k));
						sum +=  TYPEDOUBLE(this->texel(2*i  ,2*j+1,2*k));
						sum +=  TYPEDOUBLE(this->texel(2*i,  2*j,  2*k+1));
						sum +=  TYPEDOUBLE(this->texel(2*i+1,2*j  ,2*k+1));
						sum +=  TYPEDOUBLE(this->texel(2*i+1,2*j+1,2*k+1));
						sum +=  TYPEDOUBLE(this->texel(2*i  ,2*j+1,2*k+1));
						newImg->texel(i,j,k) = TYPE (sum/8.0);
					}
		break;
	}
	return newImg;
}

template < unsigned int DIM, typename TYPE >
template <typename TYPEDOUBLE>
void Image<DIM,TYPE>::subSample2()
{
	Image<DIM,TYPE>* newImg = subSampleToNewImage2<TYPEDOUBLE>();
	swap(*newImg);
	delete newImg;
}


template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::swapTexels(unsigned int x0, unsigned int x1)
{
	TYPE tempo = this->texel(x0);
	this->texel(x0) = this->texel(x1);
	this->texel(x1) = tempo;
}

template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::swapTexels(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1)
{

	TYPE tempo = this->texel(x0,y0);
	this->texel(x0,y0) = this->texel(x1,y1);
	this->texel(x1,y1) = tempo;
}

template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::swapTexels(unsigned int x0, unsigned int y0, unsigned int z0, unsigned int x1, unsigned int y1, unsigned int z1)
{
	TYPE tempo = this->texel(x0,y0,z0);
	this->texel(x0,y0,z0) = this->texel(x1,y1,z1);
	this->texel(x1,y1,z1) = tempo;
}

Sylvain Thery's avatar
Sylvain Thery committed
618 619 620

template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::flip(unsigned int axis)
Sylvain Thery's avatar
Sylvain Thery committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
{
	assert(axis<=DIM);

	switch(DIM)
	{
	case 1:
		{
			unsigned int sz0 = this->m_size[0]/2;
			for (unsigned int i=0; i< sz0; ++i)
				swapTexels(i, this->m_size[0]-1);
		}
		break;
	case 2:
		if (axis==1)
		{
			unsigned int sz0 = this->m_size[0]/2;
			for (unsigned int j=0; j<  this->m_size[1]; ++j)
				for (unsigned int i=0; i< sz0; ++i)
					swapTexels(i,j, this->m_size[0]-1-i,j );
		}
		else if (axis==2)
		{
			unsigned int sz1 = this->m_size[1]/2;
			for (unsigned int j=0; j< sz1; ++j)
				for (unsigned int i=0; i< this->m_size[0]; ++i)
					swapTexels(i,j, i,this->m_size[1]-1-j);
		}
		break;
	case 3:
		if (axis==1)
		{
			unsigned int sz0 = this->m_size[0]/2;
			for (unsigned int k=0; k< this->m_size[2]; ++k)
				for (unsigned int j=0; j< this->m_size[1]; ++j)
					for (unsigned int i=0; i< sz0; ++i)
						swapTexels(i,j,k, this->m_size[0]-1-i,j,k);
		}
		else if (axis==2)
		{
			unsigned int sz1 = this->m_size[1]/2;
			for (unsigned int k=0; k< this->m_size[2]; ++k)
				for (unsigned int j=0; j< sz1; ++j)
					for (unsigned int i=0; i< this->m_size[0]; ++i)
						swapTexels(i,j,k, i,this->m_size[1]-1-j,k);
		}
		else if (axis==3)
		{
			unsigned int sz2 = this->m_size[2]/2;
			for (unsigned int k=0; k< sz2; ++k)
				for (unsigned int j=0; j< this->m_size[1]; ++j)
					for (unsigned int i=0; i< this->m_size[0]; ++i)
						swapTexels(i,j,k, i,j,this->m_size[2]-1-k);
		}
		break;
	}

Sylvain Thery's avatar
Sylvain Thery committed
677
}
Sylvain Thery's avatar
Sylvain Thery committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714


template < unsigned int DIM, typename TYPE >
Image<DIM,TYPE>* Image<DIM,TYPE>::rotate90ToNewImage(int axis)
{
	COORD newSize;
	Image<DIM,TYPE>* newImg=NULL;

	switch(DIM)
	{
	case 1:
		CGoGNerr << " no rotation with image of dimension 1"<<CGoGNendl;
		break;
	case 2:
		newSize[0] = this->m_size[1];
		newSize[1] = this->m_size[0];
		newImg = new Image<DIM,TYPE>(newSize);

		if (axis==3)
		{
			for (unsigned int j=0; j<  newSize[1]; ++j)
				for (unsigned int i=0; i< newSize[0]; ++i)
					newImg->texel(i,j) = this->texel(j, this->m_size[1]-1-i);
		}
		else if (axis==-3)
		{
			for (unsigned int j=0; j<  newSize[1]; ++j)
				for (unsigned int i=0; i< newSize[0]; ++i)
					newImg->texel(i,j) = this->texel(this->m_size[0]-1-j, i);
		}
		break;
	case 3:
		CGoGNerr << " no yet implermented !"<<CGoGNendl;
		break;

	}
	return newImg;
Sylvain Thery's avatar
Sylvain Thery committed
715 716 717 718
}

template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::rotate90(int axis)
Sylvain Thery's avatar
Sylvain Thery committed
719 720 721
{
	Image<DIM,TYPE>* newImg = rotate90ToNewImage(axis);
	swap(*newImg);
Sylvain Thery's avatar
Sylvain Thery committed
722 723
	delete newImg;
}
Sylvain Thery's avatar
Sylvain Thery committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047


template < unsigned int DIM, typename TYPE >
template <typename TYPEDOUBLE>
TYPE Image<DIM,TYPE>::applyFilterOneTexel(const Filter<DIM>& filter, const COORD& t) const
{
	TYPEDOUBLE val(0);

	COORD tmin = t - filter.size()/2;
	switch(DIM)
	{
	case 1:
		{
			unsigned int sz0 = filter.size()[0];
			for (unsigned int i=0; i<sz0; ++i)
				val += TYPEDOUBLE(texel(tmin[0]+i)) * filter.texel(i);
		}
		break;
	case 2:
		{
			unsigned int sz0 = filter.size()[0];
			unsigned int sz1 = filter.size()[1];
			for (unsigned int j=0; j<sz1; ++j)
				for (unsigned int i=0; i<sz0; ++i)
					val += TYPEDOUBLE(texel(tmin[0]+i,tmin[1]+j)) * filter.texel(i,j);
		}
		break;
	case 3:
		{
			unsigned int sz0 = filter.size()[0];
			unsigned int sz1 = filter.size()[1];
			unsigned int sz2 = filter.size()[2];
			for (unsigned int k=0; k<sz2; ++k)
				for (unsigned int j=0; j<sz1; ++j)
					for (unsigned int i=0; i<sz0; ++i)
						val += TYPEDOUBLE(texel(tmin[0]+i,tmin[1]+j,tmin[2]+k)) * filter.texel(i,j,k);
		}
		break;
	}

	return TYPE(val);
}

template < unsigned int DIM, typename TYPE >
template <typename TYPEDOUBLE>
Image<DIM,TYPE>* Image<DIM,TYPE>::applyFilter(const Filter<DIM>& filter)
{
	Image<DIM,TYPE>* newImg = new Image<DIM,TYPE>;
	newImg->create(this->m_size);

	// on squizz les bords !!
	switch(DIM)
	{
	case 1:

		break;
	case 2:
		{
			unsigned int xb = filter.size()[0]/2;
			unsigned int yb = filter.size()[1]/2;
			unsigned int xe = this->m_size[0] - xb;
			unsigned int ye = this->m_size[1] - yb;
			for (unsigned int j=0; j< this->m_size[1]; ++j)
				for (unsigned int i=0; i< this->m_size[0]; ++i)
					if ((i>xb)&&(i<xe) && (j>yb)&&(j<ye))
						newImg->texel(i,j) = applyFilterOneTexel<TYPEDOUBLE>(filter,COORD(i,j));
					else
						newImg->texel(i,j) = this->texel(i,j);
		}
		break;
	case 3:
		break;
	}

	return newImg;
}



template <unsigned int DIM>
Filter<DIM>* Filter<DIM>::createAverage(int radius)
{
	unsigned int sz=2*radius+1;

	Filter<DIM>* filter = new Filter<DIM>;
	filter->create(COORD(sz));

	switch(DIM)
	{
	case 1:
		{
			double val = 1.0/double(sz);
			for (unsigned int i=0; i< sz; ++i)
				filter->texel(i) = val;
		}
		break;
	case 2:
		{
			double val = 1.0/double(sz*sz);
			for (unsigned int j=0; j< sz; ++j)
				for (unsigned int i=0; i< sz; ++i)
					filter->texel(i,j) = val;
		}
		break;
	case 3:
		{
			double val = 1.0/double(sz*sz*sz);
			for (unsigned int k=0; k< sz; ++k)
				for (unsigned int j=0; j< sz; ++j)
					for (unsigned int i=0; i< sz; ++i)
						filter->texel(i,j) = val;
			}
		break;
	}

	return filter;
}


template <unsigned int DIM>
Filter<DIM>* Filter<DIM>::createGaussian(int radius, double sigma)
{
	int sz=2*radius+1;

	Filter<DIM>* filter = new Filter<DIM>;
	filter->create(COORD(sz));

	double sig2 = 2.0*sigma*sigma;

	switch(DIM)
	{
	case 1:
		{
			double coef = 1.0/(sigma*sqrt(2.0*M_PI));
			for (int i=0; i< sz; ++i)
				filter->texel(i) = coef * exp(-double(i-radius)*(i-radius)/sig2);
		}
		break;
	case 2:
		{
			double tot=0.0;
			double coef = 1.0/(sig2*M_PI);
			for (int j=0; j< sz; ++j)
				for (int i=0; i< sz; ++i)
					filter->texel(i,j) = coef * exp(-double((i-radius)*(i-radius)+(j-radius)*(j-radius))/sig2);
		}
		break;
	case 3:
		CGoGNout<< "Not implemented"<< CGoGNendl;
		break;
	}

	return filter;

}



template < unsigned int DIM, typename TYPE >
Texture<DIM,TYPE>::Texture(GLenum type):
m_type(type)
{
	switch(DIM)
	{
	case 1:
		m_target = GL_TEXTURE_1D;
		break;
	case 2:
		m_target = GL_TEXTURE_2D;
		break;
	case 3:
		m_target = GL_TEXTURE_3D;
		break;
	}

	switch(type)
	{
	case GL_UNSIGNED_BYTE:
	case GL_BYTE:
		m_compo = sizeof(TYPE);
		break;
	case GL_UNSIGNED_SHORT:
	case GL_SHORT:
		m_compo = sizeof(TYPE)/2;
		break;
	case GL_UNSIGNED_INT:
	case GL_INT:
	case GL_FLOAT:
		m_compo = sizeof(TYPE)/4;
		break;
	}

	glGenTextures(1, &m_id);

	glBindTexture(m_target, m_id);
	glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexParameteri(m_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(m_target, GL_TEXTURE_WRAP_T, GL_CLAMP);

}

template < unsigned int DIM, typename TYPE >
GLenum Texture<DIM,TYPE>::format()
{
	if (m_compo == 1)
		return GL_LUMINANCE;
	if (m_compo == 2)
		return GL_LUMINANCE_ALPHA;
	if (m_compo == 3)
		return GL_RGB;
	return GL_RGBA;
}

template < unsigned int DIM, typename TYPE >
GLenum Texture<DIM,TYPE>::internalFormat()
{
	if (m_type == GL_FLOAT)
	{
		if (m_compo == 1)
			return GL_LUMINANCE32F_ARB;
		if (m_compo == 2)
			return GL_LUMINANCE_ALPHA32F_ARB;
		if (m_compo == 3)
			return GL_RGB32F_ARB;
		return GL_RGBA32F_ARB;
	}
	if (m_compo == 1)
		return GL_LUMINANCE;
	if (m_compo == 2)
		return GL_LUMINANCE_ALPHA;
	if (m_compo == 3)
		return GL_RGB;
	return GL_RGBA;
}

template < unsigned int DIM, typename TYPE >
void Texture<DIM,TYPE>::bind()
{
	glBindTexture(m_target, m_id);
}


template < unsigned int DIM, typename TYPE >
void Texture<DIM,TYPE>::checkAlignment()
{
	unsigned int sz = sizeof(TYPE)*this->m_size[0];
	if (sz%4==0)
		glPixelStorei(GL_UNPACK_ALIGNMENT,4);
	else if (sz%2==0)
		glPixelStorei(GL_UNPACK_ALIGNMENT,2);
	else
		glPixelStorei(GL_UNPACK_ALIGNMENT,1);
}


template < unsigned int DIM, typename TYPE >
void Texture<DIM,TYPE>::update()
{
	glBindTexture(m_target, m_id);
	checkAlignment();
	switch(DIM)
	{
	case 1:
		glTexImage1D(m_target, 0, internalFormat(), this->m_size[0], 0, format(), m_type, this->m_data_ptr);
		break;
	case 2:
		glTexImage2D(m_target, 0, internalFormat(), this->m_size[0], this->m_size[1], 0, format(), m_type, this->m_data_ptr);
		break;
	case 3:
		glTexImage3D(m_target, 0, internalFormat(), this->m_size[0], this->m_size[1], this->m_size[2], 0, format(), m_type, this->m_data_ptr);
		break;
	default:
		CGoGNerr << "Texture of dimension >3 not supported by OpenGL !"<<CGoGNendl;
		break;
	}
}

template < unsigned int DIM, typename TYPE >
void Texture<DIM,TYPE>::update(const COORD& origin, const COORD& sz)
{
	glBindTexture(m_target, m_id);
	checkAlignment();
	switch(DIM)
	{
	case 1:
		glTexSubImage1D(m_target, 0, origin[0], sz[0], format(), m_type, this->m_data_ptr);
		break;
	case 2:
		glTexSubImage2D(m_target, 0, origin[0], origin[1], sz[0], sz[1], format(), m_type, this->m_data_ptr);
		break;
	case 3:
		glTexSubImage3D(m_target, 0, origin[0], origin[1], origin[2], sz[0], sz[1], sz[2], format(), m_type, this->m_data_ptr);
		break;
	default:
		CGoGNerr << "Texture of dimension >3 not supported by OpenGL !"<<CGoGNendl;
		break;
	}
}


template < unsigned int DIM, typename TYPE >
void Texture<DIM,TYPE>::setFiltering(GLint param)
{
	glBindTexture(m_target, m_id);
	glTexParameteri(m_target, GL_TEXTURE_MIN_FILTER, param);
	glTexParameteri(m_target, GL_TEXTURE_MAG_FILTER, param);
}



template < unsigned int DIM, typename TYPE >
void Texture<DIM,TYPE>::setWrapping(GLint param)
{
	glBindTexture(m_target, m_id);
	glTexParameteri(m_target, GL_TEXTURE_WRAP_S, param);
	glTexParameteri(m_target, GL_TEXTURE_WRAP_T, param);
	glTexParameteri(m_target, GL_TEXTURE_WRAP_R, param);
}




Sylvain Thery's avatar
Sylvain Thery committed
1048 1049

}
Sylvain Thery's avatar
Sylvain Thery committed
1050
}