| 1 |
/* |
| 2 |
* smooth.c - pre-filtering bitmap before splitting |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "../base/mdjvucfg.h" |
| 6 |
#include <minidjvu/minidjvu.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <stdio.h> |
| 10 |
|
| 11 |
/* all input rows must be 0-or-1 unpacked */ |
| 12 |
static void smooth_row(unsigned char *r, /* result */ |
| 13 |
unsigned char *u, /* upper row */ |
| 14 |
unsigned char *t, /* this row - must have margin 1 */ |
| 15 |
unsigned char *l, /* lower row */ |
| 16 |
int32 n) |
| 17 |
{ |
| 18 |
int32 i; |
| 19 |
for (i = 0; i < n; i++) |
| 20 |
{ |
| 21 |
int score = u[i] + l[i] + t[i-1] + t[i+1] - 2; |
| 22 |
|
| 23 |
/* if (score > 0) |
| 24 |
r[i] = 1; |
| 25 |
else */ |
| 26 |
/* clearing black pixels doesn't look cool, alas */ |
| 27 |
|
| 28 |
if (score < 0) |
| 29 |
r[i] = 0; |
| 30 |
else |
| 31 |
r[i] = t[i]; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
MDJVU_IMPLEMENT void mdjvu_smooth(mdjvu_bitmap_t b) |
| 37 |
{ |
| 38 |
int32 w = mdjvu_bitmap_get_width(b); |
| 39 |
int32 h = mdjvu_bitmap_get_height(b); |
| 40 |
int32 i; |
| 41 |
unsigned char *u, *t, *l, *r; |
| 42 |
|
| 43 |
if (h < 3) return; |
| 44 |
|
| 45 |
u = (unsigned char *) calloc(w + 2, 1) + 1; /* upper row */ |
| 46 |
t = (unsigned char *) calloc(w + 2, 1) + 1; /* this row */ |
| 47 |
l = (unsigned char *) calloc(w + 2, 1) + 1; /* lower row */ |
| 48 |
r = (unsigned char *) malloc(w); /* result */ |
| 49 |
|
| 50 |
mdjvu_bitmap_unpack_row_0_or_1(b, l, 0); |
| 51 |
for (i = 0; i < h; i++) |
| 52 |
{ |
| 53 |
unsigned char *tmp = u; |
| 54 |
u = t; |
| 55 |
t = l; |
| 56 |
l = tmp; |
| 57 |
|
| 58 |
if (i + 1 < h) |
| 59 |
mdjvu_bitmap_unpack_row_0_or_1(b, l, i + 1); |
| 60 |
else |
| 61 |
memset(l, 0, w); |
| 62 |
|
| 63 |
smooth_row(r, u, t, l, w); |
| 64 |
mdjvu_bitmap_pack_row(b, r, i); |
| 65 |
} |
| 66 |
|
| 67 |
free(u - 1); |
| 68 |
free(t - 1); |
| 69 |
free(l - 1); |
| 70 |
free(r); |
| 71 |
} |