img3D_IO.cpp 10.3 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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     *
* aint 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

#include "Utils/img3D_IO.h"
28
#include "Utils/cgognStream.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
29

unknown's avatar
unknown committed
30 31 32 33
#ifdef WITH_QT
#include <QImage>
#include <QString>
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
34 35 36 37 38 39 40

namespace CGoGN
{

namespace Utils
{

Sylvain Thery's avatar
Sylvain Thery committed
41
#ifdef WITH_QT
Pierre Kraemer's avatar
Pierre Kraemer committed
42 43 44 45 46 47 48 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
namespace Img3D_IO
{

template<typename DataType>
DataType* compressZ8(DataType* src, int w, int h, int d, int& new_d)
{
	new_d = (d/8);
	int wh=w*h;
	if (d%8) new_d++;

	DataType* newImg = new DataType[(w*h*new_d)+w]; // +w pour stocker entete
	DataType *dest = newImg;

	int z = 0;
	while (z < d)
	{
		DataType* ptrs[8];
		ptrs[0] = src;
		z++;
		for (int i =1; i<8; ++i)
		{
			if (z<d)
				ptrs[i] = ptrs[i-1] + wh;
			else ptrs[i] = NULL;
			z++;
		}

		for (int i=0;i<wh;++i)
		{
			DataType val=0;
			for(int j=7; j>=0; --j)
			{
				val *= 2;
				if (ptrs[j] != NULL)
				{
					if (*((ptrs[j])++) != 0)
					{
						val++;
					}
				}
			}
			*dest++ = val;
		}
		src += 8*wh;
	}

	return newImg;
}


template<typename DataType>
DataType* uncompressZ8(DataType* src, int w, int h, int d)
{
	int wh = w*h;
	
	DataType* newImg = new DataType[wh*d];
	DataType *dest = newImg;

	int z = 0;
	while (z < d)
	{
		DataType* ptrs[8];
		ptrs[0] = dest;
		z++;
		for (int i =1; i<8; ++i)
		{
			if (z<d)
				ptrs[i] = ptrs[i-1] + wh;
			else ptrs[i] = NULL;
			z++;
		}

		for (int i=0;i<wh;++i)
		{
			DataType val=*src++;

			for(int j=0; j<8; ++j)
			{
				if (ptrs[j] != NULL)
				{
					if (val%2)
					{
						*((ptrs[j])++) =  255;
					}	
					else
					{
						*((ptrs[j])++) =  0;
					}
				}
				val /= 2;
			}
		}
		dest += 8*wh;
	}

	return newImg;

}



Sylvain Thery's avatar
Sylvain Thery committed
143
void saveBool(const std::string& filename, unsigned char* data, int w, int h, int d, float vx, float vy, float vz, int tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
{
	// compress image from bool to 8bit pp
	int nd;
	unsigned char *dat_comp = compressZ8<unsigned char>(data,w,h,d,nd);
	// add 3D info
	int *entete1=reinterpret_cast<int*>(dat_comp+w*h*nd);
	*entete1++ = BOOL8; // type 1: bool 255 compressee
	*entete1++ = tag;
	*entete1++ = w;
	*entete1++ = h;
	*entete1++ = d;
	float *entete2=reinterpret_cast<float*>(entete1);
	*entete2++ = vx;
	*entete2++ = vy;
	*entete2++ = vz;

	// save image2D

Sylvain Thery's avatar
Sylvain Thery committed
162
	int hh = (h*nd)+1;
unknown's avatar
unknown committed
163 164
	QImage img(dat_comp, w, hh, QImage::Format_Indexed8);
	img.save(QString(filename.c_str()));
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166 167
}


Sylvain Thery's avatar
Sylvain Thery committed
168
unsigned char* loadVal_8(const std::string& filename, int& w, int& h, int &d, float& vx, float& vy, float& vz, int& tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
169
{
Sylvain Thery's avatar
Sylvain Thery committed
170 171 172
	QImage* ptrImg = new QImage(filename.c_str());
	if (ptrImg==NULL)
		return NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
173 174
	
//	get the info of 2D image
Sylvain Thery's avatar
Sylvain Thery committed
175 176 177
	w = ptrImg->width();
	h = ptrImg->height();
	unsigned int bpp = ptrImg->depth();
Sylvain Thery's avatar
Sylvain Thery committed
178

Pierre Kraemer's avatar
Pierre Kraemer committed
179
	// image OK ?
Sylvain Thery's avatar
Sylvain Thery committed
180
	if ((bpp!=8))
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182
		return NULL;

unknown's avatar
unknown committed
183
	unsigned char* ptr=ptrImg->bits();
Pierre Kraemer's avatar
Pierre Kraemer committed
184 185
	int* ptr_int = reinterpret_cast<int*>(ptr+(w*(h-1)));
	int t = *ptr_int++;
Sylvain Thery's avatar
Sylvain Thery committed
186
	
Pierre Kraemer's avatar
Pierre Kraemer committed
187 188
	if ((t!= BOOL8) && (t!= VAL8))
		return NULL;
Sylvain Thery's avatar
Sylvain Thery committed
189
		
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193 194 195 196 197 198 199 200 201 202 203
	tag = *ptr_int++;
	w = *ptr_int++;
	h = *ptr_int++;
	d = *ptr_int++;
	float* ptr_float = reinterpret_cast<float*>(ptr_int);
	vx = *ptr_float++;
	vy = *ptr_float++;
	vz = *ptr_float++;

	if (t== BOOL8)
	{
		// uncompress data
		unsigned char* data = uncompressZ8<unsigned char>(ptr,w,h,d);
	
unknown's avatar
unknown committed
204
		delete ptrImg;
Pierre Kraemer's avatar
Pierre Kraemer committed
205 206 207
		// return 3D image
		return data;
	}
Sylvain Thery's avatar
Sylvain Thery committed
208 209 210
	
	unsigned char* ptr2 = new unsigned char[w*h*d];
	memcpy (ptr2, ptr, w*h*d); 
unknown's avatar
unknown committed
211
	delete ptrImg;
Sylvain Thery's avatar
Sylvain Thery committed
212
	return ptr2;
Pierre Kraemer's avatar
Pierre Kraemer committed
213 214 215 216 217 218 219
}






Sylvain Thery's avatar
Sylvain Thery committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
void saveVal(const std::string& filename, unsigned char* data, int w, int h, int d, float vx, float vy, float vz, int tag)
{
	// init image2D
	QImage img(w,(h*d)+1,QImage::Format_Indexed8);
	memcpy(img.bits(), data, w*h*d);
	
	// add 3D info
	int *entete1=reinterpret_cast<int*>(img.bits()+w*h*d); // get the end of 3D data
	*entete1++ = VAL8; 
	*entete1++ = tag;
	*entete1++ = w;
	*entete1++ = h;
	*entete1++ = d;
	float *entete2=reinterpret_cast<float*>(entete1);
	*entete2++ = vx;
	*entete2++ = vy;
	*entete2++ = vz;
Pierre Kraemer's avatar
Pierre Kraemer committed
237

Sylvain Thery's avatar
Sylvain Thery committed
238
	// save image
unknown's avatar
unknown committed
239
	img.save(QString(filename.c_str()));
Sylvain Thery's avatar
Sylvain Thery committed
240
}
Pierre Kraemer's avatar
Pierre Kraemer committed
241 242 243



Sylvain Thery's avatar
Sylvain Thery committed
244
unsigned char* loadRGB(const std::string& filename, int& w, int& h, int &d, float& vx, float& vy, float& vz, int& tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
245
{
Sylvain Thery's avatar
Sylvain Thery committed
246 247 248
	QImage* ptrImg = new QImage(filename.c_str());
	if (ptrImg==NULL)
		return NULL;
Pierre Kraemer's avatar
Pierre Kraemer committed
249 250
	
//	get the info of 2D image
Sylvain Thery's avatar
Sylvain Thery committed
251 252 253
	w = ptrImg->width();
	h = ptrImg->height();
	unsigned int bpp = ptrImg->depth();
Pierre Kraemer's avatar
Pierre Kraemer committed
254 255

	// image OK ?
Sylvain Thery's avatar
Sylvain Thery committed
256
	if ((bpp!=24))
Pierre Kraemer's avatar
Pierre Kraemer committed
257 258
		return NULL;

unknown's avatar
unknown committed
259
	unsigned char* ptr=ptrImg->bits();
Pierre Kraemer's avatar
Pierre Kraemer committed
260 261 262 263 264 265 266 267 268 269 270 271 272
	int* ptr_int = reinterpret_cast<int*>(ptr+3*(w*(h-1)));
	int t = *ptr_int++;
	if (t!= RGB8)
		return NULL;
	tag = *ptr_int++;
	w = *ptr_int++;
	h = *ptr_int++;
	d = *ptr_int++;
	float* ptr_float = reinterpret_cast<float*>(ptr_int);
	vx = *ptr_float++;
	vy = *ptr_float++;
	vz = *ptr_float++;

Sylvain Thery's avatar
Sylvain Thery committed
273 274
	unsigned char* ptr2 = new unsigned char[3*w*h*d];
	memcpy (ptr2, ptr, 3*w*h*d); 
unknown's avatar
unknown committed
275
	delete ptrImg;
Sylvain Thery's avatar
Sylvain Thery committed
276
	return ptr2;
Pierre Kraemer's avatar
Pierre Kraemer committed
277 278 279
}


Sylvain Thery's avatar
Sylvain Thery committed
280
void saveRGB(const std::string& filename, unsigned char* data, int w, int h, int d, float vx, float vy, float vz, int tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
281
{
Sylvain Thery's avatar
Sylvain Thery committed
282

Pierre Kraemer's avatar
Pierre Kraemer committed
283
	// init image2D
Sylvain Thery's avatar
Sylvain Thery committed
284 285 286
	QImage img(w,(h*d)+1,QImage::Format_RGB888);
	memcpy(img.bits(), data, 3*w*h*d);

Pierre Kraemer's avatar
Pierre Kraemer committed
287 288
	// add 3D info
	
Sylvain Thery's avatar
Sylvain Thery committed
289
	int *entete1=reinterpret_cast<int*>(img.bits()+3*w*h*d); // get the end of 3D data
Pierre Kraemer's avatar
Pierre Kraemer committed
290 291 292 293 294 295 296 297 298 299
	*entete1++ = RGB8;
	*entete1++ = tag;
	*entete1++ = w;
	*entete1++ = h;
	*entete1++ = d;
	float *entete2=reinterpret_cast<float*>(entete1);
	*entete2++ = vx;
	*entete2++ = vy;
	*entete2++ = vz;

Sylvain Thery's avatar
Sylvain Thery committed
300

Pierre Kraemer's avatar
Pierre Kraemer committed
301
	// save image
unknown's avatar
unknown committed
302
	img.save(QString(filename.c_str()));
Pierre Kraemer's avatar
Pierre Kraemer committed
303 304
}

Sylvain Thery's avatar
Sylvain Thery committed
305 306 307
/*
TODO continue porting to Qt ?
unsigned short* loadVal_16(const std::string& filename, int& w, int& h, int &d, float& vx, float& vy, float& vz, int& tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
308 309 310 311 312 313 314 315 316 317 318 319 320
{
	ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
	ilEnable(IL_ORIGIN_SET);
	ILuint imgName;
	ilGenImages(1,&imgName);
	ilBindImage(imgName);
	ilLoadImage(filename);
	
//	get the info of 2D image
	w = ilGetInteger(IL_IMAGE_WIDTH);
	h = ilGetInteger(IL_IMAGE_HEIGHT);
	ILuint bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);

321
	CGoGNout << "BPP:"<<bpp<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
322 323 324 325 326
	// image OK ?
	if (bpp != 2)
		return NULL;

	unsigned short* ptr= reinterpret_cast<unsigned short*>(ilGetData());
327
//	CGoGNout << "PTR:"<<long(ptr)<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
328 329 330 331 332 333 334 335 336 337 338 339 340
	int* ptr_int = reinterpret_cast<int*>(ptr+(w*(h-1)));
	int t = *ptr_int++;
	if (t!= VAL16)
		return NULL;
	tag = *ptr_int++;
	w = *ptr_int++;
	h = *ptr_int++;
	d = *ptr_int++;
	float* ptr_float = reinterpret_cast<float*>(ptr_int);
	vx = *ptr_float++;
	vy = *ptr_float++;
	vz = *ptr_float++;

341
//	CGoGNout << "PTR:"<<long(ptr)<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
342 343 344 345 346 347
	return ptr;

}



Sylvain Thery's avatar
Sylvain Thery committed
348
void saveVal_16(const std::string& filename, unsigned short* data, int w, int h, int d, float vx, float vy, float vz, int tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
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
{
	// init image2D
	ILuint imgName;
	ilGenImages(1,&imgName);
	ilBindImage(imgName);
	ilTexImage(w,(h*d)+1,1,1,IL_LUMINANCE, IL_UNSIGNED_SHORT, NULL);
	// copy 3D image in 2D image
	ilSetPixels(0,0,0,w,h*d,1,IL_LUMINANCE, IL_UNSIGNED_SHORT, data);

	// add 3D info
	int *entete1=reinterpret_cast<int*>(ilGetData()+2*w*h*d); // get the end of 3D data
	*entete1++ = VAL16; 
	*entete1++ = tag;
	*entete1++ = w;
	*entete1++ = h;
	*entete1++ = d;
	float *entete2=reinterpret_cast<float*>(entete1);
	*entete2++ = vx;
	*entete2++ = vy;
	*entete2++ = vz;

	// save image
	ilEnable(IL_FILE_OVERWRITE);
	ilSaveImage(filename);
	ilDeleteImages(1,&imgName);
}


Sylvain Thery's avatar
Sylvain Thery committed
377
float* loadVal_float(const std::string& filename, int& w, int& h, int &d, float& vx, float& vy, float& vz, int& tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
{
	ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
	ilEnable(IL_ORIGIN_SET);
	ILuint imgName;
	ilGenImages(1,&imgName);
	ilBindImage(imgName);
	ilLoadImage(filename);
	
//	get the info of 2D image
	w = ilGetInteger(IL_IMAGE_WIDTH);
	h = ilGetInteger(IL_IMAGE_HEIGHT);
	ILuint bpp = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL);

	// image OK ?
	if (bpp != 4)
		return NULL;

	float* ptr = reinterpret_cast<float*>(ilGetData());
	int* ptr_int = reinterpret_cast<int*>(ptr+(w*(h-1)));
	int t = *ptr_int++;
	if (t!= VALFLOAT)
		return NULL;
	tag = *ptr_int++;
	w = *ptr_int++;
	h = *ptr_int++;
	d = *ptr_int++;

	float* ptr_float = reinterpret_cast<float*>(ptr_int);
	vx = *ptr_float++;
	vy = *ptr_float++;
	vz = *ptr_float++;

	return ptr;

}


Sylvain Thery's avatar
Sylvain Thery committed
415
void saveVal_float(const std::string& filename, float* data, int w, int h, int d, float vx, float vy, float vz, int tag)
Pierre Kraemer's avatar
Pierre Kraemer committed
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
{
	// init image2D
	ILuint imgName;
	ilGenImages(1,&imgName);
	ilBindImage(imgName);
	ilTexImage(w,(h*d)+1,1,4,IL_RGBA, IL_UNSIGNED_BYTE, NULL);
	// copy 3D image in 2D image
	ilSetPixels(0,0,0,w,h*d,1,IL_RGBA, IL_UNSIGNED_BYTE, reinterpret_cast<float*>(data));

	// add 3D info
	int *entete1=reinterpret_cast<int*>(ilGetData()+4*w*h*d); // get the end of 3D data
	*entete1++ = VALFLOAT; 
	*entete1++ = tag;
	*entete1++ = w;
	*entete1++ = h;
	*entete1++ = d;
	float *entete2=reinterpret_cast<float*>(entete1);
	*entete2++ = vx;
	*entete2++ = vy;
	*entete2++ = vz;

	// save image
	ilEnable(IL_FILE_OVERWRITE);
	ilSaveImage(filename);
	ilDeleteImages(1,&imgName);
}
Sylvain Thery's avatar
Sylvain Thery committed
442
*/
Sylvain Thery's avatar
Sylvain Thery committed
443

Pierre Kraemer's avatar
Pierre Kraemer committed
444 445

} //namespace img3D_IO
Sylvain Thery's avatar
Sylvain Thery committed
446
#endif
Pierre Kraemer's avatar
Pierre Kraemer committed
447 448 449 450 451

} //namespace Utils

} //namespace CGoGN