textures.hpp 26.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
/*******************************************************************************
* 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
23
*******************************************************************************/
Sylvain Thery's avatar
Sylvain Thery committed
24 25 26

#include "Utils/cgognStream.h"

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),
39 40
m_sizeSub(0),
m_localAlloc(false)
Sylvain Thery's avatar
Sylvain Thery committed
41
{
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),
48
m_localAlloc(true)
Sylvain Thery's avatar
Sylvain Thery committed
49
{
Sylvain Thery's avatar
Sylvain Thery committed
50
	if (img.m_data_ptr != NULL)
51
	{
Sylvain Thery's avatar
Sylvain Thery committed
52 53
		m_data_ptr = new TYPE[m_sizeSub[DIM-1]];
		memcpy(m_data_ptr, img.m_data_ptr, sizeof(TYPE)*m_sizeSub[DIM-1]);
54 55 56
	}
}

Sylvain Thery's avatar
Sylvain Thery committed
57 58 59 60 61 62 63 64

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]];
65 66
}

Sylvain Thery's avatar
Sylvain Thery committed
67

68 69 70 71 72 73 74 75

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
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}


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;
93 94
}

Sylvain Thery's avatar
Sylvain Thery committed
95 96


97 98 99 100 101 102 103 104 105
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
106 107
}

108 109 110 111

template < unsigned int DIM, typename TYPE >
void ImageData<DIM,TYPE>::create(TYPE* data, const COORD& size)
{
Sylvain Thery's avatar
Sylvain Thery committed
112
	if (m_data_ptr!=NULL)
113 114 115 116 117 118 119 120 121 122 123
	{
		delete[] m_data_ptr;
	}

	m_size = size;
	computeSub();
	m_data_ptr = data;
}

template < unsigned int DIM, typename TYPE >
void ImageData<DIM,TYPE>::create(const COORD& size)
Sylvain Thery's avatar
Sylvain Thery committed
124 125
{
	// first free old image if exist
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
	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
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


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;
}
220 221 222 223 224 225 226 227 228 229


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
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

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++);
}


246 247 248
// implementation class Image

template < unsigned int DIM, typename TYPE >
249
Image<DIM,TYPE>::Image()
250
{
Sylvain Thery's avatar
Sylvain Thery committed
251 252 253 254
}

template < unsigned int DIM, typename TYPE >
Image<DIM,TYPE>::Image(const COORD& size):
255
ImageData<DIM,TYPE>(size)
Sylvain Thery's avatar
Sylvain Thery committed
256 257 258 259 260 261 262
{
}


template < unsigned int DIM, typename TYPE >
Image<DIM,TYPE>::~Image()
{
Sylvain Thery's avatar
Sylvain Thery committed
263
//	this->m_data_ptr = NULL;
264
}
Sylvain Thery's avatar
Sylvain Thery committed
265 266


267
template < unsigned int DIM, typename TYPE >
268
bool Image<DIM,TYPE>::load(const unsigned char *ptr, unsigned int w, unsigned int h, unsigned int bpp)
269 270 271
{
	CGoGN_STATIC_ASSERT(DIM==2, incompatible_Vector_constructor_dimension);
	
Sylvain Thery's avatar
Sylvain Thery committed
272
	// compatible TYPE
273
	if (bpp != sizeof(TYPE))
Sylvain Thery's avatar
Sylvain Thery committed
274
	{
275
		CGoGNout << "Image::load incompatible type: bbp=" << bpp << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
276
		CGoGNout << "sizeof(TYPE)="<<sizeof(TYPE)<< CGoGNendl;
277
		delete ptr;
Sylvain Thery's avatar
Sylvain Thery committed
278 279 280
		return false;
	}

281
	// destroy old data	
282
	 if (this->m_data_ptr != NULL)
283
		delete[] this->m_data_ptr;
Sylvain Thery's avatar
Sylvain Thery committed
284

Sylvain Thery's avatar
Sylvain Thery committed
285 286
	this->m_data_ptr = new TYPE[sizeof(TYPE)*w*h];
	memcpy(this->m_data_ptr, ptr, sizeof(TYPE)*w*h);
287 288
	this->m_size[0] = w;
	this->m_size[1] = h;
289

Sylvain Thery's avatar
Sylvain Thery committed
290 291
	this->computeSub();

292
	return true;
Sylvain Thery's avatar
Sylvain Thery committed
293 294 295
}


296

297
#ifndef CGOGN_WITH_QT
Sylvain Thery's avatar
Sylvain Thery committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325

template < unsigned int DIM, typename TYPE >
bool Image<DIM,TYPE>::load(const std::string& filename)
{
	CGoGN_STATIC_ASSERT(DIM==2, incompatible_image_dimension);
	CGoGN_STATIC_ASSERT(sizeof(TYPE)==3, incompatible_image_type_loading);

	ilInit();
	ilEnable(IL_ORIGIN_SET);
	ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
	ILuint imgName;
	ilGenImages(1,&imgName);
	ilBindImage(imgName);
	ilLoadImage(filename.c_str());

	ILuint bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);

	if (bpp != sizeof(TYPE))
	{
		CGoGNerr << "wrong image (" << filename << ") bpp"<< CGoGNendl;
		ilDeleteImage(imgName);
		return false;
	}

	this->m_size[0] = ilGetInteger(IL_IMAGE_WIDTH);
	this->m_size[1] = ilGetInteger(IL_IMAGE_HEIGHT);
	this->computeSub();

326
	this->m_data_ptr = new TYPE[this->m_size[0]*this->m_size[1]];
Sylvain Thery's avatar
Sylvain Thery committed
327 328 329 330 331
	unsigned char* ptr = reinterpret_cast<unsigned char*>(this->m_data_ptr);

	for (int i=this->m_size[1]-1; i>=0; --i)
	{
		ilCopyPixels(0, i, 0, this->m_size[0],1, 1, IL_RGB, IL_UNSIGNED_BYTE, ptr);
332
		ptr += 3*this->m_size[0];
Sylvain Thery's avatar
Sylvain Thery committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
	}
	ilDeleteImage(imgName);

	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);

	CGoGNerr << "Save image not implemented" << CGoGNendl;
}


#else
Sylvain Thery's avatar
Sylvain Thery committed
349
template < unsigned int DIM, typename TYPE >
350
bool Image<DIM,TYPE>::load(const std::string& filename)
Sylvain Thery's avatar
Sylvain Thery committed
351 352 353
{
	CGoGN_STATIC_ASSERT(DIM==2, incompatible_Vector_constructor_dimension);

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

Sylvain Thery's avatar
Sylvain Thery committed
356
	if (ptr->isNull())
Sylvain Thery's avatar
Sylvain Thery committed
357
	{
unknown's avatar
unknown committed
358
		CGoGNout << "Impossible to load "<< filename << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
359 360 361
		return false;
	}
	
362 363
	//	get the info of images
	unsigned int bpp = ptr->depth() / 8;
Sylvain Thery's avatar
Sylvain Thery committed
364 365

	// compatible TYPE
366
/*	if (bpp < sizeof(TYPE))
Sylvain Thery's avatar
Sylvain Thery committed
367
	{
Sylvain Thery's avatar
Sylvain Thery committed
368 369 370
		CGoGNerr << "Image::load incompatible type: bbp=" << bpp << CGoGNendl;

		CGoGNerr << "sizeof(TYPE)="<<sizeof(TYPE)<< CGoGNendl;
371
		delete ptr;
Sylvain Thery's avatar
Sylvain Thery committed
372 373
		return false;
	}
374
	*/
Sylvain Thery's avatar
Sylvain Thery committed
375

376
	// destroy old data	
377
	 if (this->m_data_ptr != NULL)
378 379 380 381 382 383
		delete[] this->m_data_ptr;

	if (bpp > sizeof(TYPE))
	{
		if (sizeof(TYPE) == 3)
		{
384
			QImage img = ptr->convertToFormat(QImage::Format_RGB888,Qt::NoOpaqueDetection);
unknown's avatar
unknown committed
385
			this->m_data_ptr = new TYPE[3*img.width()*img.height()];
386 387
			//copy per line for alignment
			TYPE* ptrOut = this->m_data_ptr;
Sylvain Thery's avatar
Sylvain Thery committed
388
			for(int i=img.height()-1; i>=0; --i)
389
			{
390
				memcpy(ptrOut, img.scanLine(i), 3*img.width());
391 392
				ptrOut += img.width(); // not *3 because type is size of 3
			}
393 394 395 396
		}
		else if (sizeof(TYPE) == 1)
		{
			QImage img = ptr->convertToFormat(QImage::Format_Indexed8);
unknown's avatar
unknown committed
397
			this->m_data_ptr = new TYPE[img.width()*img.height()];
398 399
			//copy per line for alignment
			TYPE* ptrOut = this->m_data_ptr;
Sylvain Thery's avatar
Sylvain Thery committed
400
			for(int i=img.height()-1; i>=0; --i)
401
			{
402
				memcpy(ptrOut, img.scanLine(i), img.width());
403 404
				ptrOut += img.width();
			}
405
		}
406 407 408
	}
	else
	{
unknown's avatar
unknown committed
409
		this->m_data_ptr = new TYPE[ptr->width()*ptr->height()];
410 411
		TYPE* ptrOut = this->m_data_ptr;
		//copy per line for alignment
Sylvain Thery's avatar
Sylvain Thery committed
412
		for(int i=ptr->height()-1; i>=0; --i)
413
		{
414
			memcpy(ptrOut, ptr->scanLine(i), ptr->width()*bpp);
415 416
			ptrOut += ptr->width();
		}
417
	}
Sylvain Thery's avatar
Sylvain Thery committed
418

419 420
	this->m_size[0] = ptr->width();
	this->m_size[1] = ptr->height();
Sylvain Thery's avatar
Sylvain Thery committed
421 422
	this->computeSub();

423
	delete ptr;
Sylvain Thery's avatar
Sylvain Thery committed
424 425 426 427 428 429 430 431 432
	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
433
	QImage* ptrIm = NULL;
434
	if (this->m_data_ptr != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
435
	{
436 437 438
		switch(sizeof(TYPE))
		{
		case 1:
unknown's avatar
unknown committed
439
			ptrIm = new QImage((uchar *)(this->m_data_ptr), this->m_size[0],this->m_size[1], QImage::Format_Indexed8);
440 441
			break;
		case 3:
unknown's avatar
unknown committed
442
			ptrIm = new QImage((uchar *)(this->m_data_ptr), this->m_size[0],this->m_size[1], QImage::Format_RGB888);
443 444
			break;
		case 4:
unknown's avatar
unknown committed
445
			ptrIm = new QImage((uchar *)(this->m_data_ptr), this->m_size[0],this->m_size[1], QImage::Format_ARGB32);
446 447 448 449
			break;
		default:
			break;
		}
Sylvain Thery's avatar
Sylvain Thery committed
450
	}
Sylvain Thery's avatar
Sylvain Thery committed
451 452 453 454 455
	if (ptrIm != NULL)
	{
		ptrIm->save(QString(filename.c_str()));
		delete ptrIm;
	}
456
}
457
#endif
Sylvain Thery's avatar
Sylvain Thery committed
458 459


460 461
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
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
{
	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;
	}

497 498
	return newImg;
}
Sylvain Thery's avatar
Sylvain Thery committed
499 500 501 502 503

template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::crop(const COORD& origin, const COORD& sz)
{
	Image<DIM,TYPE>* newImg = subImage(origin,sz);
Sylvain Thery's avatar
Sylvain Thery committed
504
	this->swap(*newImg);
Sylvain Thery's avatar
Sylvain Thery committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
	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)
			{
unknown's avatar
unknown committed
526
				newImg->texel(i) = this->texel( (unsigned int)(p0+0.5) );
Sylvain Thery's avatar
Sylvain Thery committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540
				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)
				{
unknown's avatar
unknown committed
541
					newImg->texel(i,j) = this->texel( (unsigned int)(p0+0.5), (unsigned int)(p1+0.5) );
Sylvain Thery's avatar
Sylvain Thery committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
					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)
					{
unknown's avatar
unknown committed
562
						newImg->texel(i,j,k) = this->texel( (unsigned int)(p0+0.5), (unsigned int)(p1+0.5), (unsigned int)(p2+0.5) );
Sylvain Thery's avatar
Sylvain Thery committed
563 564 565 566 567 568 569 570 571 572 573
						p0 += inc0;
					}
					p1 += inc1;
				}
				p2 += inc2;
			}
		}
		break;
	}

	return newImg;
574
}
Sylvain Thery's avatar
Sylvain Thery committed
575 576 577 578 579 580



template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::scaleNearest(const COORD& newSize)
{
Sylvain Thery's avatar
Sylvain Thery committed
581 582 583 584 585 586
	if (newSize != this->m_size)
	{
		Image<DIM,TYPE>* newImg = scaleNearestToNewImage(newSize);
		this->swap(*newImg);
		delete newImg;
	}
Sylvain Thery's avatar
Sylvain Thery committed
587 588 589
}


Sylvain Thery's avatar
Sylvain Thery committed
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 618 619
template < unsigned int DIM, typename TYPE >
typename Image<DIM,TYPE>::COORD Image<DIM,TYPE>::newMaxSize(unsigned int maxSize)
{
	unsigned int currentMax=0;
	double sf = 1.0;

	for (unsigned int d=0; d<DIM; ++d)
	{
		if (this->m_size[d] > maxSize)
		{
			currentMax = this->m_size[d];
			sf = double(maxSize) / double(currentMax);
		}
	}

	COORD nc;
	// ensure width of image (texture) is numultiple of 4
	nc[0] = (unsigned int)(this->m_size[0]*sf);
	if (nc[0]%4)
	{
		nc[0] = (nc[0]/4)*4;
		sf = double(nc[0])/double(this->m_size[0]);
	}

	for (unsigned int d=1; d<DIM; ++d)
		nc[d] = (unsigned int)(this->m_size[d]*sf);

	return nc;
}

Sylvain Thery's avatar
Sylvain Thery committed
620 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
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>();
670
	this->swap(*newImg);
Sylvain Thery's avatar
Sylvain Thery committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
	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;
}

700 701 702

template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::flip(unsigned int axis)
Sylvain Thery's avatar
Sylvain Thery committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 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
{
	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;
	}

759
}
Sylvain Thery's avatar
Sylvain Thery committed
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


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;
797 798 799 800
}

template < unsigned int DIM, typename TYPE >
void Image<DIM,TYPE>::rotate90(int axis)
Sylvain Thery's avatar
Sylvain Thery committed
801 802
{
	Image<DIM,TYPE>* newImg = rotate90ToNewImage(axis);
803
	this->swap(*newImg);
804 805
	delete newImg;
}
Sylvain Thery's avatar
Sylvain Thery committed
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


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:
		{
946
//			double tot=0.0;
Sylvain Thery's avatar
Sylvain Thery committed
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
			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;
	}

998
	glGenTextures(1, &(*m_id));
Sylvain Thery's avatar
Sylvain Thery committed
999

1000
	glBindTexture(m_target, *m_id);
Sylvain Thery's avatar
Sylvain Thery committed
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
	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()
{
1046
	glBindTexture(m_target, *m_id);
Sylvain Thery's avatar
Sylvain Thery committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
}


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);
1060

Sylvain Thery's avatar
Sylvain Thery committed
1061 1062 1063 1064 1065 1066
}


template < unsigned int DIM, typename TYPE >
void Texture<DIM,TYPE>::update()
{
1067
	glBindTexture(m_target, *m_id);
Sylvain Thery's avatar
Sylvain Thery committed
1068
//	checkAlignment();
Sylvain Thery's avatar
Sylvain Thery committed
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
	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)
{
1089
	glBindTexture(m_target, *m_id);
Sylvain Thery's avatar
Sylvain Thery committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
	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)
{
1112
	glBindTexture(m_target, *m_id);
Sylvain Thery's avatar
Sylvain Thery committed
1113 1114 1115 1116 1117 1118 1119 1120 1121
	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)
{
1122
	glBindTexture(m_target, *m_id);
Sylvain Thery's avatar
Sylvain Thery committed
1123 1124 1125 1126 1127 1128 1129 1130
	glTexParameteri(m_target, GL_TEXTURE_WRAP_S, param);
	glTexParameteri(m_target, GL_TEXTURE_WRAP_T, param);
	glTexParameteri(m_target, GL_TEXTURE_WRAP_R, param);
}




1131 1132

}
Sylvain Thery's avatar
Sylvain Thery committed
1133
}