bz_io.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*******************************************************************************
  2. License:
  3. This software and/or related materials was developed at the National Institute
  4. of Standards and Technology (NIST) by employees of the Federal Government
  5. in the course of their official duties. Pursuant to title 17 Section 105
  6. of the United States Code, this software is not subject to copyright
  7. protection and is in the public domain.
  8. This software and/or related materials have been determined to be not subject
  9. to the EAR (see Part 734.3 of the EAR for exact details) because it is
  10. a publicly available technology and software, and is freely distributed
  11. to any interested party with no licensing requirements. Therefore, it is
  12. permissible to distribute this software as a free download from the internet.
  13. Disclaimer:
  14. This software and/or related materials was developed to promote biometric
  15. standards and biometric technology testing for the Federal Government
  16. in accordance with the USA PATRIOT Act and the Enhanced Border Security
  17. and Visa Entry Reform Act. Specific hardware and software products identified
  18. in this software were used in order to perform the software development.
  19. In no case does such identification imply recommendation or endorsement
  20. by the National Institute of Standards and Technology, nor does it imply that
  21. the products and equipment identified are necessarily the best available
  22. for the purpose.
  23. This software and/or related materials are provided "AS-IS" without warranty
  24. of any kind including NO WARRANTY OF PERFORMANCE, MERCHANTABILITY,
  25. NO WARRANTY OF NON-INFRINGEMENT OF ANY 3RD PARTY INTELLECTUAL PROPERTY
  26. or FITNESS FOR A PARTICULAR PURPOSE or for any purpose whatsoever, for the
  27. licensed product, however used. In no event shall NIST be liable for any
  28. damages and/or costs, including but not limited to incidental or consequential
  29. damages of any kind, including economic damage or injury to property and lost
  30. profits, regardless of whether NIST shall be advised, have reason to know,
  31. or in fact shall know of the possibility.
  32. By using this software, you agree to bear all risk relating to quality,
  33. use and performance of the software and/or related materials. You agree
  34. to hold the Government harmless from any claim arising from your use
  35. of the software.
  36. *******************************************************************************/
  37. /***********************************************************************
  38. LIBRARY: FING - NIST Fingerprint Systems Utilities
  39. FILE: BZ_IO.C
  40. ALGORITHM: Allan S. Bozorth (FBI)
  41. MODIFICATIONS: Michael D. Garris (NIST)
  42. Stan Janet (NIST)
  43. DATE: 09/21/2004
  44. UPDATED: 01/11/2012 by Kenneth Ko
  45. UPDATED: 03/08/2012 by Kenneth Ko
  46. UPDATED: 07/10/2014 by Kenneth Ko
  47. Contains routines responsible for supporting command line
  48. processing, file and data input to, and output from the
  49. Bozorth3 fingerprint matching algorithm.
  50. ***********************************************************************
  51. ROUTINES:
  52. #cat: set_probe_filename - stores the name of the current probe file
  53. #cat: being processed
  54. #cat: set_gallery_filename - stores the name of the current gallery file
  55. #cat: being processed
  56. #cat: get_probe_filename - retrieves the name of the current probe file
  57. #cat: being processed
  58. #cat: get_gallery_filename - retrieves the name of the current gallery
  59. #cat: file being processed
  60. #cat: bz_load - loads the contents of the specified XYT file into
  61. #cat: structured memory
  62. ***********************************************************************/
  63. #include <string.h>
  64. #include <ctype.h>
  65. #include <sys/time.h>
  66. #include "bozorth.h"
  67. /***********************************************************************/
  68. /* Used by the following set* and get* routines */
  69. static char * pfile;
  70. static char * gfile;
  71. /***********************************************************************/
  72. void set_probe_filename( char * filename ){
  73. pfile = filename;
  74. }
  75. /***********************************************************************/
  76. void set_gallery_filename( char * filename ){
  77. gfile = filename;
  78. }
  79. /***********************************************************************/
  80. char * get_probe_filename( void ){
  81. return pfile;
  82. }
  83. /***********************************************************************/
  84. char * get_gallery_filename( void ){
  85. return gfile;
  86. }
  87. /************************************************************************
  88. Load a 3-4 column (X,Y,T[,Q]) set of minutiae from the specified file
  89. and return a XYT sturcture.
  90. Row 3's value is an angle which is normalized to the interval (-180,180].
  91. A maximum of MAX_BOZORTH_MINUTIAE minutiae can be returned -- fewer if
  92. "max_minutiae" is smaller. If the file contains more minutiae than are
  93. to be returned, the highest-quality minutiae are returned.
  94. *************************************************************************/
  95. /***********************************************************************/
  96. struct xyt_struct * bz_load( const char * xyt_file ){
  97. int nminutiae;
  98. int m;
  99. FILE * fp;
  100. struct xyt_struct * xyt_s;
  101. /* Temporary lists to store all the minutaie from a file */
  102. struct xytq_struct xytq_s;
  103. char xyt_line[ MAX_LINE_LENGTH ];
  104. fp = fopen( xyt_file, "r" );
  105. if ( !fp ){
  106. printf( "ERROR: fopen() of minutiae file \"%s\" failed: %s\n", xyt_file, strerror(errno) );
  107. return NULL;
  108. }
  109. nminutiae = 0;
  110. while( fgets(xyt_line, sizeof xyt_line, fp) ) {
  111. m = sscanf( xyt_line, "%d %d %d %d", &xytq_s.xcol[nminutiae], &xytq_s.ycol[nminutiae], &xytq_s.thetacol[nminutiae], &xytq_s.qualitycol[nminutiae] );
  112. if ( m == 3 ) xytq_s.qualitycol[nminutiae] = 1;
  113. else if( m != 4 ){
  114. printf( "ERROR: sscanf() failed on line %u in minutiae file \"%s\"\n", nminutiae+1, xyt_file );
  115. fclose(fp);
  116. return NULL;
  117. }
  118. ++nminutiae;
  119. if ( nminutiae == MAX_FILE_MINUTIAE ) break;
  120. }
  121. fclose(fp);
  122. xytq_s.nrows = nminutiae;
  123. xyt_s = bz_prune( &xytq_s );
  124. return xyt_s;
  125. }
  126. /************************************************************************
  127. Load a XYTQ structure and return a XYT struct.
  128. Row 3's value is an angle which is normalized to the interval (-180,180].
  129. A maximum of MAX_BOZORTH_MINUTIAE minutiae can be returned -- fewer if
  130. "max_minutiae" is smaller. If the file contains more minutiae than are
  131. to be returned, the highest-quality minutiae are returned.
  132. *************************************************************************/
  133. struct xyt_struct * bz_prune( struct xytq_struct *xytq_s ){
  134. int nminutiae;
  135. //int index;
  136. int j;
  137. //int m;
  138. struct xyt_struct * xyt_s;
  139. int * xptr;
  140. int * yptr;
  141. int * tptr;
  142. int * qptr;
  143. struct minutiae_struct c[MAX_FILE_MINUTIAE];
  144. int xvals_lng[MAX_FILE_MINUTIAE],
  145. yvals_lng[MAX_FILE_MINUTIAE],
  146. tvals_lng[MAX_FILE_MINUTIAE],
  147. qvals_lng[MAX_FILE_MINUTIAE];
  148. int order[MAX_FILE_MINUTIAE];
  149. int xvals[MAX_BOZORTH_MINUTIAE],
  150. yvals[MAX_BOZORTH_MINUTIAE],
  151. tvals[MAX_BOZORTH_MINUTIAE],
  152. qvals[MAX_BOZORTH_MINUTIAE];
  153. //char xyt_line[ MAX_LINE_LENGTH ];
  154. #define C1 0
  155. #define C2 1
  156. int i;
  157. nminutiae = xytq_s->nrows;
  158. for ( i=0; i<nminutiae; i++ ) {
  159. xvals_lng[i] = xytq_s->xcol[i];
  160. yvals_lng[i] = xytq_s->ycol[i];
  161. if ( xytq_s->thetacol[i] > 180 ) tvals_lng[i] = xytq_s->thetacol[i] - 360;
  162. else tvals_lng[i] = xytq_s->thetacol[i];
  163. qvals_lng[i] = xytq_s->qualitycol[i];
  164. }
  165. if ( nminutiae > MAX_BOZORTH_MINUTIAE ) {
  166. printf( "WARNING: bz_prune(): trimming minutiae to the %d of highest quality\n", MAX_BOZORTH_MINUTIAE );
  167. if ( sort_order_decreasing( qvals_lng, nminutiae, order )) {
  168. printf( "ERROR: sort failed and returned on error\n" );
  169. return NULL;
  170. }
  171. for ( j = 0; j < nminutiae; j++ ) {
  172. if ( j == 0 ) continue;
  173. if ( qvals_lng[order[j]] > qvals_lng[order[j-1]] ) {
  174. printf( "ERROR: sort failed: j=%d; qvals_lng[%d] > qvals_lng[%d]\n", j, order[j], order[j-1] );
  175. return NULL;
  176. }
  177. }
  178. for ( j = 0; j < MAX_BOZORTH_MINUTIAE; j++ ) {
  179. xvals[j] = xvals_lng[order[j]];
  180. yvals[j] = yvals_lng[order[j]];
  181. tvals[j] = tvals_lng[order[j]];
  182. qvals[j] = qvals_lng[order[j]];
  183. }
  184. if ( C1 ) {
  185. qsort( (void *) &c, (size_t) nminutiae, sizeof(struct minutiae_struct), sort_quality_decreasing );
  186. for ( j = 0; j < nminutiae; j++ ) {
  187. if ( j > 0 && c[j].col[3] > c[j-1].col[3] ) {
  188. printf( "ERROR: sort failed: c[%d].col[3] > c[%d].col[3]\n", j, j-1 );
  189. return NULL;
  190. }
  191. }
  192. }
  193. xptr = xvals;
  194. yptr = yvals;
  195. tptr = tvals;
  196. qptr = qvals;
  197. nminutiae = MAX_BOZORTH_MINUTIAE;
  198. } else {
  199. xptr = xvals_lng;
  200. yptr = yvals_lng;
  201. tptr = tvals_lng;
  202. qptr = qvals_lng;
  203. }
  204. for ( j=0; j < nminutiae; j++ ) {
  205. c[j].col[0] = xptr[j];
  206. c[j].col[1] = yptr[j];
  207. c[j].col[2] = tptr[j];
  208. c[j].col[3] = qptr[j];
  209. }
  210. qsort( (void *) &c, (size_t) nminutiae, sizeof(struct minutiae_struct), sort_x_y );
  211. xyt_s = (struct xyt_struct *) malloc( sizeof( struct xyt_struct ) );
  212. if ( xyt_s == NULL ) {
  213. printf( "ERROR: malloc() failure of xyt_struct.");
  214. return NULL;
  215. }
  216. for ( j = 0; j < nminutiae; j++ ) {
  217. xyt_s->xcol[j] = c[j].col[0];
  218. xyt_s->ycol[j] = c[j].col[1];
  219. xyt_s->thetacol[j] = c[j].col[2];
  220. }
  221. xyt_s->nrows = nminutiae;
  222. return xyt_s;
  223. }