-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwavelet.cpp
More file actions
350 lines (298 loc) · 12.5 KB
/
wavelet.cpp
File metadata and controls
350 lines (298 loc) · 12.5 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "wavelet.h"
#include <math.h>
#include <zlib.h>
#include <string.h>
#include "utils.h"
namespace wavelet
{
const float g_d2[] = { 1.0f / sqrtf( 2.0f ), 1.0f / sqrtf( 2.0f ) };
const float g_d4[] = { 0.6830127f / sqrtf( 2.0f ), 1.183027f / sqrtf( 2.0f ), 0.3169873f / sqrtf( 2.0f ), -0.183027f / sqrtf( 2.0f ) };
const float g_d6[] = { 0.47046721f / sqrtf( 2.0f ), 1.14111692f / sqrtf( 2.0f ), 0.650365f / sqrtf( 2.0f ), -0.19093442 / sqrtf( 2.0f ),
-0.12083221f / sqrtf( 2.0f ), 0.0498175 / sqrtf( 2.0f ) };
const float g_d8[] = { 0.32580343f / sqrtf( 2.0f ), 1.01094572f / sqrtf( 2.0f ), 0.8922014f / sqrtf( 2.0f ), -0.03967503f / sqrtf( 2.0f ),
-0.26450717f / sqrtf( 2.0f ), 0.0436163f / sqrtf( 2.0f ), 0.0465036f / sqrtf( 2.0f ), -0.01498699f / sqrtf( 2.0f ) };
const int32_t g_waveletCoeffsNum[ WAVELET_COUNT ] = { 2, 4, 6, 8 };
const uint32_t MIN_WIDTH_HEIGHT = 16;
typedef int16_t quant_t;
//
struct CompressedHeader
{
uint32_t yCompLength;
uint32_t cbCompLength;
uint32_t crCompLength;
WAVELET wavelet;
uint32_t Ywidth;
uint32_t Yheight;
uint32_t CbCrwidth;
uint32_t CbCrheight;
};
//
void GetHpfCoeffs( const float* cl, int32_t n, float* ch )
{
float sign = -1.0f;
for( int32_t i = 0; i < n; i++ )
{
sign *= -1.0f;
ch[ i ] = sign * cl[ n - i - 1 ];
}
}
//
void GetICoeffs( const float* cl, const float* ch, int32_t n, float* icl, float* ich )
{
for( int32_t k = 0; k < n; k += 2 )
{
ich[ k + 0 ] = cl[ n - k - 1 ];
icl[ k + 0 ] = cl[ n - k - 2 ];
ich[ k + 1 ] = ch[ n - k - 1 ];
icl[ k + 1 ] = ch[ n - k - 2 ];
}
}
//
void DWT1( float* inData, float* outData, int32_t dataLen, int32_t width, const float* cl, const float* ch, int32_t coeffsNum, int32_t delta )
{
for( int32_t k = 0; k < dataLen; k += 2 )
{
float lf = 0.0f;
float hf = 0.0f;
for( int32_t i = 0; i < coeffsNum; i++ )
{
int32_t index = k + i - delta;
index = index >= 0 ? index : dataLen + index;
lf += inData[ ( index % dataLen ) * width ] * cl[ i ];
hf += inData[ ( index % dataLen ) * width ] * ch[ i ];
}
outData[ ( k + 0 ) * width ] = lf;
outData[ ( k + 1 ) * width ] = hf;
}
}
//
void DWT2_recursive( float* imageFloat, float* temp, uint32_t width, uint32_t height, uint32_t originWidth, const float* cl, const float* ch, int32_t coeffsNum )
{
if( width < MIN_WIDTH_HEIGHT )
return;
for( uint32_t i = 0; i < height; i++ )
DWT1( &imageFloat[ i * originWidth ], &temp[ i * originWidth ], width, 1, cl, ch, coeffsNum, 0 );
for( uint32_t i = 0; i < width; i++ )
DWT1( &temp[ i ], &imageFloat[ i ], height, originWidth, cl, ch, coeffsNum, 0 );
for( uint32_t i = 0; i < height; i++ )
{
for( uint32_t j = 0; j < width; j++ )
{
uint32_t k = ( j % 2 ) ? ( width / 2 + j / 2 ) : ( j / 2 );
temp[ i * width + k ] = imageFloat[ i * width + j ];
}
}
for( uint32_t j = 0; j < width; j++ )
{
for( uint32_t i = 0; i < height; i++ )
{
uint32_t k = ( i % 2 ) ? ( height / 2 + i / 2 ) : ( i / 2 );
imageFloat[ k * width + j ] = temp[ i * width + j ];
}
}
DWT2_recursive( imageFloat, temp, width / 2, height / 2, originWidth, cl, ch, coeffsNum );
}
//
void IDWT2_recursive( float* imageFloat, float* temp, uint32_t width, uint32_t height, uint32_t originWidth, const float* icl, const float* ich, int32_t coeffsNum )
{
if( width > originWidth )
return;
for( uint32_t j = 0; j < width; j++ )
{
for( uint32_t i = 0; i < height; i++ )
{
uint32_t k = ( i % 2 ) ? ( height / 2 + i / 2 ) : ( i / 2 );
temp[ i * width + j ] = imageFloat[ k * width + j ];
}
}
for( uint32_t i = 0; i < height; i++ )
{
for( uint32_t j = 0; j < width; j++ )
{
uint32_t k = ( j % 2 ) ? ( width / 2 + j / 2 ) : ( j / 2 );
imageFloat[ i * width + j ] = temp[ i * width + k ];
}
}
for( uint32_t i = 0; i < width; i++ )
DWT1( &imageFloat[ i ], &temp[ i ], height, originWidth, icl, ich, coeffsNum, coeffsNum - 2 );
for( uint32_t i = 0; i < height; i++ )
DWT1( &temp[ i * originWidth ], &imageFloat[ i * originWidth ], width, 1, icl, ich, coeffsNum, coeffsNum - 2 );
IDWT2_recursive( imageFloat, temp, width * 2, height * 2, originWidth, icl, ich, coeffsNum );
}
//
void QuantizationThreshold( const float* inImageFloat, quant_t* outImageInt, uint32_t length, uint32_t threshold )
{
for( uint32_t i = 0; i < length; i++ )
outImageInt[ i ] = fabs( inImageFloat[ i ] ) > threshold ? inImageFloat[ i ] : 0.0f;
}
//
void DequantizationThreshold( const quant_t* inImageInt, float* outImageFloat, uint32_t length )
{
for( uint32_t i = 0; i < length; i++ )
outImageFloat[ i ] = inImageInt[ i ];
}
//
uint8_t* DWT2Compress( uint8_t** inData, uint32_t width, uint32_t height, uint8_t threshold, WAVELET wavelet, uint32_t& compressedLength )
{
// float data[] = { 0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f };
// float result[ 8 ];
// WAVELET wavelet = WAVELET_D8;
// int32_t coeffsNum = g_waveletCoeffsNum[ wavelet ];
// const float* cl;
// float ch[ 8 ], icl[ 8 ], ich[ 8 ];
// switch( wavelet )
// {
// case( WAVELET_D2 ):
// cl = g_d2;
// break;
// case( WAVELET_D4 ):
// cl = g_d4;
// break;
// case( WAVELET_D6 ):
// cl = g_d6;
// break;
// case( WAVELET_D8 ):
// cl = g_d8;
// break;
// }
// GetHpfCoeffs( cl, coeffsNum, ch );
// GetICoeffs( cl, ch, coeffsNum, icl, ich );
// DWT1( data, result, sizeof( data ) / sizeof( data[ 0 ] ), cl, ch, coeffsNum, 0 );
// DWT1( result, data, sizeof( data ) / sizeof( data[ 0 ] ), icl, ich, coeffsNum, coeffsNum - 2 );
// DWT1( data, result, sizeof( data ) / sizeof( data[ 0 ] ), icl, ich, coeffsNum, coeffsNum - 2 );
int32_t coeffsNum = g_waveletCoeffsNum[ wavelet ];
const float* cl;
float ch[ 8 ], icl[ 8 ], ich[ 8 ];
switch( wavelet )
{
case( WAVELET_D2 ):
cl = g_d2;
break;
case( WAVELET_D4 ):
cl = g_d4;
break;
case( WAVELET_D6 ):
cl = g_d6;
break;
case( WAVELET_D8 ):
cl = g_d8;
break;
default:
return nullptr;
}
GetHpfCoeffs( cl, coeffsNum, ch );
GetICoeffs( cl, ch, coeffsNum, icl, ich );
uint32_t length = width * height;
float* imageFloat = new float[ length ];
float* temp = new float[ length ];
quant_t* imageInt = new quant_t[ length ];
uint8_t* compressed = new uint8_t[ length * sizeof( quant_t ) ];
uLongf l_compressedLength = compressBound( length * sizeof( quant_t ) );
for( uint32_t i = 0; i < height; i++ )
{
for( uint32_t j = 0; j < width; j++ )
imageFloat[ i * width + j ] = inData[ j ][ i ];
}
// compress
DWT2_recursive( imageFloat, temp, width, height, width, cl, ch, coeffsNum );
QuantizationThreshold( imageFloat, imageInt, length, threshold );
compress( ( Bytef* )compressed, &l_compressedLength, ( const Bytef* )imageInt, length * sizeof( quant_t ) );
delete[] imageFloat;
delete[] temp;
delete[] imageInt;
compressedLength = l_compressedLength;
return compressed;
}
//
void DWT2Decompress( uint8_t* compressed, uint32_t compressedLength, uint32_t width, uint32_t height, WAVELET wavelet, uint8_t** outImage )
{
int32_t coeffsNum = g_waveletCoeffsNum[ wavelet ];
const float* cl;
float ch[ 8 ], icl[ 8 ], ich[ 8 ];
switch( wavelet )
{
case( WAVELET_D2 ):
cl = g_d2;
break;
case( WAVELET_D4 ):
cl = g_d4;
break;
case( WAVELET_D6 ):
cl = g_d6;
break;
case( WAVELET_D8 ):
cl = g_d8;
break;
default:
return;
}
GetHpfCoeffs( cl, coeffsNum, ch );
GetICoeffs( cl, ch, coeffsNum, icl, ich );
uint32_t length = width * height;
float* imageFloat = new float[ length ];
float* temp = new float[ length ];
quant_t* imageInt = new quant_t[ length ];
uLongf decompLength = length * sizeof( quant_t );
// decompress
uncompress( ( Bytef* )imageInt, &decompLength, ( const Bytef* )compressed, compressedLength );
DequantizationThreshold( imageInt, imageFloat, length );
IDWT2_recursive( imageFloat, temp, MIN_WIDTH_HEIGHT, MIN_WIDTH_HEIGHT, width, icl, ich, coeffsNum );
for( uint32_t i = 0; i < height; i++ )
{
for( uint32_t j = 0; j < width; j++ )
outImage[ j ][ i ] = clamp( imageFloat[ i * width + j ], 0.0f, 255.0f );
}
delete[] imageFloat;
delete[] temp;
delete[] imageInt;
}
//
uint8_t* Compress( YCbCr_ubyte& input, WAVELET wavelet, uint8_t threshold, uint32_t& compressedLength )
{
if( input.Ywidth != input.Yheight )
{
// only square images
return nullptr;
}
uint32_t yCompLength = 0;
uint32_t cbCompLength = 0;
uint32_t crCompLength = 0;
uint8_t* yComp = DWT2Compress( input.Y, input.Ywidth, input.Yheight, threshold, wavelet, yCompLength );
uint8_t* cbComp = DWT2Compress( input.Cb, input.CbCrwidth, input.CbCrheight, threshold, wavelet, cbCompLength );
uint8_t* crComp = DWT2Compress( input.Cr, input.CbCrwidth, input.CbCrheight, threshold, wavelet, crCompLength );
compressedLength = yCompLength + cbCompLength + crCompLength + sizeof( CompressedHeader );
uint8_t* ret = new uint8_t[ compressedLength ];
CompressedHeader* header = ( CompressedHeader* )ret;
header->yCompLength = yCompLength;
header->cbCompLength = cbCompLength;
header->crCompLength = crCompLength;
header->wavelet = wavelet;
header->Ywidth = input.Ywidth;
header->Yheight = input.Yheight;
header->CbCrwidth = input.CbCrwidth;
header->CbCrheight = input.CbCrheight;
memcpy( ret + sizeof( CompressedHeader ), yComp, yCompLength );
memcpy( ret + sizeof( CompressedHeader ) + yCompLength, cbComp, cbCompLength );
memcpy( ret + sizeof( CompressedHeader ) + yCompLength + cbCompLength, crComp, crCompLength );
delete[] yComp;
delete[] cbComp;
delete[] crComp;
return ret;
}
//
void Decompress( uint8_t* compressed, YCbCr_ubyte& output )
{
CompressedHeader* header = ( CompressedHeader* )compressed;
uint32_t yCompLength = header->yCompLength;
uint32_t cbCompLength = header->cbCompLength;
uint32_t crCompLength = header->crCompLength;
WAVELET wavelet = header->wavelet;
output.Ywidth = header->Ywidth;
output.Yheight = header->Yheight;
output.CbCrwidth = header->CbCrwidth;
output.CbCrheight = header->CbCrheight;
DWT2Decompress( compressed + sizeof( CompressedHeader ), yCompLength, output.Ywidth, output.Yheight, wavelet, output.Y );
DWT2Decompress( compressed + sizeof( CompressedHeader ) + yCompLength, cbCompLength, output.CbCrwidth, output.CbCrheight, wavelet, output.Cb );
DWT2Decompress( compressed + sizeof( CompressedHeader ) + yCompLength + cbCompLength, crCompLength, output.CbCrwidth, output.CbCrheight, wavelet, output.Cr );
}
} // wavelet