summaryrefslogtreecommitdiff
path: root/src/graphics/Pixel32.h
blob: 7ec6e987abc65edcdd440b2a8d6999bd987bfa1e (plain)
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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#ifndef _Pixel32_H_
#define _Pixel32_H_

#include "../api.h"
#include "../base/UTF8String.h"

#include "Pixeldefs.h"

#include <string>
#include <math.h>
#include <stdlib.h>

namespace avg {

class Pixel32
{

public:
    Pixel32();
    Pixel32(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
    Pixel32(unsigned char r, unsigned char g, unsigned char b);
    void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
    void set(unsigned char r, unsigned char g, unsigned char b);
    void setR(unsigned char r);
    void setG(unsigned char g);
    void setB(unsigned char b);
    void setA(unsigned char a);
    unsigned char getR() const;
    unsigned char getG() const;
    unsigned char getB() const;
    unsigned char getA() const;
    void flipRB();
    void toHSL(float& h, float& s, float& l);

    bool operator ==(const Pixel32 pix) const;
    bool operator !=(const Pixel32 pix) const;
    void operator +=(const Pixel32 pix);
    void operator -=(const Pixel32 pix);
    Pixel32 operator *(float f) const;

    // Simple and fast 'distance' between two pixels. Just adds the
    // distances between the color components and treats colors
    // equally.
    int boxDist(const Pixel32 pix) const;

    std::string AVG_API getColorString() const;

  private:
    unsigned char m_Data[4];
};

AVG_API std::ostream& operator <<(std::ostream& os, const Pixel32& pix);

AVG_API Pixel32 colorStringToColor(const UTF8String& s);

void YUVtoBGR32Pixel(Pixel32* pDest, int y, int u, int v);
void YUVJtoBGR32Pixel(Pixel32* pDest, int y, int u, int v);

inline Pixel32::Pixel32()
{
}


inline Pixel32::Pixel32(unsigned char r, unsigned char g, unsigned char b, 
        unsigned char a)
{
  set (r, g, b, a);
}


inline Pixel32::Pixel32(unsigned char r, unsigned char g, unsigned char b)
{
  set (r, g, b, 255);
}


inline void Pixel32::set(unsigned char r, unsigned char g, unsigned char b, 
        unsigned char a)
{
  m_Data[REDPOS] = r;
  m_Data[GREENPOS] = g;
  m_Data[BLUEPOS] = b;
  m_Data[ALPHAPOS] = a;
}

inline void Pixel32::set(unsigned char r, unsigned char g, unsigned char b)
{
  m_Data[REDPOS] = r;
  m_Data[GREENPOS] = g;
  m_Data[BLUEPOS] = b;
}

inline void Pixel32::setR(unsigned char r)
{
  m_Data[REDPOS] = r;
}


inline void Pixel32::setG(unsigned char g)
{
  m_Data[GREENPOS] = g;
}


inline void Pixel32::setB(unsigned char b)
{
  m_Data[BLUEPOS] = b;
}

inline void Pixel32::setA(unsigned char a)
{
  m_Data[ALPHAPOS] = a;
}


inline unsigned char Pixel32::getR() const
{
  return m_Data[REDPOS];
}


inline unsigned char Pixel32::getG() const
{
  return m_Data[GREENPOS];
}


inline unsigned char Pixel32::getB() const
{
  return m_Data[BLUEPOS];
}


inline unsigned char Pixel32::getA() const
{
  return m_Data[ALPHAPOS];
}

inline void Pixel32::flipRB() 
{
    unsigned char tmp = m_Data[BLUEPOS];
    m_Data[BLUEPOS] = m_Data[REDPOS];
    m_Data[REDPOS] = tmp;
}

inline int Pixel32::boxDist(const Pixel32 pix) const
{
  return (abs ((int)getR()-pix.getR()) +
          abs ((int)getG()-pix.getG()) +
          abs ((int)getB()-pix.getB()));
}

inline bool Pixel32::operator ==(const Pixel32 pix) const
{
  return (*(const int *)this == *(const int*)&pix);
}

inline bool Pixel32::operator !=(const Pixel32 pix) const
{
  return (!(*this == pix));
}

inline void Pixel32::operator +=(const Pixel32 pix)
{
  m_Data[0] += pix.m_Data[0];
  m_Data[1] += pix.m_Data[1];
  m_Data[2] += pix.m_Data[2];
}

inline void Pixel32::operator -=(const Pixel32 pix)
{
  m_Data[0] -= pix.m_Data[0];
  m_Data[1] -= pix.m_Data[1];
  m_Data[2] -= pix.m_Data[2];
}

inline Pixel32 Pixel32::operator *(float f) const
{
  return Pixel32((unsigned char)(f*getR()), 
          (unsigned char)(f*getG()), 
          (unsigned char)(f*getB()));
}



}
#endif