summaryrefslogtreecommitdiff
path: root/src/graphics/Pixel8.h
blob: 1d070377434000d87c0fa47ff3dc7d26b17e2bd4 (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
//
//  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 _Pixel8_H_
#define _Pixel8_H_

#include "../api.h"
#include "Pixeldefs.h"
#include "Pixel32.h"

#include <stdlib.h>

namespace avg {

class AVG_API Pixel8
{
  public:
    Pixel8 ();
    Pixel8 (unsigned char i);
    void set (unsigned char i);
    unsigned char get () const;
    unsigned char getR () const;
    unsigned char getG () const;
    unsigned char getB () const;
    void flipRB();

    template<class SrcPixel>
    Pixel8 operator = (const SrcPixel& Pix)
    {
        set ((Pix.getR()*54+Pix.getG()*183+Pix.getB()*19)/256);
        return *this;
    }
   
    Pixel8 operator = (const Pixel8& Pix)
    {
        set (Pix.get());
        return *this;
    }

    operator Pixel32 () const;

    bool operator ==(const Pixel8&) const;
    bool operator !=(const Pixel8&) const;
    void operator +=(const Pixel8&);
    void operator -=(const Pixel8&);

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

    // Returns a weighed average between two pixels. Factor must be 
    // between 0 and 256. Factor=256 means Pix1 is the result, Factor=0 
    // means Pix2 is the result.
    static Pixel8 Blend (int Factor, const Pixel8 Pix1, 
                            const Pixel8 Pix2);

  private:
    unsigned char m_i;
};

inline Pixel8::Pixel8()
{
}

inline Pixel8::Pixel8(unsigned char i)
{
  set (i);
}

inline void Pixel8::set(unsigned char i)
{
  m_i = i;
}

inline unsigned char Pixel8::get() const
{
  return m_i;
}

inline unsigned char Pixel8::getR() const
{
  return m_i;
}

inline unsigned char Pixel8::getG() const
{
  return m_i;
}

inline unsigned char Pixel8::getB() const
{
  return m_i;
}

inline int Pixel8::boxDist (const Pixel8 Pix) const
{
  return (abs ((int)get()-Pix.get()));
}

inline Pixel8 Pixel8::Blend (int Factor, const Pixel8 Pix1, const Pixel8 Pix2)
{
  return Pixel8 ((Pix1.get()*Factor+Pix2.get()*(256-Factor))>>8);
}

inline Pixel8::operator Pixel32 () const
{
  return Pixel32 (m_i, m_i, m_i, 255);
}

inline bool Pixel8::operator ==(const Pixel8& Pix) const
{
  return (get() == Pix.get());
}

inline bool Pixel8::operator !=(const Pixel8& Pix) const
{
  return (!(*this == Pix));
}

inline void Pixel8::operator += (const Pixel8& Pix)
{
  m_i += Pix.m_i;
}

inline void Pixel8::operator -= (const Pixel8& Pix)
{
  m_i -= Pix.m_i;
}

}
#endif