similar-image.c

filter: Statementstmt 0.0% 0 / 232 Branchbran 0.0% 0 / 236 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 468 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Similar.xs ↑ Summary
1 /* Given two grey images, compare them using the algorithm described
2 in "An Image Signature for Any Kind of Image" by H. Chi Wong,
3 Marshall Bern and David Goldberg, 2002. */
4  
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <math.h>
8 #include <stdarg.h>
9 #include "similar-image.h"
10  
11 #ifdef HEADER
12  
13 #define SIZE 9
14 #define DIRECTIONS 8
15  
16 /* A point in the grid. */
17  
18 typedef struct point
19 {
20 double average_grey_level;
21 int d[DIRECTIONS];
22 }
23 point_t;
24  
25 typedef int (*simage_error_channel_t) (void * s, const char * format, ...);
26  
27 typedef struct simage
28 {
29 /* The width of the image in pixels. */
30 unsigned int width;
31 /* The height of the image in pixels. */
32 unsigned int height;
33 /* The image data. */
34 unsigned char * data;
35 /* The computed signature. */
36 char * signature;
37 /* The length of the signature. */
38 int signature_length;
39 /* The P-value for this image, see equation in article. */
40 unsigned int p;
41 /* The grid of values. */
42 point_t grid[SIZE*SIZE];
43 /* width / (SIZE + 1) */
44 double w10;
45 /* height / (SIZE + 1) */
46 double h10;
47 /* The number of times malloc/calloc were called related to this
48 object. */
49 int nmallocs;
50 simage_error_channel_t error_channel;
51 /* This contains a true value if we have actually loaded image
52 data, or a false value if not. This may be false if we just
53 loaded a signature rather than the image. */
54 unsigned int valid_image : 1;
55 /* The grid is already filled. */
56 unsigned int grid_filled : 1;
57 }
58 simage_t;
59  
60 typedef enum
61 {
62 simage_ok,
63 /* malloc failed. */
64 simage_status_memory_failure,
65 /* x or y is outside the image dimensions. */
66 simage_status_bounds,
67 simage_status_bad_image,
68 /* Some upstream program did a stupid thing. */
69 simage_status_bad_logic,
70 /* */
71 simage_status_free_underflow,
72 /* */
73 simage_status_memory_leak,
74 }
75 simage_status_t;
76  
77 typedef enum
78 {
79 much_darker = -2,
80 darker = -1,
81 same = 0,
82 lighter = 1,
83 much_lighter = 2,
84 }
85 comparison_t;
86  
87 #endif /* def HEADER */
88  
89 #define FAIL(test,status,message,...) { \
90 if (test) { \
91 if (s->error_channel) { \
92 (*s->error_channel) (s, "%s:%d: ", __FILE__, __LINE__); \
93 (*s->error_channel) (s, message, ## __VA_ARGS__); \
94 (*s->error_channel) (s, "\n"); \
95 } \
96 return simage_status_ ## status; \
97 } \
98 }
99  
100 #define CALL(x) { \
101 simage_status_t status; \
102 status = x; \
103 if (status != simage_ok) { \
104 if (s->error_channel) { \
105 (*s->error_channel) (s, "%s:%d: ", __FILE__, __LINE__); \
106 (*s->error_channel) (s, "%s failed with status %d", \
107 #x, status); \
108 (*s->error_channel) (s, "\n"); \
109 } \
110 return status; \
111 } \
112 }
113  
114 #define CHECK_XY(s,x,y) { \
115 FAIL (x > s->width || x < 0, bounds, \
116 "x coordinate %d is outside the image", x); \
117 FAIL (y > s->height || y < 0, bounds, \
118 "y coordinate %d is outside the image", y); \
119 }
120  
121 /* Default place to print errors, if the user doesn't override this. */
122  
123 static int
124 0 simage_default_error_channel (void * vs, const char * message, ...)
125 {
126 va_list va;
127 int chars;
128 0 va_start (va, message);
129 0 chars = vfprintf (stderr, message, va);
130 0 va_end (va);
131 0 return chars;
132 }
133  
134 #define OUTSIDE -1
135  
136 /* Given x and y coordinates, return the part of the grid which
137 corresponds to that. */
138  
139 0 int x_y_to_entry (int x, int y)
140 {
141 int entry;
142 0 ▶ if (x < 0 || x >= SIZE) {
Branch: 2 branches
BranchTrueFalseTotal
if (x < 0 || x >= SIZE) { 0 0 0
if (x < 0 || x >= SIZE) { 0 0 0
143 0 return OUTSIDE;
144 }
145 0 ▶ if (y < 0 || y >= SIZE) {
Branch: 2 branches
BranchTrueFalseTotal
if (y < 0 || y >= SIZE) { 0 0 0
if (y < 0 || y >= SIZE) { 0 0 0
146 0 return OUTSIDE;
147 }
148 0 entry = y * SIZE + x;
149 0 ▶ if (entry < 0 || entry >= SIZE * SIZE) {
Branch: 2 branches
BranchTrueFalseTotal
if (entry < 0 || entry >= SIZE * SIZE) { 0 0 0
if (entry < 0 || entry >= SIZE * SIZE) { 0 0 0
150 0 fprintf (stderr, "%s:%d: overflow %d\n", __FILE__, __LINE__, entry);
151 0 return OUTSIDE;
152 }
153 0 return entry;
154 }
155  
156 simage_status_t
157 0 simage_dump (simage_t * s)
158 {
159 0 printf ("{\n");
160 0 printf ("\"width\":%d,\n", s->width);
161 0 printf ("\"height\":%d,\n", s->height);
162 0 printf ("\"p\":%d,\n", s->p);
163 0 printf ("\"dummy\":0\n");
164 0 printf ("}\n");
165 0 return simage_ok;
166 }
167  
168 simage_status_t
169 0 simage_init (simage_t * s, unsigned int width, unsigned int height)
170 {
171 unsigned int p;
172 /* The minimum of the width and the height. */
173 unsigned int min_w_h;
174  
175 0 s->data = calloc (width * height, sizeof (unsigned char));
176 0 ▶ CALL (simage_inc_nmallocs (s, s->data));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_inc_nmallocs (s, s->data)); 0 0 0
CALL (simage_inc_nmallocs (s, s->data)); 0 0 0
177 0 s->height = height;
178 0 s->width = width;
179 0 s->p = 2;
180 0 s->error_channel = & simage_default_error_channel;
181 0 min_w_h = width;
182 0 ▶ if (height < min_w_h) {
Branch: 1 branch
BranchTrueFalseTotal
if (height < min_w_h) { 0 0 0
183 0 min_w_h = height;
184 }
185 0 p = (unsigned int) (floor (0.5 + ((double) min_w_h) / 20.0));
186 0 ▶ if (p > s->p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p > s->p) { 0 0 0
187 0 s->p = p;
188 }
189  
190 // This contains a valid image data, although it is just black
191 // pixels at the moment.
192 0 s->valid_image = 1;
193 0 return simage_ok;
194 }
195  
196 simage_status_t
197 0 simage_inc_nmallocs (simage_t * s, void * signature)
198 {
199 0 ▶ FAIL (! signature, memory_failure, "Out of memory");
Branch: 2 branches
BranchTrueFalseTotal
FAIL (! signature, memory_failure, "Out of memory"); 0 0 0
FAIL (! signature, memory_failure, "Out of memory"); 0 0 0
200 0 s->nmallocs++;
201 0 return simage_ok;
202 }
203  
204 simage_status_t
205 0 simage_dec_nmallocs (simage_t * s)
206 {
207 0 s->nmallocs--;
208 0 ▶ FAIL (s->nmallocs < 0, free_underflow,
Branch: 2 branches
BranchTrueFalseTotal
FAIL (s->nmallocs < 0, free_underflow, 0 0 0
FAIL (s->nmallocs < 0, free_underflow, 0 0 0
209 "too many frees, %d should be 0.\n",
210 s->nmallocs);
211 0 return simage_ok;
212 }
213  
214 /* Free all the memory associated with "s", except for "s" itself,
215 which is allocated by the user. */
216  
217 simage_status_t
218 0 simage_free (simage_t * s)
219 {
220 0 ▶ if (s->data) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->data) { 0 0 0
221 0 free (s->data);
222 0 s->data = 0;
223 0 ▶ CALL (simage_dec_nmallocs (s));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_dec_nmallocs (s)); 0 0 0
CALL (simage_dec_nmallocs (s)); 0 0 0
224 }
225 0 ▶ if (s->signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->signature) { 0 0 0
226 0 free (s->signature);
227 0 s->signature = 0;
228 0 ▶ CALL (simage_dec_nmallocs (s));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_dec_nmallocs (s)); 0 0 0
CALL (simage_dec_nmallocs (s)); 0 0 0
229 }
230 0 ▶ FAIL (s->nmallocs != 0, memory_leak,
Branch: 2 branches
BranchTrueFalseTotal
FAIL (s->nmallocs != 0, memory_leak, 0 0 0
FAIL (s->nmallocs != 0, memory_leak, 0 0 0
231 "memory leak: %d should be 0.", s->nmallocs);
232 0 return simage_ok;
233 }
234  
235 /* Set the pixel at "x", "y" to the value "grey". */
236  
237 simage_status_t
238 0 simage_set_pixel (simage_t * s, int x, int y, unsigned char grey)
239 {
240 0 ▶ CHECK_XY (s, x, y);
Branch: 6 branches
BranchTrueFalseTotal
CHECK_XY (s, x, y); 0 0 0
CHECK_XY (s, x, y); 0 0 0
CHECK_XY (s, x, y); 0 0 0
CHECK_XY (s, x, y); 0 0 0
CHECK_XY (s, x, y); 0 0 0
CHECK_XY (s, x, y); 0 0 0
241 0 s->data[y * s->width + x] = grey;
242 0 return simage_ok;
243 }
244  
245 /* Compute the average intensity of the grid square at the coordinates
246 "i", "j" on the grid. This assumes that "s->w10" and "s->h10" have
247 already been computed. */
248  
249 simage_status_t
250 0 simage_fill_entry (simage_t * s, int i, int j)
251 {
252 double total;
253 int px;
254 int py;
255 double xd;
256 double yd;
257 int x_min;
258 int y_min;
259 int x_max;
260 int y_max;
261 int size;
262 int entry;
263 int grey;
264 0 xd = (i + 1) * s->w10;
265 0 yd = (j + 1) * s->h10;
266 0 x_min = round (xd - s->p / 2.0);
267 0 y_min = round (yd - s->p / 2.0);
268 0 x_max = round (xd + s->p / 2.0);
269 0 y_max = round (yd + s->p / 2.0);
270  
271 /* For very small images, these boundaries are sometimes
272 reached. */
273  
274 0 ▶ if (y_max >= s->height) {
Branch: 1 branch
BranchTrueFalseTotal
if (y_max >= s->height) { 0 0 0
275 0 y_max = s->height - 1;
276 }
277 0 ▶ if (x_max >= s->width) {
Branch: 1 branch
BranchTrueFalseTotal
if (x_max >= s->width) { 0 0 0
278 0 x_max = s->width - 1;
279 }
280 0 ▶ if (x_min < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (x_min < 0) { 0 0 0
281 0 x_min = 0;
282 }
283 0 ▶ if (y_min < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (y_min < 0) { 0 0 0
284 0 y_min = 0;
285 }
286  
287 0 total = 0.0;
288 0 ▶ for (py = y_min; py <= y_max; py++) {
Branch: 1 branch
BranchTrueFalseTotal
for (py = y_min; py <= y_max; py++) { 0 0 0
289 0 ▶ FAIL (py < 0 || py >= s->height, bounds,
Branch: 3 branches
BranchTrueFalseTotal
FAIL (py < 0 || py >= s->height, bounds, 0 0 0
FAIL (py < 0 || py >= s->height, bounds, 0 0 0
FAIL (py < 0 || py >= s->height, bounds, 0 0 0
290 "overflow py=%d for i, j = (%d, %d)\n", py, i, j);
291 0 ▶ for (px = x_min; px <= x_max; px++) {
Branch: 1 branch
BranchTrueFalseTotal
for (px = x_min; px <= x_max; px++) { 0 0 0
292 0 ▶ FAIL (px < 0 || px >= s->width, bounds,
Branch: 3 branches
BranchTrueFalseTotal
FAIL (px < 0 || px >= s->width, bounds, 0 0 0
FAIL (px < 0 || px >= s->width, bounds, 0 0 0
FAIL (px < 0 || px >= s->width, bounds, 0 0 0
293 "overflow px=%d for i, j = (%d, %d)\n", px, i, j);
294 0 total += s->data[py * s->width + px];
295 }
296 }
297 0 size = (x_max - x_min + 1) * (y_max - y_min + 1);
298 0 grey = (int) round (total / ((double) size));
299 0 ▶ FAIL (grey < 0 || grey >= 256, bounds,
Branch: 3 branches
BranchTrueFalseTotal
FAIL (grey < 0 || grey >= 256, bounds, 0 0 0
FAIL (grey < 0 || grey >= 256, bounds, 0 0 0
FAIL (grey < 0 || grey >= 256, bounds, 0 0 0
300 "bad average grey value %d.", grey);
301 0 entry = x_y_to_entry (i, j);
302 0 ▶ FAIL (entry == OUTSIDE, bounds,
Branch: 2 branches
BranchTrueFalseTotal
FAIL (entry == OUTSIDE, bounds, 0 0 0
FAIL (entry == OUTSIDE, bounds, 0 0 0
303 "bounds error with %d %d -> %d\n",
304 i, j, entry);
305 0 s->grid[entry].average_grey_level = grey;
306 0 return simage_ok;
307 }
308  
309 /* Go around the image and make the average values for each of the
310 points on the grid. */
311  
312 simage_status_t
313 0 simage_fill_entries (simage_t * s)
314 {
315 int i;
316 int j;
317 0 ▶ FAIL (s->width == 0 || s->height == 0, bad_image,
Branch: 3 branches
BranchTrueFalseTotal
FAIL (s->width == 0 || s->height == 0, bad_image, 0 0 0
FAIL (s->width == 0 || s->height == 0, bad_image, 0 0 0
FAIL (s->width == 0 || s->height == 0, bad_image, 0 0 0
318 "empty image w/h %d/%d.\n",
319 s->width, s->height);
320 0 s->w10 = ((double) s->width) / ((double) (SIZE + 1));
321 0 s->h10 = ((double) s->height) / ((double) (SIZE + 1));
322 0 ▶ for (i = 0; i < SIZE; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SIZE; i++) { 0 0 0
323 0 ▶ for (j = 0; j < SIZE; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < SIZE; j++) { 0 0 0
324 0 ▶ CALL (simage_fill_entry (s, i, j));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_fill_entry (s, i, j)); 0 0 0
CALL (simage_fill_entry (s, i, j)); 0 0 0
325 }
326 }
327 0 return simage_ok;
328 }
329  
330 /* Given offsets xo and yo, return the array offset for the difference
331 array which corresponds to that. */
332  
333 0 int xo_yo_to_direction (int xo, int yo)
334 {
335 int direction;
336 0 ▶ if (xo <= 0 && yo <= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (xo <= 0 && yo <= 0) { 0 0 0
if (xo <= 0 && yo <= 0) { 0 0 0
337 0 direction = (xo + 1) + 3 * (yo + 1);
338 }
339 else {
340 // Adjust for not having a centre square, so +1, +1 is 7, not 8.
341 0 direction = (xo + 1) + 3 * (yo + 1) - 1;
342 }
343 0 return direction;
344 }
345  
346 simage_status_t
347 0 direction_to_xo_yo (int direction, int * xo, int * yo)
348 {
349 0 ▶ if (direction < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (direction < 3) { 0 0 0
350 0 * yo = -1;
351 0 * xo = direction - 1;
352 0 return simage_ok;
353 }
354 0 ▶ if (direction < 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (direction < 5) { 0 0 0
355 0 * yo = 0;
356 0 ▶ if (direction == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (direction == 3) { 0 0 0
357 0 * xo = -1;
358 }
359 0 ▶ else if (direction == 4) {
Branch: 1 branch
BranchTrueFalseTotal
else if (direction == 4) { 0 0 0
360 0 * xo = 1;
361 }
362 else {
363 0 return simage_status_bounds;
364 }
365 0 return simage_ok;
366 }
367 0 ▶ if (direction < DIRECTIONS) {
Branch: 1 branch
BranchTrueFalseTotal
if (direction < DIRECTIONS) { 0 0 0
368 0 * yo = 1;
369 0 * xo = direction - 6;
370 0 return simage_ok;
371 }
372 0 fprintf (stderr, "%s:%d: direction %d >= DIRECTIONS %d.\n",
373 __FILE__, __LINE__, direction, DIRECTIONS);
374 0 return simage_status_bounds;
375 }
376  
377 0 int diff (int thisgrey, int thatgrey)
378 {
379 int d;
380 0 d = thisgrey - thatgrey;
381 0 ▶ if (d >= -2 && d <= 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (d >= -2 && d <= 2) { 0 0 0
if (d >= -2 && d <= 2) { 0 0 0
382 0 return same;
383 }
384 0 ▶ else if (d > 100) {
Branch: 1 branch
BranchTrueFalseTotal
else if (d > 100) { 0 0 0
385 0 return much_darker;
386 }
387 0 ▶ else if (d > 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (d > 2) { 0 0 0
388 0 return darker;
389 }
390 0 ▶ else if (d < -100) {
Branch: 1 branch
BranchTrueFalseTotal
else if (d < -100) { 0 0 0
391 0 return much_lighter;
392 }
393 0 ▶ else if (d < -2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (d < -2) { 0 0 0
394 0 return lighter;
395 }
396 else {
397 0 fprintf (stderr, "%s:%d: mysterious d value %d\n",
398 __FILE__, __LINE__, d);
399 0 return same;
400 }
401 }
402  
403 /* Make the difference between two adjoining points. */
404  
405 simage_status_t
406 0 simage_make_point_diffs (simage_t * s, int x, int y)
407 {
408 int xo;
409 int yo;
410 int thisgrey;
411 int thisentry;
412 point_t * thispoint;
413 0 thisentry = x_y_to_entry (x, y);
414 /* Make 100% sure that we don't try to access outside the "grid" array
415 within "s". */
416 0 ▶ FAIL (thisentry == OUTSIDE, bounds, "entry outside grid %d %d %d\n",
Branch: 2 branches
BranchTrueFalseTotal
FAIL (thisentry == OUTSIDE, bounds, "entry outside grid %d %d %d\n", 0 0 0
FAIL (thisentry == OUTSIDE, bounds, "entry outside grid %d %d %d\n", 0 0 0
417 x, y, thisentry);
418 0 thispoint = & s->grid[thisentry];
419 0 thisgrey = thispoint->average_grey_level;
420 0 ▶ for (xo = -1; xo <= 1; xo++) {
Branch: 1 branch
BranchTrueFalseTotal
for (xo = -1; xo <= 1; xo++) { 0 0 0
421 0 ▶ for (yo = -1; yo <= 1; yo++) {
Branch: 1 branch
BranchTrueFalseTotal
for (yo = -1; yo <= 1; yo++) { 0 0 0
422 int thatentry;
423 int direction;
424 int thatgrey;
425 0 ▶ if (xo == 0 && yo == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (xo == 0 && yo == 0) { 0 0 0
if (xo == 0 && yo == 0) { 0 0 0
426 // Skip the middle square, since this would be the
427 // difference between us and ourselves.
428 0 continue;
429 }
430 0 thatentry = x_y_to_entry (x + xo, y + yo);
431 0 ▶ if (thatentry == OUTSIDE) {
Branch: 1 branch
BranchTrueFalseTotal
if (thatentry == OUTSIDE) { 0 0 0
432 // Skip entries which are outside the grid, which
433 // happens e.g. if x = 0 and xo = -1.
434 0 continue;
435 }
436 // Get the grey level of the other point
437 0 thatgrey = s->grid[thatentry].average_grey_level;
438 // turn xo, yo into an array offset "direction".
439 0 direction = xo_yo_to_direction (xo, yo);
440 // Put the difference into d[direction] of the current point.
441 0 thispoint->d[direction] = diff (thisgrey, thatgrey);
442 // fprintf (stderr, "# %d %d %d\n", thisentry, direction, thispoint->d[direction]);
443 }
444 }
445 0 return simage_ok;
446 }
447  
448 simage_status_t
449 0 entry_to_x_y (int entry, int * x_ptr, int * y_ptr)
450 {
451 int x;
452 int y;
453 0 x = entry % SIZE;
454 0 y = entry / SIZE;
455 0 * x_ptr = x;
456 0 * y_ptr = y;
457 0 return simage_ok;
458 }
459  
460 /* Make the array of differences between adjoining points. */
461  
462 simage_status_t
463 0 simage_make_differences (simage_t * s)
464 {
465 int cell;
466 0 ▶ for (cell = 0; cell < SIZE * SIZE; cell++) {
Branch: 1 branch
BranchTrueFalseTotal
for (cell = 0; cell < SIZE * SIZE; cell++) { 0 0 0
467 int x;
468 int y;
469 0 ▶ CALL (entry_to_x_y (cell, & x, & y));
Branch: 2 branches
BranchTrueFalseTotal
CALL (entry_to_x_y (cell, & x, & y)); 0 0 0
CALL (entry_to_x_y (cell, & x, & y)); 0 0 0
470 0 ▶ CALL (simage_make_point_diffs (s, x, y));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_make_point_diffs (s, x, y)); 0 0 0
CALL (simage_make_point_diffs (s, x, y)); 0 0 0
471 }
472 0 return simage_ok;
473 }
474  
475 #define MAXDIM 10000
476  
477 simage_status_t
478 0 simage_check_image (simage_t * s)
479 {
480 0 ▶ FAIL (s->width == 0 || s->height == 0, bad_image,
Branch: 3 branches
BranchTrueFalseTotal
FAIL (s->width == 0 || s->height == 0, bad_image, 0 0 0
FAIL (s->width == 0 || s->height == 0, bad_image, 0 0 0
FAIL (s->width == 0 || s->height == 0, bad_image, 0 0 0
481 "empty image w/h %d/%d.\n",
482 s->width, s->height);
483 0 ▶ FAIL (s->width > MAXDIM || s->height > MAXDIM, bad_image,
Branch: 3 branches
BranchTrueFalseTotal
FAIL (s->width > MAXDIM || s->height > MAXDIM, bad_image, 0 0 0
FAIL (s->width > MAXDIM || s->height > MAXDIM, bad_image, 0 0 0
FAIL (s->width > MAXDIM || s->height > MAXDIM, bad_image, 0 0 0
484 "oversize image w/h %d/%d.\n",
485 s->width, s->height);
486 0 return simage_ok;
487 }
488  
489 simage_status_t
490 0 simage_fill_grid (simage_t * s)
491 {
492 0 ▶ FAIL (s->grid_filled, bad_logic,
Branch: 2 branches
BranchTrueFalseTotal
FAIL (s->grid_filled, bad_logic, 0 0 0
FAIL (s->grid_filled, bad_logic, 0 0 0
493 "double call to fill_grid.\n");
494 0 ▶ CALL (simage_check_image (s));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_check_image (s)); 0 0 0
CALL (simage_check_image (s)); 0 0 0
495 0 ▶ CALL (simage_fill_entries (s));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_fill_entries (s)); 0 0 0
CALL (simage_fill_entries (s)); 0 0 0
496 0 ▶ CALL (simage_make_differences (s));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_make_differences (s)); 0 0 0
CALL (simage_make_differences (s)); 0 0 0
497 0 s->grid_filled = 1;
498 0 return simage_ok;
499 }
500  
501 simage_status_t
502 0 simage_diff (simage_t * s1, simage_t * s2, double * total_diff)
503 {
504 int total;
505 int total1;
506 int total2;
507 int cell;
508 0 total = 0;
509 0 total1 = 0;
510 0 total2 = 0;
511 0 ▶ for (cell = 0; cell < SIZE * SIZE; cell++) {
Branch: 1 branch
BranchTrueFalseTotal
for (cell = 0; cell < SIZE * SIZE; cell++) { 0 0 0
512 int direction;
513 0 ▶ for (direction = 0; direction < DIRECTIONS; direction++) {
Branch: 1 branch
BranchTrueFalseTotal
for (direction = 0; direction < DIRECTIONS; direction++) { 0 0 0
514 int diff;
515 int s1cd;
516 int s2cd;
517 0 s1cd = s1->grid[cell].d[direction];
518 0 s2cd = s2->grid[cell].d[direction];
519 0 diff = s1cd - s2cd;
520 // Add the squares of the values to the totals.
521 0 total += diff * diff;
522 0 total1 += s1cd * s1cd;
523 0 total2 += s2cd * s2cd;
524 }
525 }
526 0 ▶ if (total1 == 0 && total2 == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (total1 == 0 && total2 == 0) { 0 0 0
if (total1 == 0 && total2 == 0) { 0 0 0
527 0 *total_diff = 0.0;
528 0 return simage_ok;
529 }
530 0 *total_diff = ((double) total) / ((double)(total1 + total2));
531 0 return simage_ok;
532 }
533  
534 /* Check whether this direction and cell point to another
535 cell or are outside the image. */
536  
537 0 int inside (int cell, int direction)
538 {
539 int x;
540 int y;
541 int xo;
542 int yo;
543 int nextcell;
544 0 x = cell % SIZE;
545 0 y = cell / SIZE;
546 0 direction_to_xo_yo (direction, & xo, & yo);
547 0 nextcell = x_y_to_entry (x + xo, y + yo);
548 0 ▶ if (nextcell == OUTSIDE) {
Branch: 1 branch
BranchTrueFalseTotal
if (nextcell == OUTSIDE) { 0 0 0
549 0 return 0;
550 }
551 0 return 1;
552 }
553  
554 simage_status_t
555 0 simage_allocate_signature (simage_t * s, int size)
556 {
557 0 s->signature = calloc (size + 1, sizeof (unsigned char));
558 0 ▶ CALL (simage_inc_nmallocs (s, s->signature));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_inc_nmallocs (s, s->signature)); 0 0 0
CALL (simage_inc_nmallocs (s, s->signature)); 0 0 0
559 0 return simage_ok;
560 }
561  
562 /* Compute the signature of "s->data". If it has already been
563 computed, return without doing anything. */
564  
565 simage_status_t
566 0 simage_signature (simage_t * s)
567 {
568 int cell;
569 int max_size;
570 0 ▶ if (s->signature) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->signature) { 0 0 0
571 0 return simage_ok;
572 }
573 0 max_size = DIRECTIONS * SIZE * SIZE;
574 0 ▶ CALL (simage_allocate_signature (s, max_size));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_allocate_signature (s, max_size)); 0 0 0
CALL (simage_allocate_signature (s, max_size)); 0 0 0
575 0 s->signature_length = 0;
576 0 ▶ for (cell = 0; cell < SIZE * SIZE; cell++) {
Branch: 1 branch
BranchTrueFalseTotal
for (cell = 0; cell < SIZE * SIZE; cell++) { 0 0 0
577 int direction;
578 0 ▶ for (direction = 0; direction < DIRECTIONS; direction++) {
Branch: 1 branch
BranchTrueFalseTotal
for (direction = 0; direction < DIRECTIONS; direction++) { 0 0 0
579 0 ▶ if (inside (cell, direction)) {
Branch: 1 branch
BranchTrueFalseTotal
if (inside (cell, direction)) { 0 0 0
580 int value;
581 0 value = s->grid[cell].d[direction] + 2 + '0';
582 0 ▶ FAIL (value < '0' || value > '5', bounds,
Branch: 3 branches
BranchTrueFalseTotal
FAIL (value < '0' || value > '5', bounds, 0 0 0
FAIL (value < '0' || value > '5', bounds, 0 0 0
FAIL (value < '0' || value > '5', bounds, 0 0 0
583 "overflow %d at cell=%d direction=%d",
584 value, cell, direction);
585 0 s->signature[s->signature_length] = (char) value;
586 0 s->signature_length++;
587 0 ▶ FAIL (s->signature_length > max_size, bounds,
Branch: 2 branches
BranchTrueFalseTotal
FAIL (s->signature_length > max_size, bounds, 0 0 0
FAIL (s->signature_length > max_size, bounds, 0 0 0
588 "signature length %d > max size %d",
589 s->signature_length, max_size);
590 }
591 }
592 }
593 0 return simage_ok;
594 }
595  
596 simage_status_t
597 0 simage_fill_from_signature (simage_t * s, char * signature, int signature_length)
598 {
599 // Cell number
600 int c;
601 // Offset into signature.
602 int o;
603 0 ▶ CALL (simage_allocate_signature (s, signature_length));
Branch: 2 branches
BranchTrueFalseTotal
CALL (simage_allocate_signature (s, signature_length)); 0 0 0
CALL (simage_allocate_signature (s, signature_length)); 0 0 0
604 0 s->signature_length = signature_length;
605 0 o = 0;
606 0 ▶ for (c = 0; c < SIZE * SIZE; c++) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < SIZE * SIZE; c++) { 0 0 0
607 /* The direction. */
608 int d;
609 int x;
610 int y;
611 0 ▶ CALL (entry_to_x_y (c, & x, & y));
Branch: 2 branches
BranchTrueFalseTotal
CALL (entry_to_x_y (c, & x, & y)); 0 0 0
CALL (entry_to_x_y (c, & x, & y)); 0 0 0
612 0 ▶ for (d = 0; d < DIRECTIONS; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < DIRECTIONS; d++) { 0 0 0
613 0 ▶ if (inside (c, d)) {
Branch: 1 branch
BranchTrueFalseTotal
if (inside (c, d)) { 0 0 0
614 //printf ("%d %d %d\n", c, d, o);
615 /* The value of this cell. */
616 int value;
617 0 ▶ FAIL (o >= signature_length, bounds,
Branch: 2 branches
BranchTrueFalseTotal
FAIL (o >= signature_length, bounds, 0 0 0
FAIL (o >= signature_length, bounds, 0 0 0
618 "offset %d exceeded signature length %d.\n",
619 o, signature_length);
620 0 value = signature[o] - '0' - 2;
621 0 s->signature[o] = signature[o];
622 0 o++;
623 0 s->grid[c].d[d] = value;
624 }
625 }
626 }
627 0 s->signature[s->signature_length] = '\0';
628 0 return simage_ok;
629 }
« Similar.xs ↑ Summary