~ubuntu-branches/ubuntu/oneiric/libclaw/oneiric

« back to all changes in this revision

Viewing changes to claw/pixel.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge
  • Date: 2008-05-17 15:36:57 UTC
  • Revision ID: james.westby@ubuntu.com-20080517153657-0b1204j754ykoz48
Tags: upstream-1.5.2b
ImportĀ upstreamĀ versionĀ 1.5.2b

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  CLAW - a C++ Library Absolutely Wonderful
 
3
 
 
4
  CLAW is a free library without any particular aim but being useful to 
 
5
  anyone.
 
6
 
 
7
  Copyright (C) 2005-2008 Julien Jorge
 
8
 
 
9
  This library is free software; you can redistribute it and/or
 
10
  modify it under the terms of the GNU Lesser General Public
 
11
  License as published by the Free Software Foundation; either
 
12
  version 2.1 of the License, or (at your option) any later version.
 
13
 
 
14
  This library is distributed in the hope that it will be useful,
 
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
  Lesser General Public License for more details.
 
18
 
 
19
  You should have received a copy of the GNU Lesser General Public
 
20
  License along with this library; if not, write to the Free Software
 
21
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
22
 
 
23
  contact: julien_jorge@yahoo.fr
 
24
*/
 
25
/**
 
26
 * \file pixel.hpp
 
27
 * \brief Representation of a pixel in image processing.
 
28
 * \author Julien Jorge
 
29
 */
 
30
#ifndef __CLAW_PIXEL_HPP_
 
31
#define __CLAW_PIXEL_HPP_
 
32
 
 
33
namespace claw
 
34
{
 
35
  namespace graphic
 
36
  {
 
37
    /**
 
38
     * \brief RGB pixel.
 
39
     */
 
40
    struct pixel24
 
41
    {
 
42
      typedef unsigned char component_type;
 
43
 
 
44
      /** \brief Component by component representation. */
 
45
      struct
 
46
      {
 
47
        /** \brief Red component. */
 
48
        component_type red;
 
49
 
 
50
        /** \brief Green component. */
 
51
        component_type green;
 
52
 
 
53
        /** \brief Blue component. */
 
54
        component_type blue;
 
55
 
 
56
      } components; 
 
57
 
 
58
    public:
 
59
      /** \brief Default constructor. */
 
60
      pixel24() {}
 
61
 
 
62
      /**
 
63
       * \brief Constructor.
 
64
       * \param r The value of the red field.
 
65
       * \param g The value of the green field.
 
66
       * \param b The value of the blue field.
 
67
       */
 
68
      pixel24( component_type r, component_type g, component_type b )
 
69
      { 
 
70
        components.red = r;
 
71
        components.green = g;
 
72
        components.blue = b;
 
73
      } // pixel24()
 
74
 
 
75
    }; // struct pixel24
 
76
 
 
77
    /**
 
78
     * \brief RGBA pixel.
 
79
     */
 
80
    union pixel32
 
81
    {
 
82
      typedef unsigned char component_type;
 
83
 
 
84
      /** \brief Compressed representation. */
 
85
      unsigned int pixel;
 
86
 
 
87
      /** \brief Component by component representation. */
 
88
      struct
 
89
      {
 
90
        /** \brief Red component. */
 
91
        component_type red;
 
92
 
 
93
        /** \brief Green component. */
 
94
        component_type green;
 
95
 
 
96
        /** \brief Blue component. */
 
97
        component_type blue;
 
98
 
 
99
        /** \brief Translucy. */
 
100
        component_type alpha;
 
101
 
 
102
      } components; 
 
103
 
 
104
    public:
 
105
      /** \brief Default constructor. */
 
106
      pixel32() {}
 
107
 
 
108
      /**
 
109
       * \brief Constructor.
 
110
       * \param r The value of the red field.
 
111
       * \param g The value of the green field.
 
112
       * \param b The value of the blue field.
 
113
       * \param a The value of the alpha field.
 
114
       */
 
115
      pixel32( component_type r, component_type g, component_type b,
 
116
               component_type a )
 
117
      { 
 
118
        components.red = r;
 
119
        components.green = g;
 
120
        components.blue = b;
 
121
        components.alpha = a;
 
122
      } // pixel32()
 
123
 
 
124
      /**
 
125
       * \brief Assignement operator.
 
126
       * \param that The other pixel.
 
127
       * \remark The alpha component of the \a this is set to 255.
 
128
       */
 
129
      pixel32& operator=( const pixel24& that )
 
130
      {
 
131
        components.red = that.components.red;
 
132
        components.green = that.components.green;
 
133
        components.blue = that.components.blue;
 
134
        components.alpha = 255;
 
135
 
 
136
        return *this;
 
137
      } // operator=()
 
138
 
 
139
      /**
 
140
       * \brief Tell if two pixels are equal.
 
141
       * \param that The other pixel.
 
142
       */
 
143
      bool operator==( const pixel32& that ) const
 
144
      {
 
145
        return pixel == that.pixel;
 
146
      } // operator==()
 
147
 
 
148
      /**
 
149
       * \brief Tell if two pixels are different.
 
150
       * \param that The other pixel.
 
151
       */
 
152
      bool operator!=( const pixel32& that ) const
 
153
      {
 
154
        return pixel != that.pixel;
 
155
      } // operator!=()
 
156
 
 
157
      /**
 
158
       * \brief Get the luminosity of the pixel.
 
159
       * \return The luminosity between 0 (dark) and 255 (light).
 
160
       *
 
161
       * The luminosity is approximated with integers:
 
162
       * \f$0,715160 \simeq 183 \div 256 = 0,71484375 \f$
 
163
       * \f$0,212671 \simeq 54 \div 256 = 0,2109375 \f$
 
164
       * \f$0,072169 \simeq 18 \div 256 = 0,0703125 \f$
 
165
       */
 
166
      component_type luminosity() const
 
167
      {
 
168
        return ((unsigned int)components.red * 183 
 
169
                + (unsigned int)components.green * 54 
 
170
                + (unsigned int)components.blue * 18
 
171
                ) / 256;
 
172
      } // luminosity()
 
173
 
 
174
    };
 
175
  } // namespace graphic
 
176
} // namespace claw
 
177
 
 
178
#endif // __CLAW_PIXEL_HPP__