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 32 33 34 35 36 37 38 39 40 41 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
|
/Class Color.h
#include <ostream>
#ifndef _COLOR
#define _COLOR
namespace imaging
{
/*! An alias for the floating point representation of color components (32bit per color channel).
*
* Each color channel goes from 0.0f (darkness) to 1.0f (full color brightness).
* For example, bright red is (1,0,0), white is (1,1,1), magenta is (1,0,1) etc.
*/
typedef float component_t;
/*! Represents a triplet of Red, Green, Blue (RGB) values.
*/
class Color
{
public:
// members
component_t r, //! The red color channel (component)
g, //! The green color channel (component)
b; //! The blue color channel (component)
// member functions
/*! This operator returns the index-th component of the image.
*
* For speed, no bounds for index values are checked.
*
* \param index is the index of the component to obtain. Values should be 0, 1 or 2.
*
* \return a reference to the respective color component.
*/
component_t & operator [] (size_t index)
{
return *(&r + index);
}
/*! Addition operator.
*
* Adds a color to the current one and returns the result.
*
* \param right is the right Color operand of the + sign.
*
* \return the resulting color after the component-wise addition of the two colors.
*/
Color operator + (Color & right)
{
Color left;
left.r = r + right.r;
left.g = g + right.g;
left.b = b + right.b;
return left;
}
// constructors
/*! Parameterized constructor.
*
* \param r is the red component of the color.
* \param g is the green component of the color.
* \param b is the blue component of the color.
*/
Color(component_t r, component_t g, component_t b) : r(r), g(g), b(b) {}
/*! Default constructor.
*
* All components set to zero, i.e. a black color.
*/
Color() : r(0), g(0), b(0) {}
};
}
#endif _COLOR
//Class Image.cpp
// data mutators
/*! Sets the RGB values for an (x,y) pixel.
*
* The method should perform any necessary bounds checking.
*
* \param x is the (zero-based) horizontal index of the pixel to set.
* \param y is the (zero-based) vertical index of the pixel to set.
* \param value is the new color for the (x,y) pixel.
*/
void setPixel(unsigned int x, unsigned int y, Color & value)
{
if ((x <= Image::getWidth()) && (y <= Image::getHeight()) && ((x >= 0)) && (y >=0) ) //mporei alliws oi parentheseis
{
buffer[x * y + RED] = value.r;
buffer[x * y + GREEN] = value.g;
buffer[x * y + BLUE] = value.b;
}
else
{
std::cout<< "Width or heigth out of bounds!" << std::endl;
}
}
|