itom 2.2.1
|
00001 /* ******************************************************************** 00002 itom software 00003 URL: http://www.uni-stuttgart.de/ito 00004 Copyright (C) 2016, Institut fuer Technische Optik (ITO), 00005 Universitaet Stuttgart, Germany 00006 00007 This file is part of itom and its software development toolkit (SDK). 00008 00009 itom is free software; you can redistribute it and/or modify it 00010 under the terms of the GNU Library General Public Licence as published by 00011 the Free Software Foundation; either version 2 of the Licence, or (at 00012 your option) any later version. 00013 00014 In addition, as a special exception, the Institut fuer Technische 00015 Optik (ITO) gives you certain additional rights. 00016 These rights are described in the ITO LGPL Exception version 1.0, 00017 which can be found in the file LGPL_EXCEPTION.txt in this package. 00018 00019 itom is distributed in the hope that it will be useful, but 00020 WITHOUT ANY WARRANTY; without even the implied warranty of 00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library 00022 General Public Licence for more details. 00023 00024 You should have received a copy of the GNU Library General Public License 00025 along with itom. If not, see <http://www.gnu.org/licenses/>. 00026 *********************************************************************** */ 00027 00028 #ifndef COLOR_H 00029 #define COLOR_H 00030 00031 #include "typeDefs.h" 00032 00033 namespace ito 00034 { 00035 00037 00046 class Rgba32 : public RgbaBase32 00047 { 00048 public: 00049 00050 enum RGBSelectionFlags 00051 { 00052 RGBA_B = 0, 00053 RGBA_G = 1, 00054 RGBA_R = 2, 00055 RGBA_A = 3, 00056 RGBA_Y = 4, 00057 RGBA_RGB = 5 00058 }; 00059 00060 Rgba32() 00061 { 00062 //memset(m_value, 0, 4*sizeof(ito::uint8)); 00063 } 00064 00065 static Rgba32 zeros() 00066 { 00067 Rgba32 temp; 00068 temp.rgba = 0; 00069 return temp; 00070 } 00071 00072 static Rgba32 black() 00073 { 00074 Rgba32 temp(255, 0,0,0); 00075 return temp; 00076 } 00077 00079 00082 static Rgba32 fromUnsignedLong(const uint32 val) 00083 { 00084 Rgba32 temp; 00085 memcpy(temp.u8ptr(), &val, 4*sizeof(ito::uint8)); 00086 return temp; 00087 } 00088 00089 Rgba32(const uint8 &a, const uint8 &r, const uint8 &g, const uint8 &b) 00090 { 00091 this->b = b; 00092 this->g = g; 00093 this->r = r; 00094 this->a = a; 00095 } 00096 00098 00101 explicit Rgba32(const uint8 gray) 00102 { 00103 a = 0xFF; 00104 r = gray; 00105 b = gray; 00106 g = gray; 00107 } 00108 00109 Rgba32(const Rgba32 &rhs) 00110 { 00111 rgba = rhs.rgba; 00112 }; 00113 00114 Rgba32& operator +=(const Rgba32 &rhs) 00115 { 00116 b = static_cast<uint8>(std::min<int16>(b + rhs.b, 255)); 00117 g = static_cast<uint8>(std::min<int16>(g + rhs.g, 255)); 00118 r = static_cast<uint8>(std::min<int16>(r + rhs.r, 255)); 00119 a = static_cast<uint8>(std::min<int16>(a + rhs.a, 255)); 00120 return *this; 00121 } 00122 00123 Rgba32& operator =(const Rgba32 &rhs) 00124 { 00125 rgba = rhs.rgba; 00126 return *this; 00127 } 00128 00129 Rgba32& operator =(const uint32 &rhs) 00130 { 00131 rgba = rhs; 00132 return *this; 00133 } 00134 00135 Rgba32& operator -=(const Rgba32 &rhs) 00136 { 00137 b = static_cast<uint8>(std::max<int16>(b - rhs.b, 0)); 00138 g = static_cast<uint8>(std::max<int16>(g - rhs.g, 0)); 00139 r = static_cast<uint8>(std::max<int16>(r - rhs.r, 0)); 00140 a = static_cast<uint8>(std::max<int16>(a - rhs.a, 0)); 00141 return *this; 00142 } 00143 00145 00148 Rgba32& operator *=(const Rgba32 &rhs) 00149 { 00150 b = static_cast<uint8>(b * rhs.b / 255); 00151 g = static_cast<uint8>(g * rhs.g / 255); 00152 r = static_cast<uint8>(r * rhs.r / 255); 00153 a = static_cast<uint8>(a * rhs.a / 255); 00154 return *this; 00155 } 00156 00158 00162 Rgba32& operator *=(const ito::float32 &grayFactor) 00163 { 00164 if (grayFactor < 0.0) 00165 { 00166 throw std::runtime_error("Multiplication factor must be >= 0.0"); 00167 } 00168 unsigned int t = static_cast<unsigned int>(b * grayFactor); 00169 b = static_cast<uint8>(t <= 255 ? t : 255); 00170 t = static_cast<unsigned int>(g * grayFactor); 00171 g = static_cast<uint8>(t <= 255 ? t : 255); 00172 t = static_cast<unsigned int>(r * grayFactor); 00173 r = static_cast<uint8>(t <= 255 ? t : 255); 00174 return *this; 00175 } 00176 00177 Rgba32& operator /=(const Rgba32 &rhs) 00178 { 00179 if(rhs.b == 0 || rhs.g == 0 || rhs.r == 0 || rhs.a == 0) 00180 { 00181 throw std::runtime_error("Division by zero not allowed for rgba32-values"); 00182 } 00183 b = static_cast<uint8>(std::min<int16>((b * (ito::int16)255) / rhs.b, 255)); 00184 g = static_cast<uint8>(std::min<int16>((g * (ito::int16)255) / rhs.g, 255)); 00185 r = static_cast<uint8>(std::min<int16>((r * (ito::int16)255) / rhs.r, 255)); 00186 a = static_cast<uint8>(std::min<int16>((a * (ito::int16)255) / rhs.a, 255)); 00187 return *this; 00188 } 00189 00190 Rgba32 operator +(const Rgba32 &second) const 00191 { 00192 Rgba32 first(*this); 00193 first += second; 00194 return first; 00195 } 00196 00197 Rgba32 operator -(const Rgba32 &second) const 00198 { 00199 Rgba32 first(*this); 00200 first -= second; 00201 return first; 00202 } 00203 00204 Rgba32 operator *(const Rgba32 &second) const 00205 { 00206 Rgba32 first(*this); 00207 first *= second; 00208 return first; 00209 } 00210 00211 Rgba32 operator *(const ito::float32 &second) const 00212 { 00213 Rgba32 first(*this); 00214 first *= second; 00215 return first; 00216 } 00217 00218 Rgba32 operator /(const Rgba32 &second) const 00219 { 00220 Rgba32 first(*this); 00221 first /= second; 00222 return first; 00223 } 00224 00225 bool operator ==(const Rgba32 &rhs) const 00226 { 00227 return (b == rhs.b) && (g == rhs.g) && (r == rhs.r && (a == rhs.a)); 00228 } 00229 00230 bool operator !=(const Rgba32 &rhs) const 00231 { 00232 return (b != rhs.b) || (g != rhs.g) || (r != rhs.r || (a != rhs.a)); 00233 } 00234 00235 inline float32 gray() const {return static_cast<float32>(0.299 * r + 0.587 * g + 0.114 * b);} 00237 uint8& alpha() {return a;}; 00238 uint8& red() {return r;}; 00239 uint8& green() {return g;}; 00240 uint8& blue() {return b;}; 00242 uint8 alpha() const {return a;}; 00243 uint8 red() const {return r;}; 00244 uint8 green() const {return g;}; 00245 uint8 blue() const {return b;}; 00247 uint32& argb() {return rgba;}; 00248 uint32 argb() const {return rgba;}; 00250 uint32* u32ptr() {return (&rgba);} 00251 uint8* u8ptr() {return ((uint8*)(&rgba));} 00252 }; 00253 00254 00255 template<uint8 _COLOR> class RGBChannel : Rgba32 00256 { 00257 public: 00258 RGBChannel() 00259 { 00260 //memset(m_value, 0, 4*sizeof(ito::uint8)); 00261 }; 00262 00263 explicit RGBChannel(const uint8 gray) 00264 { 00265 r = g = b = 0; 00266 a = 0xFF; 00267 items[_COLOR] = gray; 00268 } 00269 00270 static RGBChannel<_COLOR> zeros() 00271 { 00272 RGBChannel<_COLOR> temp; 00273 temp.rgba = 0; 00274 return temp; 00275 } 00276 00277 static RGBChannel<_COLOR> black() 00278 { 00279 RGBChannel<_COLOR> temp; 00280 temp.rgba = 0; 00281 temp.a = 0xFF; 00282 return temp; 00283 } 00284 00285 RGBChannel(const RGBChannel &rhs) 00286 { 00287 rgba = 0; 00288 a = 0xFF; 00289 items[_COLOR] = rhs.items[_COLOR]; 00290 } 00291 00292 RGBChannel& operator +=(const RGBChannel &rhs) 00293 { 00294 items[_COLOR] = static_cast<uint8>(std::min<int16>(items[_COLOR] + rhs.items[_COLOR], 255)); 00295 return *this; 00296 } 00297 00298 RGBChannel& operator =(const RGBChannel &rhs) 00299 { 00300 items[_COLOR] = rhs.items[_COLOR]; 00301 return *this; 00302 } 00303 00304 RGBChannel& operator =(const uint32 &rhs) 00305 { 00306 items[_COLOR] = ((unsigned char*)&rhs)[_COLOR]; 00307 return *this; 00308 } 00309 00310 RGBChannel& operator -=(const RGBChannel &rhs) 00311 { 00312 items[_COLOR] = static_cast<uint8>(std::max<int16>(items[_COLOR] - rhs.items[_COLOR], 0)); 00313 return *this; 00314 } 00315 00316 RGBChannel& operator *=(const RGBChannel &rhs) 00317 { 00318 items[_COLOR] = static_cast<uint8>(items[_COLOR] * rhs.items[_COLOR] / 255); 00319 return *this; 00320 } 00321 00322 RGBChannel& operator /=(const RGBChannel &rhs) 00323 { 00324 if(rhs.items[_COLOR] == 0) 00325 { 00326 throw std::runtime_error("Division by zero not allowed for rgba32-values"); 00327 } 00328 items[_COLOR] = static_cast<uint8>(std::min<int16>((items[_COLOR] * (ito::int16)255)/ rhs.items[_COLOR], 255)); 00329 return *this; 00330 } 00331 00332 RGBChannel operator +(const RGBChannel &second) const 00333 { 00334 RGBChannel<_COLOR> first(*this); 00335 first += second; 00336 return first; 00337 } 00338 00339 RGBChannel operator -(const RGBChannel &second) const 00340 { 00341 RGBChannel<_COLOR> first(*this); 00342 first -= second; 00343 return first; 00344 } 00345 00346 RGBChannel operator *(const RGBChannel &second) const 00347 { 00348 RGBChannel<_COLOR> first(*this); 00349 first *= second; 00350 return first; 00351 } 00352 00353 RGBChannel operator /(const RGBChannel &second) const 00354 { 00355 RGBChannel<_COLOR> first(*this); 00356 first /= second; 00357 return first; 00358 } 00359 00360 bool operator ==(const RGBChannel &rhs) const 00361 { 00362 return items[_COLOR] == rhs.items[_COLOR]; 00363 } 00364 00365 bool operator !=(const RGBChannel &rhs) const 00366 { 00367 return items[_COLOR] != rhs.items[_COLOR]; 00368 } 00369 00370 bool operator <(const RGBChannel &rhs) const 00371 { 00372 return (items[_COLOR] < rhs.items[_COLOR]); 00373 } 00374 00375 bool operator >(const RGBChannel &rhs) const 00376 { 00377 return (items[_COLOR] < rhs.items[_COLOR]); 00378 } 00379 00380 inline float32 gray() const {return static_cast<float32> (items[_COLOR]);}; 00382 uint8& value() {return items[_COLOR];}; 00383 uint8 value() const {return items[_COLOR];}; 00384 }; 00385 00386 typedef RGBChannel<Rgba32::RGBA_A> AlphaChannel; 00387 typedef RGBChannel<Rgba32::RGBA_R> RedChannel; 00388 typedef RGBChannel<Rgba32::RGBA_G> GreenChannel; 00389 typedef RGBChannel<Rgba32::RGBA_B> BlueChannel; 00391 00392 } //end namespace ito 00393 00394 #endif //COLOR_H