From 5e296959697643aab14feac709c8adda8726d582 Mon Sep 17 00:00:00 2001 From: Vasily Khoruzhick Date: Tue, 9 Feb 2016 17:19:39 -0800 Subject: [PATCH] lib: move some functions from vfs5011.c into img.c --- libfprint/drivers/vfs5011.c | 37 ++----------------------------------- libfprint/fp_internal.h | 4 ++++ libfprint/img.c | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/libfprint/drivers/vfs5011.c b/libfprint/drivers/vfs5011.c index fa1dcab..90947d3 100644 --- a/libfprint/drivers/vfs5011.c +++ b/libfprint/drivers/vfs5011.c @@ -252,39 +252,6 @@ static void usb_exchange_async(struct fpi_ssm *ssm, /* ====================== utils ======================= */ -#if VFS5011_LINE_SIZE > INT_MAX/(256*256) -#error We might get integer overflow while computing standard deviation! -#endif - -/* Calculade squared standand deviation */ -static int get_deviation(unsigned char *buf, int size) -{ - int res = 0, mean = 0, i; - for (i = 0; i < size; i++) - mean += buf[i]; - - mean /= size; - - for (i = 0; i < size; i++) { - int dev = (int)buf[i] - mean; - res += dev*dev; - } - - return res / size; -} - -/* Calculate mean square difference of two lines */ -static int get_diff_norm(unsigned char *buf1, unsigned char *buf2, int size) -{ - int res = 0, i; - for (i = 0; i < size; i++) { - int dev = (int)buf1[i] - (int)buf2[i]; - res += dev*dev; - } - - return res / size; -} - /* Calculade squared standand deviation of sum of two lines */ static int vfs5011_get_deviation2(struct fpi_line_asmbl_ctx *ctx, GSList *row1, GSList *row2) { @@ -395,7 +362,7 @@ static int process_chunk(struct vfs5011_data *data, int transferred) unsigned char *linebuf = data->capture_buffer + i * VFS5011_LINE_SIZE; - if (get_deviation(linebuf + 8, VFS5011_IMAGE_WIDTH) + if (fpi_std_sq_dev(linebuf + 8, VFS5011_IMAGE_WIDTH) < DEVIATION_THRESHOLD) { if (data->lines_captured == 0) continue; @@ -417,7 +384,7 @@ static int process_chunk(struct vfs5011_data *data, int transferred) } if ((data->lastline == NULL) - || (get_diff_norm( + || (fpi_mean_sq_diff_norm( data->lastline + 8, linebuf + 8, VFS5011_IMAGE_WIDTH) >= DIFFERENCE_THRESHOLD)) { diff --git a/libfprint/fp_internal.h b/libfprint/fp_internal.h index 5970e31..e309ea9 100644 --- a/libfprint/fp_internal.h +++ b/libfprint/fp_internal.h @@ -489,5 +489,9 @@ void fpi_imgdev_image_captured(struct fp_img_dev *imgdev, struct fp_img *img); void fpi_imgdev_abort_scan(struct fp_img_dev *imgdev, int result); void fpi_imgdev_session_error(struct fp_img_dev *imgdev, int error); +/* utils */ +int fpi_std_sq_dev(const unsigned char *buf, int size); +int fpi_mean_sq_diff_norm(unsigned char *buf1, unsigned char *buf2, int size); + #endif diff --git a/libfprint/img.c b/libfprint/img.c index f9545db..408dd56 100644 --- a/libfprint/img.c +++ b/libfprint/img.c @@ -511,3 +511,37 @@ API_EXPORTED struct fp_minutia **fp_img_get_minutiae(struct fp_img *img, return img->minutiae->list; } +/* Calculate squared standand deviation */ +int fpi_std_sq_dev(const unsigned char *buf, int size) +{ + int res = 0, mean = 0, i; + + if (size > (INT_MAX / 65536)) { + fp_err("%s: we might get an overflow!", __func__); + return -EOVERFLOW; + } + + for (i = 0; i < size; i++) + mean += buf[i]; + + mean /= size; + + for (i = 0; i < size; i++) { + int dev = (int)buf[i] - mean; + res += dev*dev; + } + + return res / size; +} + +/* Calculate normalized mean square difference of two lines */ +int fpi_mean_sq_diff_norm(unsigned char *buf1, unsigned char *buf2, int size) +{ + int res = 0, i; + for (i = 0; i < size; i++) { + int dev = (int)buf1[i] - (int)buf2[i]; + res += dev * dev; + } + + return res / size; +}