forked from ambilight-4-mediaportal/AtmoWin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAtmoOutputFilter.cpp
More file actions
254 lines (202 loc) · 8.24 KB
/
Copy pathAtmoOutputFilter.cpp
File metadata and controls
254 lines (202 loc) · 8.24 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* AtmoOutputFilter.cpp: Post Processor for the color data retrieved from
* a CAtmoInput
*
* mostly 1:1 from vdr-linux-src "filter.c" copied
*
* See the README.txt file for copyright information and how to reach the author(s).
*
* $Id: aeab6fd98bf521829faf8d2c39458eac3a961b2b $
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "AtmoOutputFilter.h"
CAtmoOutputFilter::CAtmoOutputFilter(CAtmoConfig *atmoConfig )
{
this->m_pAtmoConfig = atmoConfig;
this->m_percent_filter_output_old = NULL;
this->m_mean_filter_output_old = NULL;
this->m_mean_values = NULL;
this->m_mean_sums = NULL;
ResetFilter();
}
CAtmoOutputFilter::~CAtmoOutputFilter(void)
{
if(m_percent_filter_output_old)
delete (char *)m_percent_filter_output_old;
if(m_mean_filter_output_old)
delete (char *)m_mean_filter_output_old;
if(m_mean_values)
delete (char *)m_mean_values;
if(m_mean_sums)
delete (char *)m_mean_sums;
}
void CAtmoOutputFilter::ResetFilter(void)
{
// reset filter values
MeanFilter(NULL, true);
PercentFilter(NULL, true);
}
pColorPacket CAtmoOutputFilter::Filtering(pColorPacket ColorPacket)
{
switch (m_pAtmoConfig->getLiveViewFilterMode())
{
case afmNoFilter:
return ColorPacket;
break;
case afmCombined:
return MeanFilter(ColorPacket, false);
break;
case afmPercent:
return PercentFilter(ColorPacket, false);
break;
}
return ColorPacket;
}
pColorPacket CAtmoOutputFilter::PercentFilter(pColorPacket filter_input, ATMO_BOOL init)
{
// last values needed for the percentage filter
if (init) // Initialization
{
if(m_percent_filter_output_old)
delete (char *)m_percent_filter_output_old;
m_percent_filter_output_old = NULL;
return(NULL);
}
if(!m_percent_filter_output_old || (m_percent_filter_output_old->numColors!=filter_input->numColors)) {
delete m_percent_filter_output_old;
AllocColorPacket(m_percent_filter_output_old, filter_input->numColors);
ZeroColorPacket(m_percent_filter_output_old);
}
int percentNew = this->m_pAtmoConfig->getLiveViewFilter_PercentNew();
pColorPacket filter_output;
AllocColorPacket(filter_output, filter_input->numColors);
for (int zone = 0; zone < filter_input->numColors; zone++)
{
filter_output->zone[zone].r = (filter_input->zone[zone].r *
(100-percentNew) + m_percent_filter_output_old->zone[zone].r * percentNew) / 100;
filter_output->zone[zone].g = (filter_input->zone[zone].g *
(100-percentNew) + m_percent_filter_output_old->zone[zone].g * percentNew) / 100;
filter_output->zone[zone].b = (filter_input->zone[zone].b *
(100-percentNew) + m_percent_filter_output_old->zone[zone].b * percentNew) / 100;
}
CopyColorPacket( filter_output, m_percent_filter_output_old );
delete (char *)filter_input;
return filter_output;
}
pColorPacket CAtmoOutputFilter::MeanFilter(pColorPacket filter_input, ATMO_BOOL init)
{
// needed vor the running mean value filter
// needed for the percentage filter
static int filter_length_old;
char reinitialize = 0;
long int tmp;
pColorPacket filter_output;
if (init) // Initialization
{
if(m_mean_filter_output_old)
delete (char *)m_mean_filter_output_old;
m_mean_filter_output_old = NULL;
if(m_mean_values)
delete (char *)m_mean_values;
m_mean_values = NULL;
if(m_mean_sums)
delete (char *)m_mean_sums;
m_mean_sums = NULL;
return (NULL);
}
if(!m_mean_filter_output_old || (m_mean_filter_output_old->numColors!=filter_input->numColors)) {
delete m_mean_filter_output_old;
AllocColorPacket(m_mean_filter_output_old, filter_input->numColors);
ZeroColorPacket(m_mean_filter_output_old);
}
if(!m_mean_values || (m_mean_values->numColors!=filter_input->numColors)) {
delete m_mean_values;
AllocColorPacket(m_mean_values, filter_input->numColors);
ZeroColorPacket(m_mean_values);
}
if(!m_mean_sums || (m_mean_sums->numColors!=filter_input->numColors)) {
delete m_mean_sums;
AllocLongColorPacket(m_mean_sums, filter_input->numColors);
ZeroLongColorPacket(m_mean_sums);
}
AllocColorPacket(filter_output, filter_input->numColors);
int AtmoSetup_Filter_MeanLength = m_pAtmoConfig->getLiveViewFilter_MeanLength();
int AtmoSetup_Filter_PercentNew = m_pAtmoConfig->getLiveViewFilter_PercentNew();
int AtmoSetup_Filter_MeanThreshold = m_pAtmoConfig->getLiveViewFilter_MeanThreshold();
// if filter_length has changed
if (filter_length_old != AtmoSetup_Filter_MeanLength)
{
// force reinitialization of the filter
reinitialize = 1;
}
filter_length_old = AtmoSetup_Filter_MeanLength;
if (filter_length_old < 20) filter_length_old = 20; // avoid division by 0
for (int zone = 0; zone < filter_input->numColors; zone++)
{
// calculate the mean-value filters
m_mean_sums->longZone[zone].r +=
(long int)(filter_input->zone[zone].r - m_mean_values->zone[zone].r); // red
tmp = m_mean_sums->longZone[zone].r / ((long int)filter_length_old / 20);
if(tmp<0) tmp = 0; else { if(tmp>255) tmp = 255; }
m_mean_values->zone[zone].r = (unsigned char)tmp;
m_mean_sums->longZone[zone].g +=
(long int)(filter_input->zone[zone].g - m_mean_values->zone[zone].g); // green
tmp = m_mean_sums->longZone[zone].g / ((long int)filter_length_old / 20);
if(tmp<0) tmp = 0; else { if(tmp>255) tmp = 255; }
m_mean_values->zone[zone].g = (unsigned char)tmp;
m_mean_sums->longZone[zone].b +=
(long int)(filter_input->zone[zone].b - m_mean_values->zone[zone].b); // blue
tmp = m_mean_sums->longZone[zone].b / ((long int)filter_length_old / 20);
if(tmp<0) tmp = 0; else { if(tmp>255) tmp = 255; }
m_mean_values->zone[zone].b = (unsigned char)tmp;
// check, if there is a jump -> check if differences between actual values and filter values are too big
long int dist; // distance between the two colors in the 3D RGB space
dist = (m_mean_values->zone[zone].r - filter_input->zone[zone].r) *
(m_mean_values->zone[zone].r - filter_input->zone[zone].r) +
(m_mean_values->zone[zone].g - filter_input->zone[zone].g) *
(m_mean_values->zone[zone].g - filter_input->zone[zone].g) +
(m_mean_values->zone[zone].b - filter_input->zone[zone].b) *
(m_mean_values->zone[zone].b - filter_input->zone[zone].b);
/*
if (dist > 0) { dist = (long int)sqrt((double)dist); }
avoid sqrt(0) (TODO: necessary?)
I think its cheaper to calculate the square of something ..? insteas geting the square root?
*/
double distMean = ((double)AtmoSetup_Filter_MeanThreshold * 3.6f);
distMean = distMean * distMean;
/*
compare calculated distance with the filter threshold
if ((dist > (long int)((double)AtmoSetup.Filter_MeanThreshold * 3.6f)) || ( reinitialize == 1))
*/
if ((dist > distMean) || ( reinitialize == 1))
{
// filter jump detected -> set the long filters to the result of the short filters
filter_output->zone[zone] = m_mean_values->zone[zone] = filter_input->zone[zone];
m_mean_sums->longZone[zone].r = filter_input->zone[zone].r *
(filter_length_old / 20);
m_mean_sums->longZone[zone].g = filter_input->zone[zone].g *
(filter_length_old / 20);
m_mean_sums->longZone[zone].b = filter_input->zone[zone].b *
(filter_length_old / 20);
}
else
{
// apply an additional percent filter and return calculated values
filter_output->zone[zone].r = (m_mean_values->zone[zone].r *
(100-AtmoSetup_Filter_PercentNew) +
m_mean_filter_output_old->zone[zone].r * AtmoSetup_Filter_PercentNew) / 100;
filter_output->zone[zone].g = (m_mean_values->zone[zone].g *
(100-AtmoSetup_Filter_PercentNew) +
m_mean_filter_output_old->zone[zone].g * AtmoSetup_Filter_PercentNew) / 100;
filter_output->zone[zone].b = (m_mean_values->zone[zone].b *
(100-AtmoSetup_Filter_PercentNew) +
m_mean_filter_output_old->zone[zone].b * AtmoSetup_Filter_PercentNew) / 100;
}
}
CopyColorPacket(filter_output, m_mean_filter_output_old);
delete (char *)filter_input;
return(filter_output);
}