forked from stupel/Matcher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatcher_isoconverter.cpp
290 lines (231 loc) · 9.07 KB
/
matcher_isoconverter.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
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
#include "matcher_isoconverter.h"
MatcherISOConverter::MatcherISOConverter(QObject *parent) : QObject(parent)
{
this->_ISO_template = nullptr;
this->templateSize = 0;
}
MatcherISOConverter::MatcherISOConverter(int _fpHeight, int _fpWidth, int _fpQuality, QVector<MINUTIA> _minData) :
fpHeight(_fpHeight),
fpWidth(_fpWidth),
fpQuality(_fpQuality),
minutiaeData(_minData),
_ISO_template(nullptr),
templateSize(0)
{ }
MatcherISOConverter::~MatcherISOConverter()
{
}
// funkcia, ktora prekonvertuje informacie o odtlacku do ISO formatu
// funkcia vrati unsigned char*
unsigned char * MatcherISOConverter::convertToISO()
{
if(this->minutiaeData.size() == 0){
qDebug() << "Load data first.";
return nullptr;
}
int numberOfMinutiae = this->minutiaeData.size();
this->templateSize = HEADER_LENGHT + (FINGER_VIEW_HEADER_LENGTH + (numberOfMinutiae * ISO_MINUTIA_LENGTH) + EXTENDED_DATA_BLOCK_LENGTH);
int byte_offset=0;
/* ------------------ ISO HEADER --------------------*/
// FMR block 4B
unsigned char fmr_block_4B[4] = {'F','M','R',0};
memcpy(_ISO_template+byte_offset, fmr_block_4B, sizeof(unsigned char)*4);
byte_offset +=4;
// Standard version block 4B
unsigned char standard_block_4B[4] = {0x20,0x32,0x30,0};
memcpy(_ISO_template+byte_offset, standard_block_4B, sizeof(unsigned char)*4);
byte_offset +=4;
// Total record length block 4B
int total_record_length = this->templateSize;
*(_ISO_template+byte_offset) = ((unsigned char)(total_record_length >> 24) & 0xff);
byte_offset++;
*(_ISO_template+byte_offset) = ((unsigned char)(total_record_length >> 16) & 0xff);
byte_offset++;
*(_ISO_template+byte_offset) = ((unsigned char)(total_record_length >> 8) & 0xff);
byte_offset++;
*(_ISO_template+byte_offset) = ((unsigned char)(total_record_length) & 0xff);
byte_offset++;
// Capture device 2B
unsigned char capture_block_2B[2] = {0x00,0x00};
memcpy(_ISO_template+byte_offset, capture_block_2B, sizeof(unsigned char)*2);
byte_offset +=2;
// Image size X 2B
int image_size_X = this->fpWidth;
*(_ISO_template+byte_offset) = ((unsigned char)(image_size_X >> 8) & 0xff);
byte_offset++;
*(_ISO_template+byte_offset) = ((unsigned char)(image_size_X ) & 0xff);
byte_offset++;
// Image size Y 2B
int image_size_Y = this->fpHeight;
*(_ISO_template+byte_offset) = ((unsigned char)(image_size_Y >> 8) & 0xff);
byte_offset++;
*(_ISO_template+byte_offset) = ((unsigned char)(image_size_Y ) & 0xff);
byte_offset++;
// Resolution X 2B
int resolution_X = 197; // 500 ppi = 197 pixels/cm
*(_ISO_template+byte_offset) = ((unsigned char)(resolution_X >> 8) & 0xff);
byte_offset++;
*(_ISO_template+byte_offset) = ((unsigned char)(resolution_X ) & 0xff);
byte_offset++;
// Resolution Y 2B
int resolution_Y = 197; // 500 ppi = 197 pixels/cm
*(_ISO_template+byte_offset) = ((unsigned char)(resolution_Y >> 8) & 0xff);
byte_offset++;
*(_ISO_template+byte_offset) = ((unsigned char)(resolution_Y ) & 0xff);
byte_offset++;
// Number of finger views 1B
int numFingViews = 1;
*(_ISO_template+byte_offset) = ((unsigned char)(numFingViews) & 0xff);
byte_offset++;
// Reserved byte 1B
int reservedByte = 0;
*(_ISO_template+byte_offset) = ((unsigned char)(reservedByte) & 0xff);
byte_offset++;
/* ------------------ FINGER VIEW --------------------*/
/* ----- FINGER VIEW HEADER -----*/
// Finger position 1B
int fingerPosition = 0; // 0 az 10
*(_ISO_template+byte_offset) = ((unsigned char)(fingerPosition) & 0xff);
byte_offset++;
// View number and impression type 1B
// view number: 0-15
// impression type: 0-3 alebo 8
int view_and_impression = 0;
*(_ISO_template+byte_offset) = ((unsigned char)(view_and_impression) & 0xff);
byte_offset++;
// Finger quality 1B
int fingerQuality = this->fpQuality; // 0 az 100
*(_ISO_template+byte_offset) = ((unsigned char)(fingerQuality) & 0xff);
byte_offset++;
// Number of minutiae 1B
int numMinutiae = numberOfMinutiae;
*(_ISO_template+byte_offset) = ((unsigned char)(numMinutiae) & 0xff);
byte_offset++;
/* ----- MINUTIAE DATA -----*/
int minutiaX = -1; // 2 bajty (prve 2 bity su minutia type)
int minutiaY = -1; // 2 bajty (prve 2 bity su rezervovane)
int minutiaAngle = -1; // 1 bajt (0-255)
int minutiaQuality = -1; // 1 bajt (0-100, 0 quality not reported)
for(int m = 0; m < numberOfMinutiae; m++){
// kuzlo
minutiaX = std::bitset<16>(
std::bitset<2>(minutiaeData[m].type+1).to_string() +
std::bitset<14>(minutiaeData[m].xy.x()).to_string()
).to_ulong();
*(_ISO_template+byte_offset) = ((unsigned char)(minutiaX >> 8) & 0xff);
byte_offset++;
*(_ISO_template+byte_offset) = ((unsigned char)minutiaX & 0xff);
byte_offset++;
minutiaY = minutiaeData[m].xy.y();
*(_ISO_template+byte_offset) = ((unsigned char)(minutiaY >> 8) & 0xff);
byte_offset++;
*(_ISO_template+byte_offset) = ((unsigned char)minutiaY & 0xff);
byte_offset++;
minutiaAngle = (int)(minutiaeData[m].angle * 180 / M_PI * (256.0/360));
*(_ISO_template+byte_offset) = ((unsigned char)minutiaAngle & 0xff);
byte_offset++;
minutiaQuality = minutiaeData[m].quality;
*(_ISO_template+byte_offset) = ((unsigned char)minutiaQuality & 0xff);
byte_offset++;
}
/* ----- EXTENDED DATA -----*/
// Extended Data Block Length 2B
int extendedLength = 0;
*(_ISO_template+byte_offset) = ((unsigned char)(extendedLength >> 8) & 0xff);
byte_offset++;
*(_ISO_template+byte_offset) = ((unsigned char)(extendedLength) & 0xff);
byte_offset++;
return _ISO_template;
}
int MatcherISOConverter::bitsetToInt(const unsigned char * ISOTemplate, int byte_offset, int byte, bool type)
{
int bin[6] = {256, 512, 1024, 2048, 4096, 8192};
int result = 0;
std::bitset<8> bs = *(ISOTemplate + byte_offset);
if (type == true) {
if (bs[6]) return 1;
else return 0;
}
else if (byte == 2) {
for (int i = 0; i < 6; i++)
if (bs[i]) result += bin[i];
}
else {
result = bs.to_ulong();
}
return result;
}
QVector<MINUTIA> MatcherISOConverter::convertFromISO(const unsigned char * ISOTemplate)
{
QVector<MINUTIA> minutiae;
int x, y, type, quality;
qreal angle;
int byteCnt = 27;
int minutia_num = this->bitsetToInt(ISOTemplate, byteCnt++, 1, false);
for (int i = 0; i < minutia_num; i++) {
type = this->bitsetToInt(ISOTemplate, byteCnt, 1, true);
x = this->bitsetToInt(ISOTemplate, byteCnt++, 2, false);
x += this->bitsetToInt(ISOTemplate, byteCnt++, 1, false);
y = this->bitsetToInt(ISOTemplate, byteCnt++, 2, false);
y += this->bitsetToInt(ISOTemplate, byteCnt++, 1, false);
angle = this->bitsetToInt(ISOTemplate, byteCnt++, 1, false);
angle = angle / (256.0/360) / 180 * M_PI;
quality = this->bitsetToInt(ISOTemplate, byteCnt++, 1, false);
minutiae.push_back({QPoint(x,y), type, angle, quality, QPoint{this->getImageWidth(ISOTemplate), this->getImageHeight(ISOTemplate)}});
}
return minutiae;
}
int MatcherISOConverter::getTemplateSize(const unsigned char * ISOTemplate)
{
int templateSize = 0;
int shift = 24;
for(int i=0; i<4; i++){
templateSize += ((std::bitset<32>(ISOTemplate[8+i])) << shift).to_ulong();
shift -= 8;
}
return templateSize;
}
int MatcherISOConverter::getImageWidth(const unsigned char * ISOTemplate)
{
int imgWidth = 0;
int shift = 8;
for(int i=0; i<2; i++){
imgWidth += ((std::bitset<16>(ISOTemplate[14+i])) << shift).to_ulong();
shift -= 8;
}
return imgWidth;
}
int MatcherISOConverter::getImageHeight(const unsigned char * ISOTemplate)
{
int imgHeight = 0;
int shift = 8;
for(int i=0; i<2; i++){
imgHeight += ((std::bitset<16>(ISOTemplate[16+i])) << shift).to_ulong();
shift -= 8;
}
return imgHeight;
}
void MatcherISOConverter::saveISOToFile(const QString & templateFilename)
{
QFile file(templateFilename);
if(file.open(QIODevice::WriteOnly)){
QDataStream datastream(&file);
for(int i = 0;i <this->templateSize; i++){
datastream << this->_ISO_template[i];
}
}
else{
qDebug("Writing to text file failed.");
}
file.close();
}
void MatcherISOConverter::load(int _fpHeight, int _fpWidth, int _fpQuality, QVector<MINUTIA> _minData)
{
this->minutiaeData.clear();
this->fpHeight = _fpHeight;
this->fpWidth = _fpWidth;
this->fpQuality = _fpQuality;
this->minutiaeData = QVector<MINUTIA>(_minData);
this->templateSize = HEADER_LENGHT + (FINGER_VIEW_HEADER_LENGTH + (minutiaeData.size() * ISO_MINUTIA_LENGTH) + EXTENDED_DATA_BLOCK_LENGTH);
this->_ISO_template = new unsigned char[this->templateSize];
}