colourConverter.hpp 8.26 KB
Newer Older
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

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 41
			assert (-0.001 < col[0] && col[0] < 1.001) ;
			assert (-0.001 < col[1] && col[1] < 1.001) ;
			assert (-0.001 < col[2] && col[2] < 1.001) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
42 43 44 45
			RGB = new VEC3(col) ;
			break ;

		case (C_Luv) :
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
46 47 48
			assert (-0.001 < col[0] && col[0] < 100.001) ;
			assert (-83.001 < col[1] && col[1] < 175.001) ;
			assert (-134.001 < col[2] && col[2] < 108.001) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
49 50 51 52
			Luv = new VEC3(col) ;
			break ;

		case (C_XYZ) :
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
53 54 55
			assert (-0.001 < col[0] && col[0] < 1.001) ;
			assert (-0.001 < col[1] && col[1] < 1.001) ;
			assert (-0.001 < col[2] && col[2] < 1.001) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
56 57
			XYZ = new VEC3(col) ;
			break ;
58 59

		case (C_Lab) :
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
60 61 62
			assert (-0.001 < col[0] && col[0] < 100.001) ;
			assert (-86.001 < col[1] && col[1] < 98.001) ;
			assert (-108.001 < col[2] && col[2] < 95.001) ;
63 64
			Lab = new VEC3(col) ;
			break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	}
}

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

84 85
template<typename REAL>
Geom::Vector<3,REAL> ColourConverter<REAL>::getLab() {
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
86
	if (Lab == NULL)
87 88
		convert(originalEnc,C_Lab) ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
89
	return *Lab ;
90 91
}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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
151 152
	REAL Ydiv = Y/Yn ;
	if (Ydiv > 0.008856)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
153
		L = 116.0 * pow(Ydiv,1.0/3.0) - 16.0 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
154
	else // near black
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
155
		L = 903.3 * Ydiv ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
156 157 158

	REAL den = X + 15.0 * Y + 3 * Z ;
	REAL u1 = (4.0 * X) / den ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
159 160 161
	REAL v1 = (9.0 * Y) / den ;
	u = 13*L * (u1 - un) ;
	v = 13*L * (v1 - vn) ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

	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
177
		Y = Yn * L / 903.3 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
178 179

	REAL den = 13.0 * L ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
180 181
	REAL u1 = u/den + un ;
	REAL v1 = v/den + vn ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
182
	den = 4.0*v1 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
183
	X = Y * 9.0 * u1 / den ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
184
	Z = Y * (12.0 - 3.0*u1 - 20.0*v1) / den ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
185 186 187 188

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

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
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
221 222 223
	REAL &L = (*Lab)[0] ;
	REAL &a = (*Lab)[1] ;
	REAL &b = (*Lab)[2] ;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

	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
246 247
template<typename REAL>
bool ColourConverter<REAL>::convert(enum ColourEncoding from, enum ColourEncoding to) {
248 249 250 251 252
	if (to == from) {
		std::cerr << "ColourConverter::convert(from,to) : conversion into same colour space" << std::endl ;
		return false ;
	}

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
253 254 255 256 257 258 259 260 261 262 263 264 265
	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 ;
266 267 268 269 270 271
				case(C_Lab) :
					if (XYZ == NULL)
						convertRGBtoXYZ() ;
					if (Lab == NULL)
						convertXYZtoLab() ;
					break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
				default :
					std::cerr << "Colour conversion not supported" << std::endl ;
					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 ;
				}
293 294 295 296 297 298 299
				case(C_Lab) :
					if (Lab == NULL) {
						if (XYZ == NULL)
							convertLuvToXYZ() ;
						convertXYZtoLab() ;
					}
					break ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
				default :
					std::cerr << "Colour conversion not supported" << std::endl ;
					return false ;
			}
			break ;

		case(C_XYZ) :
			switch (to) {
				case(C_RGB) :
					if (RGB==NULL)
						convertXYZtoRGB() ;
					break ;
				case(C_Luv) :
					if (Luv == NULL)
						convertXYZtoLuv() ;
					break ;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
				case(C_Lab) :
					if (Lab == NULL)
						convertXYZtoLab() ;
					break ;
				default :
					std::cerr << "Colour conversion not supported" << std::endl ;
					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
348 349 350 351 352
				default :
					std::cerr << "Colour conversion not supported" << std::endl ;
					return false ;
			}
			break ;
353

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367
		default :
			std::cerr << "Colour conversion not supported" << std::endl ;
			return false ;
	}

	return true ;
}



} // namespace Utils
} // namespace CGoGN