From 6b8b17f575f5932ebe865d1db93a2807a49b961b Mon Sep 17 00:00:00 2001 From: Daniel Drake Date: Sun, 10 Aug 2008 14:34:44 -0500 Subject: [PATCH] Split imagemagick hack from main library Cleans up the conditional compilation system --- libfprint/Makefile.am | 3 +- libfprint/drivers/aes4000.c | 22 ++++++++---- libfprint/fp_internal.h | 2 +- libfprint/imagemagick.c | 67 +++++++++++++++++++++++++++++++++++++ libfprint/imgdev.c | 67 ++----------------------------------- 5 files changed, 88 insertions(+), 73 deletions(-) create mode 100644 libfprint/imagemagick.c diff --git a/libfprint/Makefile.am b/libfprint/Makefile.am index 9c53c891..844b09ed 100644 --- a/libfprint/Makefile.am +++ b/libfprint/Makefile.am @@ -91,7 +91,8 @@ DRIVER_SRC += $(AES4000_SRC) endif if REQUIRE_IMAGEMAGICK -libfprint_la_CFLAGS += $(IMAGEMAGICK_CFLAGS) -DREQUIRE_IMAGEMAGICK +OTHER_SRC += imagemagick.c +libfprint_la_CFLAGS += $(IMAGEMAGICK_CFLAGS) libfprint_la_LIBADD += $(IMAGEMAGICK_LIBS) endif diff --git a/libfprint/drivers/aes4000.c b/libfprint/drivers/aes4000.c index 51613ba1..a8f71e6a 100644 --- a/libfprint/drivers/aes4000.c +++ b/libfprint/drivers/aes4000.c @@ -34,6 +34,10 @@ #define NR_SUBARRAYS 6 #define SUBARRAY_LEN 768 +#define IMG_HEIGHT 96 +#define IMG_WIDTH 96 +#define ENLARGE_FACTOR 3 + struct aes4k_dev { struct libusb_transfer *img_trf; }; @@ -120,6 +124,7 @@ static void img_cb(struct libusb_transfer *transfer) struct fp_img_dev *dev = transfer->user_data; struct aes4k_dev *aesdev = dev->priv; unsigned char *ptr = transfer->buffer; + struct fp_img *tmp; struct fp_img *img; int i; @@ -135,15 +140,21 @@ static void img_cb(struct libusb_transfer *transfer) fpi_imgdev_report_finger_status(dev, TRUE); - img = fpi_img_new_for_imgdev(dev); - img->flags = FP_IMG_COLORS_INVERTED | FP_IMG_V_FLIPPED | FP_IMG_H_FLIPPED; + tmp = fpi_img_new(IMG_WIDTH * IMG_HEIGHT); + tmp->width = IMG_WIDTH; + tmp->height = IMG_HEIGHT; + tmp->flags = FP_IMG_COLORS_INVERTED | FP_IMG_V_FLIPPED | FP_IMG_H_FLIPPED; for (i = 0; i < NR_SUBARRAYS; i++) { fp_dbg("subarray header byte %02x", *ptr); ptr++; - aes_assemble_image(ptr, 96, 16, img->data + (i * 96 * 16)); + aes_assemble_image(ptr, 96, 16, tmp->data + (i * 96 * 16)); ptr += SUBARRAY_LEN; } + /* FIXME: this is an ugly hack to make the image big enough for NBIS + * to process reliably */ + img = fpi_im_resize(tmp, ENLARGE_FACTOR); + fp_img_free(tmp); fpi_imgdev_image_captured(dev, img); /* FIXME: rather than assuming finger has gone, we should poll regs until @@ -244,9 +255,8 @@ struct fp_img_driver aes4000_driver = { .id_table = id_table, }, .flags = 0, - .img_height = 96, - .img_width = 96, - .enlarge_factor = 3, + .img_height = IMG_HEIGHT * ENLARGE_FACTOR, + .img_width = IMG_WIDTH * ENLARGE_FACTOR, /* temporarily lowered until image quality improves */ .bz3_threshold = 9, diff --git a/libfprint/fp_internal.h b/libfprint/fp_internal.h index dde69cda..01f55659 100644 --- a/libfprint/fp_internal.h +++ b/libfprint/fp_internal.h @@ -222,7 +222,6 @@ struct fp_img_driver { uint16_t flags; int img_width; int img_height; - unsigned int enlarge_factor; int bz3_threshold; /* Device operations */ @@ -346,6 +345,7 @@ int fpi_img_compare_print_data(struct fp_print_data *enrolled_print, struct fp_print_data *new_print); int fpi_img_compare_print_data_to_gallery(struct fp_print_data *print, struct fp_print_data **gallery, int match_threshold, size_t *match_offset); +struct fp_img *fpi_im_resize(struct fp_img *img, unsigned int factor); /* polling and timeouts */ diff --git a/libfprint/imagemagick.c b/libfprint/imagemagick.c new file mode 100644 index 00000000..ed6a1cad --- /dev/null +++ b/libfprint/imagemagick.c @@ -0,0 +1,67 @@ +/* + * Imaging utility functions for libfprint + * Copyright (C) 2007-2008 Daniel Drake + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "fp_internal.h" + +struct fp_img *fpi_im_resize(struct fp_img *img, unsigned int factor) +{ + Image *mimg; + Image *resized; + ExceptionInfo exception; + MagickBooleanType ret; + int new_width = img->width * factor; + int new_height = img->height * factor; + struct fp_img *newimg; + + /* It is possible to implement resizing using a simple algorithm, however + * we use ImageMagick because it applies some kind of smoothing to the + * result, which improves matching performances in my experiments. */ + + if (!IsMagickInstantiated()) + InitializeMagick(NULL); + + GetExceptionInfo(&exception); + mimg = ConstituteImage(img->width, img->height, "I", CharPixel, img->data, + &exception); + + GetExceptionInfo(&exception); + resized = ResizeImage(mimg, new_width, new_height, 0, 1.0, &exception); + + newimg = fpi_img_new(new_width * new_height); + newimg->width = new_width; + newimg->height = new_height; + newimg->flags = img->flags; + + GetExceptionInfo(&exception); + ret = ExportImagePixels(resized, 0, 0, new_width, new_height, "I", + CharPixel, newimg->data, &exception); + if (ret != MagickTrue) { + fp_err("export failed"); + return NULL; + } + + DestroyImage(mimg); + DestroyImage(resized); + + return newimg; +} + diff --git a/libfprint/imgdev.c b/libfprint/imgdev.c index e1330580..4b49c73e 100644 --- a/libfprint/imgdev.c +++ b/libfprint/imgdev.c @@ -20,9 +20,6 @@ #include #include -#ifdef REQUIRE_IMAGEMAGICK -#include -#endif #include "fp_internal.h" @@ -89,51 +86,6 @@ static int dev_change_state(struct fp_img_dev *imgdev, return imgdrv->change_state(imgdev, state); } -#ifdef REQUIRE_IMAGEMAGICK -static struct fp_img *im_resize(struct fp_img *img, unsigned int factor) -{ - Image *mimg; - Image *resized; - ExceptionInfo exception; - MagickBooleanType ret; - int new_width = img->width * factor; - int new_height = img->height * factor; - struct fp_img *newimg; - - /* It is possible to implement resizing using a simple algorithm, however - * we use ImageMagick because it applies some kind of smoothing to the - * result, which improves matching performances in my experiments. */ - - if (!IsMagickInstantiated()) - InitializeMagick(NULL); - - GetExceptionInfo(&exception); - mimg = ConstituteImage(img->width, img->height, "I", CharPixel, img->data, - &exception); - - GetExceptionInfo(&exception); - resized = ResizeImage(mimg, new_width, new_height, 0, 1.0, &exception); - - newimg = fpi_img_new(new_width * new_height); - newimg->width = new_width; - newimg->height = new_height; - newimg->flags = img->flags; - - GetExceptionInfo(&exception); - ret = ExportImagePixels(resized, 0, 0, new_width, new_height, "I", - CharPixel, newimg->data, &exception); - if (ret != MagickTrue) { - fp_err("export failed"); - return NULL; - } - - DestroyImage(mimg); - DestroyImage(resized); - - return newimg; -} -#endif - /* check image properties and resize it if necessary. potentially returns a new * image after freeing the old one. */ static int sanitize_image(struct fp_img_dev *imgdev, struct fp_img **_img) @@ -161,17 +113,6 @@ static int sanitize_image(struct fp_img_dev *imgdev, struct fp_img **_img) return -EINVAL; } -#ifdef REQUIRE_IMAGEMAGICK - if (imgdrv->enlarge_factor > 1) { - /* FIXME: enlarge_factor should not exist! instead, MINDTCT should - * actually look at the value of the pixels-per-mm parameter and - * figure out itself when the image needs to be treated as if it - * were bigger. */ - struct fp_img *tmp = im_resize(img, imgdrv->enlarge_factor); - fp_img_free(img); - *_img = tmp; - } -#endif return 0; } @@ -392,9 +333,7 @@ int fpi_imgdev_get_img_width(struct fp_img_dev *imgdev) struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(drv); int width = imgdrv->img_width; - if (width > 0 && imgdrv->enlarge_factor > 1) - width *= imgdrv->enlarge_factor; - else if (width == -1) + if (width == -1) width = 0; return width; @@ -406,9 +345,7 @@ int fpi_imgdev_get_img_height(struct fp_img_dev *imgdev) struct fp_img_driver *imgdrv = fpi_driver_to_img_driver(drv); int height = imgdrv->img_height; - if (height > 0 && imgdrv->enlarge_factor > 1) - height *= imgdrv->enlarge_factor; - else if (height == -1) + if (height == -1) height = 0; return height;