Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * FPrint Image - Private APIs | ||
3 | * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org> | ||
4 | * Copyright (C) 2019 Benjamin Berg <bberg@redhat.com> | ||
5 | * | ||
6 | * This library is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Lesser General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2.1 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * This library is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with this library; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | */ | ||
20 | |||
21 | #define FP_COMPONENT "image" | ||
22 | |||
23 | #include "fpi-image.h" | ||
24 | #include "fpi-log.h" | ||
25 | |||
26 | #include <nbis.h> | ||
27 | #include <config.h> | ||
28 | |||
29 | #ifdef HAVE_PIXMAN | ||
30 | #include <pixman.h> | ||
31 | #endif | ||
32 | |||
33 | /** | ||
34 | * SECTION: fpi-image | ||
35 | * @title: Internal FpImage | ||
36 | * @short_description: Internal image handling routines | ||
37 | * | ||
38 | * Internal image handling routines. See #FpImage for public routines. | ||
39 | */ | ||
40 | |||
41 | /** | ||
42 | * fpi_std_sq_dev: | ||
43 | * @buf: buffer (usually bitmap, one byte per pixel) | ||
44 | * @size: size of @buffer | ||
45 | * | ||
46 | * Calculates the squared standard deviation of the individual | ||
47 | * pixels in the buffer, as per the following formula: | ||
48 | * |[<!-- --> | ||
49 | * mean = sum (buf[0..size]) / size | ||
50 | * sq_dev = sum ((buf[0.size] - mean) ^ 2) | ||
51 | * ]| | ||
52 | * This function is usually used to determine whether image | ||
53 | * is empty. | ||
54 | * | ||
55 | * Returns: the squared standard deviation for @buffer | ||
56 | */ | ||
57 | gint | ||
58 | 2093 | fpi_std_sq_dev (const guint8 *buf, | |
59 | gint size) | ||
60 | { | ||
61 | 2093 | guint64 res = 0, mean = 0; | |
62 | 2093 | gint i; | |
63 | |||
64 |
2/2✓ Branch 0 taken 743552 times.
✓ Branch 1 taken 2093 times.
|
745645 | for (i = 0; i < size; i++) |
65 | 743552 | mean += buf[i]; | |
66 | |||
67 | 2093 | mean /= size; | |
68 | |||
69 |
2/2✓ Branch 0 taken 743552 times.
✓ Branch 1 taken 2093 times.
|
745645 | for (i = 0; i < size; i++) |
70 | { | ||
71 | 743552 | int dev = (int) buf[i] - mean; | |
72 | 743552 | res += dev * dev; | |
73 | } | ||
74 | |||
75 | 2093 | return res / size; | |
76 | } | ||
77 | |||
78 | /** | ||
79 | * fpi_mean_sq_diff_norm: | ||
80 | * @buf1: buffer (usually bitmap, one byte per pixel) | ||
81 | * @buf2: buffer (usually bitmap, one byte per pixel) | ||
82 | * @size: buffer size of smallest buffer | ||
83 | * | ||
84 | * This function calculates the normalized mean square difference of | ||
85 | * two buffers, usually two lines, as per the following formula: | ||
86 | * |[<!-- --> | ||
87 | * sq_diff = sum ((buf1[0..size] - buf2[0..size]) ^ 2) / size | ||
88 | * ]| | ||
89 | * | ||
90 | * This functions is usually used to get numerical difference | ||
91 | * between two images. | ||
92 | * | ||
93 | * Returns: the normalized mean squared difference between @buf1 and @buf2 | ||
94 | */ | ||
95 | gint | ||
96 | 2058 | fpi_mean_sq_diff_norm (const guint8 *buf1, | |
97 | const guint8 *buf2, | ||
98 | gint size) | ||
99 | { | ||
100 | 2058 | int res = 0, i; | |
101 | |||
102 |
2/2✓ Branch 0 taken 329280 times.
✓ Branch 1 taken 2058 times.
|
331338 | for (i = 0; i < size; i++) |
103 | { | ||
104 | 329280 | int dev = (int) buf1[i] - (int) buf2[i]; | |
105 | 329280 | res += dev * dev; | |
106 | } | ||
107 | |||
108 | 2058 | return res / size; | |
109 | } | ||
110 | |||
111 | FpImage * | ||
112 | 3 | fpi_image_resize (FpImage *orig_img, | |
113 | guint w_factor, | ||
114 | guint h_factor) | ||
115 | { | ||
116 | #ifdef HAVE_PIXMAN | ||
117 | 3 | int new_width = orig_img->width * w_factor; | |
118 | 3 | int new_height = orig_img->height * h_factor; | |
119 | 3 | pixman_image_t *orig, *resized; | |
120 | 3 | pixman_transform_t transform; | |
121 | 3 | FpImage *newimg; | |
122 | |||
123 | 3 | orig = pixman_image_create_bits (PIXMAN_a8, orig_img->width, orig_img->height, (uint32_t *) orig_img->data, orig_img->width); | |
124 | 3 | resized = pixman_image_create_bits (PIXMAN_a8, new_width, new_height, NULL, new_width); | |
125 | |||
126 | 3 | pixman_transform_init_identity (&transform); | |
127 | 3 | pixman_transform_scale (NULL, &transform, pixman_int_to_fixed (w_factor), pixman_int_to_fixed (h_factor)); | |
128 | 3 | pixman_image_set_transform (orig, &transform); | |
129 | 3 | pixman_image_set_filter (orig, PIXMAN_FILTER_BILINEAR, NULL, 0); | |
130 | 3 | pixman_image_composite32 (PIXMAN_OP_SRC, | |
131 | orig, /* src */ | ||
132 | NULL, /* mask */ | ||
133 | resized, /* dst */ | ||
134 | 0, 0, /* src x y */ | ||
135 | 0, 0, /* mask x y */ | ||
136 | 0, 0, /* dst x y */ | ||
137 | new_width, new_height /* width height */ | ||
138 | ); | ||
139 | |||
140 | 3 | newimg = fp_image_new (new_width, new_height); | |
141 | 3 | newimg->flags = orig_img->flags; | |
142 | |||
143 | 3 | memcpy (newimg->data, pixman_image_get_data (resized), new_width * new_height); | |
144 | |||
145 | 3 | pixman_image_unref (orig); | |
146 | 3 | pixman_image_unref (resized); | |
147 | |||
148 | 3 | return newimg; | |
149 | #else | ||
150 | fp_err ("Libfprint compiled without pixman support, impossible to resize"); | ||
151 | |||
152 | return g_object_ref (orig_img); | ||
153 | #endif | ||
154 | } | ||
155 |