1
/* $Id: FilterColor.h 19485 2009-03-31 22:34:34Z gsrb3d $
2
-----------------------------------------------------------------------------
3
This source file is part of blendTex library
5
Copyright (c) 2007 The Zdeno Ash Miklas
7
This program is free software; you can redistribute it and/or modify it under
8
the terms of the GNU Lesser General Public License as published by the Free Software
9
Foundation; either version 2 of the License, or (at your option) any later
12
This program is distributed in the hope that it will be useful, but WITHOUT
13
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public License along with
17
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
19
http://www.gnu.org/copyleft/lesser.txt.
20
-----------------------------------------------------------------------------
23
#if !defined FILTERCOLOR_H
28
#include "FilterBase.h"
31
/// pixel filter for gray scale
32
class FilterGray : public FilterBase
38
virtual ~FilterGray (void) {}
41
/// filter pixel template, source int buffer
42
template <class SRC> unsigned int tFilter (SRC src, short x, short y,
43
short * size, unsigned int pixSize, unsigned int val)
45
// calculate gray value
46
unsigned int gray = (28 * (VT_B(val)) + 151 * (VT_G(val))
47
+ 77 * (VT_R(val))) >> 8;
48
// return gray scale value
55
/// virtual filtering function for byte source
56
virtual unsigned int filter (unsigned char * src, short x, short y,
57
short * size, unsigned int pixSize, unsigned int val = 0)
58
{ return tFilter(src, x, y, size, pixSize, val); }
59
/// virtual filtering function for unsigned int source
60
virtual unsigned int filter (unsigned int * src, short x, short y,
61
short * size, unsigned int pixSize, unsigned int val = 0)
62
{ return tFilter(src, x, y, size, pixSize, val); }
66
/// type for color matrix
67
typedef short ColorMatrix[4][5];
69
/// pixel filter for color calculation
70
class FilterColor : public FilterBase
76
virtual ~FilterColor (void) {}
79
ColorMatrix & getMatrix (void) { return m_matrix; }
81
void setMatrix (ColorMatrix & mat);
84
/// color calculation matrix
87
/// calculate one color component
88
unsigned char calcColor (unsigned int val, short idx)
90
return (((m_matrix[idx][0] * (VT_R(val)) + m_matrix[idx][1] * (VT_G(val))
91
+ m_matrix[idx][2] * (VT_B(val)) + m_matrix[idx][3] * (VT_A(val))
92
+ m_matrix[idx][4]) >> 8) & 0xFF);
95
/// filter pixel template, source int buffer
96
template <class SRC> unsigned int tFilter (SRC src, short x, short y,
97
short * size, unsigned int pixSize, unsigned int val)
99
// return calculated color
101
VT_RGBA(color, calcColor(val, 0), calcColor(val, 1), calcColor(val, 2), calcColor(val, 3));
105
/// virtual filtering function for byte source
106
virtual unsigned int filter (unsigned char * src, short x, short y,
107
short * size, unsigned int pixSize, unsigned int val = 0)
108
{ return tFilter(src, x, y, size, pixSize, val); }
109
/// virtual filtering function for unsigned int source
110
virtual unsigned int filter (unsigned int * src, short x, short y,
111
short * size, unsigned int pixSize, unsigned int val = 0)
112
{ return tFilter(src, x, y, size, pixSize, val); }
116
/// type for color levels
117
typedef unsigned short ColorLevel[4][3];
119
/// pixel filter for color calculation
120
class FilterLevel : public FilterBase
126
virtual ~FilterLevel (void) {}
129
ColorLevel & getLevels (void) { return levels; }
131
void setLevels (ColorLevel & lev);
134
/// color calculation matrix
137
/// calculate one color component
138
unsigned int calcColor (unsigned int val, short idx)
140
unsigned int col = VT_C(val,idx);;
141
if (col <= levels[idx][0]) col = 0;
142
else if (col >= levels[idx][1]) col = 0xFF;
143
else col = (((col - levels[idx][0]) << 8) / levels[idx][2]) & 0xFF;
147
/// filter pixel template, source int buffer
148
template <class SRC> unsigned int tFilter (SRC src, short x, short y,
149
short * size, unsigned int pixSize, unsigned int val)
151
// return calculated color
153
VT_RGBA(color, calcColor(val, 0), calcColor(val, 1), calcColor(val, 2), calcColor(val, 3));
157
/// virtual filtering function for byte source
158
virtual unsigned int filter (unsigned char * src, short x, short y,
159
short * size, unsigned int pixSize, unsigned int val = 0)
160
{ return tFilter(src, x, y, size, pixSize, val); }
161
/// virtual filtering function for unsigned int source
162
virtual unsigned int filter (unsigned int * src, short x, short y,
163
short * size, unsigned int pixSize, unsigned int val = 0)
164
{ return tFilter(src, x, y, size, pixSize, val); }