| 1 |
/* |
| 2 |
* patterns.c - pattern matching algorithm |
| 3 |
*/ |
| 4 |
|
| 5 |
/* This is `patterns.c', the unit that handles pattern matching. |
| 6 |
* Its task is only to compare pairs of images, not to classify a set of them. |
| 7 |
* And this has absolutely nothing to do with choosing a cross-coding prototype. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "../base/mdjvucfg.h" |
| 11 |
#include <minidjvu/minidjvu.h> |
| 12 |
#include "bitmaps.h" |
| 13 |
#include <stdlib.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <string.h> |
| 16 |
#include <assert.h> |
| 17 |
#include <math.h> |
| 18 |
|
| 19 |
|
| 20 |
#define TIMES_TO_THIN 1 |
| 21 |
#define TIMES_TO_THICKEN 1 |
| 22 |
|
| 23 |
|
| 24 |
#define SIGNATURE_SIZE 32 |
| 25 |
|
| 26 |
|
| 27 |
typedef struct |
| 28 |
{ |
| 29 |
double pithdiff1_threshold; |
| 30 |
double pithdiff2_threshold; |
| 31 |
double shiftdiff1_threshold; |
| 32 |
double shiftdiff2_threshold; |
| 33 |
double shiftdiff3_threshold; |
| 34 |
int aggression; |
| 35 |
int method; |
| 36 |
} Options; |
| 37 |
|
| 38 |
/* These are hand-tweaked parameters of this classifier. */ |
| 39 |
|
| 40 |
static const double pithdiff1_veto_threshold = 23; |
| 41 |
static const double pithdiff2_veto_threshold = 4; |
| 42 |
static const double shiftdiff1_veto_threshold = 1000; |
| 43 |
static const double shiftdiff2_veto_threshold = 1500; |
| 44 |
static const double shiftdiff3_veto_threshold = 2000; |
| 45 |
|
| 46 |
static const double size_difference_threshold = 10; |
| 47 |
static const double mass_difference_threshold = 15; |
| 48 |
|
| 49 |
static const double shiftdiff1_falloff = .9; |
| 50 |
static const double shiftdiff2_falloff = 1; |
| 51 |
static const double shiftdiff3_falloff = 1.15; |
| 52 |
|
| 53 |
static void interpolate(Options *opt, const double *v1, const double *v2, |
| 54 |
int l, int r, int x) |
| 55 |
{ |
| 56 |
double w1 = ((double)(r - x)) / (r - l); /* weights */ |
| 57 |
double w2 = 1 - w1; |
| 58 |
opt->pithdiff1_threshold = v1[0] * w1 + v2[0] * w2; |
| 59 |
opt->pithdiff2_threshold = v1[1] * w1 + v2[1] * w2; |
| 60 |
opt->shiftdiff1_threshold = v1[2] * w1 + v2[2] * w2; |
| 61 |
opt->shiftdiff2_threshold = v1[3] * w1 + v2[3] * w2; |
| 62 |
opt->shiftdiff3_threshold = v1[4] * w1 + v2[4] * w2; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
/* Sets `aggression' for pattern matching. |
| 67 |
* Lower values are safer, bigger values produce smaller files. |
| 68 |
*/ |
| 69 |
|
| 70 |
MDJVU_IMPLEMENT void mdjvu_set_aggression(mdjvu_matcher_options_t opt, int level) |
| 71 |
{ |
| 72 |
const double set200[5] = {9, 1.2, 70, 90, 180}; |
| 73 |
const double set120[5] = {5, .2, 50, 70, 150}; |
| 74 |
const double set0[5] = {0, 0, 0, 0, 0}; |
| 75 |
|
| 76 |
if (level < 0) level = 0; |
| 77 |
|
| 78 |
((Options *) opt)->aggression = level; |
| 79 |
|
| 80 |
if (level > 120) |
| 81 |
interpolate((Options *) opt, set120, set200, 120, 200, level); |
| 82 |
else |
| 83 |
interpolate((Options *) opt, set0, set120, 0, 120, level); |
| 84 |
} |
| 85 |
|
| 86 |
/* ========================================================================== */ |
| 87 |
|
| 88 |
MDJVU_IMPLEMENT mdjvu_matcher_options_t mdjvu_matcher_options_create(void) |
| 89 |
{ |
| 90 |
mdjvu_matcher_options_t options = (mdjvu_matcher_options_t) MALLOC1(Options); |
| 91 |
mdjvu_init(); |
| 92 |
mdjvu_set_aggression(options, 100); |
| 93 |
((Options *) options)->method = 0; |
| 94 |
return options; |
| 95 |
} |
| 96 |
|
| 97 |
MDJVU_IMPLEMENT void mdjvu_use_matcher_method(mdjvu_matcher_options_t opt, int method) |
| 98 |
{ |
| 99 |
((Options *) opt)->method |= method; |
| 100 |
} |
| 101 |
|
| 102 |
MDJVU_IMPLEMENT void mdjvu_matcher_options_destroy(mdjvu_matcher_options_t opt) |
| 103 |
{ |
| 104 |
FREE((Options *) opt); |
| 105 |
} |
| 106 |
|
| 107 |
/* ========================================================================== */ |
| 108 |
|
| 109 |
typedef unsigned char byte; |
| 110 |
|
| 111 |
typedef struct ComparableImageData |
| 112 |
{ |
| 113 |
byte **pixels; /* 0 - purely white, 255 - purely black (inverse to PGM!) */ |
| 114 |
byte **pith2_inner; |
| 115 |
byte **pith2_outer; |
| 116 |
int32 width, height, mass; |
| 117 |
int32 mass_center_x, mass_center_y; |
| 118 |
byte signature[SIGNATURE_SIZE]; /* for shiftdiff 1 and 3 tests */ |
| 119 |
byte signature2[SIGNATURE_SIZE]; /* for shiftdiff 2 test */ |
| 120 |
} Image; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
/* Each image pair undergoes simple tests (dimensions and mass) |
| 125 |
* and at most five more advanced tests. |
| 126 |
* Each test may end up with three outcomes: veto (-1), doubt (0) and match(1). |
| 127 |
* Images are equivalent if and only if |
| 128 |
* there was no `veto' |
| 129 |
* and there was at least one `match'. |
| 130 |
*/ |
| 131 |
|
| 132 |
|
| 133 |
/* We check whether images' dimensions are different |
| 134 |
* no more than by size_difference_threshold percent. |
| 135 |
* Return value is usual: veto (-1) or doubt (0). |
| 136 |
* Mass checking was introduced by Leon Bottou. |
| 137 |
*/ |
| 138 |
|
| 139 |
static int simple_tests(Image *i1, Image *i2) |
| 140 |
{ |
| 141 |
int32 w1 = i1->width, h1 = i1->height, m1 = i1->mass; |
| 142 |
int32 w2 = i2->width, h2 = i2->height, m2 = i2->mass; |
| 143 |
|
| 144 |
if (100.* w1 > (100.+ size_difference_threshold) * w2) return -1; |
| 145 |
if (100.* w2 > (100.+ size_difference_threshold) * w1) return -1; |
| 146 |
if (100.* h1 > (100.+ size_difference_threshold) * h2) return -1; |
| 147 |
if (100.* h2 > (100.+ size_difference_threshold) * h1) return -1; |
| 148 |
if (100.* m1 > (100.+ mass_difference_threshold) * m2) return -1; |
| 149 |
if (100.* m2 > (100.+ mass_difference_threshold) * m1) return -1; |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
#define USE_PITHDIFF 1 |
| 156 |
#define USE_SHIFTDIFF_1 1 |
| 157 |
#define USE_SHIFTDIFF_2 1 |
| 158 |
#define USE_SHIFTDIFF_3 1 |
| 159 |
|
| 160 |
|
| 161 |
/* Computing distance by comparing pixels {{{ */ |
| 162 |
|
| 163 |
/* This function compares two images pixel by pixel. |
| 164 |
* The exact way to compare pixels is defined by two functions, |
| 165 |
* compare_row and compare_with_white. |
| 166 |
* Both functions take pointers to byte rows and their length. |
| 167 |
* |
| 168 |
* Now images are aligned by mass centers. |
| 169 |
* Code needs some clarification, yes... |
| 170 |
*/ |
| 171 |
static int32 distance_by_pixeldiff_functions_by_shift(Image *i1, Image *i2, |
| 172 |
int32 (*compare_row)(byte *, byte *, int32), |
| 173 |
int32 (*compare_1_with_white)(byte *, int32), |
| 174 |
int32 (*compare_2_with_white)(byte *, int32), |
| 175 |
int32 ceiling, |
| 176 |
int32 shift_x, int32 shift_y) /* of i1's coordinate system with respect to i2 */ |
| 177 |
{ |
| 178 |
int32 w1 = i1->width, w2 = i2->width, h1 = i1->height, h2 = i2->height; |
| 179 |
int32 min_y = shift_y < 0 ? shift_y : 0; |
| 180 |
int32 right1 = shift_x + w1; |
| 181 |
int32 max_y_plus_1 = h2 > shift_y + h1 ? h2 : shift_y + h1; |
| 182 |
int32 i; |
| 183 |
int32 min_overlap_x = shift_x > 0 ? shift_x : 0; |
| 184 |
int32 max_overlap_x_plus_1 = w2 < right1 ? w2 : right1; |
| 185 |
int32 min_overlap_x_for_i1 = min_overlap_x - shift_x; |
| 186 |
int32 max_overlap_x_plus_1_for_i1 = max_overlap_x_plus_1 - shift_x; |
| 187 |
int32 overlap_length = max_overlap_x_plus_1 - min_overlap_x; |
| 188 |
int32 score = 0; |
| 189 |
|
| 190 |
if (overlap_length <= 0) return INT32_MAX; |
| 191 |
|
| 192 |
for (i = min_y; i < max_y_plus_1; i++) |
| 193 |
{ |
| 194 |
int32 y1 = i - shift_y; |
| 195 |
|
| 196 |
/* calculate difference in the i-th line */ |
| 197 |
|
| 198 |
if (i < 0 || i >= h2) |
| 199 |
{ |
| 200 |
/* calculate difference of i1 with white */ |
| 201 |
score += compare_1_with_white(i1->pixels[y1], w1); |
| 202 |
} |
| 203 |
else if (i < shift_y || i >= shift_y + h1) |
| 204 |
{ |
| 205 |
/* calculate difference of i2 with white */ |
| 206 |
score += compare_2_with_white(i2->pixels[i], w2); |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
/* calculate difference in a line where the bitmaps overlap */ |
| 211 |
score += compare_row(i1->pixels[y1] + min_overlap_x_for_i1, |
| 212 |
i2->pixels[i] + min_overlap_x, |
| 213 |
overlap_length); |
| 214 |
|
| 215 |
|
| 216 |
/* calculate penalty for the left margin */ |
| 217 |
if (min_overlap_x > 0) |
| 218 |
score += compare_2_with_white(i2->pixels[i], min_overlap_x); |
| 219 |
else |
| 220 |
score += compare_1_with_white(i1->pixels[y1], min_overlap_x_for_i1); |
| 221 |
|
| 222 |
/* calculate penalty for the right margin */ |
| 223 |
if (max_overlap_x_plus_1 < w2) |
| 224 |
{ |
| 225 |
score += compare_2_with_white( |
| 226 |
i2->pixels[i] + max_overlap_x_plus_1, |
| 227 |
w2 - max_overlap_x_plus_1); |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
score += compare_1_with_white( |
| 232 |
i1->pixels[y1] + max_overlap_x_plus_1_for_i1, |
| 233 |
w1 - max_overlap_x_plus_1_for_i1); |
| 234 |
|
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if (score >= ceiling) return INT32_MAX; |
| 239 |
} |
| 240 |
return score; |
| 241 |
} |
| 242 |
|
| 243 |
static int32 distance_by_pixeldiff_functions(Image *i1, Image *i2, |
| 244 |
int32 (*compare_row)(byte *, byte *, int32), |
| 245 |
int32 (*compare_1_with_white)(byte *, int32), |
| 246 |
int32 (*compare_2_with_white)(byte *, int32), |
| 247 |
int32 ceiling) |
| 248 |
{ |
| 249 |
byte **p1, **p2; |
| 250 |
int32 w1, w2, h1, h2; |
| 251 |
int32 shift_x, shift_y; /* of i1's coordinate system with respect to i2 */ |
| 252 |
/*int32 s = 0, i, i_start, i_cap; |
| 253 |
int32 right_margin_start, right_margin_width;*/ |
| 254 |
|
| 255 |
/* make i1 to be narrower than i2 */ |
| 256 |
if (i1->width > i2->width) |
| 257 |
{ |
| 258 |
Image *img = i1; |
| 259 |
i1 = i2; |
| 260 |
i2 = img; |
| 261 |
} |
| 262 |
|
| 263 |
w1 = i1->width; h1 = i1->height; p1 = i1->pixels; |
| 264 |
w2 = i2->width; h2 = i2->height; p2 = i2->pixels; |
| 265 |
|
| 266 |
/* (shift_x, shift_y) */ |
| 267 |
/* is what should be added to i1's coordinates to get i2's coordinates. */ |
| 268 |
shift_x = (w2 - w2/2) - (w1 - w1/2); /* center favors right */ |
| 269 |
shift_y = h2/2 - h1/2; /* center favors top */ |
| 270 |
|
| 271 |
shift_x = i2->mass_center_x - i1->mass_center_x; |
| 272 |
if (shift_x < 0) |
| 273 |
shift_x = (shift_x - MDJVU_CENTER_QUANT / 2) / MDJVU_CENTER_QUANT; |
| 274 |
else |
| 275 |
shift_x = (shift_x + MDJVU_CENTER_QUANT / 2) / MDJVU_CENTER_QUANT; |
| 276 |
|
| 277 |
shift_y = i2->mass_center_y - i1->mass_center_y; |
| 278 |
if (shift_y < 0) |
| 279 |
shift_y = (shift_y - MDJVU_CENTER_QUANT / 2) / MDJVU_CENTER_QUANT; |
| 280 |
else |
| 281 |
shift_y = (shift_y + MDJVU_CENTER_QUANT / 2) / MDJVU_CENTER_QUANT; |
| 282 |
|
| 283 |
return distance_by_pixeldiff_functions_by_shift( |
| 284 |
i1, i2, compare_row, compare_1_with_white, compare_2_with_white, |
| 285 |
ceiling, shift_x, shift_y); |
| 286 |
} |
| 287 |
|
| 288 |
/* Computing distance by comparing pixels }}} */ |
| 289 |
/* inscribed framework penalty counting {{{ */ |
| 290 |
|
| 291 |
/* (Look at `frames.c' to see what it's all about) */ |
| 292 |
|
| 293 |
#if USE_PITHDIFF |
| 294 |
|
| 295 |
/* If the framework of one letter is inscribed into another and vice versa, |
| 296 |
* then those letters are probably equivalent. |
| 297 |
* That's the idea... |
| 298 |
* Counting penalty points here for any pixel |
| 299 |
* that's framework in one image and white in the other. |
| 300 |
*/ |
| 301 |
|
| 302 |
static int32 pithdiff_compare_row(byte *row1, byte *row2, int32 n) |
| 303 |
{ |
| 304 |
int32 i, s = 0; |
| 305 |
for (i = 0; i < n; i++) |
| 306 |
{ |
| 307 |
int32 k = row1[i], l = row2[i]; |
| 308 |
if (k == 255) |
| 309 |
s += 255 - l; |
| 310 |
else if (l == 255) |
| 311 |
s += 255 - k; |
| 312 |
} |
| 313 |
return s; |
| 314 |
} |
| 315 |
|
| 316 |
static int32 pithdiff_compare_with_white(byte *row, int32 n) |
| 317 |
{ |
| 318 |
int32 i, s = 0; |
| 319 |
for (i = 0; i < n; i++) if (row[i] == 255) s += 255; |
| 320 |
return s; |
| 321 |
} |
| 322 |
|
| 323 |
static int32 pithdiff_distance(Image *i1, Image *i2, int32 ceiling) |
| 324 |
{ |
| 325 |
return distance_by_pixeldiff_functions(i1, i2, |
| 326 |
&pithdiff_compare_row, |
| 327 |
&pithdiff_compare_with_white, |
| 328 |
&pithdiff_compare_with_white, |
| 329 |
ceiling); |
| 330 |
} |
| 331 |
|
| 332 |
static int pithdiff_equivalence(Image *i1, Image *i2, double threshold, int32 dpi) |
| 333 |
{ |
| 334 |
int32 perimeter = i1->width + i1->height + i2->width + i2->height; |
| 335 |
int32 ceiling = (int32) (pithdiff1_veto_threshold * dpi * perimeter / 100); |
| 336 |
int32 d = pithdiff_distance(i1, i2, ceiling); |
| 337 |
if (d == INT32_MAX) return -1; |
| 338 |
if (d < threshold * dpi * perimeter / 100) return 1; |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
#endif /* if USE_PITHDIFF */ |
| 343 |
|
| 344 |
/* inscribed framework penalty counting }}} */ |
| 345 |
|
| 346 |
/* shift signature comparison {{{ */ |
| 347 |
|
| 348 |
/* Just finding the square of a normal Euclidean distance between vectors |
| 349 |
* (but with falloff) |
| 350 |
*/ |
| 351 |
|
| 352 |
#if USE_SHIFTDIFF_1 || USE_SHIFTDIFF_2 || USE_SHIFTDIFF_3 |
| 353 |
static int shiftdiff_equivalence(byte *s1, byte *s2, double falloff, double veto, double threshold) |
| 354 |
{ |
| 355 |
int i, delay_before_falloff = 1, delay_counter = 1; |
| 356 |
double penalty = 0; |
| 357 |
double weight = 1; |
| 358 |
|
| 359 |
for (i = 1; i < SIGNATURE_SIZE; i++) /* kluge: ignores the first byte */ |
| 360 |
{ |
| 361 |
int difference = s1[i] - s2[i]; |
| 362 |
penalty += difference * difference * weight; |
| 363 |
if (!--delay_counter) |
| 364 |
{ |
| 365 |
weight *= falloff; |
| 366 |
delay_counter = delay_before_falloff <<= 1; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
if (penalty >= veto * SIGNATURE_SIZE) return -1; |
| 371 |
if (penalty <= threshold * SIGNATURE_SIZE) return 1; |
| 372 |
return 0; |
| 373 |
} |
| 374 |
#endif |
| 375 |
/* shift signature comparison }}} */ |
| 376 |
|
| 377 |
#ifndef NO_MINIDJVU |
| 378 |
mdjvu_pattern_t mdjvu_pattern_create(mdjvu_matcher_options_t opt, mdjvu_bitmap_t bitmap) |
| 379 |
{ |
| 380 |
/* not calling mdjvu_init() since we already have a bitmap */ |
| 381 |
int32 w = mdjvu_bitmap_get_width(bitmap); |
| 382 |
int32 h = mdjvu_bitmap_get_height(bitmap); |
| 383 |
mdjvu_pattern_t pattern; |
| 384 |
byte **pixels = mdjvu_create_2d_array(w, h); |
| 385 |
mdjvu_bitmap_unpack_all(bitmap, pixels); |
| 386 |
pattern = mdjvu_pattern_create_from_array(opt, pixels, w, h); |
| 387 |
mdjvu_destroy_2d_array(pixels); |
| 388 |
return pattern; |
| 389 |
} |
| 390 |
#endif |
| 391 |
|
| 392 |
/* Finding mass center {{{ */ |
| 393 |
|
| 394 |
static void get_mass_center(unsigned char **pixels, int32 w, int32 h, |
| 395 |
int32 *pmass_center_x, int32 *pmass_center_y) |
| 396 |
{ |
| 397 |
double x_sum = 0, y_sum = 0, mass = 0; |
| 398 |
int32 i, j; |
| 399 |
|
| 400 |
for (i = 0; i < h; i++) |
| 401 |
{ |
| 402 |
unsigned char *row = pixels[i]; |
| 403 |
for (j = 0; j < w; j++) |
| 404 |
{ |
| 405 |
unsigned char pixel = row[j]; |
| 406 |
x_sum += pixel * j; |
| 407 |
y_sum += pixel * i; |
| 408 |
mass += pixel; |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
*pmass_center_x = (int32) (x_sum * MDJVU_CENTER_QUANT / mass); |
| 413 |
*pmass_center_y = (int32) (y_sum * MDJVU_CENTER_QUANT / mass); |
| 414 |
} |
| 415 |
|
| 416 |
/* Finding mass center }}} */ |
| 417 |
|
| 418 |
|
| 419 |
/* get a center (in 1/MDJVU_CENTER_QUANT pixels; defined in the header for image) */ |
| 420 |
MDJVU_IMPLEMENT void mdjvu_pattern_get_center(mdjvu_pattern_t p, int32 *cx, int32 *cy) |
| 421 |
{ |
| 422 |
*cx = ((Image *) p)->mass_center_x; |
| 423 |
*cy = ((Image *) p)->mass_center_y; |
| 424 |
} |
| 425 |
|
| 426 |
static void sweep(unsigned char **pixels, unsigned char **source, int w, int h) |
| 427 |
{ |
| 428 |
int x, y; |
| 429 |
|
| 430 |
for (y = 0; y < h; y++) |
| 431 |
{ |
| 432 |
unsigned char *row = pixels[y]; |
| 433 |
unsigned char *srow = source[y]; |
| 434 |
unsigned char *supper = source[y-1]; |
| 435 |
unsigned char *slower = source[y+1]; |
| 436 |
|
| 437 |
for (x = 0; x < w; x++) |
| 438 |
{ |
| 439 |
row[x] = ( supper[x] | |
| 440 |
srow[x-1] | srow[x] | srow[x+1] | |
| 441 |
slower[x] ); |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
static unsigned char **quick_thin(unsigned char **pixels, int w, int h, int N) |
| 447 |
{ |
| 448 |
unsigned char **aux = provide_margins(pixels, w, h, 1); |
| 449 |
unsigned char **buf = allocate_bitmap_with_white_margins(w, h); |
| 450 |
|
| 451 |
clear_bitmap(buf, w, h); |
| 452 |
invert_bitmap(aux, w, h, 0); |
| 453 |
|
| 454 |
while (N--) |
| 455 |
{ |
| 456 |
sweep(buf, aux, w, h); |
| 457 |
assign_bitmap(aux, buf, w, h); |
| 458 |
} |
| 459 |
|
| 460 |
invert_bitmap(buf, w, h, 0); |
| 461 |
|
| 462 |
free_bitmap_with_margins(aux); |
| 463 |
return buf; |
| 464 |
} |
| 465 |
|
| 466 |
static unsigned char **quick_thicken(unsigned char **pixels, int w, int h, int N) |
| 467 |
{ |
| 468 |
int r_w = w + N * 2; |
| 469 |
int r_h = h + N * 2; |
| 470 |
int y, x; |
| 471 |
unsigned char **aux = allocate_bitmap_with_white_margins(r_w, r_h); |
| 472 |
unsigned char **buf = allocate_bitmap_with_white_margins(r_w, r_h); |
| 473 |
|
| 474 |
clear_bitmap(buf, r_w, r_h); |
| 475 |
clear_bitmap(aux, r_w, r_h); |
| 476 |
for (y = 0; y < h; y++) for (x = 0; x < w; x++) |
| 477 |
{ |
| 478 |
aux[y + N][x + N] = pixels[y][x]; |
| 479 |
} |
| 480 |
|
| 481 |
while (N--) |
| 482 |
{ |
| 483 |
sweep(buf, aux, r_w, r_h); |
| 484 |
assign_bitmap(aux, buf, r_w, r_h); |
| 485 |
} |
| 486 |
|
| 487 |
free_bitmap_with_margins(aux); |
| 488 |
return buf; |
| 489 |
} |
| 490 |
|
| 491 |
MDJVU_IMPLEMENT mdjvu_pattern_t mdjvu_pattern_create_from_array(mdjvu_matcher_options_t m_opt, byte **pixels, int32 w, int32 h)/*{{{*/ |
| 492 |
{ |
| 493 |
Options *opt = (Options *) m_opt; |
| 494 |
int32 i, mass; |
| 495 |
Image *img = MALLOC1(Image); |
| 496 |
|
| 497 |
mdjvu_init(); |
| 498 |
|
| 499 |
img->width = w; |
| 500 |
img->height = h; |
| 501 |
|
| 502 |
img->pixels = allocate_bitmap(w, h); |
| 503 |
memset(img->pixels[0], 0, w * h); |
| 504 |
|
| 505 |
mass = 0; |
| 506 |
for (i = 0; i < h; i++) |
| 507 |
{ |
| 508 |
int32 j; |
| 509 |
for (j = 0; j < w; j++) |
| 510 |
if (pixels[i][j]) |
| 511 |
{ |
| 512 |
img->pixels[i][j] = 255; /* i don't remember what for */ |
| 513 |
mass += 1; |
| 514 |
} |
| 515 |
} |
| 516 |
img->mass = mass; |
| 517 |
|
| 518 |
mdjvu_soften_pattern(img->pixels, img->pixels, w, h); |
| 519 |
|
| 520 |
get_mass_center(img->pixels, w, h, |
| 521 |
&img->mass_center_x, &img->mass_center_y); |
| 522 |
mdjvu_get_gray_signature(img->pixels, w, h, |
| 523 |
img->signature, SIGNATURE_SIZE); |
| 524 |
|
| 525 |
mdjvu_get_black_and_white_signature(img->pixels, w, h, |
| 526 |
img->signature2, SIGNATURE_SIZE); |
| 527 |
|
| 528 |
if (!opt->aggression) |
| 529 |
{ |
| 530 |
free_bitmap(img->pixels); |
| 531 |
img->pixels = NULL; |
| 532 |
} |
| 533 |
|
| 534 |
if (opt->method & MDJVU_MATCHER_PITH_2) |
| 535 |
{ |
| 536 |
img->pith2_inner = quick_thin(pixels, w, h, TIMES_TO_THIN); |
| 537 |
img->pith2_outer = quick_thicken(pixels, w, h, TIMES_TO_THICKEN); |
| 538 |
assert(img->pith2_inner); |
| 539 |
assert(img->pith2_outer); |
| 540 |
} |
| 541 |
else |
| 542 |
{ |
| 543 |
img->pith2_inner = NULL; |
| 544 |
img->pith2_outer = NULL; |
| 545 |
} |
| 546 |
|
| 547 |
return (mdjvu_pattern_t) img; |
| 548 |
}/*}}}*/ |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
static int32 pith2_row_subset(byte *A, byte *B, int32 length) |
| 553 |
{ |
| 554 |
int32 i, s = 0; |
| 555 |
for (i = 0; i < length; i++) |
| 556 |
{ |
| 557 |
if (A[i] & !B[i]) |
| 558 |
s += 255; |
| 559 |
} |
| 560 |
return s; |
| 561 |
} |
| 562 |
|
| 563 |
static int32 pith2_row_has_black(byte *row, int32 length) |
| 564 |
{ |
| 565 |
int32 i, s = 0; |
| 566 |
for (i = 0; i < length; i++) |
| 567 |
{ |
| 568 |
if (row[i]) |
| 569 |
s += 255; |
| 570 |
} |
| 571 |
return s; |
| 572 |
} |
| 573 |
|
| 574 |
static int32 pith2_return_0(byte *A, int32 length) |
| 575 |
{ |
| 576 |
return 0; |
| 577 |
} |
| 578 |
|
| 579 |
static int pith2_is_subset(mdjvu_pattern_t ptr1, mdjvu_pattern_t ptr2, double threshold, int32 dpi) |
| 580 |
{ |
| 581 |
Image *i1 = (Image *) ptr1; |
| 582 |
Image *i2 = (Image *) ptr2; |
| 583 |
Image ptr1_inner; |
| 584 |
Image ptr2_outer; |
| 585 |
int32 perimeter = i1->width + i1->height + i2->width + i2->height; |
| 586 |
int32 ceiling = (int32) (pithdiff2_veto_threshold * dpi * perimeter / 100); |
| 587 |
int32 d = 0; |
| 588 |
|
| 589 |
ptr1_inner.pixels = i1->pith2_inner; |
| 590 |
assert(i1->pith2_inner); |
| 591 |
ptr1_inner.width = i1->width; |
| 592 |
ptr1_inner.height = i1->height; |
| 593 |
ptr1_inner.mass_center_x = i1->mass_center_x; |
| 594 |
ptr1_inner.mass_center_y = i1->mass_center_y; |
| 595 |
|
| 596 |
ptr2_outer.pixels = i2->pith2_outer; |
| 597 |
assert(i2->pith2_outer); |
| 598 |
ptr2_outer.width = i2->width + TIMES_TO_THICKEN*2; |
| 599 |
ptr2_outer.height = i2->height + TIMES_TO_THICKEN*2; |
| 600 |
ptr2_outer.mass_center_x = i2->mass_center_x + MDJVU_CENTER_QUANT; |
| 601 |
ptr2_outer.mass_center_y = i2->mass_center_y + MDJVU_CENTER_QUANT; |
| 602 |
|
| 603 |
d = distance_by_pixeldiff_functions(&ptr1_inner, &ptr2_outer, |
| 604 |
&pith2_row_subset, |
| 605 |
&pith2_row_has_black, |
| 606 |
&pith2_return_0, |
| 607 |
ceiling); |
| 608 |
|
| 609 |
if (d == INT32_MAX) return -1; |
| 610 |
else if (d < threshold * dpi * perimeter / 100) return 1; |
| 611 |
return 0; |
| 612 |
} |
| 613 |
|
| 614 |
/* Requires `opt' to be non-NULL */ |
| 615 |
static int compare_patterns(mdjvu_pattern_t ptr1, mdjvu_pattern_t ptr2,/*{{{*/ |
| 616 |
int32 dpi, Options *opt) |
| 617 |
|
| 618 |
{ |
| 619 |
Image *i1 = (Image *) ptr1, *i2 = (Image *) ptr2; |
| 620 |
int i, state = 0; /* 0 - unsure, 1 - equal unless veto */ |
| 621 |
|
| 622 |
if (simple_tests(i1, i2)) return -1; |
| 623 |
|
| 624 |
#if USE_SHIFTDIFF_1 |
| 625 |
i = shiftdiff_equivalence(i1->signature, i2->signature, |
| 626 |
shiftdiff1_falloff, shiftdiff1_veto_threshold, opt->shiftdiff1_threshold); |
| 627 |
if (i == -1) return -1; |
| 628 |
state |= i; |
| 629 |
#endif |
| 630 |
|
| 631 |
#if USE_SHIFTDIFF_2 |
| 632 |
i = shiftdiff_equivalence(i1->signature2, i2->signature2, |
| 633 |
shiftdiff2_falloff, shiftdiff2_veto_threshold, opt->shiftdiff2_threshold); |
| 634 |
if (i == -1) return -1; |
| 635 |
state |= i; |
| 636 |
#endif |
| 637 |
|
| 638 |
#if USE_SHIFTDIFF_3 |
| 639 |
i = shiftdiff_equivalence(i1->signature, i2->signature, |
| 640 |
shiftdiff3_falloff, shiftdiff3_veto_threshold, opt->shiftdiff3_threshold); |
| 641 |
if (i == -1) return -1; |
| 642 |
state |= i; |
| 643 |
#endif |
| 644 |
|
| 645 |
i = pith2_is_subset(ptr1, ptr2, opt->pithdiff2_threshold, dpi); |
| 646 |
if (i < 1) return i; |
| 647 |
i = pith2_is_subset(ptr2, ptr1, opt->pithdiff2_threshold, dpi); |
| 648 |
if (i < 1) return i; |
| 649 |
|
| 650 |
if (opt->method & MDJVU_MATCHER_RAMPAGE) |
| 651 |
return 1; |
| 652 |
|
| 653 |
#if USE_PITHDIFF |
| 654 |
if (opt->aggression > 0) |
| 655 |
{ |
| 656 |
i = pithdiff_equivalence(i1, i2, opt->pithdiff1_threshold, dpi); |
| 657 |
if (i == -1) return 0; /* pithdiff has no right to veto at upper level */ |
| 658 |
state |= i; |
| 659 |
} |
| 660 |
#endif |
| 661 |
|
| 662 |
#if 0 |
| 663 |
if (opt->aggression > 0) |
| 664 |
{ |
| 665 |
i = softdiff_equivalence(i1, i2, opt->softdiff_threshold, dpi); |
| 666 |
if (i == -1) return 0; /* softdiff has no right to veto at upper level */ |
| 667 |
state |= i; |
| 668 |
} |
| 669 |
#endif |
| 670 |
|
| 671 |
return state; |
| 672 |
}/*}}}*/ |
| 673 |
|
| 674 |
MDJVU_IMPLEMENT int mdjvu_match_patterns(mdjvu_pattern_t ptr1, mdjvu_pattern_t ptr2, |
| 675 |
int32 dpi, mdjvu_matcher_options_t options) |
| 676 |
{ |
| 677 |
Options *opt; |
| 678 |
int result; |
| 679 |
if (options) |
| 680 |
opt = (Options *) options; |
| 681 |
else |
| 682 |
opt = (Options *) mdjvu_matcher_options_create(); |
| 683 |
|
| 684 |
result = compare_patterns(ptr1, ptr2, dpi, opt); |
| 685 |
|
| 686 |
if (!options) |
| 687 |
mdjvu_matcher_options_destroy((mdjvu_matcher_options_t) opt); |
| 688 |
|
| 689 |
return result; |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
MDJVU_IMPLEMENT void mdjvu_pattern_destroy(mdjvu_pattern_t p)/*{{{*/ |
| 694 |
{ |
| 695 |
Image *img = (Image *) p; |
| 696 |
if (img->pixels) |
| 697 |
free_bitmap(img->pixels); |
| 698 |
|
| 699 |
if (img->pith2_inner) |
| 700 |
free_bitmap_with_margins(img->pith2_inner); |
| 701 |
|
| 702 |
if (img->pith2_outer) |
| 703 |
free_bitmap_with_margins(img->pith2_outer); |
| 704 |
|
| 705 |
FREE(img); |
| 706 |
}/*}}}*/ |