colourConverter.hpp 9.25 KB
Newer Older
Kenneth Vanhoey's avatar
Kenneth Vanhoey 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           *
Kenneth Vanhoey's avatar
Kenneth Vanhoey 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     *
* 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.u-strasbg.fr/                                         *
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
21
22
23
24
25
26
27
28
29
30
31
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN {
namespace Utils {

template<typename REAL>
ColourConverter<REAL>::ColourConverter(VEC3 col, enum ColourEncoding enc) :
	RGB(NULL),
	Luv(NULL),
32
	Lab(NULL),
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
33
34
35
36
37
38
	XYZ(NULL)
{
	originalEnc = enc ;

	switch(originalEnc) {
		case(C_RGB):
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
39
40
			if (!(-0.001 < col[0] && col[0] < 1.001 && -0.001 < col[1] && col[1] < 1.001 &&  -0.001 < col[2] && col[2] < 1.001))
				CGoGNerr << "Warning : an unvalid RGB color was entered in ColourConverter constructor" << CGoGNendl ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
41
42
43
44
			RGB = new VEC3(col) ;
			break ;

		case (C_Luv) :
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
45
46
			if (!(-0.001 < col[0] && col[0] < 100.001 && -83.001 < col[1] && col[1] < 175.001 &&  -134.001 < col[2] && col[2] < 108.001))
				CGoGNerr << "Warning : an unvalid Luv color was entered in ColourConverter constructor" << CGoGNendl ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
47
48
49
50
			Luv = new VEC3(col) ;
			break ;

		case (C_XYZ) :
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
51
52
			if (!(-0.001 < col[0] && col[0] < 1.001 && -0.001 < col[1] && col[1] < 1.001 &&  -0.001 < col[2] && col[2] < 1.001))
				CGoGNerr << "Warning : an unvalid XYZ color was entered in ColourConverter constructor" << CGoGNendl ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
53
54
			XYZ = new VEC3(col) ;
			break ;
55
56

		case (C_Lab) :
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
57
58
			if (!(-0.001 < col[0] && col[0] < 100.001 && -86.001 < col[1] && col[1] < 98.001 && -108.001 < col[2] && col[2] < 95.001))
				CGoGNerr << "Warning : an unvalid Lab color was entered in ColourConverter constructor" << CGoGNendl ;
59
60
			Lab = new VEC3(col) ;
			break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
61
62
63
	}
}

64
65
66
67
68
69
70
71
72
template<typename REAL>
ColourConverter<REAL>::~ColourConverter()
{
	delete RGB ;
	delete Luv ;
	delete XYZ ;
	delete Lab ;
}

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
template<typename REAL>
Geom::Vector<3,REAL> ColourConverter<REAL>::getColour(enum ColourEncoding enc) {
	switch (enc) {
	case (C_RGB) :
		return getRGB() ;
		break ;

	case (C_XYZ) :
		return getXYZ() ;
		break ;

	case (C_Luv) :
		return getLuv() ;
		break ;

	case (C_Lab) :
		return getLab() ;
		break ;

	default :
		assert(!"Should never arrive here : ColourConverter::getColour default case") ;
		return getOriginal() ;
	}
}

template<typename REAL>
Geom::Vector<3,REAL> ColourConverter<REAL>::getOriginal() {
	return getColour(this->originalEnc) ;
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
template<typename REAL>
Geom::Vector<3,REAL> ColourConverter<REAL>::getRGB() {
	if (RGB == NULL)
		convert(originalEnc,C_RGB) ;

	return *RGB ;
}

template<typename REAL>
Geom::Vector<3,REAL> ColourConverter<REAL>::getLuv() {
	if (Luv == NULL)
		convert(originalEnc,C_Luv) ;

	return *Luv ;
}

119
120
template<typename REAL>
Geom::Vector<3,REAL> ColourConverter<REAL>::getLab() {
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
121
	if (Lab == NULL)
122
123
		convert(originalEnc,C_Lab) ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
124
	return *Lab ;
125
126
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
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
template<typename REAL>
Geom::Vector<3,REAL> ColourConverter<REAL>::getXYZ() {
	if (XYZ == NULL) {
		convert(originalEnc,C_XYZ) ;
	}

	return *XYZ ;
}

template<typename REAL>
void ColourConverter<REAL>::convertRGBtoXYZ() {
	Geom::Matrix<3,3,REAL> M ;

	M(0,0) = 0.412453 ;
	M(0,1) = 0.357580 ;
	M(0,2) = 0.180423 ;

	M(1,0) = 0.212671 ;
	M(1,1) = 0.715160 ;
	M(1,2) = 0.072169 ;

	M(2,0) = 0.019334 ;
	M(2,1) = 0.119193 ;
	M(2,2) = 0.950227 ;

	VEC3 c = M * (*RGB) ;

	XYZ = new VEC3(c) ;
}

template<typename REAL>
void ColourConverter<REAL>::convertXYZtoRGB() {
	Geom::Matrix<3,3,REAL> M ;

	M(0,0) = 3.240479 ;
	M(0,1) = -1.537150 ;
	M(0,2) = -0.498535 ;

	M(1,0) = -0.969256 ;
	M(1,1) = 1.875992 ;
	M(1,2) = 0.041556 ;

	M(2,0) = 0.055648 ;
	M(2,1) = -0.204043 ;
	M(2,2) = 1.057311 ;

	VEC3 c = M * (*XYZ) ;

	RGB = new VEC3(c) ;
}

template<typename REAL>
void ColourConverter<REAL>::convertXYZtoLuv() {
	REAL L,u,v ;

	REAL &X = (*XYZ)[0] ;
	REAL &Y = (*XYZ)[1] ;
	REAL &Z = (*XYZ)[2] ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
186
187
	REAL Ydiv = Y/Yn ;
	if (Ydiv > 0.008856)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
188
		L = 116.0 * pow(Ydiv,1.0/3.0) - 16.0 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
189
	else // near black
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
190
		L = 903.3 * Ydiv ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
191
192
193

	REAL den = X + 15.0 * Y + 3 * Z ;
	REAL u1 = (4.0 * X) / den ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
194
195
196
	REAL v1 = (9.0 * Y) / den ;
	u = 13*L * (u1 - un) ;
	v = 13*L * (v1 - vn) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

	Luv = new VEC3(L,u,v) ;
}

template<typename REAL>
void ColourConverter<REAL>::convertLuvToXYZ() {
	REAL X,Y,Z ;

	REAL &L = (*Luv)[0] ;
	REAL &u = (*Luv)[1] ;
	REAL &v = (*Luv)[2] ;

	if (L > 8.0)
		Y = pow(((L+16.0) / 116.0),3) ;
	else // near black
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
212
		Y = Yn * L / 903.3 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
213
214

	REAL den = 13.0 * L ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
215
216
	REAL u1 = u/den + un ;
	REAL v1 = v/den + vn ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
217
	den = 4.0*v1 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
218
	X = Y * 9.0 * u1 / den ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
219
	Z = Y * (12.0 - 3.0*u1 - 20.0*v1) / den ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
220
221
222
223

	XYZ = new VEC3(X,Y,Z) ;
}

224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
template<typename REAL>
void ColourConverter<REAL>::convertXYZtoLab() {
	REAL L,a,b ;

	REAL &X = (*XYZ)[0] ;
	REAL &Y = (*XYZ)[1] ;
	REAL &Z = (*XYZ)[2] ;

	struct Local {
		static REAL f(REAL x) {
			if (x > 0.008856)
			return pow(x,1.0/3.0) ;
			else
				return 7.787 * x + 16.0/116.0 ;
		}
	} ;

	if (Y > 0.008856)
		L = 116.0f * pow(Y,1.0f/3.0) - 16 ;
	else // near black
		L = 903.3 * Y ;

	a = 500.0 * (Local::f(X/Xn) - Local::f(Y/Yn)) ;
	b = 200.0 * (Local::f(Y/Yn) - Local::f(Z/Zn)) ;

	Lab = new VEC3(L,a,b) ;
}

template<typename REAL>
void ColourConverter<REAL>::convertLabToXYZ() {
	REAL X,Y,Z ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
256
257
258
	REAL &L = (*Lab)[0] ;
	REAL &a = (*Lab)[1] ;
	REAL &b = (*Lab)[2] ;
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

	struct Local {
		static REAL f(REAL x) {
			if (x > 0.206893)
			return pow(x,3.0) ;
			else
				return x / 7.787 - 16.0/903.3 ;
		}
	} ;

	if (L > 8.0)
		Y = pow(((L+16.0) / 116.0),3) ;
	else // near black
		Y = L / 903.3 ;

	REAL nom = (L+16.0) / 116.0 ;
	X = Xn * Local::f( nom +  a/500.0) ;
	Z = Zn * Local::f( nom -  b/200.0) ;

	XYZ = new VEC3(X,Y,Z) ;
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
281
282
template<typename REAL>
bool ColourConverter<REAL>::convert(enum ColourEncoding from, enum ColourEncoding to) {
283
	if (to == from) {
284
		CGoGNerr << "ColourConverter::convert(from,to) : conversion into same colour space" << CGoGNendl ;
285
286
287
		return false ;
	}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
288
289
290
291
292
293
294
295
296
297
298
299
300
	switch(from) {
		case(C_RGB) :
			switch (to) {
				case (C_XYZ) :
					if (XYZ == NULL)
						convertRGBtoXYZ() ;
					break ;
				case (C_Luv) :
					if (XYZ == NULL)
						convertRGBtoXYZ() ;
					if (Luv == NULL)
						convertXYZtoLuv() ;
					break ;
301
302
303
304
305
306
				case(C_Lab) :
					if (XYZ == NULL)
						convertRGBtoXYZ() ;
					if (Lab == NULL)
						convertXYZtoLab() ;
					break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
307
				default :
308
					CGoGNerr << "Colour conversion not supported" << CGoGNendl ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
					return false ;
			}
			break ;

		case(C_Luv) :
			switch(to) {
				case(C_RGB) : {
					if (RGB == NULL) {
						if (XYZ == NULL)
							convertLuvToXYZ() ;
						convertXYZtoRGB() ;
					}
					break ;
				}
				case(C_XYZ) : {
					if (XYZ == NULL)
						convertLuvToXYZ() ;
					break ;
				}
328
329
330
331
332
333
334
				case(C_Lab) :
					if (Lab == NULL) {
						if (XYZ == NULL)
							convertLuvToXYZ() ;
						convertXYZtoLab() ;
					}
					break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
335
				default :
336
					CGoGNerr << "Colour conversion not supported" << CGoGNendl ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
337
338
339
340
341
342
343
344
345
346
347
348
349
350
					return false ;
			}
			break ;

		case(C_XYZ) :
			switch (to) {
				case(C_RGB) :
					if (RGB==NULL)
						convertXYZtoRGB() ;
					break ;
				case(C_Luv) :
					if (Luv == NULL)
						convertXYZtoLuv() ;
					break ;
351
352
353
354
355
				case(C_Lab) :
					if (Lab == NULL)
						convertXYZtoLab() ;
					break ;
				default :
356
					CGoGNerr << "Colour conversion not supported" << CGoGNendl ;
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
					return false ;
			}
			break ;

		case(C_Lab) :
			switch (to) {
				case(C_RGB) : {
					if (RGB == NULL) {
						if (XYZ == NULL)
							convertLabToXYZ() ;
						convertXYZtoRGB() ;
					}
					break ;
				}
				case(C_XYZ) : {
					if (XYZ == NULL)
						convertLabToXYZ() ;
					break ;
				}
				case(C_Luv) :
					if (Luv == NULL) {
						if (XYZ == NULL)
							convertLabToXYZ() ;
						convertXYZtoLuv() ;
					}
					break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
383
				default :
384
					CGoGNerr << "Colour conversion not supported" << CGoGNendl ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
385
386
387
					return false ;
			}
			break ;
388

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
389
		default :
390
			CGoGNerr << "Colour conversion not supported" << CGoGNendl ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
391
392
393
394
395
396
397
398
399
400
401
402
			return false ;
	}

	return true ;
}



} // namespace Utils
} // namespace CGoGN