-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.hpp
198 lines (187 loc) · 5.79 KB
/
filters.hpp
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
#ifndef FILTERS_HEADER
#define FILTERS_HEADER
#include "numerics.hpp"
#include "filter_utils.hpp"
uint8_t* filter_all_ffv1(uint8_t* in_bytes, uint32_t range, uint32_t width, uint32_t height){
uint8_t* filtered = new uint8_t[width * height];
filtered[0] = in_bytes[0];//TL prediction
if(range == 256){
for(size_t i=1;i<width;i++){
filtered[i] = in_bytes[i] - in_bytes[i - 1];//top edge is always left-predicted
}
for(size_t y=1;y<height;y++){
filtered[y * width] = in_bytes[y * width] - in_bytes[(y-1) * width];//left edge is always top-predicted
for(size_t i=1;i<width;i++){
uint8_t L = in_bytes[y * width + i - 1];
uint8_t TL = in_bytes[(y-1) * width + i - 1];
uint8_t T = in_bytes[(y-1) * width + i];
filtered[(y * width) + i] = (
in_bytes[y * width + i] - ffv1(
L,
T,
TL
)
);
}
}
}
else{
for(size_t i=1;i<width;i++){
filtered[i] = sub_mod(in_bytes[i],in_bytes[i - 1],range);//top edge is always left-predicted
}
for(size_t y=1;y<height;y++){
filtered[y * width] = sub_mod(in_bytes[y * width],in_bytes[(y-1) * width],range);//left edge is always top-predicted
for(size_t i=1;i<width;i++){
uint8_t L = in_bytes[y * width + i - 1];
uint8_t TL = in_bytes[(y-1) * width + i - 1];
uint8_t T = in_bytes[(y-1) * width + i];
filtered[(y * width) + i] = sub_mod(
in_bytes[y * width + i],
ffv1(
L,
T,
TL
),
range
);
}
}
}
return filtered;
}
uint8_t* filter_all_left(uint8_t* in_bytes, uint32_t range, uint32_t width, uint32_t height){
uint8_t* filtered = new uint8_t[width * height];
filtered[0] = in_bytes[0];//TL prediction
if(range == 256){
for(size_t i=1;i<width;i++){
filtered[i] = in_bytes[i] - in_bytes[i - 1];//top edge is always left-predicted
}
for(size_t y=1;y<height;y++){
filtered[y * width] = in_bytes[y * width] - in_bytes[(y-1) * width];//left edge is always top-predicted
for(size_t i=1;i<width;i++){
filtered[(y * width) + i] = in_bytes[y * width + i] - in_bytes[y * width + i - 1];
}
}
}
else{
for(size_t i=1;i<width;i++){
filtered[i] = sub_mod(in_bytes[i],in_bytes[i - 1],range);//top edge is always left-predicted
}
for(size_t y=1;y<height;y++){
filtered[y * width] = sub_mod(
in_bytes[y * width],
in_bytes[(y-1) * width],
range
);//left edge is always top-predicted
for(size_t i=1;i<width;i++){
filtered[(y * width) + i] = sub_mod(
in_bytes[y * width + i],
in_bytes[y * width + i - 1],
range
);
}
}
}
return filtered;
}
uint8_t* filter_all_top(uint8_t* in_bytes, uint32_t range, uint32_t width, uint32_t height){
uint8_t* filtered = new uint8_t[width * height];
filtered[0] = in_bytes[0];//TL prediction
if(range == 256){
for(size_t i=1;i<width;i++){
filtered[i] = in_bytes[i] - in_bytes[i - 1];//top edge is always left-predicted
}
for(size_t y=1;y<height;y++){
for(size_t i=0;i<width;i++){
filtered[(y * width) + i] = in_bytes[y * width + i] - in_bytes[(y-1) * width + i];
}
}
}
else{
for(size_t i=1;i<width;i++){
filtered[i] = sub_mod(in_bytes[i],in_bytes[i - 1],range);//top edge is always left-predicted
}
for(size_t y=1;y<height;y++){
for(size_t i=0;i<width;i++){
filtered[(y * width) + i] = sub_mod(
in_bytes[y * width + i],
in_bytes[(y-1) * width + i],
range
);
}
}
}
return filtered;
}
uint8_t* filter_all_generic_noD(uint8_t* in_bytes, uint32_t width, uint32_t height,int a,int b,int c){
uint8_t* filtered = new uint8_t[width * height];
uint8_t sum = a + b + c;
uint8_t halfsum = sum >> 1;
filtered[0] = in_bytes[0];//TL prediction
for(size_t i=1;i<width;i++){
filtered[i] = in_bytes[i] - in_bytes[i - 1];//top edge is always left-predicted
}
for(size_t y=1;y<height;y++){
filtered[y * width] = in_bytes[y * width] - in_bytes[(y-1) * width];//left edge is always top-predicted
for(size_t i=1;i<width;i++){
int L = in_bytes[y * width + i - 1];
int TL = in_bytes[(y-1) * width + i - 1];
int T = in_bytes[(y-1) * width + i];
filtered[(y * width) + i] = in_bytes[y * width + i] - clamp(
(
a*L + b*T + c*TL + halfsum
)/sum
);
}
}
return filtered;
}
uint8_t* filter_all_generic(uint8_t* in_bytes, uint32_t width, uint32_t height,int a,int b,int c,int d){
uint8_t* filtered = new uint8_t[width * height];
uint8_t sum = a + b + c + d;
uint8_t halfsum = sum >> 1;
filtered[0] = in_bytes[0];//TL prediction
for(size_t i=1;i<width;i++){
filtered[i] = in_bytes[i] - in_bytes[i - 1];//top edge is always left-predicted
}
for(size_t y=1;y<height;y++){
filtered[y * width] = in_bytes[y * width] - in_bytes[(y-1) * width];//left edge is always top-predicted
for(size_t i=1;i<width;i++){
int L = in_bytes[y * width + i - 1];
int TL = in_bytes[(y-1) * width + i - 1];
int T = in_bytes[(y-1) * width + i];
int TR = in_bytes[(y-1) * width + i + 1];
filtered[(y * width) + i] = in_bytes[y * width + i] - clamp(
(
a*L + b*T + c*TL + d*TR + halfsum
)/sum
);
}
}
return filtered;
}
uint8_t* filter_all(uint8_t* in_bytes, uint32_t range, uint32_t width, uint32_t height,uint16_t predictor){
if(predictor == 0){
return filter_all_ffv1(in_bytes, range, width, height);
}
else if(predictor == 0b0001000011010000){
return filter_all_left(in_bytes, range, width, height);
}
else if(predictor == 0b0000000111010000){
return filter_all_top(in_bytes, range, width, height);
}
else{
int a = (predictor & 0b1111000000000000) >> 12;
int b = (predictor & 0b0000111100000000) >> 8;
int c = (int)((predictor & 0b0000000011110000) >> 4) - 13;
int d = (predictor & 0b0000000000001111);
//printf("abcd %d %d %d %d\n",a,b,c,d);
if(d == 0){
return filter_all_generic_noD(in_bytes, width, height,a,b,c);
}
else{
return filter_all_generic(in_bytes, width, height,a,b,c,d);
}
}
}
#endif //FILTERS