-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cpp
88 lines (77 loc) · 2.18 KB
/
Utils.cpp
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
#include "Utils.h"
vector<pair<int, int> >* zeroSeqCodind(int seq[64]){
int ind = 1, ct = 0;
int zerBlocks = 0;
vector<pair<int, int> > *res = new vector<pair<int, int> >();
while(ind < 64){
if(seq[ind] == 0) ct++;
else{
while(zerBlocks > 0){
res->push_back(pair<int, int> (0, 15));
zerBlocks--;
}
res->push_back(pair<int, int> (seq[ind], ct));
ct = 0;
}
if(ct == 15){zerBlocks++; ct = 0;}
ind++;
}
if(ct != 0)res->push_back(pair<int, int> (0, 0));
return res;
}
pair<int, int> getCode(int numb){
if(numb == 0)return pair<int, int>(0, 0);
int buf = numb>0? numb: -numb;
int size = 0;
while(buf > 0){
size++;
buf = buf>>1;
}
if(size == 16)return pair<int, int> (16, 0);
buf = 1<<size;
int code;
if(numb<0){
buf = -buf;
buf++;
code = numb - buf;
}else code = numb;
return pair<int, int> (size, code);
}
pair<int, int> getUnaryCode(int length){
if(length == 16)return pair<int, int>(16, 0xFF);
int res = 0;
for(int i = 0; i < length; i++) res = (res<<1) + 1;
res = res<<1;
return pair<int, int>(length + 1, res);
}
int* toIntMtr(float mtr[64]){
int* res = new int[64];
for(int i = 0; i < 64; i++) res[i] = (int)(mtr[i] + 0.5);
return res;
}
//int encodeMatrix(float mtr[64], CodeWriter* writer, ComponentEncoders* encoders, int prevDc){
// encodeDC(mtr[0] - prevDc, writer, encoders->dcEncoder);
// int* iMtr = toIntMtr(mtr);
// vector<pair<int, int> > * vec = zeroSeqCodind(iMtr);
// for(int i = 0; i < vec->size(); i++){
// writeAC((*vec)[i], writer, encoders->dcEncoder);
// }
// delete iMtr;
// return mtr[0];
//}
vector<int>* numbers(vector<pair<pair<int, int>, int> >* table){
vector<int>* res = new vector<int>();
int length = 1;
int ct = 0;
for(int i = 0; i < table->size(); i++){
if((*table)[i].first.first == length) ct++;
else{
res->push_back(ct);
length++;
ct = 0;
i--;
}
}
if(ct != 0)res->push_back(ct);
return res;
}