last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998-2004 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * 00006 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00007 * 00008 * The CVR-Lib is free software; you can redistribute it and/or 00009 * modify it under the terms of the BSD License. 00010 * 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. Neither the name of the authors nor the names of its contributors may be 00024 * used to endorse or promote products derived from this software without 00025 * specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 00042 /** 00043 * \file cvrRGBPixel.h 00044 * Contains the basic template class to represent RGB pixels (without 00045 * an alpha channel) 00046 * \author Pablo Alvarado 00047 * \date 01.11.2002 00048 * 00049 * $Id: cvrRGBPixel.h,v 1.8 2007/09/10 02:49:53 alvarado Exp $ 00050 */ 00051 00052 #ifndef _CVR_RGB_PIXEL_H_ 00053 #define _CVR_RGB_PIXEL_H_ 00054 00055 #include "cvrAssert.h" 00056 #include "cvrTypes.h" 00057 #include "cvrTypeInfo.h" 00058 #include "cvrIoHandler.h" 00059 00060 #include "cvrRGBAPixel.h" 00061 00062 #include <iosfwd> // Remember: instead of <iostream> we can just include 00063 // iosfwd as we only need the forward declaration of the 00064 // stream! This saves compilation times! 00065 00066 namespace cvr { 00067 00068 /** 00069 * Color pixel representation in RGB color space. 00070 * 00071 * This is a more general type than cvr::rgbaPixel, which is used to 00072 * represent points in the RGB color space with arbitrary precision, 00073 * specified through the template type of the class. 00074 * 00075 * The types supported for T are byte, ubyte, int16, uint16, int32, uint32, 00076 * float and double. 00077 * 00078 * @ingroup gColor 00079 */ 00080 template <typename T> 00081 class rgbPixel { 00082 public: 00083 /** 00084 * Anonymous union to provide efficient access through three different 00085 * mechanism to the pixel components. 00086 * 00087 * The order of the data in this union assumes little endianness. 00088 */ 00089 union { 00090 /** 00091 * Anonymous union with the three channels 00092 */ 00093 __extension__ struct { 00094 /** 00095 * Red channel 00096 */ 00097 T red; 00098 00099 /** 00100 * Green channel 00101 */ 00102 T green; 00103 00104 /** 00105 * Blue channel 00106 */ 00107 T blue; 00108 }; 00109 00110 /** 00111 * The four values as array 00112 */ 00113 T data[3]; 00114 }; 00115 00116 /** 00117 * Used for the template-based interface for pixels as vectors. 00118 */ 00119 typedef T value_type; 00120 00121 /** 00122 * Return type of the size() member 00123 */ 00124 typedef int size_type; 00125 00126 /** 00127 * Default constructor 00128 * 00129 * The default constructor of the rgbPixel<T> does \b not 00130 * initialize anything, in order to allow the creation of uninitialized 00131 * images in a more efficient way. 00132 */ 00133 rgbPixel(); 00134 00135 /** 00136 * RGB constructor 00137 * @param r value for the red component 00138 * @param g value for the green component 00139 * @param b value for the blue component 00140 */ 00141 rgbPixel(const T r,const T g,const T b); 00142 00143 /** 00144 * Copy constructor 00145 */ 00146 rgbPixel(const rgbaPixel& other); 00147 00148 /** 00149 * Copy constructor 00150 */ 00151 template<class U> 00152 inline rgbPixel(const rgbPixel<U>& other); 00153 00154 /** 00155 * Set the red, green, blue values for the pixel 00156 */ 00157 template<class U> 00158 inline void set(const U r, const U g, const U blue); 00159 00160 /** 00161 * Sets the red component to given value 00162 */ 00163 inline void setRed(const T r); 00164 00165 /** 00166 * Sets the green component to given value 00167 */ 00168 inline void setGreen(const T g); 00169 00170 /** 00171 * Sets the blue component to given value 00172 */ 00173 inline void setBlue(const T b); 00174 00175 /** 00176 * Get the three color components and write them in the given arguments 00177 */ 00178 template<class U> 00179 inline void get(U& r,U& g,U& b) const; 00180 00181 /** 00182 * Returns red component 00183 */ 00184 inline const T& getRed() const; 00185 00186 /** 00187 * Returns green component 00188 */ 00189 inline const T& getGreen() const; 00190 00191 /** 00192 * Returns blue component 00193 */ 00194 inline const T& getBlue() const; 00195 00196 /** 00197 * Returns red component 00198 */ 00199 inline T& getRed(); 00200 00201 /** 00202 * Returns green component 00203 */ 00204 inline T& getGreen(); 00205 00206 /** 00207 * Returns blue component 00208 */ 00209 inline T& getBlue(); 00210 00211 /** 00212 * Used to simulate vector access. 00213 * 00214 * The correspondence between the elements of the vector and 00215 * the color components is at(0) for red, at(1) for green and 00216 * at(2) for blue. 00217 */ 00218 inline T& at(const int x); 00219 00220 /** 00221 * Used to simulate read-only vector access. 00222 * 00223 * The correspondence between the elements of the vector and 00224 * the color components still depents on the endianness of the 00225 * system, but is usually at(0) for red, at(1) for green and 00226 * at(2) for blue. 00227 */ 00228 inline const T& at(const int x) const; 00229 00230 /** 00231 * Used to simulate vector access. 00232 * 00233 * The correspondence between the elements of the vector and 00234 * the color components is [0] for red, [1] for green and 00235 * [2] for blue. 00236 */ 00237 inline T& operator[](const int x); 00238 00239 /** 00240 * Used to simulate read-only vector access. 00241 * 00242 * The correspondence between the elements of the vector and 00243 * the color components still depents on the endianness of the 00244 * system, but is usually [0] for red, [1] for green and 00245 * [2] for blue. 00246 */ 00247 inline const T& operator[](const int x) const; 00248 00249 /** 00250 * Used to simulate the vector size. 00251 * 00252 * It returns always 3. 00253 */ 00254 inline size_type size() const; 00255 00256 /** 00257 * Copy the "other" pixel 00258 */ 00259 inline rgbPixel<T>& copy(const rgbPixel<T>& other); 00260 00261 /** 00262 * Alias for copy 00263 */ 00264 inline rgbPixel<T>& operator=(const rgbPixel<T>& other); 00265 00266 /** 00267 * Copy the "other" pixel 00268 */ 00269 inline rgbPixel<T>& copy(const rgbaPixel& other); 00270 00271 /** 00272 * Alias for copy 00273 */ 00274 inline rgbPixel<T>& operator=(const rgbaPixel& other); 00275 00276 /** 00277 * Cast To an cvr::rgbaPixel. 00278 * 00279 * The alpha value of the rgbaPixel is always set to zero 00280 */ 00281 inline void castTo(rgbaPixel& other) const; 00282 00283 /** 00284 * Get a new cvr::rgbaPixel with the contents of this pixel. 00285 * 00286 * Just a static_cast of the attributes will be made and the alpha is set 00287 * to zero. No clipping of the values is made in case of under or 00288 * overflow. 00289 */ 00290 inline rgbaPixel getRGBAPixel() const; 00291 00292 /** 00293 * Get a new cvr::rgbaPixel with the contents of this pixel. 00294 * 00295 * Just a static_cast of the attributes will be made and the alpha is set 00296 * to zero. In case of under/overflow, the values are set to zero or 255. 00297 */ 00298 inline rgbaPixel getClippedRGBAPixel() const; 00299 00300 /** 00301 * Compare two pixels (true if equal!) 00302 * 00303 * Two pixels are \e equal if all \b four components (red, green, 00304 * blue, and \b alpha) are equal. It is an usual error to leave the 00305 * alpha channel uninizalized and then compare for equalilty. 00306 */ 00307 inline bool isEqual(const rgbPixel<T>& other) const; 00308 00309 /** 00310 * Alias for compare() 00311 * 00312 * Two pixels are \e equal if all \b four components (red, green, 00313 * blue, and \b alpha) are equal. It is an usual error to leave the 00314 * alpha channel uninizalized and then compare for equalilty. 00315 */ 00316 inline bool operator==(const rgbPixel<T>& other) const; 00317 00318 /** 00319 * Alias for !compare() 00320 * 00321 * Two pixels are \e equal if all \b four components (red, green, 00322 * blue, and \b alpha) are equal. It is an usual error to leave the 00323 * alpha channel uninizalized and then compare for equalilty. 00324 */ 00325 inline bool operator!=(const rgbPixel<T>& other) const; 00326 00327 /** 00328 * Less than operator. 00329 * 00330 * An rgbPixel<T> is said to be "smaller" than another one, if 00331 * its red component its smaller, or (if both red components are 00332 * equal) if the green component if smaller, or (if both green components 00333 * are equal) if the blue component is smaller. 00334 */ 00335 inline bool operator<(const rgbPixel<T>& other) const; 00336 00337 /** 00338 * Greater than operator. 00339 * 00340 * An rgbPixel<T> is said to be "greater" than another one, if 00341 * its red component its greater, or (if both red components are 00342 * equal) if the green component if greater, or (if both green components 00343 * are equal) if the blue component is greater. 00344 */ 00345 inline bool operator>(const rgbPixel<T>& other) const; 00346 00347 /** 00348 * Add this pixel with another one. 00349 * 00350 * The alpha channel is kept unchanged. 00351 */ 00352 inline rgbPixel<T>& add(const rgbPixel<T>& other); 00353 00354 /** 00355 * Add this pixel with another one. 00356 * 00357 * The alpha channel is kept unchanged. 00358 */ 00359 inline rgbPixel<T>& operator+=(const rgbPixel<T>& other); 00360 00361 /** 00362 * Add this pixel with the other one without altering anything. 00363 * 00364 * The alpha channel of the resulting pixel is equal to 00365 * the one of this pixel (the first operand in the binary expression) 00366 */ 00367 inline rgbPixel<T> operator+(const rgbPixel<T>& other) const; 00368 00369 /** 00370 * Subtract 'other' from this pixel. 00371 * 00372 * The alpha channel is kept unchanged. 00373 */ 00374 inline rgbPixel<T>& subtract(const rgbPixel<T>& other); 00375 00376 /** 00377 * Subtract 'other' from this pixel. 00378 * 00379 * The alpha channel is kept unchanged. 00380 */ 00381 inline rgbPixel<T>& operator-=(const rgbPixel<T>& other); 00382 00383 /** 00384 * Subtract 'other' from this pixel without altering anything. 00385 * 00386 * The alpha channel of the resulting pixel is equal to 00387 * the one of this pixel (the first operand in the binary expression) 00388 */ 00389 inline rgbPixel<T> operator-(const rgbPixel<T>& other) const; 00390 00391 /** 00392 * Multiply this pixel with another one. 00393 * 00394 * The pixel multiplication multiplies elementwise the elements of 00395 * the pixel, except the alpha channel, which is kept unchanged. 00396 */ 00397 inline rgbPixel<T>& multiply(const rgbPixel<T>& other); 00398 00399 /** 00400 * Multiply this pixel with another one. 00401 * 00402 * The pixel multiplication multiplies elementwise the elements of 00403 * the pixel, except the alpha channel, which is kept unchanged. 00404 */ 00405 inline rgbPixel<T>& operator*=(const rgbPixel<T>& other); 00406 00407 /** 00408 * Multiply this pixel with another one without altering anything. 00409 * 00410 * The alpha channel of the resulting pixel is equal to 00411 * the one of this pixel (the first operand in the binary expression) 00412 */ 00413 inline rgbPixel<T> operator*(const rgbPixel<T>& other) const; 00414 00415 /** 00416 * Multiply all components of this pixel with an integer, except the alpha 00417 * channel, which is kept unchanged. 00418 */ 00419 inline rgbPixel<T>& multiply(const int other); 00420 00421 /** 00422 * Multiply all components of this pixel with a float, except the alpha 00423 * channel, which is kept unchanged. 00424 */ 00425 inline rgbPixel<T>& multiply(const float& other); 00426 00427 /** 00428 * Multiply all components of this pixel with a float, except the alpha 00429 * channel, which is kept unchanged. 00430 */ 00431 inline rgbPixel<T>& multiply(const double& other); 00432 00433 /** 00434 * Multiply all components of this pixel with a an integer, except the 00435 * alpha channel, which is kept unchanged. 00436 */ 00437 inline rgbPixel<T>& operator*=(const int other); 00438 00439 /** 00440 * Multiply all components of this pixel with a a float, except the 00441 * alpha channel, which is kept unchanged. 00442 */ 00443 inline rgbPixel<T>& operator*=(const float& other); 00444 00445 /** 00446 * Multiply all components of this pixel with a a double, except the 00447 * alpha channel, which is kept unchanged. 00448 */ 00449 inline rgbPixel<T>& operator*=(const double& other); 00450 00451 /** 00452 * Multiply all components of this pixel with a an integer, except the 00453 * alpha channel. 00454 * 00455 * The alpha channel of the resulting pixel is equal to 00456 * the one of this pixel (the first operand in the binary expression) 00457 */ 00458 inline rgbPixel<T> operator*(const int other) const; 00459 00460 /** 00461 * Multiply all components of this pixel with a a float, except the 00462 * alpha channel. 00463 * 00464 * The alpha channel of the resulting pixel is equal to 00465 * the one of this pixel (the first operand in the binary expression) 00466 */ 00467 inline rgbPixel<T> operator*(const float& other) const; 00468 00469 /** 00470 * Multiply all components of this pixel with a a double, except the 00471 * alpha channel. 00472 * 00473 * The alpha channel of the resulting pixel is equal to 00474 * the one of this pixel (the first operand in the binary expression) 00475 */ 00476 inline rgbPixel<T> operator*(const double& other) const; 00477 00478 /** 00479 * Divide this pixel with another one. 00480 * 00481 * The pixel division divides elementwise the elements of the pixel 00482 * except the alpha channel, which is kept unchanged. 00483 */ 00484 inline rgbPixel<T>& divide(const rgbPixel<T>& other); 00485 00486 /** 00487 * Divide this pixel with another one. 00488 * 00489 * The pixel division divides elementwise the elements of the pixel 00490 * except the alpha channel, which is kept unchanged. 00491 */ 00492 inline rgbPixel<T>& operator/=(const rgbPixel<T>& other); 00493 00494 /** 00495 * Divide this pixel with another one. 00496 * 00497 * The pixel division divides elementwise the elements of the pixel 00498 * except the alpha channel. 00499 * 00500 * The alpha channel of the resulting pixel is equal to 00501 * the one of this pixel (the first operand in the binary expression) 00502 */ 00503 inline rgbPixel<T> operator/(const rgbPixel<T>& other) const; 00504 00505 /** 00506 * Divide all components of this pixel with an integer. 00507 */ 00508 inline rgbPixel<T>& divide(const int other); 00509 00510 /** 00511 * Divide all components of this pixel with an integer. 00512 */ 00513 inline rgbPixel<T>& divide(const float& other); 00514 00515 /** 00516 * Divide all components of this pixel with an integer. 00517 */ 00518 inline rgbPixel<T>& divide(const double& other); 00519 00520 /** 00521 * Divide all components of this pixel with an integer. 00522 */ 00523 inline rgbPixel<T>& operator/=(const int other); 00524 00525 /** 00526 * Divide all components of this pixel with an integer. 00527 */ 00528 inline rgbPixel<T>& operator/=(const float& other); 00529 00530 /** 00531 * Divide all components of this pixel with an integer. 00532 */ 00533 inline rgbPixel<T>& operator/=(const double& other); 00534 00535 /** 00536 * Divide all components of this pixel with an integer without altering 00537 * anything. 00538 * 00539 * The alpha channel of the resulting pixel is equal to 00540 * the one of this pixel (the first operand in the binary expression) 00541 */ 00542 inline rgbPixel<T> operator/(const int other) const; 00543 00544 /** 00545 * Divide all components of this pixel with an integer without altering 00546 * anything. 00547 * 00548 * The alpha channel of the resulting pixel is equal to 00549 * the one of this pixel (the first operand in the binary expression) 00550 */ 00551 inline rgbPixel<T> operator/(const float& other) const; 00552 00553 /** 00554 * Divide all components of this pixel with an integer without altering 00555 * anything. 00556 * 00557 * The alpha channel of the resulting pixel is equal to 00558 * the one of this pixel (the first operand in the binary expression) 00559 */ 00560 inline rgbPixel<T> operator/(const double& other) const; 00561 00562 /** 00563 * Compute the square of the magnitud of this pixel 00564 * \f$red^2+green^2+blue^2\f$. 00565 */ 00566 inline typename typeInfo<T>::square_accumulation_type absSqr() const; 00567 00568 /** 00569 * Scalar product in the 3D RGB color space. 00570 * 00571 * Get the scalar product of this pixel with another one, considering 00572 * them as a 3D point in the RGB color space. 00573 * 00574 * The dot product will be the sum of the 00575 * red*other.red + green*other.green + blue*other.blue 00576 */ 00577 inline typename typeInfo<T>::square_accumulation_type 00578 dot(const rgbPixel<T>& other) const; 00579 00580 /** 00581 * Square of the distance between this pixel and the other one. 00582 * 00583 * \f$(red-other.red)^2+(green-other.green)^2+(blue-other.blue)^2\f$. 00584 */ 00585 inline typename typeInfo<T>::square_accumulation_type 00586 distanceSqr(const rgbPixel<T>& other) const; 00587 }; 00588 00589 /** 00590 * Multiply all components of the given pixel with an integer 00591 * except the alpha channel. 00592 */ 00593 template <typename T> 00594 inline rgbPixel<T> operator*(const int p,const rgbPixel<T>& other); 00595 00596 /** 00597 * Multiply all components of the given pixel with an integer 00598 * except the alpha channel. 00599 */ 00600 template <typename T> 00601 inline rgbPixel<T> operator*(const float p,const rgbPixel<T>& other); 00602 00603 /** 00604 * Multiply all components of the given pixel with an integer 00605 * except the alpha channel. 00606 */ 00607 template <typename T> 00608 inline rgbPixel<T> operator*(const double& p,const rgbPixel<T>& other); 00609 00610 /** 00611 * Read the vector from the given ioHandler. 00612 * 00613 * The complete flag indicates if the enclosing begin and end should be also 00614 * be readed 00615 * 00616 * @ingroup gStorable 00617 */ 00618 template <typename T> 00619 bool read(ioHandler& handler,rgbPixel<T>& p,const bool complete=true); 00620 00621 /** 00622 * Write the vector in the given ioHandler. 00623 * 00624 * The complete flag indicates if the enclosing begin and end should be also 00625 * be written or not 00626 * 00627 * @ingroup gStorable 00628 */ 00629 template <typename T> 00630 bool write(ioHandler& handler,const rgbPixel<T>& p,const bool complete=true); 00631 00632 /** 00633 * Alias for rgbPixel<int> 00634 */ 00635 typedef rgbPixel<int> irgbPixel; 00636 00637 /** 00638 * Alias for rgbPixel<float> 00639 */ 00640 typedef rgbPixel<float> frgbPixel; 00641 00642 /** 00643 * Alias for rgbPixel<double> 00644 */ 00645 typedef rgbPixel<double> drgbPixel; 00646 } 00647 00648 namespace std { 00649 template <typename T> 00650 ostream& operator<<(ostream& s,const cvr::rgbPixel<T>& p); 00651 00652 template <typename T> 00653 istream& operator>>(istream& s,cvr::rgbPixel<T>& p); 00654 } 00655 00656 #include "cvrRGBPixel_inline.h" 00657 00658 #endif 00659 00660