| 1 |
/* |
| 2 |
* average.c - computing average bitmap |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "../base/mdjvucfg.h" |
| 6 |
#include <minidjvu/minidjvu.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <assert.h> |
| 9 |
#include <stdio.h> |
| 10 |
|
| 11 |
MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_average(mdjvu_bitmap_t *bitmaps, |
| 12 |
int32 n, |
| 13 |
int32 *cx, int32 *cy) |
| 14 |
{ |
| 15 |
int32 i; |
| 16 |
int32 min_x = 0, min_y = 0, max_x_plus_1 = 0, max_y_plus_1 = 0; |
| 17 |
int32 *buf; |
| 18 |
int32 buf_w, buf_h; |
| 19 |
unsigned char *row; |
| 20 |
int32 tmp_x, tmp_y; |
| 21 |
int32 threshold = n / 2; |
| 22 |
mdjvu_bitmap_t result; |
| 23 |
|
| 24 |
if (n == 1) |
| 25 |
{ |
| 26 |
return mdjvu_bitmap_clone(bitmaps[0]); |
| 27 |
} |
| 28 |
|
| 29 |
for (i = 0; i < n; i++) |
| 30 |
{ |
| 31 |
int32 w = mdjvu_bitmap_get_width(bitmaps[i]); |
| 32 |
int32 h = mdjvu_bitmap_get_height(bitmaps[i]); |
| 33 |
int32 ncx = cx[i] / MDJVU_CENTER_QUANT; |
| 34 |
int32 ncy = cy[i] / MDJVU_CENTER_QUANT; |
| 35 |
|
| 36 |
assert(ncx >= 0 && ncx < w); |
| 37 |
assert(ncy >= 0 && ncy < h); |
| 38 |
|
| 39 |
if (-ncx < min_x) min_x = -ncx; |
| 40 |
if (-ncy < min_y) min_y = -ncy; |
| 41 |
if (w-ncx > max_x_plus_1) max_x_plus_1 = w-ncx; |
| 42 |
if (h-ncy > max_y_plus_1) max_y_plus_1 = h-ncy; |
| 43 |
} |
| 44 |
|
| 45 |
buf_w = max_x_plus_1 - min_x; |
| 46 |
buf_h = max_y_plus_1 - min_y; |
| 47 |
buf = (int32 *) calloc(buf_w * buf_h, sizeof(int32)); |
| 48 |
row = (unsigned char *) malloc(buf_w); |
| 49 |
|
| 50 |
/* Now adding the bitmaps to the buffer */ |
| 51 |
for (i = 0; i < n; i++) |
| 52 |
{ |
| 53 |
int32 w = mdjvu_bitmap_get_width(bitmaps[i]); |
| 54 |
int32 h = mdjvu_bitmap_get_height(bitmaps[i]); |
| 55 |
int32 sx = min_x + cx[i] / MDJVU_CENTER_QUANT, sy = min_y + cy[i] / MDJVU_CENTER_QUANT; |
| 56 |
int32 x, y; |
| 57 |
|
| 58 |
for (y = 0; y < h; y++) |
| 59 |
{ |
| 60 |
int32 *buf_row = buf + buf_w * (y - sy); |
| 61 |
mdjvu_bitmap_unpack_row(bitmaps[i], row, y); |
| 62 |
for (x = 0; x < w; x++) |
| 63 |
{ |
| 64 |
if (row[x]) |
| 65 |
buf_row[x - sx]++; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
result = mdjvu_bitmap_create(buf_w, buf_h); |
| 71 |
for (i = 0; i < buf_h; i++) |
| 72 |
{ |
| 73 |
int32 j; |
| 74 |
for (j = 0; j < buf_w; j++) |
| 75 |
{ |
| 76 |
row[j] = ( buf[i * buf_w + j] > threshold ? 1 : 0 ); |
| 77 |
} |
| 78 |
mdjvu_bitmap_pack_row(result, row, i); |
| 79 |
} |
| 80 |
|
| 81 |
mdjvu_bitmap_remove_margins(result, &tmp_x, &tmp_y); |
| 82 |
|
| 83 |
free(row); |
| 84 |
free(buf); |
| 85 |
|
| 86 |
return result; |
| 87 |
} |