Enrollment 28.2.0
Loading...
Searching...
No Matches
ICSTemplate.h
Go to the documentation of this file.
1
9
10#pragma once
11
13#include "enrollment/Export.h"
23#include <array>
24#include <mutex>
25#include <optional>
26#include <string>
27#include <vector>
28
29// cppcoreguidelines-non-private-member-variables-in-classes,misc-non-private-member-variables-in-classes: This is not a
30// problem because we are using them only as read only.
31// cppcoreguidelines-avoid-const-or-ref-data-members: We design te API with const member variables instead of
32// implementing getter methods. This is nicely transformed in C# to properties. Usually our objects are immutable by
33// intention to have no problem in multithreaded applications.
34// NOLINTBEGIN(cppcoreguidelines-non-private-member-variables-in-classes,misc-non-private-member-variables-in-classes,cppcoreguidelines-avoid-const-or-ref-data-members)
35namespace inno
36{
40 */
41 class ICSTemplate : public Serializable
42 {
43 public:
45 const std::vector<uint8_t> icsTemplateData;
46
51 */
52 [[nodiscard]] bool IsICSTemplateBiometricPositionKnown() const
53 {
54 return pos.has_value();
55 }
56
61 */
63 {
64 if (!pos.has_value())
65 {
66 throw EnrollmentRuntimeException("ICSTemplate has unknown biometric position");
67 }
68
69 return pos.value();
70 }
71
75 */
76 [[nodiscard]] std::string GetVersion() const
77 {
78 unsigned int majorVersion = 0;
79 unsigned int minorVersion = 0;
80
81 exec->GetICSTemplateVersion(icsTemplateData.data(),
82 static_cast<uint32_t>(icsTemplateData.size()),
83 &majorVersion,
84 &minorVersion,
85 ThrowOnError{});
86
87 return std::to_string(majorVersion) + "." + std::to_string(minorVersion);
88 }
89
93 */
94 [[nodiscard]] std::shared_ptr<ICSTemplate> Anonymize() const
95 {
96 return std::make_shared<ICSTemplate>(SetPosition(icsTemplateData, 0, exec));
97 }
98
110 */
111 explicit ICSTemplate(std::vector<uint8_t> d)
112 : ICSTemplate(std::move(d), GlobalPrintExecutor::Get())
113 {
114 }
115
128 */
129 ICSTemplate(std::vector<uint8_t> d, std::shared_ptr<PrintExecutor> executor)
130 : icsTemplateData(std::move(d))
131 , exec(std::move(executor))
132 {
133 if (exec == nullptr)
134 {
135 throw NullArgumentException("executor can not be null");
136 }
137 if (icsTemplateData.empty())
138 {
139 throw InvalidTemplateException("ICSTemplate MUST have data");
140 }
141
142 int positionFromData = 0;
143 exec->GetICSTemplatePosition(
144 &positionFromData, icsTemplateData.data(), static_cast<uint32_t>(icsTemplateData.size()), ThrowOnError{});
145 if (positionFromData != 0)
146 {
147 pos = IsoToPrintPosition(positionFromData);
148 }
149 }
150
163 */
164 explicit ICSTemplate(std::vector<uint8_t> d, PrintPosition position)
165 : ICSTemplate(std::move(d), position, GlobalPrintExecutor::Get())
166 {
167 }
168
182 */
183 ICSTemplate(std::vector<uint8_t> d, PrintPosition position, std::shared_ptr<PrintExecutor> executor)
184 : icsTemplateData(SetPosition(std::move(d), PrintToIsoPosition(position), executor))
185 , exec(std::move(executor))
186 , pos(position)
187 {
188 }
189
198 */
199 PrintSimilarityScore SimilarWith(const ICSTemplate& tmpl, const ICSMatcher& matcher) const
200 {
201 return matcher.Match(*this, tmpl);
202 }
203
210 */
211 uint8_t CalculateQuality()
212 {
213 const std::scoped_lock lock(guard);
214
215 if (!templateQuality.has_value())
216 {
217 uint8_t localQuality = 0;
218 exec->GetICSTemplateQuality(
219 icsTemplateData.data(), static_cast<uint32_t>(icsTemplateData.size()), &localQuality, ThrowOnError{});
220 templateQuality = localQuality;
221 }
222 return templateQuality.value();
223 }
224
231 */
232 std::shared_ptr<ISOTemplate> ToISO(const std::shared_ptr<const Print>& print) const
233 {
234 size_t isoLength = 0;
235 std::vector<uint8_t> iso(exec->GetMaxISOTemplateSize());
236
237 exec->ConvertICSToISO(
238 icsTemplateData.data(),
239 icsTemplateData.size(),
240 static_cast<unsigned int>(print->printAttributes.GetCaptureDeviceVendorID()),
241 print->printAttributes.GetCertificationFlag(),
242 print->image->Dpi().ToDPCM(),
243 print->image->Width(),
244 print->image->Height(),
245 static_cast<unsigned int>(PrintToIsoPosition(print->position)),
246 static_cast<unsigned int>(PrintToIsoImpression(print->printAttributes.GetImpression())),
247 iso.data(),
248 &isoLength,
249 ThrowOnError{});
250
251 iso.resize(isoLength);
252 return std::make_shared<ISOTemplate>(std::move(iso));
253 }
254
261 */
262 std::shared_ptr<ANSITemplate> ToANSI(const std::shared_ptr<const Print>& print) const
263 {
264 size_t ansiLength = 0;
265 std::vector<uint8_t> ansi(exec->GetMaxANSITemplateSize());
266
267 exec->ConvertICSToANSI(
268 icsTemplateData.data(),
269 icsTemplateData.size(),
270 static_cast<unsigned int>(print->printAttributes.GetCaptureDeviceVendorID()),
271 print->printAttributes.GetCertificationFlag(),
272 print->image->Dpi().ToDPCM(),
273 print->image->Width(),
274 print->image->Height(),
275 static_cast<unsigned int>(PrintToIsoPosition(print->position)),
276 static_cast<unsigned int>(PrintToIsoImpression(print->printAttributes.GetImpression())),
277 ansi.data(),
278 &ansiLength,
279 ThrowOnError{});
280
281 ansi.resize(ansiLength);
282 return std::make_shared<ANSITemplate>(std::move(ansi));
283 }
284
289 */
290 std::vector<std::shared_ptr<Minutia>> GetMinutiae() const
291 {
292 size_t minutiaeCount = 0;
293 inno_Minutia_out* m = nullptr;
294
295 exec->GetMinutiae(GlobalLogger::Get(),
296 icsTemplateData.data(),
297 static_cast<unsigned int>(icsTemplateData.size()),
298 &m,
299 &minutiaeCount,
300 ThrowOnError{});
301
302 std::vector<std::shared_ptr<Minutia>> minutiae;
303 for (size_t i = 0; i < minutiaeCount; i++)
304 {
305 static constexpr float MaxDegree = 360.0F;
306 static constexpr float MaxValuesInByte = 256.0F;
307 static constexpr float AngleUnit = MaxDegree / MaxValuesInByte;
308 minutiae.emplace_back(std::make_shared<Minutia>(
309 Point{ m[i].x, m[i].y }, // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
310 static_cast<Minutia::Type>(m[i].type), // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
311 AngleUnit * static_cast<float>(m[i].angle), // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
312 m[i].quality)); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
313 };
314
315 exec->FreeMinutiae(m);
316
317 return minutiae;
318 }
319
327 */
328 std::vector<std::shared_ptr<CriticalPoint>> GetCriticalPoints() const
329 {
330 size_t criticalPointsCount = 0;
331 inno_CriticalPoint_out* p = nullptr;
332
333 std::vector<inno_CriticalPoint_out> criticalPoints;
334 auto criticalPointAllocator = +[](void* container, size_t length) -> inno_CriticalPoint_out*
335 {
336 // It is "safe" cast because we forward pointer to vector created in "user cpp space"
337 // (the space where this header is compiled) and the "own cpp space" (out compiled shared library)
338 // wil provide it back for us to the same "user cpp space". We have all spaces under control.
339 // It is used to do not have unecessary copy data from "own cpp space" into "user cpp space".
340 std::vector<inno_CriticalPoint_out>& vec =
341 *static_cast<std::vector<inno_CriticalPoint_out>*>(container);
342 vec.resize(length);
343 return vec.data();
344 };
345
346 exec->GetCriticalPoints(GlobalLogger::Get(),
347 icsTemplateData.data(),
348 static_cast<unsigned int>(icsTemplateData.size()),
349 &p,
350 &criticalPointsCount,
351 criticalPointAllocator,
352 &criticalPoints,
353 ThrowOnError{});
354
355 std::vector<std::shared_ptr<CriticalPoint>> points;
356 for (size_t i = 0; i < criticalPointsCount; i++)
357 {
358 static constexpr float MaxDegree = 360.0F;
359 static constexpr float MaxValuesInByte = 256.0F;
360 static constexpr float AngleUnit = MaxDegree / MaxValuesInByte;
361 points.emplace_back(std::make_shared<CriticalPoint>(
362 Point{ p[i].x, p[i].y }, // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
363 static_cast<CriticalPoint::Type>(
364 p[i].type), // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
365 AngleUnit * static_cast<float>(p[i].angle)) // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic)
366 );
367 };
368 return points;
369 }
371 void WriteTo(Writer& w) const override
372 {
374
375 const std::scoped_lock lock(guard);
376
377 if (templateQuality.has_value())
378 {
379 w.Write("q", templateQuality.value());
380 }
381 }
382
383 ICSTemplate(const ICSTemplate&) = delete;
384 ICSTemplate(ICSTemplate&&) = delete;
385 ICSTemplate& operator=(const ICSTemplate&) = delete;
386 ICSTemplate& operator=(ICSTemplate&&) = delete;
387 ICSTemplate() = delete;
388 virtual ~ICSTemplate() = default;
389
390 private:
391 std::shared_ptr<PrintExecutor> exec;
392 mutable std::mutex guard;
393 std::optional<uint8_t> templateQuality;
394 std::optional<PrintPosition> pos;
395
396 // IEngine does not support position change in template, we have to create new template
397 static std::vector<uint8_t> SetPosition(std::vector<uint8_t> icsTemplateData,
398 int position,
399 const std::shared_ptr<PrintExecutor>& executor)
400 {
401 if (executor == nullptr)
402 {
403 throw NullArgumentException("executor can not be null");
404 }
405 if (icsTemplateData.empty())
406 {
407 throw InvalidTemplateException("ICSTemplate MUST have data");
408 }
409 executor->SetICSTemplatePosition(
410 position, icsTemplateData.data(), static_cast<uint32_t>(icsTemplateData.size()), ThrowOnError{});
411 return icsTemplateData;
412 }
413 };
414} // namespace inno
415// NOLINTEND(cppcoreguidelines-non-private-member-variables-in-classes,misc-non-private-member-variables-in-classes,cppcoreguidelines-avoid-const-or-ref-data-members)
Type
Type of CriticalPoint.
Definition CriticalPoint.h:30
Base exception for all enrollment-sdk runtime exceptions.
Definition EnrollmentException.h:84
static std::shared_ptr< Logger > Get()
Provides current logger.
Definition Logger.h:281
Holds instance of PrintExecutor.
Definition PrintExecutor.h:1300
It is general matcher for ICS templates.
Definition ICSMatcher.h:22
virtual PrintSimilarityScore Match(const ICSTemplate &probe, const ICSTemplate &gallery) const =0
Calculates similarity score of probe and gallery template.
Print template in ICS format used for biometric operations.
Definition ICSTemplate.h:41
std::shared_ptr< ISOTemplate > ToISO(const std::shared_ptr< const Print > &print) const
Convert this ICSTemplate to into ISO/IEC 19794-2 compliant template.
Definition ICSTemplate.h:231
ICSTemplate(std::vector< uint8_t > d)
Create ICSTemplate object GlobalPrintExecutor is used to execute template functionality.
Definition ICSTemplate.h:110
std::vector< std::shared_ptr< Minutia > > GetMinutiae() const
Get minutiae from template.
Definition ICSTemplate.h:289
ICSTemplate(std::vector< uint8_t > d, std::shared_ptr< PrintExecutor > executor)
Create ICSTemplate object Biometric position is loaded from template is possible.
Definition ICSTemplate.h:128
std::vector< std::shared_ptr< CriticalPoint > > GetCriticalPoints() const
Get critical points (core/delta) from template.
Definition ICSTemplate.h:327
std::string GetVersion() const
Provides version information of Innovatrics proprietary algorithm used to create the ICS template.
Definition ICSTemplate.h:75
std::shared_ptr< ICSTemplate > Anonymize() const
Creates ICSTemplate without position.
Definition ICSTemplate.h:93
uint8_t CalculateQuality()
Function calculates a fingerprint template quality.
Definition ICSTemplate.h:210
ICSTemplate(std::vector< uint8_t > d, PrintPosition position, std::shared_ptr< PrintExecutor > executor)
Create ICSTemplate object The template format is validated.
Definition ICSTemplate.h:182
std::shared_ptr< ANSITemplate > ToANSI(const std::shared_ptr< const Print > &print) const
Convert this ICSTemplate to into ANSI/INCITS 378 compliant template.
Definition ICSTemplate.h:261
const std::vector< uint8_t > icsTemplateData
template data
Definition ICSTemplate.h:44
bool IsICSTemplateBiometricPositionKnown() const
It checks if template contains known biometric position.
Definition ICSTemplate.h:51
ICSTemplate(std::vector< uint8_t > d, PrintPosition position)
Create ICSTemplate object GlobalPrintExecutor is used to execute template functionality.
Definition ICSTemplate.h:163
PrintPosition GetICSTemplateBiometricPosition() const
It returns biometric position to which template belongs.
Definition ICSTemplate.h:61
void WriteTo(Writer &w) const override
Write itself into writer.
Definition ICSTemplate.h:370
PrintSimilarityScore SimilarWith(const ICSTemplate &tmpl, const ICSMatcher &matcher) const
It calculates similarity score between this and given ICSTemplate.
Definition ICSTemplate.h:198
Type
Type of Minutia.
Definition Minutia.h:30
Null argument is not allowed.
Definition EnrollmentException.h:156
It holds X and Y coordinates of point.
Definition ImageAttribute.h:312
Provides interface for serialization of all classes.
Definition Writer.h:164
virtual void Write(const char *name, const Serializable &s)=0
Serialize serializable.
void WriteArray(const char *name, const std::vector< uint8_t > &value)
Serialize bytes from vector.
Definition Writer.h:233
static constexpr PrintPosition IsoToPrintPosition(int isoPosition)
Function converts ISO position to PrintPosition.
Definition PrintPosition.h:138
PrintPosition
It is position of finger on hands.
Definition PrintPosition.h:29
unsigned int PrintSimilarityScore
It is similarity score between print Templates.
Definition SimilarityScore.h:28
static constexpr int PrintToIsoPosition(PrintPosition position)
Function converts Print position to ISO position.
Definition PrintPosition.h:204
static constexpr int PrintToIsoImpression(PrintImpression printImpression)
Function converts PrintImpression to ISO impression.
Definition PrintImpression.h:91